17 #include <boost/algorithm/string/classification.hpp> 18 #include <boost/algorithm/string/split.hpp> 21 const std::string
EXAMPLE_ADDRESS[2] = {
"bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hufdl",
"bc1q02ad21edsxd23d32dfgqqsz4vv4nmtfzuklhy3"};
24 const std::list<UniValueType>& typesExpected,
29 if (params.
size() <= i)
33 if (!(fAllowNull && v.
isNull())) {
48 const std::map<std::string, UniValueType>& typesExpected,
52 for (
const auto& t : typesExpected) {
54 if (!fAllowNull && v.
isNull())
57 if (!(t.second.typeAny || v.
type() == t.second.type || (fAllowNull && v.
isNull()))) {
58 std::string err =
strprintf(
"Expected type %s for %s, got %s",
66 for (
const std::string& k : o.
getKeys())
68 if (typesExpected.count(k) == 0)
70 std::string err =
strprintf(
"Unexpected key %s", k);
91 std::string strHex(v.
get_str());
92 if (64 != strHex.length())
121 std::string hash_type_input = param.
get_str();
123 if (hash_type_input ==
"hash_serialized_2") {
125 }
else if (hash_type_input ==
"none") {
133 std::string
HelpExampleCli(
const std::string& methodname,
const std::string& args)
135 return "> bitcoin-cli " + methodname +
" " + args +
"\n";
138 std::string
HelpExampleRpc(
const std::string& methodname,
const std::string& args)
140 return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", " 141 "\"method\": \"" + methodname +
"\", \"params\": [" + args +
"]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
147 if (!
IsHex(hex_in)) {
169 if (!keystore.
GetPubKey(key, vchPubKey)) {
185 if ((
int)pubkeys.size() < required) {
188 if (pubkeys.size() > 16) {
199 for (
const CPubKey& pk : pubkeys) {
200 if (!pk.IsCompressed()) {
225 obj.
pushKV(
"isscript",
false);
226 obj.
pushKV(
"iswitness",
false);
233 obj.
pushKV(
"isscript",
true);
234 obj.
pushKV(
"iswitness",
false);
241 obj.
pushKV(
"isscript",
false);
242 obj.
pushKV(
"iswitness",
true);
243 obj.
pushKV(
"witness_version", 0);
251 obj.
pushKV(
"isscript",
true);
252 obj.
pushKV(
"iswitness",
true);
253 obj.
pushKV(
"witness_version", 0);
261 obj.
pushKV(
"iswitness",
true);
262 obj.
pushKV(
"witness_version", (
int)
id.version);
275 const int target{value.
get_int()};
276 const unsigned int unsigned_target{
static_cast<unsigned int>(target)};
277 if (target < 1 || unsigned_target > max_target) {
280 return unsigned_target;
304 if (err_string.length() > 0) {
316 Section(
const std::string& left,
const std::string& right)
317 : m_left{left}, m_right{right} {}
332 m_max_pad = std::max(m_max_pad, s.
m_left.size());
333 m_sections.push_back(s);
341 const auto indent = std::string(current_indent,
' ');
342 const auto indent_next = std::string(current_indent + 2,
' ');
354 if (arg.
m_type_str.size() != 0 && push_name) {
366 PushSection({indent + (push_name ?
"\"" + arg.
GetName() +
"\": " :
"") +
"{", right});
367 for (
const auto& arg_inner : arg.
m_inner) {
371 PushSection({indent_next +
"...",
""});
373 PushSection({indent +
"}" + (outer_type !=
OuterType::NONE ?
"," :
""),
""});
378 left += push_name ?
"\"" + arg.
GetName() +
"\": " :
"";
381 PushSection({left, right});
382 for (
const auto& arg_inner : arg.
m_inner) {
385 PushSection({indent_next +
"...",
""});
386 PushSection({indent +
"]" + (outer_type !=
OuterType::NONE ?
"," :
""),
""});
398 const size_t pad = m_max_pad + 4;
399 for (
const auto& s : m_sections) {
403 if (s.m_right.empty()) {
409 std::string left = s.m_left;
410 left.resize(pad,
' ');
416 size_t new_line_pos = s.m_right.find_first_of(
'\n');
418 right += s.m_right.substr(begin, new_line_pos - begin);
419 if (new_line_pos == std::string::npos) {
422 right +=
"\n" + std::string(pad,
' ');
423 begin = s.m_right.find_first_not_of(
' ', new_line_pos + 1);
424 if (begin == std::string::npos) {
427 new_line_pos = s.m_right.find_first_of(
'\n', begin + 1);
437 :
RPCHelpMan{std::move(name), std::move(description), std::move(args), std::move(results), std::move(examples),
nullptr} {}
440 :
m_name{std::move(name)},
441 m_fun{std::move(fun)},
447 std::set<std::string> named_args;
448 for (
const auto& arg :
m_args) {
449 std::vector<std::string> names;
450 boost::split(names, arg.m_names, boost::is_any_of(
"|"));
452 for (
const std::string&
name : names) {
462 if (r.m_cond.empty()) {
463 result +=
"\nResult:\n";
465 result +=
"\nResult (" + r.m_cond +
"):\n";
468 r.ToSections(sections);
481 size_t num_required_args = 0;
482 for (
size_t n =
m_args.size(); n > 0; --n) {
483 if (!
m_args.at(n - 1).IsOptional()) {
484 num_required_args = n;
488 return num_required_args <= num_args && num_args <=
m_args.size();
493 std::vector<std::string> ret;
494 for (
const auto& arg :
m_args) {
495 ret.emplace_back(arg.m_names);
506 bool was_optional{
false};
507 for (
const auto& arg :
m_args) {
508 if (arg.m_hidden)
break;
509 const bool optional = arg.IsOptional();
512 if (!was_optional) ret +=
"( ";
515 if (was_optional) ret +=
") ";
516 was_optional =
false;
518 ret += arg.ToString(
true);
520 if (was_optional) ret +=
" )";
528 for (
size_t i{0}; i < m_args.size(); ++i) {
529 const auto& arg = m_args.at(i);
530 if (arg.m_hidden)
break;
532 if (i == 0) ret +=
"\nArguments:\n";
535 sections.
m_sections.emplace_back(::
ToString(i + 1) +
". " + arg.GetFirstName(), arg.ToDescriptionString());
554 return m_names.substr(0, m_names.find(
"|"));
565 if (m_fallback.index() == 1) {
576 if (m_type_str.size() != 0) {
577 ret += m_type_str.at(1);
590 ret +=
"numeric or string";
594 ret +=
"numeric or array";
602 case Type::OBJ_USER_KEYS: {
603 ret +=
"json object";
612 if (m_fallback.index() == 1) {
613 ret +=
", optional, default=" + std::get<std::string>(m_fallback);
615 switch (std::get<RPCArg::Optional>(m_fallback)) {
638 const std::string indent(current_indent,
' ');
639 const std::string indent_next(current_indent + 2,
' ');
642 const std::string maybe_separator{outer_type !=
OuterType::NONE ?
"," :
""};
645 const std::string maybe_key{
647 "\"" + this->m_key_name +
"\" : " :
651 const auto Description = [&](
const std::string& type) {
652 return "(" + type + (this->m_optional ?
", optional" :
"") +
")" +
657 case Type::ELISION: {
663 sections.
PushSection({indent +
"null" + maybe_separator, Description(
"json null")});
667 sections.
PushSection({indent + maybe_key +
"\"str\"" + maybe_separator, Description(
"string")});
670 case Type::STR_AMOUNT: {
671 sections.
PushSection({indent + maybe_key +
"n" + maybe_separator, Description(
"numeric")});
674 case Type::STR_HEX: {
675 sections.
PushSection({indent + maybe_key +
"\"hex\"" + maybe_separator, Description(
"string")});
679 sections.
PushSection({indent + maybe_key +
"n" + maybe_separator, Description(
"numeric")});
682 case Type::NUM_TIME: {
683 sections.
PushSection({indent + maybe_key +
"xxx" + maybe_separator, Description(
"numeric")});
687 sections.
PushSection({indent + maybe_key +
"true|false" + maybe_separator, Description(
"boolean")});
690 case Type::ARR_FIXED:
692 sections.
PushSection({indent + maybe_key +
"[", Description(
"json array")});
693 for (
const auto& i : m_inner) {
697 if (m_type == Type::ARR && m_inner.back().m_type != Type::ELISION) {
703 sections.
PushSection({indent +
"]" + maybe_separator,
""});
708 sections.
PushSection({indent + maybe_key +
"{", Description(
"json object")});
709 for (
const auto& i : m_inner) {
713 if (m_type == Type::OBJ_DYN && m_inner.back().m_type != Type::ELISION) {
720 sections.
PushSection({indent +
"}" + maybe_separator,
""});
731 res += GetFirstName();
739 return res +
"\"str\"";
741 return res +
"\"hex\"";
745 return res +
"n or [n,n]";
747 return res +
"amount";
752 for (
const auto& i : m_inner) {
753 res += i.ToString(oneline) +
",";
757 case Type::OBJ_USER_KEYS:
766 if (oneline && !m_oneline_description.empty())
return m_oneline_description;
771 return "\"" + GetFirstName() +
"\"";
777 return GetFirstName();
780 case Type::OBJ_USER_KEYS: {
782 if (m_type == Type::OBJ) {
783 return "{" + res +
"}";
785 return "{" + res +
",...}";
790 for (
const auto& i : m_inner) {
791 res += i.ToString(oneline) +
",";
793 return "[" + res +
"...]";
820 if ((high >> 31) != 0) {
823 if (high >= low + 1000000) {
831 std::string desc_str;
832 std::pair<int64_t, int64_t> range = {0, 1000};
833 if (scanobject.
isStr()) {
834 desc_str = scanobject.
get_str();
840 if (!range_uni.
isNull()) {
848 auto desc =
Parse(desc_str, provider, error);
852 if (!desc->IsRange()) {
856 std::vector<CScript> ret;
857 for (
int i = range.first; i <= range.second; ++i) {
858 std::vector<CScript> scripts;
859 if (!desc->Expand(i, provider, scripts, provider)) {
862 std::move(scripts.begin(), scripts.end(), std::back_inserter(ret));
875 return servicesNames;
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
CPubKey AddrToPubKey(const FillableSigningProvider &keystore, const std::string &addr_in)
Aliases for backward compatibility.
CAmount AmountFromValue(const UniValue &value)
void RPCTypeCheck(const UniValue ¶ms, const std::list< UniValueType > &typesExpected, bool fAllowNull)
Type-check arguments; throws JSONRPCError if wrong type given.
ServiceFlags
nServices flags
std::vector< std::string > GetArgNames() const
std::string ToDescriptionString() const
Return the description string.
std::vector< unsigned char > ParseHexO(const UniValue &o, std::string strKey)
Keeps track of RPCArgs by transforming them into sections for the purpose of serializing everything t...
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
std::vector< unsigned char > ParseHexV(const UniValue &v, std::string strName)
const std::string UNIX_EPOCH_TIME
String used to describe UNIX epoch time in documentation, factored out to a constant for consistency...
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
void ToSections(Sections §ions, OuterType outer_type=OuterType::NONE, const int current_indent=0) const
Append the sections of the result.
bool MoneyRange(const CAmount &nValue)
#define CHECK_NONFATAL(condition)
Throw a NonFatalCheckError when the condition evaluates to false.
std::vector< unsigned char > ParseHex(const char *psz)
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
std::string ToDescriptionString() const
const std::string m_right
void RPCTypeCheckArgument(const UniValue &value, const UniValueType &typeExpected)
Type-check one argument; throws JSONRPCError if wrong type given.
const RPCExamples m_examples
const std::string & get_str() const
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
bilingual_str TransactionErrorString(const TransactionError err)
std::vector< std::string > serviceFlagsToStr(uint64_t flags)
Convert service flags (a bitmask of NODE_*) to human readable strings.
const RPCMethodImpl m_fun
int64_t get_int64() const
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
bool IsValidNumArgs(size_t num_args) const
If the supplied number of args is neither too small nor too high.
const std::vector< std::string > & getKeys() const
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
UniValue operator()(const WitnessUnknown &id) const
const std::string & getValStr() const
UniValue operator()(const WitnessV0ScriptHash &id) const
std::string ToString() const
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
const std::vector< RPCArg > m_inner
Only used for arrays or dicts.
Invalid, missing or duplicate parameter.
void RPCTypeCheckObj(const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull, bool fStrict)
const UniValue & find_value(const UniValue &obj, const std::string &name)
const std::string m_description
int64_t CAmount
Amount in satoshis (Can be negative)
std::unique_ptr< Descriptor > Parse(const std::string &descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum)
Parse a descriptor string.
Special type that is a STR with only hex chars.
std::string GetName() const
Return the name, throws when there are aliases.
const char * uvTypeName(UniValue::VType t)
UniValue operator()(const PKHash &keyID) const
UniValue JSONRPCError(int code, const std::string &message)
bool push_back(const UniValue &val)
UniValue operator()(const CNoDestination &dest) const
UniValue operator()(const ScriptHash &scriptID) const
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
CTxDestination subtype to encode any future Witness version.
unsigned int ParseConfirmTarget(const UniValue &value, unsigned int max_target)
Parse a confirm target option and raise an RPC error if it is invalid.
uint256 uint256S(const char *str)
An encapsulated public key.
Fillable signing provider that keeps keys in an address->secret map.
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
bool IsHex(const std::string &str)
Unexpected type was passed as parameter.
Special type where the user must set the keys e.g. to define multiple addresses; as opposed to e...
std::function< UniValue(const RPCHelpMan &, const JSONRPCRequest &)> RPCMethodImpl
UniValue operator()(const WitnessV0KeyHash &id) const
static std::pair< int64_t, int64_t > ParseRange(const UniValue &value)
bool pushKV(const std::string &key, const UniValue &val)
std::string GetFirstName() const
Return the first of all aliases.
const std::vector< RPCArg > m_args
CTxDestination AddAndGetMultisigDestination(const int required, const std::vector< CPubKey > &pubkeys, OutputType type, FillableSigningProvider &keystore, CScript &script_out)
CTxDestination AddAndGetDestinationForScript(FillableSigningProvider &keystore, const CScript &script, OutputType type)
Get a destination of the requested type (if possible) to the specified script.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Optional arg that is a named argument and has a default value of null.
const std::vector< std::string > m_type_str
Should be empty unless it is supposed to override the auto-generated type strings. Vector length is either 0 or 2, m_type_str.at(0) will override the type of the value in a key-value pair, m_type_str.at(1) will override the type in the argument description.
const RPCResults m_results
Special type that is a NUM or [NUM,NUM].
OuterType
Serializing JSON objects depends on the outer type.
RPCHelpMan(std::string name, std::string description, std::vector< RPCArg > args, RPCResults results, RPCExamples examples)
Optional argument with default value omitted because they are implicitly clear.
CTxDestination DecodeDestination(const std::string &str)
std::string ToDescriptionString() const
Return the description string, including the argument type and whether the argument is required...
std::vector< Section > m_sections
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 ...
Serialized script, used inside transaction inputs and outputs.
Section(const std::string &left, const std::string &right)
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.
A reference to a CKey: the Hash160 of its serialized public key.
Special type representing a floating point amount (can be either NUM or STR)
UniValue GetServicesNames(ServiceFlags services)
Returns, given services flags, a list of humanly readable (known) network services.
std::pair< int64_t, int64_t > ParseDescriptorRange(const UniValue &value)
Parse a JSON range specified as int64, or [int64, int64].
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
UniValue JSONRPCTransactionError(TransactionError terr, const std::string &err_string)
No valid connection manager instance found.
A Span is an object that can refer to a contiguous sequence of objects.
void PushSection(const Section &s)
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded Values (throws error if not hex).
RPCErrorCode
Bitcoin RPC error codes.
std::string ToString() const
Concatenate all sections with proper padding.
std::string ToString(bool oneline) const
Return the type string of the argument.
A pair of strings that can be aligned (through padding) with other Sections later on...
UniValue DescribeAddress(const CTxDestination &dest)
bool error(const char *fmt, const Args &... args)
const std::string EXAMPLE_ADDRESS[2]
Example bech32 addresses for the RPCExamples help documentation.
Wrapper for UniValue::VType, which includes typeAny: Used to denote don't care type.
CPubKey HexToPubKey(const std::string &hex_in)
Error parsing or validating structure in raw format.
CoinStatsHashType ParseHashType(const UniValue ¶m, const CoinStatsHashType default_type)
std::string ToStringObj(bool oneline) const
Return the type string of the argument when it is in an object (dict).
uint256 ParseHashO(const UniValue &o, std::string strKey)