Branch data Line data Source code
1 : : // Copyright (c) 2017-2022 The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <clientversion.h>
6 : : #include <core_io.h>
7 : : #include <common/args.h>
8 : : #include <consensus/amount.h>
9 : : #include <script/interpreter.h>
10 : : #include <key_io.h>
11 : : #include <outputtype.h>
12 : : #include <rpc/util.h>
13 : : #include <script/descriptor.h>
14 : : #include <script/signingprovider.h>
15 : : #include <script/solver.h>
16 : : #include <tinyformat.h>
17 : : #include <util/check.h>
18 : : #include <util/result.h>
19 : : #include <util/strencodings.h>
20 : : #include <util/string.h>
21 : : #include <util/translation.h>
22 : :
23 : : #include <string_view>
24 : : #include <tuple>
25 : :
26 : : const std::string UNIX_EPOCH_TIME = "UNIX epoch time";
27 [ + - ][ - + ]: 4 : const std::string EXAMPLE_ADDRESS[2] = {"bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hufdl", "bc1q02ad21edsxd23d32dfgqqsz4vv4nmtfzuklhy3"};
[ # # ]
28 : :
29 : 2 : std::string GetAllOutputTypes()
30 : : {
31 : 2 : std::vector<std::string> ret;
32 : : using U = std::underlying_type<TxoutType>::type;
33 [ + + ]: 22 : for (U i = (U)TxoutType::NONSTANDARD; i <= (U)TxoutType::WITNESS_UNKNOWN; ++i) {
34 [ + - ][ + - ]: 20 : ret.emplace_back(GetTxnOutputType(static_cast<TxoutType>(i)));
35 : 20 : }
36 [ + - ]: 2 : return Join(ret, ", ");
37 : 2 : }
38 : :
39 : 0 : void RPCTypeCheckObj(const UniValue& o,
40 : : const std::map<std::string, UniValueType>& typesExpected,
41 : : bool fAllowNull,
42 : : bool fStrict)
43 : : {
44 [ # # ]: 0 : for (const auto& t : typesExpected) {
45 : 0 : const UniValue& v = o.find_value(t.first);
46 [ # # ][ # # ]: 0 : if (!fAllowNull && v.isNull())
47 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));
[ # # ]
48 : :
49 [ # # ][ # # ]: 0 : if (!(t.second.typeAny || v.type() == t.second.type || (fAllowNull && v.isNull())))
[ # # ]
50 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_TYPE_ERROR, strprintf("JSON value of type %s for field %s is not of expected type %s", uvTypeName(v.type()), t.first, uvTypeName(t.second.type)));
[ # # ][ # # ]
[ # # ][ # # ]
51 : : }
52 : :
53 [ # # ]: 0 : if (fStrict)
54 : : {
55 [ # # ]: 0 : for (const std::string& k : o.getKeys())
56 : : {
57 [ # # ]: 0 : if (typesExpected.count(k) == 0)
58 : : {
59 : 0 : std::string err = strprintf("Unexpected key %s", k);
60 [ # # ]: 0 : throw JSONRPCError(RPC_TYPE_ERROR, err);
61 : 0 : }
62 : : }
63 : 0 : }
64 : 0 : }
65 : :
66 : 0 : CAmount AmountFromValue(const UniValue& value, int decimals)
67 : : {
68 [ # # ][ # # ]: 0 : if (!value.isNum() && !value.isStr())
69 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string");
[ # # ]
70 : : CAmount amount;
71 [ # # ]: 0 : if (!ParseFixedPoint(value.getValStr(), decimals, &amount))
72 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
[ # # ]
73 [ # # ]: 0 : if (!MoneyRange(amount))
74 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range");
[ # # ]
75 : 0 : return amount;
76 : 0 : }
77 : :
78 : 0 : uint256 ParseHashV(const UniValue& v, std::string_view name)
79 : : {
80 : 0 : const std::string& strHex(v.get_str());
81 [ # # ]: 0 : if (64 != strHex.length())
82 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d, for '%s')", name, 64, strHex.length(), strHex));
[ # # ]
83 [ # # ]: 0 : if (!IsHex(strHex)) // Note: IsHex("") is false
84 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));
[ # # ]
85 : 0 : return uint256S(strHex);
86 : 0 : }
87 : 0 : uint256 ParseHashO(const UniValue& o, std::string_view strKey)
88 : : {
89 : 0 : return ParseHashV(o.find_value(strKey), strKey);
90 : : }
91 : 0 : std::vector<unsigned char> ParseHexV(const UniValue& v, std::string_view name)
92 : : {
93 : 0 : std::string strHex;
94 [ # # ][ # # ]: 0 : if (v.isStr())
95 [ # # ][ # # ]: 0 : strHex = v.get_str();
96 [ # # ][ # # ]: 0 : if (!IsHex(strHex))
97 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));
[ # # ][ # # ]
98 [ # # ]: 0 : return ParseHex(strHex);
99 : 0 : }
100 : 0 : std::vector<unsigned char> ParseHexO(const UniValue& o, std::string_view strKey)
101 : : {
102 : 0 : return ParseHexV(o.find_value(strKey), strKey);
103 : : }
104 : :
105 : : namespace {
106 : :
107 : : /**
108 : : * Quote an argument for shell.
109 : : *
110 : : * @note This is intended for help, not for security-sensitive purposes.
111 : : */
112 : 0 : std::string ShellQuote(const std::string& s)
113 : : {
114 : 0 : std::string result;
115 [ # # ]: 0 : result.reserve(s.size() * 2);
116 [ # # ]: 0 : for (const char ch: s) {
117 [ # # ]: 0 : if (ch == '\'') {
118 [ # # ]: 0 : result += "'\''";
119 : 0 : } else {
120 [ # # ]: 0 : result += ch;
121 : : }
122 : : }
123 [ # # ][ # # ]: 0 : return "'" + result + "'";
124 : 0 : }
125 : :
126 : : /**
127 : : * Shell-quotes the argument if it needs quoting, else returns it literally, to save typing.
128 : : *
129 : : * @note This is intended for help, not for security-sensitive purposes.
130 : : */
131 : 0 : std::string ShellQuoteIfNeeded(const std::string& s)
132 : : {
133 [ # # ]: 0 : for (const char ch: s) {
134 [ # # ][ # # ]: 0 : if (ch == ' ' || ch == '\'' || ch == '"') {
[ # # ]
135 : 0 : return ShellQuote(s);
136 : : }
137 : : }
138 : :
139 : 0 : return s;
140 : 0 : }
141 : :
142 : : }
143 : :
144 : 8 : std::string HelpExampleCli(const std::string& methodname, const std::string& args)
145 : : {
146 [ + - ][ + - ]: 8 : return "> bitcoin-cli " + methodname + " " + args + "\n";
[ - + ]
147 : 0 : }
148 : :
149 : 0 : std::string HelpExampleCliNamed(const std::string& methodname, const RPCArgList& args)
150 : : {
151 : 0 : std::string result = "> bitcoin-cli -named " + methodname;
152 [ # # ]: 0 : for (const auto& argpair: args) {
153 [ # # ][ # # ]: 0 : const auto& value = argpair.second.isStr()
154 [ # # ][ # # ]: 0 : ? argpair.second.get_str()
155 [ # # ]: 0 : : argpair.second.write();
156 [ # # ][ # # ]: 0 : result += " " + argpair.first + "=" + ShellQuoteIfNeeded(value);
[ # # ][ # # ]
[ # # ]
157 : 0 : }
158 [ # # ]: 0 : result += "\n";
159 : 0 : return result;
160 [ # # ]: 0 : }
161 : :
162 : 8 : std::string HelpExampleRpc(const std::string& methodname, const std::string& args)
163 : : {
164 : 8 : return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", "
165 [ + - ][ + - ]: 8 : "\"method\": \"" + methodname + "\", \"params\": [" + args + "]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
[ - + ]
166 : 0 : }
167 : :
168 : 0 : std::string HelpExampleRpcNamed(const std::string& methodname, const RPCArgList& args)
169 : : {
170 [ # # ]: 0 : UniValue params(UniValue::VOBJ);
171 [ # # ]: 0 : for (const auto& param: args) {
172 [ # # ][ # # ]: 0 : params.pushKV(param.first, param.second);
[ # # ]
173 : : }
174 : :
175 : 0 : return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", "
176 [ # # ][ # # ]: 0 : "\"method\": \"" + methodname + "\", \"params\": " + params.write() + "}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
[ # # ][ # # ]
[ # # ]
177 : 0 : }
178 : :
179 : : // Converts a hex string to a public key if possible
180 : 0 : CPubKey HexToPubKey(const std::string& hex_in)
181 : : {
182 [ # # ]: 0 : if (!IsHex(hex_in)) {
183 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in);
[ # # ]
184 : : }
185 [ # # ][ # # ]: 0 : CPubKey vchPubKey(ParseHex(hex_in));
186 [ # # ]: 0 : if (!vchPubKey.IsFullyValid()) {
187 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in);
[ # # ]
188 : : }
189 : 0 : return vchPubKey;
190 : 0 : }
191 : :
192 : : // Retrieves a public key for an address from the given FillableSigningProvider
193 : 0 : CPubKey AddrToPubKey(const FillableSigningProvider& keystore, const std::string& addr_in)
194 : : {
195 : 0 : CTxDestination dest = DecodeDestination(addr_in);
196 [ # # ][ # # ]: 0 : if (!IsValidDestination(dest)) {
197 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address: " + addr_in);
[ # # ]
198 : : }
199 [ # # ]: 0 : CKeyID key = GetKeyForDestination(keystore, dest);
200 [ # # ][ # # ]: 0 : if (key.IsNull()) {
201 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("'%s' does not refer to a key", addr_in));
[ # # ]
202 : : }
203 [ # # ]: 0 : CPubKey vchPubKey;
204 [ # # ][ # # ]: 0 : if (!keystore.GetPubKey(key, vchPubKey)) {
205 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("no full public key for address %s", addr_in));
[ # # ]
206 : : }
207 [ # # ][ # # ]: 0 : if (!vchPubKey.IsFullyValid()) {
208 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet contains an invalid public key");
[ # # ]
209 : : }
210 : : return vchPubKey;
211 : 0 : }
212 : :
213 : : // Creates a multisig address from a given list of public keys, number of signatures required, and the address type
214 : 0 : CTxDestination AddAndGetMultisigDestination(const int required, const std::vector<CPubKey>& pubkeys, OutputType type, FillableSigningProvider& keystore, CScript& script_out)
215 : : {
216 : : // Gather public keys
217 [ # # ]: 0 : if (required < 1) {
218 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "a multisignature address must require at least one key to redeem");
[ # # ]
219 : : }
220 [ # # ]: 0 : if ((int)pubkeys.size() < required) {
221 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("not enough keys supplied (got %u keys, but need at least %d to redeem)", pubkeys.size(), required));
[ # # ]
222 : : }
223 [ # # ]: 0 : if (pubkeys.size() > MAX_PUBKEYS_PER_MULTISIG) {
224 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Number of keys involved in the multisignature address creation > %d\nReduce the number", MAX_PUBKEYS_PER_MULTISIG));
[ # # ]
225 : : }
226 : :
227 : 0 : script_out = GetScriptForMultisig(required, pubkeys);
228 : :
229 : : // Check if any keys are uncompressed. If so, the type is legacy
230 [ # # ]: 0 : for (const CPubKey& pk : pubkeys) {
231 [ # # ]: 0 : if (!pk.IsCompressed()) {
232 : 0 : type = OutputType::LEGACY;
233 : 0 : break;
234 : : }
235 : : }
236 : :
237 [ # # ][ # # ]: 0 : if (type == OutputType::LEGACY && script_out.size() > MAX_SCRIPT_ELEMENT_SIZE) {
238 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, (strprintf("redeemScript exceeds size limit: %d > %d", script_out.size(), MAX_SCRIPT_ELEMENT_SIZE)));
[ # # ][ # # ]
239 : : }
240 : :
241 : : // Make the address
242 : 0 : CTxDestination dest = AddAndGetDestinationForScript(keystore, script_out, type);
243 : :
244 : 0 : return dest;
245 [ # # ]: 0 : }
246 : :
247 : : class DescribeAddressVisitor
248 : : {
249 : : public:
250 : : explicit DescribeAddressVisitor() = default;
251 : :
252 : 0 : UniValue operator()(const CNoDestination& dest) const
253 : : {
254 [ # # ]: 0 : return UniValue(UniValue::VOBJ);
255 : 0 : }
256 : :
257 : 0 : UniValue operator()(const PubKeyDestination& dest) const
258 : : {
259 [ # # ]: 0 : return UniValue(UniValue::VOBJ);
260 : 0 : }
261 : :
262 : 0 : UniValue operator()(const PKHash& keyID) const
263 : : {
264 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
265 [ # # ][ # # ]: 0 : obj.pushKV("isscript", false);
[ # # ]
266 [ # # ][ # # ]: 0 : obj.pushKV("iswitness", false);
[ # # ]
267 : 0 : return obj;
268 [ # # ]: 0 : }
269 : :
270 : 0 : UniValue operator()(const ScriptHash& scriptID) const
271 : : {
272 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
273 [ # # ][ # # ]: 0 : obj.pushKV("isscript", true);
[ # # ]
274 [ # # ][ # # ]: 0 : obj.pushKV("iswitness", false);
[ # # ]
275 : 0 : return obj;
276 [ # # ]: 0 : }
277 : :
278 : 0 : UniValue operator()(const WitnessV0KeyHash& id) const
279 : : {
280 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
281 [ # # ][ # # ]: 0 : obj.pushKV("isscript", false);
[ # # ]
282 [ # # ][ # # ]: 0 : obj.pushKV("iswitness", true);
[ # # ]
283 [ # # ][ # # ]: 0 : obj.pushKV("witness_version", 0);
[ # # ]
284 [ # # ][ # # ]: 0 : obj.pushKV("witness_program", HexStr(id));
[ # # ][ # # ]
[ # # ]
285 : 0 : return obj;
286 [ # # ]: 0 : }
287 : :
288 : 0 : UniValue operator()(const WitnessV0ScriptHash& id) const
289 : : {
290 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
291 [ # # ][ # # ]: 0 : obj.pushKV("isscript", true);
[ # # ]
292 [ # # ][ # # ]: 0 : obj.pushKV("iswitness", true);
[ # # ]
293 [ # # ][ # # ]: 0 : obj.pushKV("witness_version", 0);
[ # # ]
294 [ # # ][ # # ]: 0 : obj.pushKV("witness_program", HexStr(id));
[ # # ][ # # ]
[ # # ]
295 : 0 : return obj;
296 [ # # ]: 0 : }
297 : :
298 : 0 : UniValue operator()(const WitnessV1Taproot& tap) const
299 : : {
300 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
301 [ # # ][ # # ]: 0 : obj.pushKV("isscript", true);
[ # # ]
302 [ # # ][ # # ]: 0 : obj.pushKV("iswitness", true);
[ # # ]
303 [ # # ][ # # ]: 0 : obj.pushKV("witness_version", 1);
[ # # ]
304 [ # # ][ # # ]: 0 : obj.pushKV("witness_program", HexStr(tap));
[ # # ][ # # ]
[ # # ]
305 : 0 : return obj;
306 [ # # ]: 0 : }
307 : :
308 : 0 : UniValue operator()(const WitnessUnknown& id) const
309 : : {
310 [ # # ]: 0 : UniValue obj(UniValue::VOBJ);
311 [ # # ][ # # ]: 0 : obj.pushKV("iswitness", true);
[ # # ]
312 [ # # ][ # # ]: 0 : obj.pushKV("witness_version", id.GetWitnessVersion());
[ # # ][ # # ]
313 [ # # ][ # # ]: 0 : obj.pushKV("witness_program", HexStr(id.GetWitnessProgram()));
[ # # ][ # # ]
[ # # ][ # # ]
314 : 0 : return obj;
315 [ # # ]: 0 : }
316 : : };
317 : :
318 : 0 : UniValue DescribeAddress(const CTxDestination& dest)
319 : : {
320 : 0 : return std::visit(DescribeAddressVisitor(), dest);
321 : : }
322 : :
323 : : /**
324 : : * Returns a sighash value corresponding to the passed in argument.
325 : : *
326 : : * @pre The sighash argument should be string or null.
327 : : */
328 : 0 : int ParseSighashString(const UniValue& sighash)
329 : : {
330 [ # # ]: 0 : if (sighash.isNull()) {
331 : 0 : return SIGHASH_DEFAULT;
332 : : }
333 : 0 : const auto result{SighashFromStr(sighash.get_str())};
334 [ # # ]: 0 : if (!result) {
335 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, util::ErrorString(result).original);
[ # # ][ # # ]
336 : : }
337 [ # # ]: 0 : return result.value();
338 : 0 : }
339 : :
340 : 0 : unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target)
341 : : {
342 : 0 : const int target{value.getInt<int>()};
343 : 0 : const unsigned int unsigned_target{static_cast<unsigned int>(target)};
344 [ # # ]: 0 : if (target < 1 || unsigned_target > max_target) {
345 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid conf_target, must be between %u and %u", 1, max_target));
[ # # ][ # # ]
346 : : }
347 : 0 : return unsigned_target;
348 : 0 : }
349 : :
350 : 0 : RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
351 : : {
352 [ # # # # : 0 : switch (terr) {
# # ]
353 : : case TransactionError::MEMPOOL_REJECTED:
354 : 0 : return RPC_TRANSACTION_REJECTED;
355 : : case TransactionError::ALREADY_IN_CHAIN:
356 : 0 : return RPC_TRANSACTION_ALREADY_IN_CHAIN;
357 : : case TransactionError::P2P_DISABLED:
358 : 0 : return RPC_CLIENT_P2P_DISABLED;
359 : : case TransactionError::INVALID_PSBT:
360 : : case TransactionError::PSBT_MISMATCH:
361 : 0 : return RPC_INVALID_PARAMETER;
362 : : case TransactionError::SIGHASH_MISMATCH:
363 : 0 : return RPC_DESERIALIZATION_ERROR;
364 : 0 : default: break;
365 : : }
366 : 0 : return RPC_TRANSACTION_ERROR;
367 : 0 : }
368 : :
369 : 0 : UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string)
370 : : {
371 [ # # ]: 0 : if (err_string.length() > 0) {
372 : 0 : return JSONRPCError(RPCErrorFromTransactionError(terr), err_string);
373 : : } else {
374 [ # # ]: 0 : return JSONRPCError(RPCErrorFromTransactionError(terr), TransactionErrorString(terr).original);
375 : : }
376 : 0 : }
377 : :
378 : : /**
379 : : * A pair of strings that can be aligned (through padding) with other Sections
380 : : * later on
381 : : */
382 : 0 : struct Section {
383 : 0 : Section(const std::string& left, const std::string& right)
384 [ # # ]: 0 : : m_left{left}, m_right{right} {}
385 : : std::string m_left;
386 : : const std::string m_right;
387 : : };
388 : :
389 : : /**
390 : : * Keeps track of RPCArgs by transforming them into sections for the purpose
391 : : * of serializing everything to a single string
392 : : */
393 : 0 : struct Sections {
394 : : std::vector<Section> m_sections;
395 : 0 : size_t m_max_pad{0};
396 : :
397 : 0 : void PushSection(const Section& s)
398 : : {
399 : 0 : m_max_pad = std::max(m_max_pad, s.m_left.size());
400 : 0 : m_sections.push_back(s);
401 : 0 : }
402 : :
403 : : /**
404 : : * Recursive helper to translate an RPCArg into sections
405 : : */
406 : 0 : void Push(const RPCArg& arg, const size_t current_indent = 5, const OuterType outer_type = OuterType::NONE)
407 : : {
408 [ # # ]: 0 : const auto indent = std::string(current_indent, ' ');
409 [ # # ]: 0 : const auto indent_next = std::string(current_indent + 2, ' ');
410 : 0 : const bool push_name{outer_type == OuterType::OBJ}; // Dictionary keys must have a name
411 : 0 : const bool is_top_level_arg{outer_type == OuterType::NONE}; // True on the first recursion
412 : :
413 [ # # # # ]: 0 : switch (arg.m_type) {
414 : : case RPCArg::Type::STR_HEX:
415 : : case RPCArg::Type::STR:
416 : : case RPCArg::Type::NUM:
417 : : case RPCArg::Type::AMOUNT:
418 : : case RPCArg::Type::RANGE:
419 : : case RPCArg::Type::BOOL:
420 : : case RPCArg::Type::OBJ_NAMED_PARAMS: {
421 [ # # ]: 0 : if (is_top_level_arg) return; // Nothing more to do for non-recursive types on first recursion
422 [ # # ]: 0 : auto left = indent;
423 [ # # ][ # # ]: 0 : if (arg.m_opts.type_str.size() != 0 && push_name) {
424 [ # # ][ # # ]: 0 : left += "\"" + arg.GetName() + "\": " + arg.m_opts.type_str.at(0);
[ # # ][ # # ]
[ # # ][ # # ]
425 : 0 : } else {
426 [ # # ][ # # ]: 0 : left += push_name ? arg.ToStringObj(/*oneline=*/false) : arg.ToString(/*oneline=*/false);
[ # # ][ # # ]
427 : : }
428 [ # # ]: 0 : left += ",";
429 [ # # ][ # # ]: 0 : PushSection({left, arg.ToDescriptionString(/*is_named_arg=*/push_name)});
[ # # ]
430 : : break;
431 : 0 : }
432 : : case RPCArg::Type::OBJ:
433 : : case RPCArg::Type::OBJ_USER_KEYS: {
434 [ # # ][ # # ]: 0 : const auto right = is_top_level_arg ? "" : arg.ToDescriptionString(/*is_named_arg=*/push_name);
[ # # ][ # # ]
[ # # ]
435 [ # # ][ # # ]: 0 : PushSection({indent + (push_name ? "\"" + arg.GetName() + "\": " : "") + "{", right});
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
436 [ # # ]: 0 : for (const auto& arg_inner : arg.m_inner) {
437 [ # # ]: 0 : Push(arg_inner, current_indent + 2, OuterType::OBJ);
438 : : }
439 [ # # ]: 0 : if (arg.m_type != RPCArg::Type::OBJ) {
440 [ # # ][ # # ]: 0 : PushSection({indent_next + "...", ""});
[ # # ][ # # ]
441 : 0 : }
442 [ # # ][ # # ]: 0 : PushSection({indent + "}" + (is_top_level_arg ? "" : ","), ""});
[ # # ][ # # ]
[ # # ]
443 : : break;
444 : 0 : }
445 : : case RPCArg::Type::ARR: {
446 [ # # ]: 0 : auto left = indent;
447 [ # # ][ # # ]: 0 : left += push_name ? "\"" + arg.GetName() + "\": " : "";
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
448 [ # # ]: 0 : left += "[";
449 [ # # ][ # # ]: 0 : const auto right = is_top_level_arg ? "" : arg.ToDescriptionString(/*is_named_arg=*/push_name);
[ # # ][ # # ]
[ # # ]
450 [ # # ][ # # ]: 0 : PushSection({left, right});
451 [ # # ]: 0 : for (const auto& arg_inner : arg.m_inner) {
452 [ # # ]: 0 : Push(arg_inner, current_indent + 2, OuterType::ARR);
453 : 16 : }
454 [ # # ][ # # ]: 0 : PushSection({indent_next + "...", ""});
[ # # ][ # # ]
455 [ # # ][ # # ]: 0 : PushSection({indent + "]" + (is_top_level_arg ? "" : ","), ""});
[ # # ][ # # ]
[ # # ]
456 : : break;
457 : 0 : }
458 : : } // no default case, so the compiler can warn about missing cases
459 [ # # ]: 0 : }
460 : :
461 : : /**
462 : : * Concatenate all sections with proper padding
463 : : */
464 : 0 : std::string ToString() const
465 : : {
466 : 0 : std::string ret;
467 : 0 : const size_t pad = m_max_pad + 4;
468 [ # # ]: 0 : for (const auto& s : m_sections) {
469 : : // The left part of a section is assumed to be a single line, usually it is the name of the JSON struct or a
470 : : // brace like {, }, [, or ]
471 [ # # ]: 0 : CHECK_NONFATAL(s.m_left.find('\n') == std::string::npos);
472 [ # # ]: 0 : if (s.m_right.empty()) {
473 [ # # ]: 0 : ret += s.m_left;
474 [ # # ]: 0 : ret += "\n";
475 : 0 : continue;
476 : : }
477 : :
478 [ # # ]: 0 : std::string left = s.m_left;
479 [ # # ]: 0 : left.resize(pad, ' ');
480 [ # # ]: 0 : ret += left;
481 : :
482 : : // Properly pad after newlines
483 : 0 : std::string right;
484 : 0 : size_t begin = 0;
485 : 0 : size_t new_line_pos = s.m_right.find_first_of('\n');
486 : 0 : while (true) {
487 [ # # ][ # # ]: 0 : right += s.m_right.substr(begin, new_line_pos - begin);
488 [ # # ]: 0 : if (new_line_pos == std::string::npos) {
489 : 0 : break; //No new line
490 : : }
491 [ # # ][ # # ]: 0 : right += "\n" + std::string(pad, ' ');
[ # # ]
492 : 0 : begin = s.m_right.find_first_not_of(' ', new_line_pos + 1);
493 [ # # ]: 0 : if (begin == std::string::npos) {
494 : 0 : break; // Empty line
495 : : }
496 : 0 : new_line_pos = s.m_right.find_first_of('\n', begin + 1);
497 : : }
498 [ # # ]: 0 : ret += right;
499 [ # # ]: 0 : ret += "\n";
500 : 0 : }
501 : 0 : return ret;
502 [ # # ]: 0 : }
503 : : };
504 : :
505 : 0 : RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples)
506 [ # # ][ # # ]: 0 : : RPCHelpMan{std::move(name), std::move(description), std::move(args), std::move(results), std::move(examples), nullptr} {}
[ # # ]
507 : :
508 : 16 : RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun)
509 : 16 : : m_name{std::move(name)},
510 : 16 : m_fun{std::move(fun)},
511 : 16 : m_description{std::move(description)},
512 : 16 : m_args{std::move(args)},
513 [ + - ]: 16 : m_results{std::move(results)},
514 [ + - ]: 16 : m_examples{std::move(examples)}
515 : : {
516 : : // Map of parameter names and types just used to check whether the names are
517 : : // unique. Parameter names always need to be unique, with the exception that
518 : : // there can be pairs of POSITIONAL and NAMED parameters with the same name.
519 : : enum ParamType { POSITIONAL = 1, NAMED = 2, NAMED_ONLY = 4 };
520 : 16 : std::map<std::string, int> param_names;
521 : :
522 [ + + ]: 24 : for (const auto& arg : m_args) {
523 [ - + ]: 8 : std::vector<std::string> names = SplitString(arg.m_names, '|');
524 : : // Should have unique named arguments
525 [ + + ]: 16 : for (const std::string& name : names) {
526 [ + - ]: 8 : auto& param_type = param_names[name];
527 [ + - ]: 8 : CHECK_NONFATAL(!(param_type & POSITIONAL));
528 [ + - ]: 8 : CHECK_NONFATAL(!(param_type & NAMED_ONLY));
529 : 8 : param_type |= POSITIONAL;
530 : : }
531 [ + - ]: 8 : if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
532 [ # # ]: 0 : for (const auto& inner : arg.m_inner) {
533 [ # # ]: 0 : std::vector<std::string> inner_names = SplitString(inner.m_names, '|');
534 [ # # ]: 0 : for (const std::string& inner_name : inner_names) {
535 [ # # ]: 0 : auto& param_type = param_names[inner_name];
536 [ # # ]: 0 : CHECK_NONFATAL(!(param_type & POSITIONAL) || inner.m_opts.also_positional);
537 [ # # ]: 0 : CHECK_NONFATAL(!(param_type & NAMED));
538 [ # # ]: 0 : CHECK_NONFATAL(!(param_type & NAMED_ONLY));
539 : 0 : param_type |= inner.m_opts.also_positional ? NAMED : NAMED_ONLY;
540 : : }
541 : 0 : }
542 : 0 : }
543 : : // Default value type should match argument type only when defined
544 [ - + ]: 8 : if (arg.m_fallback.index() == 2) {
545 : 0 : const RPCArg::Type type = arg.m_type;
546 [ # # ][ # # ]: 0 : switch (std::get<RPCArg::Default>(arg.m_fallback).getType()) {
[ # # # #
# # # ]
547 : : case UniValue::VOBJ:
548 [ # # ]: 0 : CHECK_NONFATAL(type == RPCArg::Type::OBJ);
549 : 0 : break;
550 : : case UniValue::VARR:
551 [ # # ]: 0 : CHECK_NONFATAL(type == RPCArg::Type::ARR);
552 : 0 : break;
553 : : case UniValue::VSTR:
554 [ # # ][ # # ]: 0 : CHECK_NONFATAL(type == RPCArg::Type::STR || type == RPCArg::Type::STR_HEX || type == RPCArg::Type::AMOUNT);
[ # # ]
555 : 0 : break;
556 : : case UniValue::VNUM:
557 [ # # ][ # # ]: 0 : CHECK_NONFATAL(type == RPCArg::Type::NUM || type == RPCArg::Type::AMOUNT || type == RPCArg::Type::RANGE);
[ # # ]
558 : 0 : break;
559 : : case UniValue::VBOOL:
560 [ # # ]: 0 : CHECK_NONFATAL(type == RPCArg::Type::BOOL);
561 : 0 : break;
562 : : case UniValue::VNULL:
563 : : // Null values are accepted in all arguments
564 : 0 : break;
565 : : default:
566 [ # # ][ # # ]: 0 : NONFATAL_UNREACHABLE();
567 : : break;
568 : : }
569 : 0 : }
570 : 8 : }
571 : 16 : }
572 : :
573 : 0 : std::string RPCResults::ToDescriptionString() const
574 : : {
575 : 0 : std::string result;
576 [ # # ]: 0 : for (const auto& r : m_results) {
577 [ # # ]: 0 : if (r.m_type == RPCResult::Type::ANY) continue; // for testing only
578 [ # # ]: 0 : if (r.m_cond.empty()) {
579 [ # # ]: 0 : result += "\nResult:\n";
580 : 0 : } else {
581 [ # # ][ # # ]: 0 : result += "\nResult (" + r.m_cond + "):\n";
[ # # ]
582 : : }
583 : 0 : Sections sections;
584 [ # # ]: 0 : r.ToSections(sections);
585 [ # # ][ # # ]: 0 : result += sections.ToString();
586 : 0 : }
587 : 0 : return result;
588 [ # # ]: 0 : }
589 : :
590 : 0 : std::string RPCExamples::ToDescriptionString() const
591 : : {
592 [ # # ]: 0 : return m_examples.empty() ? m_examples : "\nExamples:\n" + m_examples;
593 : : }
594 : :
595 : 0 : UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request) const
596 : : {
597 [ # # ]: 0 : if (request.mode == JSONRPCRequest::GET_ARGS) {
598 : 0 : return GetArgMap();
599 : : }
600 : : /*
601 : : * Check if the given request is valid according to this command or if
602 : : * the user is asking for help information, and throw help when appropriate.
603 : : */
604 [ # # ]: 0 : if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) {
605 [ # # ][ # # ]: 0 : throw std::runtime_error(ToString());
[ # # ]
606 : : }
607 [ # # ]: 0 : UniValue arg_mismatch{UniValue::VOBJ};
608 [ # # ]: 0 : for (size_t i{0}; i < m_args.size(); ++i) {
609 [ # # ]: 0 : const auto& arg{m_args.at(i)};
610 [ # # ][ # # ]: 0 : UniValue match{arg.MatchesType(request.params[i])};
611 [ # # ][ # # ]: 0 : if (!match.isTrue()) {
612 [ # # ][ # # ]: 0 : arg_mismatch.pushKV(strprintf("Position %s (%s)", i + 1, arg.m_names), std::move(match));
613 : 0 : }
614 : 0 : }
615 [ # # ][ # # ]: 0 : if (!arg_mismatch.empty()) {
616 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Wrong type passed:\n%s", arg_mismatch.write(4)));
[ # # ][ # # ]
617 : : }
618 [ # # ]: 0 : CHECK_NONFATAL(m_req == nullptr);
619 : 0 : m_req = &request;
620 [ # # ]: 0 : UniValue ret = m_fun(*this, request);
621 : 0 : m_req = nullptr;
622 [ # # ][ # # ]: 0 : if (gArgs.GetBoolArg("-rpcdoccheck", DEFAULT_RPC_DOC_CHECK)) {
[ # # ]
623 [ # # ]: 0 : UniValue mismatch{UniValue::VARR};
624 [ # # ]: 0 : for (const auto& res : m_results.m_results) {
625 [ # # ]: 0 : UniValue match{res.MatchesType(ret)};
626 [ # # ][ # # ]: 0 : if (match.isTrue()) {
627 [ # # ]: 0 : mismatch.setNull();
628 : 0 : break;
629 : : }
630 [ # # ][ # # ]: 0 : mismatch.push_back(match);
631 [ # # ]: 0 : }
632 [ # # ][ # # ]: 0 : if (!mismatch.isNull()) {
633 [ # # ][ # # ]: 0 : std::string explain{
634 [ # # ][ # # ]: 0 : mismatch.empty() ? "no possible results defined" :
[ # # ]
635 [ # # ][ # # ]: 0 : mismatch.size() == 1 ? mismatch[0].write(4) :
[ # # ][ # # ]
636 [ # # ]: 0 : mismatch.write(4)};
637 [ # # ][ # # ]: 0 : throw std::runtime_error{
638 [ # # ]: 0 : strprintf("Internal bug detected: RPC call \"%s\" returned incorrect type:\n%s\n%s %s\nPlease report this issue here: %s\n",
639 : 0 : m_name, explain,
640 [ # # ]: 0 : PACKAGE_NAME, FormatFullVersion(),
641 : : PACKAGE_BUGREPORT)};
642 : 0 : }
643 : 0 : }
644 : 0 : return ret;
645 [ # # ]: 0 : }
646 : :
647 : : using CheckFn = void(const RPCArg&);
648 : 0 : static const UniValue* DetailMaybeArg(CheckFn* check, const std::vector<RPCArg>& params, const JSONRPCRequest* req, size_t i)
649 : : {
650 : 0 : CHECK_NONFATAL(i < params.size());
651 : 0 : const UniValue& arg{CHECK_NONFATAL(req)->params[i]};
652 : 0 : const RPCArg& param{params.at(i)};
653 [ # # ]: 0 : if (check) check(param);
654 : :
655 [ # # ]: 0 : if (!arg.isNull()) return &arg;
656 [ # # ]: 0 : if (!std::holds_alternative<RPCArg::Default>(param.m_fallback)) return nullptr;
657 : 0 : return &std::get<RPCArg::Default>(param.m_fallback);
658 : 0 : }
659 : :
660 : 0 : static void CheckRequiredOrDefault(const RPCArg& param)
661 : : {
662 : : // Must use `Arg<Type>(i)` to get the argument or its default value.
663 : 0 : const bool required{
664 [ # # ]: 0 : std::holds_alternative<RPCArg::Optional>(param.m_fallback) && RPCArg::Optional::NO == std::get<RPCArg::Optional>(param.m_fallback),
665 : : };
666 [ # # ]: 0 : CHECK_NONFATAL(required || std::holds_alternative<RPCArg::Default>(param.m_fallback));
667 : 0 : }
668 : :
669 : : #define TMPL_INST(check_param, ret_type, return_code) \
670 : : template <> \
671 : : ret_type RPCHelpMan::ArgValue<ret_type>(size_t i) const \
672 : : { \
673 : : const UniValue* maybe_arg{ \
674 : : DetailMaybeArg(check_param, m_args, m_req, i), \
675 : : }; \
676 : : return return_code \
677 : : } \
678 : : void force_semicolon(ret_type)
679 : :
680 : : // Optional arg (without default). Can also be called on required args, if needed.
681 [ # # ]: 0 : TMPL_INST(nullptr, std::optional<double>, maybe_arg ? std::optional{maybe_arg->get_real()} : std::nullopt;);
682 [ # # ]: 0 : TMPL_INST(nullptr, std::optional<bool>, maybe_arg ? std::optional{maybe_arg->get_bool()} : std::nullopt;);
683 [ # # ]: 0 : TMPL_INST(nullptr, const std::string*, maybe_arg ? &maybe_arg->get_str() : nullptr;);
684 : :
685 : : // Required arg or optional arg with default value.
686 : 0 : TMPL_INST(CheckRequiredOrDefault, bool, CHECK_NONFATAL(maybe_arg)->get_bool(););
687 : 0 : TMPL_INST(CheckRequiredOrDefault, int, CHECK_NONFATAL(maybe_arg)->getInt<int>(););
688 : 0 : TMPL_INST(CheckRequiredOrDefault, uint64_t, CHECK_NONFATAL(maybe_arg)->getInt<uint64_t>(););
689 : 0 : TMPL_INST(CheckRequiredOrDefault, const std::string&, CHECK_NONFATAL(maybe_arg)->get_str(););
690 : :
691 : 0 : bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
692 : : {
693 : 0 : size_t num_required_args = 0;
694 [ # # ]: 0 : for (size_t n = m_args.size(); n > 0; --n) {
695 [ # # ]: 0 : if (!m_args.at(n - 1).IsOptional()) {
696 : 0 : num_required_args = n;
697 : 0 : break;
698 : : }
699 : 0 : }
700 [ # # ]: 0 : return num_required_args <= num_args && num_args <= m_args.size();
701 : : }
702 : :
703 : 8 : std::vector<std::pair<std::string, bool>> RPCHelpMan::GetArgNames() const
704 : : {
705 : 8 : std::vector<std::pair<std::string, bool>> ret;
706 [ + - ]: 8 : ret.reserve(m_args.size());
707 [ + + ]: 12 : for (const auto& arg : m_args) {
708 [ + - ]: 4 : if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
709 [ # # ]: 0 : for (const auto& inner : arg.m_inner) {
710 [ # # ]: 0 : ret.emplace_back(inner.m_names, /*named_only=*/true);
711 : : }
712 : 0 : }
713 [ + - ]: 4 : ret.emplace_back(arg.m_names, /*named_only=*/false);
714 : : }
715 : 8 : return ret;
716 [ + - ]: 8 : }
717 : :
718 : 0 : std::string RPCHelpMan::ToString() const
719 : : {
720 : 0 : std::string ret;
721 : :
722 : : // Oneline summary
723 [ # # ]: 0 : ret += m_name;
724 : 0 : bool was_optional{false};
725 [ # # ]: 0 : for (const auto& arg : m_args) {
726 [ # # ]: 0 : if (arg.m_opts.hidden) break; // Any arg that follows is also hidden
727 [ # # ]: 0 : const bool optional = arg.IsOptional();
728 [ # # ]: 0 : ret += " ";
729 [ # # ]: 0 : if (optional) {
730 [ # # ][ # # ]: 0 : if (!was_optional) ret += "( ";
731 : 0 : was_optional = true;
732 : 0 : } else {
733 [ # # ][ # # ]: 0 : if (was_optional) ret += ") ";
734 : 0 : was_optional = false;
735 : : }
736 [ # # ][ # # ]: 0 : ret += arg.ToString(/*oneline=*/true);
737 : : }
738 [ # # ][ # # ]: 0 : if (was_optional) ret += " )";
739 : :
740 : : // Description
741 [ # # ][ # # ]: 0 : ret += "\n\n" + TrimString(m_description) + "\n";
[ # # ][ # # ]
742 : :
743 : : // Arguments
744 : 0 : Sections sections;
745 : 0 : Sections named_only_sections;
746 [ # # ]: 0 : for (size_t i{0}; i < m_args.size(); ++i) {
747 [ # # ]: 0 : const auto& arg = m_args.at(i);
748 [ # # ]: 0 : if (arg.m_opts.hidden) break; // Any arg that follows is also hidden
749 : :
750 : : // Push named argument name and description
751 [ # # ][ # # ]: 0 : sections.m_sections.emplace_back(::ToString(i + 1) + ". " + arg.GetFirstName(), arg.ToDescriptionString(/*is_named_arg=*/true));
[ # # ][ # # ]
[ # # ][ # # ]
752 [ # # ]: 0 : sections.m_max_pad = std::max(sections.m_max_pad, sections.m_sections.back().m_left.size());
753 : :
754 : : // Recursively push nested args
755 [ # # ]: 0 : sections.Push(arg);
756 : :
757 : : // Push named-only argument sections
758 [ # # ]: 0 : if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
759 [ # # ]: 0 : for (const auto& arg_inner : arg.m_inner) {
760 [ # # ][ # # ]: 0 : named_only_sections.PushSection({arg_inner.GetFirstName(), arg_inner.ToDescriptionString(/*is_named_arg=*/true)});
[ # # ][ # # ]
761 [ # # ]: 0 : named_only_sections.Push(arg_inner);
762 : : }
763 : 0 : }
764 : 0 : }
765 : :
766 [ # # ][ # # ]: 0 : if (!sections.m_sections.empty()) ret += "\nArguments:\n";
767 [ # # ][ # # ]: 0 : ret += sections.ToString();
768 [ # # ][ # # ]: 0 : if (!named_only_sections.m_sections.empty()) ret += "\nNamed Arguments:\n";
769 [ # # ][ # # ]: 0 : ret += named_only_sections.ToString();
770 : :
771 : : // Result
772 [ # # ][ # # ]: 0 : ret += m_results.ToDescriptionString();
773 : :
774 : : // Examples
775 [ # # ][ # # ]: 0 : ret += m_examples.ToDescriptionString();
776 : :
777 : 0 : return ret;
778 [ # # ]: 0 : }
779 : :
780 : 0 : UniValue RPCHelpMan::GetArgMap() const
781 : : {
782 [ # # ]: 0 : UniValue arr{UniValue::VARR};
783 : :
784 : 0 : auto push_back_arg_info = [&arr](const std::string& rpc_name, int pos, const std::string& arg_name, const RPCArg::Type& type) {
785 [ # # ]: 0 : UniValue map{UniValue::VARR};
786 [ # # ][ # # ]: 0 : map.push_back(rpc_name);
787 [ # # ][ # # ]: 0 : map.push_back(pos);
788 [ # # ][ # # ]: 0 : map.push_back(arg_name);
789 [ # # ][ # # ]: 0 : map.push_back(type == RPCArg::Type::STR ||
[ # # ]
790 : 0 : type == RPCArg::Type::STR_HEX);
791 [ # # ][ # # ]: 0 : arr.push_back(map);
792 : 0 : };
793 : :
794 [ # # ]: 0 : for (int i{0}; i < int(m_args.size()); ++i) {
795 [ # # ]: 0 : const auto& arg = m_args.at(i);
796 [ # # ]: 0 : std::vector<std::string> arg_names = SplitString(arg.m_names, '|');
797 [ # # ]: 0 : for (const auto& arg_name : arg_names) {
798 [ # # ]: 0 : push_back_arg_info(m_name, i, arg_name, arg.m_type);
799 [ # # ]: 0 : if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
800 [ # # ]: 0 : for (const auto& inner : arg.m_inner) {
801 [ # # ]: 0 : std::vector<std::string> inner_names = SplitString(inner.m_names, '|');
802 [ # # ]: 0 : for (const std::string& inner_name : inner_names) {
803 [ # # ]: 0 : push_back_arg_info(m_name, i, inner_name, inner.m_type);
804 : : }
805 : 0 : }
806 : 0 : }
807 : : }
808 : 0 : }
809 : 0 : return arr;
810 [ # # ]: 0 : }
811 : :
812 : 0 : static std::optional<UniValue::VType> ExpectedType(RPCArg::Type type)
813 : : {
814 : : using Type = RPCArg::Type;
815 [ # # # # : 0 : switch (type) {
# # # # ]
816 : : case Type::STR_HEX:
817 : : case Type::STR: {
818 : 0 : return UniValue::VSTR;
819 : : }
820 : : case Type::NUM: {
821 : 0 : return UniValue::VNUM;
822 : : }
823 : : case Type::AMOUNT: {
824 : : // VNUM or VSTR, checked inside AmountFromValue()
825 : 0 : return std::nullopt;
826 : : }
827 : : case Type::RANGE: {
828 : : // VNUM or VARR, checked inside ParseRange()
829 : 0 : return std::nullopt;
830 : : }
831 : : case Type::BOOL: {
832 : 0 : return UniValue::VBOOL;
833 : : }
834 : : case Type::OBJ:
835 : : case Type::OBJ_NAMED_PARAMS:
836 : : case Type::OBJ_USER_KEYS: {
837 : 0 : return UniValue::VOBJ;
838 : : }
839 : : case Type::ARR: {
840 : 0 : return UniValue::VARR;
841 : : }
842 : : } // no default case, so the compiler can warn about missing cases
843 [ # # ]: 0 : NONFATAL_UNREACHABLE();
844 : 0 : }
845 : :
846 : 0 : UniValue RPCArg::MatchesType(const UniValue& request) const
847 : : {
848 [ # # ]: 0 : if (m_opts.skip_type_check) return true;
849 [ # # ][ # # ]: 0 : if (IsOptional() && request.isNull()) return true;
850 : 0 : const auto exp_type{ExpectedType(m_type)};
851 [ # # ]: 0 : if (!exp_type) return true; // nothing to check
852 : :
853 [ # # ]: 0 : if (*exp_type != request.getType()) {
854 [ # # ]: 0 : return strprintf("JSON value of type %s is not of expected type %s", uvTypeName(request.getType()), uvTypeName(*exp_type));
855 : : }
856 : 0 : return true;
857 : 0 : }
858 : :
859 : 0 : std::string RPCArg::GetFirstName() const
860 : : {
861 : 0 : return m_names.substr(0, m_names.find('|'));
862 : : }
863 : :
864 : 0 : std::string RPCArg::GetName() const
865 : : {
866 : 0 : CHECK_NONFATAL(std::string::npos == m_names.find('|'));
867 : 0 : return m_names;
868 : : }
869 : :
870 : 0 : bool RPCArg::IsOptional() const
871 : : {
872 [ # # ]: 0 : if (m_fallback.index() != 0) {
873 : 0 : return true;
874 : : } else {
875 : 0 : return RPCArg::Optional::NO != std::get<RPCArg::Optional>(m_fallback);
876 : : }
877 : 0 : }
878 : :
879 : 0 : std::string RPCArg::ToDescriptionString(bool is_named_arg) const
880 : : {
881 : 0 : std::string ret;
882 [ # # ]: 0 : ret += "(";
883 [ # # ]: 0 : if (m_opts.type_str.size() != 0) {
884 [ # # ][ # # ]: 0 : ret += m_opts.type_str.at(1);
885 : 0 : } else {
886 [ # # # # : 0 : switch (m_type) {
# # # ]
887 : : case Type::STR_HEX:
888 : : case Type::STR: {
889 [ # # ]: 0 : ret += "string";
890 : 0 : break;
891 : : }
892 : : case Type::NUM: {
893 [ # # ]: 0 : ret += "numeric";
894 : 0 : break;
895 : : }
896 : : case Type::AMOUNT: {
897 [ # # ]: 0 : ret += "numeric or string";
898 : 0 : break;
899 : : }
900 : : case Type::RANGE: {
901 [ # # ]: 0 : ret += "numeric or array";
902 : 0 : break;
903 : : }
904 : : case Type::BOOL: {
905 [ # # ]: 0 : ret += "boolean";
906 : 0 : break;
907 : : }
908 : : case Type::OBJ:
909 : : case Type::OBJ_NAMED_PARAMS:
910 : : case Type::OBJ_USER_KEYS: {
911 [ # # ]: 0 : ret += "json object";
912 : 0 : break;
913 : : }
914 : : case Type::ARR: {
915 [ # # ]: 0 : ret += "json array";
916 : 0 : break;
917 : : }
918 : : } // no default case, so the compiler can warn about missing cases
919 : : }
920 [ # # ]: 0 : if (m_fallback.index() == 1) {
921 [ # # ][ # # ]: 0 : ret += ", optional, default=" + std::get<RPCArg::DefaultHint>(m_fallback);
[ # # ]
922 [ # # ]: 0 : } else if (m_fallback.index() == 2) {
923 [ # # ][ # # ]: 0 : ret += ", optional, default=" + std::get<RPCArg::Default>(m_fallback).write();
[ # # ][ # # ]
924 : 0 : } else {
925 [ # # ]: 0 : switch (std::get<RPCArg::Optional>(m_fallback)) {
[ # # # ]
926 : : case RPCArg::Optional::OMITTED: {
927 [ # # ][ # # ]: 0 : if (is_named_arg) ret += ", optional"; // Default value is "null" in dicts. Otherwise,
928 : : // nothing to do. Element is treated as if not present and has no default value
929 : 0 : break;
930 : : }
931 : : case RPCArg::Optional::NO: {
932 [ # # ]: 0 : ret += ", required";
933 : 0 : break;
934 : : }
935 : : } // no default case, so the compiler can warn about missing cases
936 : : }
937 [ # # ]: 0 : ret += ")";
938 [ # # ][ # # ]: 0 : if (m_type == Type::OBJ_NAMED_PARAMS) ret += " Options object that can be used to pass named arguments, listed below.";
939 [ # # ][ # # ]: 0 : ret += m_description.empty() ? "" : " " + m_description;
[ # # ][ # # ]
[ # # ][ # # ]
940 : 0 : return ret;
941 [ # # ]: 0 : }
942 : :
943 : 0 : void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const int current_indent) const
944 : : {
945 : : // Indentation
946 [ # # ]: 0 : const std::string indent(current_indent, ' ');
947 [ # # ]: 0 : const std::string indent_next(current_indent + 2, ' ');
948 : :
949 : : // Elements in a JSON structure (dictionary or array) are separated by a comma
950 [ # # ]: 0 : const std::string maybe_separator{outer_type != OuterType::NONE ? "," : ""};
951 : :
952 : : // The key name if recursed into a dictionary
953 [ # # ][ # # ]: 0 : const std::string maybe_key{
[ # # ][ # # ]
954 [ # # ]: 0 : outer_type == OuterType::OBJ ?
955 [ # # ][ # # ]: 0 : "\"" + this->m_key_name + "\" : " :
956 [ # # ]: 0 : ""};
957 : :
958 : : // Format description with type
959 : 0 : const auto Description = [&](const std::string& type) {
960 [ # # ][ # # ]: 0 : return "(" + type + (this->m_optional ? ", optional" : "") + ")" +
[ # # ][ # # ]
[ # # ]
961 [ # # ][ # # ]: 0 : (this->m_description.empty() ? "" : " " + this->m_description);
[ # # ]
962 : 0 : };
963 : :
964 [ # # # # : 0 : switch (m_type) {
# # # # #
# # # ]
965 : : case Type::ELISION: {
966 : : // If the inner result is empty, use three dots for elision
967 [ # # ][ # # ]: 0 : sections.PushSection({indent + "..." + maybe_separator, m_description});
[ # # ][ # # ]
968 : 0 : return;
969 : : }
970 : : case Type::ANY: {
971 [ # # ]: 0 : NONFATAL_UNREACHABLE(); // Only for testing
972 : : }
973 : : case Type::NONE: {
974 [ # # ][ # # ]: 0 : sections.PushSection({indent + "null" + maybe_separator, Description("json null")});
[ # # ][ # # ]
[ # # ][ # # ]
975 : 0 : return;
976 : : }
977 : : case Type::STR: {
978 [ # # ][ # # ]: 0 : sections.PushSection({indent + maybe_key + "\"str\"" + maybe_separator, Description("string")});
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
979 : 0 : return;
980 : : }
981 : : case Type::STR_AMOUNT: {
982 [ # # ][ # # ]: 0 : sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")});
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
983 : 0 : return;
984 : : }
985 : : case Type::STR_HEX: {
986 [ # # ][ # # ]: 0 : sections.PushSection({indent + maybe_key + "\"hex\"" + maybe_separator, Description("string")});
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
987 : 0 : return;
988 : : }
989 : : case Type::NUM: {
990 [ # # ][ # # ]: 0 : sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")});
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
991 : 0 : return;
992 : : }
993 : : case Type::NUM_TIME: {
994 [ # # ][ # # ]: 0 : sections.PushSection({indent + maybe_key + "xxx" + maybe_separator, Description("numeric")});
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
995 : 0 : return;
996 : : }
997 : : case Type::BOOL: {
998 [ # # ][ # # ]: 0 : sections.PushSection({indent + maybe_key + "true|false" + maybe_separator, Description("boolean")});
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
999 : 0 : return;
1000 : : }
1001 : : case Type::ARR_FIXED:
1002 : : case Type::ARR: {
1003 [ # # ][ # # ]: 0 : sections.PushSection({indent + maybe_key + "[", Description("json array")});
[ # # ][ # # ]
[ # # ][ # # ]
1004 [ # # ]: 0 : for (const auto& i : m_inner) {
1005 [ # # ]: 0 : i.ToSections(sections, OuterType::ARR, current_indent + 2);
1006 : : }
1007 [ # # ]: 0 : CHECK_NONFATAL(!m_inner.empty());
1008 [ # # ][ # # ]: 0 : if (m_type == Type::ARR && m_inner.back().m_type != Type::ELISION) {
1009 [ # # ][ # # ]: 0 : sections.PushSection({indent_next + "...", ""});
[ # # ][ # # ]
1010 : 0 : } else {
1011 : : // Remove final comma, which would be invalid JSON
1012 : 0 : sections.m_sections.back().m_left.pop_back();
1013 : : }
1014 [ # # ][ # # ]: 0 : sections.PushSection({indent + "]" + maybe_separator, ""});
[ # # ][ # # ]
[ # # ]
1015 : 0 : return;
1016 : : }
1017 : : case Type::OBJ_DYN:
1018 : : case Type::OBJ: {
1019 [ # # ]: 0 : if (m_inner.empty()) {
1020 [ # # ][ # # ]: 0 : sections.PushSection({indent + maybe_key + "{}", Description("empty JSON object")});
[ # # ][ # # ]
[ # # ][ # # ]
1021 : 0 : return;
1022 : : }
1023 [ # # ][ # # ]: 0 : sections.PushSection({indent + maybe_key + "{", Description("json object")});
[ # # ][ # # ]
[ # # ][ # # ]
1024 [ # # ]: 0 : for (const auto& i : m_inner) {
1025 [ # # ]: 0 : i.ToSections(sections, OuterType::OBJ, current_indent + 2);
1026 : : }
1027 [ # # ][ # # ]: 0 : if (m_type == Type::OBJ_DYN && m_inner.back().m_type != Type::ELISION) {
1028 : : // If the dictionary keys are dynamic, use three dots for continuation
1029 [ # # ][ # # ]: 0 : sections.PushSection({indent_next + "...", ""});
[ # # ][ # # ]
1030 : 0 : } else {
1031 : : // Remove final comma, which would be invalid JSON
1032 : 0 : sections.m_sections.back().m_left.pop_back();
1033 : : }
1034 [ # # ][ # # ]: 0 : sections.PushSection({indent + "}" + maybe_separator, ""});
[ # # ][ # # ]
[ # # ]
1035 : 0 : return;
1036 : : }
1037 : : } // no default case, so the compiler can warn about missing cases
1038 [ # # ]: 0 : NONFATAL_UNREACHABLE();
1039 : 0 : }
1040 : :
1041 : 0 : static std::optional<UniValue::VType> ExpectedType(RPCResult::Type type)
1042 : : {
1043 : : using Type = RPCResult::Type;
1044 [ # # # # : 0 : switch (type) {
# # # # ]
1045 : : case Type::ELISION:
1046 : : case Type::ANY: {
1047 : 0 : return std::nullopt;
1048 : : }
1049 : : case Type::NONE: {
1050 : 0 : return UniValue::VNULL;
1051 : : }
1052 : : case Type::STR:
1053 : : case Type::STR_HEX: {
1054 : 0 : return UniValue::VSTR;
1055 : : }
1056 : : case Type::NUM:
1057 : : case Type::STR_AMOUNT:
1058 : : case Type::NUM_TIME: {
1059 : 0 : return UniValue::VNUM;
1060 : : }
1061 : : case Type::BOOL: {
1062 : 0 : return UniValue::VBOOL;
1063 : : }
1064 : : case Type::ARR_FIXED:
1065 : : case Type::ARR: {
1066 : 0 : return UniValue::VARR;
1067 : : }
1068 : : case Type::OBJ_DYN:
1069 : : case Type::OBJ: {
1070 : 0 : return UniValue::VOBJ;
1071 : : }
1072 : : } // no default case, so the compiler can warn about missing cases
1073 [ # # ]: 0 : NONFATAL_UNREACHABLE();
1074 : 0 : }
1075 : :
1076 : 0 : UniValue RPCResult::MatchesType(const UniValue& result) const
1077 : : {
1078 [ # # ]: 0 : if (m_skip_type_check) {
1079 : 0 : return true;
1080 : : }
1081 : :
1082 : 0 : const auto exp_type = ExpectedType(m_type);
1083 [ # # ]: 0 : if (!exp_type) return true; // can be any type, so nothing to check
1084 : :
1085 [ # # ]: 0 : if (*exp_type != result.getType()) {
1086 [ # # ]: 0 : return strprintf("returned type is %s, but declared as %s in doc", uvTypeName(result.getType()), uvTypeName(*exp_type));
1087 : : }
1088 : :
1089 [ # # ]: 0 : if (UniValue::VARR == result.getType()) {
1090 [ # # ]: 0 : UniValue errors(UniValue::VOBJ);
1091 [ # # ][ # # ]: 0 : for (size_t i{0}; i < result.get_array().size(); ++i) {
[ # # ]
1092 : : // If there are more results than documented, reuse the last doc_inner.
1093 [ # # ][ # # ]: 0 : const RPCResult& doc_inner{m_inner.at(std::min(m_inner.size() - 1, i))};
1094 [ # # ][ # # ]: 0 : UniValue match{doc_inner.MatchesType(result.get_array()[i])};
[ # # ]
1095 [ # # ][ # # ]: 0 : if (!match.isTrue()) errors.pushKV(strprintf("%d", i), match);
[ # # ][ # # ]
[ # # ]
1096 : 0 : }
1097 [ # # ][ # # ]: 0 : if (errors.empty()) return true; // empty result array is valid
[ # # ]
1098 : 0 : return errors;
1099 : 0 : }
1100 : :
1101 [ # # ]: 0 : if (UniValue::VOBJ == result.getType()) {
1102 [ # # ][ # # ]: 0 : if (!m_inner.empty() && m_inner.at(0).m_type == Type::ELISION) return true;
1103 [ # # ]: 0 : UniValue errors(UniValue::VOBJ);
1104 [ # # ]: 0 : if (m_type == Type::OBJ_DYN) {
1105 [ # # ]: 0 : const RPCResult& doc_inner{m_inner.at(0)}; // Assume all types are the same, randomly pick the first
1106 [ # # ][ # # ]: 0 : for (size_t i{0}; i < result.get_obj().size(); ++i) {
[ # # ]
1107 [ # # ][ # # ]: 0 : UniValue match{doc_inner.MatchesType(result.get_obj()[i])};
[ # # ]
1108 [ # # ][ # # ]: 0 : if (!match.isTrue()) errors.pushKV(result.getKeys()[i], match);
[ # # ][ # # ]
[ # # ][ # # ]
1109 : 0 : }
1110 [ # # ][ # # ]: 0 : if (errors.empty()) return true; // empty result obj is valid
[ # # ]
1111 : 0 : return errors;
1112 : : }
1113 : 0 : std::set<std::string> doc_keys;
1114 [ # # ]: 0 : for (const auto& doc_entry : m_inner) {
1115 [ # # ]: 0 : doc_keys.insert(doc_entry.m_key_name);
1116 : : }
1117 : 0 : std::map<std::string, UniValue> result_obj;
1118 [ # # ]: 0 : result.getObjMap(result_obj);
1119 [ # # ]: 0 : for (const auto& result_entry : result_obj) {
1120 [ # # ][ # # ]: 0 : if (doc_keys.find(result_entry.first) == doc_keys.end()) {
1121 [ # # ][ # # ]: 0 : errors.pushKV(result_entry.first, "key returned that was not in doc");
[ # # ]
1122 : 0 : }
1123 : : }
1124 : :
1125 [ # # ]: 0 : for (const auto& doc_entry : m_inner) {
1126 [ # # ]: 0 : const auto result_it{result_obj.find(doc_entry.m_key_name)};
1127 [ # # ]: 0 : if (result_it == result_obj.end()) {
1128 [ # # ]: 0 : if (!doc_entry.m_optional) {
1129 [ # # ][ # # ]: 0 : errors.pushKV(doc_entry.m_key_name, "key missing, despite not being optional in doc");
[ # # ]
1130 : 0 : }
1131 : 0 : continue;
1132 : : }
1133 [ # # ]: 0 : UniValue match{doc_entry.MatchesType(result_it->second)};
1134 [ # # ][ # # ]: 0 : if (!match.isTrue()) errors.pushKV(doc_entry.m_key_name, match);
[ # # ][ # # ]
[ # # ]
1135 : 0 : }
1136 [ # # ][ # # ]: 0 : if (errors.empty()) return true;
[ # # ]
1137 : 0 : return errors;
1138 : 0 : }
1139 : :
1140 : 0 : return true;
1141 : 0 : }
1142 : :
1143 : 398 : void RPCResult::CheckInnerDoc() const
1144 : : {
1145 [ + + ]: 398 : if (m_type == Type::OBJ) {
1146 : : // May or may not be empty
1147 : 78 : return;
1148 : : }
1149 : : // Everything else must either be empty or not
1150 [ + + ][ - + ]: 320 : const bool inner_needed{m_type == Type::ARR || m_type == Type::ARR_FIXED || m_type == Type::OBJ_DYN};
1151 : 320 : CHECK_NONFATAL(inner_needed != m_inner.empty());
1152 : 398 : }
1153 : :
1154 : 0 : std::string RPCArg::ToStringObj(const bool oneline) const
1155 : : {
1156 : 0 : std::string res;
1157 [ # # ]: 0 : res += "\"";
1158 [ # # ][ # # ]: 0 : res += GetFirstName();
1159 [ # # ]: 0 : if (oneline) {
1160 [ # # ]: 0 : res += "\":";
1161 : 0 : } else {
1162 [ # # ]: 0 : res += "\": ";
1163 : : }
1164 [ # # # # : 0 : switch (m_type) {
# # # #
# ]
1165 : : case Type::STR:
1166 [ # # ]: 0 : return res + "\"str\"";
1167 : : case Type::STR_HEX:
1168 [ # # ]: 0 : return res + "\"hex\"";
1169 : : case Type::NUM:
1170 [ # # ]: 0 : return res + "n";
1171 : : case Type::RANGE:
1172 [ # # ]: 0 : return res + "n or [n,n]";
1173 : : case Type::AMOUNT:
1174 [ # # ]: 0 : return res + "amount";
1175 : : case Type::BOOL:
1176 [ # # ]: 0 : return res + "bool";
1177 : : case Type::ARR:
1178 [ # # ]: 0 : res += "[";
1179 [ # # ]: 0 : for (const auto& i : m_inner) {
1180 [ # # ][ # # ]: 0 : res += i.ToString(oneline) + ",";
[ # # ]
1181 : : }
1182 [ # # ]: 0 : return res + "...]";
1183 : : case Type::OBJ:
1184 : : case Type::OBJ_NAMED_PARAMS:
1185 : : case Type::OBJ_USER_KEYS:
1186 : : // Currently unused, so avoid writing dead code
1187 [ # # ]: 0 : NONFATAL_UNREACHABLE();
1188 : : } // no default case, so the compiler can warn about missing cases
1189 [ # # ]: 0 : NONFATAL_UNREACHABLE();
1190 : 0 : }
1191 : :
1192 : 0 : std::string RPCArg::ToString(const bool oneline) const
1193 : : {
1194 [ # # ][ # # ]: 0 : if (oneline && !m_opts.oneline_description.empty()) {
1195 [ # # ][ # # ]: 0 : if (m_opts.oneline_description[0] == '\"' && m_type != Type::STR_HEX && m_type != Type::STR && gArgs.GetBoolArg("-rpcdoccheck", DEFAULT_RPC_DOC_CHECK)) {
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1196 [ # # ][ # # ]: 0 : throw std::runtime_error{
[ # # ]
1197 [ # # ][ # # ]: 0 : STR_INTERNAL_BUG(strprintf("non-string RPC arg \"%s\" quotes oneline_description:\n%s",
1198 : : m_names, m_opts.oneline_description)
1199 : : )};
1200 : : }
1201 : 0 : return m_opts.oneline_description;
1202 : : }
1203 : :
1204 [ # # # # : 0 : switch (m_type) {
# ]
1205 : : case Type::STR_HEX:
1206 : : case Type::STR: {
1207 [ # # ][ # # ]: 0 : return "\"" + GetFirstName() + "\"";
1208 : : }
1209 : : case Type::NUM:
1210 : : case Type::RANGE:
1211 : : case Type::AMOUNT:
1212 : : case Type::BOOL: {
1213 : 0 : return GetFirstName();
1214 : : }
1215 : : case Type::OBJ:
1216 : : case Type::OBJ_NAMED_PARAMS:
1217 : : case Type::OBJ_USER_KEYS: {
1218 : 0 : const std::string res = Join(m_inner, ",", [&](const RPCArg& i) { return i.ToStringObj(oneline); });
1219 [ # # ]: 0 : if (m_type == Type::OBJ) {
1220 [ # # ][ # # ]: 0 : return "{" + res + "}";
1221 : : } else {
1222 [ # # ][ # # ]: 0 : return "{" + res + ",...}";
1223 : : }
1224 : 0 : }
1225 : : case Type::ARR: {
1226 : 0 : std::string res;
1227 [ # # ]: 0 : for (const auto& i : m_inner) {
1228 [ # # ][ # # ]: 0 : res += i.ToString(oneline) + ",";
[ # # ]
1229 : : }
1230 [ # # ][ # # ]: 0 : return "[" + res + "...]";
1231 : 0 : }
1232 : : } // no default case, so the compiler can warn about missing cases
1233 [ # # ]: 0 : NONFATAL_UNREACHABLE();
1234 : 0 : }
1235 : :
1236 : 0 : static std::pair<int64_t, int64_t> ParseRange(const UniValue& value)
1237 : : {
1238 [ # # ]: 0 : if (value.isNum()) {
1239 : 0 : return {0, value.getInt<int64_t>()};
1240 : : }
1241 [ # # ]: 0 : if (value.isArray() && value.size() == 2 && value[0].isNum() && value[1].isNum()) {
1242 : 0 : int64_t low = value[0].getInt<int64_t>();
1243 : 0 : int64_t high = value[1].getInt<int64_t>();
1244 [ # # ][ # # ]: 0 : if (low > high) throw JSONRPCError(RPC_INVALID_PARAMETER, "Range specified as [begin,end] must not have begin after end");
[ # # ][ # # ]
1245 : 0 : return {low, high};
1246 : : }
1247 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified as end or as [begin,end]");
[ # # ]
1248 : 0 : }
1249 : :
1250 : 0 : std::pair<int64_t, int64_t> ParseDescriptorRange(const UniValue& value)
1251 : : {
1252 : : int64_t low, high;
1253 : 0 : std::tie(low, high) = ParseRange(value);
1254 [ # # ]: 0 : if (low < 0) {
1255 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should be greater or equal than 0");
[ # # ]
1256 : : }
1257 [ # # ]: 0 : if ((high >> 31) != 0) {
1258 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "End of range is too high");
[ # # ]
1259 : : }
1260 [ # # ]: 0 : if (high >= low + 1000000) {
1261 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Range is too large");
[ # # ]
1262 : : }
1263 : 0 : return {low, high};
1264 : 0 : }
1265 : :
1266 : 0 : std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, FlatSigningProvider& provider, const bool expand_priv)
1267 : : {
1268 : 0 : std::string desc_str;
1269 : 0 : std::pair<int64_t, int64_t> range = {0, 1000};
1270 [ # # ][ # # ]: 0 : if (scanobject.isStr()) {
1271 [ # # ][ # # ]: 0 : desc_str = scanobject.get_str();
1272 [ # # ][ # # ]: 0 : } else if (scanobject.isObject()) {
1273 [ # # ]: 0 : const UniValue& desc_uni{scanobject.find_value("desc")};
1274 [ # # ][ # # ]: 0 : if (desc_uni.isNull()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor needs to be provided in scan object");
[ # # ][ # # ]
[ # # ]
1275 [ # # ][ # # ]: 0 : desc_str = desc_uni.get_str();
1276 [ # # ]: 0 : const UniValue& range_uni{scanobject.find_value("range")};
1277 [ # # ][ # # ]: 0 : if (!range_uni.isNull()) {
1278 [ # # ]: 0 : range = ParseDescriptorRange(range_uni);
1279 : 0 : }
1280 : 0 : } else {
1281 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Scan object needs to be either a string or an object");
[ # # ]
1282 : : }
1283 : :
1284 : 0 : std::string error;
1285 [ # # ]: 0 : auto desc = Parse(desc_str, provider, error);
1286 [ # # ]: 0 : if (!desc) {
1287 [ # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
1288 : : }
1289 [ # # ][ # # ]: 0 : if (!desc->IsRange()) {
1290 : 0 : range.first = 0;
1291 : 0 : range.second = 0;
1292 : 0 : }
1293 : 0 : std::vector<CScript> ret;
1294 [ # # ]: 0 : for (int i = range.first; i <= range.second; ++i) {
1295 : 0 : std::vector<CScript> scripts;
1296 [ # # ][ # # ]: 0 : if (!desc->Expand(i, provider, scripts, provider)) {
1297 [ # # ][ # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys: '%s'", desc_str));
[ # # ]
1298 : : }
1299 [ # # ]: 0 : if (expand_priv) {
1300 [ # # ]: 0 : desc->ExpandPrivate(/*pos=*/i, provider, /*out=*/provider);
1301 : 0 : }
1302 [ # # ][ # # ]: 0 : std::move(scripts.begin(), scripts.end(), std::back_inserter(ret));
1303 : 0 : }
1304 : 0 : return ret;
1305 [ # # ]: 0 : }
1306 : :
1307 : : /** Convert a vector of bilingual strings to a UniValue::VARR containing their original untranslated values. */
1308 : 0 : [[nodiscard]] static UniValue BilingualStringsToUniValue(const std::vector<bilingual_str>& bilingual_strings)
1309 : : {
1310 : 0 : CHECK_NONFATAL(!bilingual_strings.empty());
1311 [ # # ]: 0 : UniValue result{UniValue::VARR};
1312 [ # # ]: 0 : for (const auto& s : bilingual_strings) {
1313 [ # # ][ # # ]: 0 : result.push_back(s.original);
1314 : : }
1315 : 0 : return result;
1316 [ # # ]: 0 : }
1317 : :
1318 : 0 : void PushWarnings(const UniValue& warnings, UniValue& obj)
1319 : : {
1320 [ # # ]: 0 : if (warnings.empty()) return;
1321 [ # # ][ # # ]: 0 : obj.pushKV("warnings", warnings);
[ # # ]
1322 : 0 : }
1323 : :
1324 : 0 : void PushWarnings(const std::vector<bilingual_str>& warnings, UniValue& obj)
1325 : : {
1326 [ # # ]: 0 : if (warnings.empty()) return;
1327 [ # # ][ # # ]: 0 : obj.pushKV("warnings", BilingualStringsToUniValue(warnings));
[ # # ]
1328 : 0 : }
|