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