Coverage Report

Created: 2026-11-13 17:38

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