Bitcoin Core  0.20.99
P2P Digital Currency
util.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2020 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 
117 {
118  if (param.isNull()) {
119  return default_type;
120  } else {
121  std::string hash_type_input = param.get_str();
122 
123  if (hash_type_input == "hash_serialized_2") {
125  } else if (hash_type_input == "none") {
127  } else {
128  throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%d is not a valid hash_type", hash_type_input));
129  }
130  }
131 }
132 
133 std::string HelpExampleCli(const std::string& methodname, const std::string& args)
134 {
135  return "> bitcoin-cli " + methodname + " " + args + "\n";
136 }
137 
138 std::string HelpExampleRpc(const std::string& methodname, const std::string& args)
139 {
140  return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", "
141  "\"method\": \"" + methodname + "\", \"params\": [" + args + "]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
142 }
143 
144 // Converts a hex string to a public key if possible
145 CPubKey HexToPubKey(const std::string& hex_in)
146 {
147  if (!IsHex(hex_in)) {
148  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in);
149  }
150  CPubKey vchPubKey(ParseHex(hex_in));
151  if (!vchPubKey.IsFullyValid()) {
152  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in);
153  }
154  return vchPubKey;
155 }
156 
157 // Retrieves a public key for an address from the given FillableSigningProvider
158 CPubKey AddrToPubKey(const FillableSigningProvider& keystore, const std::string& addr_in)
159 {
160  CTxDestination dest = DecodeDestination(addr_in);
161  if (!IsValidDestination(dest)) {
162  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address: " + addr_in);
163  }
164  CKeyID key = GetKeyForDestination(keystore, dest);
165  if (key.IsNull()) {
166  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("%s does not refer to a key", addr_in));
167  }
168  CPubKey vchPubKey;
169  if (!keystore.GetPubKey(key, vchPubKey)) {
170  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("no full public key for address %s", addr_in));
171  }
172  if (!vchPubKey.IsFullyValid()) {
173  throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet contains an invalid public key");
174  }
175  return vchPubKey;
176 }
177 
178 // Creates a multisig address from a given list of public keys, number of signatures required, and the address type
179 CTxDestination AddAndGetMultisigDestination(const int required, const std::vector<CPubKey>& pubkeys, OutputType type, FillableSigningProvider& keystore, CScript& script_out)
180 {
181  // Gather public keys
182  if (required < 1) {
183  throw JSONRPCError(RPC_INVALID_PARAMETER, "a multisignature address must require at least one key to redeem");
184  }
185  if ((int)pubkeys.size() < required) {
186  throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("not enough keys supplied (got %u keys, but need at least %d to redeem)", pubkeys.size(), required));
187  }
188  if (pubkeys.size() > 16) {
189  throw JSONRPCError(RPC_INVALID_PARAMETER, "Number of keys involved in the multisignature address creation > 16\nReduce the number");
190  }
191 
192  script_out = GetScriptForMultisig(required, pubkeys);
193 
194  if (script_out.size() > MAX_SCRIPT_ELEMENT_SIZE) {
195  throw JSONRPCError(RPC_INVALID_PARAMETER, (strprintf("redeemScript exceeds size limit: %d > %d", script_out.size(), MAX_SCRIPT_ELEMENT_SIZE)));
196  }
197 
198  // Check if any keys are uncompressed. If so, the type is legacy
199  for (const CPubKey& pk : pubkeys) {
200  if (!pk.IsCompressed()) {
201  type = OutputType::LEGACY;
202  break;
203  }
204  }
205 
206  // Make the address
207  CTxDestination dest = AddAndGetDestinationForScript(keystore, script_out, type);
208 
209  return dest;
210 }
211 
212 class DescribeAddressVisitor : public boost::static_visitor<UniValue>
213 {
214 public:
216 
217  UniValue operator()(const CNoDestination& dest) const
218  {
219  return UniValue(UniValue::VOBJ);
220  }
221 
222  UniValue operator()(const PKHash& keyID) const
223  {
225  obj.pushKV("isscript", false);
226  obj.pushKV("iswitness", false);
227  return obj;
228  }
229 
230  UniValue operator()(const ScriptHash& scriptID) const
231  {
233  obj.pushKV("isscript", true);
234  obj.pushKV("iswitness", false);
235  return obj;
236  }
237 
239  {
241  obj.pushKV("isscript", false);
242  obj.pushKV("iswitness", true);
243  obj.pushKV("witness_version", 0);
244  obj.pushKV("witness_program", HexStr(id));
245  return obj;
246  }
247 
249  {
251  obj.pushKV("isscript", true);
252  obj.pushKV("iswitness", true);
253  obj.pushKV("witness_version", 0);
254  obj.pushKV("witness_program", HexStr(id));
255  return obj;
256  }
257 
259  {
261  obj.pushKV("iswitness", true);
262  obj.pushKV("witness_version", (int)id.version);
263  obj.pushKV("witness_program", HexStr(Span<const unsigned char>(id.program, id.length)));
264  return obj;
265  }
266 };
267 
269 {
270  return boost::apply_visitor(DescribeAddressVisitor(), dest);
271 }
272 
273 unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target)
274 {
275  int target = value.get_int();
276  if (target < 1 || (unsigned int)target > max_target) {
277  throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid conf_target, must be between %u - %u", 1, max_target));
278  }
279  return (unsigned int)target;
280 }
281 
283 {
284  switch (terr) {
293  return RPC_INVALID_PARAMETER;
296  default: break;
297  }
298  return RPC_TRANSACTION_ERROR;
299 }
300 
301 UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string)
302 {
303  if (err_string.length() > 0) {
304  return JSONRPCError(RPCErrorFromTransactionError(terr), err_string);
305  } else {
307  }
308 }
309 
314 struct Section {
315  Section(const std::string& left, const std::string& right)
316  : m_left{left}, m_right{right} {}
317  std::string m_left;
318  const std::string m_right;
319 };
320 
325 struct Sections {
326  std::vector<Section> m_sections;
327  size_t m_max_pad{0};
328 
329  void PushSection(const Section& s)
330  {
331  m_max_pad = std::max(m_max_pad, s.m_left.size());
332  m_sections.push_back(s);
333  }
334 
338  void Push(const RPCArg& arg, const size_t current_indent = 5, const OuterType outer_type = OuterType::NONE)
339  {
340  const auto indent = std::string(current_indent, ' ');
341  const auto indent_next = std::string(current_indent + 2, ' ');
342  const bool push_name{outer_type == OuterType::OBJ}; // Dictionary keys must have a name
343 
344  switch (arg.m_type) {
346  case RPCArg::Type::STR:
347  case RPCArg::Type::NUM:
349  case RPCArg::Type::RANGE:
350  case RPCArg::Type::BOOL: {
351  if (outer_type == OuterType::NONE) return; // Nothing more to do for non-recursive types on first recursion
352  auto left = indent;
353  if (arg.m_type_str.size() != 0 && push_name) {
354  left += "\"" + arg.GetName() + "\": " + arg.m_type_str.at(0);
355  } else {
356  left += push_name ? arg.ToStringObj(/* oneline */ false) : arg.ToString(/* oneline */ false);
357  }
358  left += ",";
359  PushSection({left, arg.ToDescriptionString()});
360  break;
361  }
362  case RPCArg::Type::OBJ:
364  const auto right = outer_type == OuterType::NONE ? "" : arg.ToDescriptionString();
365  PushSection({indent + (push_name ? "\"" + arg.GetName() + "\": " : "") + "{", right});
366  for (const auto& arg_inner : arg.m_inner) {
367  Push(arg_inner, current_indent + 2, OuterType::OBJ);
368  }
369  if (arg.m_type != RPCArg::Type::OBJ) {
370  PushSection({indent_next + "...", ""});
371  }
372  PushSection({indent + "}" + (outer_type != OuterType::NONE ? "," : ""), ""});
373  break;
374  }
375  case RPCArg::Type::ARR: {
376  auto left = indent;
377  left += push_name ? "\"" + arg.GetName() + "\": " : "";
378  left += "[";
379  const auto right = outer_type == OuterType::NONE ? "" : arg.ToDescriptionString();
380  PushSection({left, right});
381  for (const auto& arg_inner : arg.m_inner) {
382  Push(arg_inner, current_indent + 2, OuterType::ARR);
383  }
384  PushSection({indent_next + "...", ""});
385  PushSection({indent + "]" + (outer_type != OuterType::NONE ? "," : ""), ""});
386  break;
387  }
388  } // no default case, so the compiler can warn about missing cases
389  }
390 
394  std::string ToString() const
395  {
396  std::string ret;
397  const size_t pad = m_max_pad + 4;
398  for (const auto& s : m_sections) {
399  // The left part of a section is assumed to be a single line, usually it is the name of the JSON struct or a
400  // brace like {, }, [, or ]
401  CHECK_NONFATAL(s.m_left.find('\n') == std::string::npos);
402  if (s.m_right.empty()) {
403  ret += s.m_left;
404  ret += "\n";
405  continue;
406  }
407 
408  std::string left = s.m_left;
409  left.resize(pad, ' ');
410  ret += left;
411 
412  // Properly pad after newlines
413  std::string right;
414  size_t begin = 0;
415  size_t new_line_pos = s.m_right.find_first_of('\n');
416  while (true) {
417  right += s.m_right.substr(begin, new_line_pos - begin);
418  if (new_line_pos == std::string::npos) {
419  break; //No new line
420  }
421  right += "\n" + std::string(pad, ' ');
422  begin = s.m_right.find_first_not_of(' ', new_line_pos + 1);
423  if (begin == std::string::npos) {
424  break; // Empty line
425  }
426  new_line_pos = s.m_right.find_first_of('\n', begin + 1);
427  }
428  ret += right;
429  ret += "\n";
430  }
431  return ret;
432  }
433 };
434 
435 RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples)
436  : RPCHelpMan{std::move(name), std::move(description), std::move(args), std::move(results), std::move(examples), nullptr} {}
437 
438 RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun)
439  : m_name{std::move(name)},
440  m_fun{std::move(fun)},
441  m_description{std::move(description)},
442  m_args{std::move(args)},
443  m_results{std::move(results)},
444  m_examples{std::move(examples)}
445 {
446  std::set<std::string> named_args;
447  for (const auto& arg : m_args) {
448  std::vector<std::string> names;
449  boost::split(names, arg.m_names, boost::is_any_of("|"));
450  // Should have unique named arguments
451  for (const std::string& name : names) {
452  CHECK_NONFATAL(named_args.insert(name).second);
453  }
454  }
455 }
456 
458 {
459  std::string result;
460  for (const auto& r : m_results) {
461  if (r.m_cond.empty()) {
462  result += "\nResult:\n";
463  } else {
464  result += "\nResult (" + r.m_cond + "):\n";
465  }
466  Sections sections;
467  r.ToSections(sections);
468  result += sections.ToString();
469  }
470  return result;
471 }
472 
474 {
475  return m_examples.empty() ? m_examples : "\nExamples:\n" + m_examples;
476 }
477 
478 bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
479 {
480  size_t num_required_args = 0;
481  for (size_t n = m_args.size(); n > 0; --n) {
482  if (!m_args.at(n - 1).IsOptional()) {
483  num_required_args = n;
484  break;
485  }
486  }
487  return num_required_args <= num_args && num_args <= m_args.size();
488 }
489 
490 std::vector<std::string> RPCHelpMan::GetArgNames() const
491 {
492  std::vector<std::string> ret;
493  for (const auto& arg : m_args) {
494  ret.emplace_back(arg.m_names);
495  }
496  return ret;
497 }
498 
499 std::string RPCHelpMan::ToString() const
500 {
501  std::string ret;
502 
503  // Oneline summary
504  ret += m_name;
505  bool was_optional{false};
506  for (const auto& arg : m_args) {
507  if (arg.m_hidden) break; // Any arg that follows is also hidden
508  const bool optional = arg.IsOptional();
509  ret += " ";
510  if (optional) {
511  if (!was_optional) ret += "( ";
512  was_optional = true;
513  } else {
514  if (was_optional) ret += ") ";
515  was_optional = false;
516  }
517  ret += arg.ToString(/* oneline */ true);
518  }
519  if (was_optional) ret += " )";
520  ret += "\n";
521 
522  // Description
523  ret += m_description;
524 
525  // Arguments
526  Sections sections;
527  for (size_t i{0}; i < m_args.size(); ++i) {
528  const auto& arg = m_args.at(i);
529  if (arg.m_hidden) break; // Any arg that follows is also hidden
530 
531  if (i == 0) ret += "\nArguments:\n";
532 
533  // Push named argument name and description
534  sections.m_sections.emplace_back(::ToString(i + 1) + ". " + arg.GetFirstName(), arg.ToDescriptionString());
535  sections.m_max_pad = std::max(sections.m_max_pad, sections.m_sections.back().m_left.size());
536 
537  // Recursively push nested args
538  sections.Push(arg);
539  }
540  ret += sections.ToString();
541 
542  // Result
544 
545  // Examples
547 
548  return ret;
549 }
550 
551 std::string RPCArg::GetFirstName() const
552 {
553  return m_names.substr(0, m_names.find("|"));
554 }
555 
556 std::string RPCArg::GetName() const
557 {
558  CHECK_NONFATAL(std::string::npos == m_names.find("|"));
559  return m_names;
560 }
561 
562 bool RPCArg::IsOptional() const
563 {
564  if (m_fallback.which() == 1) {
565  return true;
566  } else {
567  return RPCArg::Optional::NO != boost::get<RPCArg::Optional>(m_fallback);
568  }
569 }
570 
571 std::string RPCArg::ToDescriptionString() const
572 {
573  std::string ret;
574  ret += "(";
575  if (m_type_str.size() != 0) {
576  ret += m_type_str.at(1);
577  } else {
578  switch (m_type) {
579  case Type::STR_HEX:
580  case Type::STR: {
581  ret += "string";
582  break;
583  }
584  case Type::NUM: {
585  ret += "numeric";
586  break;
587  }
588  case Type::AMOUNT: {
589  ret += "numeric or string";
590  break;
591  }
592  case Type::RANGE: {
593  ret += "numeric or array";
594  break;
595  }
596  case Type::BOOL: {
597  ret += "boolean";
598  break;
599  }
600  case Type::OBJ:
601  case Type::OBJ_USER_KEYS: {
602  ret += "json object";
603  break;
604  }
605  case Type::ARR: {
606  ret += "json array";
607  break;
608  }
609  } // no default case, so the compiler can warn about missing cases
610  }
611  if (m_fallback.which() == 1) {
612  ret += ", optional, default=" + boost::get<std::string>(m_fallback);
613  } else {
614  switch (boost::get<RPCArg::Optional>(m_fallback)) {
616  // nothing to do. Element is treated as if not present and has no default value
617  break;
618  }
620  ret += ", optional"; // Default value is "null"
621  break;
622  }
623  case RPCArg::Optional::NO: {
624  ret += ", required";
625  break;
626  }
627  } // no default case, so the compiler can warn about missing cases
628  }
629  ret += ")";
630  ret += m_description.empty() ? "" : " " + m_description;
631  return ret;
632 }
633 
634 void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const int current_indent) const
635 {
636  // Indentation
637  const std::string indent(current_indent, ' ');
638  const std::string indent_next(current_indent + 2, ' ');
639 
640  // Elements in a JSON structure (dictionary or array) are separated by a comma
641  const std::string maybe_separator{outer_type != OuterType::NONE ? "," : ""};
642 
643  // The key name if recursed into an dictionary
644  const std::string maybe_key{
645  outer_type == OuterType::OBJ ?
646  "\"" + this->m_key_name + "\" : " :
647  ""};
648 
649  // Format description with type
650  const auto Description = [&](const std::string& type) {
651  return "(" + type + (this->m_optional ? ", optional" : "") + ")" +
652  (this->m_description.empty() ? "" : " " + this->m_description);
653  };
654 
655  switch (m_type) {
656  case Type::ELISION: {
657  // If the inner result is empty, use three dots for elision
658  sections.PushSection({indent + "..." + maybe_separator, m_description});
659  return;
660  }
661  case Type::NONE: {
662  sections.PushSection({indent + "null" + maybe_separator, Description("json null")});
663  return;
664  }
665  case Type::STR: {
666  sections.PushSection({indent + maybe_key + "\"str\"" + maybe_separator, Description("string")});
667  return;
668  }
669  case Type::STR_AMOUNT: {
670  sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")});
671  return;
672  }
673  case Type::STR_HEX: {
674  sections.PushSection({indent + maybe_key + "\"hex\"" + maybe_separator, Description("string")});
675  return;
676  }
677  case Type::NUM: {
678  sections.PushSection({indent + maybe_key + "n" + maybe_separator, Description("numeric")});
679  return;
680  }
681  case Type::NUM_TIME: {
682  sections.PushSection({indent + maybe_key + "xxx" + maybe_separator, Description("numeric")});
683  return;
684  }
685  case Type::BOOL: {
686  sections.PushSection({indent + maybe_key + "true|false" + maybe_separator, Description("boolean")});
687  return;
688  }
689  case Type::ARR_FIXED:
690  case Type::ARR: {
691  sections.PushSection({indent + maybe_key + "[", Description("json array")});
692  for (const auto& i : m_inner) {
693  i.ToSections(sections, OuterType::ARR, current_indent + 2);
694  }
695  CHECK_NONFATAL(!m_inner.empty());
696  if (m_type == Type::ARR && m_inner.back().m_type != Type::ELISION) {
697  sections.PushSection({indent_next + "...", ""});
698  } else {
699  // Remove final comma, which would be invalid JSON
700  sections.m_sections.back().m_left.pop_back();
701  }
702  sections.PushSection({indent + "]" + maybe_separator, ""});
703  return;
704  }
705  case Type::OBJ_DYN:
706  case Type::OBJ: {
707  sections.PushSection({indent + maybe_key + "{", Description("json object")});
708  for (const auto& i : m_inner) {
709  i.ToSections(sections, OuterType::OBJ, current_indent + 2);
710  }
711  CHECK_NONFATAL(!m_inner.empty());
712  if (m_type == Type::OBJ_DYN && m_inner.back().m_type != Type::ELISION) {
713  // If the dictionary keys are dynamic, use three dots for continuation
714  sections.PushSection({indent_next + "...", ""});
715  } else {
716  // Remove final comma, which would be invalid JSON
717  sections.m_sections.back().m_left.pop_back();
718  }
719  sections.PushSection({indent + "}" + maybe_separator, ""});
720  return;
721  }
722  } // no default case, so the compiler can warn about missing cases
723  CHECK_NONFATAL(false);
724 }
725 
726 std::string RPCArg::ToStringObj(const bool oneline) const
727 {
728  std::string res;
729  res += "\"";
730  res += GetFirstName();
731  if (oneline) {
732  res += "\":";
733  } else {
734  res += "\": ";
735  }
736  switch (m_type) {
737  case Type::STR:
738  return res + "\"str\"";
739  case Type::STR_HEX:
740  return res + "\"hex\"";
741  case Type::NUM:
742  return res + "n";
743  case Type::RANGE:
744  return res + "n or [n,n]";
745  case Type::AMOUNT:
746  return res + "amount";
747  case Type::BOOL:
748  return res + "bool";
749  case Type::ARR:
750  res += "[";
751  for (const auto& i : m_inner) {
752  res += i.ToString(oneline) + ",";
753  }
754  return res + "...]";
755  case Type::OBJ:
756  case Type::OBJ_USER_KEYS:
757  // Currently unused, so avoid writing dead code
758  CHECK_NONFATAL(false);
759  } // no default case, so the compiler can warn about missing cases
760  CHECK_NONFATAL(false);
761 }
762 
763 std::string RPCArg::ToString(const bool oneline) const
764 {
765  if (oneline && !m_oneline_description.empty()) return m_oneline_description;
766 
767  switch (m_type) {
768  case Type::STR_HEX:
769  case Type::STR: {
770  return "\"" + GetFirstName() + "\"";
771  }
772  case Type::NUM:
773  case Type::RANGE:
774  case Type::AMOUNT:
775  case Type::BOOL: {
776  return GetFirstName();
777  }
778  case Type::OBJ:
779  case Type::OBJ_USER_KEYS: {
780  const std::string res = Join(m_inner, ",", [&](const RPCArg& i) { return i.ToStringObj(oneline); });
781  if (m_type == Type::OBJ) {
782  return "{" + res + "}";
783  } else {
784  return "{" + res + ",...}";
785  }
786  }
787  case Type::ARR: {
788  std::string res;
789  for (const auto& i : m_inner) {
790  res += i.ToString(oneline) + ",";
791  }
792  return "[" + res + "...]";
793  }
794  } // no default case, so the compiler can warn about missing cases
795  CHECK_NONFATAL(false);
796 }
797 
798 static std::pair<int64_t, int64_t> ParseRange(const UniValue& value)
799 {
800  if (value.isNum()) {
801  return {0, value.get_int64()};
802  }
803  if (value.isArray() && value.size() == 2 && value[0].isNum() && value[1].isNum()) {
804  int64_t low = value[0].get_int64();
805  int64_t high = value[1].get_int64();
806  if (low > high) throw JSONRPCError(RPC_INVALID_PARAMETER, "Range specified as [begin,end] must not have begin after end");
807  return {low, high};
808  }
809  throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified as end or as [begin,end]");
810 }
811 
812 std::pair<int64_t, int64_t> ParseDescriptorRange(const UniValue& value)
813 {
814  int64_t low, high;
815  std::tie(low, high) = ParseRange(value);
816  if (low < 0) {
817  throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should be greater or equal than 0");
818  }
819  if ((high >> 31) != 0) {
820  throw JSONRPCError(RPC_INVALID_PARAMETER, "End of range is too high");
821  }
822  if (high >= low + 1000000) {
823  throw JSONRPCError(RPC_INVALID_PARAMETER, "Range is too large");
824  }
825  return {low, high};
826 }
827 
828 std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, FlatSigningProvider& provider)
829 {
830  std::string desc_str;
831  std::pair<int64_t, int64_t> range = {0, 1000};
832  if (scanobject.isStr()) {
833  desc_str = scanobject.get_str();
834  } else if (scanobject.isObject()) {
835  UniValue desc_uni = find_value(scanobject, "desc");
836  if (desc_uni.isNull()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor needs to be provided in scan object");
837  desc_str = desc_uni.get_str();
838  UniValue range_uni = find_value(scanobject, "range");
839  if (!range_uni.isNull()) {
840  range = ParseDescriptorRange(range_uni);
841  }
842  } else {
843  throw JSONRPCError(RPC_INVALID_PARAMETER, "Scan object needs to be either a string or an object");
844  }
845 
846  std::string error;
847  auto desc = Parse(desc_str, provider, error);
848  if (!desc) {
850  }
851  if (!desc->IsRange()) {
852  range.first = 0;
853  range.second = 0;
854  }
855  std::vector<CScript> ret;
856  for (int i = range.first; i <= range.second; ++i) {
857  std::vector<CScript> scripts;
858  if (!desc->Expand(i, provider, scripts, provider)) {
859  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys: '%s'", desc_str));
860  }
861  std::move(scripts.begin(), scripts.end(), std::back_inserter(ret));
862  }
863  return ret;
864 }
865 
867 {
868  UniValue servicesNames(UniValue::VARR);
869 
870  for (const auto& flag : serviceFlagsToStr(services)) {
871  servicesNames.push_back(flag);
872  }
873 
874  return servicesNames;
875 }
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
CPubKey AddrToPubKey(const FillableSigningProvider &keystore, const std::string &addr_in)
Definition: util.cpp:158
Aliases for backward compatibility.
Definition: protocol.h:53
bool isObject() const
Definition: univalue.h:84
CAmount AmountFromValue(const UniValue &value)
Definition: util.cpp:77
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
ServiceFlags
nServices flags
Definition: protocol.h:269
std::vector< std::string > GetArgNames() const
Definition: util.cpp:490
std::string ToDescriptionString() const
Return the description string.
Definition: util.cpp:457
Required arg.
std::vector< unsigned char > ParseHexO(const UniValue &o, std::string strKey)
Definition: util.cpp:111
Keeps track of RPCArgs by transforming them into sections for the purpose of serializing everything t...
Definition: util.cpp:325
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:36
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
bool typeAny
Definition: util.h:48
std::vector< unsigned char > ParseHexV(const UniValue &v, std::string strName)
Definition: util.cpp:102
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
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
Definition: util.cpp:282
void ToSections(Sections &sections, OuterType outer_type=OuterType::NONE, const int current_indent=0) const
Append the sections of the result.
Definition: util.cpp:634
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:26
#define CHECK_NONFATAL(condition)
Throw a NonFatalCheckError when the condition evaluates to false.
Definition: check.h:32
std::vector< unsigned char > ParseHex(const char *psz)
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:322
std::string ToDescriptionString() const
Definition: util.cpp:473
const std::string m_right
Definition: util.cpp:318
void RPCTypeCheckArgument(const UniValue &value, const UniValueType &typeExpected)
Type-check one argument; throws JSONRPCError if wrong type given.
Definition: util.cpp:40
const RPCExamples m_examples
Definition: util.h:366
const std::string & get_str() const
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:82
bool isStr() const
Definition: univalue.h:81
bilingual_str TransactionErrorString(const TransactionError err)
Definition: error.cpp:11
std::vector< std::string > serviceFlagsToStr(uint64_t flags)
Convert service flags (a bitmask of NODE_*) to human readable strings.
Definition: protocol.cpp:222
const RPCMethodImpl m_fun
Definition: util.h:362
int64_t get_int64() const
bool IsValidNumArgs(size_t num_args) const
If the supplied number of args is neither too small nor too high.
Definition: util.cpp:478
const std::vector< std::string > & getKeys() const
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:23
UniValue operator()(const WitnessUnknown &id) const
Definition: util.cpp:258
const std::string & getValStr() const
Definition: univalue.h:65
UniValue operator()(const WitnessV0ScriptHash &id) const
Definition: util.cpp:248
std::string ToString() const
Definition: util.cpp:499
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool IsNull() const
Definition: uint256.h:31
const std::vector< RPCArg > m_inner
Only used for arrays or dicts.
Definition: util.h:151
OutputType
Definition: outputtype.h:17
Invalid, missing or duplicate parameter.
Definition: protocol.h:43
void RPCTypeCheckObj(const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull, bool fStrict)
Definition: util.cpp:47
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:234
size_t m_max_pad
Definition: util.cpp:327
UniValue::VType type
Definition: util.h:49
const std::string m_description
Definition: util.h:363
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
std::unique_ptr< Descriptor > Parse(const std::string &descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum)
Parse a descriptor string.
CoinStatsHashType
Definition: coinstats.h:17
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:556
const char * uvTypeName(UniValue::VType t)
Definition: univalue.cpp:219
UniValue operator()(const PKHash &keyID) const
Definition: util.cpp:222
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:51
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
Definition: util.h:118
UniValue operator()(const CNoDestination &dest) const
Definition: util.cpp:217
UniValue operator()(const ScriptHash &scriptID) const
Definition: util.cpp:230
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
Definition: pubkey.cpp:230
CTxDestination subtype to encode any future Witness version.
Definition: standard.h:182
const char * name
Definition: rest.cpp:41
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:273
bool IsOptional() const
Definition: util.cpp:562
uint256 uint256S(const char *str)
Definition: uint256.h:137
An encapsulated public key.
Definition: pubkey.h:31
Fillable signing provider that keeps keys in an address->secret map.
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:138
bool IsHex(const std::string &str)
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...
std::function< UniValue(const RPCHelpMan &, const JSONRPCRequest &)> RPCMethodImpl
Definition: util.h:336
UniValue operator()(const WitnessV0KeyHash &id) const
Definition: util.cpp:238
static std::pair< int64_t, int64_t > ParseRange(const UniValue &value)
Definition: util.cpp:798
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
std::string GetFirstName() const
Return the first of all aliases.
Definition: util.cpp:551
const std::vector< RPCArg > m_args
Definition: util.h:364
int get_int() const
Invalid address or key.
Definition: protocol.h:41
CTxDestination AddAndGetMultisigDestination(const int required, const std::vector< CPubKey > &pubkeys, OutputType type, FillableSigningProvider &keystore, CScript &script_out)
Definition: util.cpp:179
bool isNull() const
Definition: univalue.h:77
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:81
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Optional arg that is a named argument and has a default value of null.
const std::vector< std::string > m_type_str
Should be empty unless it is supposed to override the auto-generated type strings. Vector length is either 0 or 2, m_type_str.at(0) will override the type of the value in a key-value pair, m_type_str.at(1) will override the type in the argument description.
Definition: util.h:155
const RPCResults m_results
Definition: util.h:365
Special type that is a NUM or [NUM,NUM].
OuterType
Serializing JSON objects depends on the outer type.
Definition: util.h:112
RPCHelpMan(std::string name, std::string description, std::vector< RPCArg > args, RPCResults results, RPCExamples examples)
Definition: util.cpp:435
256-bit opaque blob.
Definition: uint256.h:124
Optional argument with default value omitted because they are implicitly clear.
CTxDestination DecodeDestination(const std::string &str)
Definition: key_io.cpp:215
enum VType type() const
Definition: univalue.h:177
std::string ToDescriptionString() const
Return the description string, including the argument type and whether the argument is required...
Definition: util.cpp:571
std::vector< Section > m_sections
Definition: util.cpp:326
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:828
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
std::string m_left
Definition: util.cpp:317
Section(const std::string &left, const std::string &right)
Definition: util.cpp:315
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:338
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
Special type representing a floating point amount (can be either NUM or STR)
TransactionError
Definition: error.h:22
UniValue GetServicesNames(ServiceFlags services)
Returns, given services flags, a list of humanly readable (known) network services.
Definition: util.cpp:866
std::pair< int64_t, int64_t > ParseDescriptorRange(const UniValue &value)
Parse a JSON range specified as int64, or [int64, int64].
Definition: util.cpp:812
const Type m_type
Definition: util.h:149
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:133
size_type size() const
Definition: prevector.h:282
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:311
UniValue JSONRPCTransactionError(TransactionError terr, const std::string &err_string)
Definition: util.cpp:301
No valid connection manager instance found.
Definition: protocol.h:64
size_t size() const
Definition: univalue.h:68
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:82
void PushSection(const Section &s)
Definition: util.cpp:329
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded Values (throws error if not hex).
Definition: util.cpp:89
const std::string m_name
Definition: util.h:359
RPCErrorCode
Bitcoin RPC error codes.
Definition: protocol.h:23
std::string ToString() const
Concatenate all sections with proper padding.
Definition: util.cpp:394
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:214
std::string ToString(bool oneline) const
Return the type string of the argument.
Definition: util.cpp:763
A pair of strings that can be aligned (through padding) with other Sections later on...
Definition: util.cpp:314
UniValue DescribeAddress(const CTxDestination &dest)
Definition: util.cpp:268
bool error(const char *fmt, const Args &... args)
Definition: system.h:52
const std::string EXAMPLE_ADDRESS[2]
Example bech32 addresses for the RPCExamples help documentation.
Definition: util.cpp:21
bool isArray() const
Definition: univalue.h:83
Wrapper for UniValue::VType, which includes typeAny: Used to denote don&#39;t care type.
Definition: util.h:45
CPubKey HexToPubKey(const std::string &hex_in)
Definition: util.cpp:145
Error parsing or validating structure in raw format.
Definition: protocol.h:45
CoinStatsHashType ParseHashType(const UniValue &param, const CoinStatsHashType default_type)
Definition: util.cpp:116
std::string ToStringObj(bool oneline) const
Return the type string of the argument when it is in an object (dict).
Definition: util.cpp:726
uint256 ParseHashO(const UniValue &o, std::string strKey)
Definition: util.cpp:98