4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
'''
6
6
Generate valid and invalid base58/bech32(m) address and private key test vectors.
7
-
8
- Usage:
9
- ./gen_key_io_test_vectors.py valid 70 > ../../src/test/data/key_io_valid.json
10
- ./gen_key_io_test_vectors.py invalid 70 > ../../src/test/data/key_io_invalid.json
11
7
'''
12
8
13
9
from itertools import islice
@@ -131,7 +127,7 @@ def is_valid_bech32(v):
131
127
def gen_valid_base58_vector (template ):
132
128
'''Generate valid base58 vector'''
133
129
prefix = bytearray (template [0 ])
134
- payload = bytearray ( os . urandom ( template [1 ]) )
130
+ payload = rand_bytes ( size = template [1 ])
135
131
suffix = bytearray (template [2 ])
136
132
dst_prefix = bytearray (template [4 ])
137
133
dst_suffix = bytearray (template [5 ])
@@ -143,7 +139,7 @@ def gen_valid_bech32_vector(template):
143
139
'''Generate valid bech32 vector'''
144
140
hrp = template [0 ]
145
141
witver = template [1 ]
146
- witprog = bytearray ( os . urandom ( template [2 ]) )
142
+ witprog = rand_bytes ( size = template [2 ])
147
143
encoding = template [4 ]
148
144
dst_prefix = bytearray (template [5 ])
149
145
rv = bech32_encode (encoding , hrp , [witver ] + convertbits (witprog , 8 , 5 ))
@@ -173,17 +169,17 @@ def gen_invalid_base58_vector(template):
173
169
corrupt_suffix = randbool (0.2 )
174
170
175
171
if corrupt_prefix :
176
- prefix = os . urandom ( 1 )
172
+ prefix = rand_bytes ( size = 1 )
177
173
else :
178
174
prefix = bytearray (template [0 ])
179
175
180
176
if randomize_payload_size :
181
- payload = os . urandom ( max (int (random .expovariate (0.5 )), 50 ))
177
+ payload = rand_bytes ( size = max (int (random .expovariate (0.5 )), 50 ))
182
178
else :
183
- payload = os . urandom ( template [1 ])
179
+ payload = rand_bytes ( size = template [1 ])
184
180
185
181
if corrupt_suffix :
186
- suffix = os . urandom ( len (template [2 ]))
182
+ suffix = rand_bytes ( size = len (template [2 ]))
187
183
else :
188
184
suffix = bytearray (template [2 ])
189
185
@@ -204,7 +200,7 @@ def gen_invalid_bech32_vector(template):
204
200
to_upper = randbool (0.1 )
205
201
hrp = template [0 ]
206
202
witver = template [1 ]
207
- witprog = bytearray ( os . urandom ( template [2 ]) )
203
+ witprog = rand_bytes ( size = template [2 ])
208
204
encoding = template [3 ]
209
205
210
206
if no_data :
@@ -234,6 +230,9 @@ def randbool(p = 0.5):
234
230
'''Return True with P(p)'''
235
231
return random .random () < p
236
232
233
+ def rand_bytes (* , size ):
234
+ return bytearray (random .getrandbits (8 ) for _ in range (size ))
235
+
237
236
def gen_invalid_vectors ():
238
237
'''Generate invalid test vectors'''
239
238
# start with some manual edge-cases
@@ -250,6 +249,7 @@ def gen_invalid_vectors():
250
249
if __name__ == '__main__' :
251
250
import json
252
251
iters = {'valid' :gen_valid_vectors , 'invalid' :gen_invalid_vectors }
252
+ random .seed (42 )
253
253
try :
254
254
uiter = iters [sys .argv [1 ]]
255
255
except IndexError :
0 commit comments