0
$\begingroup$

According to this webpage:

The process of encoding a private key into WIF format is as follows. Raw private keys are simply large numbers, which are represented as bytes. WIF format adds a prefix byte (0x80 for mainnet and 0xef for testnet) so that almost all Bitcoin private keys begin with ‘5’ or a ‘K’ on mainnet. Next, a ‘0x01’ byte is added to the end of the private key if its corresponding public key should use compressed SEC format. Finally, a four byte double SHA-256 checksum is appended to the byte-encoded private key, in order to prevent typos or tampering. This byte string is then converted from bytes to Base58Check.

Below is an example of a private key displayed as a hexadecimal number and in WIF format.

Hex: 0x224b2d71866c35d3701f0fcdd7871cb191c2ae25068602759fcb9b59d9100e00

WIF: 5J5PZqvCe1uThJ3FZeUUFLCh2FuK9pZhtEK4MzhNmugqTmxCdwE

However, the website doesn't explain step-by-step how the Hex converts to WIF.

Request someone to please demonstrate it step-by-step, using online tools such as this and this and this?

$\endgroup$
3
  • 2
    $\begingroup$ Encoding a private key with an online tool is foolish: the online tool can keep a copy of the private key. In a cryptocurrency context, that amounts to giving open access to one's safe. $\endgroup$
    – fgrieu
    Commented Aug 1, 2022 at 8:52
  • $\begingroup$ @fgrieu Fair enough. I'm merely trying to understand the process using a "throwaway" private key. Not planning to encode my own private key this way. $\endgroup$ Commented Aug 1, 2022 at 21:33
  • 1
    $\begingroup$ The 4-byte checksum is part of bitcoin's base58check, so it isn't really checksum 'then' base58check, rather base58check is checksum-then-base58. More substantively it does NOT prevent tampering, only accidents. $\endgroup$ Commented Aug 2, 2022 at 1:25

1 Answer 1

3
$\begingroup$

Using the base58 encoder/decoder here (in hex mode), you can see that 5J5PZqvCe1uThJ3FZeUUFLCh2FuK9pZhtEK4MzhNmugqTmxCdwE is 80224b2d71866c35d3701f0fcdd7871cb191c2ae25068602759fcb9b59d9100e007ac24c81 in hex.

Now, it's clear to see that it's the hex string in your question, with 80 prepended (to indicate a mainnet address), and with the "double SHA-256 checksum" 7ac24c81 appended.

To get 7ac24c81 from 80224b2d71866c35d3701f0fcdd7871cb191c2ae25068602759fcb9b59d9100e00, put it into SHA-256 (use hex mode) once to get 1651c54d5e52bdc693d12a07838dca644719660518ee56316e200b0316f0a5f3, and then put that into SHA-256 a second time to get 7ac24c81d45ddec22d5070661edc732cde65e7f25db58b696b4ef7db54073064. The first 8 characters of that string are 7ac24c81.

$\endgroup$
4
  • $\begingroup$ Wonderful explanation. Thank you. Exactly what I was looking for. $\endgroup$ Commented Aug 1, 2022 at 21:28
  • $\begingroup$ Follow-up question for you: Can you please recommend me a couple of resources to understand Bitcoin addressing. For example, I want to be crystal clear about issues such as: when using a private key to access Bitcoins, would the person use an address that looked like 5J5PZqvCe1uThJ3FZeU... or like 0x224b2d71866c35d.... Etc. Thanks. $\endgroup$ Commented Aug 1, 2022 at 21:36
  • 1
    $\begingroup$ @thanks_in_advance Almost everyone uses the same standard for receiving addresses, because otherwise people would get very confused when trying to ask for payment. But private keys are generally only seen by your own wallet, and so every wallet can decide on a different way to present or store a private key. E.g. I think the popular electrum wallet will only let you see your seed words, and won't even show you a hex representation of your private key. And there are many ways to implement seed words (with different types of checksum), and different ways of presenting the raw private key $\endgroup$
    – knaccc
    Commented Aug 1, 2022 at 22:00
  • $\begingroup$ Wow. A wallet that won't let you see your Private Keys. Interesting. Thanks! $\endgroup$ Commented Aug 2, 2022 at 3:15

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.