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