Skip to content

Commit cdd207c

Browse files
committed
test: add coverage for migrating standalone imported keys
1 parent 297a876 commit cdd207c

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

test/functional/wallet_migration.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from test_framework.key import ECPubKey
2020
from test_framework.test_framework import BitcoinTestFramework
2121
from test_framework.messages import COIN, CTransaction, CTxOut
22+
from test_framework.script import hash160
2223
from test_framework.script_util import key_to_p2pkh_script, key_to_p2pk_script, script_to_p2sh_script, script_to_p2wsh_script
2324
from test_framework.util import (
2425
assert_equal,
@@ -1019,6 +1020,50 @@ def test_migrate_simple_watch_only(self):
10191020
assert_equal(wo_wallet.listdescriptors()['descriptors'][0]['desc'], descsum_create(f'pk({pubkey.hex()})'))
10201021
wo_wallet.unloadwallet()
10211022

1023+
def test_manual_keys_import(self):
1024+
self.log.info("Test migrating standalone private keys")
1025+
wallet = self.create_legacy_wallet("import_privkeys", blank=True)
1026+
privkey, pubkey = generate_keypair(wif=True)
1027+
wallet.importprivkey(privkey=privkey, label="hi", rescan=False)
1028+
1029+
# Migrate and verify
1030+
res, wallet = self.migrate_and_get_rpc("import_privkeys")
1031+
1032+
# There should be descriptors containing the imported key for: pk(), pkh(), sh(wpkh()), wpkh()
1033+
key_origin = hash160(pubkey)[:4].hex()
1034+
pubkey_hex = pubkey.hex()
1035+
pk_desc = descsum_create(f'pk([{key_origin}]{pubkey_hex})')
1036+
pkh_desc = descsum_create(f'pkh([{key_origin}]{pubkey_hex})')
1037+
sh_wpkh_desc = descsum_create(f'sh(wpkh([{key_origin}]{pubkey_hex}))')
1038+
wpkh_desc = descsum_create(f'wpkh([{key_origin}]{pubkey_hex})')
1039+
expected_descs = [pk_desc, pkh_desc, sh_wpkh_desc, wpkh_desc]
1040+
1041+
# Verify all expected descriptors were migrated
1042+
migrated_desc = [item['desc'] for item in wallet.listdescriptors()['descriptors'] if pubkey.hex() in item['desc']]
1043+
assert_equal(expected_descs, migrated_desc)
1044+
wallet.unloadwallet()
1045+
1046+
######################################################
1047+
self.log.info("Test migrating standalone public keys")
1048+
wallet = self.create_legacy_wallet("import_pubkeys", blank=True)
1049+
wallet.importpubkey(pubkey=pubkey_hex, rescan=False)
1050+
1051+
res, _ = self.migrate_and_get_rpc("import_pubkeys")
1052+
1053+
# Same as before, there should be descriptors in the watch-only wallet for the imported pubkey
1054+
wo_wallet = self.nodes[0].get_wallet_rpc(res['watchonly_name'])
1055+
# As we imported the pubkey only, there will be no key origin in the following descriptors
1056+
pk_desc = descsum_create(f'pk({pubkey_hex})')
1057+
pkh_desc = descsum_create(f'pkh({pubkey_hex})')
1058+
sh_wpkh_desc = descsum_create(f'sh(wpkh({pubkey_hex}))')
1059+
wpkh_desc = descsum_create(f'wpkh({pubkey_hex})')
1060+
expected_descs = [pk_desc, pkh_desc, sh_wpkh_desc, wpkh_desc]
1061+
1062+
# Verify all expected descriptors were migrated
1063+
migrated_desc = [item['desc'] for item in wo_wallet.listdescriptors()['descriptors']]
1064+
assert_equal(expected_descs, migrated_desc)
1065+
wo_wallet.unloadwallet()
1066+
10221067
def run_test(self):
10231068
self.master_node = self.nodes[0]
10241069
self.old_node = self.nodes[1]
@@ -1045,6 +1090,7 @@ def run_test(self):
10451090
self.test_preserve_tx_extra_info()
10461091
self.test_blank()
10471092
self.test_migrate_simple_watch_only()
1093+
self.test_manual_keys_import()
10481094

10491095

10501096
if __name__ == '__main__':

0 commit comments

Comments
 (0)