I wasn't aware of a private key format, however numerous stackoverflow posts hint towards a formatting of some kind

Format of private key

Which private key format is this?

What encoding or format is the private key in Dumpprivkey?

And looking at iancoleman/bip39's website, indeed the private key changes as you change the coin, implying that there is at least a format that takes into account version bytes.

However, using hdkey in js I am unable to reproduce this format, or even the "32-byte big-endian secret parameter" Pieter Wuille describes in the last example.

var bip39 = require('bip39');
var hdkey = require('hdkey');    

const mnemonic = 'thunder purchase pave tower lecture upgrade supreme half kid fitness tray shove'
const seed = bip39.mnemonicToSeed(mnemonic); //creates seed buffer
console.log('mnemonic: ' + mnemonic);

const root = hdkey.fromMasterSeed(seed);
const masterPrivateKey = root.privateKey.toString('hex');

const addrnode = root.derive("m/0'/0");
console.log('addrnodePublicKey: '+ addrnode._publicKey.toString('hex'))
console.log('addernodePrivateKey: ' + addrnode._privateKey.toString('hex'))

What is the detailed private key format? What does it consist of, how do you derive it from a standard private key? What is it used for compared to the unformatted private key?

The format you use depends on how you need to use the private key. Your code produces a raw hex private key, without formatting.

$ node index.js
mnemonic: thunder purchase pave tower lecture upgrade supreme half kid fitness tray shove
addrnodePublicKey: 02ceb48796223dc3777fe210a2034059b5e39b3743e59d62f75ef07a32f8440caf
addernodePrivateKey: 310fe2e677a3ad28acb91d2645bb33882f015ab11e59dce9d2a72905979e3cb6

Raw Private Key

A private key (in bitcoin, i.e. ECDSA SECP256K1) is a 32 byte number between 0x1 and 0xFFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFE BAAE DCE6 AF48 A03B BFD2 5E8C D036 4140.

For example:

0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

Wallet Input Format (WIF)

However, wallet software typically uses the Wallet Input Format (WIF) to make it easier to copy over. It uses base58check encoding so it avoids ambiguous characters (like 0 and O), it is shorter, and includes a checksum in case of typos. For example:

5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ

WIF Conversion

The process of converting a raw private key to WIF is outline here: https://en.bitcoin.it/wiki/Wallet_import_format:

  • Take a private key

    0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

  • Add a 0x80 byte in front of it for mainnet addresses or 0xef for testnet addresses. Also add a 0x01 byte at the end if the private key will correspond to a compressed public key

    800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

  • Perform SHA-256 hash on the extended key

    8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592

  • Perform SHA-256 hash on result of SHA-256 hash

    507A5B8DFED0FC6FE8801743720CEDEC06AA5C6FCA72B07C49964492FB98A714

  • Take the first 4 bytes of the second SHA-256 hash, this is the checksum

    507A5B8D

  • Add the 4 checksum bytes from point 5 at the end of the extended key from point 2

    800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D507A5B8D

  • Convert the result from a byte string into a base58 string using Base58Check encoding. This is the Wallet Import Format

    5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ

Potential node.js resources:

  • % echo 0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D | bx base58check-encode -v 128 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ – skaht Sep 14 at 1:02
  • % echo 0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D01 | bx base58check-encode -v 128 KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617 – skaht Sep 14 at 1:03

Somewhere I answered this already, but coldn't find it anymore... I was confused on the different keys as well, and created this graphic to have a clearer picture:

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.