mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-09 05:14:22 +01:00
test: refactor: introduce generate_keypair
helper with WIF support
In functional tests it is a quite common scenario to generate fresh elliptic curve keypairs, which is currently a bit cumbersome as it involves multiple steps, e.g.: privkey = ECKey() privkey.generate() privkey_wif = bytes_to_wif(privkey.get_bytes()) pubkey = privkey.get_pubkey().get_bytes() Simplify this by providing a new `generate_keypair` helper function that returns the private key either as `ECKey` object or as WIF-string (depending on the boolean `wif` parameter) and the public key as byte-string; these formats are what we mostly need (currently we don't use `ECPubKey` objects from generated keypairs anywhere). With this, most of the affected code blocks following the pattern above can be replaced by one-liners, e.g.: privkey, pubkey = generate_keypair(wif=True) Note that after this commit, the only direct uses of `ECKey` remain in situations where we want to set the private key explicitly, e.g. in MiniWallet (test/functional/test_framework/wallet.py) or the test for the signet miner script (test/functional/tool_signet_miner.py).
This commit is contained in:
parent
7f0b79ea13
commit
1a572ce7d6
18 changed files with 79 additions and 130 deletions
|
@ -35,7 +35,6 @@ from test_framework.blocktools import (
|
||||||
create_block,
|
create_block,
|
||||||
create_coinbase,
|
create_coinbase,
|
||||||
)
|
)
|
||||||
from test_framework.key import ECKey
|
|
||||||
from test_framework.messages import (
|
from test_framework.messages import (
|
||||||
CBlockHeader,
|
CBlockHeader,
|
||||||
COutPoint,
|
COutPoint,
|
||||||
|
@ -46,9 +45,13 @@ from test_framework.messages import (
|
||||||
msg_headers,
|
msg_headers,
|
||||||
)
|
)
|
||||||
from test_framework.p2p import P2PInterface
|
from test_framework.p2p import P2PInterface
|
||||||
from test_framework.script import (CScript, OP_TRUE)
|
from test_framework.script import (
|
||||||
|
CScript,
|
||||||
|
OP_TRUE,
|
||||||
|
)
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.util import assert_equal
|
from test_framework.util import assert_equal
|
||||||
|
from test_framework.wallet_util import generate_keypair
|
||||||
|
|
||||||
|
|
||||||
class BaseNode(P2PInterface):
|
class BaseNode(P2PInterface):
|
||||||
|
@ -90,9 +93,7 @@ class AssumeValidTest(BitcoinTestFramework):
|
||||||
self.blocks = []
|
self.blocks = []
|
||||||
|
|
||||||
# Get a pubkey for the coinbase TXO
|
# Get a pubkey for the coinbase TXO
|
||||||
coinbase_key = ECKey()
|
_, coinbase_pubkey = generate_keypair()
|
||||||
coinbase_key.generate()
|
|
||||||
coinbase_pubkey = coinbase_key.get_pubkey().get_bytes()
|
|
||||||
|
|
||||||
# Create the first block with a coinbase output to our key
|
# Create the first block with a coinbase output to our key
|
||||||
height = 1
|
height = 1
|
||||||
|
|
|
@ -14,7 +14,6 @@ from test_framework.blocktools import (
|
||||||
get_legacy_sigopcount_block,
|
get_legacy_sigopcount_block,
|
||||||
MAX_BLOCK_SIGOPS,
|
MAX_BLOCK_SIGOPS,
|
||||||
)
|
)
|
||||||
from test_framework.key import ECKey
|
|
||||||
from test_framework.messages import (
|
from test_framework.messages import (
|
||||||
CBlock,
|
CBlock,
|
||||||
COIN,
|
COIN,
|
||||||
|
@ -55,6 +54,7 @@ from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
assert_greater_than,
|
assert_greater_than,
|
||||||
)
|
)
|
||||||
|
from test_framework.wallet_util import generate_keypair
|
||||||
from data import invalid_txs
|
from data import invalid_txs
|
||||||
|
|
||||||
|
|
||||||
|
@ -98,9 +98,7 @@ class FullBlockTest(BitcoinTestFramework):
|
||||||
self.bootstrap_p2p() # Add one p2p connection to the node
|
self.bootstrap_p2p() # Add one p2p connection to the node
|
||||||
|
|
||||||
self.block_heights = {}
|
self.block_heights = {}
|
||||||
self.coinbase_key = ECKey()
|
self.coinbase_key, self.coinbase_pubkey = generate_keypair()
|
||||||
self.coinbase_key.generate()
|
|
||||||
self.coinbase_pubkey = self.coinbase_key.get_pubkey().get_bytes()
|
|
||||||
self.tip = None
|
self.tip = None
|
||||||
self.blocks = {}
|
self.blocks = {}
|
||||||
self.genesis_hash = int(self.nodes[0].getbestblockhash(), 16)
|
self.genesis_hash = int(self.nodes[0].getbestblockhash(), 16)
|
||||||
|
|
|
@ -35,8 +35,7 @@ from test_framework.util import (
|
||||||
assert_raises_rpc_error,
|
assert_raises_rpc_error,
|
||||||
)
|
)
|
||||||
from test_framework.wallet import getnewdestination
|
from test_framework.wallet import getnewdestination
|
||||||
from test_framework.key import ECKey
|
from test_framework.wallet_util import generate_keypair
|
||||||
from test_framework.wallet_util import bytes_to_wif
|
|
||||||
|
|
||||||
NULLDUMMY_ERROR = "non-mandatory-script-verify-flag (Dummy CHECKMULTISIG argument must be zero)"
|
NULLDUMMY_ERROR = "non-mandatory-script-verify-flag (Dummy CHECKMULTISIG argument must be zero)"
|
||||||
|
|
||||||
|
@ -71,12 +70,9 @@ class NULLDUMMYTest(BitcoinTestFramework):
|
||||||
return tx_from_hex(signedtx["hex"])
|
return tx_from_hex(signedtx["hex"])
|
||||||
|
|
||||||
def run_test(self):
|
def run_test(self):
|
||||||
eckey = ECKey()
|
self.privkey, self.pubkey = generate_keypair(wif=True)
|
||||||
eckey.generate()
|
cms = self.nodes[0].createmultisig(1, [self.pubkey.hex()])
|
||||||
self.privkey = bytes_to_wif(eckey.get_bytes())
|
wms = self.nodes[0].createmultisig(1, [self.pubkey.hex()], 'p2sh-segwit')
|
||||||
self.pubkey = eckey.get_pubkey().get_bytes().hex()
|
|
||||||
cms = self.nodes[0].createmultisig(1, [self.pubkey])
|
|
||||||
wms = self.nodes[0].createmultisig(1, [self.pubkey], 'p2sh-segwit')
|
|
||||||
self.ms_address = cms["address"]
|
self.ms_address = cms["address"]
|
||||||
ms_unlock_details = {"scriptPubKey": address_to_scriptpubkey(self.ms_address).hex(),
|
ms_unlock_details = {"scriptPubKey": address_to_scriptpubkey(self.ms_address).hex(),
|
||||||
"redeemScript": cms["redeemScript"]}
|
"redeemScript": cms["redeemScript"]}
|
||||||
|
|
|
@ -97,6 +97,7 @@ from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
random_bytes,
|
random_bytes,
|
||||||
)
|
)
|
||||||
|
from test_framework.wallet_util import generate_keypair
|
||||||
from test_framework.key import (
|
from test_framework.key import (
|
||||||
generate_privkey,
|
generate_privkey,
|
||||||
compute_xonly_pubkey,
|
compute_xonly_pubkey,
|
||||||
|
@ -1186,11 +1187,8 @@ def spenders_taproot_active():
|
||||||
|
|
||||||
# Also add a few legacy spends into the mix, so that transactions which combine taproot and pre-taproot spends get tested too.
|
# Also add a few legacy spends into the mix, so that transactions which combine taproot and pre-taproot spends get tested too.
|
||||||
for compressed in [False, True]:
|
for compressed in [False, True]:
|
||||||
eckey1 = ECKey()
|
eckey1, pubkey1 = generate_keypair(compressed=compressed)
|
||||||
eckey1.set(generate_privkey(), compressed)
|
eckey2, _ = generate_keypair(compressed=compressed)
|
||||||
pubkey1 = eckey1.get_pubkey().get_bytes()
|
|
||||||
eckey2 = ECKey()
|
|
||||||
eckey2.set(generate_privkey(), compressed)
|
|
||||||
for p2sh in [False, True]:
|
for p2sh in [False, True]:
|
||||||
for witv0 in [False, True]:
|
for witv0 in [False, True]:
|
||||||
for hashtype in VALID_SIGHASHES_ECDSA + [random.randrange(0x04, 0x80), random.randrange(0x84, 0x100)]:
|
for hashtype in VALID_SIGHASHES_ECDSA + [random.randrange(0x04, 0x80), random.randrange(0x84, 0x100)]:
|
||||||
|
|
|
@ -9,7 +9,6 @@ from decimal import Decimal
|
||||||
import math
|
import math
|
||||||
|
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.key import ECKey
|
|
||||||
from test_framework.messages import (
|
from test_framework.messages import (
|
||||||
MAX_BIP125_RBF_SEQUENCE,
|
MAX_BIP125_RBF_SEQUENCE,
|
||||||
COIN,
|
COIN,
|
||||||
|
@ -44,6 +43,7 @@ from test_framework.util import (
|
||||||
assert_raises_rpc_error,
|
assert_raises_rpc_error,
|
||||||
)
|
)
|
||||||
from test_framework.wallet import MiniWallet
|
from test_framework.wallet import MiniWallet
|
||||||
|
from test_framework.wallet_util import generate_keypair
|
||||||
|
|
||||||
|
|
||||||
class MempoolAcceptanceTest(BitcoinTestFramework):
|
class MempoolAcceptanceTest(BitcoinTestFramework):
|
||||||
|
@ -283,9 +283,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
|
||||||
rawtxs=[tx.serialize().hex()],
|
rawtxs=[tx.serialize().hex()],
|
||||||
)
|
)
|
||||||
tx = tx_from_hex(raw_tx_reference)
|
tx = tx_from_hex(raw_tx_reference)
|
||||||
key = ECKey()
|
_, pubkey = generate_keypair()
|
||||||
key.generate()
|
|
||||||
pubkey = key.get_pubkey().get_bytes()
|
|
||||||
tx.vout[0].scriptPubKey = keys_to_multisig_script([pubkey] * 3, k=2) # Some bare multisig script (2-of-3)
|
tx.vout[0].scriptPubKey = keys_to_multisig_script([pubkey] * 3, k=2) # Some bare multisig script (2-of-3)
|
||||||
self.check_mempool_result(
|
self.check_mempool_result(
|
||||||
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bare-multisig'}],
|
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bare-multisig'}],
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
"""Test dust limit mempool policy (`-dustrelayfee` parameter)"""
|
"""Test dust limit mempool policy (`-dustrelayfee` parameter)"""
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
from test_framework.key import ECKey
|
|
||||||
from test_framework.messages import (
|
from test_framework.messages import (
|
||||||
COIN,
|
COIN,
|
||||||
CTxOut,
|
CTxOut,
|
||||||
|
@ -32,6 +31,7 @@ from test_framework.util import (
|
||||||
get_fee,
|
get_fee,
|
||||||
)
|
)
|
||||||
from test_framework.wallet import MiniWallet
|
from test_framework.wallet import MiniWallet
|
||||||
|
from test_framework.wallet_util import generate_keypair
|
||||||
|
|
||||||
|
|
||||||
DUST_RELAY_TX_FEE = 3000 # default setting [sat/kvB]
|
DUST_RELAY_TX_FEE = 3000 # default setting [sat/kvB]
|
||||||
|
@ -74,11 +74,8 @@ class DustRelayFeeTest(BitcoinTestFramework):
|
||||||
self.wallet = MiniWallet(self.nodes[0])
|
self.wallet = MiniWallet(self.nodes[0])
|
||||||
|
|
||||||
# prepare output scripts of each standard type
|
# prepare output scripts of each standard type
|
||||||
key = ECKey()
|
_, uncompressed_pubkey = generate_keypair(compressed=False)
|
||||||
key.generate(compressed=False)
|
_, pubkey = generate_keypair(compressed=True)
|
||||||
uncompressed_pubkey = key.get_pubkey().get_bytes()
|
|
||||||
key.generate(compressed=True)
|
|
||||||
pubkey = key.get_pubkey().get_bytes()
|
|
||||||
|
|
||||||
output_scripts = (
|
output_scripts = (
|
||||||
(key_to_p2pk_script(uncompressed_pubkey), "P2PK (uncompressed)"),
|
(key_to_p2pk_script(uncompressed_pubkey), "P2PK (uncompressed)"),
|
||||||
|
|
|
@ -14,7 +14,6 @@ from test_framework.blocktools import (
|
||||||
create_block,
|
create_block,
|
||||||
create_coinbase,
|
create_coinbase,
|
||||||
)
|
)
|
||||||
from test_framework.key import ECKey
|
|
||||||
from test_framework.messages import (
|
from test_framework.messages import (
|
||||||
MAX_BIP125_RBF_SEQUENCE,
|
MAX_BIP125_RBF_SEQUENCE,
|
||||||
CBlockHeader,
|
CBlockHeader,
|
||||||
|
@ -89,6 +88,7 @@ from test_framework.util import (
|
||||||
assert_raises_rpc_error,
|
assert_raises_rpc_error,
|
||||||
)
|
)
|
||||||
from test_framework.wallet import MiniWallet
|
from test_framework.wallet import MiniWallet
|
||||||
|
from test_framework.wallet_util import generate_keypair
|
||||||
|
|
||||||
|
|
||||||
MAX_SIGOP_COST = 80000
|
MAX_SIGOP_COST = 80000
|
||||||
|
@ -1448,9 +1448,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||||
|
|
||||||
# Segwit transactions using uncompressed pubkeys are not accepted
|
# Segwit transactions using uncompressed pubkeys are not accepted
|
||||||
# under default policy, but should still pass consensus.
|
# under default policy, but should still pass consensus.
|
||||||
key = ECKey()
|
key, pubkey = generate_keypair(compressed=False)
|
||||||
key.generate(False)
|
|
||||||
pubkey = key.get_pubkey().get_bytes()
|
|
||||||
assert_equal(len(pubkey), 65) # This should be an uncompressed pubkey
|
assert_equal(len(pubkey), 65) # This should be an uncompressed pubkey
|
||||||
|
|
||||||
utxo = self.utxo.pop(0)
|
utxo = self.utxo.pop(0)
|
||||||
|
@ -1544,11 +1542,7 @@ class SegWitTest(BitcoinTestFramework):
|
||||||
|
|
||||||
@subtest
|
@subtest
|
||||||
def test_signature_version_1(self):
|
def test_signature_version_1(self):
|
||||||
|
key, pubkey = generate_keypair()
|
||||||
key = ECKey()
|
|
||||||
key.generate()
|
|
||||||
pubkey = key.get_pubkey().get_bytes()
|
|
||||||
|
|
||||||
witness_script = key_to_p2pk_script(pubkey)
|
witness_script = key_to_p2pk_script(pubkey)
|
||||||
script_pubkey = script_to_p2wsh_script(witness_script)
|
script_pubkey = script_to_p2wsh_script(witness_script)
|
||||||
|
|
||||||
|
|
|
@ -12,13 +12,13 @@ from test_framework.address import address_to_scriptpubkey
|
||||||
from test_framework.blocktools import COINBASE_MATURITY
|
from test_framework.blocktools import COINBASE_MATURITY
|
||||||
from test_framework.authproxy import JSONRPCException
|
from test_framework.authproxy import JSONRPCException
|
||||||
from test_framework.descriptors import descsum_create, drop_origins
|
from test_framework.descriptors import descsum_create, drop_origins
|
||||||
from test_framework.key import ECPubKey, ECKey
|
from test_framework.key import ECPubKey
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.util import (
|
from test_framework.util import (
|
||||||
assert_raises_rpc_error,
|
assert_raises_rpc_error,
|
||||||
assert_equal,
|
assert_equal,
|
||||||
)
|
)
|
||||||
from test_framework.wallet_util import bytes_to_wif
|
from test_framework.wallet_util import generate_keypair
|
||||||
from test_framework.wallet import (
|
from test_framework.wallet import (
|
||||||
MiniWallet,
|
MiniWallet,
|
||||||
getnewdestination,
|
getnewdestination,
|
||||||
|
@ -38,10 +38,9 @@ class RpcCreateMultiSigTest(BitcoinTestFramework):
|
||||||
self.priv = []
|
self.priv = []
|
||||||
node0, node1, node2 = self.nodes
|
node0, node1, node2 = self.nodes
|
||||||
for _ in range(self.nkeys):
|
for _ in range(self.nkeys):
|
||||||
k = ECKey()
|
privkey, pubkey = generate_keypair(wif=True)
|
||||||
k.generate()
|
self.pub.append(pubkey.hex())
|
||||||
self.pub.append(k.get_pubkey().get_bytes().hex())
|
self.priv.append(privkey)
|
||||||
self.priv.append(bytes_to_wif(k.get_bytes(), k.is_compressed))
|
|
||||||
if self.is_bdb_compiled():
|
if self.is_bdb_compiled():
|
||||||
self.final = node2.getnewaddress()
|
self.final = node2.getnewaddress()
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -8,7 +8,7 @@ from decimal import Decimal
|
||||||
from itertools import product
|
from itertools import product
|
||||||
|
|
||||||
from test_framework.descriptors import descsum_create
|
from test_framework.descriptors import descsum_create
|
||||||
from test_framework.key import ECKey, H_POINT
|
from test_framework.key import H_POINT
|
||||||
from test_framework.messages import (
|
from test_framework.messages import (
|
||||||
COutPoint,
|
COutPoint,
|
||||||
CTransaction,
|
CTransaction,
|
||||||
|
@ -43,8 +43,8 @@ from test_framework.util import (
|
||||||
random_bytes,
|
random_bytes,
|
||||||
)
|
)
|
||||||
from test_framework.wallet_util import (
|
from test_framework.wallet_util import (
|
||||||
bytes_to_wif,
|
generate_keypair,
|
||||||
get_generate_key
|
get_generate_key,
|
||||||
)
|
)
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
@ -710,9 +710,7 @@ class PSBTTest(BitcoinTestFramework):
|
||||||
|
|
||||||
self.log.info("Test that we can fund psbts with external inputs specified")
|
self.log.info("Test that we can fund psbts with external inputs specified")
|
||||||
|
|
||||||
eckey = ECKey()
|
privkey, _ = generate_keypair(wif=True)
|
||||||
eckey.generate()
|
|
||||||
privkey = bytes_to_wif(eckey.get_bytes())
|
|
||||||
|
|
||||||
self.nodes[1].createwallet("extfund")
|
self.nodes[1].createwallet("extfund")
|
||||||
wallet = self.nodes[1].get_wallet_rpc("extfund")
|
wallet = self.nodes[1].get_wallet_rpc("extfund")
|
||||||
|
@ -825,11 +823,9 @@ class PSBTTest(BitcoinTestFramework):
|
||||||
self.nodes[1].createwallet(wallet_name="scriptwatchonly", disable_private_keys=True)
|
self.nodes[1].createwallet(wallet_name="scriptwatchonly", disable_private_keys=True)
|
||||||
watchonly = self.nodes[1].get_wallet_rpc("scriptwatchonly")
|
watchonly = self.nodes[1].get_wallet_rpc("scriptwatchonly")
|
||||||
|
|
||||||
eckey = ECKey()
|
privkey, pubkey = generate_keypair(wif=True)
|
||||||
eckey.generate()
|
|
||||||
privkey = bytes_to_wif(eckey.get_bytes())
|
|
||||||
|
|
||||||
desc = descsum_create("wsh(pkh({}))".format(eckey.get_pubkey().get_bytes().hex()))
|
desc = descsum_create("wsh(pkh({}))".format(pubkey.hex()))
|
||||||
if self.options.descriptors:
|
if self.options.descriptors:
|
||||||
res = watchonly.importdescriptors([{"desc": desc, "timestamp": "now"}])
|
res = watchonly.importdescriptors([{"desc": desc, "timestamp": "now"}])
|
||||||
else:
|
else:
|
||||||
|
@ -846,11 +842,9 @@ class PSBTTest(BitcoinTestFramework):
|
||||||
|
|
||||||
# Same test but for taproot
|
# Same test but for taproot
|
||||||
if self.options.descriptors:
|
if self.options.descriptors:
|
||||||
eckey = ECKey()
|
privkey, pubkey = generate_keypair(wif=True)
|
||||||
eckey.generate()
|
|
||||||
privkey = bytes_to_wif(eckey.get_bytes())
|
|
||||||
|
|
||||||
desc = descsum_create("tr({},pk({}))".format(H_POINT, eckey.get_pubkey().get_bytes().hex()))
|
desc = descsum_create("tr({},pk({}))".format(H_POINT, pubkey.hex()))
|
||||||
res = watchonly.importdescriptors([{"desc": desc, "timestamp": "now"}])
|
res = watchonly.importdescriptors([{"desc": desc, "timestamp": "now"}])
|
||||||
assert res[0]["success"]
|
assert res[0]["success"]
|
||||||
addr = self.nodes[0].deriveaddresses(desc)[0]
|
addr = self.nodes[0].deriveaddresses(desc)[0]
|
||||||
|
|
|
@ -11,7 +11,6 @@ from test_framework.address import (
|
||||||
address_to_scriptpubkey,
|
address_to_scriptpubkey,
|
||||||
script_to_p2sh,
|
script_to_p2sh,
|
||||||
)
|
)
|
||||||
from test_framework.key import ECKey
|
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.util import (
|
from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
|
@ -23,16 +22,16 @@ from test_framework.script_util import (
|
||||||
script_to_p2sh_p2wsh_script,
|
script_to_p2sh_p2wsh_script,
|
||||||
script_to_p2wsh_script,
|
script_to_p2wsh_script,
|
||||||
)
|
)
|
||||||
|
from test_framework.wallet import (
|
||||||
|
getnewdestination,
|
||||||
|
)
|
||||||
from test_framework.wallet_util import (
|
from test_framework.wallet_util import (
|
||||||
bytes_to_wif,
|
generate_keypair,
|
||||||
)
|
)
|
||||||
|
|
||||||
from decimal import (
|
from decimal import (
|
||||||
Decimal,
|
Decimal,
|
||||||
)
|
)
|
||||||
from test_framework.wallet import (
|
|
||||||
getnewdestination,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class SignRawTransactionWithKeyTest(BitcoinTestFramework):
|
class SignRawTransactionWithKeyTest(BitcoinTestFramework):
|
||||||
|
@ -80,11 +79,8 @@ class SignRawTransactionWithKeyTest(BitcoinTestFramework):
|
||||||
def witness_script_test(self):
|
def witness_script_test(self):
|
||||||
self.log.info("Test signing transaction to P2SH-P2WSH addresses without wallet")
|
self.log.info("Test signing transaction to P2SH-P2WSH addresses without wallet")
|
||||||
# Create a new P2SH-P2WSH 1-of-1 multisig address:
|
# Create a new P2SH-P2WSH 1-of-1 multisig address:
|
||||||
eckey = ECKey()
|
embedded_privkey, embedded_pubkey = generate_keypair(wif=True)
|
||||||
eckey.generate()
|
p2sh_p2wsh_address = self.nodes[1].createmultisig(1, [embedded_pubkey.hex()], "p2sh-segwit")
|
||||||
embedded_privkey = bytes_to_wif(eckey.get_bytes())
|
|
||||||
embedded_pubkey = eckey.get_pubkey().get_bytes().hex()
|
|
||||||
p2sh_p2wsh_address = self.nodes[1].createmultisig(1, [embedded_pubkey], "p2sh-segwit")
|
|
||||||
# send transaction to P2SH-P2WSH 1-of-1 multisig address
|
# send transaction to P2SH-P2WSH 1-of-1 multisig address
|
||||||
self.block_hash = self.generate(self.nodes[0], COINBASE_MATURITY + 1)
|
self.block_hash = self.generate(self.nodes[0], COINBASE_MATURITY + 1)
|
||||||
self.blk_idx = 0
|
self.blk_idx = 0
|
||||||
|
@ -109,10 +105,7 @@ class SignRawTransactionWithKeyTest(BitcoinTestFramework):
|
||||||
|
|
||||||
def verify_txn_with_witness_script(self, tx_type):
|
def verify_txn_with_witness_script(self, tx_type):
|
||||||
self.log.info("Test with a {} script as the witnessScript".format(tx_type))
|
self.log.info("Test with a {} script as the witnessScript".format(tx_type))
|
||||||
eckey = ECKey()
|
embedded_privkey, embedded_pubkey = generate_keypair(wif=True)
|
||||||
eckey.generate()
|
|
||||||
embedded_privkey = bytes_to_wif(eckey.get_bytes())
|
|
||||||
embedded_pubkey = eckey.get_pubkey().get_bytes().hex()
|
|
||||||
witness_script = {
|
witness_script = {
|
||||||
'P2PKH': key_to_p2pkh_script(embedded_pubkey).hex(),
|
'P2PKH': key_to_p2pkh_script(embedded_pubkey).hex(),
|
||||||
'P2PK': key_to_p2pk_script(embedded_pubkey).hex()
|
'P2PK': key_to_p2pk_script(embedded_pubkey).hex()
|
||||||
|
|
|
@ -20,6 +20,7 @@ from test_framework.address import (
|
||||||
key_to_p2wpkh,
|
key_to_p2wpkh,
|
||||||
output_key_to_p2tr,
|
output_key_to_p2tr,
|
||||||
)
|
)
|
||||||
|
from test_framework.blocktools import COINBASE_MATURITY
|
||||||
from test_framework.descriptors import descsum_create
|
from test_framework.descriptors import descsum_create
|
||||||
from test_framework.key import (
|
from test_framework.key import (
|
||||||
ECKey,
|
ECKey,
|
||||||
|
@ -53,7 +54,7 @@ from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
assert_greater_than_or_equal,
|
assert_greater_than_or_equal,
|
||||||
)
|
)
|
||||||
from test_framework.blocktools import COINBASE_MATURITY
|
from test_framework.wallet_util import generate_keypair
|
||||||
|
|
||||||
DEFAULT_FEE = Decimal("0.0001")
|
DEFAULT_FEE = Decimal("0.0001")
|
||||||
|
|
||||||
|
@ -395,9 +396,7 @@ def getnewdestination(address_type='bech32m'):
|
||||||
'legacy', 'p2sh-segwit', 'bech32' and 'bech32m'. Can be used when a random
|
'legacy', 'p2sh-segwit', 'bech32' and 'bech32m'. Can be used when a random
|
||||||
destination is needed, but no compiled wallet is available (e.g. as
|
destination is needed, but no compiled wallet is available (e.g. as
|
||||||
replacement to the getnewaddress/getaddressinfo RPCs)."""
|
replacement to the getnewaddress/getaddressinfo RPCs)."""
|
||||||
key = ECKey()
|
key, pubkey = generate_keypair()
|
||||||
key.generate()
|
|
||||||
pubkey = key.get_pubkey().get_bytes()
|
|
||||||
if address_type == 'legacy':
|
if address_type == 'legacy':
|
||||||
scriptpubkey = key_to_p2pkh_script(pubkey)
|
scriptpubkey = key_to_p2pkh_script(pubkey)
|
||||||
address = key_to_p2pkh(pubkey)
|
address = key_to_p2pkh(pubkey)
|
||||||
|
|
|
@ -63,12 +63,9 @@ def get_generate_key():
|
||||||
"""Generate a fresh key
|
"""Generate a fresh key
|
||||||
|
|
||||||
Returns a named tuple of privkey, pubkey and all address and scripts."""
|
Returns a named tuple of privkey, pubkey and all address and scripts."""
|
||||||
eckey = ECKey()
|
privkey, pubkey = generate_keypair(wif=True)
|
||||||
eckey.generate()
|
|
||||||
privkey = bytes_to_wif(eckey.get_bytes())
|
|
||||||
pubkey = eckey.get_pubkey().get_bytes().hex()
|
|
||||||
return Key(privkey=privkey,
|
return Key(privkey=privkey,
|
||||||
pubkey=pubkey,
|
pubkey=pubkey.hex(),
|
||||||
p2pkh_script=key_to_p2pkh_script(pubkey).hex(),
|
p2pkh_script=key_to_p2pkh_script(pubkey).hex(),
|
||||||
p2pkh_addr=key_to_p2pkh(pubkey),
|
p2pkh_addr=key_to_p2pkh(pubkey),
|
||||||
p2wpkh_script=key_to_p2wpkh_script(pubkey).hex(),
|
p2wpkh_script=key_to_p2wpkh_script(pubkey).hex(),
|
||||||
|
@ -114,8 +111,14 @@ def bytes_to_wif(b, compressed=True):
|
||||||
b += b'\x01'
|
b += b'\x01'
|
||||||
return byte_to_base58(b, 239)
|
return byte_to_base58(b, 239)
|
||||||
|
|
||||||
def generate_wif_key():
|
def generate_keypair(compressed=True, wif=False):
|
||||||
# Makes a WIF privkey for imports
|
"""Generate a new random keypair and return the corresponding ECKey /
|
||||||
k = ECKey()
|
bytes objects. The private key can also be provided as WIF (wallet
|
||||||
k.generate()
|
import format) string instead, which is often useful for wallet RPC
|
||||||
return bytes_to_wif(k.get_bytes(), k.is_compressed)
|
interaction."""
|
||||||
|
privkey = ECKey()
|
||||||
|
privkey.generate(compressed)
|
||||||
|
pubkey = privkey.get_pubkey().get_bytes()
|
||||||
|
if wif:
|
||||||
|
privkey = bytes_to_wif(privkey.get_bytes(), compressed)
|
||||||
|
return privkey, pubkey
|
||||||
|
|
|
@ -10,11 +10,10 @@ from test_framework.address import (
|
||||||
ADDRESS_BCRT1_UNSPENDABLE,
|
ADDRESS_BCRT1_UNSPENDABLE,
|
||||||
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR,
|
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR,
|
||||||
)
|
)
|
||||||
from test_framework.key import ECKey
|
|
||||||
from test_framework.util import (
|
from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
)
|
)
|
||||||
from test_framework.wallet_util import bytes_to_wif
|
from test_framework.wallet_util import generate_keypair
|
||||||
|
|
||||||
|
|
||||||
class WalletBlankTest(BitcoinTestFramework):
|
class WalletBlankTest(BitcoinTestFramework):
|
||||||
|
@ -50,10 +49,8 @@ class WalletBlankTest(BitcoinTestFramework):
|
||||||
assert_equal(info["descriptors"], False)
|
assert_equal(info["descriptors"], False)
|
||||||
assert_equal(info["blank"], True)
|
assert_equal(info["blank"], True)
|
||||||
|
|
||||||
eckey = ECKey()
|
_, pubkey = generate_keypair(compressed=comp)
|
||||||
eckey.generate(compressed=comp)
|
wallet.importpubkey(pubkey.hex())
|
||||||
|
|
||||||
wallet.importpubkey(eckey.get_pubkey().get_bytes().hex())
|
|
||||||
assert_equal(wallet.getwalletinfo()["blank"], False)
|
assert_equal(wallet.getwalletinfo()["blank"], False)
|
||||||
|
|
||||||
def test_importprivkey(self):
|
def test_importprivkey(self):
|
||||||
|
@ -67,10 +64,7 @@ class WalletBlankTest(BitcoinTestFramework):
|
||||||
assert_equal(info["descriptors"], False)
|
assert_equal(info["descriptors"], False)
|
||||||
assert_equal(info["blank"], True)
|
assert_equal(info["blank"], True)
|
||||||
|
|
||||||
eckey = ECKey()
|
wif, _ = generate_keypair(compressed=comp, wif=True)
|
||||||
eckey.generate(compressed=comp)
|
|
||||||
wif = bytes_to_wif(eckey.get_bytes(), eckey.is_compressed)
|
|
||||||
|
|
||||||
wallet.importprivkey(wif)
|
wallet.importprivkey(wif)
|
||||||
assert_equal(wallet.getwalletinfo()["blank"], False)
|
assert_equal(wallet.getwalletinfo()["blank"], False)
|
||||||
|
|
||||||
|
|
|
@ -7,13 +7,13 @@
|
||||||
|
|
||||||
from test_framework.address import key_to_p2wpkh
|
from test_framework.address import key_to_p2wpkh
|
||||||
from test_framework.descriptors import descsum_create
|
from test_framework.descriptors import descsum_create
|
||||||
from test_framework.key import ECKey
|
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.util import (
|
from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
assert_raises_rpc_error,
|
assert_raises_rpc_error,
|
||||||
)
|
)
|
||||||
from test_framework.wallet_util import bytes_to_wif, generate_wif_key
|
from test_framework.wallet_util import generate_keypair
|
||||||
|
|
||||||
|
|
||||||
EMPTY_PASSPHRASE_MSG = "Empty string given as passphrase, wallet will not be encrypted."
|
EMPTY_PASSPHRASE_MSG = "Empty string given as passphrase, wallet will not be encrypted."
|
||||||
LEGACY_WALLET_MSG = "Wallet created successfully. The legacy wallet type is being deprecated and support for creating and opening legacy wallets will be removed in the future."
|
LEGACY_WALLET_MSG = "Wallet created successfully. The legacy wallet type is being deprecated and support for creating and opening legacy wallets will be removed in the future."
|
||||||
|
@ -50,14 +50,12 @@ class CreateWalletTest(BitcoinTestFramework):
|
||||||
w1.importpubkey(w0.getaddressinfo(address1)['pubkey'])
|
w1.importpubkey(w0.getaddressinfo(address1)['pubkey'])
|
||||||
|
|
||||||
self.log.info('Test that private keys cannot be imported')
|
self.log.info('Test that private keys cannot be imported')
|
||||||
eckey = ECKey()
|
privkey, pubkey = generate_keypair(wif=True)
|
||||||
eckey.generate()
|
|
||||||
privkey = bytes_to_wif(eckey.get_bytes())
|
|
||||||
assert_raises_rpc_error(-4, 'Cannot import private keys to a wallet with private keys disabled', w1.importprivkey, privkey)
|
assert_raises_rpc_error(-4, 'Cannot import private keys to a wallet with private keys disabled', w1.importprivkey, privkey)
|
||||||
if self.options.descriptors:
|
if self.options.descriptors:
|
||||||
result = w1.importdescriptors([{'desc': descsum_create('wpkh(' + privkey + ')'), 'timestamp': 'now'}])
|
result = w1.importdescriptors([{'desc': descsum_create('wpkh(' + privkey + ')'), 'timestamp': 'now'}])
|
||||||
else:
|
else:
|
||||||
result = w1.importmulti([{'scriptPubKey': {'address': key_to_p2wpkh(eckey.get_pubkey().get_bytes())}, 'timestamp': 'now', 'keys': [privkey]}])
|
result = w1.importmulti([{'scriptPubKey': {'address': key_to_p2wpkh(pubkey)}, 'timestamp': 'now', 'keys': [privkey]}])
|
||||||
assert not result[0]['success']
|
assert not result[0]['success']
|
||||||
assert 'warnings' not in result[0]
|
assert 'warnings' not in result[0]
|
||||||
assert_equal(result[0]['error']['code'], -4)
|
assert_equal(result[0]['error']['code'], -4)
|
||||||
|
@ -77,7 +75,7 @@ class CreateWalletTest(BitcoinTestFramework):
|
||||||
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getnewaddress)
|
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getnewaddress)
|
||||||
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getrawchangeaddress)
|
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getrawchangeaddress)
|
||||||
# Import private key
|
# Import private key
|
||||||
w3.importprivkey(generate_wif_key())
|
w3.importprivkey(generate_keypair(wif=True)[0])
|
||||||
# Imported private keys are currently ignored by the keypool
|
# Imported private keys are currently ignored by the keypool
|
||||||
assert_equal(w3.getwalletinfo()['keypoolsize'], 0)
|
assert_equal(w3.getwalletinfo()['keypoolsize'], 0)
|
||||||
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getnewaddress)
|
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getnewaddress)
|
||||||
|
|
|
@ -10,7 +10,6 @@ from itertools import product
|
||||||
from math import ceil
|
from math import ceil
|
||||||
|
|
||||||
from test_framework.descriptors import descsum_create
|
from test_framework.descriptors import descsum_create
|
||||||
from test_framework.key import ECKey
|
|
||||||
from test_framework.messages import (
|
from test_framework.messages import (
|
||||||
COIN,
|
COIN,
|
||||||
)
|
)
|
||||||
|
@ -25,7 +24,7 @@ from test_framework.util import (
|
||||||
count_bytes,
|
count_bytes,
|
||||||
find_vout_for_address,
|
find_vout_for_address,
|
||||||
)
|
)
|
||||||
from test_framework.wallet_util import bytes_to_wif
|
from test_framework.wallet_util import generate_keypair
|
||||||
|
|
||||||
ERR_NOT_ENOUGH_PRESET_INPUTS = "The preselected coins total amount does not cover the transaction target. " \
|
ERR_NOT_ENOUGH_PRESET_INPUTS = "The preselected coins total amount does not cover the transaction target. " \
|
||||||
"Please allow other inputs to be automatically selected or include more coins manually"
|
"Please allow other inputs to be automatically selected or include more coins manually"
|
||||||
|
@ -999,11 +998,7 @@ class RawTransactionsTest(BitcoinTestFramework):
|
||||||
|
|
||||||
def test_external_inputs(self):
|
def test_external_inputs(self):
|
||||||
self.log.info("Test funding with external inputs")
|
self.log.info("Test funding with external inputs")
|
||||||
|
privkey, _ = generate_keypair(wif=True)
|
||||||
eckey = ECKey()
|
|
||||||
eckey.generate()
|
|
||||||
privkey = bytes_to_wif(eckey.get_bytes())
|
|
||||||
|
|
||||||
self.nodes[2].createwallet("extfund")
|
self.nodes[2].createwallet("extfund")
|
||||||
wallet = self.nodes[2].get_wallet_rpc("extfund")
|
wallet = self.nodes[2].get_wallet_rpc("extfund")
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ from decimal import Decimal
|
||||||
|
|
||||||
from test_framework.address import key_to_p2wpkh
|
from test_framework.address import key_to_p2wpkh
|
||||||
from test_framework.blocktools import COINBASE_MATURITY
|
from test_framework.blocktools import COINBASE_MATURITY
|
||||||
from test_framework.key import ECKey
|
|
||||||
from test_framework.messages import (
|
from test_framework.messages import (
|
||||||
CMerkleBlock,
|
CMerkleBlock,
|
||||||
from_hex,
|
from_hex,
|
||||||
|
@ -17,7 +16,7 @@ from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
assert_raises_rpc_error,
|
assert_raises_rpc_error,
|
||||||
)
|
)
|
||||||
from test_framework.wallet_util import bytes_to_wif
|
from test_framework.wallet_util import generate_keypair
|
||||||
|
|
||||||
|
|
||||||
class ImportPrunedFundsTest(BitcoinTestFramework):
|
class ImportPrunedFundsTest(BitcoinTestFramework):
|
||||||
|
@ -40,10 +39,8 @@ class ImportPrunedFundsTest(BitcoinTestFramework):
|
||||||
# pubkey
|
# pubkey
|
||||||
address2 = self.nodes[0].getnewaddress()
|
address2 = self.nodes[0].getnewaddress()
|
||||||
# privkey
|
# privkey
|
||||||
eckey = ECKey()
|
address3_privkey, address3_pubkey = generate_keypair(wif=True)
|
||||||
eckey.generate()
|
address3 = key_to_p2wpkh(address3_pubkey)
|
||||||
address3_privkey = bytes_to_wif(eckey.get_bytes())
|
|
||||||
address3 = key_to_p2wpkh(eckey.get_pubkey().get_bytes())
|
|
||||||
self.nodes[0].importprivkey(address3_privkey)
|
self.nodes[0].importprivkey(address3_privkey)
|
||||||
|
|
||||||
# Check only one address
|
# Check only one address
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
from test_framework.address import key_to_p2wpkh
|
from test_framework.address import key_to_p2wpkh
|
||||||
from test_framework.blocktools import COINBASE_MATURITY
|
from test_framework.blocktools import COINBASE_MATURITY
|
||||||
from test_framework.descriptors import descsum_create
|
from test_framework.descriptors import descsum_create
|
||||||
from test_framework.key import ECKey
|
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.messages import MAX_BIP125_RBF_SEQUENCE
|
from test_framework.messages import MAX_BIP125_RBF_SEQUENCE
|
||||||
from test_framework.util import (
|
from test_framework.util import (
|
||||||
|
@ -15,7 +14,7 @@ from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
assert_raises_rpc_error,
|
assert_raises_rpc_error,
|
||||||
)
|
)
|
||||||
from test_framework.wallet_util import bytes_to_wif
|
from test_framework.wallet_util import generate_keypair
|
||||||
|
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
|
@ -202,10 +201,8 @@ class ListSinceBlockTest(BitcoinTestFramework):
|
||||||
self.sync_all()
|
self.sync_all()
|
||||||
|
|
||||||
# share utxo between nodes[1] and nodes[2]
|
# share utxo between nodes[1] and nodes[2]
|
||||||
eckey = ECKey()
|
privkey, pubkey = generate_keypair(wif=True)
|
||||||
eckey.generate()
|
address = key_to_p2wpkh(pubkey)
|
||||||
privkey = bytes_to_wif(eckey.get_bytes())
|
|
||||||
address = key_to_p2wpkh(eckey.get_pubkey().get_bytes())
|
|
||||||
self.nodes[2].sendtoaddress(address, 10)
|
self.nodes[2].sendtoaddress(address, 10)
|
||||||
self.generate(self.nodes[2], 6)
|
self.generate(self.nodes[2], 6)
|
||||||
self.nodes[2].importprivkey(privkey)
|
self.nodes[2].importprivkey(privkey)
|
||||||
|
|
|
@ -9,7 +9,6 @@ from itertools import product
|
||||||
|
|
||||||
from test_framework.authproxy import JSONRPCException
|
from test_framework.authproxy import JSONRPCException
|
||||||
from test_framework.descriptors import descsum_create
|
from test_framework.descriptors import descsum_create
|
||||||
from test_framework.key import ECKey
|
|
||||||
from test_framework.messages import (
|
from test_framework.messages import (
|
||||||
ser_compact_size,
|
ser_compact_size,
|
||||||
WITNESS_SCALE_FACTOR,
|
WITNESS_SCALE_FACTOR,
|
||||||
|
@ -22,7 +21,8 @@ from test_framework.util import (
|
||||||
assert_raises_rpc_error,
|
assert_raises_rpc_error,
|
||||||
count_bytes,
|
count_bytes,
|
||||||
)
|
)
|
||||||
from test_framework.wallet_util import bytes_to_wif
|
from test_framework.wallet_util import generate_keypair
|
||||||
|
|
||||||
|
|
||||||
class WalletSendTest(BitcoinTestFramework):
|
class WalletSendTest(BitcoinTestFramework):
|
||||||
def add_options(self, parser):
|
def add_options(self, parser):
|
||||||
|
@ -500,9 +500,7 @@ class WalletSendTest(BitcoinTestFramework):
|
||||||
assert res["complete"]
|
assert res["complete"]
|
||||||
|
|
||||||
self.log.info("External outputs")
|
self.log.info("External outputs")
|
||||||
eckey = ECKey()
|
privkey, _ = generate_keypair(wif=True)
|
||||||
eckey.generate()
|
|
||||||
privkey = bytes_to_wif(eckey.get_bytes())
|
|
||||||
|
|
||||||
self.nodes[1].createwallet("extsend")
|
self.nodes[1].createwallet("extsend")
|
||||||
ext_wallet = self.nodes[1].get_wallet_rpc("extsend")
|
ext_wallet = self.nodes[1].get_wallet_rpc("extsend")
|
||||||
|
|
Loading…
Add table
Reference in a new issue