21 const std::string
EXAMPLE_ADDRESS[2] = {
"bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hufdl",
"bc1q02ad21edsxd23d32dfgqqsz4vv4nmtfzuklhy3"};
25 std::vector<std::string>
ret;
26 using U = std::underlying_type<TxoutType>::type;
34 const std::list<UniValueType>& typesExpected,
39 if (params.
size() <= i)
43 if (!(fAllowNull && v.
isNull())) {
59 const std::map<std::string, UniValueType>& typesExpected,
63 for (
const auto&
t : typesExpected) {
65 if (!fAllowNull && v.
isNull())
68 if (!(
t.second.typeAny || v.
type() ==
t.second.type || (fAllowNull && v.
isNull()))) {
69 std::string err =
strprintf(
"Expected type %s for %s, got %s",
77 for (
const std::string&
k : o.
getKeys())
79 if (typesExpected.count(
k) == 0)
81 std::string err =
strprintf(
"Unexpected key %s",
k);
102 const std::string& strHex(v.
get_str());
103 if (64 != strHex.length())
134 std::string ShellQuote(
const std::string& s)
137 result.reserve(s.size() * 2);
138 for (
const char ch: s) {
145 return "'" + result +
"'";
153 std::string ShellQuoteIfNeeded(
const std::string& s)
155 for (
const char ch: s) {
156 if (ch ==
' ' || ch ==
'\'' || ch ==
'"') {
157 return ShellQuote(s);
168 return "> bitcoin-cli " + methodname +
" " +
args +
"\n";
173 std::string result =
"> bitcoin-cli -named " + methodname;
174 for (
const auto& argpair:
args) {
175 const auto& value = argpair.second.isStr()
176 ? argpair.second.get_str()
177 : argpair.second.write();
178 result +=
" " + argpair.first +
"=" + ShellQuoteIfNeeded(value);
186 return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", " 187 "\"method\": \"" + methodname +
"\", \"params\": [" +
args +
"]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
193 for (
const auto& param:
args) {
194 params.
pushKV(param.first, param.second);
197 return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", " 198 "\"method\": \"" + methodname +
"\", \"params\": " + params.
write() +
"}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
204 if (!
IsHex(hex_in)) {
226 if (!keystore.
GetPubKey(key, vchPubKey)) {
242 if ((
int)pubkeys.size() < required) {
252 for (
const CPubKey& pk : pubkeys) {
253 if (!pk.IsCompressed()) {
282 obj.
pushKV(
"isscript",
false);
283 obj.
pushKV(
"iswitness",
false);
290 obj.
pushKV(
"isscript",
true);
291 obj.
pushKV(
"iswitness",
false);
298 obj.
pushKV(
"isscript",
false);
299 obj.
pushKV(
"iswitness",
true);
300 obj.
pushKV(
"witness_version", 0);
308 obj.
pushKV(
"isscript",
true);
309 obj.
pushKV(
"iswitness",
true);
310 obj.
pushKV(
"witness_version", 0);
318 obj.
pushKV(
"isscript",
true);
319 obj.
pushKV(
"iswitness",
true);
320 obj.
pushKV(
"witness_version", 1);
328 obj.
pushKV(
"iswitness",
true);
329 obj.
pushKV(
"witness_version", (
int)
id.version);
330 obj.
pushKV(
"witness_program",
HexStr({
id.program,
id.length}));
342 const int target{value.
getInt<
int>()};
343 const unsigned int unsigned_target{
static_cast<unsigned int>(target)};
344 if (target < 1 || unsigned_target > max_target) {
347 return unsigned_target;
371 if (err_string.length() > 0) {
383 Section(
const std::string& left,
const std::string& right)
408 const auto indent = std::string(current_indent,
' ');
409 const auto indent_next = std::string(current_indent + 2,
' ');
421 if (arg.
m_type_str.size() != 0 && push_name) {
433 PushSection({indent + (push_name ?
"\"" + arg.
GetName() +
"\": " :
"") +
"{", right});
434 for (
const auto& arg_inner : arg.
m_inner) {
445 left += push_name ?
"\"" + arg.
GetName() +
"\": " :
"";
449 for (
const auto& arg_inner : arg.
m_inner) {
470 if (s.m_right.empty()) {
476 std::string left = s.m_left;
477 left.resize(pad,
' ');
483 size_t new_line_pos = s.m_right.find_first_of(
'\n');
485 right += s.m_right.substr(begin, new_line_pos - begin);
486 if (new_line_pos == std::string::npos) {
489 right +=
"\n" + std::string(pad,
' ');
490 begin = s.m_right.find_first_not_of(
' ', new_line_pos + 1);
491 if (begin == std::string::npos) {
494 new_line_pos = s.m_right.find_first_of(
'\n', begin + 1);
504 :
RPCHelpMan{std::move(
name), std::move(description), std::move(
args), std::move(results), std::move(examples),
nullptr} {}
507 : m_name{std::move(
name)},
508 m_fun{std::move(fun)},
509 m_description{std::move(description)},
510 m_args{std::move(
args)},
511 m_results{std::move(results)},
512 m_examples{std::move(examples)}
514 std::set<std::string> named_args;
515 for (
const auto& arg : m_args) {
516 std::vector<std::string> names =
SplitString(arg.m_names,
'|');
518 for (
const std::string&
name : names) {
522 if (arg.m_fallback.index() == 2) {
524 switch (std::get<RPCArg::Default>(arg.m_fallback).getType()) {
556 if (r.m_cond.empty()) {
557 result +=
"\nResult:\n";
559 result +=
"\nResult (" + r.m_cond +
"):\n";
562 r.ToSections(sections);
583 throw std::runtime_error(
ToString());
594 size_t num_required_args = 0;
595 for (
size_t n =
m_args.size(); n > 0; --n) {
596 if (!
m_args.at(n - 1).IsOptional()) {
597 num_required_args = n;
601 return num_required_args <= num_args && num_args <=
m_args.size();
606 std::vector<std::string>
ret;
607 for (
const auto& arg :
m_args) {
608 ret.emplace_back(arg.m_names);
619 bool was_optional{
false};
620 for (
const auto& arg :
m_args) {
621 if (arg.m_hidden)
break;
622 const bool optional = arg.IsOptional();
625 if (!was_optional)
ret +=
"( ";
628 if (was_optional)
ret +=
") ";
629 was_optional =
false;
631 ret += arg.ToString(
true);
633 if (was_optional)
ret +=
" )";
640 for (
size_t i{0}; i <
m_args.size(); ++i) {
641 const auto& arg =
m_args.at(i);
642 if (arg.m_hidden)
break;
644 if (i == 0)
ret +=
"\nArguments:\n";
647 sections.
m_sections.emplace_back(::
ToString(i + 1) +
". " + arg.GetFirstName(), arg.ToDescriptionString());
667 for (
int i{0}; i < int(
m_args.size()); ++i) {
668 const auto& arg =
m_args.at(i);
669 std::vector<std::string> arg_names =
SplitString(arg.m_names,
'|');
670 for (
const auto& arg_name : arg_names) {
674 map.push_back(arg_name);
721 ret +=
"numeric or string";
725 ret +=
"numeric or array";
734 ret +=
"json object";
744 ret +=
", optional, default=" + std::get<RPCArg::DefaultHint>(
m_fallback);
746 ret +=
", optional, default=" + std::get<RPCArg::Default>(
m_fallback).write();
748 switch (std::get<RPCArg::Optional>(
m_fallback)) {
771 const std::string indent(current_indent,
' ');
772 const std::string indent_next(current_indent + 2,
' ');
775 const std::string maybe_separator{outer_type !=
OuterType::NONE ?
"," :
""};
778 const std::string maybe_key{
784 const auto Description = [&](
const std::string& type) {
785 return "(" + type + (this->
m_optional ?
", optional" :
"") +
")" +
799 sections.
PushSection({indent +
"null" + maybe_separator, Description(
"json null")});
803 sections.
PushSection({indent + maybe_key +
"\"str\"" + maybe_separator, Description(
"string")});
807 sections.
PushSection({indent + maybe_key +
"n" + maybe_separator, Description(
"numeric")});
811 sections.
PushSection({indent + maybe_key +
"\"hex\"" + maybe_separator, Description(
"string")});
815 sections.
PushSection({indent + maybe_key +
"n" + maybe_separator, Description(
"numeric")});
819 sections.
PushSection({indent + maybe_key +
"xxx" + maybe_separator, Description(
"numeric")});
823 sections.
PushSection({indent + maybe_key +
"true|false" + maybe_separator, Description(
"boolean")});
828 sections.
PushSection({indent + maybe_key +
"[", Description(
"json array")});
829 for (
const auto& i :
m_inner) {
839 sections.
PushSection({indent +
"]" + maybe_separator,
""});
845 sections.
PushSection({indent + maybe_key +
"{}", Description(
"empty JSON object")});
848 sections.
PushSection({indent + maybe_key +
"{", Description(
"json object")});
849 for (
const auto& i :
m_inner) {
859 sections.
PushSection({indent +
"}" + maybe_separator,
""});
897 if (!doc_inner.MatchesType(result.
get_array()[i]))
return false;
907 for (
size_t i{0}; i < result.
get_obj().
size(); ++i) {
908 if (!doc_inner.MatchesType(result.
get_obj()[i])) {
914 std::set<std::string> doc_keys;
915 for (
const auto& doc_entry :
m_inner) {
916 doc_keys.insert(doc_entry.m_key_name);
918 std::map<std::string, UniValue> result_obj;
920 for (
const auto& result_entry : result_obj) {
921 if (doc_keys.find(result_entry.first) == doc_keys.end()) {
926 for (
const auto& doc_entry :
m_inner) {
927 const auto result_it{result_obj.find(doc_entry.m_key_name)};
928 if (result_it == result_obj.end()) {
929 if (!doc_entry.m_optional) {
934 if (!doc_entry.MatchesType(result_it->second)) {
967 return res +
"\"str\"";
969 return res +
"\"hex\"";
973 return res +
"n or [n,n]";
975 return res +
"amount";
980 for (
const auto& i :
m_inner) {
981 res += i.ToString(oneline) +
",";
1011 return "{" + res +
"}";
1013 return "{" + res +
",...}";
1018 for (
const auto& i :
m_inner) {
1019 res += i.ToString(oneline) +
",";
1021 return "[" + res +
"...]";
1029 if (value.
isNum()) {
1030 return {0, value.
getInt<int64_t>()};
1033 int64_t low = value[0].
getInt<int64_t>();
1034 int64_t high = value[1].
getInt<int64_t>();
1048 if ((high >> 31) != 0) {
1051 if (high >= low + 1000000) {
1059 std::string desc_str;
1060 std::pair<int64_t, int64_t> range = {0, 1000};
1061 if (scanobject.
isStr()) {
1062 desc_str = scanobject.
get_str();
1063 }
else if (scanobject.
isObject()) {
1066 desc_str = desc_uni.
get_str();
1068 if (!range_uni.
isNull()) {
1076 auto desc =
Parse(desc_str, provider,
error);
1080 if (!desc->IsRange()) {
1084 std::vector<CScript>
ret;
1085 for (
int i = range.first; i <= range.second; ++i) {
1086 std::vector<CScript> scripts;
1087 if (!desc->Expand(i, provider, scripts, provider)) {
1090 std::move(scripts.begin(), scripts.end(), std::back_inserter(
ret));
1103 return servicesNames;
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 ...
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
Aliases for backward compatibility.
void push_back(UniValue val)
const std::vector< RPCResult > m_inner
Only used for arrays or dicts.
const Fallback m_fallback
std::string HelpExampleRpcNamed(const std::string &methodname, const RPCArgList &args)
ServiceFlags
nServices flags
std::vector< std::string > GetArgNames() const
void CheckInnerDoc() const
std::string ToDescriptionString() const
Return the description string.
void RPCTypeCheckObj(const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull, bool fStrict)
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
Keeps track of RPCArgs by transforming them into sections for the purpose of serializing everything t...
const std::string m_oneline_description
Should be empty unless it is supposed to override the auto-generated summary line.
static std::pair< int64_t, int64_t > ParseRange(const UniValue &value)
std::vector< unsigned char > ParseHexV(const UniValue &v, std::string strName)
UniValue GetServicesNames(ServiceFlags services)
Returns, given services flags, a list of humanly readable (known) network services.
static constexpr bool DEFAULT_RPC_DOC_CHECK
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)
Identity function.
bool IsHex(std::string_view str)
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
const std::string m_key_name
Only used for dicts.
std::string ToDescriptionString() const
const std::string m_right
CPubKey HexToPubKey(const std::string &hex_in)
UniValue DescribeAddress(const CTxDestination &dest)
const RPCExamples m_examples
const std::string & get_str() const
const std::string EXAMPLE_ADDRESS[2]
Example bech32 addresses for the RPCExamples help documentation.
enum VType getType() 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).
const UniValue & get_array() const
unsigned int ParseConfirmTarget(const UniValue &value, unsigned int max_target)
Parse a confirm target option and raise an RPC error if it is invalid.
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.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
const RPCMethodImpl m_fun
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
UniValue GetArgMap() const
Return the named args that need to be converted from string to another JSON type. ...
std::vector< std::string > SplitString(std::string_view str, char sep)
UniValue operator()(const WitnessUnknown &id) const
const std::string & getValStr() const
static const int MAX_PUBKEYS_PER_MULTISIG
UniValue operator()(const WitnessV0ScriptHash &id) const
std::string ToString() const
const bool m_skip_type_check
const std::vector< RPCArg > m_inner
Only used for arrays or dicts.
Invalid, missing or duplicate parameter.
const UniValue & find_value(const UniValue &obj, const std::string &name)
const std::string m_description
UniValue operator()(const WitnessV1Taproot &tap) const
std::pair< int64_t, int64_t > ParseDescriptorRange(const UniValue &value)
Parse a JSON range specified as int64, or [int64, int64].
int64_t CAmount
Amount in satoshis (Can be negative)
std::string GetAllOutputTypes()
Gets all existing output types formatted for RPC help sections.
CTxDestination AddAndGetMultisigDestination(const int required, const std::vector< CPubKey > &pubkeys, OutputType type, FillableSigningProvider &keystore, CScript &script_out)
void RPCTypeCheckArgument(const UniValue &value, const UniValueType &typeExpected)
Type-check one argument; throws JSONRPCError if wrong type given.
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.
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
const char * uvTypeName(UniValue::VType t)
std::vector< Byte > ParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
UniValue operator()(const PKHash &keyID) const
uint256 ParseHashO(const UniValue &o, std::string strKey)
UniValue JSONRPCError(int code, const std::string &message)
Special string with only hex chars.
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()) ...
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string GetTxnOutputType(TxoutType t)
Get the name of a TxoutType as a string.
DescribeAddressVisitor()=default
CTxDestination subtype to encode any future Witness version.
void getObjMap(std::map< std::string, UniValue > &kv) const
enum JSONRPCRequest::Mode mode
Special array that has a fixed number of entries.
uint256 uint256S(const char *str)
An encapsulated public key.
Fillable signing provider that keeps keys in an address->secret map.
const std::string m_names
The name of the arg (can be empty for inner args, can contain multiple aliases separated by | for nam...
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...
Special type to disable type checks (for testing only)
std::function< UniValue(const RPCHelpMan &, const JSONRPCRequest &)> RPCMethodImpl
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
UniValue operator()(const WitnessV0KeyHash &id) const
const std::vector< RPCResult > m_results
bool MatchesType(const UniValue &result) const
Check whether the result JSON type matches.
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
std::string GetFirstName() const
Return the first of all aliases.
const std::vector< RPCArg > m_args
std::string HelpExampleCliNamed(const std::string &methodname, const RPCArgList &args)
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
const std::string m_description
Special numeric to denote unix epoch time.
CTxDestination AddAndGetDestinationForScript(FillableSigningProvider &keystore, const CScript &script, OutputType type)
Get a destination of the requested type (if possible) to the specified script.
const std::string m_examples
Optional arg that is a named argument and has a default value of null.
bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
std::vector< std::pair< std::string, UniValue > > RPCArgList
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded Values (throws error if not hex).
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.
std::string ToDescriptionString() const
Return the description string, including the argument type and whether the argument is required...
std::vector< Section > m_sections
Special string to represent a floating point amount.
void pushKV(std::string key, UniValue val)
Serialized script, used inside transaction inputs and outputs.
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Section(const std::string &left, const std::string &right)
const std::string m_description
const UniValue & get_obj() const
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.
#define NONFATAL_UNREACHABLE()
NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code.
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
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)
Only for Witness versions not already defined above.
std::vector< unsigned char > ParseHexO(const UniValue &o, std::string strKey)
CAmount AmountFromValue(const UniValue &value, int decimals)
Validate and return a CAmount from a UniValue number or string.
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
No valid connection manager instance found.
void PushSection(const Section &s)
UniValue JSONRPCTransactionError(TransactionError terr, const std::string &err_string)
RPCErrorCode
Bitcoin RPC error codes.
Special dictionary with keys that are not literals.
std::string ToString() const
Concatenate all sections with proper padding.
UniValue HandleRequest(const JSONRPCRequest &request) const
std::string ToString(bool oneline) const
Return the type string of the argument.
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
A pair of strings that can be aligned (through padding) with other Sections later on...
std::string TrimString(std::string_view str, std::string_view pattern=" \\\)
bool error(const char *fmt, const Args &... args)
CPubKey AddrToPubKey(const FillableSigningProvider &keystore, const std::string &addr_in)
Wrapper for UniValue::VType, which includes typeAny: Used to denote don't care type.
Error parsing or validating structure in raw format.
const std::string UNIX_EPOCH_TIME
String used to describe UNIX epoch time in documentation, factored out to a constant for consistency...
Special type to denote elision (...)
void RPCTypeCheck(const UniValue ¶ms, const std::list< UniValueType > &typesExpected, bool fAllowNull)
Type-check arguments; throws JSONRPCError if wrong type given.
std::string ToStringObj(bool oneline) const
Return the type string of the argument when it is in an object (dict).