Deriving hex private key from a HD wallet

1

I have the following test Mnemonic:

stable primary already collect buffalo bike into robust fury cement proof agree neither blouse course

From this I can generate a BIP39 Wallet: https://iancoleman.io/bip39/

From this I can get all the private keys of the derived addresses (if you scroll down to the bottom).

But I'm not sure what format they're in.

The library, bitconjs-lib does the following:

const ONE = Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
keyPair = ECPair.fromPrivateKey(ONE)

https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/ecpair.js#L29

But the private keys generated by https://iancoleman.io/bip39/ do not match the length required by bitcoinjs-lib

timothyylim

Posted 2019-01-08T11:44:48.603

Reputation: 313

Answers

2

The mnemonic represents a BIP32 extended private key: xprv9s21ZrQH143K4BbpSvRBDykoXLhwtznbp7qAfKVLw1QdJtJsmgS5rZJfSgZNtgk7e39uRNiYHbcRQnYvruqfftfG6WFmuiCspehEz1VS3YM

The function you are calling accepts a regular private key.

You can derive child private keys from the extended private key and use these to generate addresses.

It it be possible to derive the corresponding private key of either the children of this extended private key or the corresponding private key itself using the toWIF method in the bip32 JS library. See https://github.com/bitcoinjs/bip32/blob/master/index.js

You can see some examples of the use of BIP32 here: https://github.com/bitcoinjs/bitcoinjs-lib/blob/a6f2cc2924df170537f0b588254178f5445d7dc8/test/integration/bip32.js

Thorkil Værge

Posted 2019-01-08T11:44:48.603

Reputation: 637

Thanks so much @Thorkil, just one last question: which line in the file you added shows a derivation to a regular private key?timothyylim 2019-01-08T12:01:36.957

Can you call this?

const seed = bip39.mnemonicToSeed(mnemonic); const node = bip32.fromSeed(seed); const privateKey = node.toWIF(); – Thorkil Værge 2019-01-08T13:43:08.187

that worked! Just for future reference, I used the wif node module to decode it to hex.timothyylim 2019-01-09T16:12:26.263