Skip to content

Instantly share code, notes, and snippets.

@t4sk
Last active August 1, 2023 20:15
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save t4sk/ac6f2d607c96156ca15f577290716fcc to your computer and use it in GitHub Desktop.
Save t4sk/ac6f2d607c96156ca15f577290716fcc to your computer and use it in GitHub Desktop.
How to convert private key to WIF

How to convert private key to WIF

0. Overview

WIF = base58check encode ([version byte][private key][checksum])

version byte = 80 for mainnet, ef for testnet and regtest

checksum = first 4 bytes of double SHA256 of private key

1. Add version byte

In this example

  • private key = 619c335025c7f4012e556c2a58b2506e30b8511b53ade95ea316fd8c3286feb9

    > export PRIV_KEY=619c335025c7f4012e556c2a58b2506e30b8511b53ade95ea316fd8c3286feb9
    

Add version byte in front of private key (ef for regtest)

> export VER=ef

> echo ${VER}${PRIV_KEY}
ef619c335025c7f4012e556c2a58b2506e30b8511b53ade95ea316fd8c3286feb9
Network Version Byte (Hex)
mainnet 80
testnet ef
regtest ef

2. Compute checksum (Double SHA256)

> echo ${VER}${PRIV_KEY} -n | xxd -r -p | openssl dgst -sha256 -binary | openssl dgst -sha256
5ea6574663729d86cdc55bc9b7b47eda13a7ae8e4c7bc7084e248f8ddd755cbc

Take the first 4 bytes of the double SHA256 hash

5ea65746

> export CHECKSUM=5ea65746

3. Append checksum

> echo ${VER}${PRIV_KEY}${CHECKSUM}
ef619c335025c7f4012e556c2a58b2506e30b8511b53ade95ea316fd8c3286feb95ea65746

4. Convert to Base58Check (encoder)

92KuV1Mtf9jTttTrw1yawobsa9uCZGbfpambH8H1Y7KfdDxxc4d

5. Verify (Optional)

In bitcoin-qt console

> importprivkey 92KuV1Mtf9jTttTrw1yawobsa9uCZGbfpambH8H1Y7KfdDxxc4d "test-priv-key"
null

> getaddressesbyaccount "test-priv-key"
[
  "mi7uHKSho5sj9EwN8Tat4GuLu5ZjbJqT4Q"
]

References

Wallet Import Format - Bitcoin Wiki

@NathanHazout
Copy link

Step 4 fails for me, Invalid Base58 Character(s)!

Copy link

ghost commented Apr 10, 2020

Step 4 fails for me, Invalid Base58 Character(s)!

Same here

@t4sk
Copy link
Author

t4sk commented Apr 12, 2020

@WillWammer
base58 encoding ef619c335025c7f4012e556c2a58b2506e30b8511b53ade95ea316fd8c3286feb95ea65746 from step 3 returns
92KuV1Mtf9jTttTrw1yawobsa9uCZGbfpambH8H1Y7KfdDxxc4d in step 4

Did that resolve your issue?

Copy link

ghost commented Apr 13, 2020

@t4sk
Well yes, clearly THAT encoding is valid. Something on my end with the previous steps probably, I'll look into it seeing this is clearly more maintained than I thought

@Odot2456
Copy link

I need some help with Point 5 ??
When I have Base 58 what do I have then to do?? to catch the WIF Format?

@t4sk
Copy link
Author

t4sk commented Apr 30, 2020

@Odot2456 open the console from bitcoin-qt and type the command in step 5

The bitcoin-qt console should look something like this
https://www.youtube.com/watch?v=PN1oTxnAUUE&t=160

This was more than 2 years ago, so things might have changed

@smaicloud
Copy link

smaicloud commented May 29, 2020

This guide works great and all steps so easy. When you are copy and paste things, do it with brain, not with the mouse.

@t4sk
Copy link
Author

t4sk commented May 29, 2020

This guide works great and all steps so easy. When you are copy and paste things, do it with brain, not with the mouse.

Thanks. I'm surprised it still works

@TahiruMorales
Copy link

step five doesn't seem to work for me

@GregTonoski
Copy link

step five doesn't seem to work for me

There is the shell script that converts a private key from hex to WIF Bitcoin: https://gist.github.com/GregTonoski/438992249df6e4bd613f9758421ff38a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment