@@ -4165,32 +4165,19 @@ bool DoMigration(CWallet& wallet, WalletContext& context, bilingual_str& error,
4165
4165
return true ;
4166
4166
}
4167
4167
4168
- util::Result<MigrationResult> MigrateLegacyToDescriptor (std::shared_ptr<CWallet>&& wallet , WalletContext& context)
4168
+ util::Result<MigrationResult> MigrateLegacyToDescriptor (const std::string& wallet_name , WalletContext& context)
4169
4169
{
4170
- // Before anything else, check if there is something to migrate.
4171
- if (!wallet->GetLegacyScriptPubKeyMan ()) {
4172
- return util::Error{_ (" Error: This wallet is already a descriptor wallet" )};
4173
- }
4174
-
4175
4170
MigrationResult res;
4176
4171
bilingual_str error;
4177
4172
std::vector<bilingual_str> warnings;
4178
4173
4179
- // Make a backup of the DB
4180
- std::string wallet_name = wallet->GetName ();
4181
- fs::path this_wallet_dir = fs::absolute (fs::PathFromString (wallet->GetDatabase ().Filename ())).parent_path ();
4182
- fs::path backup_filename = fs::PathFromString (strprintf (" %s-%d.legacy.bak" , wallet_name, GetTime ()));
4183
- fs::path backup_path = this_wallet_dir / backup_filename;
4184
- if (!wallet->BackupWallet (fs::PathToString (backup_path))) {
4185
- return util::Error{_ (" Error: Unable to make a backup of your wallet" )};
4186
- }
4187
- res.backup_path = backup_path;
4188
-
4189
- // Unload the wallet so that nothing else tries to use it while we're changing it
4190
- if (!RemoveWallet (context, wallet, /* load_on_start=*/ std::nullopt, warnings)) {
4191
- return util::Error{_ (" Unable to unload the wallet before migrating" )};
4174
+ // If the wallet is still loaded, unload it so that nothing else tries to use it while we're changing it
4175
+ if (auto wallet = GetWallet (context, wallet_name)) {
4176
+ if (!RemoveWallet (context, wallet, /* load_on_start=*/ std::nullopt, warnings)) {
4177
+ return util::Error{_ (" Unable to unload the wallet before migrating" )};
4178
+ }
4179
+ UnloadWallet (std::move (wallet));
4192
4180
}
4193
- UnloadWallet (std::move (wallet));
4194
4181
4195
4182
// Load the wallet but only in the context of this function.
4196
4183
// No signals should be connected nor should anything else be aware of this wallet
@@ -4204,11 +4191,26 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
4204
4191
return util::Error{Untranslated (" Wallet file verification failed." ) + Untranslated (" " ) + error};
4205
4192
}
4206
4193
4194
+ // Make the local wallet
4207
4195
std::shared_ptr<CWallet> local_wallet = CWallet::Create (empty_context, wallet_name, std::move (database), options.create_flags , error, warnings);
4208
4196
if (!local_wallet) {
4209
4197
return util::Error{Untranslated (" Wallet loading failed." ) + Untranslated (" " ) + error};
4210
4198
}
4211
4199
4200
+ // Before anything else, check if there is something to migrate.
4201
+ if (!local_wallet->GetLegacyScriptPubKeyMan ()) {
4202
+ return util::Error{_ (" Error: This wallet is already a descriptor wallet" )};
4203
+ }
4204
+
4205
+ // Make a backup of the DB
4206
+ fs::path this_wallet_dir = fs::absolute (fs::PathFromString (local_wallet->GetDatabase ().Filename ())).parent_path ();
4207
+ fs::path backup_filename = fs::PathFromString (strprintf (" %s-%d.legacy.bak" , wallet_name, GetTime ()));
4208
+ fs::path backup_path = this_wallet_dir / backup_filename;
4209
+ if (!local_wallet->BackupWallet (fs::PathToString (backup_path))) {
4210
+ return util::Error{_ (" Error: Unable to make a backup of your wallet" )};
4211
+ }
4212
+ res.backup_path = backup_path;
4213
+
4212
4214
bool success = false ;
4213
4215
{
4214
4216
LOCK (local_wallet->cs_wallet );
0 commit comments