def hash_160_bytes_to_address(h160, addrtype=PUBKEY_ADDRESS): if addrtype == PUBKEY_ADDRESS: prefix = chr(ADDRESS_PREFIXES[lbryschema.BLOCKCHAIN_NAME][PUBKEY_ADDRESS]) elif addrtype == SCRIPT_ADDRESS: prefix = chr(ADDRESS_PREFIXES[lbryschema.BLOCKCHAIN_NAME][SCRIPT_ADDRESS]) else: raise Exception("Invalid address prefix") return b58encode(prefix + h160 + double_sha256(prefix + h160)[0:4])
def test_verify_proof(self): claim1_name = 97 # 'a' claim1_txid = 'bd9fa7ffd57d810d4ce14de76beea29d847b8ac34e8e536802534ecb1ca43b68' claim1_outpoint = 0 claim1_height = 10 claim1_node_hash = get_hash_for_outpoint( unhexlify(claim1_txid)[::-1], claim1_outpoint, claim1_height) claim2_name = 98 # 'b' claim2_txid = 'ad9fa7ffd57d810d4ce14de76beea29d847b8ac34e8e536802534ecb1ca43b68' claim2_outpoint = 1 claim2_height = 5 claim2_node_hash = get_hash_for_outpoint( unhexlify(claim2_txid)[::-1], claim2_outpoint, claim2_height) to_hash1 = claim1_node_hash hash1 = double_sha256(to_hash1) to_hash2 = bytes((claim1_name, )) + hash1 + bytes( (claim2_name, )) + claim2_node_hash root_hash = double_sha256(to_hash2) proof = { 'last takeover height': claim1_height, 'txhash': claim1_txid, 'nOut': claim1_outpoint, 'nodes': [ { 'children': [{ 'character': 97 }, { 'character': 98, 'nodeHash': hexlify(claim2_node_hash[::-1]) }] }, { 'children': [] }, ] } out = verify_proof(proof, hexlify(root_hash[::-1]), 'a') self.assertEqual(out, True)
def validate_b58_checksum(addr_bytes): addr_without_checksum = addr_bytes[:-ADDRESS_CHECKSUM_LENGTH] addr_checksum = addr_bytes[-ADDRESS_CHECKSUM_LENGTH:] if double_sha256( addr_without_checksum)[:ADDRESS_CHECKSUM_LENGTH] != addr_checksum: raise InvalidAddress("Invalid address checksum")
def b58encode_with_checksum(addr_bytes): addr_checksum = double_sha256(addr_bytes)[:ADDRESS_CHECKSUM_LENGTH] return b58encode(addr_bytes + addr_checksum)