Skip to content

Commit

Permalink
Merge bitcoin#19239: tests: move generate_wif_key to wallet_util.py
Browse files Browse the repository at this point in the history
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 bitcoin#17977

ACKs for top commit:
  MarcoFalke:
    ACK 3a83a01 🍪

Tree-SHA512: 24985dffb75202721ccc0c6c5b52f1fa5d1ce7963bccde24389feb913cab4dad0c265274ca67892c46c8b64e6a065a0f23263a89be4fb9134dfefbdbe5c7238a
  • Loading branch information
MarcoFalke authored and knst committed Mar 8, 2024
1 parent eaf31ad commit a1e3885
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 96 deletions.
14 changes: 7 additions & 7 deletions test/functional/rpc_createmultisig.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test multisig RPCs"""
import binascii
import decimal
import itertools
import json
import os

from test_framework.authproxy import JSONRPCException
from test_framework.descriptors import descsum_create, drop_origins
from test_framework.key import ECPubKey, ECKey
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_raises_rpc_error,
assert_equal,
)
from test_framework.key import ECPubKey, ECKey, bytes_to_wif

import binascii
import decimal
import itertools
import json
import os
from test_framework.wallet_util import bytes_to_wif

class RpcCreateMultiSigTest(BitcoinTestFramework):
def set_test_params(self):
Expand Down
71 changes: 69 additions & 2 deletions test/functional/test_framework/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,64 @@
# This file encodes and decodes BASE58 P2PKH and P2SH addresses
#

from .base58 import byte_to_base58
from .script import hash160, CScript
import unittest

from .script import hash160, hash256, CScript
from .util import hex_str_to_bytes

from test_framework.util import assert_equal

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


chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'


def byte_to_base58(b, version):
result = ''
str = b.hex()
str = chr(version).encode('latin-1').hex() + str
checksum = hash256(hex_str_to_bytes(str)).hex()
str += checksum[:8]
value = int('0x'+str,0)
while value > 0:
result = chars[value % 58] + result
value //= 58
while (str[:2] == '00'):
result = chars[0] + result
str = str[2:]
return result


def base58_to_byte(s, verify_checksum=True):
if not s:
return b''
n = 0
for c in s:
n *= 58
assert c in chars
digit = chars.index(c)
n += digit
h = '%x' % n
if len(h) % 2:
h = '0' + h
res = n.to_bytes((n.bit_length() + 7) // 8, 'big')
pad = 0
for c in s:
if c == chars[0]:
pad += 1
else:
break
res = b'\x00' * pad + res
if verify_checksum:
assert_equal(hash256(res[:-4])[:4], res[-4:])

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


def keyhash_to_p2pkh(hash, main = False):
assert len(hash) == 20
version = 76 if main else 140
Expand Down Expand Up @@ -50,3 +98,22 @@ def check_script(script):
if (type(script) is bytes or type(script) is CScript):
return script
assert False


class TestFrameworkScript(unittest.TestCase):
def test_base58encodedecode(self):
def check_base58(data, version):
self.assertEqual(base58_to_byte(byte_to_base58(data, version)), (data, version))

check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 111)
check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 111)
check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 0)
check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 0)
check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
70 changes: 0 additions & 70 deletions test/functional/test_framework/base58.py

This file was deleted.

12 changes: 0 additions & 12 deletions test/functional/test_framework/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import unittest

from test_framework.crypto import secp256k1
from .base58 import byte_to_base58

# Order of the secp256k1 curve
ORDER = secp256k1.GE.ORDER
Expand Down Expand Up @@ -185,17 +184,6 @@ def sign_ecdsa(self, msg, low_s=True, rfc6979=False):
sb = s.to_bytes((s.bit_length() + 8) // 8, 'big')
return b'\x30' + bytes([4 + len(rb) + len(sb), 2, len(rb)]) + rb + bytes([2, len(sb)]) + sb

def bytes_to_wif(b, compressed=True):
if compressed:
b += b'\x01'
return byte_to_base58(b, 239)

def generate_wif_key():
# Makes a WIF privkey for imports
k = ECKey()
k.generate()
return bytes_to_wif(k.get_bytes(), k.is_compressed)

def compute_xonly_pubkey(key):
"""Compute an x-only (32 byte) public key from a (32 byte) private key.
Expand Down
17 changes: 13 additions & 4 deletions test/functional/test_framework/wallet_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
from collections import namedtuple

from test_framework.address import (
byte_to_base58,
key_to_p2pkh,
script_to_p2sh,
)
from test_framework.key import (
bytes_to_wif,
ECKey,
)
from test_framework.key import ECKey
from test_framework.script import (
CScript,
OP_2,
Expand Down Expand Up @@ -90,3 +88,14 @@ def test_address(node, address, **kwargs):
raise AssertionError("key {} unexpectedly returned in getaddressinfo.".format(key))
elif addr_info[key] != value:
raise AssertionError("key {} value {} did not match expected value {}".format(key, addr_info[key], value))

def bytes_to_wif(b, compressed=True):
if compressed:
b += b'\x01'
return byte_to_base58(b, 239)

def generate_wif_key():
# Makes a WIF privkey for imports
k = ECKey()
k.generate()
return bytes_to_wif(k.get_bytes(), k.is_compressed)
2 changes: 1 addition & 1 deletion test/functional/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
# List of framework modules containing unit tests. Should be kept in sync with
# the output of `git grep unittest.TestCase ./test/functional/test_framework`
TEST_FRAMEWORK_MODULES = [
"base58",
"address",
"crypto.bip324_cipher",
"blocktools",
"crypto.chacha20",
Expand Down

0 comments on commit a1e3885

Please sign in to comment.