fuzz coverage

Coverage Report

Created: 2025-08-28 15:26

/Users/eugenesiegel/btc/bitcoin/src/wallet/rpc/wallet.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <bitcoin-build-config.h> // IWYU pragma: keep
7
8
#include <core_io.h>
9
#include <key_io.h>
10
#include <rpc/server.h>
11
#include <rpc/util.h>
12
#include <util/translation.h>
13
#include <wallet/context.h>
14
#include <wallet/receive.h>
15
#include <wallet/rpc/wallet.h>
16
#include <wallet/rpc/util.h>
17
#include <wallet/wallet.h>
18
#include <wallet/walletutil.h>
19
20
#include <optional>
21
22
#include <univalue.h>
23
24
25
namespace wallet {
26
27
static const std::map<uint64_t, std::string> WALLET_FLAG_CAVEATS{
28
    {WALLET_FLAG_AVOID_REUSE,
29
     "You need to rescan the blockchain in order to correctly mark used "
30
     "destinations in the past. Until this is done, some destinations may "
31
     "be considered unused, even if the opposite is the case."},
32
};
33
34
/** Checks if a CKey is in the given CWallet compressed or otherwise*/
35
bool HaveKey(const SigningProvider& wallet, const CKey& key)
36
0
{
37
0
    CKey key2;
38
0
    key2.Set(key.begin(), key.end(), !key.IsCompressed());
39
0
    return wallet.HaveKey(key.GetPubKey().GetID()) || wallet.HaveKey(key2.GetPubKey().GetID());
40
0
}
41
42
static RPCHelpMan getwalletinfo()
43
0
{
44
0
    return RPCHelpMan{"getwalletinfo",
45
0
                "Returns an object containing various wallet state info.\n",
46
0
                {},
47
0
                RPCResult{
48
0
                    RPCResult::Type::OBJ, "", "",
49
0
                    {
50
0
                        {
51
0
                        {RPCResult::Type::STR, "walletname", "the wallet name"},
52
0
                        {RPCResult::Type::NUM, "walletversion", "the wallet version"},
53
0
                        {RPCResult::Type::STR, "format", "the database format (bdb or sqlite)"},
54
0
                        {RPCResult::Type::STR_AMOUNT, "balance", "DEPRECATED. Identical to getbalances().mine.trusted"},
55
0
                        {RPCResult::Type::STR_AMOUNT, "unconfirmed_balance", "DEPRECATED. Identical to getbalances().mine.untrusted_pending"},
56
0
                        {RPCResult::Type::STR_AMOUNT, "immature_balance", "DEPRECATED. Identical to getbalances().mine.immature"},
57
0
                        {RPCResult::Type::NUM, "txcount", "the total number of transactions in the wallet"},
58
0
                        {RPCResult::Type::NUM_TIME, "keypoololdest", /*optional=*/true, "the " + UNIX_EPOCH_TIME + " of the oldest pre-generated key in the key pool. Legacy wallets only."},
59
0
                        {RPCResult::Type::NUM, "keypoolsize", "how many new keys are pre-generated (only counts external keys)"},
60
0
                        {RPCResult::Type::NUM, "keypoolsize_hd_internal", /*optional=*/true, "how many new keys are pre-generated for internal use (used for change outputs, only appears if the wallet is using this feature, otherwise external keys are used)"},
61
0
                        {RPCResult::Type::NUM_TIME, "unlocked_until", /*optional=*/true, "the " + UNIX_EPOCH_TIME + " until which the wallet is unlocked for transfers, or 0 if the wallet is locked (only present for passphrase-encrypted wallets)"},
62
0
                        {RPCResult::Type::STR_AMOUNT, "paytxfee", "the transaction fee configuration, set in " + CURRENCY_UNIT + "/kvB"},
63
0
                        {RPCResult::Type::STR_HEX, "hdseedid", /*optional=*/true, "the Hash160 of the HD seed (only present when HD is enabled)"},
64
0
                        {RPCResult::Type::BOOL, "private_keys_enabled", "false if privatekeys are disabled for this wallet (enforced watch-only wallet)"},
65
0
                        {RPCResult::Type::BOOL, "avoid_reuse", "whether this wallet tracks clean/dirty coins in terms of reuse"},
66
0
                        {RPCResult::Type::OBJ, "scanning", "current scanning details, or false if no scan is in progress",
67
0
                        {
68
0
                            {RPCResult::Type::NUM, "duration", "elapsed seconds since scan start"},
69
0
                            {RPCResult::Type::NUM, "progress", "scanning progress percentage [0.0, 1.0]"},
70
0
                        }, /*skip_type_check=*/true},
71
0
                        {RPCResult::Type::BOOL, "descriptors", "whether this wallet uses descriptors for output script management"},
72
0
                        {RPCResult::Type::BOOL, "external_signer", "whether this wallet is configured to use an external signer such as a hardware wallet"},
73
0
                        {RPCResult::Type::BOOL, "blank", "Whether this wallet intentionally does not contain any keys, scripts, or descriptors"},
74
0
                        {RPCResult::Type::NUM_TIME, "birthtime", /*optional=*/true, "The start time for blocks scanning. It could be modified by (re)importing any descriptor with an earlier timestamp."},
75
0
                        RESULT_LAST_PROCESSED_BLOCK,
76
0
                    }},
77
0
                },
78
0
                RPCExamples{
79
0
                    HelpExampleCli("getwalletinfo", "")
80
0
            + HelpExampleRpc("getwalletinfo", "")
81
0
                },
82
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
83
0
{
84
0
    const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
85
0
    if (!pwallet) return UniValue::VNULL;
86
87
    // Make sure the results are valid at least up to the most recent block
88
    // the user could have gotten from another RPC command prior to now
89
0
    pwallet->BlockUntilSyncedToCurrentChain();
90
91
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
92
93
0
    UniValue obj(UniValue::VOBJ);
94
95
0
    size_t kpExternalSize = pwallet->KeypoolCountExternalKeys();
96
0
    const auto bal = GetBalance(*pwallet);
97
0
    obj.pushKV("walletname", pwallet->GetName());
98
0
    obj.pushKV("walletversion", pwallet->GetVersion());
99
0
    obj.pushKV("format", pwallet->GetDatabase().Format());
100
0
    obj.pushKV("balance", ValueFromAmount(bal.m_mine_trusted));
101
0
    obj.pushKV("unconfirmed_balance", ValueFromAmount(bal.m_mine_untrusted_pending));
102
0
    obj.pushKV("immature_balance", ValueFromAmount(bal.m_mine_immature));
103
0
    obj.pushKV("txcount",       (int)pwallet->mapWallet.size());
104
0
    const auto kp_oldest = pwallet->GetOldestKeyPoolTime();
105
0
    if (kp_oldest.has_value()) {
106
0
        obj.pushKV("keypoololdest", kp_oldest.value());
107
0
    }
108
0
    obj.pushKV("keypoolsize", (int64_t)kpExternalSize);
109
110
0
    LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
111
0
    if (spk_man) {
112
0
        CKeyID seed_id = spk_man->GetHDChain().seed_id;
113
0
        if (!seed_id.IsNull()) {
114
0
            obj.pushKV("hdseedid", seed_id.GetHex());
115
0
        }
116
0
    }
117
118
0
    if (pwallet->CanSupportFeature(FEATURE_HD_SPLIT)) {
119
0
        obj.pushKV("keypoolsize_hd_internal",   (int64_t)(pwallet->GetKeyPoolSize() - kpExternalSize));
120
0
    }
121
0
    if (pwallet->IsCrypted()) {
122
0
        obj.pushKV("unlocked_until", pwallet->nRelockTime);
123
0
    }
124
0
    obj.pushKV("paytxfee", ValueFromAmount(pwallet->m_pay_tx_fee.GetFeePerK()));
125
0
    obj.pushKV("private_keys_enabled", !pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
126
0
    obj.pushKV("avoid_reuse", pwallet->IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE));
127
0
    if (pwallet->IsScanning()) {
128
0
        UniValue scanning(UniValue::VOBJ);
129
0
        scanning.pushKV("duration", Ticks<std::chrono::seconds>(pwallet->ScanningDuration()));
130
0
        scanning.pushKV("progress", pwallet->ScanningProgress());
131
0
        obj.pushKV("scanning", std::move(scanning));
132
0
    } else {
133
0
        obj.pushKV("scanning", false);
134
0
    }
135
0
    obj.pushKV("descriptors", pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
136
0
    obj.pushKV("external_signer", pwallet->IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER));
137
0
    obj.pushKV("blank", pwallet->IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET));
138
0
    if (int64_t birthtime = pwallet->GetBirthTime(); birthtime != UNKNOWN_TIME) {
139
0
        obj.pushKV("birthtime", birthtime);
140
0
    }
141
142
0
    AppendLastProcessedBlock(obj, *pwallet);
143
0
    return obj;
144
0
},
145
0
    };
146
0
}
147
148
static RPCHelpMan listwalletdir()
149
0
{
150
0
    return RPCHelpMan{"listwalletdir",
151
0
                "Returns a list of wallets in the wallet directory.\n",
152
0
                {},
153
0
                RPCResult{
154
0
                    RPCResult::Type::OBJ, "", "",
155
0
                    {
156
0
                        {RPCResult::Type::ARR, "wallets", "",
157
0
                        {
158
0
                            {RPCResult::Type::OBJ, "", "",
159
0
                            {
160
0
                                {RPCResult::Type::STR, "name", "The wallet name"},
161
0
                            }},
162
0
                        }},
163
0
                    }
164
0
                },
165
0
                RPCExamples{
166
0
                    HelpExampleCli("listwalletdir", "")
167
0
            + HelpExampleRpc("listwalletdir", "")
168
0
                },
169
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
170
0
{
171
0
    UniValue wallets(UniValue::VARR);
172
0
    for (const auto& [path, _] : ListDatabases(GetWalletDir())) {
173
0
        UniValue wallet(UniValue::VOBJ);
174
0
        wallet.pushKV("name", path.utf8string());
175
0
        wallets.push_back(std::move(wallet));
176
0
    }
177
178
0
    UniValue result(UniValue::VOBJ);
179
0
    result.pushKV("wallets", std::move(wallets));
180
0
    return result;
181
0
},
182
0
    };
183
0
}
184
185
static RPCHelpMan listwallets()
186
0
{
187
0
    return RPCHelpMan{"listwallets",
188
0
                "Returns a list of currently loaded wallets.\n"
189
0
                "For full information on the wallet, use \"getwalletinfo\"\n",
190
0
                {},
191
0
                RPCResult{
192
0
                    RPCResult::Type::ARR, "", "",
193
0
                    {
194
0
                        {RPCResult::Type::STR, "walletname", "the wallet name"},
195
0
                    }
196
0
                },
197
0
                RPCExamples{
198
0
                    HelpExampleCli("listwallets", "")
199
0
            + HelpExampleRpc("listwallets", "")
200
0
                },
201
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
202
0
{
203
0
    UniValue obj(UniValue::VARR);
204
205
0
    WalletContext& context = EnsureWalletContext(request.context);
206
0
    for (const std::shared_ptr<CWallet>& wallet : GetWallets(context)) {
207
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
208
0
        obj.push_back(wallet->GetName());
209
0
    }
210
211
0
    return obj;
212
0
},
213
0
    };
214
0
}
215
216
static RPCHelpMan loadwallet()
217
0
{
218
0
    return RPCHelpMan{"loadwallet",
219
0
                "\nLoads a wallet from a wallet file or directory."
220
0
                "\nNote that all wallet command-line options used when starting bitcoind will be"
221
0
                "\napplied to the new wallet.\n",
222
0
                {
223
0
                    {"filename", RPCArg::Type::STR, RPCArg::Optional::NO, "The path to the directory of the wallet to be loaded, either absolute or relative to the \"wallets\" directory. The \"wallets\" directory is set by the -walletdir option and defaults to the \"wallets\" folder within the data directory."},
224
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."},
225
0
                },
226
0
                RPCResult{
227
0
                    RPCResult::Type::OBJ, "", "",
228
0
                    {
229
0
                        {RPCResult::Type::STR, "name", "The wallet name if loaded successfully."},
230
0
                        {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to loading the wallet.",
231
0
                        {
232
0
                            {RPCResult::Type::STR, "", ""},
233
0
                        }},
234
0
                    }
235
0
                },
236
0
                RPCExamples{
237
0
                    "\nLoad wallet from the wallet dir:\n"
238
0
                    + HelpExampleCli("loadwallet", "\"walletname\"")
239
0
                    + HelpExampleRpc("loadwallet", "\"walletname\"")
240
0
                    + "\nLoad wallet using absolute path (Unix):\n"
241
0
                    + HelpExampleCli("loadwallet", "\"/path/to/walletname/\"")
242
0
                    + HelpExampleRpc("loadwallet", "\"/path/to/walletname/\"")
243
0
                    + "\nLoad wallet using absolute path (Windows):\n"
244
0
                    + HelpExampleCli("loadwallet", "\"DriveLetter:\\path\\to\\walletname\\\"")
245
0
                    + HelpExampleRpc("loadwallet", "\"DriveLetter:\\path\\to\\walletname\\\"")
246
0
                },
247
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
248
0
{
249
0
    WalletContext& context = EnsureWalletContext(request.context);
250
0
    const std::string name(request.params[0].get_str());
251
252
0
    DatabaseOptions options;
253
0
    DatabaseStatus status;
254
0
    ReadDatabaseArgs(*context.args, options);
255
0
    options.require_existing = true;
256
0
    bilingual_str error;
257
0
    std::vector<bilingual_str> warnings;
258
0
    std::optional<bool> load_on_start = request.params[1].isNull() ? std::nullopt : std::optional<bool>(request.params[1].get_bool());
259
260
0
    {
261
0
        LOCK(context.wallets_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
262
0
        if (std::any_of(context.wallets.begin(), context.wallets.end(), [&name](const auto& wallet) { return wallet->GetName() == name; })) {
263
0
            throw JSONRPCError(RPC_WALLET_ALREADY_LOADED, "Wallet \"" + name + "\" is already loaded.");
264
0
        }
265
0
    }
266
267
0
    std::shared_ptr<CWallet> const wallet = LoadWallet(context, name, load_on_start, options, status, error, warnings);
268
269
0
    HandleWalletError(wallet, status, error);
270
271
0
    UniValue obj(UniValue::VOBJ);
272
0
    obj.pushKV("name", wallet->GetName());
273
0
    PushWarnings(warnings, obj);
274
275
0
    return obj;
276
0
},
277
0
    };
278
0
}
279
280
static RPCHelpMan setwalletflag()
281
0
{
282
0
            std::string flags;
283
0
            for (auto& it : WALLET_FLAG_MAP)
284
0
                if (it.second & MUTABLE_WALLET_FLAGS)
285
0
                    flags += (flags == "" ? "" : ", ") + it.first;
286
287
0
    return RPCHelpMan{"setwalletflag",
288
0
                "\nChange the state of the given wallet flag for a wallet.\n",
289
0
                {
290
0
                    {"flag", RPCArg::Type::STR, RPCArg::Optional::NO, "The name of the flag to change. Current available flags: " + flags},
291
0
                    {"value", RPCArg::Type::BOOL, RPCArg::Default{true}, "The new state."},
292
0
                },
293
0
                RPCResult{
294
0
                    RPCResult::Type::OBJ, "", "",
295
0
                    {
296
0
                        {RPCResult::Type::STR, "flag_name", "The name of the flag that was modified"},
297
0
                        {RPCResult::Type::BOOL, "flag_state", "The new state of the flag"},
298
0
                        {RPCResult::Type::STR, "warnings", /*optional=*/true, "Any warnings associated with the change"},
299
0
                    }
300
0
                },
301
0
                RPCExamples{
302
0
                    HelpExampleCli("setwalletflag", "avoid_reuse")
303
0
                  + HelpExampleRpc("setwalletflag", "\"avoid_reuse\"")
304
0
                },
305
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
306
0
{
307
0
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
308
0
    if (!pwallet) return UniValue::VNULL;
309
310
0
    std::string flag_str = request.params[0].get_str();
311
0
    bool value = request.params[1].isNull() || request.params[1].get_bool();
312
313
0
    if (!WALLET_FLAG_MAP.count(flag_str)) {
314
0
        throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unknown wallet flag: %s", flag_str));
Line
Count
Source
1172
0
#define strprintf tfm::format
315
0
    }
316
317
0
    auto flag = WALLET_FLAG_MAP.at(flag_str);
318
319
0
    if (!(flag & MUTABLE_WALLET_FLAGS)) {
320
0
        throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Wallet flag is immutable: %s", flag_str));
Line
Count
Source
1172
0
#define strprintf tfm::format
321
0
    }
322
323
0
    UniValue res(UniValue::VOBJ);
324
325
0
    if (pwallet->IsWalletFlagSet(flag) == value) {
326
0
        throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Wallet flag is already set to %s: %s", value ? "true" : "false", flag_str));
Line
Count
Source
1172
0
#define strprintf tfm::format
327
0
    }
328
329
0
    res.pushKV("flag_name", flag_str);
330
0
    res.pushKV("flag_state", value);
331
332
0
    if (value) {
333
0
        pwallet->SetWalletFlag(flag);
334
0
    } else {
335
0
        pwallet->UnsetWalletFlag(flag);
336
0
    }
337
338
0
    if (flag && value && WALLET_FLAG_CAVEATS.count(flag)) {
339
0
        res.pushKV("warnings", WALLET_FLAG_CAVEATS.at(flag));
340
0
    }
341
342
0
    return res;
343
0
},
344
0
    };
345
0
}
346
347
static RPCHelpMan createwallet()
348
0
{
349
0
    return RPCHelpMan{
350
0
        "createwallet",
351
0
        "\nCreates and loads a new wallet.\n",
352
0
        {
353
0
            {"wallet_name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name for the new wallet. If this is a path, the wallet will be created at the path location."},
354
0
            {"disable_private_keys", RPCArg::Type::BOOL, RPCArg::Default{false}, "Disable the possibility of private keys (only watchonlys are possible in this mode)."},
355
0
            {"blank", RPCArg::Type::BOOL, RPCArg::Default{false}, "Create a blank wallet. A blank wallet has no keys or HD seed. One can be set using sethdseed."},
356
0
            {"passphrase", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Encrypt the wallet with this passphrase."},
357
0
            {"avoid_reuse", RPCArg::Type::BOOL, RPCArg::Default{false}, "Keep track of coin reuse, and treat dirty and clean coins differently with privacy considerations in mind."},
358
0
            {"descriptors", RPCArg::Type::BOOL, RPCArg::Default{true}, "If set, must be \"true\""},
359
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."},
360
0
            {"external_signer", RPCArg::Type::BOOL, RPCArg::Default{false}, "Use an external signer such as a hardware wallet. Requires -signer to be configured. Wallet creation will fail if keys cannot be fetched. Requires disable_private_keys and descriptors set to true."},
361
0
        },
362
0
        RPCResult{
363
0
            RPCResult::Type::OBJ, "", "",
364
0
            {
365
0
                {RPCResult::Type::STR, "name", "The wallet name if created successfully. If the wallet was created using a full path, the wallet_name will be the full path."},
366
0
                {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to creating and loading the wallet.",
367
0
                {
368
0
                    {RPCResult::Type::STR, "", ""},
369
0
                }},
370
0
            }
371
0
        },
372
0
        RPCExamples{
373
0
            HelpExampleCli("createwallet", "\"testwallet\"")
374
0
            + HelpExampleRpc("createwallet", "\"testwallet\"")
375
0
            + HelpExampleCliNamed("createwallet", {{"wallet_name", "descriptors"}, {"avoid_reuse", true}, {"descriptors", true}, {"load_on_startup", true}})
376
0
            + HelpExampleRpcNamed("createwallet", {{"wallet_name", "descriptors"}, {"avoid_reuse", true}, {"descriptors", true}, {"load_on_startup", true}})
377
0
        },
378
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
379
0
{
380
0
    WalletContext& context = EnsureWalletContext(request.context);
381
0
    uint64_t flags = 0;
382
0
    if (!request.params[1].isNull() && request.params[1].get_bool()) {
383
0
        flags |= WALLET_FLAG_DISABLE_PRIVATE_KEYS;
384
0
    }
385
386
0
    if (!request.params[2].isNull() && request.params[2].get_bool()) {
387
0
        flags |= WALLET_FLAG_BLANK_WALLET;
388
0
    }
389
0
    SecureString passphrase;
390
0
    passphrase.reserve(100);
391
0
    std::vector<bilingual_str> warnings;
392
0
    if (!request.params[3].isNull()) {
393
0
        passphrase = std::string_view{request.params[3].get_str()};
394
0
        if (passphrase.empty()) {
395
            // Empty string means unencrypted
396
0
            warnings.emplace_back(Untranslated("Empty string given as passphrase, wallet will not be encrypted."));
397
0
        }
398
0
    }
399
400
0
    if (!request.params[4].isNull() && request.params[4].get_bool()) {
401
0
        flags |= WALLET_FLAG_AVOID_REUSE;
402
0
    }
403
0
    flags |= WALLET_FLAG_DESCRIPTORS;
404
0
    if (!self.Arg<bool>("descriptors")) {
405
0
        throw JSONRPCError(RPC_WALLET_ERROR, "descriptors argument must be set to \"true\"; it is no longer possible to create a legacy wallet.");
406
0
    }
407
0
    if (!request.params[7].isNull() && request.params[7].get_bool()) {
408
#ifdef ENABLE_EXTERNAL_SIGNER
409
        flags |= WALLET_FLAG_EXTERNAL_SIGNER;
410
#else
411
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Compiled without external signing support (required for external signing)");
412
0
#endif
413
0
    }
414
415
0
    DatabaseOptions options;
416
0
    DatabaseStatus status;
417
0
    ReadDatabaseArgs(*context.args, options);
418
0
    options.require_create = true;
419
0
    options.create_flags = flags;
420
0
    options.create_passphrase = passphrase;
421
0
    bilingual_str error;
422
0
    std::optional<bool> load_on_start = request.params[6].isNull() ? std::nullopt : std::optional<bool>(request.params[6].get_bool());
423
0
    const std::shared_ptr<CWallet> wallet = CreateWallet(context, request.params[0].get_str(), load_on_start, options, status, error, warnings);
424
0
    if (!wallet) {
425
0
        RPCErrorCode code = status == DatabaseStatus::FAILED_ENCRYPT ? RPC_WALLET_ENCRYPTION_FAILED : RPC_WALLET_ERROR;
426
0
        throw JSONRPCError(code, error.original);
427
0
    }
428
429
0
    UniValue obj(UniValue::VOBJ);
430
0
    obj.pushKV("name", wallet->GetName());
431
0
    PushWarnings(warnings, obj);
432
433
0
    return obj;
434
0
},
435
0
    };
436
0
}
437
438
static RPCHelpMan unloadwallet()
439
0
{
440
0
    return RPCHelpMan{"unloadwallet",
441
0
                "Unloads the wallet referenced by the request endpoint, otherwise unloads the wallet specified in the argument.\n"
442
0
                "Specifying the wallet name on a wallet endpoint is invalid.",
443
0
                {
444
0
                    {"wallet_name", RPCArg::Type::STR, RPCArg::DefaultHint{"the wallet name from the RPC endpoint"}, "The name of the wallet to unload. If provided both here and in the RPC endpoint, the two must be identical."},
445
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."},
446
0
                },
447
0
                RPCResult{RPCResult::Type::OBJ, "", "", {
448
0
                    {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to unloading the wallet.",
449
0
                    {
450
0
                        {RPCResult::Type::STR, "", ""},
451
0
                    }},
452
0
                }},
453
0
                RPCExamples{
454
0
                    HelpExampleCli("unloadwallet", "wallet_name")
455
0
            + HelpExampleRpc("unloadwallet", "wallet_name")
456
0
                },
457
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
458
0
{
459
0
    std::string wallet_name;
460
0
    if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
461
0
        if (!(request.params[0].isNull() || request.params[0].get_str() == wallet_name)) {
462
0
            throw JSONRPCError(RPC_INVALID_PARAMETER, "RPC endpoint wallet and wallet_name parameter specify different wallets");
463
0
        }
464
0
    } else {
465
0
        wallet_name = request.params[0].get_str();
466
0
    }
467
468
0
    WalletContext& context = EnsureWalletContext(request.context);
469
0
    std::shared_ptr<CWallet> wallet = GetWallet(context, wallet_name);
470
0
    if (!wallet) {
471
0
        throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
472
0
    }
473
474
0
    std::vector<bilingual_str> warnings;
475
0
    {
476
0
        WalletRescanReserver reserver(*wallet);
477
0
        if (!reserver.reserve()) {
478
0
            throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
479
0
        }
480
481
        // Release the "main" shared pointer and prevent further notifications.
482
        // Note that any attempt to load the same wallet would fail until the wallet
483
        // is destroyed (see CheckUniqueFileid).
484
0
        std::optional<bool> load_on_start{self.MaybeArg<bool>("load_on_startup")};
485
0
        if (!RemoveWallet(context, wallet, load_on_start, warnings)) {
486
0
            throw JSONRPCError(RPC_MISC_ERROR, "Requested wallet already unloaded");
487
0
        }
488
0
    }
489
490
0
    WaitForDeleteWallet(std::move(wallet));
491
492
0
    UniValue result(UniValue::VOBJ);
493
0
    PushWarnings(warnings, result);
494
495
0
    return result;
496
0
},
497
0
    };
498
0
}
499
500
static RPCHelpMan sethdseed()
501
0
{
502
0
    return RPCHelpMan{"sethdseed",
503
0
                "\nSet or generate a new HD wallet seed. Non-HD wallets will not be upgraded to being a HD wallet. Wallets that are already\n"
504
0
                "HD will have a new HD seed set so that new keys added to the keypool will be derived from this new seed.\n"
505
0
                "\nNote that you will need to MAKE A NEW BACKUP of your wallet after setting the HD wallet seed." + HELP_REQUIRING_PASSPHRASE +
506
0
                "Note: This command is only compatible with legacy wallets.\n",
507
0
                {
508
0
                    {"newkeypool", RPCArg::Type::BOOL, RPCArg::Default{true}, "Whether to flush old unused addresses, including change addresses, from the keypool and regenerate it.\n"
509
0
                                         "If true, the next address from getnewaddress and change address from getrawchangeaddress will be from this new seed.\n"
510
0
                                         "If false, addresses (including change addresses if the wallet already had HD Chain Split enabled) from the existing\n"
511
0
                                         "keypool will be used until it has been depleted."},
512
0
                    {"seed", RPCArg::Type::STR, RPCArg::DefaultHint{"random seed"}, "The WIF private key to use as the new HD seed.\n"
513
0
                                         "The seed value can be retrieved using the dumpwallet command. It is the private key marked hdseed=1"},
514
0
                },
515
0
                RPCResult{RPCResult::Type::NONE, "", ""},
516
0
                RPCExamples{
517
0
                    HelpExampleCli("sethdseed", "")
518
0
            + HelpExampleCli("sethdseed", "false")
519
0
            + HelpExampleCli("sethdseed", "true \"wifkey\"")
520
0
            + HelpExampleRpc("sethdseed", "true, \"wifkey\"")
521
0
                },
522
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
523
0
{
524
0
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
525
0
    if (!pwallet) return UniValue::VNULL;
526
527
0
    LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet, true);
528
529
0
    if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
530
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Cannot set a HD seed to a wallet with private keys disabled");
531
0
    }
532
533
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__)
534
535
    // Do not do anything to non-HD wallets
536
0
    if (!pwallet->CanSupportFeature(FEATURE_HD)) {
537
0
        throw JSONRPCError(RPC_WALLET_ERROR, "Cannot set an HD seed on a non-HD wallet. Use the upgradewallet RPC in order to upgrade a non-HD wallet to HD");
538
0
    }
539
540
0
    EnsureWalletIsUnlocked(*pwallet);
541
542
0
    bool flush_key_pool = true;
543
0
    if (!request.params[0].isNull()) {
544
0
        flush_key_pool = request.params[0].get_bool();
545
0
    }
546
547
0
    CPubKey master_pub_key;
548
0
    if (request.params[1].isNull()) {
549
0
        master_pub_key = spk_man.GenerateNewSeed();
550
0
    } else {
551
0
        CKey key = DecodeSecret(request.params[1].get_str());
552
0
        if (!key.IsValid()) {
553
0
            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
554
0
        }
555
556
0
        if (HaveKey(spk_man, key)) {
557
0
            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Already have this key (either as an HD seed or as a loose private key)");
558
0
        }
559
560
0
        master_pub_key = spk_man.DeriveNewSeed(key);
561
0
    }
562
563
0
    spk_man.SetHDSeed(master_pub_key);
564
0
    if (flush_key_pool) spk_man.NewKeyPool();
565
566
0
    return UniValue::VNULL;
567
0
},
568
0
    };
569
0
}
570
571
static RPCHelpMan upgradewallet()
572
0
{
573
0
    return RPCHelpMan{"upgradewallet",
574
0
        "\nUpgrade the wallet. Upgrades to the latest version if no version number is specified.\n"
575
0
        "New keys may be generated and a new wallet backup will need to be made.",
576
0
        {
577
0
            {"version", RPCArg::Type::NUM, RPCArg::Default{int{FEATURE_LATEST}}, "The version number to upgrade to. Default is the latest wallet version."}
578
0
        },
579
0
        RPCResult{
580
0
            RPCResult::Type::OBJ, "", "",
581
0
            {
582
0
                {RPCResult::Type::STR, "wallet_name", "Name of wallet this operation was performed on"},
583
0
                {RPCResult::Type::NUM, "previous_version", "Version of wallet before this operation"},
584
0
                {RPCResult::Type::NUM, "current_version", "Version of wallet after this operation"},
585
0
                {RPCResult::Type::STR, "result", /*optional=*/true, "Description of result, if no error"},
586
0
                {RPCResult::Type::STR, "error", /*optional=*/true, "Error message (if there is one)"}
587
0
            },
588
0
        },
589
0
        RPCExamples{
590
0
            HelpExampleCli("upgradewallet", "169900")
591
0
            + HelpExampleRpc("upgradewallet", "169900")
592
0
        },
593
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
594
0
{
595
0
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
596
0
    if (!pwallet) return UniValue::VNULL;
597
598
0
    EnsureWalletIsUnlocked(*pwallet);
599
600
0
    int version = 0;
601
0
    if (!request.params[0].isNull()) {
602
0
        version = request.params[0].getInt<int>();
603
0
    }
604
0
    bilingual_str error;
605
0
    const int previous_version{pwallet->GetVersion()};
606
0
    const bool wallet_upgraded{pwallet->UpgradeWallet(version, error)};
607
0
    const int current_version{pwallet->GetVersion()};
608
0
    std::string result;
609
610
0
    if (wallet_upgraded) {
611
0
        if (previous_version == current_version) {
612
0
            result = "Already at latest version. Wallet version unchanged.";
613
0
        } else {
614
0
            result = strprintf("Wallet upgraded successfully from version %i to version %i.", previous_version, current_version);
Line
Count
Source
1172
0
#define strprintf tfm::format
615
0
        }
616
0
    }
617
618
0
    UniValue obj(UniValue::VOBJ);
619
0
    obj.pushKV("wallet_name", pwallet->GetName());
620
0
    obj.pushKV("previous_version", previous_version);
621
0
    obj.pushKV("current_version", current_version);
622
0
    if (!result.empty()) {
623
0
        obj.pushKV("result", result);
624
0
    } else {
625
0
        CHECK_NONFATAL(!error.empty());
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
626
0
        obj.pushKV("error", error.original);
627
0
    }
628
0
    return obj;
629
0
},
630
0
    };
631
0
}
632
633
RPCHelpMan simulaterawtransaction()
634
0
{
635
0
    return RPCHelpMan{"simulaterawtransaction",
636
0
        "\nCalculate the balance change resulting in the signing and broadcasting of the given transaction(s).\n",
637
0
        {
638
0
            {"rawtxs", RPCArg::Type::ARR, RPCArg::Optional::OMITTED, "An array of hex strings of raw transactions.\n",
639
0
                {
640
0
                    {"rawtx", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, ""},
641
0
                },
642
0
            },
643
0
            {"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "",
644
0
                {
645
0
                    {"include_watchonly", RPCArg::Type::BOOL, RPCArg::DefaultHint{"true for watch-only wallets, otherwise false"}, "Whether to include watch-only addresses (see RPC importaddress)"},
646
0
                },
647
0
            },
648
0
        },
649
0
        RPCResult{
650
0
            RPCResult::Type::OBJ, "", "",
651
0
            {
652
0
                {RPCResult::Type::STR_AMOUNT, "balance_change", "The wallet balance change (negative means decrease)."},
653
0
            }
654
0
        },
655
0
        RPCExamples{
656
0
            HelpExampleCli("simulaterawtransaction", "[\"myhex\"]")
657
0
            + HelpExampleRpc("simulaterawtransaction", "[\"myhex\"]")
658
0
        },
659
0
    [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
660
0
{
661
0
    const std::shared_ptr<const CWallet> rpc_wallet = GetWalletForJSONRPCRequest(request);
662
0
    if (!rpc_wallet) return UniValue::VNULL;
663
0
    const CWallet& wallet = *rpc_wallet;
664
665
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
666
667
0
    UniValue include_watchonly(UniValue::VNULL);
668
0
    if (request.params[1].isObject()) {
669
0
        UniValue options = request.params[1];
670
0
        RPCTypeCheckObj(options,
671
0
            {
672
0
                {"include_watchonly", UniValueType(UniValue::VBOOL)},
673
0
            },
674
0
            true, true);
675
676
0
        include_watchonly = options["include_watchonly"];
677
0
    }
678
679
0
    isminefilter filter = ISMINE_SPENDABLE;
680
0
    if (ParseIncludeWatchonly(include_watchonly, wallet)) {
681
0
        filter |= ISMINE_WATCH_ONLY;
682
0
    }
683
684
0
    const auto& txs = request.params[0].get_array();
685
0
    CAmount changes{0};
686
0
    std::map<COutPoint, CAmount> new_utxos; // UTXO:s that were made available in transaction array
687
0
    std::set<COutPoint> spent;
688
689
0
    for (size_t i = 0; i < txs.size(); ++i) {
690
0
        CMutableTransaction mtx;
691
0
        if (!DecodeHexTx(mtx, txs[i].get_str(), /* try_no_witness */ true, /* try_witness */ true)) {
692
0
            throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Transaction hex string decoding failure.");
693
0
        }
694
695
        // Fetch previous transactions (inputs)
696
0
        std::map<COutPoint, Coin> coins;
697
0
        for (const CTxIn& txin : mtx.vin) {
698
0
            coins[txin.prevout]; // Create empty map entry keyed by prevout.
699
0
        }
700
0
        wallet.chain().findCoins(coins);
701
702
        // Fetch debit; we are *spending* these; if the transaction is signed and
703
        // broadcast, we will lose everything in these
704
0
        for (const auto& txin : mtx.vin) {
705
0
            const auto& outpoint = txin.prevout;
706
0
            if (spent.count(outpoint)) {
707
0
                throw JSONRPCError(RPC_INVALID_PARAMETER, "Transaction(s) are spending the same output more than once");
708
0
            }
709
0
            if (new_utxos.count(outpoint)) {
710
0
                changes -= new_utxos.at(outpoint);
711
0
                new_utxos.erase(outpoint);
712
0
            } else {
713
0
                if (coins.at(outpoint).IsSpent()) {
714
0
                    throw JSONRPCError(RPC_INVALID_PARAMETER, "One or more transaction inputs are missing or have been spent already");
715
0
                }
716
0
                changes -= wallet.GetDebit(txin, filter);
717
0
            }
718
0
            spent.insert(outpoint);
719
0
        }
720
721
        // Iterate over outputs; we are *receiving* these, if the wallet considers
722
        // them "mine"; if the transaction is signed and broadcast, we will receive
723
        // everything in these
724
        // Also populate new_utxos in case these are spent in later transactions
725
726
0
        const auto& hash = mtx.GetHash();
727
0
        for (size_t i = 0; i < mtx.vout.size(); ++i) {
728
0
            const auto& txout = mtx.vout[i];
729
0
            bool is_mine = 0 < (wallet.IsMine(txout) & filter);
730
0
            changes += new_utxos[COutPoint(hash, i)] = is_mine ? txout.nValue : 0;
731
0
        }
732
0
    }
733
734
0
    UniValue result(UniValue::VOBJ);
735
0
    result.pushKV("balance_change", ValueFromAmount(changes));
736
737
0
    return result;
738
0
}
739
0
    };
740
0
}
741
742
static RPCHelpMan migratewallet()
743
0
{
744
0
    return RPCHelpMan{"migratewallet",
745
0
        "\nMigrate the wallet to a descriptor wallet.\n"
746
0
        "A new wallet backup will need to be made.\n"
747
0
        "\nThe migration process will create a backup of the wallet before migrating. This backup\n"
748
0
        "file will be named <wallet name>-<timestamp>.legacy.bak and can be found in the directory\n"
749
0
        "for this wallet. In the event of an incorrect migration, the backup can be restored using restorewallet."
750
0
        "\nEncrypted wallets must have the passphrase provided as an argument to this call.\n"
751
0
        "\nThis RPC may take a long time to complete. Increasing the RPC client timeout is recommended.",
752
0
        {
753
0
            {"wallet_name", RPCArg::Type::STR, RPCArg::DefaultHint{"the wallet name from the RPC endpoint"}, "The name of the wallet to migrate. If provided both here and in the RPC endpoint, the two must be identical."},
754
0
            {"passphrase", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "The wallet passphrase"},
755
0
        },
756
0
        RPCResult{
757
0
            RPCResult::Type::OBJ, "", "",
758
0
            {
759
0
                {RPCResult::Type::STR, "wallet_name", "The name of the primary migrated wallet"},
760
0
                {RPCResult::Type::STR, "watchonly_name", /*optional=*/true, "The name of the migrated wallet containing the watchonly scripts"},
761
0
                {RPCResult::Type::STR, "solvables_name", /*optional=*/true, "The name of the migrated wallet containing solvable but not watched scripts"},
762
0
                {RPCResult::Type::STR, "backup_path", "The location of the backup of the original wallet"},
763
0
            }
764
0
        },
765
0
        RPCExamples{
766
0
            HelpExampleCli("migratewallet", "")
767
0
            + HelpExampleRpc("migratewallet", "")
768
0
        },
769
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
770
0
        {
771
0
            std::string wallet_name;
772
0
            if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
773
0
                if (!(request.params[0].isNull() || request.params[0].get_str() == wallet_name)) {
774
0
                    throw JSONRPCError(RPC_INVALID_PARAMETER, "RPC endpoint wallet and wallet_name parameter specify different wallets");
775
0
                }
776
0
            } else {
777
0
                if (request.params[0].isNull()) {
778
0
                    throw JSONRPCError(RPC_INVALID_PARAMETER, "Either RPC endpoint wallet or wallet_name parameter must be provided");
779
0
                }
780
0
                wallet_name = request.params[0].get_str();
781
0
            }
782
783
0
            SecureString wallet_pass;
784
0
            wallet_pass.reserve(100);
785
0
            if (!request.params[1].isNull()) {
786
0
                wallet_pass = std::string_view{request.params[1].get_str()};
787
0
            }
788
789
0
            WalletContext& context = EnsureWalletContext(request.context);
790
0
            util::Result<MigrationResult> res = MigrateLegacyToDescriptor(wallet_name, wallet_pass, context);
791
0
            if (!res) {
792
0
                throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(res).original);
793
0
            }
794
795
0
            UniValue r{UniValue::VOBJ};
796
0
            r.pushKV("wallet_name", res->wallet_name);
797
0
            if (res->watchonly_wallet) {
798
0
                r.pushKV("watchonly_name", res->watchonly_wallet->GetName());
799
0
            }
800
0
            if (res->solvables_wallet) {
801
0
                r.pushKV("solvables_name", res->solvables_wallet->GetName());
802
0
            }
803
0
            r.pushKV("backup_path", res->backup_path.utf8string());
804
805
0
            return r;
806
0
        },
807
0
    };
808
0
}
809
810
RPCHelpMan gethdkeys()
811
0
{
812
0
    return RPCHelpMan{
813
0
        "gethdkeys",
814
0
        "\nList all BIP 32 HD keys in the wallet and which descriptors use them.\n",
815
0
        {
816
0
            {"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "", {
817
0
                {"active_only", RPCArg::Type::BOOL, RPCArg::Default{false}, "Show the keys for only active descriptors"},
818
0
                {"private", RPCArg::Type::BOOL, RPCArg::Default{false}, "Show private keys"}
819
0
            }},
820
0
        },
821
0
        RPCResult{RPCResult::Type::ARR, "", "", {
822
0
            {
823
0
                {RPCResult::Type::OBJ, "", "", {
824
0
                    {RPCResult::Type::STR, "xpub", "The extended public key"},
825
0
                    {RPCResult::Type::BOOL, "has_private", "Whether the wallet has the private key for this xpub"},
826
0
                    {RPCResult::Type::STR, "xprv", /*optional=*/true, "The extended private key if \"private\" is true"},
827
0
                    {RPCResult::Type::ARR, "descriptors", "Array of descriptor objects that use this HD key",
828
0
                    {
829
0
                        {RPCResult::Type::OBJ, "", "", {
830
0
                            {RPCResult::Type::STR, "desc", "Descriptor string representation"},
831
0
                            {RPCResult::Type::BOOL, "active", "Whether this descriptor is currently used to generate new addresses"},
832
0
                        }},
833
0
                    }},
834
0
                }},
835
0
            }
836
0
        }},
837
0
        RPCExamples{
838
0
            HelpExampleCli("gethdkeys", "") + HelpExampleRpc("gethdkeys", "")
839
0
            + HelpExampleCliNamed("gethdkeys", {{"active_only", "true"}, {"private", "true"}}) + HelpExampleRpcNamed("gethdkeys", {{"active_only", "true"}, {"private", "true"}})
840
0
        },
841
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
842
0
        {
843
0
            const std::shared_ptr<const CWallet> wallet = GetWalletForJSONRPCRequest(request);
844
0
            if (!wallet) return UniValue::VNULL;
845
846
0
            if (!wallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
847
0
                throw JSONRPCError(RPC_WALLET_ERROR, "gethdkeys is not available for non-descriptor wallets");
848
0
            }
849
850
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
851
852
0
            UniValue options{request.params[0].isNull() ? UniValue::VOBJ : request.params[0]};
853
0
            const bool active_only{options.exists("active_only") ? options["active_only"].get_bool() : false};
854
0
            const bool priv{options.exists("private") ? options["private"].get_bool() : false};
855
0
            if (priv) {
856
0
                EnsureWalletIsUnlocked(*wallet);
857
0
            }
858
859
860
0
            std::set<ScriptPubKeyMan*> spkms;
861
0
            if (active_only) {
862
0
                spkms = wallet->GetActiveScriptPubKeyMans();
863
0
            } else {
864
0
                spkms = wallet->GetAllScriptPubKeyMans();
865
0
            }
866
867
0
            std::map<CExtPubKey, std::set<std::tuple<std::string, bool, bool>>> wallet_xpubs;
868
0
            std::map<CExtPubKey, CExtKey> wallet_xprvs;
869
0
            for (auto* spkm : spkms) {
870
0
                auto* desc_spkm{dynamic_cast<DescriptorScriptPubKeyMan*>(spkm)};
871
0
                CHECK_NONFATAL(desc_spkm);
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
872
0
                LOCK(desc_spkm->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
873
0
                WalletDescriptor w_desc = desc_spkm->GetWalletDescriptor();
874
875
                // Retrieve the pubkeys from the descriptor
876
0
                std::set<CPubKey> desc_pubkeys;
877
0
                std::set<CExtPubKey> desc_xpubs;
878
0
                w_desc.descriptor->GetPubKeys(desc_pubkeys, desc_xpubs);
879
0
                for (const CExtPubKey& xpub : desc_xpubs) {
880
0
                    std::string desc_str;
881
0
                    bool ok = desc_spkm->GetDescriptorString(desc_str, false);
882
0
                    CHECK_NONFATAL(ok);
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
883
0
                    wallet_xpubs[xpub].emplace(desc_str, wallet->IsActiveScriptPubKeyMan(*spkm), desc_spkm->HasPrivKey(xpub.pubkey.GetID()));
884
0
                    if (std::optional<CKey> key = priv ? desc_spkm->GetKey(xpub.pubkey.GetID()) : std::nullopt) {
885
0
                        wallet_xprvs[xpub] = CExtKey(xpub, *key);
886
0
                    }
887
0
                }
888
0
            }
889
890
0
            UniValue response(UniValue::VARR);
891
0
            for (const auto& [xpub, descs] : wallet_xpubs) {
892
0
                bool has_xprv = false;
893
0
                UniValue descriptors(UniValue::VARR);
894
0
                for (const auto& [desc, active, has_priv] : descs) {
895
0
                    UniValue d(UniValue::VOBJ);
896
0
                    d.pushKV("desc", desc);
897
0
                    d.pushKV("active", active);
898
0
                    has_xprv |= has_priv;
899
900
0
                    descriptors.push_back(std::move(d));
901
0
                }
902
0
                UniValue xpub_info(UniValue::VOBJ);
903
0
                xpub_info.pushKV("xpub", EncodeExtPubKey(xpub));
904
0
                xpub_info.pushKV("has_private", has_xprv);
905
0
                if (priv) {
906
0
                    xpub_info.pushKV("xprv", EncodeExtKey(wallet_xprvs.at(xpub)));
907
0
                }
908
0
                xpub_info.pushKV("descriptors", std::move(descriptors));
909
910
0
                response.push_back(std::move(xpub_info));
911
0
            }
912
913
0
            return response;
914
0
        },
915
0
    };
916
0
}
917
918
static RPCHelpMan createwalletdescriptor()
919
0
{
920
0
    return RPCHelpMan{"createwalletdescriptor",
921
0
        "Creates the wallet's descriptor for the given address type. "
922
0
        "The address type must be one that the wallet does not already have a descriptor for."
923
0
        + HELP_REQUIRING_PASSPHRASE,
924
0
        {
925
0
            {"type", RPCArg::Type::STR, RPCArg::Optional::NO, "The address type the descriptor will produce. Options are \"legacy\", \"p2sh-segwit\", \"bech32\", and \"bech32m\"."},
926
0
            {"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "", {
927
0
                {"internal", RPCArg::Type::BOOL, RPCArg::DefaultHint{"Both external and internal will be generated unless this parameter is specified"}, "Whether to only make one descriptor that is internal (if parameter is true) or external (if parameter is false)"},
928
0
                {"hdkey", RPCArg::Type::STR, RPCArg::DefaultHint{"The HD key used by all other active descriptors"}, "The HD key that the wallet knows the private key of, listed using 'gethdkeys', to use for this descriptor's key"},
929
0
            }},
930
0
        },
931
0
        RPCResult{
932
0
            RPCResult::Type::OBJ, "", "",
933
0
            {
934
0
                {RPCResult::Type::ARR, "descs", "The public descriptors that were added to the wallet",
935
0
                    {{RPCResult::Type::STR, "", ""}}
936
0
                }
937
0
            },
938
0
        },
939
0
        RPCExamples{
940
0
            HelpExampleCli("createwalletdescriptor", "bech32m")
941
0
            + HelpExampleRpc("createwalletdescriptor", "bech32m")
942
0
        },
943
0
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
944
0
        {
945
0
            std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
946
0
            if (!pwallet) return UniValue::VNULL;
947
948
            //  Make sure wallet is a descriptor wallet
949
0
            if (!pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
950
0
                throw JSONRPCError(RPC_WALLET_ERROR, "createwalletdescriptor is not available for non-descriptor wallets");
951
0
            }
952
953
0
            std::optional<OutputType> output_type = ParseOutputType(request.params[0].get_str());
954
0
            if (!output_type) {
955
0
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Unknown address type '%s'", request.params[0].get_str()));
Line
Count
Source
1172
0
#define strprintf tfm::format
956
0
            }
957
958
0
            UniValue options{request.params[1].isNull() ? UniValue::VOBJ : request.params[1]};
959
0
            UniValue internal_only{options["internal"]};
960
0
            UniValue hdkey{options["hdkey"]};
961
962
0
            std::vector<bool> internals;
963
0
            if (internal_only.isNull()) {
964
0
                internals.push_back(false);
965
0
                internals.push_back(true);
966
0
            } else {
967
0
                internals.push_back(internal_only.get_bool());
968
0
            }
969
970
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
971
0
            EnsureWalletIsUnlocked(*pwallet);
972
973
0
            CExtPubKey xpub;
974
0
            if (hdkey.isNull()) {
975
0
                std::set<CExtPubKey> active_xpubs = pwallet->GetActiveHDPubKeys();
976
0
                if (active_xpubs.size() != 1) {
977
0
                    throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to determine which HD key to use from active descriptors. Please specify with 'hdkey'");
978
0
                }
979
0
                xpub = *active_xpubs.begin();
980
0
            } else {
981
0
                xpub = DecodeExtPubKey(hdkey.get_str());
982
0
                if (!xpub.pubkey.IsValid()) {
983
0
                    throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to parse HD key. Please provide a valid xpub");
984
0
                }
985
0
            }
986
987
0
            std::optional<CKey> key = pwallet->GetKey(xpub.pubkey.GetID());
988
0
            if (!key) {
989
0
                throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Private key for %s is not known", EncodeExtPubKey(xpub)));
Line
Count
Source
1172
0
#define strprintf tfm::format
990
0
            }
991
0
            CExtKey active_hdkey(xpub, *key);
992
993
0
            std::vector<std::reference_wrapper<DescriptorScriptPubKeyMan>> spkms;
994
0
            WalletBatch batch{pwallet->GetDatabase()};
995
0
            for (bool internal : internals) {
996
0
                WalletDescriptor w_desc = GenerateWalletDescriptor(xpub, *output_type, internal);
997
0
                uint256 w_id = DescriptorID(*w_desc.descriptor);
998
0
                if (!pwallet->GetScriptPubKeyMan(w_id)) {
999
0
                    spkms.emplace_back(pwallet->SetupDescriptorScriptPubKeyMan(batch, active_hdkey, *output_type, internal));
1000
0
                }
1001
0
            }
1002
0
            if (spkms.empty()) {
1003
0
                throw JSONRPCError(RPC_WALLET_ERROR, "Descriptor already exists");
1004
0
            }
1005
1006
            // Fetch each descspkm from the wallet in order to get the descriptor strings
1007
0
            UniValue descs{UniValue::VARR};
1008
0
            for (const auto& spkm : spkms) {
1009
0
                std::string desc_str;
1010
0
                bool ok = spkm.get().GetDescriptorString(desc_str, false);
1011
0
                CHECK_NONFATAL(ok);
Line
Count
Source
103
0
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
1012
0
                descs.push_back(desc_str);
1013
0
            }
1014
0
            UniValue out{UniValue::VOBJ};
1015
0
            out.pushKV("descs", std::move(descs));
1016
0
            return out;
1017
0
        }
1018
0
    };
1019
0
}
1020
1021
// addresses
1022
RPCHelpMan getaddressinfo();
1023
RPCHelpMan getnewaddress();
1024
RPCHelpMan getrawchangeaddress();
1025
RPCHelpMan setlabel();
1026
RPCHelpMan listaddressgroupings();
1027
RPCHelpMan addmultisigaddress();
1028
RPCHelpMan keypoolrefill();
1029
RPCHelpMan newkeypool();
1030
RPCHelpMan getaddressesbylabel();
1031
RPCHelpMan listlabels();
1032
#ifdef ENABLE_EXTERNAL_SIGNER
1033
RPCHelpMan walletdisplayaddress();
1034
#endif // ENABLE_EXTERNAL_SIGNER
1035
1036
// backup
1037
RPCHelpMan dumpprivkey();
1038
RPCHelpMan importprivkey();
1039
RPCHelpMan importaddress();
1040
RPCHelpMan importpubkey();
1041
RPCHelpMan dumpwallet();
1042
RPCHelpMan importwallet();
1043
RPCHelpMan importprunedfunds();
1044
RPCHelpMan removeprunedfunds();
1045
RPCHelpMan importmulti();
1046
RPCHelpMan importdescriptors();
1047
RPCHelpMan listdescriptors();
1048
RPCHelpMan backupwallet();
1049
RPCHelpMan restorewallet();
1050
1051
// coins
1052
RPCHelpMan getreceivedbyaddress();
1053
RPCHelpMan getreceivedbylabel();
1054
RPCHelpMan getbalance();
1055
RPCHelpMan getunconfirmedbalance();
1056
RPCHelpMan lockunspent();
1057
RPCHelpMan listlockunspent();
1058
RPCHelpMan getbalances();
1059
RPCHelpMan listunspent();
1060
1061
// encryption
1062
RPCHelpMan walletpassphrase();
1063
RPCHelpMan walletpassphrasechange();
1064
RPCHelpMan walletlock();
1065
RPCHelpMan encryptwallet();
1066
1067
// spend
1068
RPCHelpMan sendtoaddress();
1069
RPCHelpMan sendmany();
1070
RPCHelpMan settxfee();
1071
RPCHelpMan fundrawtransaction();
1072
RPCHelpMan bumpfee();
1073
RPCHelpMan psbtbumpfee();
1074
RPCHelpMan send();
1075
RPCHelpMan sendall();
1076
RPCHelpMan walletprocesspsbt();
1077
RPCHelpMan walletcreatefundedpsbt();
1078
RPCHelpMan signrawtransactionwithwallet();
1079
1080
// signmessage
1081
RPCHelpMan signmessage();
1082
1083
// transactions
1084
RPCHelpMan listreceivedbyaddress();
1085
RPCHelpMan listreceivedbylabel();
1086
RPCHelpMan listtransactions();
1087
RPCHelpMan listsinceblock();
1088
RPCHelpMan gettransaction();
1089
RPCHelpMan abandontransaction();
1090
RPCHelpMan rescanblockchain();
1091
RPCHelpMan abortrescan();
1092
1093
std::span<const CRPCCommand> GetWalletRPCCommands()
1094
0
{
1095
0
    static const CRPCCommand commands[]{
1096
0
        {"rawtransactions", &fundrawtransaction},
1097
0
        {"wallet", &abandontransaction},
1098
0
        {"wallet", &abortrescan},
1099
0
        {"wallet", &addmultisigaddress},
1100
0
        {"wallet", &backupwallet},
1101
0
        {"wallet", &bumpfee},
1102
0
        {"wallet", &psbtbumpfee},
1103
0
        {"wallet", &createwallet},
1104
0
        {"wallet", &createwalletdescriptor},
1105
0
        {"wallet", &restorewallet},
1106
0
        {"wallet", &dumpprivkey},
1107
0
        {"wallet", &dumpwallet},
1108
0
        {"wallet", &encryptwallet},
1109
0
        {"wallet", &getaddressesbylabel},
1110
0
        {"wallet", &getaddressinfo},
1111
0
        {"wallet", &getbalance},
1112
0
        {"wallet", &gethdkeys},
1113
0
        {"wallet", &getnewaddress},
1114
0
        {"wallet", &getrawchangeaddress},
1115
0
        {"wallet", &getreceivedbyaddress},
1116
0
        {"wallet", &getreceivedbylabel},
1117
0
        {"wallet", &gettransaction},
1118
0
        {"wallet", &getunconfirmedbalance},
1119
0
        {"wallet", &getbalances},
1120
0
        {"wallet", &getwalletinfo},
1121
0
        {"wallet", &importaddress},
1122
0
        {"wallet", &importdescriptors},
1123
0
        {"wallet", &importmulti},
1124
0
        {"wallet", &importprivkey},
1125
0
        {"wallet", &importprunedfunds},
1126
0
        {"wallet", &importpubkey},
1127
0
        {"wallet", &importwallet},
1128
0
        {"wallet", &keypoolrefill},
1129
0
        {"wallet", &listaddressgroupings},
1130
0
        {"wallet", &listdescriptors},
1131
0
        {"wallet", &listlabels},
1132
0
        {"wallet", &listlockunspent},
1133
0
        {"wallet", &listreceivedbyaddress},
1134
0
        {"wallet", &listreceivedbylabel},
1135
0
        {"wallet", &listsinceblock},
1136
0
        {"wallet", &listtransactions},
1137
0
        {"wallet", &listunspent},
1138
0
        {"wallet", &listwalletdir},
1139
0
        {"wallet", &listwallets},
1140
0
        {"wallet", &loadwallet},
1141
0
        {"wallet", &lockunspent},
1142
0
        {"wallet", &migratewallet},
1143
0
        {"wallet", &newkeypool},
1144
0
        {"wallet", &removeprunedfunds},
1145
0
        {"wallet", &rescanblockchain},
1146
0
        {"wallet", &send},
1147
0
        {"wallet", &sendmany},
1148
0
        {"wallet", &sendtoaddress},
1149
0
        {"wallet", &sethdseed},
1150
0
        {"wallet", &setlabel},
1151
0
        {"wallet", &settxfee},
1152
0
        {"wallet", &setwalletflag},
1153
0
        {"wallet", &signmessage},
1154
0
        {"wallet", &signrawtransactionwithwallet},
1155
0
        {"wallet", &simulaterawtransaction},
1156
0
        {"wallet", &sendall},
1157
0
        {"wallet", &unloadwallet},
1158
0
        {"wallet", &upgradewallet},
1159
0
        {"wallet", &walletcreatefundedpsbt},
1160
#ifdef ENABLE_EXTERNAL_SIGNER
1161
        {"wallet", &walletdisplayaddress},
1162
#endif // ENABLE_EXTERNAL_SIGNER
1163
0
        {"wallet", &walletlock},
1164
0
        {"wallet", &walletpassphrase},
1165
0
        {"wallet", &walletpassphrasechange},
1166
0
        {"wallet", &walletprocesspsbt},
1167
0
    };
1168
0
    return commands;
1169
0
}
1170
} // namespace wallet