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