fuzz coverage

Coverage Report

Created: 2025-08-28 15:26

/Users/eugenesiegel/btc/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 <bitcoin-build-config.h> // IWYU pragma: keep
6
7
#include <chain.h>
8
#include <clientversion.h>
9
#include <core_io.h>
10
#include <hash.h>
11
#include <interfaces/chain.h>
12
#include <key_io.h>
13
#include <merkleblock.h>
14
#include <rpc/util.h>
15
#include <script/descriptor.h>
16
#include <script/script.h>
17
#include <script/solver.h>
18
#include <sync.h>
19
#include <uint256.h>
20
#include <util/bip32.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
using util::SplitString;
38
39
namespace wallet {
40
0
std::string static EncodeDumpString(const std::string &str) {
41
0
    std::stringstream ret;
42
0
    for (const unsigned char c : str) {
43
0
        if (c <= 32 || c >= 128 || c == '%') {
44
0
            ret << '%' << HexStr({&c, 1});
45
0
        } else {
46
0
            ret << c;
47
0
        }
48
0
    }
49
0
    return ret.str();
50
0
}
51
52
0
static std::string DecodeDumpString(const std::string &str) {
53
0
    std::stringstream ret;
54
0
    for (unsigned int pos = 0; pos < str.length(); pos++) {
55
0
        unsigned char c = str[pos];
56
0
        if (c == '%' && pos+2 < str.length()) {
57
0
            c = (((str[pos+1]>>6)*9+((str[pos+1]-'0')&15)) << 4) |
58
0
                ((str[pos+2]>>6)*9+((str[pos+2]-'0')&15));
59
0
            pos += 2;
60
0
        }
61
0
        ret << c;
62
0
    }
63
0
    return ret.str();
64
0
}
65
66
static bool GetWalletAddressesForKey(const LegacyScriptPubKeyMan* spk_man, const CWallet& wallet, const CKeyID& keyid, std::string& strAddr, std::string& strLabel) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
67
0
{
68
0
    bool fLabelFound = false;
69
0
    CKey key;
70
0
    spk_man->GetKey(keyid, key);
71
0
    for (const auto& dest : GetAllDestinationsForKey(key.GetPubKey())) {
72
0
        const auto* address_book_entry = wallet.FindAddressBookEntry(dest);
73
0
        if (address_book_entry) {
74
0
            if (!strAddr.empty()) {
75
0
                strAddr += ",";
76
0
            }
77
0
            strAddr += EncodeDestination(dest);
78
0
            strLabel = EncodeDumpString(address_book_entry->GetLabel());
79
0
            fLabelFound = true;
80
0
        }
81
0
    }
82
0
    if (!fLabelFound) {
83
0
        strAddr = EncodeDestination(GetDestinationForKey(key.GetPubKey(), wallet.m_default_address_type));
84
0
    }
85
0
    return fLabelFound;
86
0
}
87
88
static const int64_t TIMESTAMP_MIN = 0;
89
90
static void RescanWallet(CWallet& wallet, const WalletRescanReserver& reserver, int64_t time_begin = TIMESTAMP_MIN, bool update = true)
91
0
{
92
0
    int64_t scanned_time = wallet.RescanFromTime(time_begin, reserver, update);
93
0
    if (wallet.IsAbortingRescan()) {
94
0
        throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");
95
0
    } else if (scanned_time > time_begin) {
96
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Rescan was unable to fully rescan the blockchain. Some transactions may be missing.");
97
0
    }
98
0
}
99
100
static void EnsureBlockDataFromTime(const CWallet& wallet, int64_t timestamp)
101
0
{
102
0
    auto& chain{wallet.chain()};
103
0
    if (!chain.havePruned()) {
104
0
        return;
105
0
    }
106
107
0
    int height{0};
108
0
    const bool found{chain.findFirstBlockWithTimeAndHeight(timestamp - TIMESTAMP_WINDOW, 0, FoundBlock().height(height))};
109
110
0
    uint256 tip_hash{WITH_LOCK(wallet.cs_wallet, return wallet.GetLastBlockHash())};
Line
Count
Source
301
0
#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
111
0
    if (found && !chain.hasBlocks(tip_hash, height)) {
112
0
        throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Pruned blocks from height %d required to import keys. Use RPC call getblockchaininfo to determine your pruned height.", height));
Line
Count
Source
1172
0
#define strprintf tfm::format
113
0
    }
114
0
}
115
116
RPCHelpMan importprivkey()
117
0
{
118
0
    return RPCHelpMan{"importprivkey",
119
0
                "\nAdds a private key (as returned by dumpprivkey) to your wallet. Requires a new wallet backup.\n"
120
0
                "Hint: use importmulti to import more than one private key.\n"
121
0
            "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n"
122
0
            "may report that the imported key exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n"
123
0
            "The rescan parameter can be set to false if the key was never used to create transactions. If it is set to false,\n"
124
0
            "but the key was used to create transactions, rescanblockchain needs to be called with the appropriate block range.\n"
125
0
            "Note: Use \"getwalletinfo\" to query the scanning progress.\n"
126
0
            "Note: This command is only compatible with legacy wallets. Use \"importdescriptors\" with \"combo(X)\" for descriptor wallets.\n",
127
0
                {
128
0
                    {"privkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The private key (see dumpprivkey)"},
129
0
                    {"label", RPCArg::Type::STR, RPCArg::DefaultHint{"current label if address exists, otherwise \"\""}, "An optional label"},
130
0
                    {"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Scan the chain and mempool for wallet transactions."},
131
0
                },
132
0
                RPCResult{RPCResult::Type::NONE, "", ""},
133
0
                RPCExamples{
134
0
            "\nDump a private key\n"
135
0
            + HelpExampleCli("dumpprivkey", "\"myaddress\"") +
136
0
            "\nImport the private key with rescan\n"
137
0
            + HelpExampleCli("importprivkey", "\"mykey\"") +
138
0
            "\nImport using a label and without rescan\n"
139
0
            + HelpExampleCli("importprivkey", "\"mykey\" \"testing\" false") +
140
0
            "\nImport using default blank label and without rescan\n"
141
0
            + HelpExampleCli("importprivkey", "\"mykey\" \"\" false") +
142
0
            "\nAs a JSON-RPC call\n"
143
0
            + HelpExampleRpc("importprivkey", "\"mykey\", \"testing\", false")
144
0
                },
145
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
146
0
{
147
0
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
148
0
    if (!pwallet) return UniValue::VNULL;
149
150
0
    if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
151
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
152
0
    }
153
154
0
    EnsureLegacyScriptPubKeyMan(*pwallet, true);
155
156
0
    WalletRescanReserver reserver(*pwallet);
157
0
    bool fRescan = true;
158
0
    {
159
0
        LOCK(pwallet->cs_wallet);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
160
161
0
        EnsureWalletIsUnlocked(*pwallet);
162
163
0
        std::string strSecret = request.params[0].get_str();
164
0
        const std::string strLabel{LabelFromValue(request.params[1])};
165
166
        // Whether to perform rescan after import
167
0
        if (!request.params[2].isNull())
168
0
            fRescan = request.params[2].get_bool();
169
170
0
        if (fRescan && pwallet->chain().havePruned()) {
171
            // Exit early and print an error.
172
            // If a block is pruned after this check, we will import the key(s),
173
            // but fail the rescan with a generic error.
174
0
            throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned");
175
0
        }
176
177
0
        if (fRescan && !reserver.reserve()) {
178
0
            throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
179
0
        }
180
181
0
        CKey key = DecodeSecret(strSecret);
182
0
        if (!key.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
183
184
0
        CPubKey pubkey = key.GetPubKey();
185
0
        CHECK_NONFATAL(key.VerifyPubKey(pubkey));
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
186
0
        CKeyID vchAddress = pubkey.GetID();
187
0
        {
188
0
            pwallet->MarkDirty();
189
190
            // We don't know which corresponding address will be used;
191
            // label all new addresses, and label existing addresses if a
192
            // label was passed.
193
0
            for (const auto& dest : GetAllDestinationsForKey(pubkey)) {
194
0
                if (!request.params[1].isNull() || !pwallet->FindAddressBookEntry(dest)) {
195
0
                    pwallet->SetAddressBook(dest, strLabel, AddressPurpose::RECEIVE);
196
0
                }
197
0
            }
198
199
            // Use timestamp of 1 to scan the whole chain
200
0
            if (!pwallet->ImportPrivKeys({{vchAddress, key}}, 1)) {
201
0
                throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
202
0
            }
203
204
            // Add the wpkh script for this key if possible
205
0
            if (pubkey.IsCompressed()) {
206
0
                pwallet->ImportScripts({GetScriptForDestination(WitnessV0KeyHash(vchAddress))}, /*timestamp=*/0);
207
0
            }
208
0
        }
209
0
    }
210
0
    if (fRescan) {
211
0
        RescanWallet(*pwallet, reserver);
212
0
    }
213
214
0
    return UniValue::VNULL;
215
0
},
216
0
    };
217
0
}
218
219
RPCHelpMan importaddress()
220
0
{
221
0
    return RPCHelpMan{"importaddress",
222
0
            "\nAdds an address or script (in hex) that can be watched as if it were in your wallet but cannot be used to spend. Requires a new wallet backup.\n"
223
0
            "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n"
224
0
            "may report that the imported address exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n"
225
0
            "The rescan parameter can be set to false if the key was never used to create transactions. If it is set to false,\n"
226
0
            "but the key was used to create transactions, rescanblockchain needs to be called with the appropriate block range.\n"
227
0
            "If you have the full public key, you should call importpubkey instead of this.\n"
228
0
            "Hint: use importmulti to import more than one address.\n"
229
0
            "\nNote: If you import a non-standard raw script in hex form, outputs sending to it will be treated\n"
230
0
            "as change, and not show up in many RPCs.\n"
231
0
            "Note: Use \"getwalletinfo\" to query the scanning progress.\n"
232
0
            "Note: This command is only compatible with legacy wallets. Use \"importdescriptors\" for descriptor wallets.\n",
233
0
                {
234
0
                    {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The Bitcoin address (or hex-encoded script)"},
235
0
                    {"label", RPCArg::Type::STR, RPCArg::Default{""}, "An optional label"},
236
0
                    {"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Scan the chain and mempool for wallet transactions."},
237
0
                    {"p2sh", RPCArg::Type::BOOL, RPCArg::Default{false}, "Add the P2SH version of the script as well"},
238
0
                },
239
0
                RPCResult{RPCResult::Type::NONE, "", ""},
240
0
                RPCExamples{
241
0
            "\nImport an address with rescan\n"
242
0
            + HelpExampleCli("importaddress", "\"myaddress\"") +
243
0
            "\nImport using a label without rescan\n"
244
0
            + HelpExampleCli("importaddress", "\"myaddress\" \"testing\" false") +
245
0
            "\nAs a JSON-RPC call\n"
246
0
            + HelpExampleRpc("importaddress", "\"myaddress\", \"testing\", false")
247
0
                },
248
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
249
0
{
250
0
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
251
0
    if (!pwallet) return UniValue::VNULL;
252
253
0
    EnsureLegacyScriptPubKeyMan(*pwallet, true);
254
255
0
    const std::string strLabel{LabelFromValue(request.params[1])};
256
257
    // Whether to perform rescan after import
258
0
    bool fRescan = true;
259
0
    if (!request.params[2].isNull())
260
0
        fRescan = request.params[2].get_bool();
261
262
0
    if (fRescan && pwallet->chain().havePruned()) {
263
        // Exit early and print an error.
264
        // If a block is pruned after this check, we will import the key(s),
265
        // but fail the rescan with a generic error.
266
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned");
267
0
    }
268
269
0
    WalletRescanReserver reserver(*pwallet);
270
0
    if (fRescan && !reserver.reserve()) {
271
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
272
0
    }
273
274
    // Whether to import a p2sh version, too
275
0
    bool fP2SH = false;
276
0
    if (!request.params[3].isNull())
277
0
        fP2SH = request.params[3].get_bool();
278
279
0
    {
280
0
        LOCK(pwallet->cs_wallet);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
281
282
0
        CTxDestination dest = DecodeDestination(request.params[0].get_str());
283
0
        if (IsValidDestination(dest)) {
284
0
            if (fP2SH) {
285
0
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot use the p2sh flag with an address - use a script instead");
286
0
            }
287
0
            if (OutputTypeFromDestination(dest) == OutputType::BECH32M) {
288
0
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Bech32m addresses cannot be imported into legacy wallets");
289
0
            }
290
291
0
            pwallet->MarkDirty();
292
293
0
            pwallet->ImportScriptPubKeys(strLabel, {GetScriptForDestination(dest)}, /*have_solving_data=*/false, /*apply_label=*/true, /*timestamp=*/1);
294
0
        } else if (IsHex(request.params[0].get_str())) {
295
0
            std::vector<unsigned char> data(ParseHex(request.params[0].get_str()));
296
0
            CScript redeem_script(data.begin(), data.end());
297
298
0
            std::set<CScript> scripts = {redeem_script};
299
0
            pwallet->ImportScripts(scripts, /*timestamp=*/0);
300
301
0
            if (fP2SH) {
302
0
                scripts.insert(GetScriptForDestination(ScriptHash(redeem_script)));
303
0
            }
304
305
0
            pwallet->ImportScriptPubKeys(strLabel, scripts, /*have_solving_data=*/false, /*apply_label=*/true, /*timestamp=*/1);
306
0
        } else {
307
0
            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address or script");
308
0
        }
309
0
    }
310
0
    if (fRescan)
311
0
    {
312
0
        RescanWallet(*pwallet, reserver);
313
0
        pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
314
0
    }
315
316
0
    return UniValue::VNULL;
317
0
},
318
0
    };
319
0
}
320
321
RPCHelpMan importprunedfunds()
322
0
{
323
0
    return RPCHelpMan{"importprunedfunds",
324
0
                "\nImports 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",
325
0
                {
326
0
                    {"rawtransaction", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "A raw transaction in hex funding an already-existing address in wallet"},
327
0
                    {"txoutproof", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex output from gettxoutproof that contains the transaction"},
328
0
                },
329
0
                RPCResult{RPCResult::Type::NONE, "", ""},
330
0
                RPCExamples{""},
331
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
332
0
{
333
0
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
334
0
    if (!pwallet) return UniValue::VNULL;
335
336
0
    CMutableTransaction tx;
337
0
    if (!DecodeHexTx(tx, request.params[0].get_str())) {
338
0
        throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed. Make sure the tx has at least one input.");
339
0
    }
340
0
    uint256 hashTx = tx.GetHash();
341
342
0
    DataStream ssMB{ParseHexV(request.params[1], "proof")};
343
0
    CMerkleBlock merkleBlock;
344
0
    ssMB >> merkleBlock;
345
346
    //Search partial merkle tree in proof for our transaction and index in valid block
347
0
    std::vector<uint256> vMatch;
348
0
    std::vector<unsigned int> vIndex;
349
0
    if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot) {
350
0
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Something wrong with merkleblock");
351
0
    }
352
353
0
    LOCK(pwallet->cs_wallet);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
354
0
    int height;
355
0
    if (!pwallet->chain().findAncestorByHash(pwallet->GetLastBlockHash(), merkleBlock.header.GetHash(), FoundBlock().height(height))) {
356
0
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
357
0
    }
358
359
0
    std::vector<uint256>::const_iterator it;
360
0
    if ((it = std::find(vMatch.begin(), vMatch.end(), hashTx)) == vMatch.end()) {
361
0
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction given doesn't exist in proof");
362
0
    }
363
364
0
    unsigned int txnIndex = vIndex[it - vMatch.begin()];
365
366
0
    CTransactionRef tx_ref = MakeTransactionRef(tx);
367
0
    if (pwallet->IsMine(*tx_ref)) {
368
0
        pwallet->AddToWallet(std::move(tx_ref), TxStateConfirmed{merkleBlock.header.GetHash(), height, static_cast<int>(txnIndex)});
369
0
        return UniValue::VNULL;
370
0
    }
371
372
0
    throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No addresses in wallet correspond to included transaction");
373
0
},
374
0
    };
375
0
}
376
377
RPCHelpMan removeprunedfunds()
378
0
{
379
0
    return RPCHelpMan{"removeprunedfunds",
380
0
                "\nDeletes the specified transaction from the wallet. Meant for use with pruned wallets and as a companion to importprunedfunds. This will affect wallet balances.\n",
381
0
                {
382
0
                    {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex-encoded id of the transaction you are deleting"},
383
0
                },
384
0
                RPCResult{RPCResult::Type::NONE, "", ""},
385
0
                RPCExamples{
386
0
                    HelpExampleCli("removeprunedfunds", "\"a8d0c0184dde994a09ec054286f1ce581bebf46446a512166eae7628734ea0a5\"") +
387
0
            "\nAs a JSON-RPC call\n"
388
0
            + HelpExampleRpc("removeprunedfunds", "\"a8d0c0184dde994a09ec054286f1ce581bebf46446a512166eae7628734ea0a5\"")
389
0
                },
390
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
391
0
{
392
0
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
393
0
    if (!pwallet) return UniValue::VNULL;
394
395
0
    LOCK(pwallet->cs_wallet);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
396
397
0
    uint256 hash(ParseHashV(request.params[0], "txid"));
398
0
    std::vector<uint256> vHash;
399
0
    vHash.push_back(hash);
400
0
    if (auto res = pwallet->RemoveTxs(vHash); !res) {
401
0
        throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(res).original);
402
0
    }
403
404
0
    return UniValue::VNULL;
405
0
},
406
0
    };
407
0
}
408
409
RPCHelpMan importpubkey()
410
0
{
411
0
    return RPCHelpMan{"importpubkey",
412
0
                "\nAdds a public key (in hex) that can be watched as if it were in your wallet but cannot be used to spend. Requires a new wallet backup.\n"
413
0
                "Hint: use importmulti to import more than one public key.\n"
414
0
            "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n"
415
0
            "may report that the imported pubkey exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.\n"
416
0
            "The rescan parameter can be set to false if the key was never used to create transactions. If it is set to false,\n"
417
0
            "but the key was used to create transactions, rescanblockchain needs to be called with the appropriate block range.\n"
418
0
            "Note: Use \"getwalletinfo\" to query the scanning progress.\n"
419
0
            "Note: This command is only compatible with legacy wallets. Use \"importdescriptors\" with \"combo(X)\" for descriptor wallets.\n",
420
0
                {
421
0
                    {"pubkey", RPCArg::Type::STR, RPCArg::Optional::NO, "The hex-encoded public key"},
422
0
                    {"label", RPCArg::Type::STR, RPCArg::Default{""}, "An optional label"},
423
0
                    {"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Scan the chain and mempool for wallet transactions."},
424
0
                },
425
0
                RPCResult{RPCResult::Type::NONE, "", ""},
426
0
                RPCExamples{
427
0
            "\nImport a public key with rescan\n"
428
0
            + HelpExampleCli("importpubkey", "\"mypubkey\"") +
429
0
            "\nImport using a label without rescan\n"
430
0
            + HelpExampleCli("importpubkey", "\"mypubkey\" \"testing\" false") +
431
0
            "\nAs a JSON-RPC call\n"
432
0
            + HelpExampleRpc("importpubkey", "\"mypubkey\", \"testing\", false")
433
0
                },
434
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
435
0
{
436
0
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
437
0
    if (!pwallet) return UniValue::VNULL;
438
439
0
    EnsureLegacyScriptPubKeyMan(*pwallet, true);
440
441
0
    const std::string strLabel{LabelFromValue(request.params[1])};
442
443
    // Whether to perform rescan after import
444
0
    bool fRescan = true;
445
0
    if (!request.params[2].isNull())
446
0
        fRescan = request.params[2].get_bool();
447
448
0
    if (fRescan && pwallet->chain().havePruned()) {
449
        // Exit early and print an error.
450
        // If a block is pruned after this check, we will import the key(s),
451
        // but fail the rescan with a generic error.
452
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled when blocks are pruned");
453
0
    }
454
455
0
    WalletRescanReserver reserver(*pwallet);
456
0
    if (fRescan && !reserver.reserve()) {
457
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
458
0
    }
459
460
0
    CPubKey pubKey = HexToPubKey(request.params[0].get_str());
461
462
0
    {
463
0
        LOCK(pwallet->cs_wallet);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
464
465
0
        std::set<CScript> script_pub_keys;
466
0
        for (const auto& dest : GetAllDestinationsForKey(pubKey)) {
467
0
            script_pub_keys.insert(GetScriptForDestination(dest));
468
0
        }
469
470
0
        pwallet->MarkDirty();
471
472
0
        pwallet->ImportScriptPubKeys(strLabel, script_pub_keys, /*have_solving_data=*/true, /*apply_label=*/true, /*timestamp=*/1);
473
474
0
        pwallet->ImportPubKeys({{pubKey.GetID(), false}}, {{pubKey.GetID(), pubKey}} , /*key_origins=*/{}, /*add_keypool=*/false, /*timestamp=*/1);
475
0
    }
476
0
    if (fRescan)
477
0
    {
478
0
        RescanWallet(*pwallet, reserver);
479
0
        pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
480
0
    }
481
482
0
    return UniValue::VNULL;
483
0
},
484
0
    };
485
0
}
486
487
488
RPCHelpMan importwallet()
489
0
{
490
0
    return RPCHelpMan{"importwallet",
491
0
                "\nImports keys from a wallet dump file (see dumpwallet). Requires a new wallet backup to include imported keys.\n"
492
0
                "Note: Blockchain and Mempool will be rescanned after a successful import. Use \"getwalletinfo\" to query the scanning progress.\n"
493
0
                "Note: This command is only compatible with legacy wallets.\n",
494
0
                {
495
0
                    {"filename", RPCArg::Type::STR, RPCArg::Optional::NO, "The wallet file"},
496
0
                },
497
0
                RPCResult{RPCResult::Type::NONE, "", ""},
498
0
                RPCExamples{
499
0
            "\nDump the wallet\n"
500
0
            + HelpExampleCli("dumpwallet", "\"test\"") +
501
0
            "\nImport the wallet\n"
502
0
            + HelpExampleCli("importwallet", "\"test\"") +
503
0
            "\nImport using the json rpc call\n"
504
0
            + HelpExampleRpc("importwallet", "\"test\"")
505
0
                },
506
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
507
0
{
508
0
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
509
0
    if (!pwallet) return UniValue::VNULL;
510
511
0
    EnsureLegacyScriptPubKeyMan(*pwallet, true);
512
513
0
    WalletRescanReserver reserver(*pwallet);
514
0
    if (!reserver.reserve()) {
515
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
516
0
    }
517
518
0
    int64_t nTimeBegin = 0;
519
0
    bool fGood = true;
520
0
    {
521
0
        LOCK(pwallet->cs_wallet);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
522
523
0
        EnsureWalletIsUnlocked(*pwallet);
524
525
0
        std::ifstream file;
526
0
        file.open(fs::u8path(request.params[0].get_str()), std::ios::in | std::ios::ate);
527
0
        if (!file.is_open()) {
528
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
529
0
        }
530
0
        CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(nTimeBegin)));
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
531
532
0
        int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg());
533
0
        file.seekg(0, file.beg);
534
535
        // Use uiInterface.ShowProgress instead of pwallet.ShowProgress because pwallet.ShowProgress has a cancel button tied to AbortRescan which
536
        // we don't want for this progress bar showing the import progress. uiInterface.ShowProgress does not have a cancel button.
537
0
        pwallet->chain().showProgress(strprintf("%s %s", pwallet->GetDisplayName(), _("Importing…")), 0, false); // show progress dialog in GUI
Line
Count
Source
1172
0
#define strprintf tfm::format
538
0
        std::vector<std::tuple<CKey, int64_t, bool, std::string>> keys;
539
0
        std::vector<std::pair<CScript, int64_t>> scripts;
540
0
        while (file.good()) {
541
0
            pwallet->chain().showProgress("", std::max(1, std::min(50, (int)(((double)file.tellg() / (double)nFilesize) * 100))), false);
542
0
            std::string line;
543
0
            std::getline(file, line);
544
0
            if (line.empty() || line[0] == '#')
545
0
                continue;
546
547
0
            std::vector<std::string> vstr = SplitString(line, ' ');
548
0
            if (vstr.size() < 2)
549
0
                continue;
550
0
            CKey key = DecodeSecret(vstr[0]);
551
0
            if (key.IsValid()) {
552
0
                int64_t nTime{ParseISO8601DateTime(vstr[1]).value_or(0)};
553
0
                std::string strLabel;
554
0
                bool fLabel = true;
555
0
                for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
556
0
                    if (vstr[nStr].front() == '#')
557
0
                        break;
558
0
                    if (vstr[nStr] == "change=1")
559
0
                        fLabel = false;
560
0
                    if (vstr[nStr] == "reserve=1")
561
0
                        fLabel = false;
562
0
                    if (vstr[nStr].starts_with("label=")) {
563
0
                        strLabel = DecodeDumpString(vstr[nStr].substr(6));
564
0
                        fLabel = true;
565
0
                    }
566
0
                }
567
0
                nTimeBegin = std::min(nTimeBegin, nTime);
568
0
                keys.emplace_back(key, nTime, fLabel, strLabel);
569
0
            } else if(IsHex(vstr[0])) {
570
0
                std::vector<unsigned char> vData(ParseHex(vstr[0]));
571
0
                CScript script = CScript(vData.begin(), vData.end());
572
0
                int64_t birth_time{ParseISO8601DateTime(vstr[1]).value_or(0)};
573
0
                if (birth_time > 0) nTimeBegin = std::min(nTimeBegin, birth_time);
574
0
                scripts.emplace_back(script, birth_time);
575
0
            }
576
0
        }
577
0
        file.close();
578
0
        EnsureBlockDataFromTime(*pwallet, nTimeBegin);
579
        // We now know whether we are importing private keys, so we can error if private keys are disabled
580
0
        if (keys.size() > 0 && pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
581
0
            pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
582
0
            throw JSONRPCError(RPC_WALLET_ERROR, "Importing wallets is disabled when private keys are disabled");
583
0
        }
584
0
        double total = (double)(keys.size() + scripts.size());
585
0
        double progress = 0;
586
0
        for (const auto& key_tuple : keys) {
587
0
            pwallet->chain().showProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
588
0
            const CKey& key = std::get<0>(key_tuple);
589
0
            int64_t time = std::get<1>(key_tuple);
590
0
            bool has_label = std::get<2>(key_tuple);
591
0
            std::string label = std::get<3>(key_tuple);
592
593
0
            CPubKey pubkey = key.GetPubKey();
594
0
            CHECK_NONFATAL(key.VerifyPubKey(pubkey));
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
595
0
            CKeyID keyid = pubkey.GetID();
596
597
0
            pwallet->WalletLogPrintf("Importing %s...\n", EncodeDestination(PKHash(keyid)));
598
599
0
            if (!pwallet->ImportPrivKeys({{keyid, key}}, time)) {
600
0
                pwallet->WalletLogPrintf("Error importing key for %s\n", EncodeDestination(PKHash(keyid)));
601
0
                fGood = false;
602
0
                continue;
603
0
            }
604
605
0
            if (has_label)
606
0
                pwallet->SetAddressBook(PKHash(keyid), label, AddressPurpose::RECEIVE);
607
0
            progress++;
608
0
        }
609
0
        for (const auto& script_pair : scripts) {
610
0
            pwallet->chain().showProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
611
0
            const CScript& script = script_pair.first;
612
0
            int64_t time = script_pair.second;
613
614
0
            if (!pwallet->ImportScripts({script}, time)) {
615
0
                pwallet->WalletLogPrintf("Error importing script %s\n", HexStr(script));
616
0
                fGood = false;
617
0
                continue;
618
0
            }
619
620
0
            progress++;
621
0
        }
622
0
        pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
623
0
    }
624
0
    pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
625
0
    RescanWallet(*pwallet, reserver, nTimeBegin, /*update=*/false);
626
0
    pwallet->MarkDirty();
627
628
0
    if (!fGood)
629
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Error adding some keys/scripts to wallet");
630
631
0
    return UniValue::VNULL;
632
0
},
633
0
    };
634
0
}
635
636
RPCHelpMan dumpprivkey()
637
0
{
638
0
    return RPCHelpMan{"dumpprivkey",
639
0
                "\nReveals the private key corresponding to 'address'.\n"
640
0
                "Then the importprivkey can be used with this output\n"
641
0
                "Note: This command is only compatible with legacy wallets.\n",
642
0
                {
643
0
                    {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The bitcoin address for the private key"},
644
0
                },
645
0
                RPCResult{
646
0
                    RPCResult::Type::STR, "key", "The private key"
647
0
                },
648
0
                RPCExamples{
649
0
                    HelpExampleCli("dumpprivkey", "\"myaddress\"")
650
0
            + HelpExampleCli("importprivkey", "\"mykey\"")
651
0
            + HelpExampleRpc("dumpprivkey", "\"myaddress\"")
652
0
                },
653
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
654
0
{
655
0
    const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
656
0
    if (!pwallet) return UniValue::VNULL;
657
658
0
    const LegacyScriptPubKeyMan& spk_man = EnsureConstLegacyScriptPubKeyMan(*pwallet);
659
660
0
    LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
Line
Count
Source
259
0
    UniqueLock criticalblock1(MaybeCheckNotHeld(cs1), #cs1, __FILE__, __LINE__); \
260
0
    UniqueLock criticalblock2(MaybeCheckNotHeld(cs2), #cs2, __FILE__, __LINE__)
661
662
0
    EnsureWalletIsUnlocked(*pwallet);
663
664
0
    std::string strAddress = request.params[0].get_str();
665
0
    CTxDestination dest = DecodeDestination(strAddress);
666
0
    if (!IsValidDestination(dest)) {
667
0
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
668
0
    }
669
0
    auto keyid = GetKeyForDestination(spk_man, dest);
670
0
    if (keyid.IsNull()) {
671
0
        throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
672
0
    }
673
0
    CKey vchSecret;
674
0
    if (!spk_man.GetKey(keyid, vchSecret)) {
675
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
676
0
    }
677
0
    return EncodeSecret(vchSecret);
678
0
},
679
0
    };
680
0
}
681
682
683
RPCHelpMan dumpwallet()
684
0
{
685
0
    return RPCHelpMan{"dumpwallet",
686
0
                "\nDumps all wallet keys in a human-readable format to a server-side file. This does not allow overwriting existing files.\n"
687
0
                "Imported scripts are included in the dumpfile, but corresponding BIP173 addresses, etc. may not be added automatically by importwallet.\n"
688
0
                "Note that if your wallet contains keys which are not derived from your HD seed (e.g. imported keys), these are not covered by\n"
689
0
                "only backing up the seed itself, and must be backed up too (e.g. ensure you back up the whole dumpfile).\n"
690
0
                "Note: This command is only compatible with legacy wallets.\n",
691
0
                {
692
0
                    {"filename", RPCArg::Type::STR, RPCArg::Optional::NO, "The filename with path (absolute path recommended)"},
693
0
                },
694
0
                RPCResult{
695
0
                    RPCResult::Type::OBJ, "", "",
696
0
                    {
697
0
                        {RPCResult::Type::STR, "filename", "The filename with full absolute path"},
698
0
                    }
699
0
                },
700
0
                RPCExamples{
701
0
                    HelpExampleCli("dumpwallet", "\"test\"")
702
0
            + HelpExampleRpc("dumpwallet", "\"test\"")
703
0
                },
704
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
705
0
{
706
0
    const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
707
0
    if (!pwallet) return UniValue::VNULL;
708
709
0
    const CWallet& wallet = *pwallet;
710
0
    const LegacyScriptPubKeyMan& spk_man = EnsureConstLegacyScriptPubKeyMan(wallet);
711
712
    // Make sure the results are valid at least up to the most recent block
713
    // the user could have gotten from another RPC command prior to now
714
0
    wallet.BlockUntilSyncedToCurrentChain();
715
716
0
    LOCK(wallet.cs_wallet);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
717
718
0
    EnsureWalletIsUnlocked(wallet);
719
720
0
    fs::path filepath = fs::u8path(request.params[0].get_str());
721
0
    filepath = fs::absolute(filepath);
722
723
    /* Prevent arbitrary files from being overwritten. There have been reports
724
     * that users have overwritten wallet files this way:
725
     * https://github.com/bitcoin/bitcoin/issues/9934
726
     * It may also avoid other security issues.
727
     */
728
0
    if (fs::exists(filepath)) {
729
0
        throw JSONRPCError(RPC_INVALID_PARAMETER, filepath.utf8string() + " already exists. If you are sure this is what you want, move it out of the way first");
730
0
    }
731
732
0
    std::ofstream file;
733
0
    file.open(filepath);
734
0
    if (!file.is_open())
735
0
        throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
736
737
0
    std::map<CKeyID, int64_t> mapKeyBirth;
738
0
    wallet.GetKeyBirthTimes(mapKeyBirth);
739
740
0
    int64_t block_time = 0;
741
0
    CHECK_NONFATAL(wallet.chain().findBlock(wallet.GetLastBlockHash(), FoundBlock().time(block_time)));
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
742
743
    // Note: To avoid a lock order issue, access to cs_main must be locked before cs_KeyStore.
744
    // So we do the two things in this function that lock cs_main first: GetKeyBirthTimes, and findBlock.
745
0
    LOCK(spk_man.cs_KeyStore);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
746
747
0
    const std::map<CKeyID, int64_t>& mapKeyPool = spk_man.GetAllReserveKeys();
748
0
    std::set<CScriptID> scripts = spk_man.GetCScripts();
749
750
    // sort time/key pairs
751
0
    std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
752
0
    vKeyBirth.reserve(mapKeyBirth.size());
753
0
    for (const auto& entry : mapKeyBirth) {
754
0
        vKeyBirth.emplace_back(entry.second, entry.first);
755
0
    }
756
0
    mapKeyBirth.clear();
757
0
    std::sort(vKeyBirth.begin(), vKeyBirth.end());
758
759
    // produce output
760
0
    file << strprintf("# Wallet dump created by %s %s\n", CLIENT_NAME, FormatFullVersion());
Line
Count
Source
1172
0
#define strprintf tfm::format
    file << strprintf("# Wallet dump created by %s %s\n", CLIENT_NAME, FormatFullVersion());
Line
Count
Source
127
0
#define CLIENT_NAME "Bitcoin Core"
761
0
    file << strprintf("# * Created on %s\n", FormatISO8601DateTime(GetTime()));
Line
Count
Source
1172
0
#define strprintf tfm::format
762
0
    file << strprintf("# * Best block at time of backup was %i (%s),\n", wallet.GetLastBlockHeight(), wallet.GetLastBlockHash().ToString());
Line
Count
Source
1172
0
#define strprintf tfm::format
763
0
    file << strprintf("#   mined on %s\n", FormatISO8601DateTime(block_time));
Line
Count
Source
1172
0
#define strprintf tfm::format
764
0
    file << "\n";
765
766
    // add the base58check encoded extended master if the wallet uses HD
767
0
    CKeyID seed_id = spk_man.GetHDChain().seed_id;
768
0
    if (!seed_id.IsNull())
769
0
    {
770
0
        CKey seed;
771
0
        if (spk_man.GetKey(seed_id, seed)) {
772
0
            CExtKey masterKey;
773
0
            masterKey.SetSeed(seed);
774
775
0
            file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n\n";
776
0
        }
777
0
    }
778
0
    for (std::vector<std::pair<int64_t, CKeyID> >::const_iterator it = vKeyBirth.begin(); it != vKeyBirth.end(); it++) {
779
0
        const CKeyID &keyid = it->second;
780
0
        std::string strTime = FormatISO8601DateTime(it->first);
781
0
        std::string strAddr;
782
0
        std::string strLabel;
783
0
        CKey key;
784
0
        if (spk_man.GetKey(keyid, key)) {
785
0
            CKeyMetadata metadata;
786
0
            const auto it{spk_man.mapKeyMetadata.find(keyid)};
787
0
            if (it != spk_man.mapKeyMetadata.end()) metadata = it->second;
788
0
            file << strprintf("%s %s ", EncodeSecret(key), strTime);
Line
Count
Source
1172
0
#define strprintf tfm::format
789
0
            if (GetWalletAddressesForKey(&spk_man, wallet, keyid, strAddr, strLabel)) {
790
0
                file << strprintf("label=%s", strLabel);
Line
Count
Source
1172
0
#define strprintf tfm::format
791
0
            } else if (keyid == seed_id) {
792
0
                file << "hdseed=1";
793
0
            } else if (mapKeyPool.count(keyid)) {
794
0
                file << "reserve=1";
795
0
            } else if (metadata.hdKeypath == "s") {
796
0
                file << "inactivehdseed=1";
797
0
            } else {
798
0
                file << "change=1";
799
0
            }
800
0
            file << strprintf(" # addr=%s%s\n", strAddr, (metadata.has_key_origin ? " hdkeypath="+WriteHDKeypath(metadata.key_origin.path, /*apostrophe=*/true) : ""));
Line
Count
Source
1172
0
#define strprintf tfm::format
801
0
        }
802
0
    }
803
0
    file << "\n";
804
0
    for (const CScriptID &scriptid : scripts) {
805
0
        CScript script;
806
0
        std::string create_time = "0";
807
0
        std::string address = EncodeDestination(ScriptHash(scriptid));
808
        // get birth times for scripts with metadata
809
0
        auto it = spk_man.m_script_metadata.find(scriptid);
810
0
        if (it != spk_man.m_script_metadata.end()) {
811
0
            create_time = FormatISO8601DateTime(it->second.nCreateTime);
812
0
        }
813
0
        if(spk_man.GetCScript(scriptid, script)) {
814
0
            file << strprintf("%s %s script=1", HexStr(script), create_time);
Line
Count
Source
1172
0
#define strprintf tfm::format
815
0
            file << strprintf(" # addr=%s\n", address);
Line
Count
Source
1172
0
#define strprintf tfm::format
816
0
        }
817
0
    }
818
0
    file << "\n";
819
0
    file << "# End of dump\n";
820
0
    file.close();
821
822
0
    UniValue reply(UniValue::VOBJ);
823
0
    reply.pushKV("filename", filepath.utf8string());
824
825
0
    return reply;
826
0
},
827
0
    };
828
0
}
829
830
struct ImportData
831
{
832
    // Input data
833
    std::unique_ptr<CScript> redeemscript; //!< Provided redeemScript; will be moved to `import_scripts` if relevant.
834
    std::unique_ptr<CScript> witnessscript; //!< Provided witnessScript; will be moved to `import_scripts` if relevant.
835
836
    // Output data
837
    std::set<CScript> import_scripts;
838
    std::map<CKeyID, bool> used_keys; //!< Import these private keys if available (the value indicates whether if the key is required for solvability)
839
    std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> key_origins;
840
};
841
842
enum class ScriptContext
843
{
844
    TOP, //!< Top-level scriptPubKey
845
    P2SH, //!< P2SH redeemScript
846
    WITNESS_V0, //!< P2WSH witnessScript
847
};
848
849
// Analyse the provided scriptPubKey, determining which keys and which redeem scripts from the ImportData struct are needed to spend it, and mark them as used.
850
// Returns an error string, or the empty string for success.
851
// NOLINTNEXTLINE(misc-no-recursion)
852
static std::string RecurseImportData(const CScript& script, ImportData& import_data, const ScriptContext script_ctx)
853
0
{
854
    // Use Solver to obtain script type and parsed pubkeys or hashes:
855
0
    std::vector<std::vector<unsigned char>> solverdata;
856
0
    TxoutType script_type = Solver(script, solverdata);
857
858
0
    switch (script_type) {
859
0
    case TxoutType::PUBKEY: {
860
0
        CPubKey pubkey(solverdata[0]);
861
0
        import_data.used_keys.emplace(pubkey.GetID(), false);
862
0
        return "";
863
0
    }
864
0
    case TxoutType::PUBKEYHASH: {
865
0
        CKeyID id = CKeyID(uint160(solverdata[0]));
866
0
        import_data.used_keys[id] = true;
867
0
        return "";
868
0
    }
869
0
    case TxoutType::SCRIPTHASH: {
870
0
        if (script_ctx == ScriptContext::P2SH) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2SH inside another P2SH");
871
0
        if (script_ctx == ScriptContext::WITNESS_V0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2SH inside a P2WSH");
872
0
        CHECK_NONFATAL(script_ctx == ScriptContext::TOP);
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
873
0
        CScriptID id = CScriptID(uint160(solverdata[0]));
874
0
        auto subscript = std::move(import_data.redeemscript); // Remove redeemscript from import_data to check for superfluous script later.
875
0
        if (!subscript) return "missing redeemscript";
876
0
        if (CScriptID(*subscript) != id) return "redeemScript does not match the scriptPubKey";
877
0
        import_data.import_scripts.emplace(*subscript);
878
0
        return RecurseImportData(*subscript, import_data, ScriptContext::P2SH);
879
0
    }
880
0
    case TxoutType::MULTISIG: {
881
0
        for (size_t i = 1; i + 1< solverdata.size(); ++i) {
882
0
            CPubKey pubkey(solverdata[i]);
883
0
            import_data.used_keys.emplace(pubkey.GetID(), false);
884
0
        }
885
0
        return "";
886
0
    }
887
0
    case TxoutType::WITNESS_V0_SCRIPTHASH: {
888
0
        if (script_ctx == ScriptContext::WITNESS_V0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2WSH inside another P2WSH");
889
0
        CScriptID id{RIPEMD160(solverdata[0])};
890
0
        auto subscript = std::move(import_data.witnessscript); // Remove redeemscript from import_data to check for superfluous script later.
891
0
        if (!subscript) return "missing witnessscript";
892
0
        if (CScriptID(*subscript) != id) return "witnessScript does not match the scriptPubKey or redeemScript";
893
0
        if (script_ctx == ScriptContext::TOP) {
894
0
            import_data.import_scripts.emplace(script); // Special rule for IsMine: native P2WSH requires the TOP script imported (see script/ismine.cpp)
895
0
        }
896
0
        import_data.import_scripts.emplace(*subscript);
897
0
        return RecurseImportData(*subscript, import_data, ScriptContext::WITNESS_V0);
898
0
    }
899
0
    case TxoutType::WITNESS_V0_KEYHASH: {
900
0
        if (script_ctx == ScriptContext::WITNESS_V0) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Trying to nest P2WPKH inside P2WSH");
901
0
        CKeyID id = CKeyID(uint160(solverdata[0]));
902
0
        import_data.used_keys[id] = true;
903
0
        if (script_ctx == ScriptContext::TOP) {
904
0
            import_data.import_scripts.emplace(script); // Special rule for IsMine: native P2WPKH requires the TOP script imported (see script/ismine.cpp)
905
0
        }
906
0
        return "";
907
0
    }
908
0
    case TxoutType::NULL_DATA:
909
0
        return "unspendable script";
910
0
    case TxoutType::NONSTANDARD:
911
0
    case TxoutType::WITNESS_UNKNOWN:
912
0
    case TxoutType::WITNESS_V1_TAPROOT:
913
0
    case TxoutType::ANCHOR:
914
0
        return "unrecognized script";
915
0
    } // no default case, so the compiler can warn about missing cases
916
0
    NONFATAL_UNREACHABLE();
Line
Count
Source
124
0
    throw NonFatalCheckError(                                         \
125
0
        "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__)
917
0
}
918
919
static UniValue ProcessImportLegacy(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<std::pair<CKeyID, bool>>& ordered_pubkeys)
920
0
{
921
0
    UniValue warnings(UniValue::VARR);
922
923
    // First ensure scriptPubKey has either a script or JSON with "address" string
924
0
    const UniValue& scriptPubKey = data["scriptPubKey"];
925
0
    bool isScript = scriptPubKey.getType() == UniValue::VSTR;
926
0
    if (!isScript && !(scriptPubKey.getType() == UniValue::VOBJ && scriptPubKey.exists("address"))) {
927
0
        throw JSONRPCError(RPC_INVALID_PARAMETER, "scriptPubKey must be string with script or JSON with address string");
928
0
    }
929
0
    const std::string& output = isScript ? scriptPubKey.get_str() : scriptPubKey["address"].get_str();
930
931
    // Optional fields.
932
0
    const std::string& strRedeemScript = data.exists("redeemscript") ? data["redeemscript"].get_str() : "";
933
0
    const std::string& witness_script_hex = data.exists("witnessscript") ? data["witnessscript"].get_str() : "";
934
0
    const UniValue& pubKeys = data.exists("pubkeys") ? data["pubkeys"].get_array() : UniValue();
935
0
    const UniValue& keys = data.exists("keys") ? data["keys"].get_array() : UniValue();
936
0
    const bool internal = data.exists("internal") ? data["internal"].get_bool() : false;
937
0
    const bool watchOnly = data.exists("watchonly") ? data["watchonly"].get_bool() : false;
938
939
0
    if (data.exists("range")) {
940
0
        throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for a non-descriptor import");
941
0
    }
942
943
    // Generate the script and destination for the scriptPubKey provided
944
0
    CScript script;
945
0
    if (!isScript) {
946
0
        CTxDestination dest = DecodeDestination(output);
947
0
        if (!IsValidDestination(dest)) {
948
0
            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address \"" + output + "\"");
949
0
        }
950
0
        if (OutputTypeFromDestination(dest) == OutputType::BECH32M) {
951
0
            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Bech32m addresses cannot be imported into legacy wallets");
952
0
        }
953
0
        script = GetScriptForDestination(dest);
954
0
    } else {
955
0
        if (!IsHex(output)) {
956
0
            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid scriptPubKey \"" + output + "\"");
957
0
        }
958
0
        std::vector<unsigned char> vData(ParseHex(output));
959
0
        script = CScript(vData.begin(), vData.end());
960
0
        CTxDestination dest;
961
0
        if (!ExtractDestination(script, dest) && !internal) {
962
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Internal must be set to true for nonstandard scriptPubKey imports.");
963
0
        }
964
0
    }
965
0
    script_pub_keys.emplace(script);
966
967
    // Parse all arguments
968
0
    if (strRedeemScript.size()) {
969
0
        if (!IsHex(strRedeemScript)) {
970
0
            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid redeem script \"" + strRedeemScript + "\": must be hex string");
971
0
        }
972
0
        auto parsed_redeemscript = ParseHex(strRedeemScript);
973
0
        import_data.redeemscript = std::make_unique<CScript>(parsed_redeemscript.begin(), parsed_redeemscript.end());
974
0
    }
975
0
    if (witness_script_hex.size()) {
976
0
        if (!IsHex(witness_script_hex)) {
977
0
            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid witness script \"" + witness_script_hex + "\": must be hex string");
978
0
        }
979
0
        auto parsed_witnessscript = ParseHex(witness_script_hex);
980
0
        import_data.witnessscript = std::make_unique<CScript>(parsed_witnessscript.begin(), parsed_witnessscript.end());
981
0
    }
982
0
    for (size_t i = 0; i < pubKeys.size(); ++i) {
983
0
        CPubKey pubkey = HexToPubKey(pubKeys[i].get_str());
984
0
        pubkey_map.emplace(pubkey.GetID(), pubkey);
985
0
        ordered_pubkeys.emplace_back(pubkey.GetID(), internal);
986
0
    }
987
0
    for (size_t i = 0; i < keys.size(); ++i) {
988
0
        const auto& str = keys[i].get_str();
989
0
        CKey key = DecodeSecret(str);
990
0
        if (!key.IsValid()) {
991
0
            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
992
0
        }
993
0
        CPubKey pubkey = key.GetPubKey();
994
0
        CKeyID id = pubkey.GetID();
995
0
        if (pubkey_map.count(id)) {
996
0
            pubkey_map.erase(id);
997
0
        }
998
0
        privkey_map.emplace(id, key);
999
0
    }
1000
1001
1002
    // Verify and process input data
1003
0
    have_solving_data = import_data.redeemscript || import_data.witnessscript || pubkey_map.size() || privkey_map.size();
1004
0
    if (have_solving_data) {
1005
        // Match up data in import_data with the scriptPubKey in script.
1006
0
        auto error = RecurseImportData(script, import_data, ScriptContext::TOP);
1007
1008
        // Verify whether the watchonly option corresponds to the availability of private keys.
1009
0
        bool spendable = std::all_of(import_data.used_keys.begin(), import_data.used_keys.end(), [&](const std::pair<CKeyID, bool>& used_key){ return privkey_map.count(used_key.first) > 0; });
1010
0
        if (!watchOnly && !spendable) {
1011
0
            warnings.push_back("Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag.");
1012
0
        }
1013
0
        if (watchOnly && spendable) {
1014
0
            warnings.push_back("All private keys are provided, outputs will be considered spendable. If this is intentional, do not specify the watchonly flag.");
1015
0
        }
1016
1017
        // Check that all required keys for solvability are provided.
1018
0
        if (error.empty()) {
1019
0
            for (const auto& require_key : import_data.used_keys) {
1020
0
                if (!require_key.second) continue; // Not a required key
1021
0
                if (pubkey_map.count(require_key.first) == 0 && privkey_map.count(require_key.first) == 0) {
1022
0
                    error = "some required keys are missing";
1023
0
                }
1024
0
            }
1025
0
        }
1026
1027
0
        if (!error.empty()) {
1028
0
            warnings.push_back("Importing as non-solvable: " + error + ". If this is intentional, don't provide any keys, pubkeys, witnessscript, or redeemscript.");
1029
0
            import_data = ImportData();
1030
0
            pubkey_map.clear();
1031
0
            privkey_map.clear();
1032
0
            have_solving_data = false;
1033
0
        } else {
1034
            // RecurseImportData() removes any relevant redeemscript/witnessscript from import_data, so we can use that to discover if a superfluous one was provided.
1035
0
            if (import_data.redeemscript) warnings.push_back("Ignoring redeemscript as this is not a P2SH script.");
1036
0
            if (import_data.witnessscript) warnings.push_back("Ignoring witnessscript as this is not a (P2SH-)P2WSH script.");
1037
0
            for (auto it = privkey_map.begin(); it != privkey_map.end(); ) {
1038
0
                auto oldit = it++;
1039
0
                if (import_data.used_keys.count(oldit->first) == 0) {
1040
0
                    warnings.push_back("Ignoring irrelevant private key.");
1041
0
                    privkey_map.erase(oldit);
1042
0
                }
1043
0
            }
1044
0
            for (auto it = pubkey_map.begin(); it != pubkey_map.end(); ) {
1045
0
                auto oldit = it++;
1046
0
                auto key_data_it = import_data.used_keys.find(oldit->first);
1047
0
                if (key_data_it == import_data.used_keys.end() || !key_data_it->second) {
1048
0
                    warnings.push_back("Ignoring public key \"" + HexStr(oldit->first) + "\" as it doesn't appear inside P2PKH or P2WPKH.");
1049
0
                    pubkey_map.erase(oldit);
1050
0
                }
1051
0
            }
1052
0
        }
1053
0
    }
1054
1055
0
    return warnings;
1056
0
}
1057
1058
static UniValue ProcessImportDescriptor(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<std::pair<CKeyID, bool>>& ordered_pubkeys)
1059
0
{
1060
0
    UniValue warnings(UniValue::VARR);
1061
1062
0
    const std::string& descriptor = data["desc"].get_str();
1063
0
    FlatSigningProvider keys;
1064
0
    std::string error;
1065
0
    auto parsed_descs = Parse(descriptor, keys, error, /* require_checksum = */ true);
1066
0
    if (parsed_descs.empty()) {
1067
0
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
1068
0
    }
1069
0
    if (parsed_descs.at(0)->GetOutputType() == OutputType::BECH32M) {
1070
0
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Bech32m descriptors cannot be imported into legacy wallets");
1071
0
    }
1072
1073
0
    std::optional<bool> internal;
1074
0
    if (data.exists("internal")) {
1075
0
        if (parsed_descs.size() > 1) {
1076
0
            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot have multipath descriptor while also specifying \'internal\'");
1077
0
        }
1078
0
        internal = data["internal"].get_bool();
1079
0
    }
1080
1081
0
    have_solving_data = parsed_descs.at(0)->IsSolvable();
1082
0
    const bool watch_only = data.exists("watchonly") ? data["watchonly"].get_bool() : false;
1083
1084
0
    int64_t range_start = 0, range_end = 0;
1085
0
    if (!parsed_descs.at(0)->IsRange() && data.exists("range")) {
1086
0
        throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for an un-ranged descriptor");
1087
0
    } else if (parsed_descs.at(0)->IsRange()) {
1088
0
        if (!data.exists("range")) {
1089
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor is ranged, please specify the range");
1090
0
        }
1091
0
        std::tie(range_start, range_end) = ParseDescriptorRange(data["range"]);
1092
0
    }
1093
1094
    // Only single key descriptors are allowed to be imported to a legacy wallet's keypool
1095
0
    bool can_keypool = parsed_descs.at(0)->IsSingleKey();
1096
1097
0
    const UniValue& priv_keys = data.exists("keys") ? data["keys"].get_array() : UniValue();
1098
1099
0
    for (size_t j = 0; j < parsed_descs.size(); ++j) {
1100
0
        const auto& parsed_desc = parsed_descs.at(j);
1101
0
        bool desc_internal = internal.has_value() && internal.value();
1102
0
        if (parsed_descs.size() == 2) {
1103
0
            desc_internal = j == 1;
1104
0
        } else if (parsed_descs.size() > 2) {
1105
0
            CHECK_NONFATAL(!desc_internal);
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
1106
0
        }
1107
        // Expand all descriptors to get public keys and scripts, and private keys if available.
1108
0
        for (int i = range_start; i <= range_end; ++i) {
1109
0
            FlatSigningProvider out_keys;
1110
0
            std::vector<CScript> scripts_temp;
1111
0
            parsed_desc->Expand(i, keys, scripts_temp, out_keys);
1112
0
            std::copy(scripts_temp.begin(), scripts_temp.end(), std::inserter(script_pub_keys, script_pub_keys.end()));
1113
0
            if (can_keypool) {
1114
0
                for (const auto& key_pair : out_keys.pubkeys) {
1115
0
                    ordered_pubkeys.emplace_back(key_pair.first, desc_internal);
1116
0
                }
1117
0
            }
1118
1119
0
            for (const auto& x : out_keys.scripts) {
1120
0
                import_data.import_scripts.emplace(x.second);
1121
0
            }
1122
1123
0
            parsed_desc->ExpandPrivate(i, keys, out_keys);
1124
1125
0
            std::copy(out_keys.pubkeys.begin(), out_keys.pubkeys.end(), std::inserter(pubkey_map, pubkey_map.end()));
1126
0
            std::copy(out_keys.keys.begin(), out_keys.keys.end(), std::inserter(privkey_map, privkey_map.end()));
1127
0
            import_data.key_origins.insert(out_keys.origins.begin(), out_keys.origins.end());
1128
0
        }
1129
0
    }
1130
1131
0
    for (size_t i = 0; i < priv_keys.size(); ++i) {
1132
0
        const auto& str = priv_keys[i].get_str();
1133
0
        CKey key = DecodeSecret(str);
1134
0
        if (!key.IsValid()) {
1135
0
            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
1136
0
        }
1137
0
        CPubKey pubkey = key.GetPubKey();
1138
0
        CKeyID id = pubkey.GetID();
1139
1140
        // Check if this private key corresponds to a public key from the descriptor
1141
0
        if (!pubkey_map.count(id)) {
1142
0
            warnings.push_back("Ignoring irrelevant private key.");
1143
0
        } else {
1144
0
            privkey_map.emplace(id, key);
1145
0
        }
1146
0
    }
1147
1148
    // Check if all the public keys have corresponding private keys in the import for spendability.
1149
    // This does not take into account threshold multisigs which could be spendable without all keys.
1150
    // Thus, threshold multisigs without all keys will be considered not spendable here, even if they are,
1151
    // perhaps triggering a false warning message. This is consistent with the current wallet IsMine check.
1152
0
    bool spendable = std::all_of(pubkey_map.begin(), pubkey_map.end(),
1153
0
        [&](const std::pair<CKeyID, CPubKey>& used_key) {
1154
0
            return privkey_map.count(used_key.first) > 0;
1155
0
        }) && std::all_of(import_data.key_origins.begin(), import_data.key_origins.end(),
1156
0
        [&](const std::pair<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& entry) {
1157
0
            return privkey_map.count(entry.first) > 0;
1158
0
        });
1159
0
    if (!watch_only && !spendable) {
1160
0
        warnings.push_back("Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag.");
1161
0
    }
1162
0
    if (watch_only && spendable) {
1163
0
        warnings.push_back("All private keys are provided, outputs will be considered spendable. If this is intentional, do not specify the watchonly flag.");
1164
0
    }
1165
1166
0
    return warnings;
1167
0
}
1168
1169
static UniValue ProcessImport(CWallet& wallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
1170
0
{
1171
0
    UniValue warnings(UniValue::VARR);
1172
0
    UniValue result(UniValue::VOBJ);
1173
1174
0
    try {
1175
0
        const bool internal = data.exists("internal") ? data["internal"].get_bool() : false;
1176
        // Internal addresses should not have a label
1177
0
        if (internal && data.exists("label")) {
1178
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Internal addresses should not have a label");
1179
0
        }
1180
0
        const std::string label{LabelFromValue(data["label"])};
1181
0
        const bool add_keypool = data.exists("keypool") ? data["keypool"].get_bool() : false;
1182
1183
        // Add to keypool only works with privkeys disabled
1184
0
        if (add_keypool && !wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
1185
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Keys can only be imported to the keypool when private keys are disabled");
1186
0
        }
1187
1188
0
        ImportData import_data;
1189
0
        std::map<CKeyID, CPubKey> pubkey_map;
1190
0
        std::map<CKeyID, CKey> privkey_map;
1191
0
        std::set<CScript> script_pub_keys;
1192
0
        std::vector<std::pair<CKeyID, bool>> ordered_pubkeys;
1193
0
        bool have_solving_data;
1194
1195
0
        if (data.exists("scriptPubKey") && data.exists("desc")) {
1196
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Both a descriptor and a scriptPubKey should not be provided.");
1197
0
        } else if (data.exists("scriptPubKey")) {
1198
0
            warnings = ProcessImportLegacy(import_data, pubkey_map, privkey_map, script_pub_keys, have_solving_data, data, ordered_pubkeys);
1199
0
        } else if (data.exists("desc")) {
1200
0
            warnings = ProcessImportDescriptor(import_data, pubkey_map, privkey_map, script_pub_keys, have_solving_data, data, ordered_pubkeys);
1201
0
        } else {
1202
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Either a descriptor or scriptPubKey must be provided.");
1203
0
        }
1204
1205
        // If private keys are disabled, abort if private keys are being imported
1206
0
        if (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !privkey_map.empty()) {
1207
0
            throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
1208
0
        }
1209
1210
        // Check whether we have any work to do
1211
0
        for (const CScript& script : script_pub_keys) {
1212
0
            if (wallet.IsMine(script) & ISMINE_SPENDABLE) {
1213
0
                throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script (\"" + HexStr(script) + "\")");
1214
0
            }
1215
0
        }
1216
1217
        // All good, time to import
1218
0
        wallet.MarkDirty();
1219
0
        if (!wallet.ImportScripts(import_data.import_scripts, timestamp)) {
1220
0
            throw JSONRPCError(RPC_WALLET_ERROR, "Error adding script to wallet");
1221
0
        }
1222
0
        if (!wallet.ImportPrivKeys(privkey_map, timestamp)) {
1223
0
            throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
1224
0
        }
1225
0
        if (!wallet.ImportPubKeys(ordered_pubkeys, pubkey_map, import_data.key_origins, add_keypool, timestamp)) {
1226
0
            throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
1227
0
        }
1228
0
        if (!wallet.ImportScriptPubKeys(label, script_pub_keys, have_solving_data, !internal, timestamp)) {
1229
0
            throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
1230
0
        }
1231
1232
0
        result.pushKV("success", UniValue(true));
1233
0
    } catch (const UniValue& e) {
1234
0
        result.pushKV("success", UniValue(false));
1235
0
        result.pushKV("error", e);
1236
0
    } catch (...) {
1237
0
        result.pushKV("success", UniValue(false));
1238
1239
0
        result.pushKV("error", JSONRPCError(RPC_MISC_ERROR, "Missing required fields"));
1240
0
    }
1241
0
    PushWarnings(warnings, result);
1242
0
    return result;
1243
0
}
1244
1245
static int64_t GetImportTimestamp(const UniValue& data, int64_t now)
1246
0
{
1247
0
    if (data.exists("timestamp")) {
1248
0
        const UniValue& timestamp = data["timestamp"];
1249
0
        if (timestamp.isNum()) {
1250
0
            return timestamp.getInt<int64_t>();
1251
0
        } else if (timestamp.isStr() && timestamp.get_str() == "now") {
1252
0
            return now;
1253
0
        }
1254
0
        throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Expected number or \"now\" timestamp value for key. got type %s", uvTypeName(timestamp.type())));
Line
Count
Source
1172
0
#define strprintf tfm::format
1255
0
    }
1256
0
    throw JSONRPCError(RPC_TYPE_ERROR, "Missing required timestamp field for key");
1257
0
}
1258
1259
RPCHelpMan importmulti()
1260
0
{
1261
0
    return RPCHelpMan{"importmulti",
1262
0
                "\nImport addresses/scripts (with private or public keys, redeem script (P2SH)), optionally rescanning the blockchain from the earliest creation time of the imported scripts. Requires a new wallet backup.\n"
1263
0
                "If an address/script is imported without all of the private keys required to spend from that address, it will be watchonly. The 'watchonly' option must be set to true in this case or a warning will be returned.\n"
1264
0
                "Conversely, if all the private keys are provided and the address/script is spendable, the watchonly option must be set to false, or a warning will be returned.\n"
1265
0
            "\nNote: This call can take over an hour to complete if rescan is true, during that time, other rpc calls\n"
1266
0
            "may report that the imported keys, addresses or scripts exist but related transactions are still missing.\n"
1267
0
            "The rescan parameter can be set to false if the key was never used to create transactions. If it is set to false,\n"
1268
0
            "but the key was used to create transactions, rescanblockchain needs to be called with the appropriate block range.\n"
1269
0
            "Note: Use \"getwalletinfo\" to query the scanning progress.\n"
1270
0
            "Note: This command is only compatible with legacy wallets. Use \"importdescriptors\" for descriptor wallets.\n",
1271
0
                {
1272
0
                    {"requests", RPCArg::Type::ARR, RPCArg::Optional::NO, "Data to be imported",
1273
0
                        {
1274
0
                            {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
1275
0
                                {
1276
0
                                    {"desc", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Descriptor to import. If using descriptor, do not also provide address/scriptPubKey, scripts, or pubkeys"},
1277
0
                                    {"scriptPubKey", RPCArg::Type::STR, RPCArg::Optional::NO, "Type of scriptPubKey (string for script, json for address). Should not be provided if using a descriptor",
1278
0
                                        RPCArgOptions{.type_str={"\"<script>\" | { \"address\":\"<address>\" }", "string / json"}}
1279
0
                                    },
1280
0
                                    {"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, "Creation time of the key expressed in " + UNIX_EPOCH_TIME + ",\n"
1281
0
                                        "or the string \"now\" to substitute the current synced blockchain time. The timestamp of the oldest\n"
1282
0
                                        "key will determine how far back blockchain rescans need to begin for missing wallet transactions.\n"
1283
0
                                        "\"now\" can be specified to bypass scanning, for keys which are known to never have been used, and\n"
1284
0
                                        "0 can be specified to scan the entire blockchain. Blocks up to 2 hours before the earliest key\n"
1285
0
                                        "creation time of all keys being imported by the importmulti call will be scanned.",
1286
0
                                        RPCArgOptions{.type_str={"timestamp | \"now\"", "integer / string"}}
1287
0
                                    },
1288
0
                                    {"redeemscript", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Allowed only if the scriptPubKey is a P2SH or P2SH-P2WSH address/scriptPubKey"},
1289
0
                                    {"witnessscript", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Allowed only if the scriptPubKey is a P2SH-P2WSH or P2WSH address/scriptPubKey"},
1290
0
                                    {"pubkeys", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "Array of strings giving pubkeys to import. They must occur in P2PKH or P2WPKH scripts. They are not required when the private key is also provided (see the \"keys\" argument).",
1291
0
                                        {
1292
0
                                            {"pubKey", RPCArg::Type::STR, RPCArg::Optional::OMITTED, ""},
1293
0
                                        }
1294
0
                                    },
1295
0
                                    {"keys", RPCArg::Type::ARR, RPCArg::Default{UniValue::VARR}, "Array of strings giving private keys to import. The corresponding public keys must occur in the output or redeemscript.",
1296
0
                                        {
1297
0
                                            {"key", RPCArg::Type::STR, RPCArg::Optional::OMITTED, ""},
1298
0
                                        }
1299
0
                                    },
1300
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"},
1301
0
                                    {"internal", RPCArg::Type::BOOL, RPCArg::Default{false}, "Stating whether matching outputs should be treated as not incoming payments (also known as change)"},
1302
0
                                    {"watchonly", RPCArg::Type::BOOL, RPCArg::Default{false}, "Stating whether matching outputs should be considered watchonly."},
1303
0
                                    {"label", RPCArg::Type::STR, RPCArg::Default{""}, "Label to assign to the address, only allowed with internal=false"},
1304
0
                                    {"keypool", RPCArg::Type::BOOL, RPCArg::Default{false}, "Stating whether imported public keys should be added to the keypool for when users request new addresses. Only allowed when wallet private keys are disabled"},
1305
0
                                },
1306
0
                            },
1307
0
                        },
1308
0
                        RPCArgOptions{.oneline_description="requests"}},
1309
0
                    {"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "",
1310
0
                        {
1311
0
                            {"rescan", RPCArg::Type::BOOL, RPCArg::Default{true}, "Scan the chain and mempool for wallet transactions after all imports."},
1312
0
                        },
1313
0
                        RPCArgOptions{.oneline_description="options"}},
1314
0
                },
1315
0
                RPCResult{
1316
0
                    RPCResult::Type::ARR, "", "Response is an array with the same size as the input that has the execution result",
1317
0
                    {
1318
0
                        {RPCResult::Type::OBJ, "", "",
1319
0
                        {
1320
0
                            {RPCResult::Type::BOOL, "success", ""},
1321
0
                            {RPCResult::Type::ARR, "warnings", /*optional=*/true, "",
1322
0
                            {
1323
0
                                {RPCResult::Type::STR, "", ""},
1324
0
                            }},
1325
0
                            {RPCResult::Type::OBJ, "error", /*optional=*/true, "",
1326
0
                            {
1327
0
                                {RPCResult::Type::ELISION, "", "JSONRPC error"},
1328
0
                            }},
1329
0
                        }},
1330
0
                    }
1331
0
                },
1332
0
                RPCExamples{
1333
0
                    HelpExampleCli("importmulti", "'[{ \"scriptPubKey\": { \"address\": \"<my address>\" }, \"timestamp\":1455191478 }, "
1334
0
                                          "{ \"scriptPubKey\": { \"address\": \"<my 2nd address>\" }, \"label\": \"example 2\", \"timestamp\": 1455191480 }]'") +
1335
0
                    HelpExampleCli("importmulti", "'[{ \"scriptPubKey\": { \"address\": \"<my address>\" }, \"timestamp\":1455191478 }]' '{ \"rescan\": false}'")
1336
0
                },
1337
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& mainRequest) -> UniValue
1338
0
{
1339
0
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(mainRequest);
1340
0
    if (!pwallet) return UniValue::VNULL;
1341
0
    CWallet& wallet{*pwallet};
1342
1343
    // Make sure the results are valid at least up to the most recent block
1344
    // the user could have gotten from another RPC command prior to now
1345
0
    wallet.BlockUntilSyncedToCurrentChain();
1346
1347
0
    EnsureLegacyScriptPubKeyMan(*pwallet, true);
1348
1349
0
    const UniValue& requests = mainRequest.params[0];
1350
1351
    //Default options
1352
0
    bool fRescan = true;
1353
1354
0
    if (!mainRequest.params[1].isNull()) {
1355
0
        const UniValue& options = mainRequest.params[1];
1356
1357
0
        if (options.exists("rescan")) {
1358
0
            fRescan = options["rescan"].get_bool();
1359
0
        }
1360
0
    }
1361
1362
0
    WalletRescanReserver reserver(*pwallet);
1363
0
    if (fRescan && !reserver.reserve()) {
1364
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
1365
0
    }
1366
1367
0
    int64_t now = 0;
1368
0
    bool fRunScan = false;
1369
0
    int64_t nLowestTimestamp = 0;
1370
0
    UniValue response(UniValue::VARR);
1371
0
    {
1372
0
        LOCK(pwallet->cs_wallet);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
1373
1374
        // Check all requests are watchonly
1375
0
        bool is_watchonly{true};
1376
0
        for (size_t i = 0; i < requests.size(); ++i) {
1377
0
            const UniValue& request = requests[i];
1378
0
            if (!request.exists("watchonly") || !request["watchonly"].get_bool()) {
1379
0
                is_watchonly = false;
1380
0
                break;
1381
0
            }
1382
0
        }
1383
        // Wallet does not need to be unlocked if all requests are watchonly
1384
0
        if (!is_watchonly) EnsureWalletIsUnlocked(wallet);
1385
1386
        // Verify all timestamps are present before importing any keys.
1387
0
        CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(nLowestTimestamp).mtpTime(now)));
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
1388
0
        for (const UniValue& data : requests.getValues()) {
1389
0
            GetImportTimestamp(data, now);
1390
0
        }
1391
1392
0
        const int64_t minimumTimestamp = 1;
1393
1394
0
        for (const UniValue& data : requests.getValues()) {
1395
0
            const int64_t timestamp = std::max(GetImportTimestamp(data, now), minimumTimestamp);
1396
0
            const UniValue result = ProcessImport(*pwallet, data, timestamp);
1397
0
            response.push_back(result);
1398
1399
0
            if (!fRescan) {
1400
0
                continue;
1401
0
            }
1402
1403
            // If at least one request was successful then allow rescan.
1404
0
            if (result["success"].get_bool()) {
1405
0
                fRunScan = true;
1406
0
            }
1407
1408
            // Get the lowest timestamp.
1409
0
            if (timestamp < nLowestTimestamp) {
1410
0
                nLowestTimestamp = timestamp;
1411
0
            }
1412
0
        }
1413
0
    }
1414
0
    if (fRescan && fRunScan && requests.size()) {
1415
0
        int64_t scannedTime = pwallet->RescanFromTime(nLowestTimestamp, reserver, /*update=*/true);
1416
0
        pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
1417
1418
0
        if (pwallet->IsAbortingRescan()) {
1419
0
            throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");
1420
0
        }
1421
0
        if (scannedTime > nLowestTimestamp) {
1422
0
            std::vector<UniValue> results = response.getValues();
1423
0
            response.clear();
1424
0
            response.setArray();
1425
0
            size_t i = 0;
1426
0
            for (const UniValue& request : requests.getValues()) {
1427
                // If key creation date is within the successfully scanned
1428
                // range, or if the import result already has an error set, let
1429
                // the result stand unmodified. Otherwise replace the result
1430
                // with an error message.
1431
0
                if (scannedTime <= GetImportTimestamp(request, now) || results.at(i).exists("error")) {
1432
0
                    response.push_back(results.at(i));
1433
0
                } else {
1434
0
                    UniValue result = UniValue(UniValue::VOBJ);
1435
0
                    result.pushKV("success", UniValue(false));
1436
0
                    result.pushKV(
1437
0
                        "error",
1438
0
                        JSONRPCError(
1439
0
                            RPC_MISC_ERROR,
1440
0
                            strprintf("Rescan failed for key with creation timestamp %d. There was an error reading a "
Line
Count
Source
1172
0
#define strprintf tfm::format
1441
0
                                      "block from time %d, which is after or within %d seconds of key creation, and "
1442
0
                                      "could contain transactions pertaining to the key. As a result, transactions "
1443
0
                                      "and coins using this key may not appear in the wallet. This error could be "
1444
0
                                      "caused by pruning or data corruption (see bitcoind log for details) and could "
1445
0
                                      "be dealt with by downloading and rescanning the relevant blocks (see -reindex "
1446
0
                                      "option and rescanblockchain RPC).",
1447
0
                                GetImportTimestamp(request, now), scannedTime - TIMESTAMP_WINDOW - 1, TIMESTAMP_WINDOW)));
1448
0
                    response.push_back(std::move(result));
1449
0
                }
1450
0
                ++i;
1451
0
            }
1452
0
        }
1453
0
    }
1454
1455
0
    return response;
1456
0
},
1457
0
    };
1458
0
}
1459
1460
static UniValue ProcessDescriptorImport(CWallet& wallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
1461
0
{
1462
0
    UniValue warnings(UniValue::VARR);
1463
0
    UniValue result(UniValue::VOBJ);
1464
1465
0
    try {
1466
0
        if (!data.exists("desc")) {
1467
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor not found.");
1468
0
        }
1469
1470
0
        const std::string& descriptor = data["desc"].get_str();
1471
0
        const bool active = data.exists("active") ? data["active"].get_bool() : false;
1472
0
        const std::string label{LabelFromValue(data["label"])};
1473
1474
        // Parse descriptor string
1475
0
        FlatSigningProvider keys;
1476
0
        std::string error;
1477
0
        auto parsed_descs = Parse(descriptor, keys, error, /* require_checksum = */ true);
1478
0
        if (parsed_descs.empty()) {
1479
0
            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
1480
0
        }
1481
0
        std::optional<bool> internal;
1482
0
        if (data.exists("internal")) {
1483
0
            if (parsed_descs.size() > 1) {
1484
0
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot have multipath descriptor while also specifying \'internal\'");
1485
0
            }
1486
0
            internal = data["internal"].get_bool();
1487
0
        }
1488
1489
        // Range check
1490
0
        int64_t range_start = 0, range_end = 1, next_index = 0;
1491
0
        if (!parsed_descs.at(0)->IsRange() && data.exists("range")) {
1492
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for an un-ranged descriptor");
1493
0
        } else if (parsed_descs.at(0)->IsRange()) {
1494
0
            if (data.exists("range")) {
1495
0
                auto range = ParseDescriptorRange(data["range"]);
1496
0
                range_start = range.first;
1497
0
                range_end = range.second + 1; // Specified range end is inclusive, but we need range end as exclusive
1498
0
            } else {
1499
0
                warnings.push_back("Range not given, using default keypool range");
1500
0
                range_start = 0;
1501
0
                range_end = wallet.m_keypool_size;
1502
0
            }
1503
0
            next_index = range_start;
1504
1505
0
            if (data.exists("next_index")) {
1506
0
                next_index = data["next_index"].getInt<int64_t>();
1507
                // bound checks
1508
0
                if (next_index < range_start || next_index >= range_end) {
1509
0
                    throw JSONRPCError(RPC_INVALID_PARAMETER, "next_index is out of range");
1510
0
                }
1511
0
            }
1512
0
        }
1513
1514
        // Active descriptors must be ranged
1515
0
        if (active && !parsed_descs.at(0)->IsRange()) {
1516
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Active descriptors must be ranged");
1517
0
        }
1518
1519
        // Multipath descriptors should not have a label
1520
0
        if (parsed_descs.size() > 1 && data.exists("label")) {
1521
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Multipath descriptors should not have a label");
1522
0
        }
1523
1524
        // Ranged descriptors should not have a label
1525
0
        if (data.exists("range") && data.exists("label")) {
1526
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Ranged descriptors should not have a label");
1527
0
        }
1528
1529
        // Internal addresses should not have a label either
1530
0
        if (internal && data.exists("label")) {
1531
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Internal addresses should not have a label");
1532
0
        }
1533
1534
        // Combo descriptor check
1535
0
        if (active && !parsed_descs.at(0)->IsSingleType()) {
1536
0
            throw JSONRPCError(RPC_WALLET_ERROR, "Combo descriptors cannot be set to active");
1537
0
        }
1538
1539
        // If the wallet disabled private keys, abort if private keys exist
1540
0
        if (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !keys.keys.empty()) {
1541
0
            throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
1542
0
        }
1543
1544
0
        for (size_t j = 0; j < parsed_descs.size(); ++j) {
1545
0
            auto parsed_desc = std::move(parsed_descs[j]);
1546
0
            bool desc_internal = internal.has_value() && internal.value();
1547
0
            if (parsed_descs.size() == 2) {
1548
0
                desc_internal = j == 1;
1549
0
            } else if (parsed_descs.size() > 2) {
1550
0
                CHECK_NONFATAL(!desc_internal);
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
1551
0
            }
1552
            // Need to ExpandPrivate to check if private keys are available for all pubkeys
1553
0
            FlatSigningProvider expand_keys;
1554
0
            std::vector<CScript> scripts;
1555
0
            if (!parsed_desc->Expand(0, keys, scripts, expand_keys)) {
1556
0
                throw JSONRPCError(RPC_WALLET_ERROR, "Cannot expand descriptor. Probably because of hardened derivations without private keys provided");
1557
0
            }
1558
0
            parsed_desc->ExpandPrivate(0, keys, expand_keys);
1559
1560
            // Check if all private keys are provided
1561
0
            bool have_all_privkeys = !expand_keys.keys.empty();
1562
0
            for (const auto& entry : expand_keys.origins) {
1563
0
                const CKeyID& key_id = entry.first;
1564
0
                CKey key;
1565
0
                if (!expand_keys.GetKey(key_id, key)) {
1566
0
                    have_all_privkeys = false;
1567
0
                    break;
1568
0
                }
1569
0
            }
1570
1571
            // If private keys are enabled, check some things.
1572
0
            if (!wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
1573
0
               if (keys.keys.empty()) {
1574
0
                    throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import descriptor without private keys to a wallet with private keys enabled");
1575
0
               }
1576
0
               if (!have_all_privkeys) {
1577
0
                   warnings.push_back("Not all private keys provided. Some wallet functionality may return unexpected errors");
1578
0
               }
1579
0
            }
1580
1581
0
            WalletDescriptor w_desc(std::move(parsed_desc), timestamp, range_start, range_end, next_index);
1582
1583
            // Add descriptor to the wallet
1584
0
            auto spk_manager_res = wallet.AddWalletDescriptor(w_desc, keys, label, desc_internal);
1585
1586
0
            if (!spk_manager_res) {
1587
0
                throw JSONRPCError(RPC_INVALID_PARAMETER, util::ErrorString(spk_manager_res).original);
1588
0
            }
1589
1590
0
            auto spk_manager = spk_manager_res.value();
1591
1592
0
            if (spk_manager == nullptr) {
1593
0
                throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Could not add descriptor '%s'", descriptor));
Line
Count
Source
1172
0
#define strprintf tfm::format
1594
0
            }
1595
1596
            // Set descriptor as active if necessary
1597
0
            if (active) {
1598
0
                if (!w_desc.descriptor->GetOutputType()) {
1599
0
                    warnings.push_back("Unknown output type, cannot set descriptor to active.");
1600
0
                } else {
1601
0
                    wallet.AddActiveScriptPubKeyMan(spk_manager->GetID(), *w_desc.descriptor->GetOutputType(), desc_internal);
1602
0
                }
1603
0
            } else {
1604
0
                if (w_desc.descriptor->GetOutputType()) {
1605
0
                    wallet.DeactivateScriptPubKeyMan(spk_manager->GetID(), *w_desc.descriptor->GetOutputType(), desc_internal);
1606
0
                }
1607
0
            }
1608
0
        }
1609
1610
0
        result.pushKV("success", UniValue(true));
1611
0
    } catch (const UniValue& e) {
1612
0
        result.pushKV("success", UniValue(false));
1613
0
        result.pushKV("error", e);
1614
0
    }
1615
0
    PushWarnings(warnings, result);
1616
0
    return result;
1617
0
}
1618
1619
RPCHelpMan importdescriptors()
1620
0
{
1621
0
    return RPCHelpMan{"importdescriptors",
1622
0
                "\nImport 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"
1623
0
            "When importing descriptors with multipath key expressions, if the multipath specifier contains exactly two elements, the descriptor produced from the second elements will be imported as an internal descriptor.\n"
1624
0
            "\nNote: This call can take over an hour to complete if using an early timestamp; during that time, other rpc calls\n"
1625
0
            "may report that the imported keys, addresses or scripts exist but related transactions are still missing.\n"
1626
0
            "The rescan is significantly faster if block filters are available (using startup option \"-blockfilterindex=1\").\n",
1627
0
                {
1628
0
                    {"requests", RPCArg::Type::ARR, RPCArg::Optional::NO, "Data to be imported",
1629
0
                        {
1630
0
                            {"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "",
1631
0
                                {
1632
0
                                    {"desc", RPCArg::Type::STR, RPCArg::Optional::NO, "Descriptor to import."},
1633
0
                                    {"active", RPCArg::Type::BOOL, RPCArg::Default{false}, "Set this descriptor to be the active descriptor for the corresponding output type/externality"},
1634
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"},
1635
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"},
1636
0
                                    {"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, "Time from which to start rescanning the blockchain for this descriptor, in " + UNIX_EPOCH_TIME + "\n"
1637
0
                                        "Use the string \"now\" to substitute the current synced blockchain time.\n"
1638
0
                                        "\"now\" can be specified to bypass scanning, for outputs which are known to never have been used, and\n"
1639
0
                                        "0 can be specified to scan the entire blockchain. Blocks up to 2 hours before the earliest timestamp\n"
1640
0
                                        "of all descriptors being imported will be scanned as well as the mempool.",
1641
0
                                        RPCArgOptions{.type_str={"timestamp | \"now\"", "integer / string"}}
1642
0
                                    },
1643
0
                                    {"internal", RPCArg::Type::BOOL, RPCArg::Default{false}, "Whether matching outputs should be treated as not incoming payments (e.g. change)"},
1644
0
                                    {"label", RPCArg::Type::STR, RPCArg::Default{""}, "Label to assign to the address, only allowed with internal=false. Disabled for ranged descriptors"},
1645
0
                                },
1646
0
                            },
1647
0
                        },
1648
0
                        RPCArgOptions{.oneline_description="requests"}},
1649
0
                },
1650
0
                RPCResult{
1651
0
                    RPCResult::Type::ARR, "", "Response is an array with the same size as the input that has the execution result",
1652
0
                    {
1653
0
                        {RPCResult::Type::OBJ, "", "",
1654
0
                        {
1655
0
                            {RPCResult::Type::BOOL, "success", ""},
1656
0
                            {RPCResult::Type::ARR, "warnings", /*optional=*/true, "",
1657
0
                            {
1658
0
                                {RPCResult::Type::STR, "", ""},
1659
0
                            }},
1660
0
                            {RPCResult::Type::OBJ, "error", /*optional=*/true, "",
1661
0
                            {
1662
0
                                {RPCResult::Type::ELISION, "", "JSONRPC error"},
1663
0
                            }},
1664
0
                        }},
1665
0
                    }
1666
0
                },
1667
0
                RPCExamples{
1668
0
                    HelpExampleCli("importdescriptors", "'[{ \"desc\": \"<my descriptor>\", \"timestamp\":1455191478, \"internal\": true }, "
1669
0
                                          "{ \"desc\": \"<my descriptor 2>\", \"label\": \"example 2\", \"timestamp\": 1455191480 }]'") +
1670
0
                    HelpExampleCli("importdescriptors", "'[{ \"desc\": \"<my descriptor>\", \"timestamp\":1455191478, \"active\": true, \"range\": [0,100], \"label\": \"<my bech32 wallet>\" }]'")
1671
0
                },
1672
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& main_request) -> UniValue
1673
0
{
1674
0
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(main_request);
1675
0
    if (!pwallet) return UniValue::VNULL;
1676
0
    CWallet& wallet{*pwallet};
1677
1678
    // Make sure the results are valid at least up to the most recent block
1679
    // the user could have gotten from another RPC command prior to now
1680
0
    wallet.BlockUntilSyncedToCurrentChain();
1681
1682
    //  Make sure wallet is a descriptor wallet
1683
0
    if (!pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
1684
0
        throw JSONRPCError(RPC_WALLET_ERROR, "importdescriptors is not available for non-descriptor wallets");
1685
0
    }
1686
1687
0
    WalletRescanReserver reserver(*pwallet);
1688
0
    if (!reserver.reserve(/*with_passphrase=*/true)) {
1689
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
1690
0
    }
1691
1692
    // Ensure that the wallet is not locked for the remainder of this RPC, as
1693
    // the passphrase is used to top up the keypool.
1694
0
    LOCK(pwallet->m_relock_mutex);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
1695
1696
0
    const UniValue& requests = main_request.params[0];
1697
0
    const int64_t minimum_timestamp = 1;
1698
0
    int64_t now = 0;
1699
0
    int64_t lowest_timestamp = 0;
1700
0
    bool rescan = false;
1701
0
    UniValue response(UniValue::VARR);
1702
0
    {
1703
0
        LOCK(pwallet->cs_wallet);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
1704
0
        EnsureWalletIsUnlocked(*pwallet);
1705
1706
0
        CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(lowest_timestamp).mtpTime(now)));
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
1707
1708
        // Get all timestamps and extract the lowest timestamp
1709
0
        for (const UniValue& request : requests.getValues()) {
1710
            // This throws an error if "timestamp" doesn't exist
1711
0
            const int64_t timestamp = std::max(GetImportTimestamp(request, now), minimum_timestamp);
1712
0
            const UniValue result = ProcessDescriptorImport(*pwallet, request, timestamp);
1713
0
            response.push_back(result);
1714
1715
0
            if (lowest_timestamp > timestamp ) {
1716
0
                lowest_timestamp = timestamp;
1717
0
            }
1718
1719
            // If we know the chain tip, and at least one request was successful then allow rescan
1720
0
            if (!rescan && result["success"].get_bool()) {
1721
0
                rescan = true;
1722
0
            }
1723
0
        }
1724
0
        pwallet->ConnectScriptPubKeyManNotifiers();
1725
0
    }
1726
1727
    // Rescan the blockchain using the lowest timestamp
1728
0
    if (rescan) {
1729
0
        int64_t scanned_time = pwallet->RescanFromTime(lowest_timestamp, reserver, /*update=*/true);
1730
0
        pwallet->ResubmitWalletTransactions(/*relay=*/false, /*force=*/true);
1731
1732
0
        if (pwallet->IsAbortingRescan()) {
1733
0
            throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted by user.");
1734
0
        }
1735
1736
0
        if (scanned_time > lowest_timestamp) {
1737
0
            std::vector<UniValue> results = response.getValues();
1738
0
            response.clear();
1739
0
            response.setArray();
1740
1741
            // Compose the response
1742
0
            for (unsigned int i = 0; i < requests.size(); ++i) {
1743
0
                const UniValue& request = requests.getValues().at(i);
1744
1745
                // If the descriptor timestamp is within the successfully scanned
1746
                // range, or if the import result already has an error set, let
1747
                // the result stand unmodified. Otherwise replace the result
1748
                // with an error message.
1749
0
                if (scanned_time <= GetImportTimestamp(request, now) || results.at(i).exists("error")) {
1750
0
                    response.push_back(results.at(i));
1751
0
                } else {
1752
0
                    std::string error_msg{strprintf("Rescan failed for descriptor with timestamp %d. There "
Line
Count
Source
1172
0
#define strprintf tfm::format
1753
0
                            "was an error reading a block from time %d, which is after or within %d seconds "
1754
0
                            "of key creation, and could contain transactions pertaining to the desc. As a "
1755
0
                            "result, transactions and coins using this desc may not appear in the wallet.",
1756
0
                            GetImportTimestamp(request, now), scanned_time - TIMESTAMP_WINDOW - 1, TIMESTAMP_WINDOW)};
1757
0
                    if (pwallet->chain().havePruned()) {
1758
0
                        error_msg += strprintf(" This error could be caused by pruning or data corruption "
Line
Count
Source
1172
0
#define strprintf tfm::format
1759
0
                                "(see bitcoind log for details) and could be dealt with by downloading and "
1760
0
                                "rescanning the relevant blocks (see -reindex option and rescanblockchain RPC).");
1761
0
                    } else if (pwallet->chain().hasAssumedValidChain()) {
1762
0
                        error_msg += strprintf(" This error is likely caused by an in-progress assumeutxo "
Line
Count
Source
1172
0
#define strprintf tfm::format
1763
0
                                "background sync. Check logs or getchainstates RPC for assumeutxo background "
1764
0
                                "sync progress and try again later.");
1765
0
                    } else {
1766
0
                        error_msg += strprintf(" This error could potentially caused by data corruption. If "
Line
Count
Source
1172
0
#define strprintf tfm::format
1767
0
                                "the issue persists you may want to reindex (see -reindex option).");
1768
0
                    }
1769
1770
0
                    UniValue result = UniValue(UniValue::VOBJ);
1771
0
                    result.pushKV("success", UniValue(false));
1772
0
                    result.pushKV("error", JSONRPCError(RPC_MISC_ERROR, error_msg));
1773
0
                    response.push_back(std::move(result));
1774
0
                }
1775
0
            }
1776
0
        }
1777
0
    }
1778
1779
0
    return response;
1780
0
},
1781
0
    };
1782
0
}
1783
1784
RPCHelpMan listdescriptors()
1785
0
{
1786
0
    return RPCHelpMan{
1787
0
        "listdescriptors",
1788
0
        "\nList descriptors imported into a descriptor-enabled wallet.\n",
1789
0
        {
1790
0
            {"private", RPCArg::Type::BOOL, RPCArg::Default{false}, "Show private descriptors."}
1791
0
        },
1792
0
        RPCResult{RPCResult::Type::OBJ, "", "", {
1793
0
            {RPCResult::Type::STR, "wallet_name", "Name of wallet this operation was performed on"},
1794
0
            {RPCResult::Type::ARR, "descriptors", "Array of descriptor objects (sorted by descriptor string representation)",
1795
0
            {
1796
0
                {RPCResult::Type::OBJ, "", "", {
1797
0
                    {RPCResult::Type::STR, "desc", "Descriptor string representation"},
1798
0
                    {RPCResult::Type::NUM, "timestamp", "The creation time of the descriptor"},
1799
0
                    {RPCResult::Type::BOOL, "active", "Whether this descriptor is currently used to generate new addresses"},
1800
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"},
1801
0
                    {RPCResult::Type::ARR_FIXED, "range", /*optional=*/true, "Defined only for ranged descriptors", {
1802
0
                        {RPCResult::Type::NUM, "", "Range start inclusive"},
1803
0
                        {RPCResult::Type::NUM, "", "Range end inclusive"},
1804
0
                    }},
1805
0
                    {RPCResult::Type::NUM, "next", /*optional=*/true, "Same as next_index field. Kept for compatibility reason."},
1806
0
                    {RPCResult::Type::NUM, "next_index", /*optional=*/true, "The next index to generate addresses from; defined only for ranged descriptors"},
1807
0
                }},
1808
0
            }}
1809
0
        }},
1810
0
        RPCExamples{
1811
0
            HelpExampleCli("listdescriptors", "") + HelpExampleRpc("listdescriptors", "")
1812
0
            + HelpExampleCli("listdescriptors", "true") + HelpExampleRpc("listdescriptors", "true")
1813
0
        },
1814
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
1815
0
{
1816
0
    const std::shared_ptr<const CWallet> wallet = GetWalletForJSONRPCRequest(request);
1817
0
    if (!wallet) return UniValue::VNULL;
1818
1819
0
    if (!wallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
1820
0
        throw JSONRPCError(RPC_WALLET_ERROR, "listdescriptors is not available for non-descriptor wallets");
1821
0
    }
1822
1823
0
    const bool priv = !request.params[0].isNull() && request.params[0].get_bool();
1824
0
    if (priv) {
1825
0
        EnsureWalletIsUnlocked(*wallet);
1826
0
    }
1827
1828
0
    LOCK(wallet->cs_wallet);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
1829
1830
0
    const auto active_spk_mans = wallet->GetActiveScriptPubKeyMans();
1831
1832
0
    struct WalletDescInfo {
1833
0
        std::string descriptor;
1834
0
        uint64_t creation_time;
1835
0
        bool active;
1836
0
        std::optional<bool> internal;
1837
0
        std::optional<std::pair<int64_t,int64_t>> range;
1838
0
        int64_t next_index;
1839
0
    };
1840
1841
0
    std::vector<WalletDescInfo> wallet_descriptors;
1842
0
    for (const auto& spk_man : wallet->GetAllScriptPubKeyMans()) {
1843
0
        const auto desc_spk_man = dynamic_cast<DescriptorScriptPubKeyMan*>(spk_man);
1844
0
        if (!desc_spk_man) {
1845
0
            throw JSONRPCError(RPC_WALLET_ERROR, "Unexpected ScriptPubKey manager type.");
1846
0
        }
1847
0
        LOCK(desc_spk_man->cs_desc_man);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
1848
0
        const auto& wallet_descriptor = desc_spk_man->GetWalletDescriptor();
1849
0
        std::string descriptor;
1850
0
        if (!desc_spk_man->GetDescriptorString(descriptor, priv)) {
1851
0
            throw JSONRPCError(RPC_WALLET_ERROR, "Can't get descriptor string.");
1852
0
        }
1853
0
        const bool is_range = wallet_descriptor.descriptor->IsRange();
1854
0
        wallet_descriptors.push_back({
1855
0
            descriptor,
1856
0
            wallet_descriptor.creation_time,
1857
0
            active_spk_mans.count(desc_spk_man) != 0,
1858
0
            wallet->IsInternalScriptPubKeyMan(desc_spk_man),
1859
0
            is_range ? std::optional(std::make_pair(wallet_descriptor.range_start, wallet_descriptor.range_end)) : std::nullopt,
1860
0
            wallet_descriptor.next_index
1861
0
        });
1862
0
    }
1863
1864
0
    std::sort(wallet_descriptors.begin(), wallet_descriptors.end(), [](const auto& a, const auto& b) {
1865
0
        return a.descriptor < b.descriptor;
1866
0
    });
1867
1868
0
    UniValue descriptors(UniValue::VARR);
1869
0
    for (const WalletDescInfo& info : wallet_descriptors) {
1870
0
        UniValue spk(UniValue::VOBJ);
1871
0
        spk.pushKV("desc", info.descriptor);
1872
0
        spk.pushKV("timestamp", info.creation_time);
1873
0
        spk.pushKV("active", info.active);
1874
0
        if (info.internal.has_value()) {
1875
0
            spk.pushKV("internal", info.internal.value());
1876
0
        }
1877
0
        if (info.range.has_value()) {
1878
0
            UniValue range(UniValue::VARR);
1879
0
            range.push_back(info.range->first);
1880
0
            range.push_back(info.range->second - 1);
1881
0
            spk.pushKV("range", std::move(range));
1882
0
            spk.pushKV("next", info.next_index);
1883
0
            spk.pushKV("next_index", info.next_index);
1884
0
        }
1885
0
        descriptors.push_back(std::move(spk));
1886
0
    }
1887
1888
0
    UniValue response(UniValue::VOBJ);
1889
0
    response.pushKV("wallet_name", wallet->GetName());
1890
0
    response.pushKV("descriptors", std::move(descriptors));
1891
1892
0
    return response;
1893
0
},
1894
0
    };
1895
0
}
1896
1897
RPCHelpMan backupwallet()
1898
0
{
1899
0
    return RPCHelpMan{"backupwallet",
1900
0
                "\nSafely copies the current wallet file to the specified destination, which can either be a directory or a path with a filename.\n",
1901
0
                {
1902
0
                    {"destination", RPCArg::Type::STR, RPCArg::Optional::NO, "The destination directory or file"},
1903
0
                },
1904
0
                RPCResult{RPCResult::Type::NONE, "", ""},
1905
0
                RPCExamples{
1906
0
                    HelpExampleCli("backupwallet", "\"backup.dat\"")
1907
0
            + HelpExampleRpc("backupwallet", "\"backup.dat\"")
1908
0
                },
1909
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
1910
0
{
1911
0
    const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
1912
0
    if (!pwallet) return UniValue::VNULL;
1913
1914
    // Make sure the results are valid at least up to the most recent block
1915
    // the user could have gotten from another RPC command prior to now
1916
0
    pwallet->BlockUntilSyncedToCurrentChain();
1917
1918
0
    LOCK(pwallet->cs_wallet);
Line
Count
Source
257
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
1919
1920
0
    std::string strDest = request.params[0].get_str();
1921
0
    if (!pwallet->BackupWallet(strDest)) {
1922
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!");
1923
0
    }
1924
1925
0
    return UniValue::VNULL;
1926
0
},
1927
0
    };
1928
0
}
1929
1930
1931
RPCHelpMan restorewallet()
1932
0
{
1933
0
    return RPCHelpMan{
1934
0
        "restorewallet",
1935
0
        "\nRestores and loads a wallet from backup.\n"
1936
0
        "\nThe rescan is significantly faster if a descriptor wallet is restored"
1937
0
        "\nand block filters are available (using startup option \"-blockfilterindex=1\").\n",
1938
0
        {
1939
0
            {"wallet_name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name that will be applied to the restored wallet"},
1940
0
            {"backup_file", RPCArg::Type::STR, RPCArg::Optional::NO, "The backup file that will be used to restore the wallet."},
1941
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."},
1942
0
        },
1943
0
        RPCResult{
1944
0
            RPCResult::Type::OBJ, "", "",
1945
0
            {
1946
0
                {RPCResult::Type::STR, "name", "The wallet name if restored successfully."},
1947
0
                {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to restoring and loading the wallet.",
1948
0
                {
1949
0
                    {RPCResult::Type::STR, "", ""},
1950
0
                }},
1951
0
            }
1952
0
        },
1953
0
        RPCExamples{
1954
0
            HelpExampleCli("restorewallet", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
1955
0
            + HelpExampleRpc("restorewallet", "\"testwallet\" \"home\\backups\\backup-file.bak\"")
1956
0
            + HelpExampleCliNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak\""}, {"load_on_startup", true}})
1957
0
            + HelpExampleRpcNamed("restorewallet", {{"wallet_name", "testwallet"}, {"backup_file", "home\\backups\\backup-file.bak\""}, {"load_on_startup", true}})
1958
0
        },
1959
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
1960
0
{
1961
1962
0
    WalletContext& context = EnsureWalletContext(request.context);
1963
1964
0
    auto backup_file = fs::u8path(request.params[1].get_str());
1965
1966
0
    std::string wallet_name = request.params[0].get_str();
1967
1968
0
    std::optional<bool> load_on_start = request.params[2].isNull() ? std::nullopt : std::optional<bool>(request.params[2].get_bool());
1969
1970
0
    DatabaseStatus status;
1971
0
    bilingual_str error;
1972
0
    std::vector<bilingual_str> warnings;
1973
1974
0
    const std::shared_ptr<CWallet> wallet = RestoreWallet(context, backup_file, wallet_name, load_on_start, status, error, warnings);
1975
1976
0
    HandleWalletError(wallet, status, error);
1977
1978
0
    UniValue obj(UniValue::VOBJ);
1979
0
    obj.pushKV("name", wallet->GetName());
1980
0
    PushWarnings(warnings, obj);
1981
1982
0
    return obj;
1983
1984
0
},
1985
0
    };
1986
0
}
1987
} // namespace wallet