区块链开发之确定性算法生成BTC,ETH的私钥,公钥和地址

引言

我最近封装了一个库,使用起来更简单,大家可以移步这里:Bip44确定性算法的android实现

首先要生成12个助记词,请看我上篇文章:区块链开发之生成12个助记词

使用的库

implementation ‘org.bitcoinj:bitcoinj-core:0.14.7’

implementation ‘org.web3j:core:3.3.1-android’

具体代码如下

public static ShellWallet generateWallet(int coinType, int index, String name) throws Exception {
        ShellWallet wallet = null;
        if (words == null || words.size() != 12) {
            throw new RuntimeException("please generateMnemonic first");
        } else {
            DeterministicSeed deterministicSeed = new DeterministicSeed(words, null, "", 0);
            DeterministicKeyChain deterministicKeyChain = DeterministicKeyChain.builder().seed(deterministicSeed).build();
            //拼接路径 path = "m/44'/60'/0'/0/0"
            String path = null;
            if (coinType == BTC_COIN) {
                path = "m/44'/0'/0'/0/" + index;
                NetworkParameters networkParameters = null;
                if (!BTC_TEST_NET) {
                    networkParameters = MainNetParams.get();
                } else {
                    networkParameters = TestNet3Params.get();
                    path = "m/44'/1'/0'/0/" + index;
                }
                BigInteger privkeybtc = deterministicKeyChain.getKeyByPath(parsePath(path), true).getPrivKey();
                ECKey ecKey = ECKey.fromPrivate(privkeybtc);

                //String publicKey = Numeric.toHexStringNoPrefixZeroPadded(new BigInteger(ecKey.getPubKey()), 66);
                //LogUtils.d("pub1:" + publicKey );
                String publicKey = ecKey.getPublicKeyAsHex();
                LogUtils.d("pub2:" + publicKey);
                String privateKey = ecKey.getPrivateKeyEncoded(networkParameters).toString();
                String address = ecKey.toAddress(networkParameters).toString();

                wallet = new ShellWallet(index, coinType, privateKey, publicKey, address, name);
                LogUtils.d(wallet.toString());
            } else if (coinType == ETH_COIN) {
                path = "m/44'/60'/0'/0/" + index;
                BigInteger privkeyeth = deterministicKeyChain.getKeyByPath(parsePath(path), true).getPrivKey();
                ECKeyPair ecKeyPair = ECKeyPair.create(privkeyeth);

                String publicKey = Numeric.toHexStringWithPrefix(ecKeyPair.getPublicKey());
                String privateKey = Numeric.toHexStringWithPrefix(ecKeyPair.getPrivateKey());
                String address = "0x" + Keys.getAddress(ecKeyPair);

                wallet = new ShellWallet(index, coinType, privateKey, publicKey, address, name);
                LogUtils.d(wallet.toString());
            }

        }

        return wallet;
    }
/**
     * The path is a human-friendly representation of the deterministic path. For example:
     * <p>
     * "m/44'/0'/0'/0/0"
     * <p>
     * Where a letter "'" means hardened key. Spaces are ignored.
     */
    public static List<ChildNumber> parsePath(@Nonnull String path) {
        String[] parsedNodes = path.replace("m", "").split("/");
        List<ChildNumber> nodes = new ArrayList<>();

        for (String n : parsedNodes) {
            n = n.replaceAll(" ", "");
            if (n.length() == 0) continue;
            boolean isHard = n.endsWith("'");
            if (isHard) n = n.substring(0, n.length() - 1);
            int nodeNumber = Integer.parseInt(n);
            nodes.add(new ChildNumber(nodeNumber, isHard));
        }

        return nodes;
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值