11#!/usr/bin/env python3
2- # Copyright (c) 2012-2021 The Bitcoin Core developers
2+ # Copyright (c) 2012-2022 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'''
66Generate valid and invalid base58 address and private key test vectors.
77
88Usage:
9- PYTHONPATH=../../test/functional/test_framework ./gen_key_io_test_vectors.py valid 50 > ../../src/test/data/key_io_valid.json
10- PYTHONPATH=../../test/functional/test_framework ./gen_key_io_test_vectors.py invalid 50 > ../../src/test/data/key_io_invalid.json
9+ ./gen_key_io_test_vectors.py valid 50 > ../../src/test/data/key_io_valid.json
10+ ./gen_key_io_test_vectors.py invalid 50 > ../../src/test/data/key_io_invalid.json
1111'''
12- # 2012 Wladimir J. van der Laan
13- # Released under MIT License
14- import os
12+
1513from itertools import islice
16- from base58 import b58encode_chk , b58decode_chk , b58chars
14+ import os
1715import random
16+ import sys
17+
18+ sys .path .append (os .path .join (os .path .dirname (__file__ ), '../../test/functional' ))
19+
20+ from test_framework .address import base58_to_byte , byte_to_base58 , b58chars # noqa: E402
21+ from test_framework .script import OP_DUP , OP_EQUAL , OP_EQUALVERIFY , OP_HASH160 , OP_CHECKSIG # noqa: E402
1822
1923# key types
2024PUBKEY_ADDRESS = 76
2832PRIVKEY_REGTEST = 239
2933
3034# script
31- OP_0 = 0x00
32- OP_1 = 0x51
33- OP_2 = 0x52
34- OP_16 = 0x60
35- OP_DUP = 0x76
36- OP_EQUAL = 0x87
37- OP_EQUALVERIFY = 0x88
38- OP_HASH160 = 0xa9
39- OP_CHECKSIG = 0xac
4035pubkey_prefix = (OP_DUP , OP_HASH160 , 20 )
4136pubkey_suffix = (OP_EQUALVERIFY , OP_CHECKSIG )
4237script_prefix = (OP_HASH160 , 20 )
@@ -65,8 +60,10 @@ def is_valid(v):
6560 '''Check vector v for validity'''
6661 if len (set (v ) - set (b58chars )) > 0 :
6762 return False
68- result = b58decode_chk (v )
69- if result is None :
63+ try :
64+ payload , version = base58_to_byte (v )
65+ result = bytes ([version ]) + payload
66+ except ValueError : # thrown if checksum doesn't match
7067 return False
7168 for template in templates :
7269 prefix = bytearray (template [0 ])
@@ -83,7 +80,8 @@ def gen_valid_base58_vector(template):
8380 suffix = bytearray (template [2 ])
8481 dst_prefix = bytearray (template [4 ])
8582 dst_suffix = bytearray (template [5 ])
86- rv = b58encode_chk (prefix + payload + suffix )
83+ assert len (prefix ) == 1
84+ rv = byte_to_base58 (payload + suffix , prefix [0 ])
8785 return rv , dst_prefix + payload + dst_suffix
8886
8987def gen_valid_vectors ():
@@ -124,7 +122,8 @@ def gen_invalid_base58_vector(template):
124122 else :
125123 suffix = bytearray (template [2 ])
126124
127- val = b58encode_chk (prefix + payload + suffix )
125+ assert len (prefix ) == 1
126+ val = byte_to_base58 (payload + suffix , prefix [0 ])
128127 if random .randint (0 ,10 )< 1 : # line corruption
129128 if randbool (): # add random character to end
130129 val += random .choice (b58chars )
@@ -152,7 +151,6 @@ def gen_invalid_vectors():
152151 yield val ,
153152
154153if __name__ == '__main__' :
155- import sys
156154 import json
157155 iters = {'valid' :gen_valid_vectors , 'invalid' :gen_invalid_vectors }
158156 try :
0 commit comments