1. 程式人生 > >HD wallet的建立、匯入

HD wallet的建立、匯入

以太坊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
};