在 nodejs 中使用 bip39 npm 包一直出错?

Keep getting an error using bip39 npm package in nodejs?

我从下面得到的错误是: 'The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Promise'

当我 console.log 'seed' 我得到了 Promise { },但是这个教程有这样的代码。

代码:

const bip39 = require('bip39');
const hdkey = require('hdkey');

const mnemonic = bip39.generateMnemonic(); //generates a 12 word mnemonic

const seed = bip39.mnemonicToSeed(mnemonic); //creates seed buffer

const root = hdkey.fromMasterSeed(seed); //should not be passing a promise into here
//const masterPrivateKey = root.privateKey.toString('hex');

const addrnode = root.derive("m/44'/60'/0'/0/0");
console.log(seed);

也许您的教程已经过时了。在bip39document中,我们有2种mnemonicToSeed

  • mnemonicToSeed: returns Promise
  • 的异步函数
  • mnemonicToSeedSync: returns Buffer
  • 的同步函数

作为你的例子,我们有2种方法可以解决上面的2个函数。

mnemonicToSeedSync

...
const seed = bip39.mnemonicToSeedSync(mnemonic); // creates seed buffer
...

mnemonicToSeed

(async () => { // wrap logic into a async function
  const mnemonic = bip39.generateMnemonic(); // generates a 12 word mnemonic

  // wait until seed finished to get seed Buffer
  const seed = await bip39.mnemonicToSeed(mnemonic); // creates seed buffer

  const root = hdkey.fromMasterSeed(seed); // should not be passing a promise into here
  //const masterPrivateKey = root.privateKey.toString('hex');

  const addrnode = root.derive("m/44'/60'/0'/0/0");
  console.log(seed);
})();