#!/usr/bin/env python3

###
### Takes a dash-WIF and translates to a titus-WIF
###


wifs = [
  'XjnkiGYQkC3bbAzvDjP7jkNouHCHNRr3ug'
]

for wif in wifs:
  #print("wif:", wif)
  # now decode it again to verify
  import base58
  wif_decode_hex = base58.b58decode(wif).hex()
  #print("wif_decode_hex:", wif_decode_hex)
  source_hash = wif_decode_hex[-8:]
  source_address = wif_decode_hex[0:-8]
  import hashlib
  hash = hashlib.sha256(hashlib.sha256(bytes.fromhex(source_address)).digest()).digest().hex()
  hash = hash[0:8]
  if source_hash != hash:
    #raise ValueError("Hash mismatch, expected {}, got {}".format(source_hash, hash))
    pass
  #print("hash:", hash)
  #print("source_address:", source_address)
  prefix = source_address[0:2]
  #print("prefix:", prefix)
  data_payload = source_address[2:]
  #print("data_payload:", data_payload)

  private_key = None
  if prefix == 'cc':
    # livenet private key
    private_key = '43' + data_payload
  elif prefix == '10':
    # livenet script
    private_key = '3f' + data_payload
  elif prefix == '8c':
    # testnet public key
    private_key = '8c' + data_payload
  elif prefix == '4c':
    # livenet public key
    private_key = '42' + data_payload
  else:
    raise ValueError("Unknown prefix: " + prefix)

  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
  result = base58.b58encode(bytes.fromhex(wif_hex))
  #print("result:", result)

  print("{} => {}".format(wif, result))
