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