Changeset View
Changeset View
Standalone View
Standalone View
contrib/testgen/base58.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2012-2017 The Bitcoin Core developers | # Copyright (c) 2012-2017 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. | ||||
''' | """ | ||||
Bitcoin base58 encoding and decoding. | Bitcoin base58 encoding and decoding. | ||||
Based on https://bitcointalk.org/index.php?topic=1026.0 (public domain) | Based on https://bitcointalk.org/index.php?topic=1026.0 (public domain) | ||||
''' | """ | ||||
import hashlib | import hashlib | ||||
# for compatibility with following code... | # for compatibility with following code... | ||||
class SHA256: | class SHA256: | ||||
new = hashlib.sha256 | new = hashlib.sha256 | ||||
if str != bytes: | if str != bytes: | ||||
# Python 3.x | # Python 3.x | ||||
def ord(c): # noqa: A001 | def ord(c): # noqa: A001 | ||||
return c | return c | ||||
def chr(n): # noqa: A001 | def chr(n): # noqa: A001 | ||||
return bytes((n,)) | return bytes((n,)) | ||||
__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | |||||
__b58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" | |||||
__b58base = len(__b58chars) | __b58base = len(__b58chars) | ||||
b58chars = __b58chars | b58chars = __b58chars | ||||
def b58encode(v): | def b58encode(v): | ||||
""" encode v, which is a string of bytes, to base58. | """encode v, which is a string of bytes, to base58.""" | ||||
""" | |||||
long_value = 0 | long_value = 0 | ||||
for (i, c) in enumerate(v[::-1]): | for i, c in enumerate(v[::-1]): | ||||
if isinstance(c, str): | if isinstance(c, str): | ||||
c = ord(c) | c = ord(c) | ||||
long_value += (256**i) * c | long_value += (256**i) * c | ||||
result = '' | result = "" | ||||
while long_value >= __b58base: | while long_value >= __b58base: | ||||
div, mod = divmod(long_value, __b58base) | div, mod = divmod(long_value, __b58base) | ||||
result = __b58chars[mod] + result | result = __b58chars[mod] + result | ||||
long_value = div | long_value = div | ||||
result = __b58chars[long_value] + result | result = __b58chars[long_value] + result | ||||
# Bitcoin does a little leading-zero-compression: | # Bitcoin does a little leading-zero-compression: | ||||
# leading 0-bytes in the input become leading-1s | # leading 0-bytes in the input become leading-1s | ||||
nPad = 0 | nPad = 0 | ||||
for c in v: | for c in v: | ||||
if c == 0: | if c == 0: | ||||
nPad += 1 | nPad += 1 | ||||
else: | else: | ||||
break | break | ||||
return (__b58chars[0] * nPad) + result | return (__b58chars[0] * nPad) + result | ||||
def b58decode(v, length=None): | def b58decode(v, length=None): | ||||
""" decode v into a string of len bytes | """decode v into a string of len bytes""" | ||||
""" | |||||
long_value = 0 | long_value = 0 | ||||
for i, c in enumerate(v[::-1]): | for i, c in enumerate(v[::-1]): | ||||
pos = __b58chars.find(c) | pos = __b58chars.find(c) | ||||
assert pos != -1 | assert pos != -1 | ||||
long_value += pos * (__b58base**i) | long_value += pos * (__b58base**i) | ||||
result = bytes() | result = bytes() | ||||
while long_value >= 256: | while long_value >= 256: | ||||
Show All 33 Lines | if result is None: | ||||
return None | return None | ||||
if result[-4:] == checksum(result[:-4]): | if result[-4:] == checksum(result[:-4]): | ||||
return result[:-4] | return result[:-4] | ||||
else: | else: | ||||
return None | return None | ||||
def get_bcaddress_version(strAddress): | def get_bcaddress_version(strAddress): | ||||
""" Returns None if strAddress is invalid. Otherwise returns integer version of address. """ | """Returns None if strAddress is invalid. Otherwise returns integer version of address.""" | ||||
addr = b58decode_chk(strAddress) | addr = b58decode_chk(strAddress) | ||||
if addr is None or len(addr) != 21: | if addr is None or len(addr) != 21: | ||||
return None | return None | ||||
version = addr[0] | version = addr[0] | ||||
return ord(version) | return ord(version) | ||||
if __name__ == '__main__': | if __name__ == "__main__": | ||||
# Test case (from http://gitorious.org/bitcoin/python-base58.git) | # Test case (from http://gitorious.org/bitcoin/python-base58.git) | ||||
assert get_bcaddress_version('15VjRaDX9zpbA8LVnbrCAFzrVzN7ixHNsC') == 0 | assert get_bcaddress_version("15VjRaDX9zpbA8LVnbrCAFzrVzN7ixHNsC") == 0 | ||||
_ohai = 'o hai'.encode('ascii') | _ohai = "o hai".encode("ascii") | ||||
_tmp = b58encode(_ohai) | _tmp = b58encode(_ohai) | ||||
assert _tmp == 'DYB3oMS' | assert _tmp == "DYB3oMS" | ||||
assert b58decode(_tmp, 5) == _ohai | assert b58decode(_tmp, 5) == _ohai | ||||
print("Tests passed") | print("Tests passed") |