Closed SatoshiNakamotoBitcoin closed 2 weeks ago
import hashlib import base58 import binascii from binascii import hexlify, unhexlify
def doubleSha256(hex): """Double sha256 function""" bin = binascii.unhexlify(hex)
hash_1st = hashlib.sha256(bin).digest() # perform SHA-256 on the previous SHA-256 hash hash_2nd = hashlib.sha256(hash_1st).digest() return binascii.hexlify(hash_2nd)
private_key_WIF = "cVbZ8ovhye9AoAHFsqobCf7LxbXDAECy9Kb8TZdfsDYMZGBUyCnm" # compressed
first_decode = base58.b58decode(private_key_WIF) private_key_full = binascii.hexlify(first_decode)
if private_key_full.startswith(b"80"): testnet = False elif private_key_full.startswith(b"ef"): testnet = True
private_key_full = private_key_full[2:-8]
if private_key_full.endswith(b"01"): private_key_full = private_key_full[:-2]
if not testnet:
text = b"80" + private_key_full
elif testnet:
text = b"ef" + private_key_full
double_sha256 = doubleSha256(text)
checksum = double_sha256[0:8]
private_key_with_checksum = text+checksum
uncompressed_wif = base58.b58encode(binascii.unhexlify(private_key_with_checksum)) print(f"{uncompressed_wif=}")
Script to convert compressed WIF to private key then to uncompressed WIF.
import hashlib import base58 import binascii from binascii import hexlify, unhexlify
def doubleSha256(hex): """Double sha256 function""" bin = binascii.unhexlify(hex)
perform SHA-256 hash on the private_key
Testnet WIF
private_key_WIF = "cVbZ8ovhye9AoAHFsqobCf7LxbXDAECy9Kb8TZdfsDYMZGBUyCnm" # compressed
~ private_key_WIF_uncompressed = "93QEdCzUrzMRsnbx2YuF6MsNjxQA6iWSrv9e2wX4NM4UmYzUsLn"
first_decode = base58.b58decode(private_key_WIF) private_key_full = binascii.hexlify(first_decode)
Check if the key is mainnet or testnet
if private_key_full.startswith(b"80"): testnet = False elif private_key_full.startswith(b"ef"): testnet = True
private_key_full = private_key_full[2:-8]
if private_key_full.endswith(b"01"): private_key_full = private_key_full[:-2]
if not testnet:
prepended mainnet version byte to private key
elif testnet:
prepended testnet version byte to private key
Perfrom double sha256 on string
double_sha256 = doubleSha256(text)
create a checksum using the first 4 bytes of the previous SHA-256 hash
checksum = double_sha256[0:8]
append the 4 checksum bytes to the private_key
private_key_with_checksum = text+checksum
convert mainnet_private_key + checksum into base58 encoded string
uncompressed_wif = base58.b58encode(binascii.unhexlify(private_key_with_checksum)) print(f"{uncompressed_wif=}")