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