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