mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 12:32:48 +01:00
Make base58 python contrib code work with python3
This commit is contained in:
parent
bc6fdf2d15
commit
18740586ba
@ -28,7 +28,9 @@ def b58encode(v):
|
|||||||
"""
|
"""
|
||||||
long_value = 0
|
long_value = 0
|
||||||
for (i, c) in enumerate(v[::-1]):
|
for (i, c) in enumerate(v[::-1]):
|
||||||
long_value += (256**i) * ord(c)
|
if isinstance(c, str):
|
||||||
|
c = ord(c)
|
||||||
|
long_value += (256**i) * c
|
||||||
|
|
||||||
result = ''
|
result = ''
|
||||||
while long_value >= __b58base:
|
while long_value >= __b58base:
|
||||||
@ -41,7 +43,7 @@ def b58encode(v):
|
|||||||
# 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': nPad += 1
|
if c == 0: nPad += 1
|
||||||
else: break
|
else: break
|
||||||
|
|
||||||
return (__b58chars[0]*nPad) + result
|
return (__b58chars[0]*nPad) + result
|
||||||
@ -50,8 +52,10 @@ 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]):
|
||||||
long_value += __b58chars.find(c) * (__b58base**i)
|
pos = __b58chars.find(c)
|
||||||
|
assert pos != -1
|
||||||
|
long_value += pos * (__b58base**i)
|
||||||
|
|
||||||
result = bytes()
|
result = bytes()
|
||||||
while long_value >= 256:
|
while long_value >= 256:
|
||||||
@ -62,10 +66,12 @@ def b58decode(v, length = None):
|
|||||||
|
|
||||||
nPad = 0
|
nPad = 0
|
||||||
for c in v:
|
for c in v:
|
||||||
if c == __b58chars[0]: nPad += 1
|
if c == __b58chars[0]:
|
||||||
else: break
|
nPad += 1
|
||||||
|
continue
|
||||||
|
break
|
||||||
|
|
||||||
result = chr(0)*nPad + result
|
result = bytes(nPad) + result
|
||||||
if length is not None and len(result) != length:
|
if length is not None and len(result) != length:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env python2
|
#!/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.
|
||||||
@ -8,8 +8,6 @@ Generate valid and invalid base58 address and private key test vectors.
|
|||||||
Usage:
|
Usage:
|
||||||
gen_base58_test_vectors.py valid 50 > ../../src/test/data/base58_keys_valid.json
|
gen_base58_test_vectors.py valid 50 > ../../src/test/data/base58_keys_valid.json
|
||||||
gen_base58_test_vectors.py invalid 50 > ../../src/test/data/base58_keys_invalid.json
|
gen_base58_test_vectors.py invalid 50 > ../../src/test/data/base58_keys_invalid.json
|
||||||
|
|
||||||
Note that this script is Python2 only, and will fail in Python3
|
|
||||||
'''
|
'''
|
||||||
# 2012 Wladimir J. van der Laan
|
# 2012 Wladimir J. van der Laan
|
||||||
# Released under MIT License
|
# Released under MIT License
|
||||||
@ -48,8 +46,8 @@ def is_valid(v):
|
|||||||
if result is None:
|
if result is None:
|
||||||
return False
|
return False
|
||||||
for template in templates:
|
for template in templates:
|
||||||
prefix = str(bytearray(template[0]))
|
prefix = bytearray(template[0])
|
||||||
suffix = str(bytearray(template[2]))
|
suffix = bytearray(template[2])
|
||||||
if result.startswith(prefix) and result.endswith(suffix):
|
if result.startswith(prefix) and result.endswith(suffix):
|
||||||
if (len(result) - len(prefix) - len(suffix)) == template[1]:
|
if (len(result) - len(prefix) - len(suffix)) == template[1]:
|
||||||
return True
|
return True
|
||||||
@ -59,20 +57,23 @@ def gen_valid_vectors():
|
|||||||
'''Generate valid test vectors'''
|
'''Generate valid test vectors'''
|
||||||
while True:
|
while True:
|
||||||
for template in templates:
|
for template in templates:
|
||||||
prefix = str(bytearray(template[0]))
|
prefix = bytearray(template[0])
|
||||||
payload = os.urandom(template[1])
|
payload = bytearray(os.urandom(template[1]))
|
||||||
suffix = str(bytearray(template[2]))
|
suffix = bytearray(template[2])
|
||||||
rv = b58encode_chk(prefix + payload + suffix)
|
rv = b58encode_chk(prefix + payload + suffix)
|
||||||
assert is_valid(rv)
|
assert is_valid(rv)
|
||||||
metadata = dict([(x,y) for (x,y) in zip(metadata_keys,template[3]) if y is not None])
|
metadata = {x: y for x, y in zip(metadata_keys,template[3]) if y is not None}
|
||||||
yield (rv, b2a_hex(payload), metadata)
|
hexrepr = b2a_hex(payload)
|
||||||
|
if isinstance(hexrepr, bytes):
|
||||||
|
hexrepr = hexrepr.decode('utf8')
|
||||||
|
yield (rv, hexrepr, metadata)
|
||||||
|
|
||||||
def gen_invalid_vector(template, corrupt_prefix, randomize_payload_size, corrupt_suffix):
|
def gen_invalid_vector(template, corrupt_prefix, randomize_payload_size, corrupt_suffix):
|
||||||
'''Generate possibly invalid vector'''
|
'''Generate possibly invalid vector'''
|
||||||
if corrupt_prefix:
|
if corrupt_prefix:
|
||||||
prefix = os.urandom(1)
|
prefix = os.urandom(1)
|
||||||
else:
|
else:
|
||||||
prefix = str(bytearray(template[0]))
|
prefix = bytearray(template[0])
|
||||||
|
|
||||||
if randomize_payload_size:
|
if randomize_payload_size:
|
||||||
payload = os.urandom(max(int(random.expovariate(0.5)), 50))
|
payload = os.urandom(max(int(random.expovariate(0.5)), 50))
|
||||||
@ -82,7 +83,7 @@ def gen_invalid_vector(template, corrupt_prefix, randomize_payload_size, corrupt
|
|||||||
if corrupt_suffix:
|
if corrupt_suffix:
|
||||||
suffix = os.urandom(len(template[2]))
|
suffix = os.urandom(len(template[2]))
|
||||||
else:
|
else:
|
||||||
suffix = str(bytearray(template[2]))
|
suffix = bytearray(template[2])
|
||||||
|
|
||||||
return b58encode_chk(prefix + payload + suffix)
|
return b58encode_chk(prefix + payload + suffix)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user