Bitcoinjs-lib: Best way to save and restore an HDnode

Created on 22 Jul 2017  路  2Comments  路  Source: bitcoinjs/bitcoinjs-lib

I am struggling to restore a bitcoin.HDNode after saving an encrypted key.
Here is where I'm at....

//Generate node
var mnemonic = bip39.generateMnemonic();
var seed = bip39.mnemonicToSeed(mnemonic);
var node = bitcoin.HDNode.fromSeedBuffer(seed);

var myWifString = node.keyPair.toWIF();
var decoded = wif.decode(myWifString);

var encryptedKey = bip38.encrypt(decoded.privateKey, decoded.compressed, 'Testing OneTwoThree');

// SAVE THE ENCRYPTED KEY
//  time passes.....

//Recover the key
var decryptedKey = bip38.decrypt(encryptedKey,  'Testing OneTwoThree', function (status) {
     console.log(status.percent)
});

//Then something like this...?
var wif = wif.encode(128, decryptedKey.privateKey, decryptedKey.compressed);
var keyPair =  bitcoin.ECPair.fromWIF(wif);
var restored_node = new bitcoin.HDNode(keyPair, <?????>)

// OR
var restored_node = bitcoin.HDNode.fromBase58(<?????>, BTCjs.bitcoin.networks.testnet);


Maybe I am just missing a simple step on getting the decryoted key into a base58 string? Or I could just be going in the wrong direction entirely. Any and all help is appreciated, apologies in advance for any 'herp-derp' moments.

how to / question / docs

All 2 comments

Yes it does, I think my fixation on bip38 caused me to miss it entirely.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

askucher picture askucher  路  3Comments

namnv04 picture namnv04  路  3Comments

zhaozhiming picture zhaozhiming  路  3Comments

tuyennvtb picture tuyennvtb  路  3Comments

Beardcoding picture Beardcoding  路  3Comments