mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-03 09:56:38 -05:00
wallet: Allow MigrateLegacyToDescriptor to take a wallet name
An overload of MigrateLegacyToDescriptor is added which takes the wallet name. The original that took a wallet pointer is still available, it just gets the name, closes the wallet, and calls the new overload.
This commit is contained in:
parent
7753efcbcf
commit
dbfa345403
3 changed files with 32 additions and 26 deletions
|
@ -737,6 +737,8 @@ static RPCHelpMan migratewallet()
|
||||||
+ HelpExampleRpc("migratewallet", "")
|
+ HelpExampleRpc("migratewallet", "")
|
||||||
},
|
},
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||||
|
{
|
||||||
|
std::string wallet_name;
|
||||||
{
|
{
|
||||||
std::shared_ptr<CWallet> wallet = GetWalletForJSONRPCRequest(request);
|
std::shared_ptr<CWallet> wallet = GetWalletForJSONRPCRequest(request);
|
||||||
if (!wallet) return NullUniValue;
|
if (!wallet) return NullUniValue;
|
||||||
|
@ -744,10 +746,12 @@ static RPCHelpMan migratewallet()
|
||||||
if (wallet->IsCrypted()) {
|
if (wallet->IsCrypted()) {
|
||||||
throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: migratewallet on encrypted wallets is currently unsupported.");
|
throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: migratewallet on encrypted wallets is currently unsupported.");
|
||||||
}
|
}
|
||||||
|
wallet_name = wallet->GetName();
|
||||||
|
}
|
||||||
|
|
||||||
WalletContext& context = EnsureWalletContext(request.context);
|
WalletContext& context = EnsureWalletContext(request.context);
|
||||||
|
|
||||||
util::Result<MigrationResult> res = MigrateLegacyToDescriptor(std::move(wallet), context);
|
util::Result<MigrationResult> res = MigrateLegacyToDescriptor(wallet_name, context);
|
||||||
if (!res) {
|
if (!res) {
|
||||||
throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(res).original);
|
throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(res).original);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4165,32 +4165,19 @@ bool DoMigration(CWallet& wallet, WalletContext& context, bilingual_str& error,
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>&& wallet, WalletContext& context)
|
util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& wallet_name, WalletContext& context)
|
||||||
{
|
{
|
||||||
// Before anything else, check if there is something to migrate.
|
|
||||||
if (!wallet->GetLegacyScriptPubKeyMan()) {
|
|
||||||
return util::Error{_("Error: This wallet is already a descriptor wallet")};
|
|
||||||
}
|
|
||||||
|
|
||||||
MigrationResult res;
|
MigrationResult res;
|
||||||
bilingual_str error;
|
bilingual_str error;
|
||||||
std::vector<bilingual_str> warnings;
|
std::vector<bilingual_str> warnings;
|
||||||
|
|
||||||
// Make a backup of the DB
|
// If the wallet is still loaded, unload it so that nothing else tries to use it while we're changing it
|
||||||
std::string wallet_name = wallet->GetName();
|
if (auto wallet = GetWallet(context, wallet_name)) {
|
||||||
fs::path this_wallet_dir = fs::absolute(fs::PathFromString(wallet->GetDatabase().Filename())).parent_path();
|
|
||||||
fs::path backup_filename = fs::PathFromString(strprintf("%s-%d.legacy.bak", wallet_name, GetTime()));
|
|
||||||
fs::path backup_path = this_wallet_dir / backup_filename;
|
|
||||||
if (!wallet->BackupWallet(fs::PathToString(backup_path))) {
|
|
||||||
return util::Error{_("Error: Unable to make a backup of your wallet")};
|
|
||||||
}
|
|
||||||
res.backup_path = backup_path;
|
|
||||||
|
|
||||||
// Unload the wallet so that nothing else tries to use it while we're changing it
|
|
||||||
if (!RemoveWallet(context, wallet, /*load_on_start=*/std::nullopt, warnings)) {
|
if (!RemoveWallet(context, wallet, /*load_on_start=*/std::nullopt, warnings)) {
|
||||||
return util::Error{_("Unable to unload the wallet before migrating")};
|
return util::Error{_("Unable to unload the wallet before migrating")};
|
||||||
}
|
}
|
||||||
UnloadWallet(std::move(wallet));
|
UnloadWallet(std::move(wallet));
|
||||||
|
}
|
||||||
|
|
||||||
// Load the wallet but only in the context of this function.
|
// Load the wallet but only in the context of this function.
|
||||||
// No signals should be connected nor should anything else be aware of this wallet
|
// 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>
|
||||||
return util::Error{Untranslated("Wallet file verification failed.") + Untranslated(" ") + error};
|
return util::Error{Untranslated("Wallet file verification failed.") + Untranslated(" ") + error};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make the local wallet
|
||||||
std::shared_ptr<CWallet> local_wallet = CWallet::Create(empty_context, wallet_name, std::move(database), options.create_flags, error, warnings);
|
std::shared_ptr<CWallet> local_wallet = CWallet::Create(empty_context, wallet_name, std::move(database), options.create_flags, error, warnings);
|
||||||
if (!local_wallet) {
|
if (!local_wallet) {
|
||||||
return util::Error{Untranslated("Wallet loading failed.") + Untranslated(" ") + error};
|
return util::Error{Untranslated("Wallet loading failed.") + Untranslated(" ") + error};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Before anything else, check if there is something to migrate.
|
||||||
|
if (!local_wallet->GetLegacyScriptPubKeyMan()) {
|
||||||
|
return util::Error{_("Error: This wallet is already a descriptor wallet")};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make a backup of the DB
|
||||||
|
fs::path this_wallet_dir = fs::absolute(fs::PathFromString(local_wallet->GetDatabase().Filename())).parent_path();
|
||||||
|
fs::path backup_filename = fs::PathFromString(strprintf("%s-%d.legacy.bak", wallet_name, GetTime()));
|
||||||
|
fs::path backup_path = this_wallet_dir / backup_filename;
|
||||||
|
if (!local_wallet->BackupWallet(fs::PathToString(backup_path))) {
|
||||||
|
return util::Error{_("Error: Unable to make a backup of your wallet")};
|
||||||
|
}
|
||||||
|
res.backup_path = backup_path;
|
||||||
|
|
||||||
bool success = false;
|
bool success = false;
|
||||||
{
|
{
|
||||||
LOCK(local_wallet->cs_wallet);
|
LOCK(local_wallet->cs_wallet);
|
||||||
|
|
|
@ -1007,7 +1007,7 @@ struct MigrationResult {
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Do all steps to migrate a legacy wallet to a descriptor wallet
|
//! Do all steps to migrate a legacy wallet to a descriptor wallet
|
||||||
util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>&& wallet, WalletContext& context);
|
util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& wallet_name, WalletContext& context);
|
||||||
} // namespace wallet
|
} // namespace wallet
|
||||||
|
|
||||||
#endif // BITCOIN_WALLET_WALLET_H
|
#endif // BITCOIN_WALLET_WALLET_H
|
||||||
|
|
Loading…
Add table
Reference in a new issue