Coverage Report

Created: 2025-10-29 16:48

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