<p>The short answer is: you probably shouldn't.</p>

<p>That is not a typical way to generate an <code>xpriv</code>/<code>xpub</code>. You are trying to convert a key pair to an extended key pair. The difference is the extended keys include the following (<a href="https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format" rel="nofollow noreferrer">See BIP32 - Serialization Format)</a>:</p>

<ul>
<li><code>4 byte</code>: version bytes (mainnet: <code>0x0488B21E</code> public, <code>0x0488ADE4</code> private; testnet: <code>0x043587CF</code> public, <code>0x04358394</code> private)</li>
<li><code>1 byte</code>: depth: <code>0x00</code> for master nodes, <code>0x01</code> for level-1 derived keys, ....</li>
<li><code>4 bytes</code>: the fingerprint of the parent's key (<code>0x00000000</code> if master key)</li>
<li><code>4 bytes</code>: child number. This is ser32(i) for i in xi = xpar/i, with xi the key being serialized. (<code>0x00000000</code> if master key)</li>
<li><code>32 bytes</code>: the chain code</li>
<li><code>33 bytes</code>: the public key or private key data (serP(K) for public keys, 0x00 || ser256(k) for private keys)</li>
</ul>

<p>BIP32 standard <a href="https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#master-key-generation" rel="nofollow noreferrer">Master Key generation</a> goes like this:</p>

<ul>
<li>Generate a seed byte sequence S of a chosen length (between 128 and 512 bits; 256 bits is advised) from a (P)RNG.</li>
<li>Calculate I = HMAC-SHA512(Key = "Bitcoin seed", Data = S)</li>
<li>Split I into two 32-byte sequences, IL and IR.</li>
<li>Use parse256(IL) as master secret key, and IR as master chain code.</li>
</ul>

<p>With bitcoinjs-lib, a more <a href="https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.js#L19" rel="nofollow noreferrer">standard approach</a> would be:</p>

<pre><code>const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost'
const seed = bip39.mnemonicToSeed(mnemonic)
const node = bip32.fromSeed(seed)
console.log(node.toBase58())

'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkTcaMEcjq1vG7XmEEW22S2UiSPbrzTkUeXfcwo7PzRZ4aDiRQu'
</code></pre>

<p>If you absolutely had to do it, technically you could (not recommended) replace the private key property of the <code>node</code> object with a buffer of your own private key, i.e. <code>__d</code>:</p>

<pre><code>&gt; const bip32 = require('bip32')
undefined
&gt; let node = bip32.fromBase58('xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi')
undefined
&gt; node
BIP32 {
  __d: &lt;Buffer e8 f3 2e 72 3d ec f4 05 1a ef ac 8e 2c 93 c9 c5 b2 14 31 38 17 cd b0 1a 14 94 b9 17 c8 43 6b 35&gt;,
  __Q: null,
  chainCode: &lt;Buffer 87 3d ff 81 c0 2f 52 56 23 fd 1f e5 16 7e ac 3a 55 a0 49 de 3d 31 4b b4 2e e2 27 ff ed 37 d5 08&gt;,
  depth: 0,
  index: 0,
  network: { wif: 128, bip32: { public: 76067358, private: 76066276 } },
  parentFingerprint: 0 }
&gt; node.__d = Buffer.from('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'hex')
&lt;Buffer e3 b0 c4 42 98 fc 1c 14 9a fb f4 c8 99 6f b9 24 27 ae 41 e4 64 9b 93 4c a4 95 99 1b 78 52 b8 55&gt;
&gt; node
BIP32 {
  __d: &lt;Buffer e3 b0 c4 42 98 fc 1c 14 9a fb f4 c8 99 6f b9 24 27 ae 41 e4 64 9b 93 4c a4 95 99 1b 78 52 b8 55&gt;,
  __Q: null,
  chainCode: &lt;Buffer 87 3d ff 81 c0 2f 52 56 23 fd 1f e5 16 7e ac 3a 55 a0 49 de 3d 31 4b b4 2e e2 27 ff ed 37 d5 08&gt;,
  depth: 0,
  index: 0,
  network: { wif: 128, bip32: { public: 76067358, private: 76066276 } },
  parentFingerprint: 0 }
</code></pre>

CC BY-SA 4.0
2018-11-27T14:47:59.720Z
2019-07-16T12:02:16.830Z
2019-07-16T12:02:16.830Z
JBaczuk
<p>I have this code:</p>

<pre><code>const bitcoin = require('bitcoinjs-lib');
const keyPair = bitcoin.ECPair.makeRandom({ });
</code></pre>

<p>Question: how do I get xpriv and xpub from keyPair</p>

<bitcoinjs>
How to get xpriv and xpub from keyPair with bitcoinjs-lib
jfjobidon
# How to get xpriv and xpub from keyPair with bitcoinjs-lib

(Asked by: username_0 on 2018-11-27)

I have this code:

```
const bitcoin = require('bitcoinjs-lib');
const keyPair = bitcoin.ECPair.makeRandom({ });

```

Question: how do I get xpriv and xpub from keyPair

**Tags:** bitcoinjs


---

## 1 answers

### Answer by username_1 on 2018-11-27. Score: 4 | Accepted answer

The short answer is: you probably shouldn't.

That is not a typical way to generate an `xpriv`/`xpub`. You are trying to convert a key pair to an extended key pair. The difference is the extended keys include the following ([See BIP32 - Serialization Format)](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format):

* `4 byte`: version bytes (mainnet: `0x0488B21E` public, `0x0488ADE4` private; testnet: `0x043587CF` public, `0x04358394` private)
* `1 byte`: depth: `0x00` for master nodes, `0x01` for level-1 derived keys, ....
* `4 bytes`: the fingerprint of the parent's key (`0x00000000` if master key)
* `4 bytes`: child number. This is ser32(i) for i in xi = xpar/i, with xi the key being serialized. (`0x00000000` if master key)
* `32 bytes`: the chain code
* `33 bytes`: the public key or private key data (serP(K) for public keys, 0x00 || ser256(k) for private keys)

BIP32 standard [Master Key generation](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#master-key-generation) goes like this:

* Generate a seed byte sequence S of a chosen length (between 128 and 512 bits; 256 bits is advised) from a (P)RNG.
* Calculate I = HMAC-SHA512(Key = "Bitcoin seed", Data = S)
* Split I into two 32-byte sequences, IL and IR.
* Use parse256(IL) as master secret key, and IR as master chain code.

With bitcoinjs-lib, a more [standard approach](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.js#L19) would be:

```
const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost'
const seed = bip39.mnemonicToSeed(mnemonic)
const node = bip32.fromSeed(seed)
console.log(node.toBase58())

'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkTcaMEcjq1vG7XmEEW22S2UiSPbrzTkUeXfcwo7PzRZ4aDiRQu'

```

If you absolutely had to do it, technically you could (not recommended) replace the private key property of the `node` object with a buffer of your own private key, i.e. `__d`:

```
> const bip32 = require('bip32')
undefined
> let node = bip32.fromBase58('xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi')
undefined
> node
BIP32 {
  __d: <Buffer e8 f3 2e 72 3d ec f4 05 1a ef ac 8e 2c 93 c9 c5 b2 14 31 38 17 cd b0 1a 14 94 b9 17 c8 43 6b 35>,
  __Q: null,
  chainCode: <Buffer 87 3d ff 81 c0 2f 52 56 23 fd 1f e5 16 7e ac 3a 55 a0 49 de 3d 31 4b b4 2e e2 27 ff ed 37 d5 08>,
  depth: 0,
  index: 0,
  network: { wif: 128, bip32: { public: 76067358, private: 76066276 } },
  parentFingerprint: 0 }
> node.__d = Buffer.from('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'hex')
<Buffer e3 b0 c4 42 98 fc 1c 14 9a fb f4 c8 99 6f b9 24 27 ae 41 e4 64 9b 93 4c a4 95 99 1b 78 52 b8 55>
> node
BIP32 {
  __d: <Buffer e3 b0 c4 42 98 fc 1c 14 9a fb f4 c8 99 6f b9 24 27 ae 41 e4 64 9b 93 4c a4 95 99 1b 78 52 b8 55>,
  __Q: null,
  chainCode: <Buffer 87 3d ff 81 c0 2f 52 56 23 fd 1f e5 16 7e ac 3a 55 a0 49 de 3d 31 4b b4 2e e2 27 ff ed 37 d5 08>,
  depth: 0,
  index: 0,
  network: { wif: 128, bip32: { public: 76067358, private: 76066276 } },
  parentFingerprint: 0 }

```


---

