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