一、什么是HD钱包:
它是分层确定性(Hierarchical Deterministic)钱包的缩写 HD
Wallets,是目前广泛使用的虚拟货币钱包标准。HD钱包从单个根种子(root
seed)中创建,为128到256位的随机数,任何兼容HD钱包的根种子也可重新创造整个HD钱包,所以拥有HD钱包的根种子就等于拥有了所有密钥,方便存储、导入及导出。
二、BIP32/BIP39/BIP44含义:
BIP32
<https://link.jianshu.com/?t=https%3A%2F%2Fgithub.com%2Fbitcoin%2Fbips%2Fblob%2Fmaster%2Fbip-0032.mediawiki>
:定义 Hierarchical Deterministic wallet (简称 "HD
Wallet"),是一个系统可以从单一个 seed 产生一树状结构储存多组
keypairs(私钥和公钥)。好处是可以方便的备份、转移到其他相容装置(因为都只需要 seed),以及分层的权限控制等
BIP39
<https://link.jianshu.com/?t=https%3A%2F%2Fgithub.com%2Fbitcoin%2Fbips%2Fblob%2Fmaster%2Fbip-0039.mediawiki>
:将 seed 用方便记忆和书写的单字表示。一般由 12 个单字组成,称为 mnemonic code(phrase),中文称为助记词或助记码
BIP44
<https://link.jianshu.com/?t=https%3A%2F%2Fgithub.com%2Fbitcoin%2Fbips%2Fblob%2Fmaster%2Fbip-0044.mediawiki>
:基于 BIP32 的系统,赋予树状结构中的各层特殊的意义。让同一个 seed 可以支援多币种、多帐户等。各层定义如下:
m / purpose' / coin_type' / account' / change / address_index
其中的 purporse' 固定是 44',代表使用 BIP44。而 coin_type' 用来表示不同币种,
例如 BTC 就是 0',ETH 是 60' ETC 是 61
三、多链钱包的助记词、私钥、地址生成。
注:所需要的依赖包安装
安装 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 //引用依赖(根据工程环境安装的不同版本对应使用) import { ethers } from 'ethers'; import
bitcoin from 'bitcoinjs-lib'; import bip39 from 'bip39'; import bip32 from
'bip32'; import eosEcc from 'eosjs-ecc';
1、HD钱包助记词生成:
ethers.js生成助记词: var mnemonic = ethers.Wallet.createRandom().mnemonic
bip39生成助记词: var mnemonic = bip39.generateMnemonic()
2、BTC钱包通过助记词生成私钥、公钥、地址:
//设置生成测试or正式环境的钱包 const network = bitcoin.networks.bitcoin // 计算seed: const
seed = bip39.mnemonicToSeed(mnemonic) 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")
3、ETH钱包通过助记词生成私钥、公钥、地址:
var Wallet = ethers.Wallet.fromMnemonic(mnemonic); var privateKey =
Wallet.privateKey; console.log("ETH私钥:",privateKey) var address =
Wallet.address; console.log("ETH地址:",address) var compressedPublicKey =
Wallet.compressedPublicKey; console.log("ETH公钥:",compressedPublicKey)
//生成新的助记词、私钥、地址 var wallet = ethers.Wallet.createRandom(); let mnemonic =
wallet.mnemonic; var address = wallet.address; console.log("ETH地址:"address) var
privateKey = wallet.privateKey; console.log("ETH私钥:",privateKey) var
compressedPublicKey = wallet.signingKey.keyPair.compressedPublicKey;
console.log("ETH公钥:",compressedPublicKey)
4、EOS钱包通过助记词生成私钥、公钥:
var eosPrivate = eosEcc.seedPrivate(mnemonic);
console.log("EOS私钥:",eosPrivate) const eosPubkey =
eosEcc.privateToPublic(eosPrivate); console.log("EOS公钥:",eosPubkey)
//随机生成新的私钥公钥 eosEcc.randomKey().then(privateKey => { console.log('Private
Key:\t', privateKey) console.log('Public Key:\t',
eosEcc.privateToPublic(privateKey)) }) 如何注册EOS账户: 需安装: npm install eosjs --save
eos环境配置: var Eos = require('eosjs') var eosConfig = { keyProvider: ['私钥'], //
配置私钥字符串 httpEndpoint: 'http://51.15.224.168:8888', //DEV开发链url与端口
//httpEndpoint: 'https://nodes.get-scatter.com', //主网 chainId:
"038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca", // 通过cleos
get info可以获取chainId //chainId:
"aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906", //主网
broadcast: true, } var eos = Eos(eosConfig) var creatoraccount =
"accounthr123"; //主账号 var newaccount = "sdrghiochaiq"; //新账号 var
newaccount_pubkey = pubkey; //新账号的公钥 //构建transaction对象 eos.transaction(tr => {
//新建账号 tr.newaccount({ creator: creatoraccount, name: newaccount, owner:
newaccount_pubkey, active: newaccount_pubkey }) //为新账号充值RAM tr.buyrambytes({
payer: creatoraccount, receiver: newaccount, bytes: 3072 }) //为新账号抵押CPU和NET资源
tr.delegatebw({ from: creatoraccount, receiver: newaccount, stake_net_quantity:
'1.0000 EOS', stake_cpu_quantity: '1.0000 EOS', transfer: 0 }) }).then(r => {
console.log(r); }).catch(e => { console.log(e) }); }catch (e){ }
测试结果:
学如逆水行舟,不进则退。心似平原跑马,易放难收。全栈工程师是指掌握多种技能,并能利用多种技能独立完成产品的人。
也叫全端工程师(同时具备前端和后台能力),英文Full Stack
engineer。【人工智能】【区块链】【系统/网络/运维】【云计算/大数据】【数据库】【移动开发】【后端开发】【游戏开发】【UI设计】【微服务】【爬虫】【Java】【Go】【C++】【PHP】【Python】【Android/IOS】【HTML/CSS】【JavaScript】【Node】。。。
欢迎各位大神萌新一起专研分享各行各业技术!
IT全栈工程师技术交流群:593674370
热门工具 换一换