1

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?

1 Answer 1

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.

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.