Skip to content

Commit c75de5d

Browse files
committed
[TESTS] Move base58 to own module to break circular dependency
This breaks the script->key->address->script dependency cycle.
1 parent f8364df commit c75de5d

File tree

4 files changed

+74
-71
lines changed

4 files changed

+74
-71
lines changed

test/functional/test_framework/address.py

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
"""Encode and decode BASE58, P2PKH and P2SH addresses."""
66

77
import enum
8-
import unittest
98

10-
from .script import hash256, hash160, sha256, CScript, OP_0
9+
from .base58 import byte_to_base58
10+
from .script import hash160, sha256, CScript, OP_0
1111
from .util import hex_str_to_bytes
1212

1313
from . import segwit_addr
1414

15-
from test_framework.util import assert_equal
16-
1715
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
1816
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
1917
# Coins sent to this address can be spent with a witness stack of just OP_TRUE
@@ -25,52 +23,6 @@ class AddressType(enum.Enum):
2523
p2sh_segwit = 'p2sh-segwit'
2624
legacy = 'legacy' # P2PKH
2725

28-
29-
chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
30-
31-
32-
def byte_to_base58(b, version):
33-
result = ''
34-
str = b.hex()
35-
str = chr(version).encode('latin-1').hex() + str
36-
checksum = hash256(hex_str_to_bytes(str)).hex()
37-
str += checksum[:8]
38-
value = int('0x'+str,0)
39-
while value > 0:
40-
result = chars[value % 58] + result
41-
value //= 58
42-
while (str[:2] == '00'):
43-
result = chars[0] + result
44-
str = str[2:]
45-
return result
46-
47-
48-
def base58_to_byte(s, verify_checksum=True):
49-
if not s:
50-
return b''
51-
n = 0
52-
for c in s:
53-
n *= 58
54-
assert c in chars
55-
digit = chars.index(c)
56-
n += digit
57-
h = '%x' % n
58-
if len(h) % 2:
59-
h = '0' + h
60-
res = n.to_bytes((n.bit_length() + 7) // 8, 'big')
61-
pad = 0
62-
for c in s:
63-
if c == chars[0]:
64-
pad += 1
65-
else:
66-
break
67-
res = b'\x00' * pad + res
68-
if verify_checksum:
69-
assert_equal(hash256(res[:-4])[:4], res[-4:])
70-
71-
return res[1:-4], int(res[0])
72-
73-
7426
def keyhash_to_p2pkh(hash, main = False):
7527
assert len(hash) == 20
7628
version = 0 if main else 111
@@ -128,22 +80,3 @@ def check_script(script):
12880
if (type(script) is bytes or type(script) is CScript):
12981
return script
13082
assert False
131-
132-
133-
class TestFrameworkScript(unittest.TestCase):
134-
def test_base58encodedecode(self):
135-
def check_base58(data, version):
136-
self.assertEqual(base58_to_byte(byte_to_base58(data, version)), (data, version))
137-
138-
check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 111)
139-
check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 111)
140-
check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
141-
check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
142-
check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
143-
check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
144-
check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 0)
145-
check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 0)
146-
check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
147-
check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
148-
check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
149-
check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2016-2020 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Encode BASE58."""
6+
7+
import unittest
8+
9+
from .messages import hash256
10+
from .util import hex_str_to_bytes, assert_equal
11+
12+
chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
13+
14+
def byte_to_base58(b, version):
15+
result = ''
16+
str = b.hex()
17+
str = chr(version).encode('latin-1').hex() + str
18+
checksum = hash256(hex_str_to_bytes(str)).hex()
19+
str += checksum[:8]
20+
value = int('0x'+str,0)
21+
while value > 0:
22+
result = chars[value % 58] + result
23+
value //= 58
24+
while (str[:2] == '00'):
25+
result = chars[0] + result
26+
str = str[2:]
27+
return result
28+
29+
def base58_to_byte(s, verify_checksum=True):
30+
if not s:
31+
return b''
32+
n = 0
33+
for c in s:
34+
n *= 58
35+
assert c in chars
36+
digit = chars.index(c)
37+
n += digit
38+
h = '%x' % n
39+
if len(h) % 2:
40+
h = '0' + h
41+
res = n.to_bytes((n.bit_length() + 7) // 8, 'big')
42+
pad = 0
43+
for c in s:
44+
if c == chars[0]:
45+
pad += 1
46+
else:
47+
break
48+
res = b'\x00' * pad + res
49+
if verify_checksum:
50+
assert_equal(hash256(res[:-4])[:4], res[-4:])
51+
52+
return res[1:-4], int(res[0])
53+
54+
class TestFrameworkScript(unittest.TestCase):
55+
def test_base58encodedecode(self):
56+
def check_base58(data, version):
57+
self.assertEqual(base58_to_byte(byte_to_base58(data, version)), (data, version))
58+
59+
check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 111)
60+
check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 111)
61+
check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
62+
check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
63+
check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
64+
check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
65+
check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 0)
66+
check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 0)
67+
check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
68+
check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
69+
check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
70+
check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)

test/functional/test_framework/key.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
anything but tests."""
99
import random
1010

11-
from .address import byte_to_base58
11+
from .base58 import byte_to_base58
1212

1313
def modinv(a, n):
1414
"""Compute the modular inverse of a modulo n

test/functional/test_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
TEST_EXIT_SKIPPED = 77
6868

6969
TEST_FRAMEWORK_MODULES = [
70-
"address",
70+
"base58",
7171
"blocktools",
7272
"script",
7373
]

0 commit comments

Comments
 (0)