5#ifndef BITCOIN_RPC_UTIL_H
6#define BITCOIN_RPC_UTIL_H
24#include <initializer_list>
90 const std::map<std::string, UniValueType>& typesExpected,
91 bool fAllowNull =
false,
92 bool fStrict =
false);
128using RPCArgList = std::vector<std::pair<std::string, UniValue>>;
220 using Fallback = std::variant<Optional, DefaultHint, Default>;
233 std::string description,
248 std::string description,
249 std::vector<RPCArg> inner,
279 std::string
ToString(
bool oneline)
const;
323 std::string description,
324 std::vector<RPCResult> inner = {})
325 :
m_type{std::move(type)},
341 std::string description,
342 std::vector<RPCResult> inner = {})
343 :
RPCResult{std::move(cond), type, std::move(
m_key_name),
false, std::move(description), std::move(inner)} {}
349 std::string description,
350 std::vector<RPCResult> inner = {},
351 bool skip_type_check =
false)
352 :
m_type{std::move(type)},
366 std::string description,
367 std::vector<RPCResult> inner = {},
368 bool skip_type_check =
false)
369 :
RPCResult{type, std::move(
m_key_name),
false, std::move(description), std::move(inner), skip_type_check} {}
402 std::string ToDescriptionString()
const;
408 std::string examples)
440 template <
typename R>
441 auto Arg(std::string_view key)
const
445 if constexpr (std::is_integral_v<R> || std::is_floating_point_v<R>) {
447 return ArgValue<R>(i);
450 return ArgValue<const R&>(i);
472 template <
typename R>
477 if constexpr (std::is_integral_v<R> || std::is_floating_point_v<R>) {
479 return ArgValue<std::optional<R>>(i);
482 return ArgValue<const R*>(i);
491 std::vector<std::pair<std::string, bool>>
GetArgNames()
const;
502 template <
typename R>
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
int64_t CAmount
Amount in satoshis (Can be negative)
#define CHECK_NONFATAL(condition)
Identity function.
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
An encapsulated public key.
Serialized script, used inside transaction inputs and outputs.
Fillable signing provider that keeps keys in an address->secret map.
std::function< UniValue(const RPCHelpMan &, const JSONRPCRequest &)> RPCMethodImpl
const RPCExamples m_examples
size_t GetParamIndex(std::string_view key) const
Return positional index of a parameter using its name as key.
RPCHelpMan(std::string name, std::string description, std::vector< RPCArg > args, RPCResults results, RPCExamples examples)
const std::string m_description
R ArgValue(size_t i) const
bool IsValidNumArgs(size_t num_args) const
If the supplied number of args is neither too small nor too high.
const RPCMethodImpl m_fun
const RPCResults m_results
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.
auto Arg(std::string_view key) const
Helper to get a required or default-valued request argument.
std::vector< std::pair< std::string, bool > > GetArgNames() const
Return list of arguments and whether they are named-only.
auto MaybeArg(std::string_view key) const
Helper to get an optional request argument.
const JSONRPCRequest * m_req
UniValue HandleRequest(const JSONRPCRequest &request) const
char const * json() noexcept
Template to generate JSON data.
ServiceFlags
nServices flags
RPCErrorCode
Bitcoin RPC error codes.
std::vector< CScript > EvalDescriptorStringOrObject(const UniValue &scanobject, FlatSigningProvider &provider, const bool expand_priv=false)
Evaluate a descriptor given as a string, or as a {"desc":...,"range":...} object, with default range ...
RPCErrorCode RPCErrorFromTransactionError(node::TransactionError terr)
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)
std::vector< std::pair< std::string, UniValue > > RPCArgList
std::string HelpExampleRpcNamed(const std::string &methodname, const RPCArgList &args)
std::vector< unsigned char > ParseHexV(const UniValue &v, std::string_view name)
static constexpr bool DEFAULT_RPC_DOC_CHECK
void PushWarnings(const UniValue &warnings, UniValue &obj)
Push warning messages to an RPC "warnings" field as a JSON array of strings.
int ParseSighashString(const UniValue &sighash)
Parse a sighash string representation and raise an RPC error if it is invalid.
std::vector< unsigned char > ParseHexO(const UniValue &o, std::string_view strKey)
CFeeRate ParseFeeRate(const UniValue &json)
Parse a json number or string, denoting BTC/kvB, into a CFeeRate (sat/kvB).
OuterType
Serializing JSON objects depends on the outer type.
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
CAmount AmountFromValue(const UniValue &value, int decimals=8)
Validate and return a CAmount from a UniValue number or string.
const std::string UNIX_EPOCH_TIME
String used to describe UNIX epoch time in documentation, factored out to a constant for consistency.
void RPCTypeCheckObj(const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull=false, bool fStrict=false)
std::string GetAllOutputTypes()
Gets all existing output types formatted for RPC help sections.
UniValue JSONRPCTransactionError(node::TransactionError terr, const std::string &err_string="")
int ParseVerbosity(const UniValue &arg, int default_verbosity, bool allow_bool)
Parses verbosity from provided UniValue.
UniValue JSONRPCPSBTError(common::PSBTError err)
CPubKey HexToPubKey(const std::string &hex_in)
const std::string EXAMPLE_ADDRESS[2]
Example bech32 addresses for the RPCExamples help documentation.
CTxDestination AddAndGetMultisigDestination(const int required, const std::vector< CPubKey > &pubkeys, OutputType type, FlatSigningProvider &keystore, CScript &script_out)
uint256 ParseHashO(const UniValue &o, std::string_view strKey)
unsigned int ParseConfirmTarget(const UniValue &value, unsigned int max_target)
Parse a confirm target option and raise an RPC error if it is invalid.
std::string HelpExampleCliNamed(const std::string &methodname, const RPCArgList &args)
uint256 ParseHashV(const UniValue &v, std::string_view name)
Utilities: convert hex-encoded Values (throws error if not hex).
CPubKey AddrToPubKey(const FillableSigningProvider &keystore, const std::string &addr_in)
UniValue DescribeAddress(const CTxDestination &dest)
std::vector< RPCResult > ScriptPubKeyDoc()
@ OBJ_USER_KEYS
Special type where the user must set the keys e.g. to define multiple addresses; as opposed to e....
@ 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.
RPCArg(std::string name, Type type, Fallback fallback, std::string description, RPCArgOptions opts={})
UniValue MatchesType(const UniValue &request) const
Check whether the request JSON type matches.
const std::string m_description
std::string DefaultHint
Hint for default value.
std::variant< Optional, DefaultHint, Default > Fallback
std::string ToDescriptionString(bool is_named_arg) const
Return the description string, including the argument type and whether the argument is required.
RPCArg(std::string name, Type type, Fallback fallback, std::string description, std::vector< RPCArg > inner, RPCArgOptions opts={})
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).
bool hidden
For testing only.
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.
bool also_positional
If set allows a named-parameter field in an OBJ_NAMED_PARAM options object to have the same name as a...
std::string ToDescriptionString() const
RPCExamples(std::string examples)
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.
const std::vector< RPCResult > m_inner
Only used for arrays or dicts.
RPCResult(Type type, std::string m_key_name, bool optional, std::string description, std::vector< RPCResult > inner={}, bool skip_type_check=false)
RPCResult(Type type, std::string m_key_name, std::string description, std::vector< RPCResult > inner={}, bool skip_type_check=false)
UniValue MatchesType(const UniValue &result) const
Check whether the result JSON type matches.
std::string ToDescriptionString() const
Return the description string, including the result type.
std::string ToStringObj() const
Return the type string of the result when it is in an object (dict).
void CheckInnerDoc() const
RPCResult(std::string cond, Type type, std::string m_key_name, std::string description, std::vector< RPCResult > inner={})
RPCResult(std::string cond, Type type, std::string m_key_name, bool optional, std::string description, std::vector< RPCResult > inner={})
const std::string m_key_name
Only used for dicts.
const bool m_skip_type_check
RPCResults(RPCResult result)
const std::vector< RPCResult > m_results
RPCResults(std::initializer_list< RPCResult > results)
Keeps track of RPCArgs by transforming them into sections for the purpose of serializing everything t...
Wrapper for UniValue::VType, which includes typeAny: Used to denote don't care type.
UniValueType(UniValue::VType _type)