Branch data Line data Source code
1 : : // Copyright (c) 2009-present 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 <chain.h>
6 : : #include <clientversion.h>
7 : : #include <core_io.h>
8 : : #include <hash.h>
9 : : #include <interfaces/chain.h>
10 : : #include <key_io.h>
11 : : #include <merkleblock.h>
12 : : #include <node/types.h>
13 : : #include <rpc/util.h>
14 : : #include <script/descriptor.h>
15 : : #include <script/script.h>
16 : : #include <script/solver.h>
17 : : #include <sync.h>
18 : : #include <uint256.h>
19 : : #include <util/bip32.h>
20 : : #include <util/check.h>
21 : : #include <util/fs.h>
22 : : #include <util/time.h>
23 : : #include <util/translation.h>
24 : : #include <wallet/rpc/util.h>
25 : : #include <wallet/wallet.h>
26 : :
27 : : #include <cstdint>
28 : : #include <fstream>
29 : : #include <tuple>
30 : : #include <string>
31 : :
32 : : #include <univalue.h>
33 : :
34 : :
35 : :
36 : : using interfaces::FoundBlock;
37 : :
38 : : namespace wallet {
39 : 836 : RPCHelpMan importprunedfunds()
40 : : {
41 : 836 : return RPCHelpMan{
42 : 836 : "importprunedfunds",
43 [ + - ]: 1672 : "Imports funds without rescan. Corresponding address or script must previously be included in wallet. Aimed towards pruned wallets. The end-user is responsible to import additional transactions that subsequently spend the imported outputs or rescan after the point in the blockchain the transaction is included.\n",
44 : : {
45 [ + - + - ]: 1672 : {"rawtransaction", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "A raw transaction in hex funding an already-existing address in wallet"},
46 [ + - + - ]: 1672 : {"txoutproof", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex output from gettxoutproof that contains the transaction"},
47 : : },
48 [ + - + - : 1672 : RPCResult{RPCResult::Type::NONE, "", ""},
+ - + - ]
49 [ + - + - ]: 2508 : RPCExamples{""},
50 : 836 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
51 : : {
52 : 7 : std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
53 [ - + ]: 7 : if (!pwallet) return UniValue::VNULL;
54 : :
55 [ + - ]: 7 : CMutableTransaction tx;
56 [ + - + - : 7 : if (!DecodeHexTx(tx, request.params[0].get_str())) {
+ - + + ]
57 [ + - + - ]: 2 : throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed. Make sure the tx has at least one input.");
58 : : }
59 : :
60 [ + - + - : 12 : DataStream ssMB{ParseHexV(request.params[1], "proof")};
+ - ]
61 [ + - ]: 6 : CMerkleBlock merkleBlock;
62 [ + - ]: 6 : ssMB >> merkleBlock;
63 : :
64 : : //Search partial merkle tree in proof for our transaction and index in valid block
65 : 6 : std::vector<Txid> vMatch;
66 : 6 : std::vector<unsigned int> vIndex;
67 [ + - + + ]: 6 : if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot) {
68 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Something wrong with merkleblock");
69 : : }
70 : :
71 [ + - ]: 5 : LOCK(pwallet->cs_wallet);
72 : 5 : int height;
73 [ + - + - : 5 : if (!pwallet->chain().findAncestorByHash(pwallet->GetLastBlockHash(), merkleBlock.header.GetHash(), FoundBlock().height(height))) {
+ + ]
74 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
75 : : }
76 : :
77 : 4 : std::vector<Txid>::const_iterator it;
78 [ + - + + ]: 4 : if ((it = std::find(vMatch.begin(), vMatch.end(), tx.GetHash())) == vMatch.end()) {
79 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction given doesn't exist in proof");
80 : : }
81 : :
82 [ + - ]: 3 : unsigned int txnIndex = vIndex[it - vMatch.begin()];
83 : :
84 [ + - ]: 3 : CTransactionRef tx_ref = MakeTransactionRef(tx);
85 [ + - + + ]: 3 : if (pwallet->IsMine(*tx_ref)) {
86 [ + - + - ]: 4 : pwallet->AddToWallet(std::move(tx_ref), TxStateConfirmed{merkleBlock.header.GetHash(), height, static_cast<int>(txnIndex)});
87 [ - + ]: 2 : return UniValue::VNULL;
88 : : }
89 : :
90 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No addresses in wallet correspond to included transaction");
91 [ + - ]: 29 : },
92 [ + - + - : 5016 : };
+ + - - ]
93 [ + - + - : 3344 : }
- - ]
94 : :
95 : 832 : RPCHelpMan removeprunedfunds()
96 : : {
97 : 832 : return RPCHelpMan{
98 : 832 : "removeprunedfunds",
99 [ + - ]: 1664 : "Deletes the specified transaction from the wallet. Meant for use with pruned wallets and as a companion to importprunedfunds. This will affect wallet balances.\n",
100 : : {
101 [ + - + - ]: 1664 : {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex-encoded id of the transaction you are deleting"},
102 : : },
103 [ + - + - : 1664 : RPCResult{RPCResult::Type::NONE, "", ""},
+ - + - ]
104 : 832 : RPCExamples{
105 [ + - + - : 1664 : HelpExampleCli("removeprunedfunds", "\"a8d0c0184dde994a09ec054286f1ce581bebf46446a512166eae7628734ea0a5\"") +
+ - ]
106 : 832 : "\nAs a JSON-RPC call\n"
107 [ + - + - : 3328 : + HelpExampleRpc("removeprunedfunds", "\"a8d0c0184dde994a09ec054286f1ce581bebf46446a512166eae7628734ea0a5\"")
+ - + - ]
108 [ + - ]: 832 : },
109 : 832 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
110 : : {
111 : 3 : std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
112 [ - + ]: 3 : if (!pwallet) return UniValue::VNULL;
113 : :
114 [ + - ]: 3 : LOCK(pwallet->cs_wallet);
115 : :
116 [ + - + - ]: 3 : Txid hash{Txid::FromUint256(ParseHashV(request.params[0], "txid"))};
117 : 3 : std::vector<Txid> vHash;
118 [ + - ]: 3 : vHash.push_back(hash);
119 [ + - + + ]: 3 : if (auto res = pwallet->RemoveTxs(vHash); !res) {
120 [ + - + - ]: 2 : throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(res).original);
121 : 1 : }
122 : :
123 : 2 : return UniValue::VNULL;
124 [ + - ]: 8 : },
125 [ + - + - : 4160 : };
+ + - - ]
126 [ + - ]: 1664 : }
127 : :
128 : 723 : static int64_t GetImportTimestamp(const UniValue& data, int64_t now)
129 : : {
130 [ + - ]: 1446 : if (data.exists("timestamp")) {
131 [ + - ]: 723 : const UniValue& timestamp = data["timestamp"];
132 [ + + ]: 723 : if (timestamp.isNum()) {
133 : 216 : return timestamp.getInt<int64_t>();
134 [ + - - + ]: 507 : } else if (timestamp.isStr() && timestamp.get_str() == "now") {
135 : : return now;
136 : : }
137 [ # # # # : 0 : throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Expected number or \"now\" timestamp value for key. got type %s", uvTypeName(timestamp.type())));
# # ]
138 : : }
139 [ # # # # ]: 0 : throw JSONRPCError(RPC_TYPE_ERROR, "Missing required timestamp field for key");
140 : : }
141 : :
142 : 721 : static UniValue ProcessDescriptorImport(CWallet& wallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
143 : : {
144 : 721 : UniValue warnings(UniValue::VARR);
145 : 721 : UniValue result(UniValue::VOBJ);
146 : :
147 : 721 : try {
148 [ + - + + ]: 1442 : if (!data.exists("desc")) {
149 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor not found.");
150 : : }
151 : :
152 [ + - + - : 720 : const std::string& descriptor = data["desc"].get_str();
+ - ]
153 [ + - + + : 1756 : const bool active = data.exists("active") ? data["active"].get_bool() : false;
+ - + - +
- ]
154 [ + - + - : 721 : const std::string label{LabelFromValue(data["label"])};
+ + ]
155 : :
156 : : // Parse descriptor string
157 : 719 : FlatSigningProvider keys;
158 [ - + ]: 719 : std::string error;
159 [ - + + - ]: 719 : auto parsed_descs = Parse(descriptor, keys, error, /* require_checksum = */ true);
160 [ + + ]: 719 : if (parsed_descs.empty()) {
161 [ + - ]: 8 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
162 : : }
163 : 711 : std::optional<bool> internal;
164 [ + - + + ]: 1422 : if (data.exists("internal")) {
165 [ - + + + ]: 112 : if (parsed_descs.size() > 1) {
166 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot have multipath descriptor while also specifying \'internal\'");
167 : : }
168 [ + - + - : 111 : internal = data["internal"].get_bool();
+ - ]
169 : : }
170 : :
171 : : // Range check
172 : 710 : std::optional<bool> is_ranged;
173 : 710 : int64_t range_start = 0, range_end = 1, next_index = 0;
174 [ + - + - : 974 : if (!parsed_descs.at(0)->IsRange() && data.exists("range")) {
+ + + - +
- + + +
+ ]
175 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for an un-ranged descriptor");
176 [ + - + - : 709 : } else if (parsed_descs.at(0)->IsRange()) {
+ + ]
177 [ + - + + ]: 892 : if (data.exists("range")) {
178 [ + - + - : 120 : auto range = ParseDescriptorRange(data["range"]);
+ + ]
179 : 110 : range_start = range.first;
180 : 110 : range_end = range.second + 1; // Specified range end is inclusive, but we need range end as exclusive
181 : : } else {
182 [ + - + - ]: 331 : warnings.push_back("Range not given, using default keypool range");
183 : 331 : range_start = 0;
184 : 331 : range_end = wallet.m_keypool_size;
185 : : }
186 : 441 : next_index = range_start;
187 : 441 : is_ranged = true;
188 : :
189 [ + - ]: 882 : if (data.exists("next_index")) {
190 [ + - + - : 71 : next_index = data["next_index"].getInt<int64_t>();
+ - ]
191 : : // bound checks
192 [ - + ]: 71 : if (next_index < range_start || next_index >= range_end) {
193 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "next_index is out of range");
194 : : }
195 : : }
196 : : }
197 : :
198 : : // Active descriptors must be ranged
199 [ + + + - : 1001 : if (active && !parsed_descs.at(0)->IsRange()) {
+ + ]
200 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Active descriptors must be ranged");
201 : : }
202 : :
203 : : // Multipath descriptors should not have a label
204 [ - + + + : 757 : if (parsed_descs.size() > 1 && data.exists("label")) {
+ - + - +
+ + + ]
205 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Multipath descriptors should not have a label");
206 : : }
207 : :
208 : : // Ranged descriptors should not have a label
209 [ + + + - : 1142 : if (is_ranged.has_value() && is_ranged.value() && data.exists("label")) {
+ - + - +
+ + + ]
210 [ + - + - ]: 4 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Ranged descriptors should not have a label");
211 : : }
212 : :
213 [ + + + + : 793 : bool desc_internal = internal.has_value() && internal.value();
+ + ]
214 : : // Internal addresses should not have a label either
215 [ + - + - : 793 : if (desc_internal && data.exists("label")) {
+ + + + ]
216 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Internal addresses should not have a label");
217 : : }
218 : :
219 : : // Combo descriptor check
220 [ + + + - : 993 : if (active && !parsed_descs.at(0)->IsSingleType()) {
+ + ]
221 [ + - + - ]: 2 : throw JSONRPCError(RPC_WALLET_ERROR, "Combo descriptors cannot be set to active");
222 : : }
223 : :
224 : : // If the wallet disabled private keys, abort if private keys exist
225 [ + - + + : 698 : if (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !keys.keys.empty()) {
+ + ]
226 [ + - + - ]: 6 : throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
227 : : }
228 : :
229 [ - + + + ]: 1439 : for (size_t j = 0; j < parsed_descs.size(); ++j) {
230 [ - + ]: 751 : auto parsed_desc = std::move(parsed_descs[j]);
231 [ - + + + ]: 751 : if (parsed_descs.size() == 2) {
232 : 100 : desc_internal = j == 1;
233 [ + + ]: 651 : } else if (parsed_descs.size() > 2) {
234 [ + - ]: 9 : CHECK_NONFATAL(!desc_internal);
235 : : }
236 : : // Need to ExpandPrivate to check if private keys are available for all pubkeys
237 : 751 : FlatSigningProvider expand_keys;
238 : 751 : std::vector<CScript> scripts;
239 [ + - + + ]: 751 : if (!parsed_desc->Expand(0, keys, scripts, expand_keys)) {
240 [ + - + - ]: 2 : throw JSONRPCError(RPC_WALLET_ERROR, "Cannot expand descriptor. Probably because of hardened derivations without private keys provided");
241 : : }
242 [ + - ]: 750 : parsed_desc->ExpandPrivate(0, keys, expand_keys);
243 : :
244 [ + - + + ]: 752 : for (const auto& w : parsed_desc->Warnings()) {
245 [ + - + - ]: 2 : warnings.push_back(w);
246 : 750 : }
247 : :
248 : : // Check if all private keys are provided
249 : 750 : bool have_all_privkeys = !expand_keys.keys.empty();
250 [ + + ]: 1200 : for (const auto& entry : expand_keys.origins) {
251 : 830 : const CKeyID& key_id = entry.first;
252 : 830 : CKey key;
253 [ + - + + ]: 830 : if (!expand_keys.GetKey(key_id, key)) {
254 : 380 : have_all_privkeys = false;
255 : 380 : break;
256 : : }
257 : 830 : }
258 : :
259 : : // If private keys are enabled, check some things.
260 [ + - + + ]: 750 : if (!wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
261 [ + + ]: 560 : if (keys.keys.empty()) {
262 [ + - + - ]: 6 : throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import descriptor without private keys to a wallet with private keys enabled");
263 : : }
264 [ + + ]: 557 : if (!have_all_privkeys) {
265 [ + - + - ]: 198 : warnings.push_back("Not all private keys provided. Some wallet functionality may return unexpected errors");
266 : : }
267 : : }
268 : :
269 [ + - + - ]: 747 : WalletDescriptor w_desc(std::move(parsed_desc), timestamp, range_start, range_end, next_index);
270 : :
271 : : // Add descriptor to the wallet
272 [ + - ]: 747 : auto spk_manager_res = wallet.AddWalletDescriptor(w_desc, keys, label, desc_internal);
273 : :
274 [ + + ]: 747 : if (!spk_manager_res) {
275 [ + - + - : 9 : throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Could not add descriptor '%s': %s", descriptor, util::ErrorString(spk_manager_res).original));
+ - ]
276 : : }
277 : :
278 [ + + ]: 744 : auto& spk_manager = spk_manager_res.value().get();
279 : :
280 : : // Set descriptor as active if necessary
281 [ + + ]: 744 : if (active) {
282 [ + - + + ]: 346 : if (!w_desc.descriptor->GetOutputType()) {
283 [ + - + - ]: 1 : warnings.push_back("Unknown output type, cannot set descriptor to active.");
284 : : } else {
285 [ + - + - : 345 : wallet.AddActiveScriptPubKeyMan(spk_manager.GetID(), *w_desc.descriptor->GetOutputType(), desc_internal);
+ - ]
286 : : }
287 : : } else {
288 [ + - + + ]: 398 : if (w_desc.descriptor->GetOutputType()) {
289 [ + - + - : 224 : wallet.DeactivateScriptPubKeyMan(spk_manager.GetID(), *w_desc.descriptor->GetOutputType(), desc_internal);
+ - ]
290 : : }
291 : : }
292 : 768 : }
293 : :
294 [ + - + - : 1376 : result.pushKV("success", UniValue(true));
+ - ]
295 [ - + ]: 783 : } catch (const UniValue& e) {
296 [ + - + - : 66 : result.pushKV("success", UniValue(false));
+ - ]
297 [ + - + - : 66 : result.pushKV("error", e);
+ - ]
298 : 33 : }
299 [ + - ]: 721 : PushWarnings(warnings, result);
300 : 721 : return result;
301 : 721 : }
302 : :
303 : 1464 : RPCHelpMan importdescriptors()
304 : : {
305 : 1464 : return RPCHelpMan{
306 : 1464 : "importdescriptors",
307 [ + - ]: 2928 : "Import descriptors. This will trigger a rescan of the blockchain based on the earliest timestamp of all descriptors being imported. Requires a new wallet backup.\n"
308 : : "When importing descriptors with multipath key expressions, if the multipath specifier contains exactly two elements, the descriptor produced from the second element will be imported as an internal descriptor.\n"
309 : : "\nNote: This call can take over an hour to complete if using an early timestamp; during that time, other rpc calls\n"
310 : : "may report that the imported keys, addresses or scripts exist but related transactions are still missing.\n"
311 : : "The rescan is significantly faster if block filters are available (using startup option \"-blockfilterindex=1\").\n",
312 : : {
313 [ + - + - ]: 2928 : {"requests", RPCArg::Type::ARR, RPCArg::Optional::NO, "Data to be imported",
314 : : {
315 [ + - + - ]: 2928 : {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
316 : : {
317 [ + - + - ]: 2928 : {"desc", RPCArg::Type::STR, RPCArg::Optional::NO, "Descriptor to import."},
318 [ + - + - : 4392 : {"active", RPCArg::Type::BOOL, RPCArg::Default{false}, "Set this descriptor to be the active descriptor for the corresponding output type/externality"},
+ - ]
319 [ + - + - ]: 2928 : {"range", RPCArg::Type::RANGE, RPCArg::Optional::OMITTED, "If a ranged descriptor is used, this specifies the end or the range (in the form [begin,end]) to import"},
320 [ + - + - ]: 2928 : {"next_index", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "If a ranged descriptor is set to active, this specifies the next index to generate addresses from"},
321 [ + - + - ]: 2928 : {"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, "Time from which to start rescanning the blockchain for this descriptor, in " + UNIX_EPOCH_TIME + "\n"
322 : : "Use the string \"now\" to substitute the current synced blockchain time.\n"
323 : : "\"now\" can be specified to bypass scanning, for outputs which are known to never have been used, and\n"
324 : : "0 can be specified to scan the entire blockchain. Blocks up to 2 hours before the earliest timestamp\n"
325 : 1464 : "of all descriptors being imported will be scanned as well as the mempool.",
326 [ + - ]: 2928 : RPCArgOptions{.type_str={"timestamp | \"now\"", "integer / string"}}
327 : : },
328 [ + - + - : 4392 : {"internal", RPCArg::Type::BOOL, RPCArg::Default{false}, "Whether matching outputs should be treated as not incoming payments (e.g. change)"},
+ - ]
329 [ + - + - : 4392 : {"label", RPCArg::Type::STR, RPCArg::Default{""}, "Label to assign to the address, only allowed with internal=false. Disabled for ranged descriptors"},
+ - ]
330 : : },
331 : : },
332 : : },
333 [ + - ]: 1464 : RPCArgOptions{.oneline_description="requests"}},
334 : : },
335 [ + - ]: 2928 : RPCResult{
336 [ + - + - ]: 2928 : RPCResult::Type::ARR, "", "Response is an array with the same size as the input that has the execution result",
337 : : {
338 [ + - + - ]: 2928 : {RPCResult::Type::OBJ, "", "",
339 : : {
340 [ + - + - ]: 2928 : {RPCResult::Type::BOOL, "success", ""},
341 [ + - + - ]: 2928 : {RPCResult::Type::ARR, "warnings", /*optional=*/true, "",
342 : : {
343 [ + - + - ]: 2928 : {RPCResult::Type::STR, "", ""},
344 : : }},
345 [ + - + - ]: 2928 : {RPCResult::Type::OBJ, "error", /*optional=*/true, "",
346 : : {
347 [ + - + - ]: 2928 : {RPCResult::Type::ELISION, "", "JSONRPC error"},
348 : : }},
349 : : }},
350 : : }
351 [ + - + - : 19032 : },
+ - + - +
- + + + +
+ + + + -
- - - - -
- - ]
352 : 1464 : RPCExamples{
353 [ + - + - : 2928 : HelpExampleCli("importdescriptors", "'[{ \"desc\": \"<my descriptor>\", \"timestamp\":1455191478, \"internal\": true }, "
+ - ]
354 : 1464 : "{ \"desc\": \"<my descriptor 2>\", \"label\": \"example 2\", \"timestamp\": 1455191480 }]'") +
355 [ + - + - : 4392 : HelpExampleCli("importdescriptors", "'[{ \"desc\": \"<my descriptor>\", \"timestamp\":1455191478, \"active\": true, \"range\": [0,100], \"label\": \"<my bech32 wallet>\" }]'")
+ - + - ]
356 [ + - ]: 1464 : },
357 : 1464 : [&](const RPCHelpMan& self, const JSONRPCRequest& main_request) -> UniValue
358 : : {
359 : 635 : std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(main_request);
360 [ - + ]: 635 : if (!pwallet) return UniValue::VNULL;
361 [ + - ]: 635 : CWallet& wallet{*pwallet};
362 : :
363 : : // Make sure the results are valid at least up to the most recent block
364 : : // the user could have gotten from another RPC command prior to now
365 [ + - ]: 635 : wallet.BlockUntilSyncedToCurrentChain();
366 : :
367 : 635 : WalletRescanReserver reserver(*pwallet);
368 [ - + ]: 635 : if (!reserver.reserve(/*with_passphrase=*/true)) {
369 [ # # # # ]: 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
370 : : }
371 : :
372 : : // Ensure that the wallet is not locked for the remainder of this RPC, as
373 : : // the passphrase is used to top up the keypool.
374 [ + - ]: 635 : LOCK(pwallet->m_relock_mutex);
375 : :
376 [ + - ]: 635 : const UniValue& requests = main_request.params[0];
377 : 635 : const int64_t minimum_timestamp = 1;
378 : 635 : int64_t now = 0;
379 : 635 : int64_t lowest_timestamp = 0;
380 : 635 : bool rescan = false;
381 : 635 : UniValue response(UniValue::VARR);
382 : 635 : {
383 [ + - ]: 635 : LOCK(pwallet->cs_wallet);
384 [ + - ]: 635 : EnsureWalletIsUnlocked(*pwallet);
385 : :
386 [ + - + - ]: 635 : CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(lowest_timestamp).mtpTime(now)));
387 : :
388 : : // Get all timestamps and extract the lowest timestamp
389 [ + - + + ]: 1356 : for (const UniValue& request : requests.getValues()) {
390 : : // This throws an error if "timestamp" doesn't exist
391 [ + - + + ]: 721 : const int64_t timestamp = std::max(GetImportTimestamp(request, now), minimum_timestamp);
392 [ + - ]: 721 : const UniValue result = ProcessDescriptorImport(*pwallet, request, timestamp);
393 [ + - + - ]: 721 : response.push_back(result);
394 : :
395 [ + + ]: 721 : if (lowest_timestamp > timestamp ) {
396 : 500 : lowest_timestamp = timestamp;
397 : : }
398 : :
399 : : // If we know the chain tip, and at least one request was successful then allow rescan
400 [ + + + - : 1357 : if (!rescan && result["success"].get_bool()) {
+ - + - +
+ + + -
- ]
401 : 603 : rescan = true;
402 : : }
403 : 721 : }
404 [ + - ]: 635 : pwallet->ConnectScriptPubKeyManNotifiers();
405 [ + - ]: 635 : pwallet->RefreshAllTXOs();
406 : 0 : }
407 : :
408 : : // Rescan the blockchain using the lowest timestamp
409 [ + + ]: 635 : if (rescan) {
410 [ + - ]: 603 : int64_t scanned_time = pwallet->RescanFromTime(lowest_timestamp, reserver, /*update=*/true);
411 [ + - ]: 603 : pwallet->ResubmitWalletTransactions(node::TxBroadcast::MEMPOOL_NO_BROADCAST, /*force=*/true);
412 : :
413 [ - + ]: 603 : if (pwallet->IsAbortingRescan()) {
414 [ # # # # ]: 0 : throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");
415 : : }
416 : :
417 [ + + ]: 603 : if (scanned_time > lowest_timestamp) {
418 [ + - + - ]: 1 : std::vector<UniValue> results = response.getValues();
419 [ + - ]: 1 : response.clear();
420 [ + - ]: 1 : response.setArray();
421 : :
422 : : // Compose the response
423 [ - + + + ]: 2 : for (unsigned int i = 0; i < requests.size(); ++i) {
424 [ + - + - ]: 1 : const UniValue& request = requests.getValues().at(i);
425 : :
426 : : // If the descriptor timestamp is within the successfully scanned
427 : : // range, or if the import result already has an error set, let
428 : : // the result stand unmodified. Otherwise replace the result
429 : : // with an error message.
430 [ + - + - : 2 : if (scanned_time <= GetImportTimestamp(request, now) || results.at(i).exists("error")) {
+ - + - +
- + - - +
- - ]
431 [ # # # # : 0 : response.push_back(results.at(i));
# # ]
432 : : } else {
433 : 1 : std::string error_msg{strprintf("Rescan failed for descriptor with timestamp %d. There "
434 : : "was an error reading a block from time %d, which is after or within %d seconds "
435 : : "of key creation, and could contain transactions pertaining to the desc. As a "
436 : : "result, transactions and coins using this desc may not appear in the wallet.",
437 [ + - + - ]: 1 : GetImportTimestamp(request, now), scanned_time - TIMESTAMP_WINDOW - 1, TIMESTAMP_WINDOW)};
438 [ + - - + ]: 1 : if (pwallet->chain().havePruned()) {
439 [ # # ]: 0 : error_msg += strprintf(" This error could be caused by pruning or data corruption "
440 : : "(see bitcoind log for details) and could be dealt with by downloading and "
441 : 0 : "rescanning the relevant blocks (see -reindex option and rescanblockchain RPC).");
442 [ + - + - ]: 1 : } else if (pwallet->chain().hasAssumedValidChain()) {
443 [ + - ]: 2 : error_msg += strprintf(" This error is likely caused by an in-progress assumeutxo "
444 : : "background sync. Check logs or getchainstates RPC for assumeutxo background "
445 : 1 : "sync progress and try again later.");
446 : : } else {
447 [ # # ]: 0 : error_msg += strprintf(" This error could potentially caused by data corruption. If "
448 : 0 : "the issue persists you may want to reindex (see -reindex option).");
449 : : }
450 : :
451 : 1 : UniValue result = UniValue(UniValue::VOBJ);
452 [ + - + - : 2 : result.pushKV("success", UniValue(false));
+ - ]
453 [ + - + - : 2 : result.pushKV("error", JSONRPCError(RPC_MISC_ERROR, error_msg));
+ - ]
454 [ + - ]: 1 : response.push_back(std::move(result));
455 : 1 : }
456 : : }
457 : 1 : }
458 : : }
459 : :
460 : 635 : return response;
461 [ + - ]: 1905 : },
462 [ + - + - : 24888 : };
+ - + - +
+ + + + +
- - - - -
- ]
463 [ + - + - : 32208 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - -
- ]
464 : :
465 : 994 : RPCHelpMan listdescriptors()
466 : : {
467 : 994 : return RPCHelpMan{
468 : 994 : "listdescriptors",
469 [ + - ]: 1988 : "List all descriptors present in a wallet.\n",
470 : : {
471 [ + - + - : 2982 : {"private", RPCArg::Type::BOOL, RPCArg::Default{false}, "Show private descriptors."}
+ - ]
472 : : },
473 [ + - + - : 3976 : RPCResult{RPCResult::Type::OBJ, "", "", {
+ - ]
474 [ + - + - ]: 1988 : {RPCResult::Type::STR, "wallet_name", "Name of wallet this operation was performed on"},
475 [ + - + - ]: 1988 : {RPCResult::Type::ARR, "descriptors", "Array of descriptor objects (sorted by descriptor string representation)",
476 : : {
477 [ + - + - ]: 1988 : {RPCResult::Type::OBJ, "", "", {
478 [ + - + - ]: 1988 : {RPCResult::Type::STR, "desc", "Descriptor string representation"},
479 [ + - + - ]: 1988 : {RPCResult::Type::NUM, "timestamp", "The creation time of the descriptor"},
480 [ + - + - ]: 1988 : {RPCResult::Type::BOOL, "active", "Whether this descriptor is currently used to generate new addresses"},
481 [ + - + - ]: 1988 : {RPCResult::Type::BOOL, "internal", /*optional=*/true, "True if this descriptor is used to generate change addresses. False if this descriptor is used to generate receiving addresses; defined only for active descriptors"},
482 [ + - + - ]: 1988 : {RPCResult::Type::ARR_FIXED, "range", /*optional=*/true, "Defined only for ranged descriptors", {
483 [ + - + - ]: 1988 : {RPCResult::Type::NUM, "", "Range start inclusive"},
484 [ + - + - ]: 1988 : {RPCResult::Type::NUM, "", "Range end inclusive"},
485 : : }},
486 [ + - + - ]: 1988 : {RPCResult::Type::NUM, "next", /*optional=*/true, "Same as next_index field. Kept for compatibility reason."},
487 [ + - + - ]: 1988 : {RPCResult::Type::NUM, "next_index", /*optional=*/true, "The next index to generate addresses from; defined only for ranged descriptors"},
488 : : }},
489 : : }}
490 [ + - + - : 24850 : }},
+ - + - +
- + + + +
+ + + + -
- - - - -
- - ]
491 : 994 : RPCExamples{
492 [ + - + - : 1988 : HelpExampleCli("listdescriptors", "") + HelpExampleRpc("listdescriptors", "")
+ - + - +
- + - +
- ]
493 [ + - + - : 3976 : + HelpExampleCli("listdescriptors", "true") + HelpExampleRpc("listdescriptors", "true")
+ - + - +
- + - + -
+ - ]
494 [ + - ]: 994 : },
495 : 994 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
496 : : {
497 [ - + ]: 165 : const std::shared_ptr<const CWallet> wallet = GetWalletForJSONRPCRequest(request);
498 [ - + ]: 164 : if (!wallet) return UniValue::VNULL;
499 : :
500 [ + - + + : 164 : const bool priv = !request.params[0].isNull() && request.params[0].get_bool();
+ - + - +
+ ]
501 [ + - + + : 164 : if (wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && priv) {
+ + ]
502 [ + - + - ]: 2 : throw JSONRPCError(RPC_WALLET_ERROR, "Can't get private descriptor string for watch-only wallets");
503 : : }
504 [ + + ]: 163 : if (priv) {
505 [ + + ]: 68 : EnsureWalletIsUnlocked(*wallet);
506 : : }
507 : :
508 [ + - ]: 162 : LOCK(wallet->cs_wallet);
509 : :
510 [ + - ]: 162 : const auto active_spk_mans = wallet->GetActiveScriptPubKeyMans();
511 : :
512 [ - - ]: 3340 : struct WalletDescInfo {
513 : : std::string descriptor;
514 : : uint64_t creation_time;
515 : : bool active;
516 : : std::optional<bool> internal;
517 : : std::optional<std::pair<int64_t,int64_t>> range;
518 : : int64_t next_index;
519 : : };
520 : :
521 : 162 : std::vector<WalletDescInfo> wallet_descriptors;
522 [ + - + + ]: 1352 : for (const auto& spk_man : wallet->GetAllScriptPubKeyMans()) {
523 [ + - ]: 1190 : const auto desc_spk_man = dynamic_cast<DescriptorScriptPubKeyMan*>(spk_man);
524 [ - + ]: 1190 : if (!desc_spk_man) {
525 [ # # # # ]: 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Unexpected ScriptPubKey manager type.");
526 : : }
527 [ + - ]: 1190 : LOCK(desc_spk_man->cs_desc_man);
528 [ + - ]: 1190 : const auto& wallet_descriptor = desc_spk_man->GetWalletDescriptor();
529 [ + - ]: 1190 : std::string descriptor;
530 [ + - + - ]: 1190 : CHECK_NONFATAL(desc_spk_man->GetDescriptorString(descriptor, priv));
531 [ + - ]: 1190 : const bool is_range = wallet_descriptor.descriptor->IsRange();
532 [ - + ]: 2380 : wallet_descriptors.push_back({
533 : : descriptor,
534 : 1190 : wallet_descriptor.creation_time,
535 : 1190 : active_spk_mans.contains(desc_spk_man),
536 [ + - ]: 1190 : wallet->IsInternalScriptPubKeyMan(desc_spk_man),
537 [ + + ]: 1190 : is_range ? std::optional(std::make_pair(wallet_descriptor.range_start, wallet_descriptor.range_end)) : std::nullopt,
538 : 1190 : wallet_descriptor.next_index
539 : : });
540 [ + - ]: 2380 : }
541 : :
542 : 162 : std::sort(wallet_descriptors.begin(), wallet_descriptors.end(), [](const auto& a, const auto& b) {
543 [ - - - - : 3321 : return a.descriptor < b.descriptor;
+ + - - -
- - - - -
- - - - -
- - - +
+ ]
544 : : });
545 : :
546 : 162 : UniValue descriptors(UniValue::VARR);
547 [ + + ]: 1352 : for (const WalletDescInfo& info : wallet_descriptors) {
548 : 1190 : UniValue spk(UniValue::VOBJ);
549 [ + - + - : 2380 : spk.pushKV("desc", info.descriptor);
+ - ]
550 [ + - + - : 2380 : spk.pushKV("timestamp", info.creation_time);
+ - ]
551 [ + - + - : 2380 : spk.pushKV("active", info.active);
+ - ]
552 [ + + ]: 1190 : if (info.internal.has_value()) {
553 [ + - + - : 2274 : spk.pushKV("internal", info.internal.value());
+ - ]
554 : : }
555 [ + + ]: 1190 : if (info.range.has_value()) {
556 : 1157 : UniValue range(UniValue::VARR);
557 [ + - + - ]: 1157 : range.push_back(info.range->first);
558 [ + - + - ]: 1157 : range.push_back(info.range->second - 1);
559 [ + - + - ]: 2314 : spk.pushKV("range", std::move(range));
560 [ + - + - : 2314 : spk.pushKV("next", info.next_index);
+ - ]
561 [ + - + - : 2314 : spk.pushKV("next_index", info.next_index);
+ - ]
562 : 1157 : }
563 [ + - ]: 1190 : descriptors.push_back(std::move(spk));
564 : 1190 : }
565 : :
566 : 162 : UniValue response(UniValue::VOBJ);
567 [ + - + - : 324 : response.pushKV("wallet_name", wallet->GetName());
+ - ]
568 [ + - + - ]: 324 : response.pushKV("descriptors", std::move(descriptors));
569 : :
570 : 162 : return response;
571 [ + - + - : 2866 : },
+ - ]
572 [ + - + - : 4970 : };
+ + - - ]
573 [ + - + - : 13916 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - - - -
- - - ]
574 : :
575 : 896 : RPCHelpMan backupwallet()
576 : : {
577 : 896 : return RPCHelpMan{
578 : 896 : "backupwallet",
579 [ + - ]: 1792 : "Safely copies the current wallet file to the specified destination, which can either be a directory or a path with a filename.\n",
580 : : {
581 [ + - + - ]: 1792 : {"destination", RPCArg::Type::STR, RPCArg::Optional::NO, "The destination directory or file"},
582 : : },
583 [ + - + - : 1792 : RPCResult{RPCResult::Type::NONE, "", ""},
+ - + - ]
584 : 896 : RPCExamples{
585 [ + - + - : 1792 : HelpExampleCli("backupwallet", "\"backup.dat\"")
+ - ]
586 [ + - + - : 3584 : + HelpExampleRpc("backupwallet", "\"backup.dat\"")
+ - + - ]
587 [ + - ]: 896 : },
588 : 896 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
589 : : {
590 [ - + ]: 67 : const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
591 [ - + ]: 67 : if (!pwallet) return UniValue::VNULL;
592 : :
593 : : // Make sure the results are valid at least up to the most recent block
594 : : // the user could have gotten from another RPC command prior to now
595 [ + - ]: 67 : pwallet->BlockUntilSyncedToCurrentChain();
596 : :
597 [ + - ]: 67 : LOCK(pwallet->cs_wallet);
598 : :
599 [ + - + - : 67 : std::string strDest = request.params[0].get_str();
- + ]
600 [ + - + + ]: 67 : if (!pwallet->BackupWallet(strDest)) {
601 [ + - + - ]: 8 : throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!");
602 : : }
603 : :
604 : 63 : return UniValue::VNULL;
605 [ + - ]: 197 : },
606 [ + - + - : 4480 : };
+ + - - ]
607 [ + - ]: 1792 : }
608 : :
609 : :
610 : 865 : RPCHelpMan restorewallet()
611 : : {
612 : 865 : return RPCHelpMan{
613 : 865 : "restorewallet",
614 [ + - ]: 1730 : "Restores and loads a wallet from backup.\n"
615 : : "\nThe rescan is significantly faster if block filters are available"
616 : : "\n(using startup option \"-blockfilterindex=1\").\n",
617 : : {
618 [ + - + - ]: 1730 : {"wallet_name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name that will be applied to the restored wallet"},
619 [ + - + - ]: 1730 : {"backup_file", RPCArg::Type::STR, RPCArg::Optional::NO, "The backup file that will be used to restore the wallet."},
620 [ + - + - ]: 1730 : {"load_on_startup", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED, "Save wallet name to persistent settings and load on startup. True to add wallet to startup list, false to remove, null to leave unchanged."},
621 : : },
622 [ + - ]: 1730 : RPCResult{
623 [ + - + - ]: 1730 : RPCResult::Type::OBJ, "", "",
624 : : {
625 [ + - + - ]: 1730 : {RPCResult::Type::STR, "name", "The wallet name if restored successfully."},
626 [ + - + - ]: 1730 : {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to restoring and loading the wallet.",
627 : : {
628 [ + - + - ]: 1730 : {RPCResult::Type::STR, "", ""},
629 : : }},
630 : : }
631 [ + - + - : 6055 : },
+ - + + +
+ - - -
- ]
632 : 865 : RPCExamples{
633 [ + - + - : 1730 : HelpExampleCli("restorewallet", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
+ - ]
634 [ + - + - : 3460 : + HelpExampleRpc("restorewallet", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
+ - + - ]
635 [ + - + - : 6055 : + HelpExampleCliNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak\""}, {"load_on_startup", true}})
+ - + - +
+ - - ]
636 [ + - + - : 6055 : + HelpExampleRpcNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak\""}, {"load_on_startup", true}})
+ - + - +
+ - - ]
637 [ + - ]: 865 : },
638 : 865 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
639 : : {
640 : :
641 : 36 : WalletContext& context = EnsureWalletContext(request.context);
642 : :
643 [ - + ]: 36 : auto backup_file = fs::u8path(request.params[1].get_str());
644 : :
645 [ + - + - : 36 : std::string wallet_name = request.params[0].get_str();
- + ]
646 : :
647 [ + - + - : 36 : std::optional<bool> load_on_start = request.params[2].isNull() ? std::nullopt : std::optional<bool>(request.params[2].get_bool());
- - - - ]
648 : :
649 : 36 : DatabaseStatus status;
650 [ + - ]: 36 : bilingual_str error;
651 : 36 : std::vector<bilingual_str> warnings;
652 : :
653 [ + - ]: 36 : const std::shared_ptr<CWallet> wallet = RestoreWallet(context, backup_file, wallet_name, load_on_start, status, error, warnings);
654 : :
655 [ + + ]: 36 : HandleWalletError(wallet, status, error);
656 : :
657 : 19 : UniValue obj(UniValue::VOBJ);
658 [ + - + - : 38 : obj.pushKV("name", wallet->GetName());
+ - ]
659 [ + - ]: 19 : PushWarnings(warnings, obj);
660 : :
661 [ + - ]: 19 : return obj;
662 : :
663 : 108 : },
664 [ + - + - : 6055 : };
+ + - - ]
665 [ + - + - : 9515 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
- - - - -
- - - ]
666 : : } // namespace wallet
|