Skip to content

Commit 1874058

Browse files
eklitzkejnewbery
authored andcommitted
Make base58 python contrib code work with python3
1 parent bc6fdf2 commit 1874058

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

contrib/testgen/base58.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ def b58encode(v):
2828
"""
2929
long_value = 0
3030
for (i, c) in enumerate(v[::-1]):
31-
long_value += (256**i) * ord(c)
31+
if isinstance(c, str):
32+
c = ord(c)
33+
long_value += (256**i) * c
3234

3335
result = ''
3436
while long_value >= __b58base:
@@ -41,7 +43,7 @@ def b58encode(v):
4143
# leading 0-bytes in the input become leading-1s
4244
nPad = 0
4345
for c in v:
44-
if c == '\0': nPad += 1
46+
if c == 0: nPad += 1
4547
else: break
4648

4749
return (__b58chars[0]*nPad) + result
@@ -50,8 +52,10 @@ def b58decode(v, length = None):
5052
""" decode v into a string of len bytes
5153
"""
5254
long_value = 0
53-
for (i, c) in enumerate(v[::-1]):
54-
long_value += __b58chars.find(c) * (__b58base**i)
55+
for i, c in enumerate(v[::-1]):
56+
pos = __b58chars.find(c)
57+
assert pos != -1
58+
long_value += pos * (__b58base**i)
5559

5660
result = bytes()
5761
while long_value >= 256:
@@ -62,10 +66,12 @@ def b58decode(v, length = None):
6266

6367
nPad = 0
6468
for c in v:
65-
if c == __b58chars[0]: nPad += 1
66-
else: break
69+
if c == __b58chars[0]:
70+
nPad += 1
71+
continue
72+
break
6773

68-
result = chr(0)*nPad + result
74+
result = bytes(nPad) + result
6975
if length is not None and len(result) != length:
7076
return None
7177

contrib/testgen/gen_base58_test_vectors.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
#!/usr/bin/env python2
1+
#!/usr/bin/env python3
22
# Copyright (c) 2012-2017 The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
'''
66
Generate valid and invalid base58 address and private key test vectors.
77
8-
Usage:
8+
Usage:
99
gen_base58_test_vectors.py valid 50 > ../../src/test/data/base58_keys_valid.json
1010
gen_base58_test_vectors.py invalid 50 > ../../src/test/data/base58_keys_invalid.json
11-
12-
Note that this script is Python2 only, and will fail in Python3
1311
'''
1412
# 2012 Wladimir J. van der Laan
1513
# Released under MIT License
@@ -48,8 +46,8 @@ def is_valid(v):
4846
if result is None:
4947
return False
5048
for template in templates:
51-
prefix = str(bytearray(template[0]))
52-
suffix = str(bytearray(template[2]))
49+
prefix = bytearray(template[0])
50+
suffix = bytearray(template[2])
5351
if result.startswith(prefix) and result.endswith(suffix):
5452
if (len(result) - len(prefix) - len(suffix)) == template[1]:
5553
return True
@@ -59,20 +57,23 @@ def gen_valid_vectors():
5957
'''Generate valid test vectors'''
6058
while True:
6159
for template in templates:
62-
prefix = str(bytearray(template[0]))
63-
payload = os.urandom(template[1])
64-
suffix = str(bytearray(template[2]))
60+
prefix = bytearray(template[0])
61+
payload = bytearray(os.urandom(template[1]))
62+
suffix = bytearray(template[2])
6563
rv = b58encode_chk(prefix + payload + suffix)
6664
assert is_valid(rv)
67-
metadata = dict([(x,y) for (x,y) in zip(metadata_keys,template[3]) if y is not None])
68-
yield (rv, b2a_hex(payload), metadata)
65+
metadata = {x: y for x, y in zip(metadata_keys,template[3]) if y is not None}
66+
hexrepr = b2a_hex(payload)
67+
if isinstance(hexrepr, bytes):
68+
hexrepr = hexrepr.decode('utf8')
69+
yield (rv, hexrepr, metadata)
6970

7071
def gen_invalid_vector(template, corrupt_prefix, randomize_payload_size, corrupt_suffix):
7172
'''Generate possibly invalid vector'''
7273
if corrupt_prefix:
7374
prefix = os.urandom(1)
7475
else:
75-
prefix = str(bytearray(template[0]))
76+
prefix = bytearray(template[0])
7677

7778
if randomize_payload_size:
7879
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
8283
if corrupt_suffix:
8384
suffix = os.urandom(len(template[2]))
8485
else:
85-
suffix = str(bytearray(template[2]))
86+
suffix = bytearray(template[2])
8687

8788
return b58encode_chk(prefix + payload + suffix)
8889

0 commit comments

Comments
 (0)