Skip to content

Instantly share code, notes, and snippets.

@KolbyML
Forked from t4sk/wif.md
Last active December 26, 2018 22:14
Show Gist options
  • Save KolbyML/f42932898b462cb5fff898d7126ec5f8 to your computer and use it in GitHub Desktop.
Save KolbyML/f42932898b462cb5fff898d7126ec5f8 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
dinero 1E
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

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