<p>Your <code>xprv</code> already has the derivation path &quot;baked in&quot; so even if you were to just convert to <code>zprv</code> 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(&quot;m84'/...&quot;,&quot;m49/'/... etc), then you can create an extended public key. Then you'll have to convert to yprv, zprv, ypub, etc...</p>
<p>Example uses bitcoinjs-lib v3.3.2</p>
<pre><code>function seedToXpub(seed, deriv, account){
   var root = bitcoin.HDNode.fromSeedHex(seed);
   var acct = root.derivePath(&quot;m/&quot;+deriv+&quot;'/0'/&quot;+account+&quot;'&quot;);
   var xpub = acct.neutered().toBase58();
   return{
       xpub
   }
}

function seedToXprv(seed, deriv, account){
   var root = bitcoin.HDNode.fromSeedHex(seed);
   var acct = root.derivePath(&quot;m/&quot;+deriv+&quot;'/0'/&quot;+account+&quot;'&quot;);
   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 &quot;Invalid extended public key! Please double check that you didn't accidentally paste extra data.&quot;;
  }    
}

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

//zprvAcihK1ERNzUfX6zh3v8weMVw5s8Y2xkmjdEfeFF3v9X2jbDf9iJ4vS5t4Po6Jhwsjkzjz1TddTENc5dPVsVFyMZPzWz8Kskci6wkYSmuhup
</code></pre>

CC BY-SA 4.0
2023-03-30T03:18:28.690Z
If my xprv has the right derivation path baked in, how do I convert it to zprv?
Meglio
CC BY-SA 4.0
2021-05-14T05:16:34.650Z
2021-05-14T05:16:34.650Z
m1xolyd1an
<pre><code>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(&quot;LEGACY: &quot; + node.derivePath(&quot;m/44'/0'/0&quot;).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.
</code></pre>
<p>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.</p>

<segregated-witness><javascript>
generating a zprv from a base58 rootKey with bitcoinjs-lib
Not My Real Name
# generating a zprv from a base58 rootKey with bitcoinjs-lib

(Asked by: username_0 on 2021-05-13)

```
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.

**Tags:** segregated-witness, javascript


---

## 1 answers

### Answer by username_1 on 2021-05-14. Score: 0 | Accepted answer

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

```

> **Comments:**
> **username_2**: If my xprv has the right derivation path baked in, how do I convert it to zprv?  

---

