24 "\"qrmzys48glkpevp2l4t24jtcltc9hyzx9cep2qffm4\"";
36 if (!(
t.second.typeAny || v.
type() ==
t.second.type ||
38 std::string
err =
strprintf(
"Expected type %s for %s, got %s",
46 for (
const std::string &k :
o.getKeys()) {
75 if (64 !=
strHex.length()) {
84 strName +
" must be hexadecimal string (not '" +
101 strName +
" must be hexadecimal string (not '" +
121 result.reserve(s.size() * 2);
122 for (
const char ch : s) {
129 return "'" + result +
"'";
139 for (
const char ch : s) {
140 if (
ch ==
' ' ||
ch ==
'\'' ||
ch ==
'"') {
151 const std::string &args) {
152 return "> bitcoin-cli " +
methodname +
" " + args +
"\n";
157 std::string result =
"> bitcoin-cli -named " +
methodname;
158 for (
const auto &
argpair : args) {
159 const auto &value =
argpair.second.isStr() ?
argpair.second.get_str()
168 const std::string &args) {
169 return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", "
170 "\"id\": \"curltest\", "
173 "]}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
179 for (
const auto &
param : args) {
183 return "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", "
184 "\"id\": \"curltest\", "
187 "}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n";
194 "Invalid public key: " +
hex_in);
199 "Invalid public key: " +
hex_in);
211 "Invalid address: " +
addr_in);
219 if (!
keystore.GetPubKey(key, vchPubKey)) {
226 "Wallet contains an invalid public key");
234 const std::vector<CPubKey> &pubkeys,
242 "a multisignature address must require at least one key to redeem");
244 if ((
int)pubkeys.size() <
required) {
246 strprintf(
"not enough keys supplied (got %u keys, "
247 "but need at least %d to redeem)",
250 if (pubkeys.size() > 16) {
252 "Number of keys involved in the multisignature "
253 "address creation > 16\nReduce the number");
261 (
strprintf(
"redeemScript exceeds size limit: %d > %d",
266 for (
const CPubKey &pk : pubkeys) {
267 if (!pk.IsCompressed()) {
290 obj.pushKV(
"isscript",
false);
296 obj.pushKV(
"isscript",
true);
306 std::vector<std::string>
ret;
307 using U = std::underlying_type<TxoutType>::type;
348 Section(
const std::string &left,
const std::string &right)
379 switch (
arg.m_type) {
393 left +=
"\"" +
arg.GetName() +
394 "\": " +
arg.m_opts.type_str.at(0);
397 :
arg.ToString(
false);
453 if (s.m_right.empty()) {
459 std::string left = s.m_left;
460 left.resize(pad,
' ');
473 right +=
"\n" + std::string(pad,
' ');
474 begin = s.m_right.find_first_not_of(
' ',
new_line_pos + 1);
475 if (begin == std::string::npos) {
478 new_line_pos = s.m_right.find_first_of(
'\n', begin + 1);
515 for (
const auto &inner :
arg.m_inner) {
521 inner.m_opts.also_positional);
530 if (
arg.m_fallback.index() == 2) {
532 switch (std::get<RPCArg::Default>(
arg.m_fallback).getType()) {
571 if (r.m_cond.empty()) {
572 result +=
"\nResult:\n";
574 result +=
"\nResult (" + r.m_cond +
"):\n";
598 throw std::runtime_error(
ToString());
601 for (
size_t i{0}; i <
m_args.size(); ++i) {
604 if (!
match.isTrue()) {
619 if (
match.isTrue()) {
630 "Internal bug detected: RPC call \"%s\" returned incorrect "
631 "type:\n%s\n%s %s\nPlease report this issue here: %s\n",
641 for (
size_t n =
m_args.size(); n > 0; --n) {
642 if (!
m_args.at(n - 1).IsOptional()) {
651 std::vector<std::pair<std::string, bool>>
ret;
655 for (
const auto &inner :
arg.m_inner) {
656 ret.emplace_back(inner.m_names,
true);
659 ret.emplace_back(
arg.m_names,
false);
671 if (
arg.m_opts.hidden) {
688 ret +=
arg.ToString(
true);
701 for (
size_t i{0}; i <
m_args.size(); ++i) {
703 if (
arg.m_opts.hidden) {
711 arg.ToDescriptionString(
true));
713 sections.m_sections.back().m_left.size());
730 ret +=
"\nArguments:\n";
734 ret +=
"\nNamed Arguments:\n";
762 for (
int i{0}; i <
int(
m_args.size()); ++i) {
768 for (
const auto &inner :
arg.m_inner) {
803 case Type::OBJ_NAMED_PARAMS:
804 case Type::OBJ_USER_KEYS: {
828 return strprintf(
"JSON value of type %s is not of expected type %s",
868 ret +=
"numeric or string";
872 ret +=
"numeric or array";
882 ret +=
"json object";
893 ", optional, default=" + std::get<RPCArg::DefaultHint>(
m_fallback);
895 ret +=
", optional, default=" +
896 std::get<RPCArg::Default>(
m_fallback).write();
898 switch (std::get<RPCArg::Optional>(
m_fallback)) {
917 ret +=
" Options object that can be used to pass named arguments, "
939 const auto Description = [&](
const std::string &type) {
940 return "(" + type + (this->
m_optional ?
", optional" :
"") +
")" +
997 for (
const auto &i :
m_inner) {
1005 sections.m_sections.back().m_left.pop_back();
1019 for (
const auto &i :
m_inner) {
1029 sections.m_sections.back().m_left.pop_back();
1043 return std::nullopt;
1049 case Type::STR_HEX: {
1053 case Type::STR_AMOUNT:
1054 case Type::NUM_TIME: {
1060 case Type::ARR_FIXED:
1084 return strprintf(
"returned type is %s, but declared as %s in doc",
1090 for (
size_t i{0}; i < result.
get_array().size(); ++i) {
1096 if (!
match.isTrue()) {
1115 for (
size_t i{0}; i < result.
get_obj().size(); ++i) {
1117 if (!
match.isTrue()) {
1136 "key returned that was not in doc");
1146 "key missing, despite not being optional in doc");
1151 if (!
match.isTrue()) {
1186 return res +
"\"str\"";
1188 return res +
"\"hex\"";
1192 return res +
"n or [n,n]";
1194 return res +
"amount";
1196 return res +
"bool";
1199 for (
const auto &i :
m_inner) {
1202 return res +
"...]";
1212 return res +
"unknown";
1238 return "{" +
res +
"}";
1240 return "{" +
res +
",...}";
1245 for (
const auto &i :
m_inner) {
1248 return "[" +
res +
"...]";
1255 if (value.
isNum()) {
1265 "Range specified as [begin,end] must not have begin after end");
1270 "Range must be specified as end or as [begin,end]");
1278 "Range should be greater or equal than 0");
1280 if ((
high >> 31) != 0) {
1293 std::pair<int64_t, int64_t>
range = {0, 1000};
1301 "Descriptor needs to be provided in scan object");
1311 "Scan object needs to be either a string or an object");
1319 if (!
desc->IsRange()) {
1323 std::vector<CScript>
ret;
1324 for (
int i =
range.first; i <=
range.second; ++i) {
1325 std::vector<CScript> scripts;
1326 if (!
desc->Expand(i, provider, scripts, provider)) {
1329 strprintf(
"Cannot derive script without private keys: '%s'",
1332 std::move(scripts.begin(), scripts.end(), std::back_inserter(
ret));
bool MoneyRange(const Amount nValue)
static constexpr Amount SATOSHI
#define CHECK_NONFATAL(condition)
Identity function.
#define NONFATAL_UNREACHABLE()
NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
A reference to a CKey: the Hash160 of its serialized public key.
An encapsulated public key.
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid())
Serialized script, used inside transaction inputs and outputs.
UniValue operator()(const CNoDestination &dest) const
UniValue operator()(const ScriptHash &scriptID) const
UniValue operator()(const PKHash &keyID) const
Fillable signing provider that keeps keys in an address->secret map.
enum JSONRPCRequest::Mode mode
const RPCExamples m_examples
RPCHelpMan(std::string name, std::string description, std::vector< RPCArg > args, RPCResults results, RPCExamples examples)
const std::string m_description
bool IsValidNumArgs(size_t num_args) const
If the supplied number of args is neither too small nor too high.
std::function< UniValue(const RPCHelpMan &, const Config &config, const JSONRPCRequest &)> RPCMethodImpl
const RPCMethodImpl m_fun
const RPCResults m_results
UniValue HandleRequest(const Config &config, const JSONRPCRequest &request) const
const std::vector< RPCArg > m_args
std::string ToString() const
UniValue GetArgMap() const
Return the named args that need to be converted from string to another JSON type.
std::vector< std::pair< std::string, bool > > GetArgNames() const
Return list of arguments and whether they are named-only.
void push_back(UniValue val)
const std::string & get_str() const
const UniValue & find_value(std::string_view key) const
enum VType getType() const
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
const std::string & getValStr() const
const UniValue & get_obj() const
const std::vector< std::string > & getKeys() const
const UniValue & get_array() const
void pushKV(std::string key, UniValue val)
void getObjMap(std::map< std::string, UniValue > &kv) const
static UniValue Parse(std::string_view raw)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
std::string FormatFullVersion()
bilingual_str TransactionErrorString(const TransactionError error)
CTxDestination DecodeDestination(const std::string &addr, const CChainParams ¶ms)
bool error(const char *fmt, const Args &...args)
Implement std::hash so RCUPtr can be used as a key for maps or sets.
CTxDestination AddAndGetDestinationForScript(FillableSigningProvider &keystore, const CScript &script, OutputType type)
Get a destination of the requested type (if possible) to the specified script.
std::vector< std::string > serviceFlagsToStr(const uint64_t flags)
Convert service flags (a bitmask of NODE_*) to human readable strings.
ServiceFlags
nServices flags.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
UniValue JSONRPCError(int code, const std::string &message)
RPCErrorCode
Bitcoin RPC error codes.
@ RPC_TRANSACTION_ALREADY_IN_CHAIN
@ RPC_TYPE_ERROR
Unexpected type was passed as parameter.
@ RPC_TRANSACTION_REJECTED
@ RPC_TRANSACTION_ERROR
Aliases for backward compatibility.
@ RPC_INVALID_PARAMETER
Invalid, missing or duplicate parameter.
@ RPC_DESERIALIZATION_ERROR
Error parsing or validating structure in raw format.
@ RPC_INVALID_ADDRESS_OR_KEY
Invalid address or key.
@ RPC_CLIENT_P2P_DISABLED
No valid connection manager instance found.
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)
UniValue GetServicesNames(ServiceFlags services)
Returns, given services flags, a list of humanly readable (known) network services.
CTxDestination AddAndGetMultisigDestination(const int required, const std::vector< CPubKey > &pubkeys, OutputType type, FillableSigningProvider &keystore, CScript &script_out)
std::string HelpExampleRpcNamed(const std::string &methodname, const RPCArgList &args)
UniValue JSONRPCTransactionError(TransactionError terr, const std::string &err_string)
Amount AmountFromValue(const UniValue &value)
std::vector< uint8_t > ParseHexV(const UniValue &v, std::string strName)
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
const std::string EXAMPLE_ADDRESS
Example CashAddr address used in multiple RPCExamples.
static std::optional< UniValue::VType > ExpectedType(RPCArg::Type type)
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
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 ...
const std::string UNIX_EPOCH_TIME
String used to describe UNIX epoch time in documentation, factored out to a constant for consistency.
std::string GetAllOutputTypes()
CPubKey HexToPubKey(const std::string &hex_in)
CPubKey AddrToPubKey(const CChainParams &chainparams, const FillableSigningProvider &keystore, const std::string &addr_in)
uint256 ParseHashO(const UniValue &o, std::string strKey)
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded values (throws error if not hex).
static std::pair< int64_t, int64_t > ParseRange(const UniValue &value)
void RPCTypeCheckObj(const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull, bool fStrict)
Check for expected keys/value types in an Object.
std::string HelpExampleCliNamed(const std::string &methodname, const RPCArgList &args)
UniValue DescribeAddress(const CTxDestination &dest)
std::vector< uint8_t > ParseHexO(const UniValue &o, std::string strKey)
std::vector< std::pair< std::string, UniValue > > RPCArgList
static constexpr bool DEFAULT_RPC_DOC_CHECK
OuterType
Serializing JSON objects depends on the outer type.
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
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.
std::string GetTxnOutputType(TxoutType t)
Get the name of a TxoutType as a string.
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
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< std::string > SplitString(std::string_view str, char sep)
static const Currency & get()
@ RANGE
Special type that is a NUM or [NUM,NUM].
@ OBJ_USER_KEYS
Special type where the user must set the keys e.g.
@ 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.
const RPCArgOptions m_opts
const std::string m_names
The name of the arg (can be empty for inner args, can contain multiple aliases separated by | for nam...
const Fallback m_fallback
std::string ToString(bool oneline) const
Return the type string of the argument.
UniValue MatchesType(const UniValue &request) const
Check whether the request JSON type matches.
const std::string m_description
std::string ToDescriptionString(bool is_named_arg) const
Return the description string, including the argument type and whether the argument is required.
std::string GetName() const
Return the name, throws when there are aliases.
std::string GetFirstName() const
Return the first of all aliases.
std::string ToStringObj(bool oneline) const
Return the type string of the argument when it is in an object (dict).
@ OMITTED
The arg is optional for one of two reasons:
std::vector< std::string > type_str
Should be empty unless it is supposed to override the auto-generated type strings.
std::string oneline_description
Should be empty unless it is supposed to override the auto-generated summary line.
std::string ToDescriptionString() const
const std::string m_examples
const std::string m_description
void ToSections(Sections §ions, OuterType outer_type=OuterType::NONE, const int current_indent=0) const
Append the sections of the result.
@ 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.
UniValue MatchesType(const UniValue &result) const
Check whether the result JSON type matches.
void CheckInnerDoc() const
const std::string m_key_name
Only used for dicts.
const bool m_skip_type_check
std::string ToDescriptionString() const
Return the description string.
const std::vector< RPCResult > m_results
A pair of strings that can be aligned (through padding) with other Sections later on.
Section(const std::string &left, const std::string &right)
const std::string m_right
Keeps track of RPCArgs by transforming them into sections for the purpose of serializing everything t...
void PushSection(const Section &s)
std::vector< Section > m_sections
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.
std::string ToString() const
Concatenate all sections with proper padding.
uint256 uint256S(const char *str)
uint256 from const char *.
const char * uvTypeName(UniValue::VType t)
template std::vector< std::byte > ParseHex(std::string_view)
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)
Returns true if each character in str is a hex character, and has an even number of hex digits.