fastapi/functions.py

49 lines
2.1 KiB
Python
Raw Normal View History

2023-03-10 10:46:21 +00:00
import dependencies.segwit_addr as segwit_addr
import dependencies.bchconvert as bchconvert
import dependencies.moneropy as moneropy
import base58, hashlib, binascii
import socket
def decodeBase58(address):
decoded = base58.b58decode(address).hex()
prefixAndHash = decoded[:len(decoded)-8]
checksum = decoded[len(decoded)-8:]
hash = prefixAndHash
for _ in range(1,3):
hash = hashlib.sha256(binascii.unhexlify(hash)).hexdigest()
if(checksum == hash[:8]):
return True
return False
def decodeMonero(address):
# decode monero address
if len(address) == 95 or len(address) == 106:
# Decode the address from Base58
decoded = moneropy.decode(address)
# Verify address type byte
if decoded[0:2] not in ["12", "2a", "13"]:
return False
# Verify checksum
# later
return True
return False
def checksumCheck(method, address):
match method.lower():
case 'btc':
return decodeBase58(address) if address[0] == '1' or address[0] == '3' else True if address[0:3] == 'bc1' and segwit_addr.decode("bc", address)[0] != None else False
2023-03-13 21:12:44 +00:00
case 'btct':
2023-03-13 21:34:43 +00:00
return decodeBase58(address) if address[0] == '2' else True if address[0:3] == 'tb1' and segwit_addr.decode("tb", address)[0] != None else False
2023-03-10 10:46:21 +00:00
case 'ltc':
return decodeBase58(address) if address[0] == '3' or address[0] == 'M' or address[0] == 'L' else True if address[0:4] == 'ltc1' and segwit_addr.decode("ltc", address)[0] != None else False
case 'bch':
return bchconvert.is_valid(address) if address[0] == '1' else True if bchconvert.is_valid('bitcoincash:'+address) == True else False
case 'zec':
return decodeBase58(address) if address[0] == 't' or address[0] == 'z' else False
case 'xmr':
#needs new function to check if address is valid, a decoder maybe
return decodeMonero(address)
case _:
2023-03-13 21:12:44 +00:00
return False