cryptocoinjs / hdkey

JavaScript component for Bitcoin hierarchical deterministic keys (BIP32)
MIT License
201 stars 74 forks source link

TypeError [ERR_INVALID_ARG_TYPE] #37

Closed anhdungle93 closed 4 years ago

anhdungle93 commented 4 years ago

Hi I am trying to understand HD Wallet through this tutorial https://medium.com/@harshagoli/so-you-want-to-build-an-ethereum-hd-wallet-cb2b7d7e4998

first I generated the seed with bip39 version 3.0.2

const bip39 = require('bip39');
const mnemonic = bip39.generateMnemonic();
const seed = bip39.mnemonicToSeed(mnemonic);

seed is an instance of Promise which resolves to a Buffer

Promise {
  <Buffer 53 e5 e7 80 f0 83 d5 8e d8 6a 88 8b 24 80 5e b0 5e 91 cf 45 18 b3 1c fd 02 a7 a3 37 5c fc e2 c2 98 15 7c db 90 01 e7 8d c0 56 62 dd af f6 74 73 ff 3b ... 14 more bytes>
}

the next step was to generate the root by hdkey.fromMasterSeed (hdkey version 1.1.2)

const hdkey = require('hdkey');
const root = hdkey.fromMasterSeed(seed);

this threw an error:

> root = hdkey.fromMasterSeed(seed);
Uncaught:
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Promise
    at Hmac.update (internal/crypto/hash.js:84:11)
    at Function.HDKey.fromMasterSeed (/Users/anhdungle/learning/Ethereum_HD_wallet/node_modules/hdkey/lib/hdkey.js:177:54)
    at repl:1:14
    at Script.runInThisContext (vm.js:131:20)
    at REPLServer.defaultEval (repl.js:432:29)
    at bound (domain.js:429:14)
    at REPLServer.runBound [as eval] (domain.js:442:12)
    at REPLServer.onLine (repl.js:759:10)
    at REPLServer.emit (events.js:327:22)
    at REPLServer.EventEmitter.emit (domain.js:485:12) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Thanks a lot.

RyanZim commented 4 years ago

You're passing a promise into hdkey.fromMasterSeed; you need get the actual resolved Buffer to pass in.

anhdungle93 commented 4 years ago

For how long should I wait until it resolves? I also tried

const seed = await bip39.mnemonicToSeed(mnemonic);

but it said that mnemonicToSeed is not an async function, which is weird.

junderw commented 4 years ago

Use mnemonicToSeedSync

await needs to be used INSIDE an async function foo() { ... } type of function... the only time JS runtime error will say anything about "async" is when you try to await outside of an async function.