The error I get from below is: 'The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Promise'
When I console.log 'seed' I get Promise { }, but the tutorial for this has the code just like this.
Code:
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);
Maybe your tutorial is out of date. In bip39
's document, we have 2 kinds of mnemonicToSeed
:
mnemonicToSeed
: Async function which returns PromisemnemonicToSeedSync
: Sync function which returns BufferAs your example, we have 2 ways to solve it following 2 above functions.
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);
})();