参考[https://blog.csdn.net/qq_16137795/article/details/84530528],[https://chaindesk.cn/witbook/9/73]
一、什么是HD钱包:
它是分层确定性(Hierarchical Deterministic)钱包的缩写 HD Wallets,是目前广泛使用的虚拟货币钱包标准。HD钱包从单个根种子(root seed)中创建,为128到256位的随机数,任何兼容HD钱包的根种子也可重新创造整个HD钱包,所以拥有HD钱包的根种子就等于拥有了所有密钥,方便存储、导入及导出。
二、BIP32/BIP39/BIP44含义:
BIP32:定义 Hierarchical Deterministic wallet (简称 "HD Wallet"),是一个系统可以从单一个 seed 产生一树状结构储存多组 keypairs(私钥和公钥)。好处是可以方便的备份、转移到其他相容装置(因为都只需要 seed),以及分层的权限控制等
BIP39:将 seed 用方便记忆和书写的单字表示。一般由 12 个单字组成,称为 mnemonic code(phrase),中文称为助记词或助记码
BIP44:基于 BIP32 的系统,赋予树状结构中的各层特殊的意义。让同一个 seed 可以支援多币种、多帐户等。各层定义如下:
m / purpose' / coin_type' / account' / change / address_index
其中的 purporse' 固定是 44',代表使用 BIP44。而 coin_type' 用来表示不同币种,
例如 BTC :‘0',BTC测试网:‘1’,ETH 是 60' ETC 是 61
change:0表示外部链,接收币;1表示内部链,找零。
三、多链钱包的助记词、私钥、地址生成。
安装 bip39:https://www.npmjs.com/package/bip39
npm install bip39 --save
安装 ethers.js: https://docs.ethers.io/ethers.js/html/index.html
npm install ethers --save
安装 bitcoinjs-lib:https://www.npmjs.com/package/bitcoinjs-lib
npm install bitcoinjs-lib --save
安装 eosjs-ecc:https://github.com/EOSIO/eosjs-ecc#randomkey
npm install eosjs-ecc --save
bip32,ethereumjs-wallet,ethereumjs-util
1、HD钱包助记词生成:
ethers.js生成助记词:
let mnemonic = ethers.Wallet.createRandom().mnemonic
bip39生成助记词:
let mnemonic = bip39.generateMnemonic()
2、BTC钱包通过助记词生成私钥、公钥、地址:
//设置生成测试or正式环境的钱包
const network = bitcoin.networks.bitcoin
// 计算seed:
const seed = bip39.mnemonicToSeed(mnemonic,'')
console.log('seed:' + util.bufferToHex(seed), "\n");
const root = bip32.fromSeed(seed,network)
const path = "m/44'/0'/0'/0/0";
const keyPair = root.derivePath(path)
const privateKey = keyPair.toWIF()
console.log("BTC私钥:", privateKey)
const publicKey = keyPair.publicKey.toString("hex")
console.log("BTC公钥:", publicKey)
let address = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey , network:network})
console.log("BTC普通地址:", address.address, "\n")
let segwitAddress = bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({pubkey: keypair.publicKey, net: net}),
net: net
})
console.log('隔离见证地址:', segwitAddress.address);
使用函数实现地址
···
let address = getAddress(keypair, net);
console.log('普通地址:', address);
function getAddress(keypair, net) {
let {address} = bitcoin.payments.p2pkh({pubkey: keypair.publicKey, net: net});
return address;
}
let segwitAddress = getSegwitAddress(keypair, net);
console.log('隔离见证地址:', segwitAddress);
function getSegwitAddress(keypair, net) {
let {address} = bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({pubkey: keypair.publicKey, net: net}),
net: net
})
return address;
}
···
3、ETH钱包通过助记词生成私钥、公钥、地址:
3.1 使用ethers.js
//ethers.js 暂时不支持通过添加密码作为Salt来保护种子
let Wallet = ethers.Wallet.fromMnemonic(mnemonic);
let privateKey = Wallet.privateKey;
console.log('ETH私钥:',privateKey)
let address = Wallet.address;
console.log('ETH地址:',address);
let compressedPublicKey = Wallet.signingKey.keyPair.compressedPublicKey;
console.log('ETH公钥:',compressedPublicKey)
let newWallet = ethers.Wallet.createRandom();
let mnemonic1 = newWallet.mnemonic;
console.log('mnemonic1:',mnemonic1)
let address1 = newWallet.address;
console.log('address1:',address1);
let privateKey1 = newWallet.privateKey;
console.log('privateKey1:',privateKey);
let compressedPublicKey1 = newWallet.signingKey.keyPair.compressedPublicKey;
console.log('publicKey:',compressedPublicKey1)
3.2 使用ethereumjs-wallet
导入依赖
let hdkey = require('ethereumjs-wallet/hdkey');
let util = require('ethereumjs-util');
let hdwallet = hdkey.fromMasterSeed(seed);
for (let i = 0; i < 3; i++) {
let path = "m/44'/60'/0'/0/" + i;
console.log(path);
let keypair = hdwallet.derivePath(path);
let privateKey = util.bufferToHex(keypair._hdkey._privateKey);
console.log('私钥:', privateKey);
let publicKey = util.bufferToHex(keypair._hdkey._publicKey);
console.log('公钥:', publicKey);
let address = util.pubToAddress(keypair._hdkey._publicKey, true);
console.log('地址:', address.toString('hex'))
}