Skip to content

Commit 8b4d53e

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23647: MOVEONLY: Move wallet backup and encryption RPCs out of rpcwallet
5b2167f MOVEONLY: Move LoadWalletHelper to wallet/rpc/util (Samuel Dobson) 8b73640 MOVEONLY: Move wallet encryption RPCs to encrypt.cpp (Samuel Dobson) 803b305 MOVEONLY: Move backupwallet and restorewallet to rpc/backup.cpp (Samuel Dobson) 3a9d393 MOVEONLY: Move rpcdump.cpp to wallet/rpc/backup.cpp (Samuel Dobson) Pull request description: As part of an effort to split rpcwallet as per #23622, this moves `rpcdump.cpp` into the new wallet/rpc directory as well as moving backup and encryption RPCs out of rpcwallet. ACKs for top commit: MarcoFalke: ACK 5b2167f 🎭 Tree-SHA512: aa8054767927fa56b5c51edc91a2d94fe9f1cca198e1b2cac1ebd464f6956a89c782a7b6de4409361adca6ca1377272b6e2af660b737c4849ee323f899945ad9
2 parents 927a9b0 + 5b2167f commit 8b4d53e

File tree

6 files changed

+390
-370
lines changed

6 files changed

+390
-370
lines changed

src/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,10 @@ libbitcoin_wallet_a_SOURCES = \
410410
wallet/interfaces.cpp \
411411
wallet/load.cpp \
412412
wallet/receive.cpp \
413+
wallet/rpc/backup.cpp \
414+
wallet/rpc/encrypt.cpp \
413415
wallet/rpc/signmessage.cpp \
414416
wallet/rpc/util.cpp \
415-
wallet/rpcdump.cpp \
416417
wallet/rpcwallet.cpp \
417418
wallet/scriptpubkeyman.cpp \
418419
wallet/spend.cpp \

src/wallet/rpcdump.cpp renamed to src/wallet/rpc/backup.cpp

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

src/wallet/rpc/encrypt.cpp

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
// Copyright (c) 2011-2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <rpc/util.h>
6+
#include <wallet/rpc/util.h>
7+
#include <wallet/wallet.h>
8+
9+
10+
RPCHelpMan walletpassphrase()
11+
{
12+
return RPCHelpMan{"walletpassphrase",
13+
"\nStores the wallet decryption key in memory for 'timeout' seconds.\n"
14+
"This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
15+
"\nNote:\n"
16+
"Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
17+
"time that overrides the old one.\n",
18+
{
19+
{"passphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The wallet passphrase"},
20+
{"timeout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The time to keep the decryption key in seconds; capped at 100000000 (~3 years)."},
21+
},
22+
RPCResult{RPCResult::Type::NONE, "", ""},
23+
RPCExamples{
24+
"\nUnlock the wallet for 60 seconds\n"
25+
+ HelpExampleCli("walletpassphrase", "\"my pass phrase\" 60") +
26+
"\nLock the wallet again (before 60 seconds)\n"
27+
+ HelpExampleCli("walletlock", "") +
28+
"\nAs a JSON-RPC call\n"
29+
+ HelpExampleRpc("walletpassphrase", "\"my pass phrase\", 60")
30+
},
31+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
32+
{
33+
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
34+
if (!wallet) return NullUniValue;
35+
CWallet* const pwallet = wallet.get();
36+
37+
int64_t nSleepTime;
38+
int64_t relock_time;
39+
// Prevent concurrent calls to walletpassphrase with the same wallet.
40+
LOCK(pwallet->m_unlock_mutex);
41+
{
42+
LOCK(pwallet->cs_wallet);
43+
44+
if (!pwallet->IsCrypted()) {
45+
throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrase was called.");
46+
}
47+
48+
// Note that the walletpassphrase is stored in request.params[0] which is not mlock()ed
49+
SecureString strWalletPass;
50+
strWalletPass.reserve(100);
51+
// TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
52+
// Alternately, find a way to make request.params[0] mlock()'d to begin with.
53+
strWalletPass = request.params[0].get_str().c_str();
54+
55+
// Get the timeout
56+
nSleepTime = request.params[1].get_int64();
57+
// Timeout cannot be negative, otherwise it will relock immediately
58+
if (nSleepTime < 0) {
59+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Timeout cannot be negative.");
60+
}
61+
// Clamp timeout
62+
constexpr int64_t MAX_SLEEP_TIME = 100000000; // larger values trigger a macos/libevent bug?
63+
if (nSleepTime > MAX_SLEEP_TIME) {
64+
nSleepTime = MAX_SLEEP_TIME;
65+
}
66+
67+
if (strWalletPass.empty()) {
68+
throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty");
69+
}
70+
71+
if (!pwallet->Unlock(strWalletPass)) {
72+
throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
73+
}
74+
75+
pwallet->TopUpKeyPool();
76+
77+
pwallet->nRelockTime = GetTime() + nSleepTime;
78+
relock_time = pwallet->nRelockTime;
79+
}
80+
81+
// rpcRunLater must be called without cs_wallet held otherwise a deadlock
82+
// can occur. The deadlock would happen when RPCRunLater removes the
83+
// previous timer (and waits for the callback to finish if already running)
84+
// and the callback locks cs_wallet.
85+
AssertLockNotHeld(wallet->cs_wallet);
86+
// Keep a weak pointer to the wallet so that it is possible to unload the
87+
// wallet before the following callback is called. If a valid shared pointer
88+
// is acquired in the callback then the wallet is still loaded.
89+
std::weak_ptr<CWallet> weak_wallet = wallet;
90+
pwallet->chain().rpcRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), [weak_wallet, relock_time] {
91+
if (auto shared_wallet = weak_wallet.lock()) {
92+
LOCK(shared_wallet->cs_wallet);
93+
// Skip if this is not the most recent rpcRunLater callback.
94+
if (shared_wallet->nRelockTime != relock_time) return;
95+
shared_wallet->Lock();
96+
shared_wallet->nRelockTime = 0;
97+
}
98+
}, nSleepTime);
99+
100+
return NullUniValue;
101+
},
102+
};
103+
}
104+
105+
106+
RPCHelpMan walletpassphrasechange()
107+
{
108+
return RPCHelpMan{"walletpassphrasechange",
109+
"\nChanges the wallet passphrase from 'oldpassphrase' to 'newpassphrase'.\n",
110+
{
111+
{"oldpassphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The current passphrase"},
112+
{"newpassphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The new passphrase"},
113+
},
114+
RPCResult{RPCResult::Type::NONE, "", ""},
115+
RPCExamples{
116+
HelpExampleCli("walletpassphrasechange", "\"old one\" \"new one\"")
117+
+ HelpExampleRpc("walletpassphrasechange", "\"old one\", \"new one\"")
118+
},
119+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
120+
{
121+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
122+
if (!pwallet) return NullUniValue;
123+
124+
LOCK(pwallet->cs_wallet);
125+
126+
if (!pwallet->IsCrypted()) {
127+
throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrasechange was called.");
128+
}
129+
130+
// TODO: get rid of these .c_str() calls by implementing SecureString::operator=(std::string)
131+
// Alternately, find a way to make request.params[0] mlock()'d to begin with.
132+
SecureString strOldWalletPass;
133+
strOldWalletPass.reserve(100);
134+
strOldWalletPass = request.params[0].get_str().c_str();
135+
136+
SecureString strNewWalletPass;
137+
strNewWalletPass.reserve(100);
138+
strNewWalletPass = request.params[1].get_str().c_str();
139+
140+
if (strOldWalletPass.empty() || strNewWalletPass.empty()) {
141+
throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty");
142+
}
143+
144+
if (!pwallet->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass)) {
145+
throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
146+
}
147+
148+
return NullUniValue;
149+
},
150+
};
151+
}
152+
153+
154+
RPCHelpMan walletlock()
155+
{
156+
return RPCHelpMan{"walletlock",
157+
"\nRemoves the wallet encryption key from memory, locking the wallet.\n"
158+
"After calling this method, you will need to call walletpassphrase again\n"
159+
"before being able to call any methods which require the wallet to be unlocked.\n",
160+
{},
161+
RPCResult{RPCResult::Type::NONE, "", ""},
162+
RPCExamples{
163+
"\nSet the passphrase for 2 minutes to perform a transaction\n"
164+
+ HelpExampleCli("walletpassphrase", "\"my pass phrase\" 120") +
165+
"\nPerform a send (requires passphrase set)\n"
166+
+ HelpExampleCli("sendtoaddress", "\"" + EXAMPLE_ADDRESS[0] + "\" 1.0") +
167+
"\nClear the passphrase since we are done before 2 minutes is up\n"
168+
+ HelpExampleCli("walletlock", "") +
169+
"\nAs a JSON-RPC call\n"
170+
+ HelpExampleRpc("walletlock", "")
171+
},
172+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
173+
{
174+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
175+
if (!pwallet) return NullUniValue;
176+
177+
LOCK(pwallet->cs_wallet);
178+
179+
if (!pwallet->IsCrypted()) {
180+
throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletlock was called.");
181+
}
182+
183+
pwallet->Lock();
184+
pwallet->nRelockTime = 0;
185+
186+
return NullUniValue;
187+
},
188+
};
189+
}
190+
191+
192+
RPCHelpMan encryptwallet()
193+
{
194+
return RPCHelpMan{"encryptwallet",
195+
"\nEncrypts the wallet with 'passphrase'. This is for first time encryption.\n"
196+
"After this, any calls that interact with private keys such as sending or signing \n"
197+
"will require the passphrase to be set prior the making these calls.\n"
198+
"Use the walletpassphrase call for this, and then walletlock call.\n"
199+
"If the wallet is already encrypted, use the walletpassphrasechange call.\n",
200+
{
201+
{"passphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The pass phrase to encrypt the wallet with. It must be at least 1 character, but should be long."},
202+
},
203+
RPCResult{RPCResult::Type::STR, "", "A string with further instructions"},
204+
RPCExamples{
205+
"\nEncrypt your wallet\n"
206+
+ HelpExampleCli("encryptwallet", "\"my pass phrase\"") +
207+
"\nNow set the passphrase to use the wallet, such as for signing or sending bitcoin\n"
208+
+ HelpExampleCli("walletpassphrase", "\"my pass phrase\"") +
209+
"\nNow we can do something like sign\n"
210+
+ HelpExampleCli("signmessage", "\"address\" \"test message\"") +
211+
"\nNow lock the wallet again by removing the passphrase\n"
212+
+ HelpExampleCli("walletlock", "") +
213+
"\nAs a JSON-RPC call\n"
214+
+ HelpExampleRpc("encryptwallet", "\"my pass phrase\"")
215+
},
216+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
217+
{
218+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
219+
if (!pwallet) return NullUniValue;
220+
221+
LOCK(pwallet->cs_wallet);
222+
223+
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
224+
throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: wallet does not contain private keys, nothing to encrypt.");
225+
}
226+
227+
if (pwallet->IsCrypted()) {
228+
throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an encrypted wallet, but encryptwallet was called.");
229+
}
230+
231+
// TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
232+
// Alternately, find a way to make request.params[0] mlock()'d to begin with.
233+
SecureString strWalletPass;
234+
strWalletPass.reserve(100);
235+
strWalletPass = request.params[0].get_str().c_str();
236+
237+
if (strWalletPass.empty()) {
238+
throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty");
239+
}
240+
241+
if (!pwallet->EncryptWallet(strWalletPass)) {
242+
throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: Failed to encrypt the wallet.");
243+
}
244+
245+
return "wallet encrypted; The keypool has been flushed and a new HD seed was generated (if you are using HD). You need to make a new backup.";
246+
},
247+
};
248+
}

0 commit comments

Comments
 (0)