#!/usr/bin/env python3

private_key_base58 = '2Y7szr2rkY5irvwHLbb2g5engu8P2xWegeWJnnfBaTMMw3N'
print("private_key_base58:", private_key_base58)

import base58
#private_key = '0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D'
private_key = base58.b58decode(private_key_base58).hex()
if (private_key[-2:] == '01'):
    private_key = private_key[0:-2]
print("private_key:", private_key)

# add 0x01 at end if public key is compressed
#extended_key = '80' + private_key
#extended_key = '43' + private_key
extended_key = private_key
print("extended_key:  ", extended_key)

import hashlib
hash = hashlib.sha256(hashlib.sha256(bytes.fromhex(extended_key)).digest()).digest().hex()
hash = hash[0:8]
print("hash:", hash)

wif_hex = extended_key + hash
print("wif_hex:", wif_hex)

import base58
wif = base58.b58encode(bytes.fromhex(wif_hex))
print("wif:", wif)



wif = '3H86cX9TD7hdaLHmPMAuCERJrQEogXu9MTYjtbvbQhacVVTWSR4'
print("wif:", wif)
# now decode it again to verify
wif_decode_hex = base58.b58decode(wif).hex()
print("wif_decode_hex:", wif_decode_hex)
source_address = wif_decode_hex[0:-8]
hash = hashlib.sha256(hashlib.sha256(bytes.fromhex(source_address)).digest()).digest().hex()
hash = hash[0:8]
print("hash:", hash)

print("source_address:", source_address)
source_address_base58 = base58.b58encode(bytes.fromhex(source_address))
print("source_address_base58:", source_address_base58)
