wallet: Migrate non-HD keys to combo() descriptor

Non-HD keys in legacy wallets without a HD seed ID were being migrated
to separate pk(), pkh(), sh(wpkh()), and wpkh() descriptors for each key.
These could be more compactly represented as combo() descriptors, so
migration should make combo() for them.

It is possible that existing non-HD wallets that were migrated, or
wallets that started blank and had private keys imported into them have
run into this issue. However, as the 4 descriptors produce the same output
scripts as the single combo(), so any previously migrated wallets are
not missing any output scripts. The only observable difference should be
performance related, and the wallet size on disk.
This commit is contained in:
Ava Chow 2024-12-09 14:55:38 -05:00
parent 9039d8f1a1
commit 62b2d23edb
2 changed files with 3 additions and 7 deletions

View File

@ -1799,7 +1799,7 @@ std::optional<MigrationData> LegacyDataSPKM::MigrateToDescriptor()
keyid_it++;
continue;
}
if (m_hd_chain.seed_id == meta.hd_seed_id || m_inactive_hd_chains.count(meta.hd_seed_id) > 0) {
if (!meta.hd_seed_id.IsNull() && (m_hd_chain.seed_id == meta.hd_seed_id || m_inactive_hd_chains.count(meta.hd_seed_id) > 0)) {
keyid_it = keyids.erase(keyid_it);
continue;
}

View File

@ -1032,15 +1032,11 @@ class WalletMigrationTest(BitcoinTestFramework):
# 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]
combo_desc = descsum_create(f"combo([{key_origin}]{pubkey_hex})")
# 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)
assert_equal([combo_desc], migrated_desc)
wallet.unloadwallet()
######################################################