5 #ifndef BITCOIN_RPC_UTIL_H
6 #define BITCOIN_RPC_UTIL_H
24 #include <initializer_list>
28 #include <string_view>
29 #include <type_traits>
85 const std::map<std::string, UniValueType>& typesExpected,
86 bool fAllowNull =
false,
87 bool fStrict =
false);
112 using RPCArgList = std::vector<std::pair<std::string, UniValue>>;
202 using Fallback = std::variant<Optional, DefaultHint, Default>;
215 std::string description,
230 std::string description,
231 std::vector<RPCArg> inner,
261 std::string
ToString(
bool oneline)
const;
304 std::string description,
305 std::vector<RPCResult> inner = {})
306 :
m_type{std::move(type)},
322 std::string description,
323 std::vector<RPCResult> inner = {})
324 :
RPCResult{std::move(cond), type, std::move(
m_key_name),
false, std::move(description), std::move(inner)} {}
330 std::string description,
331 std::vector<RPCResult> inner = {},
332 bool skip_type_check =
false)
333 :
m_type{std::move(type)},
347 std::string description,
348 std::vector<RPCResult> inner = {},
349 bool skip_type_check =
false)
350 :
RPCResult{type, std::move(
m_key_name),
false, std::move(description), std::move(inner), skip_type_check} {}
383 std::string ToDescriptionString()
const;
389 std::string examples)
418 template <
typename R>
422 if constexpr (std::is_integral_v<R> || std::is_floating_point_v<R>) {
424 return ArgValue<R>(i);
427 return ArgValue<const R&>(i);
430 template <
typename R>
434 if constexpr (std::is_integral_v<R> || std::is_floating_point_v<R>) {
436 return ArgValue<std::optional<R>>(i);
439 return ArgValue<const R*>(i);
448 std::vector<std::pair<std::string, bool>>
GetArgNames()
const;
459 template <
typename R>
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, 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
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
auto Arg(size_t i) const
Helper to get a request argument.
bool IsValidNumArgs(size_t num_args) const
If the supplied number of args is neither too small nor too high.
const RPCMethodImpl m_fun
auto MaybeArg(size_t i) const
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.
std::vector< std::pair< std::string, bool > > GetArgNames() const
Return list of arguments and whether they are named-only.
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::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
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="")
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.
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
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::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 ...
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
std::vector< unsigned char > ParseHexV(const UniValue &v, std::string_view name)
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.
CPubKey HexToPubKey(const std::string &hex_in)
const std::string EXAMPLE_ADDRESS[2]
Example bech32 addresses for the RPCExamples help documentation.
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)
@ 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)
@ 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).
@ OMITTED
Optional argument for which the default value is omitted from help text for one of two reasons:
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.
@ 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.
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)