Skip to content

Commit 803b305

Browse files
committed
MOVEONLY: Move backupwallet and restorewallet to rpc/backup.cpp
1 parent 3a9d393 commit 803b305

File tree

3 files changed

+108
-98
lines changed

3 files changed

+108
-98
lines changed

src/wallet/rpc/backup.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <util/system.h>
1818
#include <util/time.h>
1919
#include <util/translation.h>
20+
#include <wallet/rpcwallet.h>
2021
#include <wallet/rpc/util.h>
2122
#include <wallet/wallet.h>
2223

@@ -1831,3 +1832,99 @@ RPCHelpMan listdescriptors()
18311832
},
18321833
};
18331834
}
1835+
1836+
RPCHelpMan backupwallet()
1837+
{
1838+
return RPCHelpMan{"backupwallet",
1839+
"\nSafely copies current wallet file to destination, which can be a directory or a path with filename.\n",
1840+
{
1841+
{"destination", RPCArg::Type::STR, RPCArg::Optional::NO, "The destination directory or file"},
1842+
},
1843+
RPCResult{RPCResult::Type::NONE, "", ""},
1844+
RPCExamples{
1845+
HelpExampleCli("backupwallet", "\"backup.dat\"")
1846+
+ HelpExampleRpc("backupwallet", "\"backup.dat\"")
1847+
},
1848+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
1849+
{
1850+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
1851+
if (!pwallet) return NullUniValue;
1852+
1853+
// Make sure the results are valid at least up to the most recent block
1854+
// the user could have gotten from another RPC command prior to now
1855+
pwallet->BlockUntilSyncedToCurrentChain();
1856+
1857+
LOCK(pwallet->cs_wallet);
1858+
1859+
std::string strDest = request.params[0].get_str();
1860+
if (!pwallet->BackupWallet(strDest)) {
1861+
throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!");
1862+
}
1863+
1864+
return NullUniValue;
1865+
},
1866+
};
1867+
}
1868+
1869+
1870+
RPCHelpMan restorewallet()
1871+
{
1872+
return RPCHelpMan{
1873+
"restorewallet",
1874+
"\nRestore and loads a wallet from backup.\n",
1875+
{
1876+
{"wallet_name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name that will be applied to the restored wallet"},
1877+
{"backup_file", RPCArg::Type::STR, RPCArg::Optional::NO, "The backup file that will be used to restore the wallet."},
1878+
{"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."},
1879+
},
1880+
RPCResult{
1881+
RPCResult::Type::OBJ, "", "",
1882+
{
1883+
{RPCResult::Type::STR, "name", "The wallet name if restored successfully."},
1884+
{RPCResult::Type::STR, "warning", "Warning message if wallet was not loaded cleanly."},
1885+
}
1886+
},
1887+
RPCExamples{
1888+
HelpExampleCli("restorewallet", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
1889+
+ HelpExampleRpc("restorewallet", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
1890+
+ HelpExampleCliNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak\""}, {"load_on_startup", true}})
1891+
+ HelpExampleRpcNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak\""}, {"load_on_startup", true}})
1892+
},
1893+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
1894+
{
1895+
1896+
WalletContext& context = EnsureWalletContext(request.context);
1897+
1898+
auto backup_file = fs::u8path(request.params[1].get_str());
1899+
1900+
if (!fs::exists(backup_file)) {
1901+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Backup file does not exist");
1902+
}
1903+
1904+
std::string wallet_name = request.params[0].get_str();
1905+
1906+
const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), fs::u8path(wallet_name));
1907+
1908+
if (fs::exists(wallet_path)) {
1909+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Wallet name already exists.");
1910+
}
1911+
1912+
if (!TryCreateDirectories(wallet_path)) {
1913+
throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Failed to create database path '%s'. Database already exists.", wallet_path.u8string()));
1914+
}
1915+
1916+
auto wallet_file = wallet_path / "wallet.dat";
1917+
1918+
fs::copy_file(backup_file, wallet_file, fs::copy_option::fail_if_exists);
1919+
1920+
auto [wallet, warnings] = LoadWalletHelper(context, request.params[2], wallet_name);
1921+
1922+
UniValue obj(UniValue::VOBJ);
1923+
obj.pushKV("name", wallet->GetName());
1924+
obj.pushKV("warning", Join(warnings, Untranslated("\n")).original);
1925+
1926+
return obj;
1927+
1928+
},
1929+
};
1930+
}

src/wallet/rpcwallet.cpp

Lines changed: 3 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,41 +1656,6 @@ static RPCHelpMan abandontransaction()
16561656
};
16571657
}
16581658

1659-
1660-
static RPCHelpMan backupwallet()
1661-
{
1662-
return RPCHelpMan{"backupwallet",
1663-
"\nSafely copies current wallet file to destination, which can be a directory or a path with filename.\n",
1664-
{
1665-
{"destination", RPCArg::Type::STR, RPCArg::Optional::NO, "The destination directory or file"},
1666-
},
1667-
RPCResult{RPCResult::Type::NONE, "", ""},
1668-
RPCExamples{
1669-
HelpExampleCli("backupwallet", "\"backup.dat\"")
1670-
+ HelpExampleRpc("backupwallet", "\"backup.dat\"")
1671-
},
1672-
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
1673-
{
1674-
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
1675-
if (!pwallet) return NullUniValue;
1676-
1677-
// Make sure the results are valid at least up to the most recent block
1678-
// the user could have gotten from another RPC command prior to now
1679-
pwallet->BlockUntilSyncedToCurrentChain();
1680-
1681-
LOCK(pwallet->cs_wallet);
1682-
1683-
std::string strDest = request.params[0].get_str();
1684-
if (!pwallet->BackupWallet(strDest)) {
1685-
throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!");
1686-
}
1687-
1688-
return NullUniValue;
1689-
},
1690-
};
1691-
}
1692-
1693-
16941659
static RPCHelpMan keypoolrefill()
16951660
{
16961661
return RPCHelpMan{"keypoolrefill",
@@ -2467,7 +2432,7 @@ static RPCHelpMan listwallets()
24672432
};
24682433
}
24692434

2470-
static std::tuple<std::shared_ptr<CWallet>, std::vector<bilingual_str>> LoadWalletHelper(WalletContext& context, UniValue load_on_start_param, const std::string wallet_name)
2435+
std::tuple<std::shared_ptr<CWallet>, std::vector<bilingual_str>> LoadWalletHelper(WalletContext& context, UniValue load_on_start_param, const std::string wallet_name)
24712436
{
24722437
DatabaseOptions options;
24732438
DatabaseStatus status;
@@ -2697,68 +2662,6 @@ static RPCHelpMan createwallet()
26972662
};
26982663
}
26992664

2700-
static RPCHelpMan restorewallet()
2701-
{
2702-
return RPCHelpMan{
2703-
"restorewallet",
2704-
"\nRestore and loads a wallet from backup.\n",
2705-
{
2706-
{"wallet_name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name that will be applied to the restored wallet"},
2707-
{"backup_file", RPCArg::Type::STR, RPCArg::Optional::NO, "The backup file that will be used to restore the wallet."},
2708-
{"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."},
2709-
},
2710-
RPCResult{
2711-
RPCResult::Type::OBJ, "", "",
2712-
{
2713-
{RPCResult::Type::STR, "name", "The wallet name if restored successfully."},
2714-
{RPCResult::Type::STR, "warning", "Warning message if wallet was not loaded cleanly."},
2715-
}
2716-
},
2717-
RPCExamples{
2718-
HelpExampleCli("restorewallet", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
2719-
+ HelpExampleRpc("restorewallet", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
2720-
+ HelpExampleCliNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak\""}, {"load_on_startup", true}})
2721-
+ HelpExampleRpcNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak\""}, {"load_on_startup", true}})
2722-
},
2723-
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
2724-
{
2725-
2726-
WalletContext& context = EnsureWalletContext(request.context);
2727-
2728-
auto backup_file = fs::u8path(request.params[1].get_str());
2729-
2730-
if (!fs::exists(backup_file)) {
2731-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Backup file does not exist");
2732-
}
2733-
2734-
std::string wallet_name = request.params[0].get_str();
2735-
2736-
const fs::path wallet_path = fsbridge::AbsPathJoin(GetWalletDir(), fs::u8path(wallet_name));
2737-
2738-
if (fs::exists(wallet_path)) {
2739-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Wallet name already exists.");
2740-
}
2741-
2742-
if (!TryCreateDirectories(wallet_path)) {
2743-
throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Failed to create database path '%s'. Database already exists.", wallet_path.u8string()));
2744-
}
2745-
2746-
auto wallet_file = wallet_path / "wallet.dat";
2747-
2748-
fs::copy_file(backup_file, wallet_file, fs::copy_option::fail_if_exists);
2749-
2750-
auto [wallet, warnings] = LoadWalletHelper(context, request.params[2], wallet_name);
2751-
2752-
UniValue obj(UniValue::VOBJ);
2753-
obj.pushKV("name", wallet->GetName());
2754-
obj.pushKV("warning", Join(warnings, Untranslated("\n")).original);
2755-
2756-
return obj;
2757-
2758-
},
2759-
};
2760-
}
2761-
27622665
static RPCHelpMan unloadwallet()
27632666
{
27642667
return RPCHelpMan{"unloadwallet",
@@ -4699,6 +4602,8 @@ RPCHelpMan importmulti();
46994602
RPCHelpMan importdescriptors();
47004603
RPCHelpMan listdescriptors();
47014604
RPCHelpMan signmessage();
4605+
RPCHelpMan backupwallet();
4606+
RPCHelpMan restorewallet();
47024607

47034608
Span<const CRPCCommand> GetWalletRPCCommands()
47044609
{

src/wallet/rpcwallet.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@
77

88
#include <span.h>
99

10+
#include <memory>
11+
#include <string>
12+
#include <vector>
13+
1014
class CRPCCommand;
15+
class CWallet;
16+
struct WalletContext;
1117

1218
Span<const CRPCCommand> GetWalletRPCCommands();
1319

20+
std::tuple<std::shared_ptr<CWallet>, std::vector<bilingual_str>> LoadWalletHelper(WalletContext& context, UniValue load_on_start_param, const std::string wallet_name);
21+
1422
RPCHelpMan getaddressinfo();
1523
RPCHelpMan signrawtransactionwithwallet();
1624
#endif // BITCOIN_WALLET_RPCWALLET_H

0 commit comments

Comments
 (0)