1
- #!/usr/bin/env python2
1
+ #!/usr/bin/env python3
2
2
# Copyright (c) 2012-2017 The Bitcoin Core developers
3
3
# Distributed under the MIT software license, see the accompanying
4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
'''
6
6
Generate valid and invalid base58 address and private key test vectors.
7
7
8
- Usage:
8
+ Usage:
9
9
gen_base58_test_vectors.py valid 50 > ../../src/test/data/base58_keys_valid.json
10
10
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
13
11
'''
14
12
# 2012 Wladimir J. van der Laan
15
13
# Released under MIT License
@@ -48,8 +46,8 @@ def is_valid(v):
48
46
if result is None :
49
47
return False
50
48
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 ])
53
51
if result .startswith (prefix ) and result .endswith (suffix ):
54
52
if (len (result ) - len (prefix ) - len (suffix )) == template [1 ]:
55
53
return True
@@ -59,20 +57,23 @@ def gen_valid_vectors():
59
57
'''Generate valid test vectors'''
60
58
while True :
61
59
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 ])
65
63
rv = b58encode_chk (prefix + payload + suffix )
66
64
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 )
69
70
70
71
def gen_invalid_vector (template , corrupt_prefix , randomize_payload_size , corrupt_suffix ):
71
72
'''Generate possibly invalid vector'''
72
73
if corrupt_prefix :
73
74
prefix = os .urandom (1 )
74
75
else :
75
- prefix = str ( bytearray (template [0 ]) )
76
+ prefix = bytearray (template [0 ])
76
77
77
78
if randomize_payload_size :
78
79
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
82
83
if corrupt_suffix :
83
84
suffix = os .urandom (len (template [2 ]))
84
85
else :
85
- suffix = str ( bytearray (template [2 ]) )
86
+ suffix = bytearray (template [2 ])
86
87
87
88
return b58encode_chk (prefix + payload + suffix )
88
89
0 commit comments