Bitcoin Core  21.99.0
P2P Digital Currency
util.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <key_io.h>
6 #include <outputtype.h>
7 #include <rpc/util.h>
8 #include <script/descriptor.h>
10 #include <tinyformat.h>
11 #include <util/strencodings.h>
12 #include <util/string.h>
13 #include <util/translation.h>
14 
15 #include <tuple>
16 
17 #include <boost/algorithm/string/classification.hpp>
18 #include <boost/algorithm/string/split.hpp>
19 
20 const std::string UNIX_EPOCH_TIME = "UNIX epoch time";
21 const std::string EXAMPLE_ADDRESS[2] = {"bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hufdl", "bc1q02ad21edsxd23d32dfgqqsz4vv4nmtfzuklhy3"};
22 
23 void RPCTypeCheck(const UniValue& params,
24  const std::list<UniValueType>& typesExpected,
25  bool fAllowNull)
26 {
27  unsigned int i = 0;
28  for (const UniValueType& t : typesExpected) {
29  if (params.size() <= i)
30  break;
31 
32  const UniValue& v = params[i];
33  if (!(fAllowNull && v.isNull())) {
35  }
36  i++;
37  }
38 }
39 
40 void RPCTypeCheckArgument(const UniValue& value, const UniValueType& typeExpected)
41 {
42  if (!typeExpected.typeAny && value.type() != typeExpected.type) {
43  throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Expected type %s, got %s", uvTypeName(typeExpected.type), uvTypeName(value.type())));
44  }
45 }
46 
47 void RPCTypeCheckObj(const UniValue& o,
48  const std::map<std::string, UniValueType>& typesExpected,
49  bool fAllowNull,
50  bool fStrict)
51 {
52  for (const auto& t : typesExpected) {
53  const UniValue& v = find_value(o, t.first);
54  if (!fAllowNull && v.isNull())
55  throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));
56 
57  if (!(t.second.typeAny || v.type() == t.second.type || (fAllowNull && v.isNull()))) {
58  std::string err = strprintf("Expected type %s for %s, got %s",
59  uvTypeName(t.second.type), t.first, uvTypeName(v.type()));
60  throw JSONRPCError(RPC_TYPE_ERROR, err);
61  }
62  }
63 
64  if (fStrict)
65  {
66  for (const std::string& k : o.getKeys())
67  {
68  if (typesExpected.count(k) == 0)
69  {
70  std::string err = strprintf("Unexpected key %s", k);
71  throw JSONRPCError(RPC_TYPE_ERROR, err);
72  }
73  }
74  }
75 }
76 
78 {
79  if (!value.isNum() && !value.isStr())
80  throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string");
81  CAmount amount;
82  if (!ParseFixedPoint(value.getValStr(), 8, &amount))
83  throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
84  if (!MoneyRange(amount))
85  throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range");
86  return amount;
87 }
88 
89 uint256 ParseHashV(const UniValue& v, std::string strName)
90 {
91  std::string strHex(v.get_str());
92  if (64 != strHex.length())
93  throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d, for '%s')", strName, 64, strHex.length(), strHex));
94  if (!IsHex(strHex)) // Note: IsHex("") is false
95  throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
96  return uint256S(strHex);
97 }
98 uint256 ParseHashO(const UniValue& o, std::string strKey)
99 {
100  return ParseHashV(find_value(o, strKey), strKey);
101 }
102 std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName)
103 {
104  std::string strHex;
105  if (v.isStr())
106  strHex = v.get_str();
107  if (!IsHex(strHex))
108  throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
109  return ParseHex(strHex);
110 }
111 std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey)
112 {
113  return ParseHexV(find_value(o, strKey), strKey);
114 }
115 
116 namespace {
117 
123 std::string ShellQuote(const std::string& s)
124 {
125  std::string result;
126  result.reserve(s.size() * 2);
127  for (const char ch: s) {
128  if (ch == '\'') {
129  result += "'\''";
130  } else {
131  result += ch;
132  }
133  }
134  return "'" + result + "'";
135 }
136 
142 std::string ShellQuoteIfNeeded(const std::string& s)
143 {
144  for (const char ch: s) {
145  if (ch == ' ' || ch == '\'' || ch == '"') {
146  return ShellQuote(s);
147  }
148  }
149 
150  return s;
151 }
152 
153 }
154 
155 std::string HelpExampleCli(const std::string& methodname, const std::string& args)
156 {
157  return "> bitcoin-cli " + methodname + " " + args + "\n";
158 }
159 
160 std::string HelpExampleCliNamed(const std::string& methodname, const RPCArgList& args)
161 {
162  std::string result = "> bitcoin-cli -named " + methodname;
163  for (const auto& argpair: args) {
164  const auto& value = argpair.second.isStr()
165  ? argpair.second.get_str()
166  : argpair.second.write();
167  result += " " + argpair.first + "=" + ShellQuoteIfNeeded(value);
168  }
169  result += "\n";
170  return result;
171 }
172 
173 std::string HelpExampleRpc(const std::string& methodname, const std::string& args)
174 {
175  return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", "
176  "\"method\": \"" + methodname + "\", \"params\": [" + args + "]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
177 }
178 
179 std::string HelpExampleRpcNamed(const std::string& methodname, const RPCArgList& args)
180 {
181  UniValue params(UniValue::VOBJ);
182  for (const auto& param: args) {
183  params.pushKV(param.first, param.second);
184  }
185 
186  return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", "
187  "\"method\": \"" + methodname + "\", \"params\": " + params.write() + "}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
188 }
189 
190 // Converts a hex string to a public key if possible
191 CPubKey HexToPubKey(const std::string& hex_in)
192 {
193  if (!IsHex(hex_in)) {
194  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in);
195  }
196  CPubKey vchPubKey(ParseHex(hex_in));
197  if (!vchPubKey.IsFullyValid()) {
198  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in);
199  }
200  return vchPubKey;
201 }
202 
203 // Retrieves a public key for an address from the given FillableSigningProvider
204 CPubKey AddrToPubKey(const FillableSigningProvider& keystore, const std::string& addr_in)
205 {
206  CTxDestination dest = DecodeDestination(addr_in);
207  if (!IsValidDestination(dest)) {
208  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address: " + addr_in);
209  }
210  CKeyID key = GetKeyForDestination(keystore, dest);
211  if (key.IsNull()) {
212  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("%s does not refer to a key", addr_in));
213  }
214  CPubKey vchPubKey;
215  if (!keystore.GetPubKey(key, vchPubKey)) {
216  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("no full public key for address %s", addr_in));
217  }
218  if (!vchPubKey.IsFullyValid()) {
219  throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet contains an invalid public key");
220  }
221  return vchPubKey;
222 }
223 
224 // Creates a multisig address from a given list of public keys, number of signatures required, and the address type
225 CTxDestination AddAndGetMultisigDestination(const int required, const std::vector<CPubKey>& pubkeys, OutputType type, FillableSigningProvider& keystore, CScript& script_out)
226 {
227  // Gather public keys
228  if (required < 1) {
229  throw JSONRPCError(RPC_INVALID_PARAMETER, "a multisignature address must require at least one key to redeem");
230  }
231  if ((int)pubkeys.size() < required) {
232  throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("not enough keys supplied (got %u keys, but need at least %d to redeem)", pubkeys.size(), required));
233  }
234  if (pubkeys.size() > 16) {
235  throw JSONRPCError(RPC_INVALID_PARAMETER, "Number of keys involved in the multisignature address creation > 16\nReduce the number");
236  }
237 
238  script_out = GetScriptForMultisig(required, pubkeys);
239 
240  if (script_out.size() > MAX_SCRIPT_ELEMENT_SIZE) {
241  throw JSONRPCError(RPC_INVALID_PARAMETER, (strprintf("redeemScript exceeds size limit: %d > %d", script_out.size(), MAX_SCRIPT_ELEMENT_SIZE)));
242  }
243 
244  // Check if any keys are uncompressed. If so, the type is legacy
245  for (const CPubKey& pk : pubkeys) {
246  if (!pk.IsCompressed()) {
247  type = OutputType::LEGACY;
248  break;
249  }
250  }
251 
252  // Make the address
253  CTxDestination dest = AddAndGetDestinationForScript(keystore, script_out, type);
254 
255  return dest;
256 }
257 
259 {
260 public:
262 
263  UniValue operator()(const CNoDestination& dest) const
264  {
265  return UniValue(UniValue::VOBJ);
266  }
267 
268  UniValue operator()(const PKHash& keyID) const
269  {
271  obj.pushKV("isscript", false);
272  obj.pushKV("iswitness", false);
273  return obj;
274  }
275 
276  UniValue operator()(const ScriptHash& scriptID) const
277  {
279  obj.pushKV("isscript", true);
280  obj.pushKV("iswitness", false);
281  return obj;
282  }
283 
285  {
287  obj.pushKV("isscript", false);
288  obj.pushKV("iswitness", true);
289  obj.pushKV("witness_version", 0);
290  obj.pushKV("witness_program", HexStr(id));
291  return obj;
292  }
293 
295  {
297  obj.pushKV("isscript", true);
298  obj.pushKV("iswitness", true);
299  obj.pushKV("witness_version", 0);
300  obj.pushKV("witness_program", HexStr(id));
301  return obj;
302  }
303 
305  {
307  obj.pushKV("iswitness", true);
308  obj.pushKV("witness_version", (int)id.version);
309  obj.pushKV("witness_program", HexStr(Span<const unsigned char>(id.program, id.length)));
310  return obj;
311  }
312 };
313 
315 {
316  return std::visit(DescribeAddressVisitor(), dest);
317 }
318 
319 unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target)
320 {
321  const int target{value.get_int()};
322  const unsigned int unsigned_target{static_cast<unsigned int>(target)};
323  if (target < 1 || unsigned_target > max_target) {
324  throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid conf_target, must be between %u and %u", 1, max_target));
325  }
326  return unsigned_target;
327 }
328 
330 {
331  switch (terr) {
340  return RPC_INVALID_PARAMETER;
343  default: break;
344  }
345  return RPC_TRANSACTION_ERROR;
346 }
347 
348 UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string)
349 {
350  if (err_string.length() > 0) {
351  return JSONRPCError(RPCErrorFromTransactionError(terr), err_string);
352  } else {
354  }
355 }
356 
361 struct Section {
362  Section(const std::string& left, const std::string& right)
363  : m_left{left}, m_right{right} {}
364  std::string m_left;
365  const std::string m_right;
366 };
367 
372 struct Sections {
373  std::vector<Section> m_sections;
374  size_t m_max_pad{0};
375 
376  void PushSection(const Section& s)
377  {
378  m_max_pad = std::max(m_max_pad, s.m_left.size());
379  m_sections.push_back(s);
380  }
381 
385  void Push(const RPCArg& arg, const size_t current_indent = 5, const OuterType outer_type = OuterType::NONE)
386  {
387  const auto indent = std::string(current_indent, ' ');
388  const auto indent_next = std::string(current_indent + 2, ' ');
389  const bool push_name{outer_type == OuterType::OBJ}; // Dictionary keys must have a name
390 
391  switch (arg.m_type) {
393  case RPCArg::Type::STR:
394  case RPCArg::Type::NUM:
396  case RPCArg::Type::RANGE:
397  case RPCArg::Type::BOOL: {
398  if (outer_type == OuterType::NONE) return; // Nothing more to do for non-recursive types on first recursion
399  auto left = indent;
400  if (arg.m_type_str.size() != 0 && push_name) {
401  left += "\"" + arg.GetName() + "\": " + arg.m_type_str.at(0);
402  } else {
403  left += push_name ? arg.ToStringObj(/* oneline */ false) : arg.ToString(/* oneline */ false);
404  }
405  left += ",";
406  PushSection({left, arg.ToDescriptionString()});
407  break;
408  }
409  case RPCArg::Type::OBJ:
411  const auto right = outer_type == OuterType::NONE ? "" : arg.ToDescriptionString();
412  PushSection({indent + (push_name ? "\"" + arg.GetName() + "\": " : "") + "{", right});
413  for (const auto& arg_inner : arg.m_inner) {
414  Push(arg_inner, current_indent + 2, OuterType::OBJ);
415  }
416  if (arg.m_type != RPCArg::Type::OBJ) {
417  PushSection({indent_next + "...", ""});
418  }
419  PushSection({indent + "}" + (outer_type != OuterType::NONE ? "," : ""), ""});
420  break;
421  }
422  case RPCArg::Type::ARR: {
423  auto left = indent;
424  left += push_name ? "\"" + arg.GetName() + "\": " : "";
425  left += "[";
426  const auto right = outer_type == OuterType::NONE ? "" : arg.ToDescriptionString();
427  PushSection({left, right});
428  for (const auto& arg_inner : arg.m_inner) {
429  Push(arg_inner, current_indent + 2, OuterType::ARR);
430  }
431  PushSection({indent_next + "...", ""});
432  PushSection({indent + "]" + (outer_type != OuterType::NONE ? "," : ""), ""});
433  break;
434  }
435  } // no default case, so the compiler can warn about missing cases
436  }
437 
441  std::string ToString() const
442  {
443  std::string ret;
444  const size_t pad = m_max_pad + 4;
445  for (const auto& s : m_sections) {
446  // The left part of a section is assumed to be a single line, usually it is the name of the JSON struct or a
447  // brace like {, }, [, or ]
448  CHECK_NONFATAL(s.m_left.find('\n') == std::string::npos);
449  if (s.m_right.empty()) {
450  ret += s.m_left;
451  ret += "\n";
452  continue;
453  }
454 
455  std::string left = s.m_left;
456  left.resize(pad, ' ');
457  ret += left;
458 
459  // Properly pad after newlines
460  std::string right;
461  size_t begin = 0;
462  size_t new_line_pos = s.m_right.find_first_of('\n');
463  while (true) {
464  right += s.m_right.substr(begin, new_line_pos - begin);
465  if (new_line_pos == std::string::npos) {
466  break; //No new line
467  }
468  right += "\n" + std::string(pad, ' ');
469  begin = s.m_right.find_first_not_of(' ', new_line_pos + 1);
470  if (begin == std::string::npos) {
471  break; // Empty line
472  }
473  new_line_pos = s.m_right.find_first_of('\n', begin + 1);
474  }
475  ret += right;
476  ret += "\n";
477  }
478  return ret;
479  }
480 };
481 
482 RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples)
483  : RPCHelpMan{std::move(name), std::move(description), std::move(args), std::move(results), std::move(examples), nullptr} {}
484 
485 RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun)
486  : m_name{std::move(name)},
487  m_fun{std::move(fun)},
488  m_description{std::move(description)},
489  m_args{std::move(args)},
490  m_results{std::move(results)},
491  m_examples{std::move(examples)}
492 {
493  std::set<std::string> named_args;
494  for (const auto& arg : m_args) {
495  std::vector<std::string> names;
496  boost::split(names, arg.m_names, boost::is_any_of("|"));
497  // Should have unique named arguments
498  for (const std::string& name : names) {
499  CHECK_NONFATAL(named_args.insert(name).second);
500  }
501  }
502 }
503 
505 {
506  std::string result;
507  for (const auto& r : m_results) {
508  if (r.m_type == RPCResult::Type::ANY) continue; // for testing only
509  if (r.m_cond.empty()) {
510  result += "\nResult:\n";
511  } else {
512  result += "\nResult (" + r.m_cond + "):\n";
513  }
514  Sections sections;
515  r.ToSections(sections);
516  result += sections.ToString();
517  }
518  return result;
519 }
520 
522 {
523  return m_examples.empty() ? m_examples : "\nExamples:\n" + m_examples;
524 }
525 
527 {
528  if (request.mode == JSONRPCRequest::GET_ARGS) {
529  return GetArgMap();
530  }
531  /*
532  * Check if the given request is valid according to this command or if
533  * the user is asking for help information, and throw help when appropriate.
534  */
535  if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) {
536  throw std::runtime_error(ToString());
537  }
538  const UniValue ret = m_fun(*this, request);
539  CHECK_NONFATAL(std::any_of(m_results.m_results.begin(), m_results.m_results.end(), [ret](const RPCResult& res) { return res.MatchesType(ret); }));
540  return ret;
541 }
542 
543 bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
544 {
545  size_t num_required_args = 0;
546  for (size_t n = m_args.size(); n > 0; --n) {
547  if (!m_args.at(n - 1).IsOptional()) {
548  num_required_args = n;
549  break;
550  }
551  }
552  return num_required_args <= num_args && num_args <= m_args.size();
553 }
554 
555 std::vector<std::string> RPCHelpMan::GetArgNames() const
556 {
557  std::vector<std::string> ret;
558  for (const auto& arg : m_args) {
559  ret.emplace_back(arg.m_names);
560  }
561  return ret;
562 }
563 
564 std::string RPCHelpMan::ToString() const
565 {
566  std::string ret;
567 
568  // Oneline summary
569  ret += m_name;
570  bool was_optional{false};
571  for (const auto& arg : m_args) {
572  if (arg.m_hidden) break; // Any arg that follows is also hidden
573  const bool optional = arg.IsOptional();
574  ret += " ";
575  if (optional) {
576  if (!was_optional) ret += "( ";
577  was_optional = true;
578  } else {
579  if (was_optional) ret += ") ";
580  was_optional = false;
581  }
582  ret += arg.ToString(/* oneline */ true);
583  }
584  if (was_optional) ret += " )";
585  ret += "\n";
586 
587  // Description
588  ret += m_description;
589 
590  // Arguments
591  Sections sections;
592  for (size_t i{0}; i < m_args.size(); ++i) {
593  const auto& arg = m_args.at(i);
594  if (arg.m_hidden) break; // Any arg that follows is also hidden
595 
596  if (i == 0) ret += "\nArguments:\n";
597 
598  // Push named argument name and description
599  sections.m_sections.emplace_back(::ToString(i + 1) + ". " + arg.GetFirstName(), arg.ToDescriptionString());
600  sections.m_max_pad = std::max(sections.m_max_pad, sections.m_sections.back().m_left.size());
601 
602  // Recursively push nested args
603  sections.Push(arg);
604  }
605  ret += sections.ToString();
606 
607  // Result
609 
610  // Examples
612 
613  return ret;
614 }
615 
617 {
619  for (int i{0}; i < int(m_args.size()); ++i) {
620  const auto& arg = m_args.at(i);
621  std::vector<std::string> arg_names;
622  boost::split(arg_names, arg.m_names, boost::is_any_of("|"));
623  for (const auto& arg_name : arg_names) {
625  map.push_back(m_name);
626  map.push_back(i);
627  map.push_back(arg_name);
628  map.push_back(arg.m_type == RPCArg::Type::STR ||
629  arg.m_type == RPCArg::Type::STR_HEX);
630  arr.push_back(map);
631  }
632  }
633  return arr;
634 }
635 
636 std::string RPCArg::GetFirstName() const
637 {
638  return m_names.substr(0, m_names.find("|"));
639 }
640 
641 std::string RPCArg::GetName() const
642 {
643  CHECK_NONFATAL(std::string::npos == m_names.find("|"));
644  return m_names;
645 }
646 
647 bool RPCArg::IsOptional() const
648 {
649  if (m_fallback.index() == 1) {
650  return true;
651  } else {
652  return RPCArg::Optional::NO != std::get<RPCArg::Optional>(m_fallback);
653  }
654 }
655 
656 std::string RPCArg::ToDescriptionString() const
657 {
658  std::string ret;
659  ret += "(";
660  if (m_type_str.size() != 0) {
661  ret += m_type_str.at(1);
662  } else {
663  switch (m_type) {
664  case Type::STR_HEX:
665  case Type::STR: {
666  ret += "string";
667  break;
668  }
669  case Type::NUM: {
670  ret += "numeric";
671  break;
672  }
673  case Type::AMOUNT: {
674  ret += "numeric or string";
675  break;
676  }
677  case Type::RANGE: {
678  ret += "numeric or array";
679  break;
680  }
681  case Type::BOOL: {
682  ret += "boolean";
683  break;
684  }
685  case Type::OBJ:
686  case Type::OBJ_USER_KEYS: {
687  ret += "json object";
688  break;
689  }
690  case Type::ARR: {
691  ret += "json array";
692  break;
693  }
694  } // no default case, so the compiler can warn about missing cases
695  }
696  if (m_fallback.index() == 1) {
697  ret += ", optional, default=" + std::get<std::string>(m_fallback);
698  } else {
699  switch (std::get<RPCArg::Optional>(m_fallback)) {
701  // nothing to do. Element is treated as if not present and has no default value
702  break;
703  }
705  ret += ", optional"; // Default value is "null"
706  break;
707  }
708  case RPCArg::Optional::NO: {
709  ret += ", required";
710  break;
711  }
712  } // no default case, so the compiler can warn about missing cases
713  }
714  ret += ")";
715  ret += m_description.empty() ? "" : " " + m_description;
716  return ret;
717 }
718 
719 void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const int current_indent) const
720 {
721  // Indentation
722  const std::string indent(current_indent, ' ');
723  const std::string indent_next(current_indent + 2, ' ');
724 
725  // Elements in a JSON structure (dictionary or array) are separated by a comma
726  const std::string maybe_separator{outer_type != OuterType::NONE ? "," : ""};
727 
728  // The key name if recursed into an dictionary
729  const std::string maybe_key{
730  outer_type == OuterType::OBJ ?
731  "\"" + this->m_key_name + "\" : " :
732  ""};
733 
734  // Format description with type
735  const auto Description = [&](const std::string& type) {
736  return "(" + type + (this->m_optional ? ", optional" : "") + ")" +
737  (this->m_description.empty() ? "" : " " + this->m_description);
738  };
739 
740  switch (m_type) {
741  case Type::ELISION: {
742  // If the inner result is empty, use three dots for elision
743  sections.PushSection({indent + "..." + maybe_separator, m_description});
744  return;
745  }
746  case Type::ANY: {
747  CHECK_NONFATAL(false); // Only for testing
748  }
749  case Type::NONE: {
750  sections.PushSection({indent + "null" + maybe_separator, Description("json null")});
751  return;
752  }
753  case Type::STR: {
754  sections.PushSection({indent + maybe_key + "\"str\"" + maybe_separator, Description("string")});
755  return;
756  }
757  case Type::STR_AMOUNT: {
758  sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")});
759  return;
760  }
761  case Type::STR_HEX: {
762  sections.PushSection({indent + maybe_key + "\"hex\"" + maybe_separator, Description("string")});
763  return;
764  }
765  case Type::NUM: {
766  sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")});
767  return;
768  }
769  case Type::NUM_TIME: {
770  sections.PushSection({indent + maybe_key + "xxx" + maybe_separator, Description("numeric")});
771  return;
772  }
773  case Type::BOOL: {
774  sections.PushSection({indent + maybe_key + "true|false" + maybe_separator, Description("boolean")});
775  return;
776  }
777  case Type::ARR_FIXED:
778  case Type::ARR: {
779  sections.PushSection({indent + maybe_key + "[", Description("json array")});
780  for (const auto& i : m_inner) {
781  i.ToSections(sections, OuterType::ARR, current_indent + 2);
782  }
783  CHECK_NONFATAL(!m_inner.empty());
784  if (m_type == Type::ARR && m_inner.back().m_type != Type::ELISION) {
785  sections.PushSection({indent_next + "...", ""});
786  } else {
787  // Remove final comma, which would be invalid JSON
788  sections.m_sections.back().m_left.pop_back();
789  }
790  sections.PushSection({indent + "]" + maybe_separator, ""});
791  return;
792  }
793  case Type::OBJ_DYN:
794  case Type::OBJ: {
795  sections.PushSection({indent + maybe_key + "{", Description("json object")});
796  for (const auto& i : m_inner) {
797  i.ToSections(sections, OuterType::OBJ, current_indent + 2);
798  }
799  CHECK_NONFATAL(!m_inner.empty());
800  if (m_type == Type::OBJ_DYN && m_inner.back().m_type != Type::ELISION) {
801  // If the dictionary keys are dynamic, use three dots for continuation
802  sections.PushSection({indent_next + "...", ""});
803  } else {
804  // Remove final comma, which would be invalid JSON
805  sections.m_sections.back().m_left.pop_back();
806  }
807  sections.PushSection({indent + "}" + maybe_separator, ""});
808  return;
809  }
810  } // no default case, so the compiler can warn about missing cases
811  CHECK_NONFATAL(false);
812 }
813 
814 bool RPCResult::MatchesType(const UniValue& result) const
815 {
816  switch (m_type) {
817  case Type::ELISION: {
818  return false;
819  }
820  case Type::ANY: {
821  return true;
822  }
823  case Type::NONE: {
824  return UniValue::VNULL == result.getType();
825  }
826  case Type::STR:
827  case Type::STR_HEX: {
828  return UniValue::VSTR == result.getType();
829  }
830  case Type::NUM:
831  case Type::STR_AMOUNT:
832  case Type::NUM_TIME: {
833  return UniValue::VNUM == result.getType();
834  }
835  case Type::BOOL: {
836  return UniValue::VBOOL == result.getType();
837  }
838  case Type::ARR_FIXED:
839  case Type::ARR: {
840  return UniValue::VARR == result.getType();
841  }
842  case Type::OBJ_DYN:
843  case Type::OBJ: {
844  return UniValue::VOBJ == result.getType();
845  }
846  } // no default case, so the compiler can warn about missing cases
847  CHECK_NONFATAL(false);
848 }
849 
850 std::string RPCArg::ToStringObj(const bool oneline) const
851 {
852  std::string res;
853  res += "\"";
854  res += GetFirstName();
855  if (oneline) {
856  res += "\":";
857  } else {
858  res += "\": ";
859  }
860  switch (m_type) {
861  case Type::STR:
862  return res + "\"str\"";
863  case Type::STR_HEX:
864  return res + "\"hex\"";
865  case Type::NUM:
866  return res + "n";
867  case Type::RANGE:
868  return res + "n or [n,n]";
869  case Type::AMOUNT:
870  return res + "amount";
871  case Type::BOOL:
872  return res + "bool";
873  case Type::ARR:
874  res += "[";
875  for (const auto& i : m_inner) {
876  res += i.ToString(oneline) + ",";
877  }
878  return res + "...]";
879  case Type::OBJ:
880  case Type::OBJ_USER_KEYS:
881  // Currently unused, so avoid writing dead code
882  CHECK_NONFATAL(false);
883  } // no default case, so the compiler can warn about missing cases
884  CHECK_NONFATAL(false);
885 }
886 
887 std::string RPCArg::ToString(const bool oneline) const
888 {
889  if (oneline && !m_oneline_description.empty()) return m_oneline_description;
890 
891  switch (m_type) {
892  case Type::STR_HEX:
893  case Type::STR: {
894  return "\"" + GetFirstName() + "\"";
895  }
896  case Type::NUM:
897  case Type::RANGE:
898  case Type::AMOUNT:
899  case Type::BOOL: {
900  return GetFirstName();
901  }
902  case Type::OBJ:
903  case Type::OBJ_USER_KEYS: {
904  const std::string res = Join(m_inner, ",", [&](const RPCArg& i) { return i.ToStringObj(oneline); });
905  if (m_type == Type::OBJ) {
906  return "{" + res + "}";
907  } else {
908  return "{" + res + ",...}";
909  }
910  }
911  case Type::ARR: {
912  std::string res;
913  for (const auto& i : m_inner) {
914  res += i.ToString(oneline) + ",";
915  }
916  return "[" + res + "...]";
917  }
918  } // no default case, so the compiler can warn about missing cases
919  CHECK_NONFATAL(false);
920 }
921 
922 static std::pair<int64_t, int64_t> ParseRange(const UniValue& value)
923 {
924  if (value.isNum()) {
925  return {0, value.get_int64()};
926  }
927  if (value.isArray() && value.size() == 2 && value[0].isNum() && value[1].isNum()) {
928  int64_t low = value[0].get_int64();
929  int64_t high = value[1].get_int64();
930  if (low > high) throw JSONRPCError(RPC_INVALID_PARAMETER, "Range specified as [begin,end] must not have begin after end");
931  return {low, high};
932  }
933  throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified as end or as [begin,end]");
934 }
935 
936 std::pair<int64_t, int64_t> ParseDescriptorRange(const UniValue& value)
937 {
938  int64_t low, high;
939  std::tie(low, high) = ParseRange(value);
940  if (low < 0) {
941  throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should be greater or equal than 0");
942  }
943  if ((high >> 31) != 0) {
944  throw JSONRPCError(RPC_INVALID_PARAMETER, "End of range is too high");
945  }
946  if (high >= low + 1000000) {
947  throw JSONRPCError(RPC_INVALID_PARAMETER, "Range is too large");
948  }
949  return {low, high};
950 }
951 
952 std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, FlatSigningProvider& provider)
953 {
954  std::string desc_str;
955  std::pair<int64_t, int64_t> range = {0, 1000};
956  if (scanobject.isStr()) {
957  desc_str = scanobject.get_str();
958  } else if (scanobject.isObject()) {
959  UniValue desc_uni = find_value(scanobject, "desc");
960  if (desc_uni.isNull()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor needs to be provided in scan object");
961  desc_str = desc_uni.get_str();
962  UniValue range_uni = find_value(scanobject, "range");
963  if (!range_uni.isNull()) {
964  range = ParseDescriptorRange(range_uni);
965  }
966  } else {
967  throw JSONRPCError(RPC_INVALID_PARAMETER, "Scan object needs to be either a string or an object");
968  }
969 
970  std::string error;
971  auto desc = Parse(desc_str, provider, error);
972  if (!desc) {
974  }
975  if (!desc->IsRange()) {
976  range.first = 0;
977  range.second = 0;
978  }
979  std::vector<CScript> ret;
980  for (int i = range.first; i <= range.second; ++i) {
981  std::vector<CScript> scripts;
982  if (!desc->Expand(i, provider, scripts, provider)) {
983  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys: '%s'", desc_str));
984  }
985  std::move(scripts.begin(), scripts.end(), std::back_inserter(ret));
986  }
987  return ret;
988 }
989 
991 {
992  UniValue servicesNames(UniValue::VARR);
993 
994  for (const auto& flag : serviceFlagsToStr(services)) {
995  servicesNames.push_back(flag);
996  }
997 
998  return servicesNames;
999 }
DescribeAddressVisitor::operator()
UniValue operator()(const WitnessV0ScriptHash &id) const
Definition: util.cpp:294
RPCHelpMan::m_fun
const RPCMethodImpl m_fun
Definition: util.h:354
UNIX_EPOCH_TIME
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:20
RPCResult::Type::ELISION
@ ELISION
Special type to denote elision (...)
Parse
std::unique_ptr< Descriptor > Parse(const std::string &descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum)
Parse a descriptor string.
Definition: descriptor.cpp:1173
ParseHashV
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded Values (throws error if not hex).
Definition: util.cpp:89
HelpExampleRpcNamed
std::string HelpExampleRpcNamed(const std::string &methodname, const RPCArgList &args)
Definition: util.cpp:179
HexToPubKey
CPubKey HexToPubKey(const std::string &hex_in)
Definition: util.cpp:191
DescribeAddressVisitor::operator()
UniValue operator()(const WitnessV0KeyHash &id) const
Definition: util.cpp:284
RPCArg::ToStringObj
std::string ToStringObj(bool oneline) const
Return the type string of the argument when it is in an object (dict).
Definition: util.cpp:850
OutputType
OutputType
Definition: outputtype.h:17
ParseHex
std::vector< unsigned char > ParseHex(const char *psz)
Definition: strencodings.cpp:84
WitnessUnknown
CTxDestination subtype to encode any future Witness version.
Definition: standard.h:180
UniValue::VOBJ
@ VOBJ
Definition: univalue.h:21
RPCArg::m_oneline_description
const std::string m_oneline_description
Should be empty unless it is supposed to override the auto-generated summary line.
Definition: util.h:155
DescribeAddressVisitor::DescribeAddressVisitor
DescribeAddressVisitor()
Definition: util.cpp:261
ParseHashO
uint256 ParseHashO(const UniValue &o, std::string strKey)
Definition: util.cpp:98
DescribeAddress
UniValue DescribeAddress(const CTxDestination &dest)
Definition: util.cpp:314
ParseFixedPoint
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
Definition: strencodings.cpp:481
RPC_INTERNAL_ERROR
@ RPC_INTERNAL_ERROR
Definition: protocol.h:35
AddrToPubKey
CPubKey AddrToPubKey(const FillableSigningProvider &keystore, const std::string &addr_in)
Definition: util.cpp:204
CHECK_NONFATAL
#define CHECK_NONFATAL(condition)
Throw a NonFatalCheckError when the condition evaluates to false.
Definition: check.h:32
EXAMPLE_ADDRESS
const std::string EXAMPLE_ADDRESS[2]
Example bech32 addresses for the RPCExamples help documentation.
Definition: util.cpp:21
OuterType::NONE
@ NONE
TransactionError::MEMPOOL_REJECTED
@ MEMPOOL_REJECTED
RPCHelpMan::m_results
const RPCResults m_results
Definition: util.h:357
TransactionErrorString
bilingual_str TransactionErrorString(const TransactionError err)
Definition: error.cpp:11
RPC_CLIENT_P2P_DISABLED
@ RPC_CLIENT_P2P_DISABLED
No valid connection manager instance found.
Definition: protocol.h:64
RPCErrorFromTransactionError
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
Definition: util.cpp:329
key_io.h
RPC_TRANSACTION_REJECTED
@ RPC_TRANSACTION_REJECTED
Definition: protocol.h:54
DescribeAddressVisitor::operator()
UniValue operator()(const CNoDestination &dest) const
Definition: util.cpp:263
OutputType::LEGACY
@ LEGACY
RPCHelpMan
Definition: util.h:336
FillableSigningProvider
Fillable signing provider that keeps keys in an address->secret map.
Definition: signingprovider.h:63
AddAndGetDestinationForScript
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:79
string.h
RPC_INVALID_PARAMETER
@ RPC_INVALID_PARAMETER
Invalid, missing or duplicate parameter.
Definition: protocol.h:43
outputtype.h
IsHex
bool IsHex(const std::string &str)
Definition: strencodings.cpp:61
RPCHelpMan::RPCMethodImpl
std::function< UniValue(const RPCHelpMan &, const JSONRPCRequest &)> RPCMethodImpl
Definition: util.h:340
serviceFlagsToStr
std::vector< std::string > serviceFlagsToStr(uint64_t flags)
Convert service flags (a bitmask of NODE_*) to human readable strings.
Definition: protocol.cpp:220
RPCArg::Optional::NO
@ NO
Required arg.
RPCResults
Definition: util.h:307
RPCArg::Type::STR
@ STR
UniValueType::type
UniValue::VType type
Definition: util.h:48
RPCArg::Type::ARR
@ ARR
RPCResult::m_type
const Type m_type
Definition: util.h:239
Section::m_left
std::string m_left
Definition: util.cpp:364
RPCArg::m_fallback
const Fallback m_fallback
Definition: util.h:153
MoneyRange
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:26
RPCResult::m_key_name
const std::string m_key_name
Only used for dicts.
Definition: util.h:240
RPCResult::Type::NUM
@ NUM
ParseHexV
std::vector< unsigned char > ParseHexV(const UniValue &v, std::string strName)
Definition: util.cpp:102
TransactionError::PSBT_MISMATCH
@ PSBT_MISMATCH
ServiceFlags
ServiceFlags
nServices flags
Definition: protocol.h:269
UniValue::isNull
bool isNull() const
Definition: univalue.h:77
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
RPCExamples::m_examples
const std::string m_examples
Definition: util.h:327
ParseHexO
std::vector< unsigned char > ParseHexO(const UniValue &o, std::string strKey)
Definition: util.cpp:111
RPCArg::m_inner
const std::vector< RPCArg > m_inner
Only used for arrays or dicts.
Definition: util.h:152
UniValue::write
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
Definition: univalue_write.cpp:29
RPCArg::Type::OBJ_USER_KEYS
@ OBJ_USER_KEYS
Special type where the user must set the keys e.g. to define multiple addresses; as opposed to e....
Section::m_right
const std::string m_right
Definition: util.cpp:365
UniValue::isNum
bool isNum() const
Definition: univalue.h:82
UniValue::getType
enum VType getType() const
Definition: univalue.h:64
RPCTypeCheckArgument
void RPCTypeCheckArgument(const UniValue &value, const UniValueType &typeExpected)
Type-check one argument; throws JSONRPCError if wrong type given.
Definition: util.cpp:40
UniValueType::typeAny
bool typeAny
Definition: util.h:47
RPCResult::m_inner
const std::vector< RPCResult > m_inner
Only used for arrays or dicts.
Definition: util.h:241
UniValue::pushKV
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
DescribeAddressVisitor::operator()
UniValue operator()(const PKHash &keyID) const
Definition: util.cpp:268
RPCArg
Definition: util.h:119
RPCHelpMan::m_args
const std::vector< RPCArg > m_args
Definition: util.h:356
RPC_TRANSACTION_ALREADY_IN_CHAIN
@ RPC_TRANSACTION_ALREADY_IN_CHAIN
Definition: protocol.h:55
UniValue
Definition: univalue.h:19
tinyformat.h
OuterType
OuterType
Serializing JSON objects depends on the outer type.
Definition: util.h:113
WitnessV0KeyHash
Definition: standard.h:170
RPCResult::ToSections
void ToSections(Sections &sections, OuterType outer_type=OuterType::NONE, const int current_indent=0) const
Append the sections of the result.
Definition: util.cpp:719
RPCResult::Type::ARR_FIXED
@ ARR_FIXED
Special array that has a fixed number of entries.
RPCArg::Type::NUM
@ NUM
UniValue::type
enum VType type() const
Definition: univalue.h:181
RPCArg::IsOptional
bool IsOptional() const
Definition: util.cpp:647
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
signingprovider.h
RPCHelpMan::m_examples
const RPCExamples m_examples
Definition: util.h:358
RPCHelpMan::GetArgMap
UniValue GetArgMap() const
Return the named args that need to be converted from string to another JSON type.
Definition: util.cpp:616
UniValue::get_str
const std::string & get_str() const
Definition: univalue_get.cpp:97
RPC_DESERIALIZATION_ERROR
@ RPC_DESERIALIZATION_ERROR
Error parsing or validating structure in raw format.
Definition: protocol.h:45
UniValueType
Wrapper for UniValue::VType, which includes typeAny: Used to denote don't care type.
Definition: util.h:44
strencodings.h
UniValue::isStr
bool isStr() const
Definition: univalue.h:81
TransactionError
TransactionError
Definition: error.h:22
Section::Section
Section(const std::string &left, const std::string &right)
Definition: util.cpp:362
RPCHelpMan::ToString
std::string ToString() const
Definition: util.cpp:564
UniValue::get_int64
int64_t get_int64() const
Definition: univalue_get.cpp:114
RPCArg::Type::OBJ
@ OBJ
RPCHelpMan::IsValidNumArgs
bool IsValidNumArgs(size_t num_args) const
If the supplied number of args is neither too small nor too high.
Definition: util.cpp:543
IsValidDestination
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:328
RPCArg::Optional::OMITTED_NAMED_ARG
@ OMITTED_NAMED_ARG
Optional arg that is a named argument and has a default value of null.
FillableSigningProvider::GetPubKey
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
Definition: signingprovider.cpp:95
RPCTypeCheckObj
void RPCTypeCheckObj(const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull, bool fStrict)
Definition: util.cpp:47
RPCArg::Type::STR_HEX
@ STR_HEX
Special type that is a STR with only hex chars.
RPCResult::Type::OBJ
@ OBJ
RPCResult::Type::NONE
@ NONE
RPCHelpMan::HandleRequest
UniValue HandleRequest(const JSONRPCRequest &request) const
Definition: util.cpp:526
GetScriptForMultisig
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:317
RPCHelpMan::m_description
const std::string m_description
Definition: util.h:355
RPCResults::ToDescriptionString
std::string ToDescriptionString() const
Return the description string.
Definition: util.cpp:504
RPCArg::m_description
const std::string m_description
Definition: util.h:154
UniValue::VBOOL
@ VBOOL
Definition: univalue.h:21
ParseRange
static std::pair< int64_t, int64_t > ParseRange(const UniValue &value)
Definition: util.cpp:922
Sections
Keeps track of RPCArgs by transforming them into sections for the purpose of serializing everything t...
Definition: util.cpp:372
RPCResult::Type::STR_HEX
@ STR_HEX
Special string with only hex chars.
TransactionError::INVALID_PSBT
@ INVALID_PSBT
TransactionError::SIGHASH_MISMATCH
@ SIGHASH_MISMATCH
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
RPCExamples
Definition: util.h:326
Sections::m_max_pad
size_t m_max_pad
Definition: util.cpp:374
GetKeyForDestination
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
Definition: signingprovider.cpp:178
JSONRPCRequest::mode
enum JSONRPCRequest::Mode mode
TransactionError::P2P_DISABLED
@ P2P_DISABLED
RPCResult::MatchesType
bool MatchesType(const UniValue &result) const
Check whether the result JSON type matches.
Definition: util.cpp:814
UniValue::getKeys
const std::vector< std::string > & getKeys() const
Definition: univalue_get.cpp:76
uvTypeName
const char * uvTypeName(UniValue::VType t)
Definition: univalue.cpp:219
Sections::m_sections
std::vector< Section > m_sections
Definition: util.cpp:373
RPCResult::Type::STR
@ STR
RPCHelpMan::RPCHelpMan
RPCHelpMan(std::string name, std::string description, std::vector< RPCArg > args, RPCResults results, RPCExamples examples)
Definition: util.cpp:482
UniValue::VNUM
@ VNUM
Definition: univalue.h:21
RPCExamples::ToDescriptionString
std::string ToDescriptionString() const
Definition: util.cpp:521
uint256
256-bit opaque blob.
Definition: uint256.h:124
uint256S
uint256 uint256S(const char *str)
Definition: uint256.h:137
DescribeAddressVisitor::operator()
UniValue operator()(const ScriptHash &scriptID) const
Definition: util.cpp:276
RPCResult::Type::NUM_TIME
@ NUM_TIME
Special numeric to denote unix epoch time.
RPCResult::Type::ARR
@ ARR
RPCArg::m_names
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:149
DecodeDestination
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg)
Definition: key_io.cpp:246
RPCArg::GetName
std::string GetName() const
Return the name, throws when there are aliases.
Definition: util.cpp:641
RPCResult::Type::ANY
@ ANY
Special type to disable type checks (for testing only)
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
UniValue::isArray
bool isArray() const
Definition: univalue.h:83
RPCArg::m_type_str
const std::vector< std::string > m_type_str
Should be empty unless it is supposed to override the auto-generated type strings....
Definition: util.h:156
RPCResult::m_optional
const bool m_optional
Definition: util.h:242
RPCArg::Type::RANGE
@ RANGE
Special type that is a NUM or [NUM,NUM].
CNoDestination
Definition: standard.h:134
DescribeAddressVisitor::operator()
UniValue operator()(const WitnessUnknown &id) const
Definition: util.cpp:304
RPCArg::GetFirstName
std::string GetFirstName() const
Return the first of all aliases.
Definition: util.cpp:636
RPCHelpMan::GetArgNames
std::vector< std::string > GetArgNames() const
Definition: util.cpp:555
JSONRPCRequest::params
UniValue params
Definition: request.h:33
RPC_INVALID_ADDRESS_OR_KEY
@ RPC_INVALID_ADDRESS_OR_KEY
Invalid address or key.
Definition: protocol.h:41
name
const char * name
Definition: rest.cpp:42
JSONRPCRequest::GET_HELP
@ GET_HELP
Definition: request.h:34
PKHash
Definition: standard.h:140
ParseConfirmTarget
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:319
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
HelpExampleRpc
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:173
UniValue::getValStr
const std::string & getValStr() const
Definition: univalue.h:65
Join
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
Definition: string.h:44
AddAndGetMultisigDestination
CTxDestination AddAndGetMultisigDestination(const int required, const std::vector< CPubKey > &pubkeys, OutputType type, FillableSigningProvider &keystore, CScript &script_out)
Definition: util.cpp:225
RPCArg::m_type
const Type m_type
Definition: util.h:150
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:31
Sections::PushSection
void PushSection(const Section &s)
Definition: util.cpp:376
Sections::Push
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:385
RPCResult::Type::BOOL
@ BOOL
UniValue::get_int
int get_int() const
Definition: univalue_get.cpp:104
RPCArgList
std::vector< std::pair< std::string, UniValue > > RPCArgList
Definition: util.h:82
translation.h
Sections::ToString
std::string ToString() const
Concatenate all sections with proper padding.
Definition: util.cpp:441
JSONRPCError
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:51
RPCErrorCode
RPCErrorCode
Bitcoin RPC error codes.
Definition: protocol.h:23
RPCArg::Type::BOOL
@ BOOL
RPCResults::m_results
const std::vector< RPCResult > m_results
Definition: util.h:308
RPCArg::Optional::OMITTED
@ OMITTED
Optional argument with default value omitted because they are implicitly clear.
RPCArg::ToDescriptionString
std::string ToDescriptionString() const
Return the description string, including the argument type and whether the argument is required.
Definition: util.cpp:656
prevector::size
size_type size() const
Definition: prevector.h:282
UniValue::push_back
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
OuterType::ARR
@ ARR
CPubKey::IsFullyValid
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid())
Definition: pubkey.cpp:230
CTxDestination
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:212
OuterType::OBJ
@ OBJ
JSONRPCRequest::GET_ARGS
@ GET_ARGS
Definition: request.h:34
HelpExampleCliNamed
std::string HelpExampleCliNamed(const std::string &methodname, const RPCArgList &args)
Definition: util.cpp:160
RPCResult::Type::OBJ_DYN
@ OBJ_DYN
Special dictionary with keys that are not literals.
UniValue::size
size_t size() const
Definition: univalue.h:68
MAX_SCRIPT_ELEMENT_SIZE
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:23
RPCArg::Type::AMOUNT
@ AMOUNT
Special type representing a floating point amount (can be either NUM or STR)
JSONRPCRequest
Definition: request.h:28
util.h
RPCResult
Definition: util.h:222
RPCArg::ToString
std::string ToString(bool oneline) const
Return the type string of the argument.
Definition: util.cpp:887
find_value
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:234
RPC_TYPE_ERROR
@ RPC_TYPE_ERROR
Unexpected type was passed as parameter.
Definition: protocol.h:40
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:50
RPCResult::Type::STR_AMOUNT
@ STR_AMOUNT
Special string to represent a floating point amount.
UniValue::VARR
@ VARR
Definition: univalue.h:21
UniValue::VNULL
@ VNULL
Definition: univalue.h:21
WitnessV0ScriptHash
Definition: standard.h:163
GetServicesNames
UniValue GetServicesNames(ServiceFlags services)
Returns, given services flags, a list of humanly readable (known) network services.
Definition: util.cpp:990
EvalDescriptorStringOrObject
std::vector< CScript > EvalDescriptorStringOrObject(const UniValue &scanobject, FlatSigningProvider &provider)
Evaluate a descriptor given as a string, or as a {"desc":...,"range":...} object, with default range ...
Definition: util.cpp:952
RPCTypeCheck
void RPCTypeCheck(const UniValue &params, const std::list< UniValueType > &typesExpected, bool fAllowNull)
Type-check arguments; throws JSONRPCError if wrong type given.
Definition: util.cpp:23
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:594
RPCResult::m_description
const std::string m_description
Definition: util.h:243
UniValue::VSTR
@ VSTR
Definition: univalue.h:21
JSONRPCTransactionError
UniValue JSONRPCTransactionError(TransactionError terr, const std::string &err_string)
Definition: util.cpp:348
RPCHelpMan::m_name
const std::string m_name
Definition: util.h:351
ScriptHash
Definition: standard.h:150
AmountFromValue
CAmount AmountFromValue(const UniValue &value)
Definition: util.cpp:77
DescribeAddressVisitor
Definition: util.cpp:258
FlatSigningProvider
Definition: signingprovider.h:47
descriptor.h
RPC_TRANSACTION_ERROR
@ RPC_TRANSACTION_ERROR
Aliases for backward compatibility.
Definition: protocol.h:53
Section
A pair of strings that can be aligned (through padding) with other Sections later on.
Definition: util.cpp:361
ParseDescriptorRange
std::pair< int64_t, int64_t > ParseDescriptorRange(const UniValue &value)
Parse a JSON range specified as int64, or [int64, int64].
Definition: util.cpp:936
HelpExampleCli
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:155
UniValue::isObject
bool isObject() const
Definition: univalue.h:84
TransactionError::ALREADY_IN_CHAIN
@ ALREADY_IN_CHAIN