Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/address.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2016-2019 The Bitcoin Core developers | # Copyright (c) 2016-2019 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Encode and decode BASE58, P2PKH and P2SH addresses.""" | """Encode and decode BASE58, P2PKH and P2SH addresses.""" | ||||
import unittest | |||||
from .script import CScript, hash160, hash256 | from .script import CScript, hash160, hash256 | ||||
from .util import hex_str_to_bytes | from .util import hex_str_to_bytes, assert_equal | ||||
ADDRESS_BCHREG_UNSPENDABLE = 'bchreg:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqha9s37tt' | ADDRESS_BCHREG_UNSPENDABLE = 'bchreg:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqha9s37tt' | ||||
ADDRESS_BCHREG_UNSPENDABLE_DESCRIPTOR = 'addr(bchreg:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqha9s37tt)#l2wkrsqu' | ADDRESS_BCHREG_UNSPENDABLE_DESCRIPTOR = 'addr(bchreg:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqha9s37tt)#l2wkrsqu' | ||||
chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | ||||
def byte_to_base58(b, version): | def byte_to_base58(b, version): | ||||
result = '' | result = '' | ||||
str = b.hex() | str = b.hex() | ||||
str = chr(version).encode('latin-1').hex() + str | str = chr(version).encode('latin-1').hex() + str | ||||
checksum = hash256(hex_str_to_bytes(str)).hex() | checksum = hash256(hex_str_to_bytes(str)).hex() | ||||
str += checksum[:8] | str += checksum[:8] | ||||
value = int('0x' + str, 0) | value = int('0x' + str, 0) | ||||
while value > 0: | while value > 0: | ||||
result = chars[value % 58] + result | result = chars[value % 58] + result | ||||
value //= 58 | value //= 58 | ||||
while (str[:2] == '00'): | while (str[:2] == '00'): | ||||
result = chars[0] + result | result = chars[0] + result | ||||
str = str[2:] | str = str[2:] | ||||
return result | return result | ||||
# TODO: def base58_decode | |||||
def base58_to_byte(s, verify_checksum=True): | |||||
if not s: | |||||
return b'' | |||||
n = 0 | |||||
for c in s: | |||||
n *= 58 | |||||
assert c in chars | |||||
digit = chars.index(c) | |||||
n += digit | |||||
h = '{:x}'.format(n) | |||||
if len(h) % 2: | |||||
h = '0' + h | |||||
res = n.to_bytes((n.bit_length() + 7) // 8, 'big') | |||||
pad = 0 | |||||
for c in s: | |||||
if c == chars[0]: | |||||
pad += 1 | |||||
else: | |||||
break | |||||
res = b'\x00' * pad + res | |||||
if verify_checksum: | |||||
assert_equal(hash256(res[:-4])[:4], res[-4:]) | |||||
return res[1:-4], int(res[0]) | |||||
def keyhash_to_p2pkh(hash, main=False): | def keyhash_to_p2pkh(hash, main=False): | ||||
assert (len(hash) == 20) | assert (len(hash) == 20) | ||||
version = 0 if main else 111 | version = 0 if main else 111 | ||||
return byte_to_base58(hash, version) | return byte_to_base58(hash, version) | ||||
Show All 22 Lines | |||||
def check_script(script): | def check_script(script): | ||||
if (isinstance(script, str)): | if (isinstance(script, str)): | ||||
script = hex_str_to_bytes(script) # Assuming this is hex string | script = hex_str_to_bytes(script) # Assuming this is hex string | ||||
if (isinstance(script, bytes) or isinstance(script, CScript)): | if (isinstance(script, bytes) or isinstance(script, CScript)): | ||||
return script | return script | ||||
assert False | assert False | ||||
class TestFrameworkScript(unittest.TestCase): | |||||
def test_base58encodedecode(self): | |||||
def check_base58(data, version): | |||||
self.assertEqual( | |||||
base58_to_byte(byte_to_base58(data, version)), | |||||
(data, version)) | |||||
check_base58( | |||||
b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', | |||||
111) | |||||
check_base58( | |||||
b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 111) | |||||
check_base58( | |||||
b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', | |||||
111) | |||||
check_base58( | |||||
b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', | |||||
111) | |||||
check_base58( | |||||
b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', | |||||
111) | |||||
check_base58( | |||||
b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', | |||||
111) | |||||
check_base58( | |||||
b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 0) | |||||
check_base58( | |||||
b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 0) | |||||
check_base58( | |||||
b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) | |||||
check_base58( | |||||
b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) | |||||
check_base58( | |||||
b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) | |||||
check_base58( | |||||
b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0) |