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