Bitcoin Core  0.18.99
P2P Digital Currency
util.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2019 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 <keystore.h>
7 #include <rpc/util.h>
8 #include <tinyformat.h>
9 #include <util/strencodings.h>
10 
11 #include <tuple>
12 
14 
15 void RPCTypeCheck(const UniValue& params,
16  const std::list<UniValueType>& typesExpected,
17  bool fAllowNull)
18 {
19  unsigned int i = 0;
20  for (const UniValueType& t : typesExpected) {
21  if (params.size() <= i)
22  break;
23 
24  const UniValue& v = params[i];
25  if (!(fAllowNull && v.isNull())) {
27  }
28  i++;
29  }
30 }
31 
32 void RPCTypeCheckArgument(const UniValue& value, const UniValueType& typeExpected)
33 {
34  if (!typeExpected.typeAny && value.type() != typeExpected.type) {
35  throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Expected type %s, got %s", uvTypeName(typeExpected.type), uvTypeName(value.type())));
36  }
37 }
38 
39 void RPCTypeCheckObj(const UniValue& o,
40  const std::map<std::string, UniValueType>& typesExpected,
41  bool fAllowNull,
42  bool fStrict)
43 {
44  for (const auto& t : typesExpected) {
45  const UniValue& v = find_value(o, t.first);
46  if (!fAllowNull && v.isNull())
47  throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));
48 
49  if (!(t.second.typeAny || v.type() == t.second.type || (fAllowNull && v.isNull()))) {
50  std::string err = strprintf("Expected type %s for %s, got %s",
51  uvTypeName(t.second.type), t.first, uvTypeName(v.type()));
52  throw JSONRPCError(RPC_TYPE_ERROR, err);
53  }
54  }
55 
56  if (fStrict)
57  {
58  for (const std::string& k : o.getKeys())
59  {
60  if (typesExpected.count(k) == 0)
61  {
62  std::string err = strprintf("Unexpected key %s", k);
63  throw JSONRPCError(RPC_TYPE_ERROR, err);
64  }
65  }
66  }
67 }
68 
70 {
71  if (!value.isNum() && !value.isStr())
72  throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string");
73  CAmount amount;
74  if (!ParseFixedPoint(value.getValStr(), 8, &amount))
75  throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
76  if (!MoneyRange(amount))
77  throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range");
78  return amount;
79 }
80 
81 uint256 ParseHashV(const UniValue& v, std::string strName)
82 {
83  std::string strHex(v.get_str());
84  if (64 != strHex.length())
85  throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d, for '%s')", strName, 64, strHex.length(), strHex));
86  if (!IsHex(strHex)) // Note: IsHex("") is false
87  throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
88  return uint256S(strHex);
89 }
90 uint256 ParseHashO(const UniValue& o, std::string strKey)
91 {
92  return ParseHashV(find_value(o, strKey), strKey);
93 }
94 std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName)
95 {
96  std::string strHex;
97  if (v.isStr())
98  strHex = v.get_str();
99  if (!IsHex(strHex))
100  throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
101  return ParseHex(strHex);
102 }
103 std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey)
104 {
105  return ParseHexV(find_value(o, strKey), strKey);
106 }
107 
108 std::string HelpExampleCli(const std::string& methodname, const std::string& args)
109 {
110  return "> bitcoin-cli " + methodname + " " + args + "\n";
111 }
112 
113 std::string HelpExampleRpc(const std::string& methodname, const std::string& args)
114 {
115  return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\":\"curltest\", "
116  "\"method\": \"" + methodname + "\", \"params\": [" + args + "] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
117 }
118 
119 // Converts a hex string to a public key if possible
120 CPubKey HexToPubKey(const std::string& hex_in)
121 {
122  if (!IsHex(hex_in)) {
123  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in);
124  }
125  CPubKey vchPubKey(ParseHex(hex_in));
126  if (!vchPubKey.IsFullyValid()) {
127  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in);
128  }
129  return vchPubKey;
130 }
131 
132 // Retrieves a public key for an address from the given CKeyStore
133 CPubKey AddrToPubKey(CKeyStore* const keystore, const std::string& addr_in)
134 {
135  CTxDestination dest = DecodeDestination(addr_in);
136  if (!IsValidDestination(dest)) {
137  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address: " + addr_in);
138  }
139  CKeyID key = GetKeyForDestination(*keystore, dest);
140  if (key.IsNull()) {
141  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("%s does not refer to a key", addr_in));
142  }
143  CPubKey vchPubKey;
144  if (!keystore->GetPubKey(key, vchPubKey)) {
145  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("no full public key for address %s", addr_in));
146  }
147  if (!vchPubKey.IsFullyValid()) {
148  throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet contains an invalid public key");
149  }
150  return vchPubKey;
151 }
152 
153 // Creates a multisig redeemscript from a given list of public keys and number required.
154 CScript CreateMultisigRedeemscript(const int required, const std::vector<CPubKey>& pubkeys)
155 {
156  // Gather public keys
157  if (required < 1) {
158  throw JSONRPCError(RPC_INVALID_PARAMETER, "a multisignature address must require at least one key to redeem");
159  }
160  if ((int)pubkeys.size() < required) {
161  throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("not enough keys supplied (got %u keys, but need at least %d to redeem)", pubkeys.size(), required));
162  }
163  if (pubkeys.size() > 16) {
164  throw JSONRPCError(RPC_INVALID_PARAMETER, "Number of keys involved in the multisignature address creation > 16\nReduce the number");
165  }
166 
167  CScript result = GetScriptForMultisig(required, pubkeys);
168 
169  if (result.size() > MAX_SCRIPT_ELEMENT_SIZE) {
170  throw JSONRPCError(RPC_INVALID_PARAMETER, (strprintf("redeemScript exceeds size limit: %d > %d", result.size(), MAX_SCRIPT_ELEMENT_SIZE)));
171  }
172 
173  return result;
174 }
175 
176 class DescribeAddressVisitor : public boost::static_visitor<UniValue>
177 {
178 public:
180 
181  UniValue operator()(const CNoDestination& dest) const
182  {
183  return UniValue(UniValue::VOBJ);
184  }
185 
186  UniValue operator()(const PKHash& keyID) const
187  {
189  obj.pushKV("isscript", false);
190  obj.pushKV("iswitness", false);
191  return obj;
192  }
193 
194  UniValue operator()(const ScriptHash& scriptID) const
195  {
197  obj.pushKV("isscript", true);
198  obj.pushKV("iswitness", false);
199  return obj;
200  }
201 
203  {
205  obj.pushKV("isscript", false);
206  obj.pushKV("iswitness", true);
207  obj.pushKV("witness_version", 0);
208  obj.pushKV("witness_program", HexStr(id.begin(), id.end()));
209  return obj;
210  }
211 
213  {
215  obj.pushKV("isscript", true);
216  obj.pushKV("iswitness", true);
217  obj.pushKV("witness_version", 0);
218  obj.pushKV("witness_program", HexStr(id.begin(), id.end()));
219  return obj;
220  }
221 
223  {
225  obj.pushKV("iswitness", true);
226  obj.pushKV("witness_version", (int)id.version);
227  obj.pushKV("witness_program", HexStr(id.program, id.program + id.length));
228  return obj;
229  }
230 };
231 
233 {
234  return boost::apply_visitor(DescribeAddressVisitor(), dest);
235 }
236 
237 unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target)
238 {
239  int target = value.get_int();
240  if (target < 1 || (unsigned int)target > max_target) {
241  throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid conf_target, must be between %u - %u", 1, max_target));
242  }
243  return (unsigned int)target;
244 }
245 
247 {
248  switch (terr) {
257  return RPC_INVALID_PARAMETER;
260  default: break;
261  }
262  return RPC_TRANSACTION_ERROR;
263 }
264 
265 UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string)
266 {
267  if (err_string.length() > 0) {
268  return JSONRPCError(RPCErrorFromTransactionError(terr), err_string);
269  } else {
271  }
272 }
273 
278 struct Section {
279  Section(const std::string& left, const std::string& right)
280  : m_left{left}, m_right{right} {}
281  const std::string m_left;
282  const std::string m_right;
283 };
284 
289 struct Sections {
290  std::vector<Section> m_sections;
291  size_t m_max_pad{0};
292 
293  void PushSection(const Section& s)
294  {
295  m_max_pad = std::max(m_max_pad, s.m_left.size());
296  m_sections.push_back(s);
297  }
298 
304  enum class OuterType {
305  ARR,
306  OBJ,
307  NAMED_ARG, // Only set on first recursion
308  };
309 
313  void Push(const RPCArg& arg, const size_t current_indent = 5, const OuterType outer_type = OuterType::NAMED_ARG)
314  {
315  const auto indent = std::string(current_indent, ' ');
316  const auto indent_next = std::string(current_indent + 2, ' ');
317  const bool push_name{outer_type == OuterType::OBJ}; // Dictionary keys must have a name
318 
319  switch (arg.m_type) {
321  case RPCArg::Type::STR:
322  case RPCArg::Type::NUM:
324  case RPCArg::Type::RANGE:
325  case RPCArg::Type::BOOL: {
326  if (outer_type == OuterType::NAMED_ARG) return; // Nothing more to do for non-recursive types on first recursion
327  auto left = indent;
328  if (arg.m_type_str.size() != 0 && push_name) {
329  left += "\"" + arg.m_name + "\": " + arg.m_type_str.at(0);
330  } else {
331  left += push_name ? arg.ToStringObj(/* oneline */ false) : arg.ToString(/* oneline */ false);
332  }
333  left += ",";
334  PushSection({left, arg.ToDescriptionString()});
335  break;
336  }
337  case RPCArg::Type::OBJ:
339  const auto right = outer_type == OuterType::NAMED_ARG ? "" : arg.ToDescriptionString();
340  PushSection({indent + (push_name ? "\"" + arg.m_name + "\": " : "") + "{", right});
341  for (const auto& arg_inner : arg.m_inner) {
342  Push(arg_inner, current_indent + 2, OuterType::OBJ);
343  }
344  if (arg.m_type != RPCArg::Type::OBJ) {
345  PushSection({indent_next + "...", ""});
346  }
347  PushSection({indent + "}" + (outer_type != OuterType::NAMED_ARG ? "," : ""), ""});
348  break;
349  }
350  case RPCArg::Type::ARR: {
351  auto left = indent;
352  left += push_name ? "\"" + arg.m_name + "\": " : "";
353  left += "[";
354  const auto right = outer_type == OuterType::NAMED_ARG ? "" : arg.ToDescriptionString();
355  PushSection({left, right});
356  for (const auto& arg_inner : arg.m_inner) {
357  Push(arg_inner, current_indent + 2, OuterType::ARR);
358  }
359  PushSection({indent_next + "...", ""});
360  PushSection({indent + "]" + (outer_type != OuterType::NAMED_ARG ? "," : ""), ""});
361  break;
362  }
363 
364  // no default case, so the compiler can warn about missing cases
365  }
366  }
367 
371  std::string ToString() const
372  {
373  std::string ret;
374  const size_t pad = m_max_pad + 4;
375  for (const auto& s : m_sections) {
376  if (s.m_right.empty()) {
377  ret += s.m_left;
378  ret += "\n";
379  continue;
380  }
381 
382  std::string left = s.m_left;
383  left.resize(pad, ' ');
384  ret += left;
385 
386  // Properly pad after newlines
387  std::string right;
388  size_t begin = 0;
389  size_t new_line_pos = s.m_right.find_first_of('\n');
390  while (true) {
391  right += s.m_right.substr(begin, new_line_pos - begin);
392  if (new_line_pos == std::string::npos) {
393  break; //No new line
394  }
395  right += "\n" + std::string(pad, ' ');
396  begin = s.m_right.find_first_not_of(' ', new_line_pos + 1);
397  if (begin == std::string::npos) {
398  break; // Empty line
399  }
400  new_line_pos = s.m_right.find_first_of('\n', begin + 1);
401  }
402  ret += right;
403  ret += "\n";
404  }
405  return ret;
406  }
407 };
408 
409 RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples)
410  : m_name{std::move(name)},
411  m_description{std::move(description)},
412  m_args{std::move(args)},
413  m_results{std::move(results)},
414  m_examples{std::move(examples)}
415 {
416  std::set<std::string> named_args;
417  for (const auto& arg : m_args) {
418  // Should have unique named arguments
419  assert(named_args.insert(arg.m_name).second);
420  }
421 }
422 
424 {
425  std::string result;
426  for (const auto& r : m_results) {
427  if (r.m_cond.empty()) {
428  result += "\nResult:\n";
429  } else {
430  result += "\nResult (" + r.m_cond + "):\n";
431  }
432  result += r.m_result;
433  }
434  return result;
435 }
436 
438 {
439  return m_examples.empty() ? m_examples : "\nExamples:\n" + m_examples;
440 }
441 
442 bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
443 {
444  size_t num_required_args = 0;
445  for (size_t n = m_args.size(); n > 0; --n) {
446  if (!m_args.at(n - 1).IsOptional()) {
447  num_required_args = n;
448  break;
449  }
450  }
451  return num_required_args <= num_args && num_args <= m_args.size();
452 }
453 std::string RPCHelpMan::ToString() const
454 {
455  std::string ret;
456 
457  // Oneline summary
458  ret += m_name;
459  bool was_optional{false};
460  for (const auto& arg : m_args) {
461  const bool optional = arg.IsOptional();
462  ret += " ";
463  if (optional) {
464  if (!was_optional) ret += "( ";
465  was_optional = true;
466  } else {
467  if (was_optional) ret += ") ";
468  was_optional = false;
469  }
470  ret += arg.ToString(/* oneline */ true);
471  }
472  if (was_optional) ret += " )";
473  ret += "\n";
474 
475  // Description
476  ret += m_description;
477 
478  // Arguments
479  Sections sections;
480  for (size_t i{0}; i < m_args.size(); ++i) {
481  const auto& arg = m_args.at(i);
482 
483  if (i == 0) ret += "\nArguments:\n";
484 
485  // Push named argument name and description
486  sections.m_sections.emplace_back(std::to_string(i + 1) + ". " + arg.m_name, arg.ToDescriptionString());
487  sections.m_max_pad = std::max(sections.m_max_pad, sections.m_sections.back().m_left.size());
488 
489  // Recursively push nested args
490  sections.Push(arg);
491  }
492  ret += sections.ToString();
493 
494  // Result
496 
497  // Examples
499 
500  return ret;
501 }
502 
503 bool RPCArg::IsOptional() const
504 {
505  if (m_fallback.which() == 1) {
506  return true;
507  } else {
508  return RPCArg::Optional::NO != boost::get<RPCArg::Optional>(m_fallback);
509  }
510 }
511 
512 std::string RPCArg::ToDescriptionString() const
513 {
514  std::string ret;
515  ret += "(";
516  if (m_type_str.size() != 0) {
517  ret += m_type_str.at(1);
518  } else {
519  switch (m_type) {
520  case Type::STR_HEX:
521  case Type::STR: {
522  ret += "string";
523  break;
524  }
525  case Type::NUM: {
526  ret += "numeric";
527  break;
528  }
529  case Type::AMOUNT: {
530  ret += "numeric or string";
531  break;
532  }
533  case Type::RANGE: {
534  ret += "numeric or array";
535  break;
536  }
537  case Type::BOOL: {
538  ret += "boolean";
539  break;
540  }
541  case Type::OBJ:
542  case Type::OBJ_USER_KEYS: {
543  ret += "json object";
544  break;
545  }
546  case Type::ARR: {
547  ret += "json array";
548  break;
549  }
550 
551  // no default case, so the compiler can warn about missing cases
552  }
553  }
554  if (m_fallback.which() == 1) {
555  ret += ", optional, default=" + boost::get<std::string>(m_fallback);
556  } else {
557  switch (boost::get<RPCArg::Optional>(m_fallback)) {
559  // nothing to do. Element is treated as if not present and has no default value
560  break;
561  }
563  ret += ", optional"; // Default value is "null"
564  break;
565  }
566  case RPCArg::Optional::NO: {
567  ret += ", required";
568  break;
569  }
570 
571  // no default case, so the compiler can warn about missing cases
572  }
573  }
574  ret += ")";
575  ret += m_description.empty() ? "" : " " + m_description;
576  return ret;
577 }
578 
579 std::string RPCArg::ToStringObj(const bool oneline) const
580 {
581  std::string res;
582  res += "\"";
583  res += m_name;
584  if (oneline) {
585  res += "\":";
586  } else {
587  res += "\": ";
588  }
589  switch (m_type) {
590  case Type::STR:
591  return res + "\"str\"";
592  case Type::STR_HEX:
593  return res + "\"hex\"";
594  case Type::NUM:
595  return res + "n";
596  case Type::RANGE:
597  return res + "n or [n,n]";
598  case Type::AMOUNT:
599  return res + "amount";
600  case Type::BOOL:
601  return res + "bool";
602  case Type::ARR:
603  res += "[";
604  for (const auto& i : m_inner) {
605  res += i.ToString(oneline) + ",";
606  }
607  return res + "...]";
608  case Type::OBJ:
609  case Type::OBJ_USER_KEYS:
610  // Currently unused, so avoid writing dead code
611  assert(false);
612 
613  // no default case, so the compiler can warn about missing cases
614  }
615  assert(false);
616 }
617 
618 std::string RPCArg::ToString(const bool oneline) const
619 {
620  if (oneline && !m_oneline_description.empty()) return m_oneline_description;
621 
622  switch (m_type) {
623  case Type::STR_HEX:
624  case Type::STR: {
625  return "\"" + m_name + "\"";
626  }
627  case Type::NUM:
628  case Type::RANGE:
629  case Type::AMOUNT:
630  case Type::BOOL: {
631  return m_name;
632  }
633  case Type::OBJ:
634  case Type::OBJ_USER_KEYS: {
635  std::string res;
636  for (size_t i = 0; i < m_inner.size();) {
637  res += m_inner[i].ToStringObj(oneline);
638  if (++i < m_inner.size()) res += ",";
639  }
640  if (m_type == Type::OBJ) {
641  return "{" + res + "}";
642  } else {
643  return "{" + res + ",...}";
644  }
645  }
646  case Type::ARR: {
647  std::string res;
648  for (const auto& i : m_inner) {
649  res += i.ToString(oneline) + ",";
650  }
651  return "[" + res + "...]";
652  }
653 
654  // no default case, so the compiler can warn about missing cases
655  }
656  assert(false);
657 }
658 
659 static std::pair<int64_t, int64_t> ParseRange(const UniValue& value)
660 {
661  if (value.isNum()) {
662  return {0, value.get_int64()};
663  }
664  if (value.isArray() && value.size() == 2 && value[0].isNum() && value[1].isNum()) {
665  int64_t low = value[0].get_int64();
666  int64_t high = value[1].get_int64();
667  if (low > high) throw JSONRPCError(RPC_INVALID_PARAMETER, "Range specified as [begin,end] must not have begin after end");
668  return {low, high};
669  }
670  throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified as end or as [begin,end]");
671 }
672 
673 std::pair<int64_t, int64_t> ParseDescriptorRange(const UniValue& value)
674 {
675  int64_t low, high;
676  std::tie(low, high) = ParseRange(value);
677  if (low < 0) {
678  throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should be greater or equal than 0");
679  }
680  if ((high >> 31) != 0) {
681  throw JSONRPCError(RPC_INVALID_PARAMETER, "End of range is too high");
682  }
683  if (high >= low + 1000000) {
684  throw JSONRPCError(RPC_INVALID_PARAMETER, "Range is too large");
685  }
686  return {low, high};
687 }
Aliases for backward compatibility.
Definition: protocol.h:62
CAmount AmountFromValue(const UniValue &value)
Definition: util.cpp:69
void RPCTypeCheck(const UniValue &params, const std::list< UniValueType > &typesExpected, bool fAllowNull)
Type-check arguments; throws JSONRPCError if wrong type given.
Definition: util.cpp:15
OuterType
Serializing RPCArgs depends on the outer type.
Definition: util.cpp:304
CScript CreateMultisigRedeemscript(const int required, const std::vector< CPubKey > &pubkeys)
Definition: util.cpp:154
std::string ToDescriptionString() const
Return the description string.
Definition: util.cpp:423
std::string TransactionErrorString(const TransactionError err)
Definition: error.cpp:9
Required arg.
std::vector< unsigned char > ParseHexO(const UniValue &o, std::string strKey)
Definition: util.cpp:103
Keeps track of RPCArgs by transforming them into sections for the purpose of serializing everything t...
Definition: util.cpp:289
const std::string m_name
The name of the arg (can be empty for inner args)
Definition: util.h:116
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1067
bool typeAny
Definition: util.h:34
InitInterfaces * g_rpc_interfaces
Pointers to interfaces that need to be accessible from RPC methods.
Definition: util.cpp:13
std::vector< unsigned char > ParseHexV(const UniValue &v, std::string strName)
Definition: util.cpp:94
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
Definition: util.cpp:246
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:26
std::vector< unsigned char > ParseHex(const char *psz)
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:326
std::string ToDescriptionString() const
Definition: util.cpp:437
const std::string m_right
Definition: util.cpp:282
void RPCTypeCheckArgument(const UniValue &value, const UniValueType &typeExpected)
Type-check one argument; throws JSONRPCError if wrong type given.
Definition: util.cpp:32
CKeyID GetKeyForDestination(const CKeyStore &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
Definition: keystore.cpp:177
const RPCExamples m_examples
Definition: util.h:245
const std::string & get_str() const
bool isNum() const
Definition: univalue.h:83
bool isStr() const
Definition: univalue.h:82
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:442
const std::vector< std::string > & getKeys() const
UniValue operator()(const WitnessUnknown &id) const
Definition: util.cpp:222
const std::string & getValStr() const
Definition: univalue.h:66
UniValue operator()(const WitnessV0ScriptHash &id) const
Definition: util.cpp:212
std::string ToString() const
Definition: util.cpp:453
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:118
Invalid, missing or duplicate parameter.
Definition: protocol.h:52
void RPCTypeCheckObj(const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull, bool fStrict)
Definition: util.cpp:39
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:234
size_t m_max_pad
Definition: util.cpp:291
UniValue::VType type
Definition: util.h:35
const std::string m_description
Definition: util.h:242
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Special type that is a STR with only hex chars.
const char * uvTypeName(UniValue::VType t)
Definition: univalue.cpp:219
UniValue operator()(const PKHash &keyID) const
Definition: util.cpp:186
Definition: util.h:86
UniValue operator()(const CNoDestination &dest) const
Definition: util.cpp:181
UniValue operator()(const ScriptHash &scriptID) const
Definition: util.cpp:194
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
Definition: sign.h:54
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
Definition: pubkey.cpp:206
CTxDestination subtype to encode any future Witness version.
Definition: standard.h:108
const char * name
Definition: rest.cpp:38
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:237
bool IsOptional() const
Definition: util.cpp:503
uint256 uint256S(const char *str)
Definition: uint256.h:131
An encapsulated public key.
Definition: pubkey.h:30
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:113
bool IsHex(const std::string &str)
Unexpected type was passed as parameter.
Definition: protocol.h:49
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:202
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
const std::vector< RPCArg > m_args
Definition: util.h:243
int get_int() const
Invalid address or key.
Definition: protocol.h:50
bool isNull() const
Definition: univalue.h:78
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:122
const RPCResults m_results
Definition: util.h:244
Special type that is a NUM or [NUM,NUM].
RPCHelpMan(std::string name, std::string description, std::vector< RPCArg > args, RPCResults results, RPCExamples examples)
Definition: util.cpp:409
256-bit opaque blob.
Definition: uint256.h:121
Optional argument with default value omitted because they are implicitly clear.
CTxDestination DecodeDestination(const std::string &str)
Definition: key_io.cpp:216
enum VType type() const
Definition: univalue.h:178
std::string ToDescriptionString() const
Return the description string, including the argument type and whether the argument is required...
Definition: util.cpp:512
std::vector< Section > m_sections
Definition: util.cpp:290
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
Section(const std::string &left, const std::string &right)
Definition: util.cpp:279
const std::string m_left
Definition: util.cpp:281
A virtual base class for key stores.
Definition: keystore.h:19
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:125
TransactionError
util/error.h is a common place for definitions of simple error types and string functions.
Definition: error.h:20
std::pair< int64_t, int64_t > ParseDescriptorRange(const UniValue &value)
Parse a JSON range specified as int64, or [int64, int64].
Definition: util.cpp:673
const Type m_type
Definition: util.h:117
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:108
size_type size() const
Definition: prevector.h:277
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:303
UniValue JSONRPCTransactionError(TransactionError terr, const std::string &err_string)
Definition: util.cpp:265
UniValue JSONRPCError(int code, const std::string &message)
Definition: protocol.cpp:50
No valid connection manager instance found.
Definition: protocol.h:73
size_t size() const
Definition: univalue.h:69
void PushSection(const Section &s)
Definition: util.cpp:293
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded Values (throws error if not hex).
Definition: util.cpp:81
const std::string m_name
Definition: util.h:241
RPCErrorCode
Bitcoin RPC error codes.
Definition: protocol.h:32
void Push(const RPCArg &arg, const size_t current_indent=5, const OuterType outer_type=OuterType::NAMED_ARG)
Recursive helper to translate an RPCArg into sections.
Definition: util.cpp:313
std::string ToString() const
Concatenate all sections with proper padding.
Definition: util.cpp:371
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:139
std::string ToString(bool oneline) const
Return the type string of the argument.
Definition: util.cpp:618
Pointers to interfaces used during init and destroyed on shutdown.
Definition: init.h:19
A pair of strings that can be aligned (through padding) with other Sections later on...
Definition: util.cpp:278
UniValue DescribeAddress(const CTxDestination &dest)
Definition: util.cpp:232
CPubKey AddrToPubKey(CKeyStore *const keystore, const std::string &addr_in)
Definition: util.cpp:133
bool isArray() const
Definition: univalue.h:84
Wrapper for UniValue::VType, which includes typeAny: Used to denote don&#39;t care type.
Definition: util.h:31
CPubKey HexToPubKey(const std::string &hex_in)
Definition: util.cpp:120
Error parsing or validating structure in raw format.
Definition: protocol.h:54
std::string ToStringObj(bool oneline) const
Return the type string of the argument when it is in an object (dict).
Definition: util.cpp:579
uint256 ParseHashO(const UniValue &o, std::string strKey)
Definition: util.cpp:90