2019-02-13 02:16:29 +01:00
// Copyright (c) 2017-2019 The Bitcoin Core developers
2017-09-29 06:21:28 +02:00
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
# ifndef BITCOIN_RPC_UTIL_H
# define BITCOIN_RPC_UTIL_H
2019-02-10 05:51:33 +01:00
# include <node/transaction.h>
2017-12-04 18:49:20 +01:00
# include <pubkey.h>
2019-02-18 12:11:41 +01:00
# include <rpc/protocol.h>
2017-12-04 18:49:20 +01:00
# include <script/standard.h>
# include <univalue.h>
2017-09-29 06:21:28 +02:00
# include <string>
# include <vector>
2018-12-10 22:56:51 +01:00
# include <boost/variant.hpp>
2017-09-29 06:21:28 +02:00
class CKeyStore ;
class CPubKey ;
class CScript ;
2017-05-30 21:55:17 +02:00
struct InitInterfaces ;
//! Pointers to interfaces that need to be accessible from RPC methods. Due to
//! limitations of the RPC framework, there's currently no direct way to pass in
//! state to RPC method implementations.
extern InitInterfaces * g_rpc_interfaces ;
2017-09-29 06:21:28 +02:00
2019-04-02 22:42:51 +02:00
/** Wrapper for UniValue::VType, which includes typeAny:
* Used to denote don ' t care type . */
struct UniValueType {
UniValueType ( UniValue : : VType _type ) : typeAny ( false ) , type ( _type ) { }
UniValueType ( ) : typeAny ( true ) { }
bool typeAny ;
UniValue : : VType type ;
} ;
/**
* Type - check arguments ; throws JSONRPCError if wrong type given . Does not check that
* the right number of arguments are passed , just that any passed are the correct type .
*/
void RPCTypeCheck ( const UniValue & params ,
const std : : list < UniValueType > & typesExpected , bool fAllowNull = false ) ;
/**
* Type - check one argument ; throws JSONRPCError if wrong type given .
*/
void RPCTypeCheckArgument ( const UniValue & value , const UniValueType & typeExpected ) ;
/*
Check for expected keys / value types in an Object .
*/
void RPCTypeCheckObj ( const UniValue & o ,
const std : : map < std : : string , UniValueType > & typesExpected ,
bool fAllowNull = false ,
bool fStrict = false ) ;
/**
* Utilities : convert hex - encoded Values
* ( throws error if not hex ) .
*/
extern uint256 ParseHashV ( const UniValue & v , std : : string strName ) ;
extern uint256 ParseHashO ( const UniValue & o , std : : string strKey ) ;
extern std : : vector < unsigned char > ParseHexV ( const UniValue & v , std : : string strName ) ;
extern std : : vector < unsigned char > ParseHexO ( const UniValue & o , std : : string strKey ) ;
extern CAmount AmountFromValue ( const UniValue & value ) ;
extern std : : string HelpExampleCli ( const std : : string & methodname , const std : : string & args ) ;
extern std : : string HelpExampleRpc ( const std : : string & methodname , const std : : string & args ) ;
2017-09-29 06:21:28 +02:00
CPubKey HexToPubKey ( const std : : string & hex_in ) ;
CPubKey AddrToPubKey ( CKeyStore * const keystore , const std : : string & addr_in ) ;
CScript CreateMultisigRedeemscript ( const int required , const std : : vector < CPubKey > & pubkeys ) ;
2017-12-04 18:49:20 +01:00
UniValue DescribeAddress ( const CTxDestination & dest ) ;
2019-02-08 21:29:45 +01:00
//! Parse a confirm target option and raise an RPC error if it is invalid.
2017-07-29 03:40:29 +02:00
unsigned int ParseConfirmTarget ( const UniValue & value , unsigned int max_target ) ;
2019-02-08 21:29:45 +01:00
2019-02-10 05:51:33 +01:00
RPCErrorCode RPCErrorFromTransactionError ( TransactionError terr ) ;
UniValue JSONRPCTransactionError ( TransactionError terr , const std : : string & err_string = " " ) ;
2019-02-27 22:41:41 +01:00
//! Parse a JSON range specified as int64, or [int64, int64]
2019-04-04 09:39:04 +02:00
std : : pair < int64_t , int64_t > ParseDescriptorRange ( const UniValue & value ) ;
2019-02-10 05:51:33 +01:00
2018-10-23 21:22:28 +02:00
struct RPCArg {
enum class Type {
OBJ ,
ARR ,
STR ,
NUM ,
BOOL ,
OBJ_USER_KEYS , //!< Special type where the user must set the keys e.g. to define multiple addresses; as opposed to e.g. an options object where the keys are predefined
AMOUNT , //!< Special type representing a floating point amount (can be either NUM or STR)
STR_HEX , //!< Special type that is a STR with only hex chars
2019-02-27 22:45:47 +01:00
RANGE , //!< Special type that is a NUM or [NUM,NUM]
2018-10-23 21:22:28 +02:00
} ;
2018-12-10 22:56:51 +01:00
enum class Optional {
/** Required arg */
NO ,
/**
2019-02-13 02:16:29 +01:00
* Optional arg that is a named argument and has a default value of
2018-12-10 22:56:51 +01:00
* ` null ` . When possible , the default value should be specified .
*/
OMITTED_NAMED_ARG ,
/**
* Optional argument with default value omitted because they are
* implicitly clear . That is , elements in an array or object may not
* exist by default .
* When possible , the default value should be specified .
*/
OMITTED ,
} ;
using Fallback = boost : : variant < Optional , /* default value for optional args */ std : : string > ;
2018-10-23 21:22:28 +02:00
const std : : string m_name ; //!< The name of the arg (can be empty for inner args)
const Type m_type ;
const std : : vector < RPCArg > m_inner ; //!< Only used for arrays or dicts
2018-12-10 22:56:51 +01:00
const Fallback m_fallback ;
2018-11-23 17:21:38 +01:00
const std : : string m_description ;
2018-10-20 14:19:44 +02:00
const std : : string m_oneline_description ; //!< Should be empty unless it is supposed to override the auto-generated summary line
2018-11-23 17:21:38 +01:00
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.
RPCArg (
const std : : string & name ,
const Type & type ,
2018-12-10 22:56:51 +01:00
const Fallback & fallback ,
2018-11-23 17:21:38 +01:00
const std : : string & description ,
const std : : string & oneline_description = " " ,
const std : : vector < std : : string > & type_str = { } )
: m_name { name } ,
m_type { type } ,
2018-12-10 22:56:51 +01:00
m_fallback { fallback } ,
2018-11-23 17:21:38 +01:00
m_description { description } ,
m_oneline_description { oneline_description } ,
m_type_str { type_str }
2018-10-23 21:22:28 +02:00
{
assert ( type ! = Type : : ARR & & type ! = Type : : OBJ ) ;
}
2018-11-23 17:21:38 +01:00
RPCArg (
const std : : string & name ,
const Type & type ,
2018-12-10 22:56:51 +01:00
const Fallback & fallback ,
2018-11-23 17:21:38 +01:00
const std : : string & description ,
const std : : vector < RPCArg > & inner ,
const std : : string & oneline_description = " " ,
const std : : vector < std : : string > & type_str = { } )
: m_name { name } ,
m_type { type } ,
m_inner { inner } ,
2018-12-10 22:56:51 +01:00
m_fallback { fallback } ,
2018-11-23 17:21:38 +01:00
m_description { description } ,
m_oneline_description { oneline_description } ,
m_type_str { type_str }
2018-10-23 21:22:28 +02:00
{
assert ( type = = Type : : ARR | | type = = Type : : OBJ ) ;
}
2019-02-13 02:16:29 +01:00
bool IsOptional ( ) const ;
2018-12-04 19:30:06 +01:00
/**
* Return the type string of the argument .
2018-12-05 23:07:30 +01:00
* Set oneline to allow it to be overridden by a custom oneline type string ( m_oneline_description ) .
2018-12-04 19:30:06 +01:00
*/
std : : string ToString ( bool oneline ) const ;
/**
* Return the type string of the argument when it is in an object ( dict ) .
* Set oneline to get the oneline representation ( less whitespace )
*/
std : : string ToStringObj ( bool oneline ) const ;
/**
* Return the description string , including the argument type and whether
* the argument is required .
*/
2018-12-10 22:56:51 +01:00
std : : string ToDescriptionString ( ) const ;
2018-10-23 21:22:28 +02:00
} ;
2018-12-21 18:29:36 +01:00
struct RPCResult {
const std : : string m_cond ;
const std : : string m_result ;
explicit RPCResult ( std : : string result )
: m_cond { } , m_result { std : : move ( result ) }
{
assert ( ! m_result . empty ( ) ) ;
}
RPCResult ( std : : string cond , std : : string result )
: m_cond { std : : move ( cond ) } , m_result { std : : move ( result ) }
{
assert ( ! m_cond . empty ( ) ) ;
assert ( ! m_result . empty ( ) ) ;
}
} ;
struct RPCResults {
const std : : vector < RPCResult > m_results ;
RPCResults ( )
: m_results { }
{
}
RPCResults ( RPCResult result )
: m_results { { result } }
{
}
RPCResults ( std : : initializer_list < RPCResult > results )
: m_results { results }
{
}
/**
* Return the description string .
*/
std : : string ToDescriptionString ( ) const ;
} ;
struct RPCExamples {
const std : : string m_examples ;
RPCExamples (
std : : string examples )
: m_examples ( std : : move ( examples ) )
{
}
std : : string ToDescriptionString ( ) const ;
} ;
2018-10-23 21:22:28 +02:00
class RPCHelpMan
{
public :
2018-12-21 18:29:36 +01:00
RPCHelpMan ( std : : string name , std : : string description , std : : vector < RPCArg > args , RPCResults results , RPCExamples examples ) ;
2018-10-23 21:22:28 +02:00
std : : string ToString ( ) const ;
2019-02-13 02:16:29 +01:00
/** If the supplied number of args is neither too small nor too high */
bool IsValidNumArgs ( size_t num_args ) const ;
2018-10-23 21:22:28 +02:00
private :
const std : : string m_name ;
2018-10-20 14:19:44 +02:00
const std : : string m_description ;
2018-10-23 21:22:28 +02:00
const std : : vector < RPCArg > m_args ;
2018-12-21 18:29:36 +01:00
const RPCResults m_results ;
const RPCExamples m_examples ;
2018-10-23 21:22:28 +02:00
} ;
2017-09-29 06:21:28 +02:00
# endif // BITCOIN_RPC_UTIL_H