1. 程式人生 > >區塊鏈錢包助記詞生成,編解碼,認證

區塊鏈錢包助記詞生成,編解碼,認證

助記詞是分層錢包中重要的概念之一,比特幣BIP39是一套助記詞庫,比特幣BIP39的JavaScript實現,用於生成確定性金鑰的助記符程式碼。

一.助記詞生成

生成12個助記詞

var bip39 = require('bip39')
var mnemonic = bip39.generateMnemonic()
console.log(mnemonic);

var bip39 = require('bip39')
var mnemonic = bip39.generateMnemonic(128)
console.log(mnemonic);	

生成15個助記詞

var bip39 = require('bip39')
var mnemonic = bip39.generateMnemonic(160)
console.log(mnemonic);	

生成18個助記詞

var bip39 = require('bip39')
var mnemonic = bip39.generateMnemonic(192)
console.log(mnemonic);	

生成21個助記詞

var bip39 = require('bip39')
var mnemonic = bip39.generateMnemonic(224)
console.log(mnemonic);	

生成24個助記詞

var bip39 = require('bip39')
var mnemonic = bip39.generateMnemonic(256)
console.log(mnemonic);	

生成中文助記詞

var mnemonicS = bip39.generateMnemonic(128, null, bip39.wordlists.chinese_simplified);
console.log(mnemonicS);

以上根據傳值的的大小限定助記詞的個數

目前助記詞庫能夠生成“簡體中文”,“繁體中文”,“英文”,“法文”,“義大利文”,“日文”,“韓文”和西班牙文。

二.助記詞編解碼

編碼助記詞

var bip39 = require('bip39')
var mnemonic = bip39.generateMnemonic()
var encrytMnemonic = bip39.mnemonicToEntropy(mnemonic)
console.log(encrytMnemonic);

解碼助記詞

var bip39 = require('bip39')
var mnemonic = bip39.generateMnemonic()
console.log(mnemonic);
var encrytMnemonic = bip39.mnemonicToEntropy(mnemonic)
var word = bip39.entropyToMnemonic(encrytMnemonic)
console.log(word);

三.生成隨機數種子

var mnemonicE = bip39.generateMnemonic()
console.log(mnemonicE);
var Seed= bip39.mnemonicToSeed(mnemonicE)
var seedHex= bip39.mnemonicToSeedHex(mnemonicE)
console.log(Seed);
console.log(seedHex);

四.驗證助記詞

var mnemonic = bip39.generateMnemonic()
console.log(mnemonic);
var bool = bip39.validateMnemonic(mnemonic)

本人的github:https://github.com/guoshijiang/blockchain-wallet/