当前位置:   article > 正文

BIP32/BIP39/BIP44 HD Wallet_bip39 路径

bip39 路径
  1. import lombok.extern.slf4j.Slf4j;
  2. import org.bitcoinj.crypto.*;
  3. import org.bitcoinj.params.MainNetParams;
  4. import org.bitcoinj.wallet.DeterministicSeed;
  5. import org.junit.Test;
  6. import org.web3j.crypto.ECKeyPair;
  7. import org.web3j.crypto.Keys;
  8. import java.io.IOException;
  9. import java.security.SecureRandom;
  10. import java.util.List;
  11. /**
  12. * <p>
  13. *
  14. * </p>
  15. *
  16. * @author: flx
  17. * @date: 2018/8/28 16:52
  18. * @description: 生成mnemonic > 生成seed > 生成 Extended Public Key
  19. * 生成地址主要依赖Extended Public Key,加上addressIndex(0至232-1)就可以确定一个地址.
  20. * BTC使用m/44’/0’/0’/0的 Extended Public Key 生成 m/44’/0’/0’/0/*,
  21. * ETH使用m/44’/60’/0’/0的 Extended Public Key 生成 m/44’/60’/0’/0/*,
  22. * mainnet的Extended Public Key以xpub做前缀
  23. * 验证网址:https://iancoleman.io/bip39/
  24. */
  25. @Slf4j
  26. public class TestBip {
  27. private static final String C_BLANK1 = " ";
  28. private static final String PREFIX = "0x";
  29. private static final byte[] SEED = null;
  30. private static final String PASSPHRASE = "";
  31. private static final Long CREATIONTIMESECONDS = 0L;
  32. /**
  33. * TestNet3Params(公共测试网络)/RegTestParams(私有测试网络)/MainNetParams(生产网络)
  34. */
  35. private static final MainNetParams mainnetParams = MainNetParams.get();
  36. @Test
  37. public void TestBip44ETH() throws Exception {
  38. String wordList = this.getWordListString();
  39. wordList = "please promote sting series horn leave squirrel juice harsh over wash reduce";
  40. log.info("generate mnemonic code:[{}]", wordList);
  41. DeterministicSeed deterministicSeed = new DeterministicSeed(wordList, SEED, PASSPHRASE, CREATIONTIMESECONDS);
  42. log.info("BIP39 seed:{}", deterministicSeed.toHexString());
  43. /**生成根私钥 root private key*/
  44. DeterministicKey rootPrivateKey = HDKeyDerivation.createMasterPrivateKey(deterministicSeed.getSeedBytes());
  45. /**根私钥进行 priB58编码*/
  46. String priv = rootPrivateKey.serializePrivB58(mainnetParams);
  47. log.info("BIP32 extended private key:{}", priv);
  48. /**由根私钥生成HD钱包*/
  49. DeterministicHierarchy deterministicHierarchy = new DeterministicHierarchy(rootPrivateKey);
  50. /**定义父路径*/
  51. List<ChildNumber> parsePath = HDUtils.parsePath("44H/60H/0H");
  52. DeterministicKey accountKey0 = deterministicHierarchy.get(parsePath, true, true);
  53. log.info("Account extended private key:{}", accountKey0.serializePrivB58(mainnetParams));
  54. log.info("Account extended public key:{}", accountKey0.serializePubB58(mainnetParams));
  55. /**由父路径,派生出第一个子私钥*/
  56. DeterministicKey childKey0 = HDKeyDerivation.deriveChildKey(accountKey0, 0);
  57. // DeterministicKey childKey0 = deterministicHierarchy.deriveChild(parsePath, true, true, new ChildNumber(0));
  58. log.info("BIP32 extended 0 private key:{}", childKey0.serializePrivB58(mainnetParams));
  59. log.info("BIP32 extended 0 public key:{}", childKey0.serializePubB58(mainnetParams));
  60. log.info("0 private key:{}", childKey0.getPrivateKeyAsHex());
  61. log.info("0 public key:{}", childKey0.getPublicKeyAsHex());
  62. ECKeyPair childEcKeyPair0 = ECKeyPair.create(childKey0.getPrivKeyBytes());
  63. log.info("0 address:{}", PREFIX + Keys.getAddress(childEcKeyPair0));
  64. /**由父路径,派生出第二个子私钥*/
  65. DeterministicKey childKey1 = HDKeyDerivation.deriveChildKey(accountKey0, 1);
  66. log.info("BIP32 extended 1 private key:{}", childKey1.serializePrivB58(mainnetParams));
  67. log.info("BIP32 extended 1 public key:{}", childKey1.serializePubB58(mainnetParams));
  68. log.info("1 private key:{}", childKey1.getPrivateKeyAsHex());
  69. log.info("1 public key:{}", childKey1.getPublicKeyAsHex());
  70. ECKeyPair childEcKeyPair1 = ECKeyPair.create(childKey1.getPrivKeyBytes());
  71. log.info("1 address:{}", Keys.toChecksumAddress(Keys.getAddress(childEcKeyPair1)));
  72. String address1 = Keys.getAddress(childKey1.decompress().getPublicKeyAsHex().substring(2));
  73. log.info("1 address:{}", Keys.toChecksumAddress(address1));
  74. }
  75. @Test
  76. public void TestBip44BTC() throws Exception {
  77. String wordList = this.getWordListString();
  78. wordList = "please promote sting series horn leave squirrel juice harsh over wash reduce";
  79. log.info("generate mnemonic code:[{}]", wordList);
  80. DeterministicSeed deterministicSeed = new DeterministicSeed(wordList, SEED, PASSPHRASE, CREATIONTIMESECONDS);
  81. log.info("BIP39 seed:{}", deterministicSeed.toHexString());
  82. /**生成根私钥 root private key*/
  83. DeterministicKey rootPrivateKey = HDKeyDerivation.createMasterPrivateKey(deterministicSeed.getSeedBytes());
  84. /**根私钥进行 priB58编码*/
  85. String priv = rootPrivateKey.serializePrivB58(mainnetParams);
  86. log.info("BIP32 extended private key:{}", priv);
  87. /**由根私钥生成HD钱包*/
  88. DeterministicHierarchy deterministicHierarchy = new DeterministicHierarchy(rootPrivateKey);
  89. /**定义父路径*/
  90. List<ChildNumber> parsePath = HDUtils.parsePath("44H/0H/0H");
  91. DeterministicKey accountKey0 = deterministicHierarchy.get(parsePath, true, true);
  92. log.info("Account extended private key:{}", accountKey0.serializePrivB58(mainnetParams));
  93. log.info("Account extended public key:{}", accountKey0.serializePubB58(mainnetParams));
  94. /**由父路径,派生出第一个子私钥*/
  95. DeterministicKey childKey0 = HDKeyDerivation.deriveChildKey(accountKey0, 0);
  96. // DeterministicKey childKey0 = deterministicHierarchy.deriveChild(parsePath, true, true, new ChildNumber(0));
  97. log.info("BIP32 extended 0 private key:{}", childKey0.serializePrivB58(mainnetParams));
  98. log.info("BIP32 extended 0 public key:{}", childKey0.serializePubB58(mainnetParams));
  99. log.info("0 private key:{}", childKey0.getPrivateKeyAsHex());
  100. log.info("0 public key:{}", childKey0.getPublicKeyAsHex());
  101. log.info("0 address:{}", childKey0.toAddress(mainnetParams));
  102. /**由父路径,派生出第二个子私钥*/
  103. DeterministicKey childKey1 = HDKeyDerivation.deriveChildKey(accountKey0, 1);
  104. log.info("BIP32 extended 1 private key:{}", childKey1.serializePrivB58(mainnetParams));
  105. log.info("BIP32 extended 1 public key:{}", childKey1.serializePubB58(mainnetParams));
  106. log.info("1 private key:{}", childKey1.getPrivateKeyAsHex());
  107. log.info("1 public key:{}", childKey1.getPublicKeyAsHex());
  108. log.info("1 address:{}", childKey1.toAddress(mainnetParams));
  109. }
  110. /**
  111. * 生成12个助记词
  112. *
  113. * @return
  114. * @throws IOException
  115. * @throws MnemonicException.MnemonicLengthException
  116. */
  117. public String getWordListString() throws IOException, MnemonicException.MnemonicLengthException {
  118. StringBuilder stringBuilder = new StringBuilder();
  119. getWordList().stream().forEach(word -> {
  120. stringBuilder.append(word).append(C_BLANK1);
  121. });
  122. return stringBuilder.toString().trim();
  123. }
  124. /**
  125. * 生成12个助记词
  126. *
  127. * @return
  128. * @throws IOException
  129. * @throws MnemonicException.MnemonicLengthException
  130. */
  131. public List<String> getWordList() throws IOException, MnemonicException.MnemonicLengthException {
  132. MnemonicCode mnemonicCode = new MnemonicCode();
  133. SecureRandom secureRandom = new SecureRandom();
  134. /**必须是被4整除*/
  135. byte[] initialEntropy = new byte[16];
  136. secureRandom.nextBytes(initialEntropy);
  137. return mnemonicCode.toMnemonic(initialEntropy);
  138. }
  139. }

参考链接:

https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki

https://github.com/bitsofproof/supernode/blob/1.1/api/src/main/java/com/bitsofproof/supernode/api/ExtendedKey.java

https://github.com/bushidowallet/bushido-java-core/tree/master/src/main/java/com/bushidowallet/core/bitcoin/bip32

测试链接:https://iancoleman.io/bip39/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/831995
推荐阅读
相关标签
  

闽ICP备14008679号