From fcc457573f9b39e6f173a4f51c45d7dbb47e7ab0 Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Thu, 8 May 2025 11:26:34 -0700 Subject: [PATCH] test: Replace importprivkey with wallet_importprivkey importprivkey was a legacy wallet only RPC which had a helper for descriptor wallets in tests. Add wallet_importprivkey helper and use it wherever importprivkey is used (other than backward compatibility tests) --- test/functional/rpc_getblockstats.py | 3 ++- test/functional/rpc_psbt.py | 5 +++-- test/functional/test_framework/test_framework.py | 3 ++- test/functional/test_framework/test_node.py | 14 -------------- test/functional/test_framework/util.py | 11 +++++++++++ test/functional/tool_signet_miner.py | 7 +++++-- test/functional/wallet_createwallet.py | 3 ++- test/functional/wallet_hd.py | 3 ++- test/functional/wallet_importprunedfunds.py | 5 +++-- test/functional/wallet_listsinceblock.py | 5 +++-- 10 files changed, 33 insertions(+), 26 deletions(-) diff --git a/test/functional/rpc_getblockstats.py b/test/functional/rpc_getblockstats.py index 002763201a5..8a88aaa5787 100755 --- a/test/functional/rpc_getblockstats.py +++ b/test/functional/rpc_getblockstats.py @@ -12,6 +12,7 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, assert_raises_rpc_error, + wallet_importprivkey, ) import json import os @@ -45,7 +46,7 @@ class GetblockstatsTest(BitcoinTestFramework): self.nodes[0].setmocktime(mocktime) self.nodes[0].createwallet(wallet_name='test') privkey = self.nodes[0].get_deterministic_priv_key().key - self.nodes[0].importprivkey(privkey) + wallet_importprivkey(self.nodes[0], privkey, 0) self.generate(self.nodes[0], COINBASE_MATURITY + 1) diff --git a/test/functional/rpc_psbt.py b/test/functional/rpc_psbt.py index deb519803cf..9d4750a7322 100755 --- a/test/functional/rpc_psbt.py +++ b/test/functional/rpc_psbt.py @@ -44,6 +44,7 @@ from test_framework.util import ( assert_greater_than_or_equal, assert_raises_rpc_error, find_vout_for_address, + wallet_importprivkey, ) from test_framework.wallet_util import ( calculate_input_weight, @@ -622,7 +623,7 @@ class PSBTTest(BitcoinTestFramework): self.nodes[2].createwallet(wallet_name="wallet{}".format(i)) wrpc = self.nodes[2].get_wallet_rpc("wallet{}".format(i)) for key in signer['privkeys']: - wrpc.importprivkey(key) + wallet_importprivkey(wrpc, key, "now") signed_tx = wrpc.walletprocesspsbt(signer['psbt'], True, "ALL")['psbt'] assert_equal(signed_tx, signer['result']) @@ -878,7 +879,7 @@ class PSBTTest(BitcoinTestFramework): addr = self.nodes[0].deriveaddresses(desc)[0] self.nodes[0].sendtoaddress(addr, 10) self.generate(self.nodes[0], 1) - self.nodes[0].importprivkey(privkey) + wallet_importprivkey(self.nodes[0], privkey, "now") psbt = watchonly.sendall([wallet.getnewaddress()])["psbt"] signed_tx = self.nodes[0].walletprocesspsbt(psbt) diff --git a/test/functional/test_framework/test_framework.py b/test/functional/test_framework/test_framework.py index 75a0cb6f112..062ea5f5291 100755 --- a/test/functional/test_framework/test_framework.py +++ b/test/functional/test_framework/test_framework.py @@ -35,6 +35,7 @@ from .util import ( initialize_datadir, p2p_port, wait_until_helper_internal, + wallet_importprivkey, ) @@ -485,7 +486,7 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass): n = self.nodes[node] if wallet_name is not None: n.createwallet(wallet_name=wallet_name, load_on_startup=True) - n.importprivkey(privkey=n.get_deterministic_priv_key().key, label='coinbase', rescan=True) + wallet_importprivkey(n.get_wallet_rpc(wallet_name), n.get_deterministic_priv_key().key, 0, label="coinbase") def run_test(self): """Tests must override this method to define test logic""" diff --git a/test/functional/test_framework/test_node.py b/test/functional/test_framework/test_node.py index 9f0baaccca2..02e3b0c4e30 100755 --- a/test/functional/test_framework/test_node.py +++ b/test/functional/test_framework/test_node.py @@ -931,20 +931,6 @@ class RPCOverloadWrapper(): def __getattr__(self, name): return getattr(self.rpc, name) - def importprivkey(self, privkey, *, label=None, rescan=None): - wallet_info = self.getwalletinfo() - if 'descriptors' not in wallet_info or ('descriptors' in wallet_info and not wallet_info['descriptors']): - return self.__getattr__('importprivkey')(privkey, label, rescan) - desc = descsum_create('combo(' + privkey + ')') - req = [{ - 'desc': desc, - 'timestamp': 0 if rescan else 'now', - 'label': label if label else '', - }] - import_res = self.importdescriptors(req) - if not import_res[0]['success']: - raise JSONRPCException(import_res[0]['error']) - def addmultisigaddress(self, nrequired, keys, *, label=None, address_type=None): wallet_info = self.getwalletinfo() if 'descriptors' not in wallet_info or ('descriptors' in wallet_info and not wallet_info['descriptors']): diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py index c7b5cc5d592..172f26de73b 100644 --- a/test/functional/test_framework/util.py +++ b/test/functional/test_framework/util.py @@ -20,6 +20,7 @@ import time from . import coverage from .authproxy import AuthServiceProxy, JSONRPCException +from .descriptors import descsum_create from collections.abc import Callable from typing import Optional, Union @@ -609,3 +610,13 @@ def sync_txindex(test_framework, node): sync_start = int(time.time()) test_framework.wait_until(lambda: node.getindexinfo("txindex")["txindex"]["synced"]) test_framework.log.debug(f"Synced in {time.time() - sync_start} seconds") + +def wallet_importprivkey(wallet_rpc, privkey, timestamp, *, label=""): + desc = descsum_create("combo(" + privkey + ")") + req = [{ + "desc": desc, + "timestamp": timestamp, + "label": label, + }] + import_res = wallet_rpc.importdescriptors(req) + assert_equal(import_res[0]["success"], True) diff --git a/test/functional/tool_signet_miner.py b/test/functional/tool_signet_miner.py index c7d5e90fce1..d2c0f6e3abc 100755 --- a/test/functional/tool_signet_miner.py +++ b/test/functional/tool_signet_miner.py @@ -14,7 +14,10 @@ from test_framework.blocktools import DIFF_1_N_BITS from test_framework.key import ECKey from test_framework.script_util import key_to_p2wpkh_script from test_framework.test_framework import BitcoinTestFramework -from test_framework.util import assert_equal +from test_framework.util import ( + assert_equal, + wallet_importprivkey, +) from test_framework.wallet_util import bytes_to_wif @@ -42,7 +45,7 @@ class SignetMinerTest(BitcoinTestFramework): def run_test(self): node = self.nodes[0] # import private key needed for signing block - node.importprivkey(bytes_to_wif(CHALLENGE_PRIVATE_KEY)) + wallet_importprivkey(node, bytes_to_wif(CHALLENGE_PRIVATE_KEY), "now") # generate block with signet miner tool base_dir = self.config["environment"]["SRCDIR"] diff --git a/test/functional/wallet_createwallet.py b/test/functional/wallet_createwallet.py index f6da0605b8a..edd54fe4387 100755 --- a/test/functional/wallet_createwallet.py +++ b/test/functional/wallet_createwallet.py @@ -10,6 +10,7 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, assert_raises_rpc_error, + wallet_importprivkey, ) from test_framework.wallet_util import generate_keypair, WalletUnlock @@ -65,7 +66,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.getrawchangeaddress) # Import private key - w3.importprivkey(generate_keypair(wif=True)[0]) + wallet_importprivkey(w3, generate_keypair(wif=True)[0], "now") # Imported private keys are currently ignored by the keypool assert_equal(w3.getwalletinfo()['keypoolsize'], 0) assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getnewaddress) diff --git a/test/functional/wallet_hd.py b/test/functional/wallet_hd.py index 23b14f00c0c..4eb080329cd 100755 --- a/test/functional/wallet_hd.py +++ b/test/functional/wallet_hd.py @@ -10,6 +10,7 @@ from test_framework.blocktools import COINBASE_MATURITY from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, + wallet_importprivkey, ) @@ -39,7 +40,7 @@ class WalletHDTest(BitcoinTestFramework): # Import a non-HD private key in the HD wallet non_hd_add = 'bcrt1qmevj8zfx0wdvp05cqwkmr6mxkfx60yezwjksmt' non_hd_key = 'cS9umN9w6cDMuRVYdbkfE4c7YUFLJRoXMfhQ569uY4odiQbVN8Rt' - self.nodes[1].importprivkey(non_hd_key) + wallet_importprivkey(self.nodes[1], non_hd_key, "now") # This should be enough to keep the master key and the non-HD key self.nodes[1].backupwallet(self.nodes[1].datadir_path / "hd.bak") diff --git a/test/functional/wallet_importprunedfunds.py b/test/functional/wallet_importprunedfunds.py index 6c822d1cd9f..b97735389c7 100755 --- a/test/functional/wallet_importprunedfunds.py +++ b/test/functional/wallet_importprunedfunds.py @@ -15,6 +15,7 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, assert_raises_rpc_error, + wallet_importprivkey, ) from test_framework.wallet_util import generate_keypair @@ -38,7 +39,7 @@ class ImportPrunedFundsTest(BitcoinTestFramework): # privkey address3_privkey, address3_pubkey = generate_keypair(wif=True) address3 = key_to_p2wpkh(address3_pubkey) - self.nodes[0].importprivkey(address3_privkey) + wallet_importprivkey(self.nodes[0], address3_privkey, "now") # Check only one address address_info = self.nodes[0].getaddressinfo(address1) @@ -95,7 +96,7 @@ class ImportPrunedFundsTest(BitcoinTestFramework): # Import with private key with no rescan w1 = self.nodes[1].get_wallet_rpc(self.default_wallet_name) - w1.importprivkey(privkey=address3_privkey, rescan=False) + wallet_importprivkey(w1, address3_privkey, "now") w1.importprunedfunds(rawtxn3, proof3) assert [tx for tx in w1.listtransactions() if tx['txid'] == txnid3] balance3 = w1.getbalance() diff --git a/test/functional/wallet_listsinceblock.py b/test/functional/wallet_listsinceblock.py index 3328ae98e62..d1f5d3f2c35 100755 --- a/test/functional/wallet_listsinceblock.py +++ b/test/functional/wallet_listsinceblock.py @@ -13,6 +13,7 @@ from test_framework.util import ( assert_array_result, assert_equal, assert_raises_rpc_error, + wallet_importprivkey, ) from test_framework.wallet_util import generate_keypair @@ -227,10 +228,10 @@ class ListSinceBlockTest(BitcoinTestFramework): address = key_to_p2wpkh(pubkey) self.nodes[2].sendtoaddress(address, 10) self.generate(self.nodes[2], 6) - self.nodes[2].importprivkey(privkey) + wallet_importprivkey(self.nodes[2], privkey, "now") utxos = self.nodes[2].listunspent() utxo = [u for u in utxos if u["address"] == address][0] - self.nodes[1].importprivkey(privkey) + wallet_importprivkey(self.nodes[1], privkey, "now") # Split network into two self.split_network()