Javascript

UnhandledPromiseRejectionWarning:TypeError:無法讀取未定義的屬性“fromSeedBuffer”

  • September 12, 2018

我在這一行收到錯誤

var root = bitcoin.HDNode.fromSeedBuffer(seed);

UnhandledPromiseRejectionWarning:TypeError:無法讀取未定義的屬性“fromSeedBuffer”

這是生成比特幣地址的程式碼片段:

'use strict'

var bip39 = require('bip39');
var bitcoin = require('bitcoinjs-lib');

var mnemonic = bip39.generateMnemonic();
if (bip39.validateMnemonic(mnemonic)) {
   console.log('\nThe mnemonic is \n' + mnemonic);
   var seed = bip39.mnemonicToSeed(mnemonic);
   var root = bitcoin.HDNode.fromSeedBuffer(seed);
   var dp = root.derivePath("m/140'/0'/0'/0/5");

   console.log('\nThe Address is \n' + dp.getAddress());    
} else {

}

其中 bitcoinjs-lib v4.0.0 和 bip39 v2.5.0 和節點 v8.0

嘗試使用該bip32模組。此外,您還需要修復一些其他問題。請參閱使用 BIP39 生成 BIP32 地址。注意,這會生成一個P2PKH地址,見getAddress()函式。

index.js:

'use strict'

var bip39 = require('bip39');
var bip32 = require('bip32');
var bitcoin = require('bitcoinjs-lib');

function getAddress (node, network) {
 return bitcoin.payments.p2pkh({ pubkey: node.publicKey, network }).address
}

var mnemonic = bip39.generateMnemonic();
if (bip39.validateMnemonic(mnemonic)) {
   console.log('\nThe mnemonic is \n' + mnemonic);
   var seed = bip39.mnemonicToSeed(mnemonic);
   var root = bip32.fromSeed(seed);
   var dp = root.derivePath("m/140'/0'/0'/0/5");

   console.log('\nThe Address is \n' + getAddress(dp));    
} else {

}

輸出:

$ node index.js

The mnemonic is
assume victory kiwi swarm furnace firm thumb exact dizzy crucial neglect certain

The Address is
1FYtPnUZZ2ZJL2fZ6XehGtkHfujUHkqK3T

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