Skip to content

Commit

Permalink
test: add coverage for migrating standalone imported keys
Browse files Browse the repository at this point in the history
Github-Pull: bitcoin#31374
Rebased-From: cdd207c
  • Loading branch information
furszy authored and achow101 committed Dec 11, 2024
1 parent f6bc1a7 commit 4a4ec83
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test/functional/wallet_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from test_framework.key import ECPubKey
from test_framework.test_framework import BitcoinTestFramework
from test_framework.messages import COIN, CTransaction, CTxOut
from test_framework.script import hash160
from test_framework.script_util import key_to_p2pkh_script, key_to_p2pk_script, script_to_p2sh_script, script_to_p2wsh_script
from test_framework.util import (
assert_equal,
Expand Down Expand Up @@ -1025,6 +1026,50 @@ def test_migrate_simple_watch_only(self):
assert_equal(wo_wallet.listdescriptors()['descriptors'][0]['desc'], descsum_create(f'pk({pubkey.hex()})'))
wo_wallet.unloadwallet()

def test_manual_keys_import(self):
self.log.info("Test migrating standalone private keys")
wallet = self.create_legacy_wallet("import_privkeys", blank=True)
privkey, pubkey = generate_keypair(wif=True)
wallet.importprivkey(privkey=privkey, label="hi", rescan=False)

# Migrate and verify
res, wallet = self.migrate_and_get_rpc("import_privkeys")

# There should be descriptors containing the imported key for: pk(), pkh(), sh(wpkh()), wpkh()
key_origin = hash160(pubkey)[:4].hex()
pubkey_hex = pubkey.hex()
pk_desc = descsum_create(f'pk([{key_origin}]{pubkey_hex})')
pkh_desc = descsum_create(f'pkh([{key_origin}]{pubkey_hex})')
sh_wpkh_desc = descsum_create(f'sh(wpkh([{key_origin}]{pubkey_hex}))')
wpkh_desc = descsum_create(f'wpkh([{key_origin}]{pubkey_hex})')
expected_descs = [pk_desc, pkh_desc, sh_wpkh_desc, wpkh_desc]

# Verify all expected descriptors were migrated
migrated_desc = [item['desc'] for item in wallet.listdescriptors()['descriptors'] if pubkey.hex() in item['desc']]
assert_equal(expected_descs, migrated_desc)
wallet.unloadwallet()

######################################################
self.log.info("Test migrating standalone public keys")
wallet = self.create_legacy_wallet("import_pubkeys", blank=True)
wallet.importpubkey(pubkey=pubkey_hex, rescan=False)

res, _ = self.migrate_and_get_rpc("import_pubkeys")

# Same as before, there should be descriptors in the watch-only wallet for the imported pubkey
wo_wallet = self.nodes[0].get_wallet_rpc(res['watchonly_name'])
# As we imported the pubkey only, there will be no key origin in the following descriptors
pk_desc = descsum_create(f'pk({pubkey_hex})')
pkh_desc = descsum_create(f'pkh({pubkey_hex})')
sh_wpkh_desc = descsum_create(f'sh(wpkh({pubkey_hex}))')
wpkh_desc = descsum_create(f'wpkh({pubkey_hex})')
expected_descs = [pk_desc, pkh_desc, sh_wpkh_desc, wpkh_desc]

# Verify all expected descriptors were migrated
migrated_desc = [item['desc'] for item in wo_wallet.listdescriptors()['descriptors']]
assert_equal(expected_descs, migrated_desc)
wo_wallet.unloadwallet()

def run_test(self):
self.generate(self.nodes[0], 101)

Expand All @@ -1048,6 +1093,7 @@ def run_test(self):
self.test_preserve_tx_extra_info()
self.test_blank()
self.test_migrate_simple_watch_only()
self.test_manual_keys_import()


if __name__ == '__main__':
Expand Down

0 comments on commit 4a4ec83

Please sign in to comment.