Bip32-Hd-Wallets

使用 BIP44 從助記詞派生應付地址

  • May 10, 2021

我正在嘗試使用 BIP 44 從助記詞/助記符到應付地址。這是我正在使用的程式碼的簡化版本。但是,當我與<https://iancoleman.io/bip39/>進行比較時,私有、公共和地址不匹配。

var mnemonic = require('bitcore-mnemonic')
var bitcore = require('bitcore-lib')

var code = new mnemonic("width bicycle axis tell burst outdoor tray episode where they forest meadow enhance twin focus");

var xpriv = code.toHDPrivateKey(bitcore.Networks.livenet);
var derived = xpriv.derive("m/44'/0'/0'/0/0")

var privateKey = derived.privateKey
var publicKey = derived.publicKey
var address = new bitcore.Address(publicKey)

console.log(`address: ${address}`)
console.log(`privateKey: ${privateKey}`)
console.log(`publicKey: ${publicKey}`)

此程式碼輸出:

address: 14TomZRSGPtoPGy9HZA8WfX2veMbDXyAAJ
privateKey: 34fb6aee170bb41fe47f97ab601fea589620cf182ecf031688c25e5491c2c06f
publicKey: 027869eb83bbb44621c868fbd2a31daed4a0f8dbaa73d3fd79a597cdb1afcc9d1d

但是,當我轉到<https://iancoleman.io/bip39/>時 ,它給出的第一個地址是這個種子片語:

path: m/44'/0'/0'/0/0
address: 1BogrbApan5Q9xEFEzwPioRu3ogYkzAQZU
public: 0236f0bf97fa766301e1137bef8377e6f6063b8a576bda57220c7ab6cf531cc992
private: L46hcht5ue2tsSKmPEV2v41CrGU8DEJAP1w7XYPbvJ72v5NBPsPF

當您說簡化程式碼時,很難說出您哪裡出錯了。

這是一個範例,說明如何使用 bitcoinjs-lib v3.3.2 從助記符中派生 bip44 密鑰對

let mnemonicInput = 'width bicycle axis tell burst outdoor tray episode where they forest meadow enhance twin focus';
let seedHex = bip39.mnemonicToSeedSync(mnemonicInput).toString('hex');

//"14a3235efb14b096e8cc3082b89e0b629ec5c7b2c6621343b2657cb61853b0830623e97b8aeac416d3377b4da90a4838d9aea4d83e0117fd833049305af46f10"

let root = bitcoin.HDNode.fromSeedHex(seedHex);
let deriv = 44;
let account = 0;
let change = 0;
let index = 0;

let acct = root.derivePath("m/"+deriv+"'/0'/"+account+"'");
let xpub = acct.neutered().toBase58();
let pair = acct.derivePath(change+"/"+index).keyPair;

let wifkey = pair.toWIF();
let address = pair.getAddress();

//"address":"1BogrbApan5Q9xEFEzwPioRu3ogYkzAQZU"
//"wifkey":"L46hcht5ue2tsSKmPEV2v41CrGU8DEJAP1w7XYPbvJ72v5NBPsPF"

引用自:https://bitcoin.stackexchange.com/questions/106171