<p>use <code>.toString('hex')</code> instead.</p>

<blockquote>
  <ul>
  <li>Don't export / import individual private keys when you are using BIP32 HD keys. IT IS DANGEROUS.</li>
  <li><code>.toString('hex')</code> and <code>.toString('base64')</code> are readable string representations of Buffers, but no crypto apps use them. The accepted format for individual private keys are WIF. But like I said, IF YOU ARE USING HD KEYS, ONLY STORE/EXPORT/IMPORT YOUR SEEDPHRASE OR AN XPUB.</li>
  </ul>
</blockquote>

<p>Source: From this <a href="https://github.com/bitcoinjs/bitcoinjs-lib/issues/1337" rel="nofollow noreferrer">Github thread</a>.</p>

CC BY-SA 4.0
2019-02-20T08:16:09.240Z
2019-02-20T08:16:09.240Z
mesqueeb
<p>I'm still new to bitcoin and experimenting a lot with bitcoinjs-lib in JavaScript.</p>

<p>How do I create a readable string from a private key?
This is my current setup:</p>



<pre><code>      const seed = bip39.mnemonicToSeed(seedPhrase)
      const bitcoinNetwork = bitcoin.networks.testnet
      const hdMaster = bitcoin.bip32.fromSeed(seed, bitcoinNetwork) // seed from above
      const keyPair = hdMaster.derivePath('m/0')
      // try to get private key as readable string
      const privateKeyBuffer = Buffer.from(keyPair.privateKey)
      const privateKey = privateKeyBuffer.toString('utf8')
      console.log('privateKey → ', privateKey)
      // fail to get private key as readable string
      const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey })
</code></pre>

<p>any ideas for me?</p>

<bitcoinjs>
How to retrieve readable private key from bitcoinjs-lib
mesqueeb
# How to retrieve readable private key from bitcoinjs-lib

(Asked by: username_0 on 2019-02-20)

I'm still new to bitcoin and experimenting a lot with bitcoinjs-lib in JavaScript.
How do I create a readable string from a private key?
This is my current setup:
      const seed = bip39.mnemonicToSeed(seedPhrase)
      const bitcoinNetwork = bitcoin.networks.testnet
      const hdMaster = bitcoin.bip32.fromSeed(seed, bitcoinNetwork) // seed from above
      const keyPair = hdMaster.derivePath('m/0')
      // try to get private key as readable string
      const privateKeyBuffer = Buffer.from(keyPair.privateKey)
      const privateKey = privateKeyBuffer.toString('utf8')
      console.log('privateKey → ', privateKey)
      // fail to get private key as readable string
      const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey })

any ideas for me?

**Tags:** bitcoinjs


---

## 1 answers

### Answer by username_0 on 2019-02-20. Score: 2

use `.toString('hex')` instead.

> * Don't export / import individual private keys when you are using BIP32 HD keys. IT IS DANGEROUS.
> * `.toString('hex')` and `.toString('base64')` are readable string representations of Buffers, but no crypto apps use them. The accepted format for individual private keys are WIF. But like I said, IF YOU ARE USING HD KEYS, ONLY STORE/EXPORT/IMPORT YOUR SEEDPHRASE OR AN XPUB.

Source: From this [Github thread](https://github.com/bitcoinjs/bitcoinjs-lib/issues/1337).


---

