关于区块链的助记词以及BTC地址,私钥,公钥,ETH地址获取的方法

VUE项目中的获取助记词的方式

安装需要的依赖环境

如果你安装了国内的淘宝镜像了 ,建议你用cnpm
bip39:npm install bip39
bip32:npm install bip32
ethereumjs-util:npm install ethereumjs-util
bitcoinjs-lib:npm install bitcoinjs-lib
这个我不知道怎么回事,安装不上需要python环境,这个是别人给弄得
代码如下:

var bitcoin = require('bitcoinjs-lib')
var bip39 = require('bip39')
var bip32 = require('bip32')
var ethUtil = require("ethereumjs-util");

var mnemonic = bip39.generateMnemonic()
// 存到localStorage中单词
var mnemonicArr = mnemonic.replace(/\s+/g, ',').split(',');
localStorage.setItem('mnemonic', JSON.stringify(mnemonicArr))
// console.log("助记词:" + mnemonic)

var seedHex = bip39.mnemonicToSeedHex(mnemonic);
// console.log(seedHex);
var seed = bip39.mnemonicToSeed(mnemonic)
// console.log(seed)
var seedAsHex = seed.toString('hex');

// 存到localStorage中bip39种子
localStorage.setItem('seed', JSON.stringify(seedAsHex))
// console.log("种子:" + seedAsHex);


var masterKey = bitcoin.HDNode.fromSeedHex(seed, bitcoin.networks.bitcoin)
// console.log("BTC");


// 存到localStorage中bip39种子
localStorage.setItem('masterKey', JSON.stringify(masterKey.toBase58()))
// console.log("root Key:" + masterKey.toBase58())


var bip32ExtendedKey = masterKey.derivePath("m/44'/0'/0'/0");
// console.log("btc:BIP32 Extended Private Key:" + bip32ExtendedKey.toBase58())


var key = bip32ExtendedKey.derive(0);
// console.log(key);
var keyPair = key.keyPair;
// console.log(keyPair);
var address = keyPair.getAddress().toString();
//  存到localStorage中btc公钥还有密钥
localStorage.setItem('BTCaddress', JSON.stringify(address))
// console.log("btc地址:" + address);
var privkey = keyPair.toWIF();
//  存到localStorage中btc私钥
localStorage.setItem('privBTC', JSON.stringify(privkey))
// console.log("btc私钥:" + privkey);
var pubkey = keyPair.getPublicKeyBuffer().toString('hex');
localStorage.setItem('publBTC', JSON.stringify(pubkey))
// console.log("btc公钥:" + pubkey);



var bip32ExtendedKey = masterKey.derivePath("m/44'/60'/0'/0");
// console.log("ETH:BIP32 Extended Private Key:" + bip32ExtendedKey.toBase58())
key = bip32ExtendedKey.derive(0);

// console.log(key);

var keyPair = key.keyPair;
var address = keyPair.getAddress().toString();
var privkey = keyPair.toWIF();
var pubkey = keyPair.getPublicKeyBuffer().toString('hex');
var privKeyBuffer = keyPair.d.toBuffer(32);
privkey = privKeyBuffer.toString('hex');
var addressBuffer = ethUtil.privateToAddress(privKeyBuffer);
address = ethUtil.addHexPrefix(ethUtil.toChecksumAddress(ethUtil.privateToAddress(privKeyBuffer).toString('hex')));
privkey = ethUtil.addHexPrefix(privkey);
pubkey = ethUtil.addHexPrefix(pubkey);
// eth的公钥私钥 地址
localStorage.setItem('ETHaddress', JSON.stringify(address))
localStorage.setItem('publETH', JSON.stringify(privkey))
localStorage.setItem('privETH', JSON.stringify(pubkey))
// 
// console.log("ETh地址:" + address);
// console.log("ETh私钥:" + privkey);
// console.log("ETh公钥:" + pubkey);

参考文章:廖雪峰大神的博客网站

上一篇:Axure如何建立共享项目、如何编辑共享项目、如何获取共享项目


下一篇:Cookie、session和localStorage、以及sessionStorage之间的区别