文档章节

HD wallet的创建、导入

王坤charlie
 王坤charlie
发布于 2018/11/15 13:55
字数 499
阅读 66
收藏 0

以太坊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中第一个帐户的第一组 keypair
let 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
};

© 著作权归作者所有

王坤charlie
粉丝 5
博文 198
码字总数 55881
作品 0
东城
程序员
私信 提问
以太坊钱包开发系列1 - 创建钱包账号

以太坊去中心化网页钱包开发系列,将从零开始开发出一个可以实际使用的钱包,本系列文章是理论与实战相结合,一共有四篇:创建钱包账号、账号Keystore文件导入导出、展示钱包信息及发起签名交...

深入浅出区块链
2018/10/25
0
0
bitshares环境部署-私链

获取可执行文件 windows 其他系统 执行文件结构简介 [图片上传失败...(image-ca0098-1534475751728)] witness_node delayed_node cli_wallet 私有链部署 witness_node 部署 生成创世块的jso...

mybestcool
2018/08/17
0
0
EOS cleos wallet key 密钥命令

导入密钥 命令格式: 注意: 如果您没有持有账户密钥,则需要使用create account命令先创建账户密钥。 列出钱包密钥 命令格式: 这将列出公钥中存储在钱包中的所有密钥。 分享个《EOS智能合约...

智能合约
2018/07/28
95
0
第10课 EOS常见命令及样例演示

1,摘要 【本文目标】 本文列出经常EOS环境经常会使用的命令格式以及样例,作为EOS命令的快速查找手册。 不管是EOS的命令行帮助说明还是命令行参考文档,对参数的引用描述都比较乱,辉哥重新...

笔名辉哥
2018/07/17
0
0
Ethereum HD KDF

There's a long discussion about using BIP32 (the Bitcoin HD wallet KDF) and BIP44 (the KDF path standard) for Ethereum. It was raised that perhaps a different scheme could be us......

wmzsonic
2018/01/12
45
0

没有更多内容

加载失败,请刷新页面

加载更多

Centos6.6 安装ffmpeg视频工具

1.安装前置工具 yum -y install gcc cc cl libmpc*//后续失败的话,自己补充自己的缺少的包 2.安装yasm 1)下载wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz...

小海bug
22分钟前
4
0
电商的支付风控怎么玩?

qwfys
26分钟前
6
0
用什么来做用户行为分析?七个实用工具推荐给你

当企业进入数据化管理阶段之后,就不得不对用户进行行为数据分析,当然其他的包括用户画像、趋势分析等等,都是现在企业经常要进行的营销分析,因此选一个好的数据分析工具是很重要的。 而现...

朕想上头条
27分钟前
5
0
Java mysql连接

import java.sql.DriverManager; import java.sql.SQLException; import com.mysql.jdbc.Connection; public class T { public static void main(String[] args) throws SQLException, Insta......

林词
28分钟前
4
0
Select 选择器 的一些官网没有的用法

一、拼接字符串 <el-option v-for="(item, index) in reagentOptions" :key="index" :label="item.sjlxmc+' '+item.sjbm" :va......

沉迷代码我爱学习
28分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部