How to convert private key to WIF key

All around the blockchain development
Post Reply
User avatar
GOSSIP-TEAM
Site Admin
Site Admin
Posts: 56
Joined: Sun Aug 12, 2018 12:19 pm
Location: Europe
Contact:

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}
ef619c335025c7f4012e556c2a58b2506e30b8511b53ade95ea316fd8c3286feb9

3. Compute checksum (Double SHA256)

Code: Select all

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

Code: Select all

export CHECKSUM=5ea65746
4. Append checksum

Code: Select all

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

5. Convert to Base58Check (encoder)

92KuV1Mtf9jTttTrw1yawobsa9uCZGbfpambH8H1Y7KfdDxxc4d

References: Wallet Import Format - Bitcoin Wiki

Source
Post Reply