Skip to content

Commit 2c6d522

Browse files
committed
merge bitcoin#24576: remove redundant base58 implementation
1 parent 53bfca8 commit 2c6d522

File tree

4 files changed

+29
-147
lines changed

4 files changed

+29
-147
lines changed

contrib/testgen/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ Utilities to generate test vectors for the data-driven Dash tests.
44

55
Usage:
66

7-
PYTHONPATH=../../test/functional/test_framework ./gen_key_io_test_vectors.py valid 50 > ../../src/test/data/key_io_valid.json
8-
PYTHONPATH=../../test/functional/test_framework ./gen_key_io_test_vectors.py invalid 50 > ../../src/test/data/key_io_invalid.json
7+
./gen_key_io_test_vectors.py valid 50 > ../../src/test/data/key_io_valid.json
8+
./gen_key_io_test_vectors.py invalid 50 > ../../src/test/data/key_io_invalid.json

contrib/testgen/base58.py

Lines changed: 0 additions & 115 deletions
This file was deleted.

contrib/testgen/gen_key_io_test_vectors.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
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
'''
66
Generate valid and invalid base58 address and private key test vectors.
77
88
Usage:
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+
1513
from itertools import islice
16-
from base58 import b58encode_chk, b58decode_chk, b58chars
14+
import os
1715
import 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
2024
PUBKEY_ADDRESS = 76
@@ -28,15 +32,6 @@
2832
PRIVKEY_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
4035
pubkey_prefix = (OP_DUP, OP_HASH160, 20)
4136
pubkey_suffix = (OP_EQUALVERIFY, OP_CHECKSIG)
4237
script_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

8987
def 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

154153
if __name__ == '__main__':
155-
import sys
156154
import json
157155
iters = {'valid':gen_valid_vectors, 'invalid':gen_invalid_vectors}
158156
try:

test/functional/test_framework/address.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@
1212
import unittest
1313

1414
from .script import hash160, hash256, CScript
15-
from .util import assert_equal
1615

1716
# Note unlike in bitcoin, this address isn't bech32 since we don't (at this time) support bech32.
1817
ADDRESS_BCRT1_UNSPENDABLE = 'yVg3NBUHNEhgDceqwVUjsZHreC5PBHnUo9'
1918
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(yVg3NBUHNEhgDceqwVUjsZHreC5PBHnUo9)#e5kt0jtk'
2019
ADDRESS_BCRT1_P2SH_OP_TRUE = '8zJctvfrzGZ5s1zQ3kagwyW1DsPYSQ4V2P'
2120

2221

23-
chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
22+
b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
2423

2524

2625
def byte_to_base58(b, version):
@@ -29,10 +28,10 @@ def byte_to_base58(b, version):
2928
b += hash256(b)[:4] # append checksum
3029
value = int.from_bytes(b, 'big')
3130
while value > 0:
32-
result = chars[value % 58] + result
31+
result = b58chars[value % 58] + result
3332
value //= 58
3433
while b[0] == 0:
35-
result = chars[0] + result
34+
result = b58chars[0] + result
3635
b = b[1:]
3736
return result
3837

@@ -46,23 +45,23 @@ def base58_to_byte(s):
4645
n = 0
4746
for c in s:
4847
n *= 58
49-
assert c in chars
50-
digit = chars.index(c)
48+
assert c in b58chars
49+
digit = b58chars.index(c)
5150
n += digit
5251
h = '%x' % n
5352
if len(h) % 2:
5453
h = '0' + h
5554
res = n.to_bytes((n.bit_length() + 7) // 8, 'big')
5655
pad = 0
5756
for c in s:
58-
if c == chars[0]:
57+
if c == b58chars[0]:
5958
pad += 1
6059
else:
6160
break
6261
res = b'\x00' * pad + res
6362

64-
# Assert if the checksum is invalid
65-
assert_equal(hash256(res[:-4])[:4], res[-4:])
63+
if hash256(res[:-4])[:4] != res[-4:]:
64+
raise ValueError('Invalid Base58Check checksum')
6665

6766
return res[1:-4], int(res[0])
6867

0 commit comments

Comments
 (0)