3

I only have the seed phrase. When I enter the seed phrase into, for example, Exodus or Trust Wallet, I can fully recover my Bitcoin in seconds. But how to do this using Python or Node.js?

1 Answer 1

3

You can obtain the extended private key with different derivation paths from a mnemonic using the NodeJS libraries: [email protected] bip39 and bs58check. From the extended private key you can generate all the child private keys necessary to import or recover your wallet.

[email protected] was last stable version that included the HDNode prototype. Newer versions break out the HD functions and requires more imports.

Basically you start with the mnemonic and you convert it to an HD seed.

let mnemonicInput = "bacon bacon bacon bacon bacon bacon bacon bacon bacon bacon bacon abandon";
let seedHex = bip39.mnemonicToSeedSync(mnemonicInput).toString('hex');

Once you have the hex seed then you designate your derivation path.

let root = bitcoin.HDNode.fromSeedHex(seedHex);
let acct = root.derivePath("m/"+deriv+"'/0'/"+account+"'");
//this example acct uses hardened derivation paths (note the apostrophes which are safer than unhardened paths). You will need to remove the apostrophes in order to access an unhardened path
let xprv = acct.toBase58();

From here you will want to convert to proper x, y or zpub which can be done using Jameson Lopps conversion script.

All together it could look like this:

let bitcoin = require("bitcoinjs-lib");//use version @3.3.2 for access to HDNode
let bip39 = require('bip39');
let b58 = require('bs58check');

let mnemonicInput = "bacon bacon bacon bacon bacon bacon bacon bacon bacon bacon bacon abandon";
let seedHex = bip39.mnemonicToSeedSync(mnemonicInput).toString('hex');

console.log(seedToXprv(seedHex,44,0));
//xprv9yqbv4K6SV9NR7kyGBJbbynFvWdf5FUXV4QCNJQe78jAAqT3GE44kuyygmUyYpMmY31Z7mDudrpk3LpHyqv4xSFuRiXpBw1ZdtmrD2aeJQL

console.log(seedToXprv(seedHex,49,0));
//yprvAJeXovoVVRmXGCh1rsQDjWR2U2MmCY2SexU9Hofbrzhx5oA8xtSrXoqLemv1gYF3bR5z8Cy8HdBrM8MMAA7rQrgRXBLuZY7npxwtemrVjMc

console.log(seedToXprv(seedHex,84,0));
//zprvAccUoAVGQCPGinCJAuCtfFogLzXDvuqFBnGnRbwEAJczSo7KC74FV1X9dd5sCHRb4osqYQrj6DeRBkTwejHSDf2U7pxWTjqiqGqsPTj2Jo8


function seedToXprv(seed, deriv, account){
   var root = bitcoin.HDNode.fromSeedHex(seed);
   var acct = root.derivePath("m/"+deriv+"'/0'/"+account+"'");
   var xprv = acct.toBase58();
   var outprv = xprv;
    if(deriv==84){
       var zprv = convertXpub(xprv,"zprv");
       outprv = zprv;
       } else if (deriv==49)
       {
           var yprv = convertXpub(xprv,"yprv");   
           outprv = yprv;
       }
       return outprv;
}

function convertXpub(xpub,target){
    //source xpub conversion script https://github.com/jlopp/xpub-converter/blob/master/js/xpubConvert.js
    const prefixes = new Map(
  [
    ['xpub', '0488b21e'],
    ['ypub', '049d7cb2'],
    ['Ypub', '0295b43f'],
    ['zpub', '04b24746'],
    ['Zpub', '02aa7ed3'],
    ['tpub', '043587cf'],
    ['upub', '044a5262'],
    ['Upub', '024289ef'],
    ['vpub', '045f1cf6'],
    ['Vpub', '02575483'],
    ['xprv', '0488ade4'],
    ['yprv', '049d7878'],
    ['zprv', '04b2430c']
  ]
  );
    
  xpub = xpub.trim();

  try {
    var data = b58.decode(xpub);
    data = data.slice(4);
    data = Buffer.concat([Buffer.from(prefixes.get(target),'hex'), data]);
    return b58.encode(data);
  } catch (err) {
    return "Invalid extended key! Please double check that you didn't accidentally paste extra data.";
  }    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.