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