def hash_160_to_btc_address(h160, v):
""" Calculates the Bitcoin address of a given RIPEMD-160 hash from an elliptic curve public key.
:param h160: RIPEMD-160 hash.
:type h160: bytes
:param v: version (prefix) used to calculate the Bitcoin address.
Possible values:
- 0 for main network (PUBKEY_HASH).
- 111 For testnet (TESTNET_PUBKEY_HASH).
:type v: int
:return: The corresponding Bitcoin address.
:rtype: hex str
"""
# If h160 is passed as hex str, the value is converted into bytes.
if match('^[0-9a-fA-F]*$', h160):
h160 = unhexlify(h160)
# Add the network version leading the previously calculated RIPEMD-160 hash.
vh160 = chr(v) + h160
# Double sha256.
h = sha256(sha256(vh160).digest()).digest()
# Add the two first bytes of the result as a checksum tailing the RIPEMD-160 hash.
addr = vh160 + h[0:4]
# Obtain the Bitcoin address by Base58 encoding the result
addr = b58encode(addr)
return addr
评论列表
文章目录