2013-12-13 16:01:22 +01:00
// Copyright (c) 2010 Satoshi Nakamoto
2018-07-26 18:36:45 -04:00
// Copyright (c) 2009-2018 The Bitcoin Core developers
2014-11-20 10:19:29 +08:00
// Distributed under the MIT software license, see the accompanying
2013-12-13 16:01:22 +01:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2017-11-10 13:57:53 +13:00
# include <crypto/ripemd160.h>
2017-09-19 18:12:25 -07:00
# include <key_io.h>
2017-11-10 13:57:53 +13:00
# include <httpserver.h>
2018-04-25 18:43:31 +10:00
# include <outputtype.h>
2017-11-10 13:57:53 +13:00
# include <rpc/blockchain.h>
# include <rpc/server.h>
2017-09-29 00:21:28 -04:00
# include <rpc/util.h>
2019-01-09 20:16:46 +01:00
# include <script/descriptor.h>
2018-10-22 15:51:11 -07:00
# include <util/system.h>
# include <util/strencodings.h>
2019-04-02 17:03:37 -04:00
# include <util/validation.h>
2013-12-13 16:01:22 +01:00
# include <stdint.h>
2019-04-04 00:39:04 -07:00
# include <tuple>
2017-03-20 10:09:01 +01:00
# ifdef HAVE_MALLOC_INFO
# include <malloc.h>
# endif
2013-12-13 16:01:22 +01:00
2015-09-04 16:11:34 +02:00
# include <univalue.h>
2015-05-18 14:02:18 +02:00
2018-05-02 17:14:48 +02:00
static UniValue validateaddress ( const JSONRPCRequest & request )
2013-12-13 16:18:00 +01:00
{
2018-10-20 08:19:44 -04:00
RPCHelpMan { " validateaddress " ,
2019-03-22 10:29:43 -04:00
" \n Return information about the given bitcoin address. \n " ,
2018-10-20 08:19:44 -04:00
{
2018-12-10 16:56:51 -05:00
{ " address " , RPCArg : : Type : : STR , RPCArg : : Optional : : NO , " The bitcoin address to validate " } ,
2018-12-21 12:29:36 -05:00
} ,
RPCResult {
2013-12-13 16:18:00 +01:00
" { \n "
2015-08-10 20:10:56 +02:00
" \" isvalid \" : true|false, (boolean) If the address is valid or not. If not, this is the only property returned. \n "
2018-08-06 15:40:20 -04:00
" \" address \" : \" address \" , (string) The lbry address validated \n "
2018-10-02 14:49:18 -05:00
" \" scriptPubKey \" : \" hex \" , (string) The hex-encoded scriptPubKey generated by the address \n "
2017-06-12 18:53:46 -07:00
" \" isscript \" : true|false, (boolean) If the key is a script \n "
" \" iswitness \" : true|false, (boolean) If the address is a witness address \n "
" \" witness_version \" : version (numeric, optional) The version number of the witness program \n "
" \" witness_program \" : \" hex \" (string, optional) The hex value of the witness program \n "
2013-12-13 16:18:00 +01:00
" } \n "
2018-12-21 12:29:36 -05:00
} ,
RPCExamples {
HelpExampleCli ( " validateaddress " , " \" 1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc \" " )
2013-12-13 16:18:00 +01:00
+ HelpExampleRpc ( " validateaddress " , " \" 1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc \" " )
2018-12-21 12:29:36 -05:00
} ,
2019-06-19 13:59:11 +09:00
} . Check ( request ) ;
2013-12-13 16:18:00 +01:00
2017-08-22 18:02:33 -07:00
CTxDestination dest = DecodeDestination ( request . params [ 0 ] . get_str ( ) ) ;
bool isValid = IsValidDestination ( dest ) ;
2013-12-13 16:18:00 +01:00
2015-05-10 14:48:35 +02:00
UniValue ret ( UniValue : : VOBJ ) ;
2017-09-22 20:04:07 +02:00
ret . pushKV ( " isvalid " , isValid ) ;
2013-12-13 16:18:00 +01:00
if ( isValid )
{
2018-02-20 09:02:34 -05:00
std : : string currentAddress = EncodeDestination ( dest ) ;
ret . pushKV ( " address " , currentAddress ) ;
2014-09-23 05:18:47 -04:00
2018-02-20 09:02:34 -05:00
CScript scriptPubKey = GetScriptForDestination ( dest ) ;
ret . pushKV ( " scriptPubKey " , HexStr ( scriptPubKey . begin ( ) , scriptPubKey . end ( ) ) ) ;
2017-06-12 18:53:46 -07:00
2018-02-20 09:02:34 -05:00
UniValue detail = DescribeAddress ( dest ) ;
ret . pushKVs ( detail ) ;
2013-12-13 16:18:00 +01:00
}
return ret ;
}
2018-05-02 17:14:48 +02:00
static UniValue createmultisig ( const JSONRPCRequest & request )
2013-12-13 16:21:38 +01:00
{
2018-11-23 11:21:38 -05:00
RPCHelpMan { " createmultisig " ,
" \n Creates a multi-signature address with n signature of m keys required. \n "
" It returns a json object with the address and redeemScript. \n " ,
{
2018-12-10 16:56:51 -05:00
{ " nrequired " , RPCArg : : Type : : NUM , RPCArg : : Optional : : NO , " The number of required signatures out of the n keys. " } ,
{ " keys " , RPCArg : : Type : : ARR , RPCArg : : Optional : : NO , " A json array of hex-encoded public keys. " ,
2018-11-23 11:21:38 -05:00
{
2018-12-10 16:56:51 -05:00
{ " key " , RPCArg : : Type : : STR_HEX , RPCArg : : Optional : : OMITTED , " The hex-encoded public key " } ,
2018-11-23 11:21:38 -05:00
} } ,
2018-12-10 16:56:51 -05:00
{ " address_type " , RPCArg : : Type : : STR , /* default */ " legacy " , " The address type to use. Options are \" legacy \" , \" p2sh-segwit \" , and \" bech32 \" . " } ,
2018-12-21 12:29:36 -05:00
} ,
RPCResult {
2013-12-13 16:21:38 +01:00
" { \n "
" \" address \" : \" multisigaddress \" , (string) The value of the new multisig address. \n "
" \" redeemScript \" : \" script \" (string) The string value of the hex-encoded redemption script. \n "
" } \n "
2018-12-21 12:29:36 -05:00
} ,
RPCExamples {
2017-09-29 00:21:28 -04:00
" \n Create a multisig address from 2 public keys \n "
+ HelpExampleCli ( " createmultisig " , " 2 \" [ \\ \" 03789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd \\ \" , \\ \" 03dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a61626 \\ \" ] \" " ) +
2018-10-02 14:49:18 -05:00
" \n As a JSON-RPC call \n "
2017-09-29 00:21:28 -04:00
+ HelpExampleRpc ( " createmultisig " , " 2, \" [ \\ \" 03789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd \\ \" , \\ \" 03dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a61626 \\ \" ] \" " )
2018-12-21 12:29:36 -05:00
} ,
2019-06-19 13:59:11 +09:00
} . Check ( request ) ;
2013-12-13 16:21:38 +01:00
2017-09-29 00:21:28 -04:00
int required = request . params [ 0 ] . get_int ( ) ;
// Get the public keys
const UniValue & keys = request . params [ 1 ] . get_array ( ) ;
std : : vector < CPubKey > pubkeys ;
for ( unsigned int i = 0 ; i < keys . size ( ) ; + + i ) {
if ( IsHex ( keys [ i ] . get_str ( ) ) & & ( keys [ i ] . get_str ( ) . length ( ) = = 66 | | keys [ i ] . get_str ( ) . length ( ) = = 130 ) ) {
pubkeys . push_back ( HexToPubKey ( keys [ i ] . get_str ( ) ) ) ;
} else {
2018-08-06 11:49:05 -04:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , strprintf ( " Invalid public key: %s \n . " , keys [ i ] . get_str ( ) ) ) ;
2017-09-29 00:21:28 -04:00
}
}
2018-04-25 18:43:31 +10:00
// Get the output type
OutputType output_type = OutputType : : LEGACY ;
if ( ! request . params [ 2 ] . isNull ( ) ) {
if ( ! ParseOutputType ( request . params [ 2 ] . get_str ( ) , output_type ) ) {
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , strprintf ( " Unknown address type '%s' " , request . params [ 2 ] . get_str ( ) ) ) ;
}
}
2013-12-13 16:21:38 +01:00
// Construct using pay-to-script-hash:
2019-06-06 16:33:23 +02:00
FillableSigningProvider keystore ;
2019-05-15 00:21:11 -04:00
CScript inner ;
const CTxDestination dest = AddAndGetMultisigDestination ( required , pubkeys , output_type , keystore , inner ) ;
2013-12-13 16:21:38 +01:00
2015-06-02 12:28:54 +02:00
UniValue result ( UniValue : : VOBJ ) ;
2018-04-25 18:43:31 +10:00
result . pushKV ( " address " , EncodeDestination ( dest ) ) ;
2017-09-22 20:04:07 +02:00
result . pushKV ( " redeemScript " , HexStr ( inner . begin ( ) , inner . end ( ) ) ) ;
2013-12-13 16:21:38 +01:00
return result ;
}
2019-02-07 15:09:08 -08:00
UniValue getdescriptorinfo ( const JSONRPCRequest & request )
{
RPCHelpMan { " getdescriptorinfo " ,
{ " \n Analyses a descriptor. \n " } ,
{
{ " descriptor " , RPCArg : : Type : : STR , RPCArg : : Optional : : NO , " The descriptor. " } ,
} ,
RPCResult {
" { \n "
" \" descriptor \" : \" desc \" , (string) The descriptor in canonical form, without private keys \n "
2019-05-08 14:03:29 -07:00
" \" checksum \" : \" chksum \" , (string) The checksum for the input descriptor \n "
2019-02-07 15:09:08 -08:00
" \" isrange \" : true|false, (boolean) Whether the descriptor is ranged \n "
" \" issolvable \" : true|false, (boolean) Whether the descriptor is solvable \n "
" \" hasprivatekeys \" : true|false, (boolean) Whether the input descriptor contained at least one private key \n "
" } \n "
} ,
RPCExamples {
" Analyse a descriptor \n " +
HelpExampleCli ( " getdescriptorinfo " , " \" wpkh([d34db33f/84h/0h/0h]0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2dce28d959f2815b16f81798) \" " )
2019-06-19 13:59:11 +09:00
} } . Check ( request ) ;
2019-02-07 15:09:08 -08:00
RPCTypeCheck ( request . params , { UniValue : : VSTR } ) ;
FlatSigningProvider provider ;
2019-08-02 18:04:02 -04:00
std : : string error ;
auto desc = Parse ( request . params [ 0 ] . get_str ( ) , provider , error ) ;
2019-02-07 15:09:08 -08:00
if ( ! desc ) {
2019-08-02 19:19:53 -04:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , error ) ;
2019-02-07 15:09:08 -08:00
}
UniValue result ( UniValue : : VOBJ ) ;
result . pushKV ( " descriptor " , desc - > ToString ( ) ) ;
2019-05-08 14:03:29 -07:00
result . pushKV ( " checksum " , GetDescriptorChecksum ( request . params [ 0 ] . get_str ( ) ) ) ;
2019-02-07 15:09:08 -08:00
result . pushKV ( " isrange " , desc - > IsRange ( ) ) ;
result . pushKV ( " issolvable " , desc - > IsSolvable ( ) ) ;
result . pushKV ( " hasprivatekeys " , provider . keys . size ( ) > 0 ) ;
return result ;
}
2019-01-09 20:16:46 +01:00
UniValue deriveaddresses ( const JSONRPCRequest & request )
{
RPCHelpMan { " deriveaddresses " ,
{ " \n Derives one or more addresses corresponding to an output descriptor. \n "
" Examples of output descriptors are: \n "
" pkh(<pubkey>) P2PKH outputs for the given pubkey \n "
" wpkh(<pubkey>) Native segwit P2PKH outputs for the given pubkey \n "
" sh(multi(<n>,<pubkey>,<pubkey>,...)) P2SH-multisig outputs for the given threshold and pubkeys \n "
" raw(<hex script>) Outputs whose scriptPubKey equals the specified hex scripts \n "
" \n In the above, <pubkey> either refers to a fixed public key in hexadecimal notation, or to an xpub/xprv optionally followed by one \n "
" or more path elements separated by \" / \" , where \" h \" represents a hardened child key. \n "
" For more information on output descriptors, see the documentation in the doc/descriptors.md file. \n " } ,
{
2018-12-10 16:56:51 -05:00
{ " descriptor " , RPCArg : : Type : : STR , RPCArg : : Optional : : NO , " The descriptor. " } ,
2019-02-27 14:11:38 -08:00
{ " range " , RPCArg : : Type : : RANGE , RPCArg : : Optional : : OMITTED_NAMED_ARG , " If a ranged descriptor is used, this specifies the end or the range (in [begin,end] notation) to derive. " } ,
2019-01-09 20:16:46 +01:00
} ,
RPCResult {
" [ address ] (array) the derived addresses \n "
} ,
RPCExamples {
" First three native segwit receive addresses \n " +
2019-05-14 12:46:24 -04:00
HelpExampleCli ( " deriveaddresses " , " \" wpkh([d34db33f/84h/0h/0h]xpub6DJ2dNUysrn5Vt36jH2KLBT2i1auw1tTSSomg8PhqNiUtx8QX2SvC9nrHu81fT41fvDUnhMjEzQgXnQjKEu3oaqMSzhSrHMxyyoEAmUHQbY/0/*)#cjjspncu \" \" [0,2] \" " )
2019-06-19 13:59:11 +09:00
} } . Check ( request ) ;
2019-01-09 20:16:46 +01:00
2019-02-27 14:11:38 -08:00
RPCTypeCheck ( request . params , { UniValue : : VSTR , UniValueType ( ) } ) ; // Range argument is checked later
2019-01-09 20:16:46 +01:00
const std : : string desc_str = request . params [ 0 ] . get_str ( ) ;
2019-02-27 14:11:38 -08:00
int64_t range_begin = 0 ;
int64_t range_end = 0 ;
2019-01-09 20:16:46 +01:00
2019-02-27 14:11:38 -08:00
if ( request . params . size ( ) > = 2 & & ! request . params [ 1 ] . isNull ( ) ) {
2019-04-04 00:39:04 -07:00
std : : tie ( range_begin , range_end ) = ParseDescriptorRange ( request . params [ 1 ] ) ;
2019-01-09 20:16:46 +01:00
}
2019-04-04 13:12:21 -07:00
FlatSigningProvider key_provider ;
2019-08-02 18:04:02 -04:00
std : : string error ;
auto desc = Parse ( desc_str , key_provider , error , /* require_checksum = */ true ) ;
2019-01-09 20:16:46 +01:00
if ( ! desc ) {
2019-08-02 19:19:53 -04:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , error ) ;
2019-01-09 20:16:46 +01:00
}
if ( ! desc - > IsRange ( ) & & request . params . size ( ) > 1 ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Range should not be specified for an un-ranged descriptor " ) ;
}
if ( desc - > IsRange ( ) & & request . params . size ( ) = = 1 ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Range must be specified for a ranged descriptor " ) ;
}
UniValue addresses ( UniValue : : VARR ) ;
for ( int i = range_begin ; i < = range_end ; + + i ) {
2019-04-04 13:12:21 -07:00
FlatSigningProvider provider ;
2019-01-09 20:16:46 +01:00
std : : vector < CScript > scripts ;
2019-04-04 13:12:21 -07:00
if ( ! desc - > Expand ( i , key_provider , scripts , provider ) ) {
2019-01-09 20:16:46 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , strprintf ( " Cannot derive script without private keys " ) ) ;
}
for ( const CScript & script : scripts ) {
CTxDestination dest ;
if ( ! ExtractDestination ( script , dest ) ) {
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , strprintf ( " Descriptor does not have a corresponding address " ) ) ;
}
addresses . push_back ( EncodeDestination ( dest ) ) ;
}
}
// This should not be possible, but an assert seems overkill:
if ( addresses . empty ( ) ) {
throw JSONRPCError ( RPC_MISC_ERROR , " Unexpected empty result " ) ;
}
return addresses ;
}
2018-05-02 17:14:48 +02:00
static UniValue verifymessage ( const JSONRPCRequest & request )
2013-12-13 16:23:39 +01:00
{
2018-10-20 08:19:44 -04:00
RPCHelpMan { " verifymessage " ,
" \n Verify a signed message \n " ,
{
2018-12-10 16:56:51 -05:00
{ " address " , RPCArg : : Type : : STR , RPCArg : : Optional : : NO , " The bitcoin address to use for the signature. " } ,
{ " signature " , RPCArg : : Type : : STR , RPCArg : : Optional : : NO , " The signature provided by the signer in base 64 encoding (see signmessage). " } ,
{ " message " , RPCArg : : Type : : STR , RPCArg : : Optional : : NO , " The message that was signed. " } ,
2018-12-21 12:29:36 -05:00
} ,
RPCResult {
2013-12-13 16:23:39 +01:00
" true|false (boolean) If the signature is verified or not. \n "
2018-12-21 12:29:36 -05:00
} ,
RPCExamples {
2013-12-13 16:23:39 +01:00
" \n Unlock the wallet for 30 seconds \n "
+ HelpExampleCli ( " walletpassphrase " , " \" mypassphrase \" 30 " ) +
" \n Create the signature \n "
2017-02-26 14:01:05 +01:00
+ HelpExampleCli ( " signmessage " , " \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \" \" my message \" " ) +
2013-12-13 16:23:39 +01:00
" \n Verify the signature \n "
2017-02-26 14:01:05 +01:00
+ HelpExampleCli ( " verifymessage " , " \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \" \" signature \" \" my message \" " ) +
2018-10-02 14:49:18 -05:00
" \n As a JSON-RPC call \n "
2017-02-26 14:01:05 +01:00
+ HelpExampleRpc ( " verifymessage " , " \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \" , \" signature \" , \" my message \" " )
2018-12-21 12:29:36 -05:00
} ,
2019-06-19 13:59:11 +09:00
} . Check ( request ) ;
2013-12-13 16:23:39 +01:00
2014-10-19 04:46:17 -04:00
LOCK ( cs_main ) ;
2017-01-04 13:22:19 +09:00
std : : string strAddress = request . params [ 0 ] . get_str ( ) ;
std : : string strSign = request . params [ 1 ] . get_str ( ) ;
std : : string strMessage = request . params [ 2 ] . get_str ( ) ;
2013-12-13 16:23:39 +01:00
2017-08-22 18:02:33 -07:00
CTxDestination destination = DecodeDestination ( strAddress ) ;
if ( ! IsValidDestination ( destination ) ) {
2013-12-13 16:23:39 +01:00
throw JSONRPCError ( RPC_TYPE_ERROR , " Invalid address " ) ;
2017-08-22 18:02:33 -07:00
}
2013-12-13 16:23:39 +01:00
2019-02-19 17:00:45 -05:00
const PKHash * pkhash = boost : : get < PKHash > ( & destination ) ;
if ( ! pkhash ) {
2013-12-13 16:23:39 +01:00
throw JSONRPCError ( RPC_TYPE_ERROR , " Address does not refer to key " ) ;
2017-08-22 18:02:33 -07:00
}
2013-12-13 16:23:39 +01:00
bool fInvalid = false ;
2017-01-04 13:22:19 +09:00
std : : vector < unsigned char > vchSig = DecodeBase64 ( strSign . c_str ( ) , & fInvalid ) ;
2013-12-13 16:23:39 +01:00
if ( fInvalid )
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Malformed base64 encoding " ) ;
CHashWriter ss ( SER_GETHASH , 0 ) ;
ss < < strMessageMagic ;
ss < < strMessage ;
CPubKey pubkey ;
if ( ! pubkey . RecoverCompact ( ss . GetHash ( ) , vchSig ) )
return false ;
2019-02-19 17:00:45 -05:00
return ( pubkey . GetID ( ) = = * pkhash ) ;
2013-12-13 16:23:39 +01:00
}
2014-11-12 18:59:41 -05:00
2018-05-02 17:14:48 +02:00
static UniValue signmessagewithprivkey ( const JSONRPCRequest & request )
2016-04-26 13:17:00 -04:00
{
2018-10-20 08:19:44 -04:00
RPCHelpMan { " signmessagewithprivkey " ,
" \n Sign a message with the private key of an address \n " ,
{
2018-12-10 16:56:51 -05:00
{ " privkey " , RPCArg : : Type : : STR , RPCArg : : Optional : : NO , " The private key to sign the message with. " } ,
{ " message " , RPCArg : : Type : : STR , RPCArg : : Optional : : NO , " The message to create a signature of. " } ,
2018-12-21 12:29:36 -05:00
} ,
RPCResult {
2016-04-26 13:17:00 -04:00
" \" signature \" (string) The signature of the message encoded in base 64 \n "
2018-12-21 12:29:36 -05:00
} ,
RPCExamples {
2016-04-26 13:17:00 -04:00
" \n Create the signature \n "
+ HelpExampleCli ( " signmessagewithprivkey " , " \" privkey \" \" my message \" " ) +
" \n Verify the signature \n "
2017-02-26 14:01:05 +01:00
+ HelpExampleCli ( " verifymessage " , " \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \" \" signature \" \" my message \" " ) +
2018-10-02 14:49:18 -05:00
" \n As a JSON-RPC call \n "
2016-04-26 13:17:00 -04:00
+ HelpExampleRpc ( " signmessagewithprivkey " , " \" privkey \" , \" my message \" " )
2018-12-21 12:29:36 -05:00
} ,
2019-06-19 13:59:11 +09:00
} . Check ( request ) ;
2016-04-26 13:17:00 -04:00
2017-01-04 13:22:19 +09:00
std : : string strPrivkey = request . params [ 0 ] . get_str ( ) ;
std : : string strMessage = request . params [ 1 ] . get_str ( ) ;
2016-04-26 13:17:00 -04:00
2017-09-19 16:49:52 -07:00
CKey key = DecodeSecret ( strPrivkey ) ;
if ( ! key . IsValid ( ) ) {
2016-04-26 13:17:00 -04:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid private key " ) ;
2017-09-19 16:49:52 -07:00
}
2016-04-26 13:17:00 -04:00
CHashWriter ss ( SER_GETHASH , 0 ) ;
ss < < strMessageMagic ;
ss < < strMessage ;
2017-01-04 13:22:19 +09:00
std : : vector < unsigned char > vchSig ;
2016-04-26 13:17:00 -04:00
if ( ! key . SignCompact ( ss . GetHash ( ) , vchSig ) )
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Sign failed " ) ;
2017-07-11 21:37:53 +12:00
return EncodeBase64 ( vchSig . data ( ) , vchSig . size ( ) ) ;
2016-04-26 13:17:00 -04:00
}
2018-05-02 17:14:48 +02:00
static UniValue setmocktime ( const JSONRPCRequest & request )
2014-11-12 18:59:41 -05:00
{
2018-10-20 08:19:44 -04:00
RPCHelpMan { " setmocktime " ,
" \n Set the local time to given timestamp (-regtest only) \n " ,
{
2018-12-10 16:56:51 -05:00
{ " timestamp " , RPCArg : : Type : : NUM , RPCArg : : Optional : : NO , " Unix seconds-since-epoch timestamp \n "
2018-11-23 11:21:38 -05:00
" Pass 0 to go back to using the system time. " } ,
2018-12-21 12:29:36 -05:00
} ,
RPCResults { } ,
RPCExamples { " " } ,
2019-06-19 13:59:11 +09:00
} . Check ( request ) ;
2014-11-12 18:59:41 -05:00
if ( ! Params ( ) . MineBlocksOnDemand ( ) )
2017-01-04 13:22:19 +09:00
throw std : : runtime_error ( " setmocktime for regression testing (-regtest mode) only " ) ;
2014-11-12 18:59:41 -05:00
2017-01-19 13:01:18 -05:00
// For now, don't change mocktime if we're in the middle of validation, as
// this could have an effect on mempool time-based eviction, as well as
// IsCurrentForFeeEstimation() and IsInitialBlockDownload().
// TODO: figure out the right way to synchronize around mocktime, and
2017-02-06 15:16:18 +01:00
// ensure all call sites of GetTime() are accessing this safely.
2016-04-16 19:13:12 -04:00
LOCK ( cs_main ) ;
2014-10-19 04:46:17 -04:00
2017-06-06 21:15:28 +02:00
RPCTypeCheck ( request . params , { UniValue : : VNUM } ) ;
2016-09-22 09:46:41 +02:00
SetMockTime ( request . params [ 0 ] . get_int64 ( ) ) ;
2014-11-12 18:59:41 -05:00
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2014-11-12 18:59:41 -05:00
}
2016-03-29 19:43:02 +02:00
2016-09-18 10:22:30 +02:00
static UniValue RPCLockedMemoryInfo ( )
{
LockedPool : : Stats stats = LockedPoolManager : : Instance ( ) . stats ( ) ;
UniValue obj ( UniValue : : VOBJ ) ;
2017-09-22 20:04:07 +02:00
obj . pushKV ( " used " , uint64_t ( stats . used ) ) ;
obj . pushKV ( " free " , uint64_t ( stats . free ) ) ;
obj . pushKV ( " total " , uint64_t ( stats . total ) ) ;
obj . pushKV ( " locked " , uint64_t ( stats . locked ) ) ;
obj . pushKV ( " chunks_used " , uint64_t ( stats . chunks_used ) ) ;
obj . pushKV ( " chunks_free " , uint64_t ( stats . chunks_free ) ) ;
2016-09-18 10:22:30 +02:00
return obj ;
}
2017-03-20 10:09:01 +01:00
# ifdef HAVE_MALLOC_INFO
static std : : string RPCMallocInfo ( )
{
char * ptr = nullptr ;
size_t size = 0 ;
FILE * f = open_memstream ( & ptr , & size ) ;
if ( f ) {
malloc_info ( 0 , f ) ;
fclose ( f ) ;
if ( ptr ) {
std : : string rv ( ptr , size ) ;
free ( ptr ) ;
return rv ;
}
}
return " " ;
}
# endif
2018-05-02 17:14:48 +02:00
static UniValue getmemoryinfo ( const JSONRPCRequest & request )
2016-09-18 10:22:30 +02:00
{
/* Please, avoid using the word "pool" here in the RPC interface or help,
* as users will undoubtedly confuse it with the other " memory pool "
*/
2018-10-20 08:19:44 -04:00
RPCHelpMan { " getmemoryinfo " ,
" Returns an object containing information about memory usage. \n " ,
{
2018-12-10 16:56:51 -05:00
{ " mode " , RPCArg : : Type : : STR , /* default */ " \" stats \" " , " determines what kind of information is returned. \n "
2018-11-23 11:21:38 -05:00
" - \" stats \" returns general statistics about memory usage in the daemon. \n "
" - \" mallocinfo \" returns an XML string describing low-level heap state (only available if compiled with glibc 2.10+). " } ,
2018-12-21 12:29:36 -05:00
} ,
{
RPCResult { " mode \" stats \" " ,
2016-09-18 10:22:30 +02:00
" { \n "
" \" locked \" : { (json object) Information about locked memory manager \n "
" \" used \" : xxxxx, (numeric) Number of bytes used \n "
" \" free \" : xxxxx, (numeric) Number of bytes available in current arenas \n "
" \" total \" : xxxxxxx, (numeric) Total number of bytes managed \n "
" \" locked \" : xxxxxx, (numeric) Amount of bytes that succeeded locking. If this number is smaller than total, locking pages failed at some point and key data could be swapped to disk. \n "
" \" chunks_used \" : xxxxx, (numeric) Number allocated chunks \n "
" \" chunks_free \" : xxxxx, (numeric) Number unused chunks \n "
" } \n "
" } \n "
2018-12-21 12:29:36 -05:00
} ,
RPCResult { " mode \" mallocinfo \" " ,
2017-03-20 10:09:01 +01:00
" \" <malloc version= \" 1 \" >... \" \n "
2018-12-21 12:29:36 -05:00
} ,
} ,
RPCExamples {
HelpExampleCli ( " getmemoryinfo " , " " )
2016-09-18 10:22:30 +02:00
+ HelpExampleRpc ( " getmemoryinfo " , " " )
2018-12-21 12:29:36 -05:00
} ,
2019-06-19 13:59:11 +09:00
} . Check ( request ) ;
2017-03-20 10:09:01 +01:00
2017-08-14 19:38:18 -04:00
std : : string mode = request . params [ 0 ] . isNull ( ) ? " stats " : request . params [ 0 ] . get_str ( ) ;
2017-03-20 10:09:01 +01:00
if ( mode = = " stats " ) {
UniValue obj ( UniValue : : VOBJ ) ;
2017-09-22 20:04:07 +02:00
obj . pushKV ( " locked " , RPCLockedMemoryInfo ( ) ) ;
2017-03-20 10:09:01 +01:00
return obj ;
} else if ( mode = = " mallocinfo " ) {
# ifdef HAVE_MALLOC_INFO
return RPCMallocInfo ( ) ;
# else
throw JSONRPCError ( RPC_INVALID_PARAMETER , " mallocinfo is only available when compiled with glibc 2.10+ " ) ;
# endif
} else {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " unknown mode " + mode ) ;
}
2016-09-18 10:22:30 +02:00
}
2018-05-02 17:14:48 +02:00
static void EnableOrDisableLogCategories ( UniValue cats , bool enable ) {
2017-04-03 13:39:11 -04:00
cats = cats . get_array ( ) ;
for ( unsigned int i = 0 ; i < cats . size ( ) ; + + i ) {
std : : string cat = cats [ i ] . get_str ( ) ;
2018-04-11 14:06:35 -07:00
bool success ;
2018-04-11 13:02:01 -07:00
if ( enable ) {
2019-01-25 15:54:49 -05:00
success = LogInstance ( ) . EnableCategory ( cat ) ;
2018-04-11 13:02:01 -07:00
} else {
2019-01-25 15:54:49 -05:00
success = LogInstance ( ) . DisableCategory ( cat ) ;
2018-04-11 14:06:35 -07:00
}
if ( ! success ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " unknown logging category " + cat ) ;
2017-08-29 20:32:16 +09:00
}
2017-04-03 13:39:11 -04:00
}
}
UniValue logging ( const JSONRPCRequest & request )
{
2018-10-20 08:19:44 -04:00
RPCHelpMan { " logging " ,
2017-04-03 13:39:11 -04:00
" Gets and sets the logging configuration. \n "
2017-08-29 20:32:16 +09:00
" When called without an argument, returns the list of categories with status that are currently being debug logged or not. \n "
" When called with arguments, adds or removes categories from debug logging and return the lists above. \n "
" The arguments are evaluated in order \" include \" , \" exclude \" . \n "
" If an item is both included and excluded, it will thus end up being excluded. \n "
2017-04-03 13:39:11 -04:00
" The valid logging categories are: " + ListLogCategories ( ) + " \n "
2017-08-29 20:32:16 +09:00
" In addition, the following are available as category names with special meanings: \n "
" - \" all \" , \" 1 \" : represent all logging categories. \n "
" - \" none \" , \" 0 \" : even if other logging categories are specified, ignore all of them. \n "
2018-10-20 08:19:44 -04:00
,
{
2018-12-10 16:56:51 -05:00
{ " include " , RPCArg : : Type : : ARR , RPCArg : : Optional : : OMITTED_NAMED_ARG , " A json array of categories to add debug logging " ,
2018-11-23 11:21:38 -05:00
{
2018-12-10 16:56:51 -05:00
{ " include_category " , RPCArg : : Type : : STR , RPCArg : : Optional : : OMITTED , " the valid logging category " } ,
2018-11-23 11:21:38 -05:00
} } ,
2018-12-10 16:56:51 -05:00
{ " exclude " , RPCArg : : Type : : ARR , RPCArg : : Optional : : OMITTED_NAMED_ARG , " A json array of categories to remove debug logging " ,
2018-11-23 11:21:38 -05:00
{
2018-12-10 16:56:51 -05:00
{ " exclude_category " , RPCArg : : Type : : STR , RPCArg : : Optional : : OMITTED , " the valid logging category " } ,
2018-11-23 11:21:38 -05:00
} } ,
2018-12-21 12:29:36 -05:00
} ,
RPCResult {
2017-08-29 20:32:16 +09:00
" { (json object where keys are the logging categories, and values indicates its status \n "
2019-01-27 22:47:55 +08:00
" \" category \" : true|false, (bool) if being debug logged or not. false:inactive, true:active \n "
2017-08-29 20:32:16 +09:00
" ... \n "
" } \n "
2018-12-21 12:29:36 -05:00
} ,
RPCExamples {
HelpExampleCli ( " logging " , " \" [ \\ \" all \\ \" ] \" \" [ \\ \" http \\ \" ] \" " )
2019-01-27 22:47:55 +08:00
+ HelpExampleRpc ( " logging " , " [ \" all \" ], [ \" libevent \" ] " )
2018-12-21 12:29:36 -05:00
} ,
2019-06-19 13:59:11 +09:00
} . Check ( request ) ;
2017-04-03 13:39:11 -04:00
2019-01-25 15:54:49 -05:00
uint32_t original_log_categories = LogInstance ( ) . GetCategoryMask ( ) ;
2017-08-14 19:38:18 -04:00
if ( request . params [ 0 ] . isArray ( ) ) {
2018-04-11 13:02:01 -07:00
EnableOrDisableLogCategories ( request . params [ 0 ] , true ) ;
2017-04-03 13:39:11 -04:00
}
2017-08-14 19:38:18 -04:00
if ( request . params [ 1 ] . isArray ( ) ) {
2018-04-11 13:02:01 -07:00
EnableOrDisableLogCategories ( request . params [ 1 ] , false ) ;
2017-04-03 13:39:11 -04:00
}
2019-01-25 15:54:49 -05:00
uint32_t updated_log_categories = LogInstance ( ) . GetCategoryMask ( ) ;
2018-04-11 13:02:01 -07:00
uint32_t changed_log_categories = original_log_categories ^ updated_log_categories ;
2017-04-03 13:39:11 -04:00
2017-04-10 10:34:23 -04:00
// Update libevent logging if BCLog::LIBEVENT has changed.
// If the library version doesn't allow it, UpdateHTTPServerLogging() returns false,
// in which case we should clear the BCLog::LIBEVENT flag.
// Throw an error if the user has explicitly asked to change only the libevent
// flag and it failed.
2018-04-11 13:02:01 -07:00
if ( changed_log_categories & BCLog : : LIBEVENT ) {
2019-01-25 15:54:49 -05:00
if ( ! UpdateHTTPServerLogging ( LogInstance ( ) . WillLogCategory ( BCLog : : LIBEVENT ) ) ) {
LogInstance ( ) . DisableCategory ( BCLog : : LIBEVENT ) ;
2018-04-11 13:02:01 -07:00
if ( changed_log_categories = = BCLog : : LIBEVENT ) {
2017-04-10 10:34:23 -04:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " libevent logging cannot be updated when using libevent before v2.1.1. " ) ;
}
}
}
2017-04-03 13:39:11 -04:00
UniValue result ( UniValue : : VOBJ ) ;
std : : vector < CLogCategoryActive > vLogCatActive = ListActiveLogCategories ( ) ;
for ( const auto & logCatActive : vLogCatActive ) {
result . pushKV ( logCatActive . category , logCatActive . active ) ;
}
return result ;
}
2018-05-02 17:14:48 +02:00
static UniValue echo ( const JSONRPCRequest & request )
2016-09-25 20:55:24 +02:00
{
if ( request . fHelp )
2017-01-04 13:22:19 +09:00
throw std : : runtime_error (
2018-10-20 08:19:44 -04:00
RPCHelpMan { " echo|echojson ... " ,
" \n Simply echo back the input arguments. This command is for testing. \n "
2018-11-15 12:19:41 -05:00
" \n The difference between echo and echojson is that echojson has argument conversion enabled in the client-side table in "
2018-08-06 15:40:20 -04:00
" lbrycrd-cli and the GUI. There is no server-side difference. "
2018-12-21 12:29:36 -05:00
{ } ,
RPCResults { } ,
RPCExamples { " " } ,
} . ToString ( )
) ;
2016-09-25 20:55:24 +02:00
return request . params ;
}
2018-08-20 14:19:43 +02:00
// clang-format off
2016-03-29 19:43:02 +02:00
static const CRPCCommand commands [ ] =
2017-06-08 17:38:23 -07:00
{ // category name actor (function) argNames
2016-03-29 19:43:02 +02:00
// --------------------- ------------------------ ----------------------- ----------
2017-06-08 17:38:23 -07:00
{ " control " , " getmemoryinfo " , & getmemoryinfo , { " mode " } } ,
2017-11-07 07:50:49 +01:00
{ " control " , " logging " , & logging , { " include " , " exclude " } } ,
2018-02-20 09:02:34 -05:00
{ " util " , " validateaddress " , & validateaddress , { " address " } } ,
2018-10-28 22:42:36 +00:00
{ " util " , " createmultisig " , & createmultisig , { " nrequired " , " keys " , " address_type " } } ,
2019-03-01 13:45:16 +01:00
{ " util " , " deriveaddresses " , & deriveaddresses , { " descriptor " , " range " } } ,
2019-02-07 15:09:08 -08:00
{ " util " , " getdescriptorinfo " , & getdescriptorinfo , { " descriptor " } } ,
2017-06-08 17:38:23 -07:00
{ " util " , " verifymessage " , & verifymessage , { " address " , " signature " , " message " } } ,
{ " util " , " signmessagewithprivkey " , & signmessagewithprivkey , { " privkey " , " message " } } ,
2016-03-29 19:43:02 +02:00
/* Not shown in help */
2017-06-08 17:38:23 -07:00
{ " hidden " , " setmocktime " , & setmocktime , { " timestamp " } } ,
{ " hidden " , " echo " , & echo , { " arg0 " , " arg1 " , " arg2 " , " arg3 " , " arg4 " , " arg5 " , " arg6 " , " arg7 " , " arg8 " , " arg9 " } } ,
{ " hidden " , " echojson " , & echo , { " arg0 " , " arg1 " , " arg2 " , " arg3 " , " arg4 " , " arg5 " , " arg6 " , " arg7 " , " arg8 " , " arg9 " } } ,
2016-03-29 19:43:02 +02:00
} ;
2018-08-20 14:19:43 +02:00
// clang-format on
2016-03-29 19:43:02 +02:00
2016-06-07 18:42:42 +02:00
void RegisterMiscRPCCommands ( CRPCTable & t )
2016-03-29 19:43:02 +02:00
{
for ( unsigned int vcidx = 0 ; vcidx < ARRAYLEN ( commands ) ; vcidx + + )
2016-06-07 18:42:42 +02:00
t . appendCommand ( commands [ vcidx ] . name , & commands [ vcidx ] ) ;
2016-03-29 19:43:02 +02:00
}