How to convert private key to WIF key

All around the blockchain development
Post Reply
User avatar
Site Admin
Site Admin
Posts: 27
Joined: Sun Aug 12, 2018 12:19 pm
Location: Europe

Mon Dec 31, 2018 6:51 pm

0. Overview

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

version byte (hex) = xx for mainnet, xx for testnet and regtest

checksum = first 4 bytes of double SHA256 of private key

1. Find the Version byte

In chainparams.cpp (sample): base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1, 98)

98 = dezeimal - hex 62

2. Add version byte

In this example

private key = 619c335025c7f4012e556c2a58b2506e30b8511b53ade95ea316fd8c3286feb9

Code: Select all

> export PRIV_KEY=619c335025c7f4012e556c2a58b2506e30b8511b53ade95ea316fd8c3286feb9
Add version byte in front of private key (ef for regtest)

Code: Select all

export VER=ef

Code: Select all

echo ${VER}${PRIV_KEY}

3. Compute checksum (Double SHA256)

Code: Select all

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

Take the first 4 bytes of the double SHA256 hash: 5ea65746

Code: Select all

export CHECKSUM=5ea65746
4. Append checksum

Code: Select all


5. Convert to Base58Check (encoder)


References: Wallet Import Format - Bitcoin Wiki

Post Reply