(转)以太坊 钱包 创建 导入 Keystore

时间:2023-03-09 16:52:04
(转)以太坊 钱包 创建 导入 Keystore

最近闲来无事 研究了下以太坊钱包 下边分享下

准备工作 :

需要用到的加密:BIP32 BIP39 BIP44 SCRYPT

加密算法 githab地址

https://github.com/NovaCrypto/BIP32

https://github.com/NovaCrypto/BIP39

https://github.com/NovaCrypto/BIP44

https://github.com/wg/scrypt/

官方依赖

https://github.com/web3j/web3j

钱包创建

1,生成一个随机的助记词   

  StringBuilder sb = new StringBuilder();
byte[] entropy = new byte[Words.TWELVE.byteLength()];
new SecureRandom().nextBytes(entropy);
new MnemonicGenerator(English.INSTANCE)
.createMnemonic(entropy, sb::append);
System.out.println(sb.toString());

2,根据助记词生成一个种子 (前两步参考BIP39)

 byte[] seed = new SeedCalculator()
.withWordsFromWordList(English.INSTANCE)
.calculateSeed(mnemonicWordsInAList(助记词List), passphrase);

3 ,根据种子生成公私钥 (web3j)//如果还需要对接比特币等等其他币种 请去查看 BIP44 与BIP32 之前写的是BIT44与32的 但是挺麻烦

    ECKeyPair ecKeyPair= ECKeyPair.create(sha256(seed));

输出16进制go

     ecKeyPair.getPrivateKey().toString(16)

     ecKeyPair.getPublicKey().toString(16)

4,根据公钥 私钥 密码 得到 keystore (参考web3j)

WalletFile walletFile = Wallet.create("钱包密码", ecKeyPair,n,p);

walletFile即钱包的keystore的实体 转化成string 就是 keystore

钱包地址=walletFile.getAddress();

到此整个钱包就生成完毕了 我们得到了 公钥 私钥 地址 keystore

钱包导入

私钥导入

ECKeyPair.create(new BigInteger(mPrivateKey,16));

助记词导入

通过助记词得到种子 然后再得到公私钥  看2-3步

Keystore导入

调用web3j中提供的方法

Wallet.decrypt("密码", WalletFile );

就这些 以太坊的钱包就完成了

我在项目中 WalletFile walletFile = Wallet.create("钱包密码", ecKeyPair,n,p);这一步出OOM了

然后就放弃了wen3j提供的方法 自己重新封装了一套 当然大部分还是借鉴的web3j的

嘿嘿,上边所有的方法其实都是别人已经封装好的 我只是把他们梳理了一下 方便下后人

我自己重新封装的部分就不贴了

转自:https://blog.csdn.net/u010123087/article/details/79608939