10
10
./gen_key_io_test_vectors.py invalid 70 > ../../src/test/data/key_io_invalid.json
11
11
'''
12
12
13
- import os
14
13
from itertools import islice
15
- from base58 import b58encode_chk , b58decode_chk , b58chars
14
+ import os
16
15
import random
17
16
import sys
18
17
19
18
sys .path .append (os .path .join (os .path .dirname (__file__ ), '../../test/functional' ))
20
19
20
+ from test_framework .address import base58_to_byte , byte_to_base58 , b58chars # noqa: E402
21
21
from test_framework .script import OP_0 , OP_1 , OP_2 , OP_3 , OP_16 , OP_DUP , OP_EQUAL , OP_EQUALVERIFY , OP_HASH160 , OP_CHECKSIG # noqa: E402
22
22
from test_framework .segwit_addr import bech32_encode , decode_segwit_address , convertbits , CHARSET , Encoding # noqa: E402
23
23
@@ -108,8 +108,10 @@ def is_valid(v):
108
108
'''Check vector v for validity'''
109
109
if len (set (v ) - set (b58chars )) > 0 :
110
110
return is_valid_bech32 (v )
111
- result = b58decode_chk (v )
112
- if result is None :
111
+ try :
112
+ payload , version = base58_to_byte (v )
113
+ result = bytes ([version ]) + payload
114
+ except AssertionError : # thrown if checksum doesn't match
113
115
return is_valid_bech32 (v )
114
116
for template in templates :
115
117
prefix = bytearray (template [0 ])
@@ -133,7 +135,8 @@ def gen_valid_base58_vector(template):
133
135
suffix = bytearray (template [2 ])
134
136
dst_prefix = bytearray (template [4 ])
135
137
dst_suffix = bytearray (template [5 ])
136
- rv = b58encode_chk (prefix + payload + suffix )
138
+ assert len (prefix ) == 1
139
+ rv = byte_to_base58 (payload + suffix , prefix [0 ])
137
140
return rv , dst_prefix + payload + dst_suffix
138
141
139
142
def gen_valid_bech32_vector (template ):
@@ -184,7 +187,8 @@ def gen_invalid_base58_vector(template):
184
187
else :
185
188
suffix = bytearray (template [2 ])
186
189
187
- val = b58encode_chk (prefix + payload + suffix )
190
+ assert len (prefix ) == 1
191
+ val = byte_to_base58 (payload + suffix , prefix [0 ])
188
192
if random .randint (0 ,10 )< 1 : # line corruption
189
193
if randbool (): # add random character to end
190
194
val += random .choice (b58chars )
0 commit comments