Skip to content

Commit

Permalink
Add a new RPC command: restorewalletfrombackup
Browse files Browse the repository at this point in the history
  • Loading branch information
lsilva01 committed Jul 27, 2021
1 parent 979f410 commit 7b1b40a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "createwallet", 5, "descriptors"},
{ "createwallet", 6, "load_on_startup"},
{ "createwallet", 7, "external_signer"},
{ "restorewalletfrombackup", 2, "load_on_startup"},
{ "loadwallet", 1, "load_on_startup"},
{ "unloadwallet", 1, "load_on_startup"},
{ "getnodeaddresses", 0, "count"},
Expand Down
102 changes: 102 additions & 0 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,107 @@ static RPCHelpMan createwallet()
};
}

static RPCHelpMan restorewalletfrombackup()
{
return RPCHelpMan{
"restorewalletfrombackup",
"\nRestore and loads a wallet from backup.\n",
{
{"wallet_name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name for the wallet. If this is a path, the wallet will be restored at the path location."},
{"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. If the wallet was restored using a full path, the wallet_name will be the full path."},
{RPCResult::Type::STR, "warning", "Warning message if wallet was not loaded cleanly."},
}
},
RPCExamples{
HelpExampleCli("restorewalletfrombackup", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
+ HelpExampleRpc("restorewalletfrombackup", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
+ HelpExampleCliNamed("restorewalletfrombackup", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak\""}, {"load_on_startup", true}})
+ HelpExampleRpcNamed("restorewalletfrombackup", {{"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, "Inexistent backup file");
}

std::string wallet_name = request.params[0].get_str();

const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), wallet_name);
fs::file_type path_type = fs::symlink_status(wallet_path).type();

if (!(path_type == fs::file_not_found || path_type == fs::directory_file ||
(path_type == fs::symlink_file && fs::is_directory(wallet_path)) ||
(path_type == fs::regular_file && fs::path(wallet_name).filename() == wallet_name))) {
throw JSONRPCError(RPC_WALLET_ERROR, Untranslated(strprintf(
"Invalid -wallet path '%s'. -wallet path should point to a directory where wallet.dat and "
"database/log.?????????? files can be stored, a location where such a directory could be created, "
"or (for backwards compatibility) the name of an existing data file in -walletdir (%s)",
wallet_name, GetWalletDir())).original);
}

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";

std::ifstream src(backup_file, std::ios::binary);
std::ofstream dst(wallet_file.string(), std::ios::binary);

dst << src.rdbuf();

dst.flush();
dst.close();

DatabaseOptions options;
options.require_existing = true;
DatabaseStatus status;
bilingual_str error;

std::vector<bilingual_str> warnings;

std::optional<bool> load_on_start = request.params[2].isNull() ? std::nullopt : std::optional<bool>(request.params[2].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);
}

UniValue obj(UniValue::VOBJ);
obj.pushKV("wallet_path", wallet->GetName());
obj.pushKV("warning", Join(warnings, Untranslated("\n")).original);

return obj;

},
};
}

static RPCHelpMan unloadwallet()
{
return RPCHelpMan{"unloadwallet",
Expand Down Expand Up @@ -4636,6 +4737,7 @@ static const CRPCCommand commands[] =
{ "wallet", &bumpfee, },
{ "wallet", &psbtbumpfee, },
{ "wallet", &createwallet, },
{ "wallet", &restorewalletfrombackup, },
{ "wallet", &dumpprivkey, },
{ "wallet", &dumpwallet, },
{ "wallet", &encryptwallet, },
Expand Down

0 comments on commit 7b1b40a

Please sign in to comment.