0
var bitcoin = require('bitcoinjs-lib');
let bip32 = require('bip32');

let rootKey = 'xprv9s21ZrQH143K32CfqoCQHtbKiEb5BqFazXX6jCtNCnuty3gUjxS4CsXWi9rcNyHdjDVPiC6P1bnyEZr2ioouRq56h6HAdwejeTty1BsSEtL'
const node = bip32.fromBase58(rootKey, bitcoin.networks.bitcoin);

//legacy account extended private key
console.log("LEGACY: " + node.derivePath("m/44'/0'/0").toBase58())
//xprv9y7nL2k2diLdA9bmop94K8Pec2WdL998PLdvTraxj5jiQ36gnLmhVJG4bY8FzgqZPaBM5HDBi9sgRK1ZCi14n5j4Pk1o6d3j4Y6T9NS4nA6
(works as expected)


//segwit account extended private key
//trying to get a segwit extended private key (zprv) using the same inputs as above.

I want to get a segwit/bech32 extended private key using the same root key as above. How can I do this using bitcoinjs-lib? BIP84 only works for mnemonics IIRC.

1 Answer 1

0

Your xprv already has the derivation path "baked in" so even if you were to just convert to zprv it will still be under the old m44/ deriv path. Also you can't go from xprv to HD seed. So to change from an xprv to a zprv or yprv you need to start with the HD seed, then declare your derivePath("m84'/...","m49/'/... etc), then you can create an extended public key. Then you'll have to convert to yprv, zprv, ypub, etc...

Example uses bitcoinjs-lib v3.3.2

function seedToXpub(seed, deriv, account){
   var root = bitcoin.HDNode.fromSeedHex(seed);
   var acct = root.derivePath("m/"+deriv+"'/0'/"+account+"'");
   var xpub = acct.neutered().toBase58();
   return{
       xpub
   }
}

function seedToXprv(seed, deriv, account){
   var root = bitcoin.HDNode.fromSeedHex(seed);
   var acct = root.derivePath("m/"+deriv+"'/0'/"+account+"'");
   var xprv = acct.toBase58();
   return{
       xprv
   }
}

function convertXpub(xpub,target){
    //source script for converter 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'],
      ['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 public key! Please double check that you didn't accidentally paste extra data.";
  }    
}

var mySeed = "114259550292e14ca462e5202875f987fa3c93300306417c8b62de591710e4b34340a9bcdf8241ebbe83cf3cdf428c2f32973c9db68e247ce623fd73b9bd1fb2";
var extPriv = seedToXprv(mySeed,84,0);
var zprv = convertXpub(extPriv,"zprv"); 

//zprvAcihK1ERNzUfX6zh3v8weMVw5s8Y2xkmjdEfeFF3v9X2jbDf9iJ4vS5t4Po6Jhwsjkzjz1TddTENc5dPVsVFyMZPzWz8Kskci6wkYSmuhup
1
  • If my xprv has the right derivation path baked in, how do I convert it to zprv?
    – Meglio
    Mar 30, 2023 at 3:18

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.