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 The Bitcoin Core developers | # Copyright (c) 2016 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.""" | ||||
from .script import CScript, hash160, hash256 | from .script import CScript, hash160, hash256 | ||||
from .util import bytes_to_hex_str, hex_str_to_bytes | from .util import hex_str_to_bytes | ||||
chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | ||||
def byte_to_base58(b, version): | def byte_to_base58(b, version): | ||||
result = '' | result = '' | ||||
str = bytes_to_hex_str(b) | str = b.hex() | ||||
str = bytes_to_hex_str(chr(version).encode('latin-1')) + str | str = chr(version).encode('latin-1').hex() + str | ||||
checksum = bytes_to_hex_str(hash256(hex_str_to_bytes(str))) | 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:] | ||||
▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines |