Skip to content

Commit 85f7db2

Browse files
author
MarcoFalke
committed
Merge #19239: tests: move generate_wif_key to wallet_util.py
3a83a01 [tests] move generate_wif_key to wallet_util.py (John Newbery) b216b0b [tests] sort imports in rpc_createmultisig.py (John Newbery) e380818 Revert "[TESTS] Move base58 to own module to break circular dependency" (John Newbery) Pull request description: generate_wif_key is a wallet utility function. Move it from the EC key module to the wallet util module. This fixes the circular dependency issue in #17977 ACKs for top commit: MarcoFalke: ACK 3a83a01 🍪 Tree-SHA512: 24985dffb75202721ccc0c6c5b52f1fa5d1ce7963bccde24389feb913cab4dad0c265274ca67892c46c8b64e6a065a0f23263a89be4fb9134dfefbdbe5c7238a
2 parents 7a24cca + 3a83a01 commit 85f7db2

File tree

6 files changed

+90
-97
lines changed

6 files changed

+90
-97
lines changed

test/functional/rpc_createmultisig.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test multisig RPCs"""
6+
import binascii
7+
import decimal
8+
import itertools
9+
import json
10+
import os
611

712
from test_framework.authproxy import JSONRPCException
813
from test_framework.descriptors import descsum_create, drop_origins
14+
from test_framework.key import ECPubKey, ECKey
915
from test_framework.test_framework import BitcoinTestFramework
1016
from test_framework.util import (
1117
assert_raises_rpc_error,
1218
assert_equal,
1319
)
14-
from test_framework.key import ECPubKey, ECKey, bytes_to_wif
15-
16-
import binascii
17-
import decimal
18-
import itertools
19-
import json
20-
import os
20+
from test_framework.wallet_util import bytes_to_wif
2121

2222
class RpcCreateMultiSigTest(BitcoinTestFramework):
2323
def set_test_params(self):

test/functional/test_framework/address.py

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

77
import enum
8+
import unittest
89

9-
from .base58 import byte_to_base58
10-
from .script import hash160, sha256, CScript, OP_0
10+
from .script import hash256, 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+
1517
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
1618
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
1719
# Coins sent to this address can be spent with a witness stack of just OP_TRUE
@@ -23,6 +25,52 @@ class AddressType(enum.Enum):
2325
p2sh_segwit = 'p2sh-segwit'
2426
legacy = 'legacy' # P2PKH
2527

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+
2674
def keyhash_to_p2pkh(hash, main = False):
2775
assert len(hash) == 20
2876
version = 0 if main else 111
@@ -80,3 +128,22 @@ def check_script(script):
80128
if (type(script) is bytes or type(script) is CScript):
81129
return script
82130
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)

test/functional/test_framework/base58.py

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

test/functional/test_framework/key.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
anything but tests."""
99
import random
1010

11-
from .base58 import byte_to_base58
12-
1311
def modinv(a, n):
1412
"""Compute the modular inverse of a modulo n
1513
@@ -386,14 +384,3 @@ def sign_ecdsa(self, msg, low_s=True):
386384
rb = r.to_bytes((r.bit_length() + 8) // 8, 'big')
387385
sb = s.to_bytes((s.bit_length() + 8) // 8, 'big')
388386
return b'\x30' + bytes([4 + len(rb) + len(sb), 2, len(rb)]) + rb + bytes([2, len(sb)]) + sb
389-
390-
def bytes_to_wif(b, compressed=True):
391-
if compressed:
392-
b += b'\x01'
393-
return byte_to_base58(b, 239)
394-
395-
def generate_wif_key():
396-
# Makes a WIF privkey for imports
397-
k = ECKey()
398-
k.generate()
399-
return bytes_to_wif(k.get_bytes(), k.is_compressed)

test/functional/test_framework/wallet_util.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
from collections import namedtuple
77

88
from test_framework.address import (
9+
byte_to_base58,
910
key_to_p2pkh,
1011
key_to_p2sh_p2wpkh,
1112
key_to_p2wpkh,
1213
script_to_p2sh,
1314
script_to_p2sh_p2wsh,
1415
script_to_p2wsh,
1516
)
16-
from test_framework.key import (
17-
bytes_to_wif,
18-
ECKey,
19-
)
17+
from test_framework.key import ECKey
2018
from test_framework.script import (
2119
CScript,
2220
OP_0,
@@ -120,3 +118,14 @@ def test_address(node, address, **kwargs):
120118
raise AssertionError("key {} unexpectedly returned in getaddressinfo.".format(key))
121119
elif addr_info[key] != value:
122120
raise AssertionError("key {} value {} did not match expected value {}".format(key, addr_info[key], value))
121+
122+
def bytes_to_wif(b, compressed=True):
123+
if compressed:
124+
b += b'\x01'
125+
return byte_to_base58(b, 239)
126+
127+
def generate_wif_key():
128+
# Makes a WIF privkey for imports
129+
k = ECKey()
130+
k.generate()
131+
return bytes_to_wif(k.get_bytes(), k.is_compressed)

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-
"base58",
70+
"address",
7171
"blocktools",
7272
"script",
7373
]

0 commit comments

Comments
 (0)