博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HD wallet的创建、导入
阅读量:6703 次
发布时间:2019-06-25

本文共 4685 字,大约阅读时间需要 15 分钟。

hot3.png

以太坊HD wallet 的创建、导入、导出如下:

const bip39 = require('bip39');const hdkey = require('ethereumjs-wallet/hdkey');const util = require('ethereumjs-util');const Wallet = require('ethereumjs-wallet');/** *生成钱包 * */let mnemonic = bip39.generateMnemonic();mnemonic = "eyebrow tube verify supreme abandon theme blast harsh jungle believe much economy";console.log("mnemonic ====== ",mnemonic);let seed = bip39.mnemonicToSeed(mnemonic);console.log("seed ====== ",seed.toString('hex'));let hdWallet = hdkey.fromMasterSeed(seed);//产生 Wallet中第一个帐户的第一组 keypairlet key1 = hdWallet.derivePath("m/44'/60'/0'/0/0");let wallet1 = key1.getWallet();let address1 = util.bufferToHex(util.pubToAddress(wallet1.getPublicKey(), true));let private1 = wallet1.getPrivateKey().toString('hex');let keystore1 = wallet1.toV3String("123456");//也可以使用下面的方式获取address、private key。/*let address1 = util.bufferToHex(util.pubToAddress(key1._hdkey._publicKey, true));let private1 = key1._hdkey._privateKey.toString('hex');*/console.log("address1 ====== ",address1);console.log("private1 ====== ",private1);console.log("keystore1 ====== ",keystore1);/** *导入钱包(助记词导入) */ mnemonic = "eyebrow tube verify supreme abandon theme blast harsh jungle believe much economy"; seed = bip39.mnemonicToSeed(mnemonic); hdWallet = hdkey.fromMasterSeed(seed); key1 = hdWallet.derivePath("m/44'/60'/0'/0/0");address1 = util.bufferToHex(util.pubToAddress(key1._hdkey._publicKey, true));private1 = key1._hdkey._privateKey.toString('hex');console.log("address1 ====== ",address1,"private1 ====== ",private1);/** * 私钥导入 a9612725853b27f0063696d55f700bbcaa44118a0a5e8627ca8c65c2da4232f1 */let fixturePrivateKeyStr =  "a9612725853b27f0063696d55f700bbcaa44118a0a5e8627ca8c65c2da4232f1"let fixturePrivateKeyBuffer = Buffer.from(fixturePrivateKeyStr, 'hex')let fixtureWallet = Wallet.fromPrivateKey(fixturePrivateKeyBuffer);let fixturePrivateKey = fixtureWallet.getPrivateKey().toString('hex');let fixturePublicKey = util.bufferToHex(util.pubToAddress(fixtureWallet.getPublicKey(), true));console.log("fixturePublicKey ====== ",fixturePublicKey);console.log("fixturePrivateKey ====== ",fixturePrivateKey);/** *生成keystore json */let fixtureKeystore = fixtureWallet.toV3String("123456");//{"version":3,"id":"0ee81400-c4f9-4d60-8a4e-d56505ba1695","address":"267bc50356a652b70269100392ab93d286a6d076","crypto":{"ciphertext":"5e94f7acae76acd4042f749f697248cf54f3e120e4d4e3ed3b9288ff1faacf70","cipherparams":{"iv":"36016c4b0f6d8642115b0a8f2014d2b6"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"e646df09e1e86e6c5b96b21f3710f0b96aafd950398f172f72fb247d70ebfcd8","n":262144,"r":8,"p":1},"mac":"17ce2293f06405f3abf5ee3aa164796644ce3d26a475d8aa2adf30b492a926ed"}}console.log("fixtureKeystore ====== ",fixtureKeystore);/** *从keystore json 还原Wallet */let restoreWallet =  Wallet.fromV3(fixtureKeystore, "123456");let restorePrivateKey = restoreWallet.getPrivateKey().toString('hex');let restorePublicKey = util.bufferToHex(util.pubToAddress(restoreWallet.getPublicKey(), true));console.log("restorePrivateKey ====== ",restorePrivateKey);console.log("restorePublicKey ====== ",restorePublicKey);/** *store.js 在浏览器端存储数据。 * https://github.com/marcuswestin/store.js * https://segmentfault.com/a/1190000007539338 * https://gist.github.com/reygreen1/7226325 */

整理之后可以写成下面的工具类。

const bip39 = require('bip39');const hdkey = require('ethereumjs-wallet/hdkey');const util = require('ethereumjs-util');const Wallet = require('ethereumjs-wallet');let generateMnemonic = ()=>{    return bip39.generateMnemonic();}let generateAccount = (mnemonic,password) =>{    let seed = bip39.mnemonicToSeed(mnemonic);    let hdWallet = hdkey.fromMasterSeed(seed);    let key1 = hdWallet.derivePath("m/44'/60'/0'/0/0");    let wallet1 = key1.getWallet();    return getWalletInfo(wallet1,password);}let getWalletInfo = (walletInstance,password)=>{    let address1 = util.bufferToHex(util.pubToAddress(walletInstance.getPublicKey(), true));    let private1 = walletInstance.getPrivateKey().toString('hex');    let keystore1 = walletInstance.toV3String(password);    return {        address:address1,        private:private1,        keystore:keystore1    }}let restoreFromPrivateKey = (privateKey,password) =>{    let keyBuffer = Buffer.from(privateKey, 'hex')    let keyWallet = Wallet.fromPrivateKey(keyBuffer);    return getWalletInfo(keyWallet,password);}let restoreFromKeystore = (keystore,password) =>{    let restoreWallet =  Wallet.fromV3(keystore,password);    return getWalletInfo(restoreWallet,password);}module.exports  = {    generateMnemonic,    generateAccount,    restoreFromPrivateKey,    restoreFromKeystore};

转载于:https://my.oschina.net/kunBlog/blog/2876581

你可能感兴趣的文章
ftp: connect: No route to host
查看>>
野心外露 Facebook或将研发自主操作系统
查看>>
分布式-分布式事务处理
查看>>
《跟菜鸟学Cisco UC部署实战》-让您学了更菜
查看>>
HTML 标签
查看>>
php mcrypt 加密解密类
查看>>
lamp|lnmp|lnamp|一键安装包
查看>>
tomcat配置虚拟主机
查看>>
Apache url重写
查看>>
安装openstack dashboard
查看>>
Android 获取ImageView中的Bitmap
查看>>
免费的Web UI开发套件
查看>>
Windows 7 部署-- WAIK制作Winpe
查看>>
Linux磁盘分区格式化
查看>>
【技术人快报】Linux基金会:未来6个月需要招聘更多开源人才+Facebook将在欧洲开数字培训中...
查看>>
我的友情链接
查看>>
Spray.io搭建Rest服务
查看>>
探索C++对象模型(二)
查看>>
内核模式和用户模式
查看>>
SSH 整合框架(自整理)
查看>>