Bitcoin Core 28.99.0
P2P Digital Currency
Classes | Macros | Typedefs | Functions | Variables
util.cpp File Reference
#include <bitcoin-build-config.h>
#include <clientversion.h>
#include <common/args.h>
#include <common/messages.h>
#include <common/types.h>
#include <consensus/amount.h>
#include <core_io.h>
#include <key_io.h>
#include <node/types.h>
#include <outputtype.h>
#include <rpc/util.h>
#include <script/descriptor.h>
#include <script/interpreter.h>
#include <script/signingprovider.h>
#include <script/solver.h>
#include <tinyformat.h>
#include <uint256.h>
#include <univalue.h>
#include <util/check.h>
#include <util/result.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/translation.h>
#include <algorithm>
#include <iterator>
#include <string_view>
#include <tuple>
#include <utility>
Include dependency graph for util.cpp:

Go to the source code of this file.

Classes

class  DescribeAddressVisitor
 
struct  Section
 A pair of strings that can be aligned (through padding) with other Sections later on. More...
 
struct  Sections
 Keeps track of RPCArgs by transforming them into sections for the purpose of serializing everything to a single string. More...
 

Macros

#define TMPL_INST(check_param, ret_type, return_code)
 

Typedefs

using CheckFn = void(const RPCArg &)
 

Functions

std::string GetAllOutputTypes ()
 Gets all existing output types formatted for RPC help sections. More...
 
void RPCTypeCheckObj (const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull, bool fStrict)
 
int ParseVerbosity (const UniValue &arg, int default_verbosity, bool allow_bool)
 Parses verbosity from provided UniValue. More...
 
CAmount AmountFromValue (const UniValue &value, int decimals)
 Validate and return a CAmount from a UniValue number or string. More...
 
CFeeRate ParseFeeRate (const UniValue &json)
 Parse a json number or string, denoting BTC/kvB, into a CFeeRate (sat/kvB). More...
 
uint256 ParseHashV (const UniValue &v, std::string_view name)
 Utilities: convert hex-encoded Values (throws error if not hex). More...
 
uint256 ParseHashO (const UniValue &o, std::string_view strKey)
 
std::vector< unsigned char > ParseHexV (const UniValue &v, std::string_view name)
 
std::vector< unsigned char > ParseHexO (const UniValue &o, std::string_view strKey)
 
std::string HelpExampleCli (const std::string &methodname, const std::string &args)
 
std::string HelpExampleCliNamed (const std::string &methodname, const RPCArgList &args)
 
std::string HelpExampleRpc (const std::string &methodname, const std::string &args)
 
std::string HelpExampleRpcNamed (const std::string &methodname, const RPCArgList &args)
 
CPubKey HexToPubKey (const std::string &hex_in)
 
CPubKey AddrToPubKey (const FillableSigningProvider &keystore, const std::string &addr_in)
 
CTxDestination AddAndGetMultisigDestination (const int required, const std::vector< CPubKey > &pubkeys, OutputType type, FlatSigningProvider &keystore, CScript &script_out)
 
UniValue DescribeAddress (const CTxDestination &dest)
 
int ParseSighashString (const UniValue &sighash)
 Returns a sighash value corresponding to the passed in argument. More...
 
unsigned int ParseConfirmTarget (const UniValue &value, unsigned int max_target)
 Parse a confirm target option and raise an RPC error if it is invalid. More...
 
RPCErrorCode RPCErrorFromPSBTError (PSBTError err)
 
RPCErrorCode RPCErrorFromTransactionError (TransactionError terr)
 
UniValue JSONRPCPSBTError (PSBTError err)
 
UniValue JSONRPCTransactionError (TransactionError terr, const std::string &err_string)
 
static const UniValueDetailMaybeArg (CheckFn *check, const std::vector< RPCArg > &params, const JSONRPCRequest *req, size_t i)
 
static void CheckRequiredOrDefault (const RPCArg &param)
 
 TMPL_INST (nullptr, const UniValue *, maybe_arg;)
 
 TMPL_INST (nullptr, std::optional< double >, maybe_arg ? std::optional{maybe_arg->get_real()} :std::nullopt;)
 
 TMPL_INST (nullptr, std::optional< bool >, maybe_arg ? std::optional{maybe_arg->get_bool()} :std::nullopt;)
 
 TMPL_INST (nullptr, const std::string *, maybe_arg ? &maybe_arg->get_str() :nullptr;)
 
 TMPL_INST (CheckRequiredOrDefault, const UniValue &, *CHECK_NONFATAL(maybe_arg);)
 
 TMPL_INST (CheckRequiredOrDefault, bool, CHECK_NONFATAL(maybe_arg) ->get_bool();)
 
 TMPL_INST (CheckRequiredOrDefault, int, CHECK_NONFATAL(maybe_arg) ->getInt< int >();)
 
 TMPL_INST (CheckRequiredOrDefault, uint64_t, CHECK_NONFATAL(maybe_arg) ->getInt< uint64_t >();)
 
 TMPL_INST (CheckRequiredOrDefault, const std::string &, CHECK_NONFATAL(maybe_arg) ->get_str();)
 
static std::optional< UniValue::VTypeExpectedType (RPCArg::Type type)
 
static std::optional< UniValue::VTypeExpectedType (RPCResult::Type type)
 
static std::pair< int64_t, int64_t > ParseRange (const UniValue &value)
 
std::pair< int64_t, int64_t > ParseDescriptorRange (const UniValue &value)
 Parse a JSON range specified as int64, or [int64, int64]. More...
 
std::vector< CScriptEvalDescriptorStringOrObject (const UniValue &scanobject, FlatSigningProvider &provider, const bool expand_priv)
 Evaluate a descriptor given as a string, or as a {"desc":...,"range":...} object, with default range of 1000. More...
 
static UniValue BilingualStringsToUniValue (const std::vector< bilingual_str > &bilingual_strings)
 Convert a vector of bilingual strings to a UniValue::VARR containing their original untranslated values. More...
 
void PushWarnings (const UniValue &warnings, UniValue &obj)
 Push warning messages to an RPC "warnings" field as a JSON array of strings. More...
 
void PushWarnings (const std::vector< bilingual_str > &warnings, UniValue &obj)
 
std::vector< RPCResultScriptPubKeyDoc ()
 

Variables

const std::string UNIX_EPOCH_TIME = "UNIX epoch time"
 String used to describe UNIX epoch time in documentation, factored out to a constant for consistency. More...
 
const std::string EXAMPLE_ADDRESS [2] = {"bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hufdl", "bc1q02ad21edsxd23d32dfgqqsz4vv4nmtfzuklhy3"}
 Example bech32 addresses for the RPCExamples help documentation. More...
 

Macro Definition Documentation

◆ TMPL_INST

#define TMPL_INST (   check_param,
  ret_type,
  return_code 
)
Value:
template <> \
ret_type RPCHelpMan::ArgValue<ret_type>(size_t i) const \
{ \
const UniValue* maybe_arg{ \
DetailMaybeArg(check_param, m_args, m_req, i), \
}; \
return return_code \
} \
void force_semicolon(ret_type)

Definition at line 731 of file util.cpp.

Typedef Documentation

◆ CheckFn

using CheckFn = void(const RPCArg&)

Definition at line 709 of file util.cpp.

Function Documentation

◆ AddAndGetMultisigDestination()

CTxDestination AddAndGetMultisigDestination ( const int  required,
const std::vector< CPubKey > &  pubkeys,
OutputType  type,
FlatSigningProvider keystore,
CScript script_out 
)

Definition at line 257 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddrToPubKey()

CPubKey AddrToPubKey ( const FillableSigningProvider keystore,
const std::string &  addr_in 
)

Definition at line 236 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AmountFromValue()

CAmount AmountFromValue ( const UniValue value,
int  decimals = 8 
)

Validate and return a CAmount from a UniValue number or string.

Parameters
[in]valueUniValue number or string to parse.
[in]decimalsNumber of significant digits (default: 8).
Returns
a CAmount if the various checks pass.

Definition at line 99 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ BilingualStringsToUniValue()

static UniValue BilingualStringsToUniValue ( const std::vector< bilingual_str > &  bilingual_strings)
static

Convert a vector of bilingual strings to a UniValue::VARR containing their original untranslated values.

Definition at line 1389 of file util.cpp.

Here is the caller graph for this function:

◆ CheckRequiredOrDefault()

static void CheckRequiredOrDefault ( const RPCArg param)
static

Definition at line 722 of file util.cpp.

◆ DescribeAddress()

UniValue DescribeAddress ( const CTxDestination dest)

Definition at line 369 of file util.cpp.

Here is the caller graph for this function:

◆ DetailMaybeArg()

static const UniValue * DetailMaybeArg ( CheckFn check,
const std::vector< RPCArg > &  params,
const JSONRPCRequest req,
size_t  i 
)
static

Definition at line 710 of file util.cpp.

◆ EvalDescriptorStringOrObject()

std::vector< CScript > EvalDescriptorStringOrObject ( const UniValue scanobject,
FlatSigningProvider provider,
const bool  expand_priv 
)

Evaluate a descriptor given as a string, or as a {"desc":...,"range":...} object, with default range of 1000.

Definition at line 1345 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ExpectedType() [1/2]

static std::optional< UniValue::VType > ExpectedType ( RPCArg::Type  type)
static

Definition at line 886 of file util.cpp.

Here is the caller graph for this function:

◆ ExpectedType() [2/2]

static std::optional< UniValue::VType > ExpectedType ( RPCResult::Type  type)
static

Definition at line 1116 of file util.cpp.

◆ GetAllOutputTypes()

std::string GetAllOutputTypes ( )

Gets all existing output types formatted for RPC help sections.

Returns
Comma separated string representing output type names.

Definition at line 47 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ HelpExampleCli()

std::string HelpExampleCli ( const std::string &  methodname,
const std::string &  args 
)

Definition at line 184 of file util.cpp.

◆ HelpExampleCliNamed()

std::string HelpExampleCliNamed ( const std::string &  methodname,
const RPCArgList args 
)

Definition at line 189 of file util.cpp.

Here is the caller graph for this function:

◆ HelpExampleRpc()

std::string HelpExampleRpc ( const std::string &  methodname,
const std::string &  args 
)

Definition at line 202 of file util.cpp.

◆ HelpExampleRpcNamed()

std::string HelpExampleRpcNamed ( const std::string &  methodname,
const RPCArgList args 
)

Definition at line 208 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ HexToPubKey()

CPubKey HexToPubKey ( const std::string &  hex_in)

Definition at line 220 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ JSONRPCPSBTError()

UniValue JSONRPCPSBTError ( PSBTError  err)

Definition at line 425 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ JSONRPCTransactionError()

UniValue JSONRPCTransactionError ( TransactionError  terr,
const std::string &  err_string 
)

Definition at line 430 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseConfirmTarget()

unsigned int ParseConfirmTarget ( const UniValue value,
unsigned int  max_target 
)

Parse a confirm target option and raise an RPC error if it is invalid.

Definition at line 391 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseDescriptorRange()

std::pair< int64_t, int64_t > ParseDescriptorRange ( const UniValue value)

Parse a JSON range specified as int64, or [int64, int64].

Definition at line 1329 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseFeeRate()

CFeeRate ParseFeeRate ( const UniValue json)

Parse a json number or string, denoting BTC/kvB, into a CFeeRate (sat/kvB).

Reject negative values or rates larger than 1BTC/kvB.

Definition at line 111 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseHashO()

uint256 ParseHashO ( const UniValue o,
std::string_view  strKey 
)

Definition at line 127 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseHashV()

uint256 ParseHashV ( const UniValue v,
std::string_view  name 
)

Utilities: convert hex-encoded Values (throws error if not hex).

Definition at line 118 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseHexO()

std::vector< unsigned char > ParseHexO ( const UniValue o,
std::string_view  strKey 
)

Definition at line 140 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseHexV()

std::vector< unsigned char > ParseHexV ( const UniValue v,
std::string_view  name 
)

Definition at line 131 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseRange()

static std::pair< int64_t, int64_t > ParseRange ( const UniValue value)
static

Definition at line 1315 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseSighashString()

int ParseSighashString ( const UniValue sighash)

Returns a sighash value corresponding to the passed in argument.

Parse a sighash string representation and raise an RPC error if it is invalid.

Precondition
The sighash argument should be string or null.

Definition at line 379 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseVerbosity()

int ParseVerbosity ( const UniValue arg,
int  default_verbosity,
bool  allow_bool 
)

Parses verbosity from provided UniValue.

Parameters
[in]argThe verbosity argument as an int (0, 1, 2,...) or bool if allow_bool is set to true
[in]default_verbosityThe value to return if verbosity argument is null
[in]allow_boolIf true, allows arg to be a bool and parses it
Returns
An integer describing the verbosity level (e.g. 0, 1, 2, etc.)
Exceptions
JSONRPCErrorif allow_bool is false but arg provided is boolean

Definition at line 84 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PushWarnings() [1/2]

void PushWarnings ( const std::vector< bilingual_str > &  warnings,
UniValue obj 
)

Definition at line 1405 of file util.cpp.

Here is the call graph for this function:

◆ PushWarnings() [2/2]

void PushWarnings ( const UniValue warnings,
UniValue obj 
)

Push warning messages to an RPC "warnings" field as a JSON array of strings.

Parameters
[in]warningsWarning messages to push.
[out]objUniValue object to push the warnings array object to.

Definition at line 1399 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RPCErrorFromPSBTError()

RPCErrorCode RPCErrorFromPSBTError ( PSBTError  err)

Definition at line 401 of file util.cpp.

Here is the caller graph for this function:

◆ RPCErrorFromTransactionError()

RPCErrorCode RPCErrorFromTransactionError ( TransactionError  terr)

Definition at line 413 of file util.cpp.

Here is the caller graph for this function:

◆ RPCTypeCheckObj()

void RPCTypeCheckObj ( const UniValue o,
const std::map< std::string, UniValueType > &  typesExpected,
bool  fAllowNull,
bool  fStrict 
)

Definition at line 57 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ScriptPubKeyDoc()

std::vector< RPCResult > ScriptPubKeyDoc ( )

Definition at line 1411 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TMPL_INST() [1/9]

TMPL_INST ( CheckRequiredOrDefault  ,
bool  ,
CHECK_NONFATAL(maybe_arg) ->get_bool();   
)

◆ TMPL_INST() [2/9]

TMPL_INST ( CheckRequiredOrDefault  ,
const std::string &  ,
CHECK_NONFATAL(maybe_arg) ->get_str();   
)

◆ TMPL_INST() [3/9]

TMPL_INST ( CheckRequiredOrDefault  ,
const UniValue ,
*CHECK_NONFATAL(maybe_arg);   
)

◆ TMPL_INST() [4/9]

TMPL_INST ( CheckRequiredOrDefault  ,
int  ,
CHECK_NONFATAL(maybe_arg) ->getInt< int >();   
)

◆ TMPL_INST() [5/9]

TMPL_INST ( CheckRequiredOrDefault  ,
uint64_t  ,
CHECK_NONFATAL(maybe_arg) ->getInt< uint64_t >();   
)

◆ TMPL_INST() [6/9]

TMPL_INST ( nullptr  ,
const std::string *  ,
maybe_arg ? &maybe_arg->get_str() :nullptr;   
)

◆ TMPL_INST() [7/9]

TMPL_INST ( nullptr  ,
const UniValue ,
maybe_arg;   
)

◆ TMPL_INST() [8/9]

TMPL_INST ( nullptr  ,
std::optional< bool >  ,
maybe_arg ? std::optional{maybe_arg->get_bool()} :std::nullopt;   
)

◆ TMPL_INST() [9/9]

TMPL_INST ( nullptr  ,
std::optional< double >  ,
maybe_arg ? std::optional{maybe_arg->get_real()} :std::nullopt;   
)

Variable Documentation

◆ EXAMPLE_ADDRESS

const std::string EXAMPLE_ADDRESS[2] = {"bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hufdl", "bc1q02ad21edsxd23d32dfgqqsz4vv4nmtfzuklhy3"}

Example bech32 addresses for the RPCExamples help documentation.

They are intentionally invalid to prevent accidental transactions by users.

Definition at line 45 of file util.cpp.

◆ UNIX_EPOCH_TIME

const std::string UNIX_EPOCH_TIME = "UNIX epoch time"

String used to describe UNIX epoch time in documentation, factored out to a constant for consistency.

Definition at line 44 of file util.cpp.