#!/usr/bin/env python3

###
### This entire thing is unnecessary, the cli dumpprivkey already generates wif's
###


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

import base58
#private_key = '0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D'
#private_key = base58.b58decode(private_key_base58).hex()
#private_key = '42bd7fb0a080ea8ca01ba0e2232ef70622a6863d536c51059985aead206351529d01' # 'T' prefix instead of 'X'
#private_key = '3f6c8d8b04c6a1e664b1ec20ec932760760c97688e' # 'S' prefix instead of '7'
private_key = '43bd7fb0a080ea8ca01ba0e2232ef70622a6863d536c51059985aead206351529d01' # 'A' or 'B' prefix instead of '7' or 'X'
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 = 'B4AKmn4JgUaLS88zue2hNaEHo3Zw3VP9sJB6Jm9Ysc7Mefnk8mcZ'
wif = 'XHdzZw6k3XJ3aWdvxgGQtdCJdEndXrPBXcJ9nysh4tpjCxgs8Yjs'
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)
