How to retrieve readable private key from bitcoinjs-lib

0

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?

mesqueeb

Posted 2019-02-20T07:57:23.240

Reputation: 101

Answers

0

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.

mesqueeb

Posted 2019-02-20T07:57:23.240

Reputation: 101