From dbc1e70a3c97d1fae9d7b7e03b667d34fde41e3b Mon Sep 17 00:00:00 2001 From: pasta Date: Tue, 3 Nov 2026 05:53:09 +0000 Subject: [PATCH] fix(wallet): require -walletmigration before migrating a legacy wallet Legacy-to-descriptor wallet migration is new and experimental in this release, and its failure handling is still being reworked. Make it opt-in so that a default node never starts a migration: - MigrateLegacyToDescriptor() refuses unless the node was started with -walletmigration. The check is its first statement, so a refused call leaves the wallet loaded and unchanged on disk. It covers both the migratewallet RPC and the GUI, which share this function. - The GUI hides File -> Migrate Wallet unless -walletmigration is set. wallet_migration.py now starts its node without the option, checks that migratewallet is refused and that the wallet directory is unchanged, then restarts with -walletmigration for the existing cases. Co-Authored-By: Claude Opus 5.5 --- doc/managing-wallets.md | 4 +++- doc/release-notes-19602.md | 3 ++- src/dummywallet.cpp | 1 + src/qt/bitcoingui.cpp | 3 +++ src/wallet/init.cpp | 1 + src/wallet/rpc/wallet.cpp | 3 ++- src/wallet/wallet.cpp | 4 ++++ src/wallet/wallet.h | 2 ++ test/functional/wallet_migration.py | 20 ++++++++++++++++++++ 9 files changed, 38 insertions(+), 3 deletions(-) diff --git a/doc/managing-wallets.md b/doc/managing-wallets.md index ca89186edd2f..43a814beb466 100644 --- a/doc/managing-wallets.md +++ b/doc/managing-wallets.md @@ -125,7 +125,9 @@ The restored wallet can also be loaded in the GUI via `File` ->`Open wallet`. ## Migrating Legacy Wallets to Descriptor Wallets Legacy wallets (traditional non-descriptor wallets) can be migrated to become Descriptor wallets -through the use of the `migratewallet` RPC. Migrated wallets will have all of their addresses and private keys added to +through the use of the `migratewallet` RPC. Migration is experimental and must be enabled by starting +the node with `-walletmigration`, which also shows the `File` -> `Migrate Wallet` menu item in the GUI. +Migrated wallets will have all of their addresses and private keys added to a newly created Descriptor wallet that has the same name as the original wallet. Because Descriptor wallets do not support having private keys and watch-only scripts, there may be up to two additional wallets created after migration. In addition to a descriptor wallet of the same name, diff --git a/doc/release-notes-19602.md b/doc/release-notes-19602.md index fc0a68a4a1a8..8339ea043484 100644 --- a/doc/release-notes-19602.md +++ b/doc/release-notes-19602.md @@ -5,5 +5,6 @@ Migrating Legacy Wallets to Descriptor Wallets --------------------------------------------- An experimental RPC `migratewallet` has been added to migrate Legacy (non-descriptor) wallets to -Descriptor wallets. More information about the migration process is available in the +Descriptor wallets. It is disabled by default: start the node with `-walletmigration` to use it, which +also shows the `File` -> `Migrate Wallet` menu item in the GUI. More information about the migration process is available in the [documentation](https://github.com/dashpay/dash/blob/master/doc/managing-wallets.md#migrating-legacy-wallets-to-descriptor-wallets). diff --git a/src/dummywallet.cpp b/src/dummywallet.cpp index 6f9e18d8b27e..96e868f251c7 100644 --- a/src/dummywallet.cpp +++ b/src/dummywallet.cpp @@ -55,6 +55,7 @@ void DummyWalletInit::AddWalletOptions(ArgsManager& argsman) const "-walletbackupsdir=", "-walletbroadcast", "-walletdir=", + "-walletmigration", "-walletnotify=", "-discardfee=", "-fallbackfee=", diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 0ad815bd8bcf..497cd5dbbb5c 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -41,6 +41,8 @@ #include #include #include + +#include #endif // ENABLE_WALLET #ifdef Q_OS_MACOS @@ -623,6 +625,7 @@ void BitcoinGUI::createActions() connect(m_close_all_wallets_action, &QAction::triggered, [this] { m_wallet_controller->closeAllWallets(this); }); + m_migrate_wallet_action->setVisible(gArgs.GetBoolArg("-walletmigration", wallet::DEFAULT_WALLET_MIGRATION)); connect(m_migrate_wallet_action, &QAction::triggered, [this] { auto activity = new MigrateWalletActivity(m_wallet_controller, this); connect(activity, &MigrateWalletActivity::migrated, this, &BitcoinGUI::setCurrentWallet); diff --git a/src/wallet/init.cpp b/src/wallet/init.cpp index 0b9de2c34f0e..4a60fad1382a 100644 --- a/src/wallet/init.cpp +++ b/src/wallet/init.cpp @@ -82,6 +82,7 @@ void WalletInit::AddWalletOptions(ArgsManager& argsman) const argsman.AddArg("-walletbackupsdir=", "Specify full path to directory for automatic wallet backups (must exist)", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET); argsman.AddArg("-walletbroadcast", strprintf("Make the wallet broadcast transactions (default: %u)", DEFAULT_WALLETBROADCAST), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET); argsman.AddArg("-walletdir=", "Specify directory to hold wallets (default: /wallets if it exists, otherwise )", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::WALLET); + argsman.AddArg("-walletmigration", strprintf("Allow migrating legacy wallets to descriptor wallets with the experimental migratewallet RPC and the GUI Migrate Wallet menu item (default: %u)", DEFAULT_WALLET_MIGRATION), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET); #if HAVE_SYSTEM argsman.AddArg("-walletnotify=", "Execute command when a wallet transaction changes. %s in cmd is replaced by TxID, %w is replaced by wallet name, %b is replaced by the hash of the block including the transaction (set to 'unconfirmed' if the transaction is not included) and %h is replaced by the block height (-1 if not included). %w is not currently implemented on windows. On systems where %w is supported, it should NOT be quoted because this would break shell escaping used to invoke the command.", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET); #endif diff --git a/src/wallet/rpc/wallet.cpp b/src/wallet/rpc/wallet.cpp index e0a85beb6d8a..f6660561c9a9 100644 --- a/src/wallet/rpc/wallet.cpp +++ b/src/wallet/rpc/wallet.cpp @@ -1136,7 +1136,8 @@ static RPCHelpMan migratewallet() "file will be named -.legacy.bak and can be found in the directory\n" "for this wallet. In the event of an incorrect migration, the backup can be restored using restorewallet." "\nEncrypted wallets must have the passphrase provided as an argument to this call.\n" - "\nThis RPC may take a long time to complete. Increasing the RPC client timeout is recommended.", + "\nThis RPC may take a long time to complete. Increasing the RPC client timeout is recommended.\n" + "\nThis RPC is experimental and requires the -walletmigration startup option.", { {"wallet_name", RPCArg::Type::STR, RPCArg::DefaultHint{"the wallet name from the RPC endpoint"}, "The name of the wallet to migrate. If provided both here and in the RPC endpoint, the two must be identical."}, {"passphrase", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "The wallet passphrase"}, diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 8d151593bc46..3d416b0ae7fc 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -5150,6 +5150,10 @@ bool DoMigration(CWallet& wallet, WalletContext& context, bilingual_str& error, util::Result MigrateLegacyToDescriptor(const std::string& wallet_name, const SecureString& passphrase, WalletContext& context) { + if (!context.args || !context.args->GetBoolArg("-walletmigration", DEFAULT_WALLET_MIGRATION)) { + return util::Error{Untranslated("Wallet migration is experimental and disabled by default. Restart with -walletmigration to enable it.")}; + } + MigrationResult res; bilingual_str error; std::vector warnings; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 02af32e21391..777df0c7cdb4 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -114,6 +114,8 @@ static const unsigned int DEFAULT_TX_CONFIRM_TARGET = 6; static const bool DEFAULT_WALLETBROADCAST = true; static const bool DEFAULT_DISABLE_WALLET = false; static const bool DEFAULT_WALLETCROSSCHAIN = false; +//! -walletmigration default +static constexpr bool DEFAULT_WALLET_MIGRATION{false}; //! -dustprotectionthreshold default (0 = disabled) static constexpr CAmount DEFAULT_DUST_PROTECTION_THRESHOLD{0}; //! -dustprotectionthreshold maximum (matches GUI spinbox cap) diff --git a/test/functional/wallet_migration.py b/test/functional/wallet_migration.py index 419772242846..87563ebedbcd 100755 --- a/test/functional/wallet_migration.py +++ b/test/functional/wallet_migration.py @@ -90,6 +90,25 @@ def check_address(self, wallet, addr, is_mine, is_change, label): else: assert_equal(addr_info['labels'], []), + def test_migration_requires_opt_in(self): + self.log.info("Test that migration is refused, leaving the wallet untouched, without -walletmigration") + wallet = self.create_legacy_wallet("optin") + wallet_dir = self.nodes[0].wallets_path / "optin" + files_before = sorted(os.listdir(wallet_dir)) + + assert_raises_rpc_error(-4, "Wallet migration is experimental and disabled by default", wallet.migratewallet) + assert_raises_rpc_error(-4, "Wallet migration is experimental and disabled by default", self.nodes[0].migratewallet, "optin") + + assert "optin" in self.nodes[0].listwallets() + info = wallet.getwalletinfo() + assert_equal(info["descriptors"], False) + assert_equal(info["format"], "bdb") + assert_equal(sorted(os.listdir(wallet_dir)), files_before) + + wallet.unloadwallet() + self.nodes[0].extra_args = ["-walletmigration"] + self.restart_node(0) + def test_basic(self): default = self.nodes[0].get_wallet_rpc(self.default_wallet_name) @@ -880,6 +899,7 @@ def check(info, node): def run_test(self): self.generate(self.nodes[0], 101) + self.test_migration_requires_opt_in() # TODO: Test the actual records in the wallet for these tests too. The behavior may be correct, but the data written may not be what we actually want self.test_basic() self.test_multisig()