最近闲来无事 研究了下以太坊钱包 下边分享下html
准备工做 :git
须要用到的加密:BIP32 BIP39 BIP44 SCRYPTgithub
加密算法 githab地址web
https://github.com/NovaCrypto/BIP32算法
https://github.com/NovaCrypto/BIP39app
https://github.com/NovaCrypto/BIP44dom
https://github.com/wg/scrypt/ui
官方依赖加密
https://github.com/web3j/web3j.net
钱包建立
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