A Key Note To Key Formats

🎼

Mi'kail Eli'yah
5 min readMar 4, 2024

Wallet Import Format (WIF)

Wallet import format (WIF, aka wallet export format) is a way of encoding a private ECDSA key so as to make it easier to copy.

import base58
import hashlib

def private_key_to_wif(private_key_hex, testnet=False, compressed=True):
# Step 1: Add prefix (mainnet or testnet) and suffix (compressed)
prefix = "ef" if testnet else "80"
suffix = "01" if compressed else ""
extended_key_hex = prefix + private_key_hex + suffix

# Step 2: Perform SHA-256 hash on the extended key
first_sha256 = hashlib.sha256(bytes.fromhex(extended_key_hex)).hexdigest()

# Step 3: Perform SHA-256 hash on result of SHA-256 hash
second_sha256 = hashlib.sha256(bytes.fromhex(first_sha256)).hexdigest()

# Step 4: Take the first 4 bytes of the second SHA-256 hash; this is the checksum
checksum = second_sha256[:8]

# Step 5: Add the 4 checksum bytes at the end of the extended key
extended_key_with_checksum = extended_key_hex + checksum

# Step 6: Convert the result from a byte string into a base58 string using Base58Check encoding
wif_encoded = base58.b58encode(bytes.fromhex(extended_key_with_checksum))

return wif_encoded.decode()


def wif_to_private_key(wif_string):
# Step 1: Convert WIF to byte string using Base58Check encoding
wif_bytes =…

--

--