Branch data Line data Source code
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 <univalue.h>
13 : : #include <util/translation.h>
14 : : #include <wallet/context.h>
15 : : #include <wallet/receive.h>
16 : : #include <wallet/rpc/util.h>
17 : : #include <wallet/rpc/wallet.h>
18 : : #include <wallet/wallet.h>
19 : : #include <wallet/walletutil.h>
20 : :
21 : : #include <optional>
22 : : #include <string_view>
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 : 1286 : static RPCHelpMan getwalletinfo()
35 : : {
36 : 1286 : return RPCHelpMan{"getwalletinfo",
37 [ + - ]: 2572 : "Returns an object containing various wallet state info.\n",
38 : : {},
39 [ + - ]: 2572 : RPCResult{
40 [ + - ]: 2572 : RPCResult::Type::OBJ, "", "",
41 : : {
42 : : {
43 [ + - + - ]: 2572 : {RPCResult::Type::STR, "walletname", "the wallet name"},
44 [ + - + - ]: 2572 : {RPCResult::Type::NUM, "walletversion", "(DEPRECATED) only related to unsupported legacy wallet, returns the latest version 169900 for backwards compatibility"},
45 [ + - + - ]: 2572 : {RPCResult::Type::STR, "format", "the database format (only sqlite)"},
46 [ + - + - ]: 2572 : {RPCResult::Type::NUM, "txcount", "the total number of transactions in the wallet"},
47 [ + - + - ]: 2572 : {RPCResult::Type::NUM, "keypoolsize", "how many new keys are pre-generated (only counts external keys)"},
48 [ + - + - ]: 2572 : {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)"},
49 [ + - + - ]: 2572 : {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)"},
50 [ + - + - ]: 2572 : {RPCResult::Type::STR_AMOUNT, "paytxfee", "the transaction fee configuration, set in " + CURRENCY_UNIT + "/kvB"},
51 [ + - + - ]: 2572 : {RPCResult::Type::BOOL, "private_keys_enabled", "false if privatekeys are disabled for this wallet (enforced watch-only wallet)"},
52 [ + - + - ]: 2572 : {RPCResult::Type::BOOL, "avoid_reuse", "whether this wallet tracks clean/dirty coins in terms of reuse"},
53 [ + - + - ]: 2572 : {RPCResult::Type::OBJ, "scanning", "current scanning details, or false if no scan is in progress",
54 : : {
55 [ + - + - ]: 2572 : {RPCResult::Type::NUM, "duration", "elapsed seconds since scan start"},
56 [ + - + - ]: 2572 : {RPCResult::Type::NUM, "progress", "scanning progress percentage [0.0, 1.0]"},
57 : : }, /*skip_type_check=*/true},
58 [ + - + - ]: 2572 : {RPCResult::Type::BOOL, "descriptors", "whether this wallet uses descriptors for output script management"},
59 [ + - + - ]: 2572 : {RPCResult::Type::BOOL, "external_signer", "whether this wallet is configured to use an external signer such as a hardware wallet"},
60 [ + - + - ]: 2572 : {RPCResult::Type::BOOL, "blank", "Whether this wallet intentionally does not contain any keys, scripts, or descriptors"},
61 [ + - + - ]: 2572 : {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."},
62 [ + - + - ]: 2572 : {RPCResult::Type::ARR, "flags", "The flags currently set on the wallet",
63 : : {
64 [ + - + - ]: 2572 : {RPCResult::Type::STR, "flag", "The name of the flag"},
65 : : }},
66 : : RESULT_LAST_PROCESSED_BLOCK,
67 : : }},
68 [ + - + - : 51440 : },
+ - + - +
+ + + + +
- - - - -
- ]
69 : 1286 : RPCExamples{
70 [ + - + - : 2572 : HelpExampleCli("getwalletinfo", "")
+ - ]
71 [ + - + - : 5144 : + HelpExampleRpc("getwalletinfo", "")
+ - + - ]
72 [ + - ]: 1286 : },
73 : 1286 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
74 : : {
75 [ - + ]: 457 : const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
76 [ - + ]: 440 : if (!pwallet) return UniValue::VNULL;
77 : :
78 : : // Make sure the results are valid at least up to the most recent block
79 : : // the user could have gotten from another RPC command prior to now
80 [ + - ]: 440 : pwallet->BlockUntilSyncedToCurrentChain();
81 : :
82 [ + - ]: 440 : LOCK(pwallet->cs_wallet);
83 : :
84 : 440 : UniValue obj(UniValue::VOBJ);
85 : :
86 : 440 : const int latest_legacy_wallet_minversion{169900};
87 : :
88 [ + - ]: 440 : size_t kpExternalSize = pwallet->KeypoolCountExternalKeys();
89 [ + - + - : 880 : obj.pushKV("walletname", pwallet->GetName());
+ - ]
90 [ + - + - : 880 : obj.pushKV("walletversion", latest_legacy_wallet_minversion);
+ - ]
91 [ + - + - : 880 : obj.pushKV("format", pwallet->GetDatabase().Format());
+ - + - ]
92 [ + - + - : 880 : obj.pushKV("txcount", pwallet->mapWallet.size());
+ - ]
93 [ + - + - : 880 : obj.pushKV("keypoolsize", kpExternalSize);
+ - ]
94 [ + - + - : 880 : obj.pushKV("keypoolsize_hd_internal", pwallet->GetKeyPoolSize() - kpExternalSize);
+ - + - ]
95 : :
96 [ + - + + ]: 440 : if (pwallet->HasEncryptionKeys()) {
97 [ + - + - : 82 : obj.pushKV("unlocked_until", pwallet->nRelockTime);
+ - ]
98 : : }
99 [ + - + - : 880 : obj.pushKV("paytxfee", ValueFromAmount(pwallet->m_pay_tx_fee.GetFeePerK()));
+ - ]
100 [ + - + - : 880 : obj.pushKV("private_keys_enabled", !pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
+ - + - ]
101 [ + - + - : 880 : obj.pushKV("avoid_reuse", pwallet->IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE));
+ - + - ]
102 [ - + ]: 440 : if (pwallet->IsScanning()) {
103 : 0 : UniValue scanning(UniValue::VOBJ);
104 [ # # # # : 0 : scanning.pushKV("duration", Ticks<std::chrono::seconds>(pwallet->ScanningDuration()));
# # ]
105 [ # # # # : 0 : scanning.pushKV("progress", pwallet->ScanningProgress());
# # ]
106 [ # # # # ]: 0 : obj.pushKV("scanning", std::move(scanning));
107 : 0 : } else {
108 [ + - + - : 880 : obj.pushKV("scanning", false);
+ - ]
109 : : }
110 [ + - + - : 880 : obj.pushKV("descriptors", pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
+ - + - ]
111 [ + - + - : 880 : obj.pushKV("external_signer", pwallet->IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER));
+ - + - ]
112 [ + - + - : 880 : obj.pushKV("blank", pwallet->IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET));
+ - + - ]
113 [ + + ]: 440 : if (int64_t birthtime = pwallet->GetBirthTime(); birthtime != UNKNOWN_TIME) {
114 [ + - + - : 802 : obj.pushKV("birthtime", birthtime);
+ - ]
115 : : }
116 : :
117 : : // Push known flags
118 : 440 : UniValue flags(UniValue::VARR);
119 [ + - ]: 440 : uint64_t wallet_flags = pwallet->GetWalletFlags();
120 [ + + ]: 28600 : for (uint64_t i = 0; i < 64; ++i) {
121 : 28160 : uint64_t flag = uint64_t{1} << i;
122 [ + + ]: 28160 : if (flag & wallet_flags) {
123 [ + - ]: 1105 : if (flag & KNOWN_WALLET_FLAGS) {
124 [ + - + - : 1105 : flags.push_back(WALLET_FLAG_TO_STRING.at(WalletFlags{flag}));
+ - ]
125 : : } else {
126 [ # # # # : 0 : flags.push_back(strprintf("unknown_flag_%u", i));
# # ]
127 : : }
128 : : }
129 : : }
130 [ + - + - : 880 : obj.pushKV("flags", flags);
+ - ]
131 : :
132 [ + - ]: 440 : AppendLastProcessedBlock(obj, *pwallet);
133 : 440 : return obj;
134 [ + - ]: 1320 : },
135 [ + - + - ]: 5144 : };
136 [ + - + - : 24434 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - -
- ]
137 : :
138 : 906 : static RPCHelpMan listwalletdir()
139 : : {
140 : 906 : return RPCHelpMan{"listwalletdir",
141 [ + - ]: 1812 : "Returns a list of wallets in the wallet directory.\n",
142 : : {},
143 [ + - ]: 1812 : RPCResult{
144 [ + - ]: 1812 : RPCResult::Type::OBJ, "", "",
145 : : {
146 [ + - + - ]: 1812 : {RPCResult::Type::ARR, "wallets", "",
147 : : {
148 [ + - + - ]: 1812 : {RPCResult::Type::OBJ, "", "",
149 : : {
150 [ + - + - ]: 1812 : {RPCResult::Type::STR, "name", "The wallet name"},
151 [ + - + - ]: 1812 : {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to loading the wallet.",
152 : : {
153 [ + - + - ]: 1812 : {RPCResult::Type::STR, "", ""},
154 : : }},
155 : : }},
156 : : }},
157 : : }
158 [ + - + - : 9966 : },
+ - + - +
- + + + +
+ + + + -
- - - - -
- - ]
159 : 906 : RPCExamples{
160 [ + - + - : 1812 : HelpExampleCli("listwalletdir", "")
+ - ]
161 [ + - + - : 3624 : + HelpExampleRpc("listwalletdir", "")
+ - + - ]
162 [ + - ]: 906 : },
163 : 906 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
164 : : {
165 : 77 : UniValue wallets(UniValue::VARR);
166 [ + - + - : 1757 : for (const auto& [path, db_type] : ListDatabases(GetWalletDir())) {
+ + ]
167 : 1603 : UniValue wallet(UniValue::VOBJ);
168 [ + - + - : 3206 : wallet.pushKV("name", path.utf8string());
+ - + - ]
169 : 1603 : UniValue warnings(UniValue::VARR);
170 [ + + ]: 1603 : if (db_type == "bdb") {
171 [ + - + - ]: 82 : warnings.push_back("This wallet is a legacy wallet and will need to be migrated with migratewallet before it can be loaded");
172 : : }
173 [ + - + - : 3206 : wallet.pushKV("warnings", warnings);
+ - ]
174 [ + - ]: 1603 : wallets.push_back(std::move(wallet));
175 : 1680 : }
176 : :
177 : 77 : UniValue result(UniValue::VOBJ);
178 [ + - + - ]: 154 : result.pushKV("wallets", std::move(wallets));
179 : 77 : return result;
180 : 77 : },
181 [ + - + - ]: 3624 : };
182 [ + - + - : 4530 : }
+ - + - +
- - - ]
183 : :
184 : 908 : static RPCHelpMan listwallets()
185 : : {
186 : 908 : return RPCHelpMan{"listwallets",
187 [ + - ]: 1816 : "Returns a list of currently loaded wallets.\n"
188 : : "For full information on the wallet, use \"getwalletinfo\"\n",
189 : : {},
190 [ + - ]: 1816 : RPCResult{
191 [ + - ]: 1816 : RPCResult::Type::ARR, "", "",
192 : : {
193 [ + - + - ]: 1816 : {RPCResult::Type::STR, "walletname", "the wallet name"},
194 : : }
195 [ + - + - : 2724 : },
+ + - - ]
196 : 908 : RPCExamples{
197 [ + - + - : 1816 : HelpExampleCli("listwallets", "")
+ - ]
198 [ + - + - : 3632 : + HelpExampleRpc("listwallets", "")
+ - + - ]
199 [ + - ]: 908 : },
200 : 908 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
201 : : {
202 : 79 : UniValue obj(UniValue::VARR);
203 : :
204 [ + - ]: 79 : WalletContext& context = EnsureWalletContext(request.context);
205 [ + - + + ]: 703 : for (const std::shared_ptr<CWallet>& wallet : GetWallets(context)) {
206 [ + - ]: 624 : LOCK(wallet->cs_wallet);
207 [ + - + - : 624 : obj.push_back(wallet->GetName());
+ - ]
208 : 703 : }
209 : :
210 : 79 : return obj;
211 : 0 : },
212 [ + - + - ]: 3632 : };
213 [ + - ]: 908 : }
214 : :
215 : 983 : static RPCHelpMan loadwallet()
216 : : {
217 : 983 : return RPCHelpMan{
218 : 983 : "loadwallet",
219 [ + - ]: 1966 : "Loads a wallet from a wallet file or directory."
220 : : "\nNote that all wallet command-line options used when starting bitcoind will be"
221 : : "\napplied to the new wallet.\n",
222 : : {
223 [ + - + - ]: 1966 : {"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 [ + - + - ]: 1966 : {"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 : : },
226 [ + - ]: 1966 : RPCResult{
227 [ + - + - ]: 1966 : RPCResult::Type::OBJ, "", "",
228 : : {
229 [ + - + - ]: 1966 : {RPCResult::Type::STR, "name", "The wallet name if loaded successfully."},
230 [ + - + - ]: 1966 : {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to loading the wallet.",
231 : : {
232 [ + - + - ]: 1966 : {RPCResult::Type::STR, "", ""},
233 : : }},
234 : : }
235 [ + - + - : 6881 : },
+ - + + +
+ - - -
- ]
236 : 983 : RPCExamples{
237 : : "\nLoad wallet from the wallet dir:\n"
238 [ + - + - : 1966 : + HelpExampleCli("loadwallet", "\"walletname\"")
+ - + - ]
239 [ + - + - : 3932 : + HelpExampleRpc("loadwallet", "\"walletname\"")
+ - + - ]
240 : 983 : + "\nLoad wallet using absolute path (Unix):\n"
241 [ + - + - : 3932 : + HelpExampleCli("loadwallet", "\"/path/to/walletname/\"")
+ - + - ]
242 [ + - + - : 3932 : + HelpExampleRpc("loadwallet", "\"/path/to/walletname/\"")
+ - + - ]
243 : 983 : + "\nLoad wallet using absolute path (Windows):\n"
244 [ + - + - : 3932 : + HelpExampleCli("loadwallet", "\"DriveLetter:\\path\\to\\walletname\\\"")
+ - + - ]
245 [ + - + - : 3932 : + HelpExampleRpc("loadwallet", "\"DriveLetter:\\path\\to\\walletname\\\"")
+ - + - ]
246 [ + - ]: 983 : },
247 : 983 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
248 : : {
249 : 154 : WalletContext& context = EnsureWalletContext(request.context);
250 [ - + ]: 154 : const std::string name(request.params[0].get_str());
251 : :
252 [ + - ]: 154 : DatabaseOptions options;
253 : 154 : DatabaseStatus status;
254 [ + - ]: 154 : ReadDatabaseArgs(*context.args, options);
255 : 154 : options.require_existing = true;
256 [ + - ]: 154 : bilingual_str error;
257 : 154 : std::vector<bilingual_str> warnings;
258 [ + - + + : 154 : std::optional<bool> load_on_start = request.params[1].isNull() ? std::nullopt : std::optional<bool>(request.params[1].get_bool());
+ - + - ]
259 : :
260 : 154 : {
261 [ + - ]: 154 : LOCK(context.wallets_mutex);
262 [ + + + + ]: 792 : if (std::any_of(context.wallets.begin(), context.wallets.end(), [&name](const auto& wallet) { return wallet->GetName() == name; })) {
263 [ + - + - ]: 6 : throw JSONRPCError(RPC_WALLET_ALREADY_LOADED, "Wallet \"" + name + "\" is already loaded.");
264 : : }
265 : 2 : }
266 : :
267 [ + - ]: 152 : std::shared_ptr<CWallet> const wallet = LoadWallet(context, name, load_on_start, options, status, error, warnings);
268 : :
269 [ + + ]: 152 : HandleWalletError(wallet, status, error);
270 : :
271 : 137 : UniValue obj(UniValue::VOBJ);
272 [ + - + - : 274 : obj.pushKV("name", wallet->GetName());
+ - ]
273 [ + - ]: 137 : PushWarnings(warnings, obj);
274 : :
275 [ + - ]: 137 : return obj;
276 : 325 : },
277 [ + - + - : 5898 : };
+ + - - ]
278 [ + - + - : 6881 : }
+ - + - +
- - - -
- ]
279 : :
280 : 837 : static RPCHelpMan setwalletflag()
281 : : {
282 : 837 : std::string flags;
283 [ + + ]: 6696 : for (auto& it : STRING_TO_WALLET_FLAG)
284 [ + + ]: 5859 : if (it.second & MUTABLE_WALLET_FLAGS)
285 [ - + + - ]: 1674 : flags += (flags == "" ? "" : ", ") + it.first;
286 : :
287 : 837 : return RPCHelpMan{
288 [ + - ]: 1674 : "setwalletflag",
289 [ + - ]: 1674 : "Change the state of the given wallet flag for a wallet.\n",
290 : : {
291 [ + - + - ]: 1674 : {"flag", RPCArg::Type::STR, RPCArg::Optional::NO, "The name of the flag to change. Current available flags: " + flags},
292 [ + - + - : 2511 : {"value", RPCArg::Type::BOOL, RPCArg::Default{true}, "The new state."},
+ - ]
293 : : },
294 [ + - ]: 1674 : RPCResult{
295 [ + - + - ]: 1674 : RPCResult::Type::OBJ, "", "",
296 : : {
297 [ + - + - ]: 1674 : {RPCResult::Type::STR, "flag_name", "The name of the flag that was modified"},
298 [ + - + - ]: 1674 : {RPCResult::Type::BOOL, "flag_state", "The new state of the flag"},
299 [ + - + - ]: 1674 : {RPCResult::Type::STR, "warnings", /*optional=*/true, "Any warnings associated with the change"},
300 : : }
301 [ + - + - : 5859 : },
+ + - - ]
302 : 837 : RPCExamples{
303 [ + - + - : 1674 : HelpExampleCli("setwalletflag", "avoid_reuse")
+ - ]
304 [ + - + - : 3348 : + HelpExampleRpc("setwalletflag", "\"avoid_reuse\"")
+ - + - ]
305 [ + - ]: 837 : },
306 : 837 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
307 : : {
308 : 8 : std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
309 [ - + ]: 8 : if (!pwallet) return UniValue::VNULL;
310 : :
311 [ + - + - : 8 : std::string flag_str = request.params[0].get_str();
- + ]
312 [ + - + + : 8 : bool value = request.params[1].isNull() || request.params[1].get_bool();
+ - + - +
+ ]
313 : :
314 [ + + ]: 8 : if (!STRING_TO_WALLET_FLAG.contains(flag_str)) {
315 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unknown wallet flag: %s", flag_str));
316 : : }
317 : :
318 [ + - ]: 7 : auto flag = STRING_TO_WALLET_FLAG.at(flag_str);
319 : :
320 [ + + ]: 7 : if (!(flag & MUTABLE_WALLET_FLAGS)) {
321 [ + - + - ]: 6 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Wallet flag is immutable: %s", flag_str));
322 : : }
323 : :
324 : 4 : UniValue res(UniValue::VOBJ);
325 : :
326 [ + - + + ]: 4 : if (pwallet->IsWalletFlagSet(flag) == value) {
327 [ + + + - : 5 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Wallet flag is already set to %s: %s", value ? "true" : "false", flag_str));
+ - ]
328 : : }
329 : :
330 [ + - + - : 4 : res.pushKV("flag_name", flag_str);
+ - ]
331 [ + - + - : 4 : res.pushKV("flag_state", value);
+ - ]
332 : :
333 [ + + ]: 2 : if (value) {
334 [ + - ]: 1 : pwallet->SetWalletFlag(flag);
335 : : } else {
336 [ + - ]: 1 : pwallet->UnsetWalletFlag(flag);
337 : : }
338 : :
339 [ + - + + : 2 : if (flag && value && WALLET_FLAG_CAVEATS.contains(flag)) {
+ - ]
340 [ + - + - : 2 : res.pushKV("warnings", WALLET_FLAG_CAVEATS.at(flag));
+ - + - ]
341 : : }
342 : :
343 : 2 : return res;
344 : 12 : },
345 [ + - + - : 4185 : };
+ + - - ]
346 [ + - + - : 6696 : }
+ - + - +
- - - -
- ]
347 : :
348 : 1414 : static RPCHelpMan createwallet()
349 : : {
350 : 1414 : return RPCHelpMan{
351 : 1414 : "createwallet",
352 [ + - ]: 2828 : "Creates and loads a new wallet.\n",
353 : : {
354 [ + - + - ]: 2828 : {"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."},
355 [ + - + - : 4242 : {"disable_private_keys", RPCArg::Type::BOOL, RPCArg::Default{false}, "Disable the possibility of private keys (only watchonlys are possible in this mode)."},
+ - ]
356 [ + - + - : 4242 : {"blank", RPCArg::Type::BOOL, RPCArg::Default{false}, "Create a blank wallet. A blank wallet has no keys."},
+ - ]
357 [ + - + - ]: 2828 : {"passphrase", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Encrypt the wallet with this passphrase."},
358 [ + - + - : 4242 : {"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."},
+ - ]
359 [ + - + - : 4242 : {"descriptors", RPCArg::Type::BOOL, RPCArg::Default{true}, "If set, must be \"true\""},
+ - ]
360 [ + - + - ]: 2828 : {"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."},
361 [ + - + - : 4242 : {"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."},
+ - ]
362 : : },
363 [ + - ]: 2828 : RPCResult{
364 [ + - + - ]: 2828 : RPCResult::Type::OBJ, "", "",
365 : : {
366 [ + - + - ]: 2828 : {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."},
367 [ + - + - ]: 2828 : {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to creating and loading the wallet.",
368 : : {
369 [ + - + - ]: 2828 : {RPCResult::Type::STR, "", ""},
370 : : }},
371 : : }
372 [ + - + - : 9898 : },
+ - + + +
+ - - -
- ]
373 : 1414 : RPCExamples{
374 [ + - + - : 2828 : HelpExampleCli("createwallet", "\"testwallet\"")
+ - ]
375 [ + - + - : 5656 : + HelpExampleRpc("createwallet", "\"testwallet\"")
+ - + - ]
376 [ + - + - : 9898 : + HelpExampleCliNamed("createwallet", {{"wallet_name", "descriptors"}, {"avoid_reuse", true}, {"load_on_startup", true}})
+ - + - +
+ - - ]
377 [ + - + - : 9898 : + HelpExampleRpcNamed("createwallet", {{"wallet_name", "descriptors"}, {"avoid_reuse", true}, {"load_on_startup", true}})
+ - + - +
+ - - ]
378 [ + - ]: 1414 : },
379 : 1414 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
380 : : {
381 : 585 : WalletContext& context = EnsureWalletContext(request.context);
382 : 585 : uint64_t flags = 0;
383 [ + + + + ]: 585 : if (!request.params[1].isNull() && request.params[1].get_bool()) {
384 : : flags |= WALLET_FLAG_DISABLE_PRIVATE_KEYS;
385 : : }
386 : :
387 [ + + + + ]: 585 : if (!request.params[2].isNull() && request.params[2].get_bool()) {
388 : 163 : flags |= WALLET_FLAG_BLANK_WALLET;
389 : : }
390 [ + - ]: 585 : SecureString passphrase;
391 [ + - ]: 585 : passphrase.reserve(100);
392 : 585 : std::vector<bilingual_str> warnings;
393 [ + - + + ]: 585 : if (!request.params[3].isNull()) {
394 [ + - + - : 16 : passphrase = std::string_view{request.params[3].get_str()};
- + + - ]
395 [ + + ]: 16 : if (passphrase.empty()) {
396 : : // Empty string means unencrypted
397 [ + - + - : 8 : warnings.emplace_back(Untranslated("Empty string given as passphrase, wallet will not be encrypted."));
+ - ]
398 : : }
399 : : }
400 : :
401 [ + - + + : 585 : if (!request.params[4].isNull() && request.params[4].get_bool()) {
+ - + - +
+ ]
402 : 3 : flags |= WALLET_FLAG_AVOID_REUSE;
403 : : }
404 : 585 : flags |= WALLET_FLAG_DESCRIPTORS;
405 [ + - + + ]: 585 : if (!self.Arg<bool>("descriptors")) {
406 [ + - + - ]: 4 : throw JSONRPCError(RPC_WALLET_ERROR, "descriptors argument must be set to \"true\"; it is no longer possible to create a legacy wallet.");
407 : : }
408 [ + - + + : 583 : if (!request.params[7].isNull() && request.params[7].get_bool()) {
+ - + - +
+ ]
409 : : #ifdef ENABLE_EXTERNAL_SIGNER
410 : 6 : flags |= WALLET_FLAG_EXTERNAL_SIGNER;
411 : : #else
412 : : throw JSONRPCError(RPC_WALLET_ERROR, "Compiled without external signing support (required for external signing)");
413 : : #endif
414 : : }
415 : :
416 [ + - ]: 583 : DatabaseOptions options;
417 : 583 : DatabaseStatus status;
418 [ + - ]: 583 : ReadDatabaseArgs(*context.args, options);
419 : 583 : options.require_create = true;
420 : 583 : options.create_flags = flags;
421 [ + - ]: 583 : options.create_passphrase = passphrase;
422 [ + - ]: 583 : bilingual_str error;
423 [ + - + + : 583 : std::optional<bool> load_on_start = request.params[6].isNull() ? std::nullopt : std::optional<bool>(request.params[6].get_bool());
+ - + - ]
424 [ + - + - : 583 : const std::shared_ptr<CWallet> wallet = CreateWallet(context, request.params[0].get_str(), load_on_start, options, status, error, warnings);
+ + ]
425 [ + + ]: 580 : HandleWalletError(wallet, status, error);
426 : :
427 : 569 : UniValue obj(UniValue::VOBJ);
428 [ + - + - : 1138 : obj.pushKV("name", wallet->GetName());
+ - ]
429 [ + - ]: 569 : PushWarnings(warnings, obj);
430 : :
431 [ + - ]: 569 : return obj;
432 : 1179 : },
433 [ + - + - : 16968 : };
+ + - - ]
434 [ + - + - : 29694 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
- - - - -
- - - ]
435 : :
436 : 1120 : static RPCHelpMan unloadwallet()
437 : : {
438 : 1120 : return RPCHelpMan{"unloadwallet",
439 [ + - ]: 2240 : "Unloads the wallet referenced by the request endpoint or the wallet_name argument.\n"
440 : : "If both are specified, they must be identical.",
441 : : {
442 [ + - + - : 3360 : {"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."},
+ - ]
443 [ + - + - ]: 2240 : {"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."},
444 : : },
445 [ + - + - : 4480 : RPCResult{RPCResult::Type::OBJ, "", "", {
+ - ]
446 [ + - + - ]: 2240 : {RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to unloading the wallet.",
447 : : {
448 [ + - + - ]: 2240 : {RPCResult::Type::STR, "", ""},
449 : : }},
450 [ + - + - : 5600 : }},
+ - + + +
+ - - -
- ]
451 : 1120 : RPCExamples{
452 [ + - + - : 2240 : HelpExampleCli("unloadwallet", "wallet_name")
+ - ]
453 [ + - + - : 4480 : + HelpExampleRpc("unloadwallet", "wallet_name")
+ - + - ]
454 [ + - ]: 1120 : },
455 : 1120 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
456 : : {
457 : 291 : const std::string wallet_name{EnsureUniqueWalletName(request, self.MaybeArg<std::string_view>("wallet_name"))};
458 : :
459 [ + - ]: 287 : WalletContext& context = EnsureWalletContext(request.context);
460 [ + - ]: 287 : std::shared_ptr<CWallet> wallet = GetWallet(context, wallet_name);
461 [ + + ]: 287 : if (!wallet) {
462 [ + - + - ]: 8 : throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
463 : : }
464 : :
465 : 283 : std::vector<bilingual_str> warnings;
466 : 283 : {
467 : 283 : WalletRescanReserver reserver(*wallet);
468 [ - + ]: 283 : if (!reserver.reserve()) {
469 [ # # # # ]: 0 : throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
470 : : }
471 : :
472 : : // Release the "main" shared pointer and prevent further notifications.
473 : : // Note that any attempt to load the same wallet would fail until the wallet
474 : : // is destroyed (see CheckUniqueFileid).
475 [ + - ]: 283 : std::optional<bool> load_on_start{self.MaybeArg<bool>("load_on_startup")};
476 [ + - - + ]: 283 : if (!RemoveWallet(context, wallet, load_on_start, warnings)) {
477 [ # # # # ]: 0 : throw JSONRPCError(RPC_MISC_ERROR, "Requested wallet already unloaded");
478 : : }
479 : 283 : }
480 : :
481 [ + - ]: 283 : WaitForDeleteWallet(std::move(wallet));
482 : :
483 : 283 : UniValue result(UniValue::VOBJ);
484 [ + - ]: 283 : PushWarnings(warnings, result);
485 : :
486 : 566 : return result;
487 [ - + ]: 287 : },
488 [ + - + - : 6720 : };
+ + - - ]
489 [ + - + - : 6720 : }
+ - + - -
- ]
490 : :
491 : 855 : RPCHelpMan simulaterawtransaction()
492 : : {
493 : 855 : return RPCHelpMan{
494 : 855 : "simulaterawtransaction",
495 [ + - ]: 1710 : "Calculate the balance change resulting in the signing and broadcasting of the given transaction(s).\n",
496 : : {
497 [ + - + - ]: 1710 : {"rawtxs", RPCArg::Type::ARR, RPCArg::Optional::OMITTED, "An array of hex strings of raw transactions.\n",
498 : : {
499 [ + - + - ]: 1710 : {"rawtx", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, ""},
500 : : },
501 : : },
502 [ + - + - ]: 1710 : {"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "",
503 : : {
504 [ + - + - : 2565 : {"include_watchonly", RPCArg::Type::BOOL, RPCArg::Default{false}, "(DEPRECATED) No longer used"},
+ - ]
505 : : },
506 : : },
507 : : },
508 [ + - ]: 1710 : RPCResult{
509 [ + - + - ]: 1710 : RPCResult::Type::OBJ, "", "",
510 : : {
511 [ + - + - ]: 1710 : {RPCResult::Type::STR_AMOUNT, "balance_change", "The wallet balance change (negative means decrease)."},
512 : : }
513 [ + - + - : 2565 : },
+ + - - ]
514 : 855 : RPCExamples{
515 [ + - + - : 1710 : HelpExampleCli("simulaterawtransaction", "[\"myhex\"]")
+ - ]
516 [ + - + - : 3420 : + HelpExampleRpc("simulaterawtransaction", "[\"myhex\"]")
+ - + - ]
517 [ + - ]: 855 : },
518 : 855 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
519 : : {
520 [ - + ]: 26 : const std::shared_ptr<const CWallet> rpc_wallet = GetWalletForJSONRPCRequest(request);
521 [ - + ]: 26 : if (!rpc_wallet) return UniValue::VNULL;
522 [ + - ]: 26 : const CWallet& wallet = *rpc_wallet;
523 : :
524 [ + - ]: 26 : LOCK(wallet.cs_wallet);
525 : :
526 [ + - + - ]: 26 : const auto& txs = request.params[0].get_array();
527 : 26 : CAmount changes{0};
528 : 26 : std::map<COutPoint, CAmount> new_utxos; // UTXO:s that were made available in transaction array
529 : 26 : std::set<COutPoint> spent;
530 : :
531 [ - + + + ]: 54 : for (size_t i = 0; i < txs.size(); ++i) {
532 [ + - ]: 38 : CMutableTransaction mtx;
533 [ + - + - : 38 : if (!DecodeHexTx(mtx, txs[i].get_str(), /*try_no_witness=*/ true, /*try_witness=*/ true)) {
+ - - + ]
534 [ # # # # ]: 0 : throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Transaction hex string decoding failure.");
535 : : }
536 : :
537 : : // Fetch previous transactions (inputs)
538 : 38 : std::map<COutPoint, Coin> coins;
539 [ + + ]: 67 : for (const CTxIn& txin : mtx.vin) {
540 [ + - ]: 29 : coins[txin.prevout]; // Create empty map entry keyed by prevout.
541 : : }
542 [ + - ]: 38 : wallet.chain().findCoins(coins);
543 : :
544 : : // Fetch debit; we are *spending* these; if the transaction is signed and
545 : : // broadcast, we will lose everything in these
546 [ + + ]: 57 : for (const auto& txin : mtx.vin) {
547 : 29 : const auto& outpoint = txin.prevout;
548 [ + + ]: 29 : if (spent.contains(outpoint)) {
549 [ + - + - ]: 6 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Transaction(s) are spending the same output more than once");
550 : : }
551 [ + + ]: 26 : if (new_utxos.contains(outpoint)) {
552 [ + - ]: 6 : changes -= new_utxos.at(outpoint);
553 : 6 : new_utxos.erase(outpoint);
554 : : } else {
555 [ + - + + ]: 20 : if (coins.at(outpoint).IsSpent()) {
556 [ + - + - ]: 14 : throw JSONRPCError(RPC_INVALID_PARAMETER, "One or more transaction inputs are missing or have been spent already");
557 : : }
558 [ + - ]: 13 : changes -= wallet.GetDebit(txin);
559 : : }
560 [ + - ]: 19 : spent.insert(outpoint);
561 : : }
562 : :
563 : : // Iterate over outputs; we are *receiving* these, if the wallet considers
564 : : // them "mine"; if the transaction is signed and broadcast, we will receive
565 : : // everything in these
566 : : // Also populate new_utxos in case these are spent in later transactions
567 : :
568 [ + - ]: 28 : const auto& hash = mtx.GetHash();
569 [ - + + + ]: 69 : for (size_t i = 0; i < mtx.vout.size(); ++i) {
570 [ + - ]: 41 : const auto& txout = mtx.vout[i];
571 [ + - ]: 41 : bool is_mine = wallet.IsMine(txout);
572 [ + + + - ]: 41 : changes += new_utxos[COutPoint(hash, i)] = is_mine ? txout.nValue : 0;
573 : : }
574 : 76 : }
575 : :
576 : 16 : UniValue result(UniValue::VOBJ);
577 [ + - + - : 32 : result.pushKV("balance_change", ValueFromAmount(changes));
+ - ]
578 : :
579 : 16 : return result;
580 [ + - ]: 78 : }
581 [ + - + - : 10260 : };
+ - + - +
+ + + + +
- - - - -
- ]
582 [ + - + - : 7695 : }
+ - + - +
- - - ]
583 : :
584 : 880 : static RPCHelpMan migratewallet()
585 : : {
586 : 880 : return RPCHelpMan{
587 : 880 : "migratewallet",
588 [ + - ]: 1760 : "Migrate the wallet to a descriptor wallet.\n"
589 : : "A new wallet backup will need to be made.\n"
590 : : "\nThe migration process will create a backup of the wallet before migrating. This backup\n"
591 : : "file will be named <wallet name>-<timestamp>.legacy.bak and can be found in the directory\n"
592 : : "for this wallet. In the event of an incorrect migration, the backup can be restored using restorewallet."
593 : : "\nEncrypted wallets must have the passphrase provided as an argument to this call.\n"
594 : : "\nThis RPC may take a long time to complete. Increasing the RPC client timeout is recommended.",
595 : : {
596 [ + - + - : 2640 : {"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."},
+ - ]
597 [ + - + - ]: 1760 : {"passphrase", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "The wallet passphrase"},
598 : : },
599 [ + - ]: 1760 : RPCResult{
600 [ + - + - ]: 1760 : RPCResult::Type::OBJ, "", "",
601 : : {
602 [ + - + - ]: 1760 : {RPCResult::Type::STR, "wallet_name", "The name of the primary migrated wallet"},
603 [ + - + - ]: 1760 : {RPCResult::Type::STR, "watchonly_name", /*optional=*/true, "The name of the migrated wallet containing the watchonly scripts"},
604 [ + - + - ]: 1760 : {RPCResult::Type::STR, "solvables_name", /*optional=*/true, "The name of the migrated wallet containing solvable but not watched scripts"},
605 [ + - + - ]: 1760 : {RPCResult::Type::STR, "backup_path", "The location of the backup of the original wallet"},
606 : : }
607 [ + - + - : 7920 : },
+ + - - ]
608 : 880 : RPCExamples{
609 [ + - + - : 1760 : HelpExampleCli("migratewallet", "")
+ - ]
610 [ + - + - : 3520 : + HelpExampleRpc("migratewallet", "")
+ - + - ]
611 [ + - ]: 880 : },
612 : 880 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
613 : : {
614 : 51 : const std::string wallet_name{EnsureUniqueWalletName(request, self.MaybeArg<std::string_view>("wallet_name"))};
615 : :
616 [ + - ]: 49 : SecureString wallet_pass;
617 [ + - ]: 49 : wallet_pass.reserve(100);
618 [ + - + + ]: 49 : if (!request.params[1].isNull()) {
619 [ + - + - : 3 : wallet_pass = std::string_view{request.params[1].get_str()};
- + + - ]
620 : : }
621 : :
622 [ + - ]: 49 : WalletContext& context = EnsureWalletContext(request.context);
623 [ + - ]: 49 : util::Result<MigrationResult> res = MigrateLegacyToDescriptor(wallet_name, wallet_pass, context);
624 [ + + ]: 49 : if (!res) {
625 [ + - + - ]: 28 : throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(res).original);
626 : : }
627 : :
628 : 35 : UniValue r{UniValue::VOBJ};
629 [ + - + - : 70 : r.pushKV("wallet_name", res->wallet_name);
+ - ]
630 [ + + ]: 35 : if (res->watchonly_wallet) {
631 [ + - + - : 16 : r.pushKV("watchonly_name", res->watchonly_wallet->GetName());
+ - ]
632 : : }
633 [ + + ]: 35 : if (res->solvables_wallet) {
634 [ + - + - : 10 : r.pushKV("solvables_name", res->solvables_wallet->GetName());
+ - ]
635 : : }
636 [ + - + - : 70 : r.pushKV("backup_path", res->backup_path.utf8string());
+ - + - ]
637 : :
638 : 35 : return r;
639 : 63 : },
640 [ + - + - : 5280 : };
+ + - - ]
641 [ + - + - : 7040 : }
+ - + - +
- + - - -
- - ]
642 : :
643 : 862 : RPCHelpMan gethdkeys()
644 : : {
645 : 862 : return RPCHelpMan{
646 : 862 : "gethdkeys",
647 [ + - ]: 1724 : "List all BIP 32 HD keys in the wallet and which descriptors use them.\n",
648 : : {
649 [ + - + - ]: 1724 : {"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "", {
650 [ + - + - : 2586 : {"active_only", RPCArg::Type::BOOL, RPCArg::Default{false}, "Show the keys for only active descriptors"},
+ - ]
651 [ + - + - : 2586 : {"private", RPCArg::Type::BOOL, RPCArg::Default{false}, "Show private keys"}
+ - ]
652 : : }},
653 : : },
654 [ + - + - : 3448 : RPCResult{RPCResult::Type::ARR, "", "", {
+ - ]
655 : : {
656 [ + - + - ]: 1724 : {RPCResult::Type::OBJ, "", "", {
657 [ + - + - ]: 1724 : {RPCResult::Type::STR, "xpub", "The extended public key"},
658 [ + - + - ]: 1724 : {RPCResult::Type::BOOL, "has_private", "Whether the wallet has the private key for this xpub"},
659 [ + - + - ]: 1724 : {RPCResult::Type::STR, "xprv", /*optional=*/true, "The extended private key if \"private\" is true"},
660 [ + - + - ]: 1724 : {RPCResult::Type::ARR, "descriptors", "Array of descriptor objects that use this HD key",
661 : : {
662 [ + - + - ]: 1724 : {RPCResult::Type::OBJ, "", "", {
663 [ + - + - ]: 1724 : {RPCResult::Type::STR, "desc", "Descriptor string representation"},
664 [ + - + - ]: 1724 : {RPCResult::Type::BOOL, "active", "Whether this descriptor is currently used to generate new addresses"},
665 : : }},
666 : : }},
667 : : }},
668 : : }
669 [ + - + - : 14654 : }},
+ - + - +
- + + + +
+ + + + -
- - - - -
- - ]
670 : 862 : RPCExamples{
671 [ + - + - : 1724 : HelpExampleCli("gethdkeys", "") + HelpExampleRpc("gethdkeys", "")
+ - + - +
- + - +
- ]
672 [ + - + - : 8620 : + HelpExampleCliNamed("gethdkeys", {{"active_only", "true"}, {"private", "true"}}) + HelpExampleRpcNamed("gethdkeys", {{"active_only", "true"}, {"private", "true"}})
+ - + - +
- + - + -
+ - + + +
+ - - -
- ]
673 [ + - ]: 862 : },
674 : 862 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
675 : : {
676 [ - + ]: 33 : const std::shared_ptr<const CWallet> wallet = GetWalletForJSONRPCRequest(request);
677 [ - + ]: 33 : if (!wallet) return UniValue::VNULL;
678 : :
679 [ + - ]: 33 : LOCK(wallet->cs_wallet);
680 : :
681 [ + - + + : 33 : UniValue options{request.params[0].isNull() ? UniValue::VOBJ : request.params[0]};
+ - + - ]
682 [ + - + + : 72 : const bool active_only{options.exists("active_only") ? options["active_only"].get_bool() : false};
+ - + - +
- ]
683 [ + - + + : 75 : const bool priv{options.exists("private") ? options["private"].get_bool() : false};
+ - + - +
- ]
684 [ + + ]: 33 : if (priv) {
685 [ + + ]: 9 : EnsureWalletIsUnlocked(*wallet);
686 : : }
687 : :
688 : :
689 [ + + ]: 32 : std::set<ScriptPubKeyMan*> spkms;
690 [ + + ]: 32 : if (active_only) {
691 [ + - ]: 12 : spkms = wallet->GetActiveScriptPubKeyMans();
692 : : } else {
693 [ + - ]: 52 : spkms = wallet->GetAllScriptPubKeyMans();
694 : : }
695 : :
696 : 32 : std::map<CExtPubKey, std::set<std::tuple<std::string, bool, bool>>> wallet_xpubs;
697 : 32 : std::map<CExtPubKey, CExtKey> wallet_xprvs;
698 [ + + ]: 264 : for (auto* spkm : spkms) {
699 [ + - ]: 232 : auto* desc_spkm{dynamic_cast<DescriptorScriptPubKeyMan*>(spkm)};
700 [ + - ]: 232 : CHECK_NONFATAL(desc_spkm);
701 [ + - ]: 232 : LOCK(desc_spkm->cs_desc_man);
702 [ + - ]: 232 : WalletDescriptor w_desc = desc_spkm->GetWalletDescriptor();
703 : :
704 : : // Retrieve the pubkeys from the descriptor
705 [ + - ]: 232 : std::set<CPubKey> desc_pubkeys;
706 : 232 : std::set<CExtPubKey> desc_xpubs;
707 [ + - ]: 232 : w_desc.descriptor->GetPubKeys(desc_pubkeys, desc_xpubs);
708 [ + + ]: 457 : for (const CExtPubKey& xpub : desc_xpubs) {
709 [ + - ]: 225 : std::string desc_str;
710 [ + - ]: 225 : bool ok = desc_spkm->GetDescriptorString(desc_str, false);
711 [ + - ]: 225 : CHECK_NONFATAL(ok);
712 [ + - + - : 225 : wallet_xpubs[xpub].emplace(desc_str, wallet->IsActiveScriptPubKeyMan(*spkm), desc_spkm->HasPrivKey(xpub.pubkey.GetID()));
+ - + - +
- ]
713 [ + + + - : 225 : if (std::optional<CKey> key = priv ? desc_spkm->GetKey(xpub.pubkey.GetID()) : std::nullopt) {
+ - + + ]
714 [ + - + - ]: 64 : wallet_xprvs[xpub] = CExtKey(xpub, *key);
715 : 225 : }
716 : 225 : }
717 [ + - ]: 464 : }
718 : :
719 : 32 : UniValue response(UniValue::VARR);
720 [ + + ]: 66 : for (const auto& [xpub, descs] : wallet_xpubs) {
721 : 34 : bool has_xprv = false;
722 : 34 : UniValue descriptors(UniValue::VARR);
723 [ + + ]: 259 : for (const auto& [desc, active, has_priv] : descs) {
724 : 225 : UniValue d(UniValue::VOBJ);
725 [ + - + - : 450 : d.pushKV("desc", desc);
+ - ]
726 [ + - + - : 450 : d.pushKV("active", active);
+ - ]
727 : 225 : has_xprv |= has_priv;
728 : :
729 [ + - ]: 225 : descriptors.push_back(std::move(d));
730 : 225 : }
731 : 34 : UniValue xpub_info(UniValue::VOBJ);
732 [ + - + - : 68 : xpub_info.pushKV("xpub", EncodeExtPubKey(xpub));
+ - + - ]
733 [ + - + - : 68 : xpub_info.pushKV("has_private", has_xprv);
+ - ]
734 [ + + ]: 34 : if (priv) {
735 [ + - + - : 16 : xpub_info.pushKV("xprv", EncodeExtKey(wallet_xprvs.at(xpub)));
+ - + - +
- ]
736 : : }
737 [ + - + - ]: 68 : xpub_info.pushKV("descriptors", std::move(descriptors));
738 : :
739 [ + - ]: 34 : response.push_back(std::move(xpub_info));
740 : 34 : }
741 : :
742 : 32 : return response;
743 [ + - ]: 98 : },
744 [ + - + - : 7758 : };
+ - + + +
+ - - -
- ]
745 [ + - + - : 13792 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - - -
- - - - -
- ]
746 : :
747 : 842 : static RPCHelpMan createwalletdescriptor()
748 : : {
749 : 842 : return RPCHelpMan{"createwalletdescriptor",
750 : : "Creates the wallet's descriptor for the given address type. "
751 : : "The address type must be one that the wallet does not already have a descriptor for."
752 [ + - ]: 1684 : + HELP_REQUIRING_PASSPHRASE,
753 : : {
754 [ + - + - : 1684 : {"type", RPCArg::Type::STR, RPCArg::Optional::NO, "The address type the descriptor will produce. Options are " + FormatAllOutputTypes() + "."},
+ - ]
755 [ + - + - ]: 1684 : {"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "", {
756 [ + - + - : 2526 : {"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)"},
+ - ]
757 [ + - + - : 2526 : {"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"},
+ - ]
758 : : }},
759 : : },
760 [ + - ]: 1684 : RPCResult{
761 [ + - + - ]: 1684 : RPCResult::Type::OBJ, "", "",
762 : : {
763 [ + - + - ]: 1684 : {RPCResult::Type::ARR, "descs", "The public descriptors that were added to the wallet",
764 [ + - + - ]: 1684 : {{RPCResult::Type::STR, "", ""}}
765 : : }
766 : : },
767 [ + - + - : 4210 : },
+ - + + +
+ - - -
- ]
768 : 842 : RPCExamples{
769 [ + - + - : 1684 : HelpExampleCli("createwalletdescriptor", "bech32m")
+ - ]
770 [ + - + - : 3368 : + HelpExampleRpc("createwalletdescriptor", "bech32m")
+ - + - ]
771 [ + - ]: 842 : },
772 : 842 : [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
773 : : {
774 : 13 : std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
775 [ - + ]: 13 : if (!pwallet) return UniValue::VNULL;
776 : :
777 [ + - + - : 13 : std::optional<OutputType> output_type = ParseOutputType(request.params[0].get_str());
- + + - ]
778 [ + + ]: 13 : if (!output_type) {
779 [ + - + - : 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Unknown address type '%s'", request.params[0].get_str()));
+ - + - ]
780 : : }
781 : :
782 [ + - + + : 12 : UniValue options{request.params[1].isNull() ? UniValue::VOBJ : request.params[1]};
+ - + - ]
783 [ + - + - : 12 : UniValue internal_only{options["internal"]};
+ - ]
784 [ + - + - : 12 : UniValue hdkey{options["hdkey"]};
+ - ]
785 : :
786 : 12 : std::vector<bool> internals;
787 [ + + ]: 12 : if (internal_only.isNull()) {
788 [ + - ]: 10 : internals.push_back(false);
789 [ + - ]: 10 : internals.push_back(true);
790 : : } else {
791 [ + - + - ]: 2 : internals.push_back(internal_only.get_bool());
792 : : }
793 : :
794 [ + - ]: 12 : LOCK(pwallet->cs_wallet);
795 [ + + ]: 12 : EnsureWalletIsUnlocked(*pwallet);
796 : :
797 [ + + ]: 11 : CExtPubKey xpub;
798 [ + + ]: 11 : if (hdkey.isNull()) {
799 [ + - ]: 7 : std::set<CExtPubKey> active_xpubs = pwallet->GetActiveHDPubKeys();
800 [ + + ]: 7 : if (active_xpubs.size() != 1) {
801 [ + - + - ]: 4 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to determine which HD key to use from active descriptors. Please specify with 'hdkey'");
802 : : }
803 : 5 : xpub = *active_xpubs.begin();
804 : 7 : } else {
805 [ + - + - ]: 4 : xpub = DecodeExtPubKey(hdkey.get_str());
806 [ + + ]: 4 : if (!xpub.pubkey.IsValid()) {
807 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to parse HD key. Please provide a valid xpub");
808 : : }
809 : : }
810 : :
811 [ + - + - ]: 8 : std::optional<CKey> key = pwallet->GetKey(xpub.pubkey.GetID());
812 [ + + ]: 8 : if (!key) {
813 [ + - + - : 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Private key for %s is not known", EncodeExtPubKey(xpub)));
+ - ]
814 : : }
815 [ + - ]: 7 : CExtKey active_hdkey(xpub, *key);
816 : :
817 : 7 : std::vector<std::reference_wrapper<DescriptorScriptPubKeyMan>> spkms;
818 [ + - ]: 7 : WalletBatch batch{pwallet->GetDatabase()};
819 [ + - - + : 38 : for (bool internal : internals) {
- + ]
820 [ + - ]: 12 : WalletDescriptor w_desc = GenerateWalletDescriptor(xpub, *output_type, internal);
821 [ + - ]: 12 : uint256 w_id = DescriptorID(*w_desc.descriptor);
822 [ + - + + ]: 12 : if (!pwallet->GetScriptPubKeyMan(w_id)) {
823 [ + - + - ]: 10 : spkms.emplace_back(pwallet->SetupDescriptorScriptPubKeyMan(batch, active_hdkey, *output_type, internal));
824 : : }
825 : 12 : }
826 [ + + ]: 7 : if (spkms.empty()) {
827 [ + - + - ]: 2 : throw JSONRPCError(RPC_WALLET_ERROR, "Descriptor already exists");
828 : : }
829 : :
830 : : // Fetch each descspkm from the wallet in order to get the descriptor strings
831 : 6 : UniValue descs{UniValue::VARR};
832 [ + + ]: 16 : for (const auto& spkm : spkms) {
833 [ + - ]: 10 : std::string desc_str;
834 [ + - ]: 10 : bool ok = spkm.get().GetDescriptorString(desc_str, false);
835 [ + - ]: 10 : CHECK_NONFATAL(ok);
836 [ + - + - ]: 10 : descs.push_back(desc_str);
837 : 10 : }
838 : 6 : UniValue out{UniValue::VOBJ};
839 [ + - + - ]: 12 : out.pushKV("descs", std::move(descs));
840 : 6 : return out;
841 [ + - ]: 53 : }
842 [ + - + - : 8420 : };
+ - + + +
+ - - -
- ]
843 [ + - + - : 8420 : }
+ - + - +
- + - - -
- - ]
844 : :
845 : : // addresses
846 : : RPCHelpMan getaddressinfo();
847 : : RPCHelpMan getnewaddress();
848 : : RPCHelpMan getrawchangeaddress();
849 : : RPCHelpMan setlabel();
850 : : RPCHelpMan listaddressgroupings();
851 : : RPCHelpMan keypoolrefill();
852 : : RPCHelpMan getaddressesbylabel();
853 : : RPCHelpMan listlabels();
854 : : #ifdef ENABLE_EXTERNAL_SIGNER
855 : : RPCHelpMan walletdisplayaddress();
856 : : #endif // ENABLE_EXTERNAL_SIGNER
857 : :
858 : : // backup
859 : : RPCHelpMan importprunedfunds();
860 : : RPCHelpMan removeprunedfunds();
861 : : RPCHelpMan importdescriptors();
862 : : RPCHelpMan listdescriptors();
863 : : RPCHelpMan backupwallet();
864 : : RPCHelpMan restorewallet();
865 : :
866 : : // coins
867 : : RPCHelpMan getreceivedbyaddress();
868 : : RPCHelpMan getreceivedbylabel();
869 : : RPCHelpMan getbalance();
870 : : RPCHelpMan lockunspent();
871 : : RPCHelpMan listlockunspent();
872 : : RPCHelpMan getbalances();
873 : : RPCHelpMan listunspent();
874 : :
875 : : // encryption
876 : : RPCHelpMan walletpassphrase();
877 : : RPCHelpMan walletpassphrasechange();
878 : : RPCHelpMan walletlock();
879 : : RPCHelpMan encryptwallet();
880 : :
881 : : // spend
882 : : RPCHelpMan sendtoaddress();
883 : : RPCHelpMan sendmany();
884 : : RPCHelpMan settxfee();
885 : : RPCHelpMan fundrawtransaction();
886 : : RPCHelpMan bumpfee();
887 : : RPCHelpMan psbtbumpfee();
888 : : RPCHelpMan send();
889 : : RPCHelpMan sendall();
890 : : RPCHelpMan walletprocesspsbt();
891 : : RPCHelpMan walletcreatefundedpsbt();
892 : : RPCHelpMan signrawtransactionwithwallet();
893 : :
894 : : // signmessage
895 : : RPCHelpMan signmessage();
896 : :
897 : : // transactions
898 : : RPCHelpMan listreceivedbyaddress();
899 : : RPCHelpMan listreceivedbylabel();
900 : : RPCHelpMan listtransactions();
901 : : RPCHelpMan listsinceblock();
902 : : RPCHelpMan gettransaction();
903 : : RPCHelpMan abandontransaction();
904 : : RPCHelpMan rescanblockchain();
905 : : RPCHelpMan abortrescan();
906 : :
907 : 424 : std::span<const CRPCCommand> GetWalletRPCCommands()
908 : : {
909 : 424 : static const CRPCCommand commands[]{
910 [ + - ]: 824 : {"rawtransactions", &fundrawtransaction},
911 [ + - ]: 824 : {"wallet", &abandontransaction},
912 [ + - ]: 824 : {"wallet", &abortrescan},
913 [ + - ]: 824 : {"wallet", &backupwallet},
914 [ + - ]: 824 : {"wallet", &bumpfee},
915 [ + - ]: 824 : {"wallet", &psbtbumpfee},
916 [ + - ]: 824 : {"wallet", &createwallet},
917 [ + - ]: 824 : {"wallet", &createwalletdescriptor},
918 [ + - ]: 824 : {"wallet", &restorewallet},
919 [ + - ]: 824 : {"wallet", &encryptwallet},
920 [ + - ]: 824 : {"wallet", &getaddressesbylabel},
921 [ + - ]: 824 : {"wallet", &getaddressinfo},
922 [ + - ]: 824 : {"wallet", &getbalance},
923 [ + - ]: 824 : {"wallet", &gethdkeys},
924 [ + - ]: 824 : {"wallet", &getnewaddress},
925 [ + - ]: 824 : {"wallet", &getrawchangeaddress},
926 [ + - ]: 824 : {"wallet", &getreceivedbyaddress},
927 [ + - ]: 824 : {"wallet", &getreceivedbylabel},
928 [ + - ]: 824 : {"wallet", &gettransaction},
929 [ + - ]: 824 : {"wallet", &getbalances},
930 [ + - ]: 824 : {"wallet", &getwalletinfo},
931 [ + - ]: 824 : {"wallet", &importdescriptors},
932 [ + - ]: 824 : {"wallet", &importprunedfunds},
933 [ + - ]: 824 : {"wallet", &keypoolrefill},
934 [ + - ]: 824 : {"wallet", &listaddressgroupings},
935 [ + - ]: 824 : {"wallet", &listdescriptors},
936 [ + - ]: 824 : {"wallet", &listlabels},
937 [ + - ]: 824 : {"wallet", &listlockunspent},
938 [ + - ]: 824 : {"wallet", &listreceivedbyaddress},
939 [ + - ]: 824 : {"wallet", &listreceivedbylabel},
940 [ + - ]: 824 : {"wallet", &listsinceblock},
941 [ + - ]: 824 : {"wallet", &listtransactions},
942 [ + - ]: 824 : {"wallet", &listunspent},
943 [ + - ]: 824 : {"wallet", &listwalletdir},
944 [ + - ]: 824 : {"wallet", &listwallets},
945 [ + - ]: 824 : {"wallet", &loadwallet},
946 [ + - ]: 824 : {"wallet", &lockunspent},
947 [ + - ]: 824 : {"wallet", &migratewallet},
948 [ + - ]: 824 : {"wallet", &removeprunedfunds},
949 [ + - ]: 824 : {"wallet", &rescanblockchain},
950 [ + - ]: 824 : {"wallet", &send},
951 [ + - ]: 824 : {"wallet", &sendmany},
952 [ + - ]: 824 : {"wallet", &sendtoaddress},
953 [ + - ]: 824 : {"wallet", &setlabel},
954 [ + - ]: 824 : {"wallet", &settxfee},
955 [ + - ]: 824 : {"wallet", &setwalletflag},
956 [ + - ]: 824 : {"wallet", &signmessage},
957 [ + - ]: 824 : {"wallet", &signrawtransactionwithwallet},
958 [ + - ]: 824 : {"wallet", &simulaterawtransaction},
959 [ + - ]: 824 : {"wallet", &sendall},
960 [ + - ]: 824 : {"wallet", &unloadwallet},
961 [ + - ]: 824 : {"wallet", &walletcreatefundedpsbt},
962 : : #ifdef ENABLE_EXTERNAL_SIGNER
963 [ + - ]: 824 : {"wallet", &walletdisplayaddress},
964 : : #endif // ENABLE_EXTERNAL_SIGNER
965 [ + - ]: 824 : {"wallet", &walletlock},
966 [ + - ]: 824 : {"wallet", &walletpassphrase},
967 [ + - ]: 824 : {"wallet", &walletpassphrasechange},
968 [ + - ]: 824 : {"wallet", &walletprocesspsbt},
969 [ + + + - : 23908 : };
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - -
- ]
970 : 424 : return commands;
971 : : }
972 : : } // namespace wallet
|