mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-21 14:34:49 +01:00
Add a new RPC command: restorewallet
This commit is contained in:
parent
0b5344b0d1
commit
ae23faba6f
2 changed files with 96 additions and 24 deletions
|
@ -187,6 +187,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
|
|||
{ "createwallet", 5, "descriptors"},
|
||||
{ "createwallet", 6, "load_on_startup"},
|
||||
{ "createwallet", 7, "external_signer"},
|
||||
{ "restorewallet", 2, "load_on_startup"},
|
||||
{ "loadwallet", 1, "load_on_startup"},
|
||||
{ "unloadwallet", 1, "load_on_startup"},
|
||||
{ "getnodeaddresses", 0, "count"},
|
||||
|
|
|
@ -2572,6 +2572,37 @@ static RPCHelpMan listwallets()
|
|||
};
|
||||
}
|
||||
|
||||
static std::tuple<std::shared_ptr<CWallet>, std::vector<bilingual_str>> LoadWalletHelper(WalletContext& context, UniValue load_on_start_param, const std::string wallet_name)
|
||||
{
|
||||
DatabaseOptions options;
|
||||
DatabaseStatus status;
|
||||
options.require_existing = true;
|
||||
bilingual_str error;
|
||||
std::vector<bilingual_str> warnings;
|
||||
std::optional<bool> load_on_start = load_on_start_param.isNull() ? std::nullopt : std::optional<bool>(load_on_start_param.get_bool());
|
||||
std::shared_ptr<CWallet> const wallet = LoadWallet(*context.chain, wallet_name, load_on_start, options, status, error, warnings);
|
||||
|
||||
if (!wallet) {
|
||||
// Map bad format to not found, since bad format is returned when the
|
||||
// wallet directory exists, but doesn't contain a data file.
|
||||
RPCErrorCode code = RPC_WALLET_ERROR;
|
||||
switch (status) {
|
||||
case DatabaseStatus::FAILED_NOT_FOUND:
|
||||
case DatabaseStatus::FAILED_BAD_FORMAT:
|
||||
code = RPC_WALLET_NOT_FOUND;
|
||||
break;
|
||||
case DatabaseStatus::FAILED_ALREADY_LOADED:
|
||||
code = RPC_WALLET_ALREADY_LOADED;
|
||||
break;
|
||||
default: // RPC_WALLET_ERROR is returned for all other cases.
|
||||
break;
|
||||
}
|
||||
throw JSONRPCError(code, error.original);
|
||||
}
|
||||
|
||||
return { wallet, warnings };
|
||||
}
|
||||
|
||||
static RPCHelpMan loadwallet()
|
||||
{
|
||||
return RPCHelpMan{"loadwallet",
|
||||
|
@ -2598,30 +2629,7 @@ static RPCHelpMan loadwallet()
|
|||
WalletContext& context = EnsureWalletContext(request.context);
|
||||
const std::string name(request.params[0].get_str());
|
||||
|
||||
DatabaseOptions options;
|
||||
DatabaseStatus status;
|
||||
options.require_existing = true;
|
||||
bilingual_str error;
|
||||
std::vector<bilingual_str> warnings;
|
||||
std::optional<bool> load_on_start = request.params[1].isNull() ? std::nullopt : std::optional<bool>(request.params[1].get_bool());
|
||||
std::shared_ptr<CWallet> const wallet = LoadWallet(*context.chain, name, load_on_start, options, status, error, warnings);
|
||||
if (!wallet) {
|
||||
// Map bad format to not found, since bad format is returned when the
|
||||
// wallet directory exists, but doesn't contain a data file.
|
||||
RPCErrorCode code = RPC_WALLET_ERROR;
|
||||
switch (status) {
|
||||
case DatabaseStatus::FAILED_NOT_FOUND:
|
||||
case DatabaseStatus::FAILED_BAD_FORMAT:
|
||||
code = RPC_WALLET_NOT_FOUND;
|
||||
break;
|
||||
case DatabaseStatus::FAILED_ALREADY_LOADED:
|
||||
code = RPC_WALLET_ALREADY_LOADED;
|
||||
break;
|
||||
default: // RPC_WALLET_ERROR is returned for all other cases.
|
||||
break;
|
||||
}
|
||||
throw JSONRPCError(code, error.original);
|
||||
}
|
||||
auto [wallet, warnings] = LoadWalletHelper(context, request.params[1], name);
|
||||
|
||||
UniValue obj(UniValue::VOBJ);
|
||||
obj.pushKV("name", wallet->GetName());
|
||||
|
@ -2795,6 +2803,68 @@ static RPCHelpMan createwallet()
|
|||
};
|
||||
}
|
||||
|
||||
static RPCHelpMan restorewallet()
|
||||
{
|
||||
return RPCHelpMan{
|
||||
"restorewallet",
|
||||
"\nRestore and loads a wallet from backup.\n",
|
||||
{
|
||||
{"wallet_name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name that will be applied to the restored wallet"},
|
||||
{"backup_file", RPCArg::Type::STR, RPCArg::Optional::NO, "The backup file that will be used to restore the wallet."},
|
||||
{"load_on_startup", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED_NAMED_ARG, "Save wallet name to persistent settings and load on startup. True to add wallet to startup list, false to remove, null to leave unchanged."},
|
||||
},
|
||||
RPCResult{
|
||||
RPCResult::Type::OBJ, "", "",
|
||||
{
|
||||
{RPCResult::Type::STR, "name", "The wallet name if restored successfully."},
|
||||
{RPCResult::Type::STR, "warning", "Warning message if wallet was not loaded cleanly."},
|
||||
}
|
||||
},
|
||||
RPCExamples{
|
||||
HelpExampleCli("restorewallet", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
|
||||
+ HelpExampleRpc("restorewallet", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
|
||||
+ HelpExampleCliNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak\""}, {"load_on_startup", true}})
|
||||
+ HelpExampleRpcNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak\""}, {"load_on_startup", true}})
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
|
||||
WalletContext& context = EnsureWalletContext(request.context);
|
||||
|
||||
std::string backup_file = request.params[1].get_str();
|
||||
|
||||
if (!fs::exists(backup_file)) {
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Backup file does not exist");
|
||||
}
|
||||
|
||||
std::string wallet_name = request.params[0].get_str();
|
||||
|
||||
const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), wallet_name);
|
||||
|
||||
if (fs::exists(wallet_path)) {
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Wallet name already exists.");
|
||||
}
|
||||
|
||||
if (!TryCreateDirectories(wallet_path)) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Failed to create database path '%s'. Database already exists.", wallet_path.string()));
|
||||
}
|
||||
|
||||
auto wallet_file = wallet_path / "wallet.dat";
|
||||
|
||||
fs::copy_file(backup_file, wallet_file, fs::copy_option::fail_if_exists);
|
||||
|
||||
auto [wallet, warnings] = LoadWalletHelper(context, request.params[2], wallet_name);
|
||||
|
||||
UniValue obj(UniValue::VOBJ);
|
||||
obj.pushKV("name", wallet->GetName());
|
||||
obj.pushKV("warning", Join(warnings, Untranslated("\n")).original);
|
||||
|
||||
return obj;
|
||||
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
static RPCHelpMan unloadwallet()
|
||||
{
|
||||
return RPCHelpMan{"unloadwallet",
|
||||
|
@ -4639,6 +4709,7 @@ static const CRPCCommand commands[] =
|
|||
{ "wallet", &bumpfee, },
|
||||
{ "wallet", &psbtbumpfee, },
|
||||
{ "wallet", &createwallet, },
|
||||
{ "wallet", &restorewallet, },
|
||||
{ "wallet", &dumpprivkey, },
|
||||
{ "wallet", &dumpwallet, },
|
||||
{ "wallet", &encryptwallet, },
|
||||
|
|
Loading…
Add table
Reference in a new issue