2012-08-21 16:38:57 +02:00
// Copyright (c) 2010 Satoshi Nakamoto
2018-05-16 21:17:40 +02:00
// Copyright (c) 2009-2018 The Bitcoin Core developers
2014-11-20 03:19:29 +01:00
// Distributed under the MIT software license, see the accompanying
2012-08-21 16:38:57 +02:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2017-11-10 01:57:53 +01:00
# include <amount.h>
# include <chain.h>
# include <consensus/validation.h>
# include <core_io.h>
# include <httpserver.h>
2017-05-30 21:55:17 +02:00
# include <init.h>
2017-07-26 16:23:01 +02:00
# include <interfaces/chain.h>
2017-11-10 01:57:53 +01:00
# include <validation.h>
2017-09-20 03:12:25 +02:00
# include <key_io.h>
2017-11-10 01:57:53 +01:00
# include <net.h>
2018-07-09 10:15:50 +02:00
# include <outputtype.h>
2017-11-10 01:57:53 +01:00
# include <policy/feerate.h>
# include <policy/fees.h>
# include <policy/policy.h>
# include <policy/rbf.h>
# include <rpc/mining.h>
2017-06-12 21:23:02 +02:00
# include <rpc/rawtransaction.h>
2017-11-10 01:57:53 +01:00
# include <rpc/server.h>
2017-09-29 06:21:28 +02:00
# include <rpc/util.h>
2017-11-10 01:57:53 +01:00
# include <script/sign.h>
2018-05-16 21:17:40 +02:00
# include <shutdown.h>
2017-11-10 01:57:53 +01:00
# include <timedata.h>
2018-10-23 00:51:11 +02:00
# include <util/system.h>
# include <util/moneystr.h>
2017-11-10 01:57:53 +01:00
# include <wallet/coincontrol.h>
# include <wallet/feebumper.h>
2017-06-13 03:53:46 +02:00
# include <wallet/rpcwallet.h>
2017-11-10 01:57:53 +01:00
# include <wallet/wallet.h>
# include <wallet/walletdb.h>
2017-10-08 22:48:07 +02:00
# include <wallet/walletutil.h>
2013-04-13 07:13:08 +02:00
# include <stdint.h>
2015-09-04 16:11:34 +02:00
# include <univalue.h>
2012-08-21 16:38:57 +02:00
2018-04-06 21:05:32 +02:00
# include <functional>
2017-07-17 12:10:23 +02:00
static const std : : string WALLET_ENDPOINT_BASE = " /wallet/ " ;
2018-06-12 15:40:52 +02:00
bool GetWalletNameFromJSONRPCRequest ( const JSONRPCRequest & request , std : : string & wallet_name )
2016-10-25 09:45:57 +02:00
{
2017-07-17 12:10:23 +02:00
if ( request . URI . substr ( 0 , WALLET_ENDPOINT_BASE . size ( ) ) = = WALLET_ENDPOINT_BASE ) {
// wallet endpoint was used
2018-06-12 15:40:52 +02:00
wallet_name = urlDecode ( request . URI . substr ( WALLET_ENDPOINT_BASE . size ( ) ) ) ;
return true ;
}
return false ;
}
std : : shared_ptr < CWallet > GetWalletForJSONRPCRequest ( const JSONRPCRequest & request )
{
std : : string wallet_name ;
if ( GetWalletNameFromJSONRPCRequest ( request , wallet_name ) ) {
std : : shared_ptr < CWallet > pwallet = GetWallet ( wallet_name ) ;
2018-04-17 19:22:23 +02:00
if ( ! pwallet ) throw JSONRPCError ( RPC_WALLET_NOT_FOUND , " Requested wallet does not exist or is not loaded " ) ;
return pwallet ;
2017-07-17 12:10:23 +02:00
}
2018-04-17 19:22:23 +02:00
2018-05-22 17:18:07 +02:00
std : : vector < std : : shared_ptr < CWallet > > wallets = GetWallets ( ) ;
2018-04-17 19:22:23 +02:00
return wallets . size ( ) = = 1 | | ( request . fHelp & & wallets . size ( ) > 0 ) ? wallets [ 0 ] : nullptr ;
2016-10-25 09:45:57 +02:00
}
2012-08-21 16:38:57 +02:00
2016-09-09 07:32:12 +02:00
std : : string HelpRequiringPassphrase ( CWallet * const pwallet )
2012-08-21 16:38:57 +02:00
{
2016-09-09 07:32:12 +02:00
return pwallet & & pwallet - > IsCrypted ( )
2013-10-29 12:29:44 +01:00
? " \n Requires wallet passphrase to be set with walletpassphrase call. "
2012-08-21 16:38:57 +02:00
: " " ;
}
2016-09-09 07:32:12 +02:00
bool EnsureWalletIsAvailable ( CWallet * const pwallet , bool avoidException )
2015-04-12 17:56:44 +02:00
{
2017-07-17 11:42:30 +02:00
if ( pwallet ) return true ;
if ( avoidException ) return false ;
2018-04-18 14:46:11 +02:00
if ( ! HasWallets ( ) ) {
2017-07-26 15:35:17 +02:00
throw JSONRPCError (
RPC_METHOD_NOT_FOUND , " Method not found (wallet method is disabled because no wallet is loaded) " ) ;
2015-04-12 17:56:44 +02:00
}
2017-07-17 11:42:30 +02:00
throw JSONRPCError ( RPC_WALLET_NOT_SPECIFIED ,
" Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path). " ) ;
2015-04-12 17:56:44 +02:00
}
2016-09-09 07:32:12 +02:00
void EnsureWalletIsUnlocked ( CWallet * const pwallet )
2012-08-21 16:38:57 +02:00
{
2016-10-25 10:04:23 +02:00
if ( pwallet - > IsLocked ( ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_UNLOCK_NEEDED , " Error: Please enter the wallet passphrase with walletpassphrase first. " ) ;
2016-10-25 10:04:23 +02:00
}
2012-08-21 16:38:57 +02:00
}
2017-07-31 21:30:21 +02:00
static void WalletTxToJSON ( interfaces : : Chain & chain , interfaces : : Chain : : Lock & locked_chain , const CWalletTx & wtx , UniValue & entry )
2012-08-21 16:38:57 +02:00
{
2017-07-31 21:30:21 +02:00
int confirms = wtx . GetDepthInMainChain ( locked_chain ) ;
2017-09-22 20:04:07 +02:00
entry . pushKV ( " confirmations " , confirms ) ;
2012-06-02 04:33:28 +02:00
if ( wtx . IsCoinBase ( ) )
2017-09-22 20:04:07 +02:00
entry . pushKV ( " generated " , true ) ;
2014-02-12 19:43:07 +01:00
if ( confirms > 0 )
2012-08-21 16:38:57 +02:00
{
2017-09-22 20:04:07 +02:00
entry . pushKV ( " blockhash " , wtx . hashBlock . GetHex ( ) ) ;
entry . pushKV ( " blockindex " , wtx . nIndex ) ;
2018-01-12 01:23:09 +01:00
entry . pushKV ( " blocktime " , LookupBlockIndex ( wtx . hashBlock ) - > GetBlockTime ( ) ) ;
2015-11-26 18:42:07 +01:00
} else {
2017-07-31 21:30:21 +02:00
entry . pushKV ( " trusted " , wtx . IsTrusted ( locked_chain ) ) ;
2012-08-21 16:38:57 +02:00
}
2014-02-14 02:12:51 +01:00
uint256 hash = wtx . GetHash ( ) ;
2017-09-22 20:04:07 +02:00
entry . pushKV ( " txid " , hash . GetHex ( ) ) ;
2015-05-10 14:48:35 +02:00
UniValue conflicts ( UniValue : : VARR ) ;
2017-06-02 03:18:57 +02:00
for ( const uint256 & conflict : wtx . GetConflicts ( ) )
2014-02-14 02:12:51 +01:00
conflicts . push_back ( conflict . GetHex ( ) ) ;
2017-09-22 20:04:07 +02:00
entry . pushKV ( " walletconflicts " , conflicts ) ;
entry . pushKV ( " time " , wtx . GetTxTime ( ) ) ;
entry . pushKV ( " timereceived " , ( int64_t ) wtx . nTimeReceived ) ;
2015-12-16 20:57:54 +01:00
// Add opt-in RBF status
std : : string rbfStatus = " no " ;
if ( confirms < = 0 ) {
LOCK ( mempool . cs ) ;
2017-05-09 08:46:26 +02:00
RBFTransactionState rbfState = IsRBFOptIn ( * wtx . tx , mempool ) ;
2018-03-09 15:03:40 +01:00
if ( rbfState = = RBFTransactionState : : UNKNOWN )
2016-04-05 14:20:49 +02:00
rbfStatus = " unknown " ;
2018-03-09 15:03:40 +01:00
else if ( rbfState = = RBFTransactionState : : REPLACEABLE_BIP125 )
2015-12-16 20:57:54 +01:00
rbfStatus = " yes " ;
}
2017-09-22 20:04:07 +02:00
entry . pushKV ( " bip125-replaceable " , rbfStatus ) ;
2015-12-16 20:57:54 +01:00
2018-05-16 00:41:53 +02:00
for ( const std : : pair < const std : : string , std : : string > & item : wtx . mapValue )
2017-09-22 20:04:07 +02:00
entry . pushKV ( item . first , item . second ) ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static std : : string LabelFromValue ( const UniValue & value )
2012-08-21 16:38:57 +02:00
{
2017-10-20 19:27:55 +02:00
std : : string label = value . get_str ( ) ;
if ( label = = " * " )
throw JSONRPCError ( RPC_WALLET_INVALID_LABEL_NAME , " Invalid label name " ) ;
return label ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue getnewaddress ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2017-12-01 01:49:11 +01:00
if ( request . fHelp | | request . params . size ( ) > 2 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2017-10-20 19:27:55 +02:00
" getnewaddress ( \" label \" \" address_type \" ) \n "
2013-10-29 12:29:44 +01:00
" \n Returns a new Bitcoin address for receiving payments. \n "
2017-10-20 19:27:55 +02:00
" If 'label' is specified, it is added to the address book \n "
" so payments received with the address will be associated with 'label'. \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
2017-10-20 19:27:55 +02:00
" 1. \" label \" (string, optional) The label name for the address to be linked to. If not provided, the default label \" \" is used. It can also be set to the empty string \" \" to represent the default label. The label does not need to exist, it will be created if there is no label by the given name. \n "
2018-01-13 16:49:46 +01:00
" 2. \" address_type \" (string, optional) The address type to use. Options are \" legacy \" , \" p2sh-segwit \" , and \" bech32 \" . Default is set by -addresstype. \n "
2013-10-29 12:29:44 +01:00
" \n Result: \n "
2016-11-21 14:39:58 +01:00
" \" address \" (string) The new bitcoin address \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
+ HelpExampleCli ( " getnewaddress " , " " )
2014-12-30 15:32:07 +01:00
+ HelpExampleRpc ( " getnewaddress " , " " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
2017-05-05 08:53:39 +02:00
if ( pwallet - > IsWalletFlagSet ( WALLET_FLAG_DISABLE_PRIVATE_KEYS ) ) {
throw JSONRPCError ( RPC_WALLET_ERROR , " Error: Private keys are disabled for this wallet " ) ;
}
2018-08-23 02:46:59 +02:00
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2017-10-20 19:27:55 +02:00
// Parse the label first so we don't generate a key if there's an error
std : : string label ;
2017-07-10 17:44:39 +02:00
if ( ! request . params [ 0 ] . isNull ( ) )
2017-10-20 19:27:55 +02:00
label = LabelFromValue ( request . params [ 0 ] ) ;
2012-08-21 16:38:57 +02:00
2018-02-11 03:06:35 +01:00
OutputType output_type = pwallet - > m_default_address_type ;
2017-12-01 01:49:11 +01:00
if ( ! request . params [ 1 ] . isNull ( ) ) {
2018-03-19 20:57:11 +01:00
if ( ! ParseOutputType ( request . params [ 1 ] . get_str ( ) , output_type ) ) {
2017-12-01 01:49:11 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , strprintf ( " Unknown address type '%s' " , request . params [ 1 ] . get_str ( ) ) ) ;
}
}
2016-10-25 10:04:23 +02:00
if ( ! pwallet - > IsLocked ( ) ) {
2016-10-25 09:45:57 +02:00
pwallet - > TopUpKeyPool ( ) ;
2016-10-25 10:04:23 +02:00
}
2012-08-21 16:38:57 +02:00
// Generate a new key that is added to wallet
CPubKey newKey ;
2016-10-25 10:04:23 +02:00
if ( ! pwallet - > GetKeyFromPool ( newKey ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_KEYPOOL_RAN_OUT , " Error: Keypool ran out, please call keypoolrefill first " ) ;
2016-10-25 10:04:23 +02:00
}
2017-12-01 01:49:11 +01:00
pwallet - > LearnRelatedScripts ( newKey , output_type ) ;
CTxDestination dest = GetDestinationForKey ( newKey , output_type ) ;
2012-08-21 16:38:57 +02:00
2017-10-20 19:27:55 +02:00
pwallet - > SetAddressBook ( dest , label , " receive " ) ;
2012-08-21 16:38:57 +02:00
2017-12-01 01:49:11 +01:00
return EncodeDestination ( dest ) ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue getrawchangeaddress ( const JSONRPCRequest & request )
2013-08-28 22:29:52 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2017-12-01 01:49:11 +01:00
if ( request . fHelp | | request . params . size ( ) > 1 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2017-12-01 01:49:11 +01:00
" getrawchangeaddress ( \" address_type \" ) \n "
2013-10-29 12:29:44 +01:00
" \n Returns a new Bitcoin address, for receiving change. \n "
" This is for use with raw transactions, NOT normal use. \n "
2017-12-01 01:49:11 +01:00
" \n Arguments: \n "
2018-01-13 16:49:46 +01:00
" 1. \" address_type \" (string, optional) The address type to use. Options are \" legacy \" , \" p2sh-segwit \" , and \" bech32 \" . Default is set by -changetype. \n "
2013-10-29 12:29:44 +01:00
" \n Result: \n "
" \" address \" (string) The address \n "
" \n Examples: \n "
+ HelpExampleCli ( " getrawchangeaddress " , " " )
+ HelpExampleRpc ( " getrawchangeaddress " , " " )
) ;
2013-08-28 22:29:52 +02:00
2017-05-05 08:53:39 +02:00
if ( pwallet - > IsWalletFlagSet ( WALLET_FLAG_DISABLE_PRIVATE_KEYS ) ) {
throw JSONRPCError ( RPC_WALLET_ERROR , " Error: Private keys are disabled for this wallet " ) ;
}
2018-08-23 02:46:59 +02:00
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2016-10-25 10:04:23 +02:00
if ( ! pwallet - > IsLocked ( ) ) {
2016-10-25 09:45:57 +02:00
pwallet - > TopUpKeyPool ( ) ;
2016-10-25 10:04:23 +02:00
}
2013-08-28 22:29:52 +02:00
2018-03-19 20:57:11 +01:00
OutputType output_type = pwallet - > m_default_change_type ! = OutputType : : CHANGE_AUTO ? pwallet - > m_default_change_type : pwallet - > m_default_address_type ;
2017-12-01 01:49:11 +01:00
if ( ! request . params [ 0 ] . isNull ( ) ) {
2018-03-19 20:57:11 +01:00
if ( ! ParseOutputType ( request . params [ 0 ] . get_str ( ) , output_type ) ) {
2017-12-01 01:49:11 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , strprintf ( " Unknown address type '%s' " , request . params [ 0 ] . get_str ( ) ) ) ;
}
}
2016-10-25 09:45:57 +02:00
CReserveKey reservekey ( pwallet ) ;
2013-08-28 22:29:52 +02:00
CPubKey vchPubKey ;
2017-01-10 16:45:30 +01:00
if ( ! reservekey . GetReservedKey ( vchPubKey , true ) )
2014-06-16 14:45:32 +02:00
throw JSONRPCError ( RPC_WALLET_KEYPOOL_RAN_OUT , " Error: Keypool ran out, please call keypoolrefill first " ) ;
2013-08-28 22:29:52 +02:00
reservekey . KeepKey ( ) ;
2017-12-01 01:49:11 +01:00
pwallet - > LearnRelatedScripts ( vchPubKey , output_type ) ;
CTxDestination dest = GetDestinationForKey ( vchPubKey , output_type ) ;
2013-08-28 22:29:52 +02:00
2017-12-01 01:49:11 +01:00
return EncodeDestination ( dest ) ;
2013-08-28 22:29:52 +02:00
}
2012-08-21 16:38:57 +02:00
2018-05-02 17:14:48 +02:00
static UniValue setlabel ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2016-03-21 16:55:02 +01:00
if ( request . fHelp | | request . params . size ( ) ! = 2 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2017-10-20 19:27:55 +02:00
" setlabel \" address \" \" label \" \n "
" \n Sets the label associated with the given address. \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
2017-10-20 19:27:55 +02:00
" 1. \" address \" (string, required) The bitcoin address to be associated with a label. \n "
2016-03-21 16:55:02 +01:00
" 2. \" label \" (string, required) The label to assign to the address. \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
2017-10-20 19:27:55 +02:00
+ HelpExampleCli ( " setlabel " , " \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \" \" tabby \" " )
+ HelpExampleRpc ( " setlabel " , " \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \" , \" tabby \" " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
2018-08-23 02:46:59 +02:00
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2017-08-23 03:02:33 +02:00
CTxDestination dest = DecodeDestination ( request . params [ 0 ] . get_str ( ) ) ;
if ( ! IsValidDestination ( dest ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid Bitcoin address " ) ;
2017-08-23 03:02:33 +02:00
}
2012-08-21 16:38:57 +02:00
2016-03-21 16:55:02 +01:00
std : : string label = LabelFromValue ( request . params [ 1 ] ) ;
2012-08-21 16:38:57 +02:00
2017-08-23 03:02:33 +02:00
if ( IsMine ( * pwallet , dest ) ) {
2017-10-20 19:27:55 +02:00
pwallet - > SetAddressBook ( dest , label , " receive " ) ;
2016-03-21 16:55:02 +01:00
} else {
pwallet - > SetAddressBook ( dest , label , " send " ) ;
2012-08-21 16:38:57 +02:00
}
2014-08-20 21:15:16 +02:00
return NullUniValue ;
2012-08-21 16:38:57 +02:00
}
2017-07-31 21:30:21 +02:00
static CTransactionRef SendMoney ( interfaces : : Chain : : Lock & locked_chain , CWallet * const pwallet , const CTxDestination & address , CAmount nValue , bool fSubtractFeeFromAmount , const CCoinControl & coin_control , mapValue_t mapValue )
2014-12-01 20:06:45 +01:00
{
2016-09-09 07:32:12 +02:00
CAmount curBalance = pwallet - > GetBalance ( ) ;
2014-12-09 14:50:01 +01:00
2014-12-01 20:06:45 +01:00
// Check amount
if ( nValue < = 0 )
2014-12-05 17:22:06 +01:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid amount " ) ;
2014-12-01 20:06:45 +01:00
2014-12-09 14:50:01 +01:00
if ( nValue > curBalance )
2014-12-01 20:06:45 +01:00
throw JSONRPCError ( RPC_WALLET_INSUFFICIENT_FUNDS , " Insufficient funds " ) ;
2016-10-25 10:04:23 +02:00
if ( pwallet - > GetBroadcastTransactions ( ) & & ! g_connman ) {
2016-05-25 02:56:17 +02:00
throw JSONRPCError ( RPC_CLIENT_P2P_DISABLED , " Error: Peer-to-peer functionality missing or disabled " ) ;
2016-10-25 10:04:23 +02:00
}
2016-05-25 02:56:17 +02:00
2014-12-01 20:06:45 +01:00
// Parse Bitcoin address
CScript scriptPubKey = GetScriptForDestination ( address ) ;
// Create and send the transaction
2016-09-09 07:32:12 +02:00
CReserveKey reservekey ( pwallet ) ;
2014-12-01 20:06:45 +01:00
CAmount nFeeRequired ;
2014-12-09 14:50:01 +01:00
std : : string strError ;
2017-01-27 02:33:45 +01:00
std : : vector < CRecipient > vecSend ;
2014-07-23 14:34:36 +02:00
int nChangePosRet = - 1 ;
CRecipient recipient = { scriptPubKey , nValue , fSubtractFeeFromAmount } ;
vecSend . push_back ( recipient ) ;
2017-02-02 21:30:03 +01:00
CTransactionRef tx ;
2017-07-31 21:30:21 +02:00
if ( ! pwallet - > CreateTransaction ( locked_chain , vecSend , tx , reservekey , nFeeRequired , nChangePosRet , strError , coin_control ) ) {
2016-11-15 17:31:40 +01:00
if ( ! fSubtractFeeFromAmount & & nValue + nFeeRequired > curBalance )
2016-10-26 11:57:05 +02:00
strError = strprintf ( " Error: This transaction requires a transaction fee of at least %s " , FormatMoney ( nFeeRequired ) ) ;
2014-07-23 14:34:36 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , strError ) ;
2014-12-01 20:06:45 +01:00
}
2016-10-25 20:27:04 +02:00
CValidationState state ;
2018-07-31 18:23:26 +02:00
if ( ! pwallet - > CommitTransaction ( tx , std : : move ( mapValue ) , { } /* orderForm */ , reservekey , g_connman . get ( ) , state ) ) {
2018-02-07 00:47:51 +01:00
strError = strprintf ( " Error: The transaction was rejected! Reason given: %s " , FormatStateMessage ( state ) ) ;
2016-10-25 20:27:04 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , strError ) ;
}
2017-02-02 21:30:03 +01:00
return tx ;
2014-12-01 20:06:45 +01:00
}
2018-05-02 17:14:48 +02:00
static UniValue sendtoaddress ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2017-06-14 21:15:40 +02:00
if ( request . fHelp | | request . params . size ( ) < 2 | | request . params . size ( ) > 8 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2017-06-14 21:15:40 +02:00
" sendtoaddress \" address \" amount ( \" comment \" \" comment_to \" subtractfeefromamount replaceable conf_target \" estimate_mode \" ) \n "
2016-01-13 11:43:07 +01:00
" \n Send an amount to a given address. \n "
2016-10-25 09:45:57 +02:00
+ HelpRequiringPassphrase ( pwallet ) +
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
2016-12-09 18:06:42 +01:00
" 1. \" address \" (string, required) The bitcoin address to send to. \n "
" 2. \" amount \" (numeric or string, required) The amount in " + CURRENCY_UNIT + " to send. eg 0.1 \n "
" 3. \" comment \" (string, optional) A comment used to store what the transaction is for. \n "
2013-10-29 12:29:44 +01:00
" This is not part of the transaction, just kept in your wallet. \n "
2016-12-09 18:06:42 +01:00
" 4. \" comment_to \" (string, optional) A comment to store the name of the person or organization \n "
2013-10-29 12:29:44 +01:00
" to which you're sending the transaction. This is not part of the \n "
" transaction, just kept in your wallet. \n "
2014-07-23 14:34:36 +02:00
" 5. subtractfeefromamount (boolean, optional, default=false) The fee will be deducted from the amount being sent. \n "
" The recipient will receive less bitcoins than you enter in the amount field. \n "
2017-06-14 21:15:40 +02:00
" 6. replaceable (boolean, optional) Allow this transaction to be replaced by a transaction with higher fees via BIP 125 \n "
" 7. conf_target (numeric, optional) Confirmation target (in blocks) \n "
" 8. \" estimate_mode \" (string, optional, default=UNSET) The fee estimate mode, must be one of: \n "
" \" UNSET \" \n "
" \" ECONOMICAL \" \n "
" \" CONSERVATIVE \" \n "
2013-10-29 12:29:44 +01:00
" \n Result: \n "
2016-12-09 18:06:42 +01:00
" \" txid \" (string) The transaction id. \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
+ HelpExampleCli ( " sendtoaddress " , " \" 1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd \" 0.1 " )
+ HelpExampleCli ( " sendtoaddress " , " \" 1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd \" 0.1 \" donation \" \" seans outpost \" " )
2014-07-23 14:34:36 +02:00
+ HelpExampleCli ( " sendtoaddress " , " \" 1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd \" 0.1 \" \" \" \" true " )
2013-10-29 12:29:44 +01:00
+ HelpExampleRpc ( " sendtoaddress " , " \" 1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd \" , 0.1, \" donation \" , \" seans outpost \" " )
) ;
2012-08-21 16:38:57 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2017-08-23 03:02:33 +02:00
CTxDestination dest = DecodeDestination ( request . params [ 0 ] . get_str ( ) ) ;
if ( ! IsValidDestination ( dest ) ) {
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid address " ) ;
}
2012-08-21 16:38:57 +02:00
// Amount
2016-09-22 09:46:41 +02:00
CAmount nAmount = AmountFromValue ( request . params [ 1 ] ) ;
2015-06-06 09:41:12 +02:00
if ( nAmount < = 0 )
throw JSONRPCError ( RPC_TYPE_ERROR , " Invalid amount for send " ) ;
2012-08-21 16:38:57 +02:00
// Wallet comments
2017-02-02 21:30:03 +01:00
mapValue_t mapValue ;
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 2 ] . isNull ( ) & & ! request . params [ 2 ] . get_str ( ) . empty ( ) )
2017-02-02 21:30:03 +01:00
mapValue [ " comment " ] = request . params [ 2 ] . get_str ( ) ;
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 3 ] . isNull ( ) & & ! request . params [ 3 ] . get_str ( ) . empty ( ) )
2017-02-02 21:30:03 +01:00
mapValue [ " to " ] = request . params [ 3 ] . get_str ( ) ;
2012-08-21 16:38:57 +02:00
2014-07-23 14:34:36 +02:00
bool fSubtractFeeFromAmount = false ;
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 4 ] . isNull ( ) ) {
2016-09-22 09:46:41 +02:00
fSubtractFeeFromAmount = request . params [ 4 ] . get_bool ( ) ;
2017-06-14 21:15:40 +02:00
}
CCoinControl coin_control ;
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 5 ] . isNull ( ) ) {
2018-04-07 18:12:46 +02:00
coin_control . m_signal_bip125_rbf = request . params [ 5 ] . get_bool ( ) ;
2017-06-14 21:15:40 +02:00
}
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 6 ] . isNull ( ) ) {
2017-07-12 20:42:57 +02:00
coin_control . m_confirm_target = ParseConfirmTarget ( request . params [ 6 ] ) ;
2017-06-14 21:15:40 +02:00
}
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 7 ] . isNull ( ) ) {
2017-06-14 21:15:40 +02:00
if ( ! FeeModeFromString ( request . params [ 7 ] . get_str ( ) , coin_control . m_fee_mode ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid estimate_mode parameter " ) ;
}
}
2014-07-23 14:34:36 +02:00
2016-10-25 09:45:57 +02:00
EnsureWalletIsUnlocked ( pwallet ) ;
2012-08-21 16:38:57 +02:00
2017-07-31 21:30:21 +02:00
CTransactionRef tx = SendMoney ( * locked_chain , pwallet , dest , nAmount , fSubtractFeeFromAmount , coin_control , std : : move ( mapValue ) ) ;
2017-02-02 21:30:03 +01:00
return tx - > GetHash ( ) . GetHex ( ) ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue listaddressgroupings ( const JSONRPCRequest & request )
2012-08-01 18:48:42 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2017-07-10 17:43:17 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 0 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2012-08-20 19:43:33 +02:00
" listaddressgroupings \n "
2013-10-29 12:29:44 +01:00
" \n Lists groups of addresses which have had their common ownership \n "
2012-08-20 19:43:33 +02:00
" made public by common use as inputs or as the resulting change \n "
2013-10-29 12:29:44 +01:00
" in past transactions \n "
" \n Result: \n "
" [ \n "
" [ \n "
" [ \n "
2017-01-11 14:29:46 +01:00
" \" address \" , (string) The bitcoin address \n "
2015-08-01 21:15:23 +02:00
" amount, (numeric) The amount in " + CURRENCY_UNIT + " \n "
2017-10-20 19:27:55 +02:00
" \" label \" (string, optional) The label \n "
2013-10-29 12:29:44 +01:00
" ] \n "
" ,... \n "
" ] \n "
" ,... \n "
" ] \n "
" \n Examples: \n "
+ HelpExampleCli ( " listaddressgroupings " , " " )
+ HelpExampleRpc ( " listaddressgroupings " , " " )
) ;
2012-08-01 18:48:42 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2015-05-10 14:48:35 +02:00
UniValue jsonGroupings ( UniValue : : VARR ) ;
2017-07-31 21:30:21 +02:00
std : : map < CTxDestination , CAmount > balances = pwallet - > GetAddressBalances ( * locked_chain ) ;
2017-08-23 03:02:33 +02:00
for ( const std : : set < CTxDestination > & grouping : pwallet - > GetAddressGroupings ( ) ) {
2015-05-10 14:48:35 +02:00
UniValue jsonGrouping ( UniValue : : VARR ) ;
2017-08-23 03:02:33 +02:00
for ( const CTxDestination & address : grouping )
2012-08-01 18:48:42 +02:00
{
2015-05-10 14:48:35 +02:00
UniValue addressInfo ( UniValue : : VARR ) ;
2017-08-23 03:02:33 +02:00
addressInfo . push_back ( EncodeDestination ( address ) ) ;
2012-08-01 18:48:42 +02:00
addressInfo . push_back ( ValueFromAmount ( balances [ address ] ) ) ;
{
2017-08-23 03:02:33 +02:00
if ( pwallet - > mapAddressBook . find ( address ) ! = pwallet - > mapAddressBook . end ( ) ) {
addressInfo . push_back ( pwallet - > mapAddressBook . find ( address ) - > second . name ) ;
2016-10-25 10:04:23 +02:00
}
2012-08-01 18:48:42 +02:00
}
jsonGrouping . push_back ( addressInfo ) ;
}
jsonGroupings . push_back ( jsonGrouping ) ;
}
return jsonGroupings ;
}
2018-05-02 17:14:48 +02:00
static UniValue signmessage ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 2 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2016-11-21 14:39:58 +01:00
" signmessage \" address \" \" message \" \n "
2013-10-29 12:29:44 +01:00
" \n Sign a message with the private key of an address "
2016-10-25 09:45:57 +02:00
+ HelpRequiringPassphrase ( pwallet ) + " \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
2016-12-09 18:06:42 +01:00
" 1. \" address \" (string, required) The bitcoin address to use for the private key. \n "
2013-10-29 12:29:44 +01:00
" 2. \" message \" (string, required) The message to create a signature of. \n "
" \n Result: \n "
" \" signature \" (string) The signature of the message encoded in base 64 \n "
" \n Examples: \n "
" \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-10-29 12:29:44 +01:00
" \n Verify the signature \n "
2017-02-26 14:01:05 +01:00
+ HelpExampleCli ( " verifymessage " , " \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \" \" signature \" \" my message \" " ) +
2018-10-02 21:49:18 +02:00
" \n As a JSON-RPC call \n "
2017-02-26 14:01:05 +01:00
+ HelpExampleRpc ( " signmessage " , " \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \" , \" my message \" " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2016-10-25 09:45:57 +02:00
EnsureWalletIsUnlocked ( pwallet ) ;
2012-08-21 16:38:57 +02:00
2017-01-27 02:33:45 +01:00
std : : string strAddress = request . params [ 0 ] . get_str ( ) ;
std : : string strMessage = request . params [ 1 ] . get_str ( ) ;
2012-08-21 16:38:57 +02:00
2017-08-23 03:02:33 +02:00
CTxDestination dest = DecodeDestination ( strAddress ) ;
if ( ! IsValidDestination ( dest ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_TYPE_ERROR , " Invalid address " ) ;
2017-08-23 03:02:33 +02:00
}
2012-08-21 16:38:57 +02:00
2017-08-23 03:02:33 +02:00
const CKeyID * keyID = boost : : get < CKeyID > ( & dest ) ;
if ( ! keyID ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_TYPE_ERROR , " Address does not refer to key " ) ;
2017-08-23 03:02:33 +02:00
}
2012-08-21 16:38:57 +02:00
CKey key ;
2017-08-23 03:02:33 +02:00
if ( ! pwallet - > GetKey ( * keyID , key ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , " Private key not available " ) ;
2016-10-25 10:04:23 +02:00
}
2012-08-21 16:38:57 +02:00
2012-10-19 23:13:44 +02:00
CHashWriter ss ( SER_GETHASH , 0 ) ;
2012-08-21 16:38:57 +02:00
ss < < strMessageMagic ;
ss < < strMessage ;
2017-01-27 02:33:45 +01:00
std : : vector < unsigned char > vchSig ;
2012-10-19 23:13:44 +02:00
if ( ! key . SignCompact ( ss . GetHash ( ) , vchSig ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Sign failed " ) ;
2012-08-21 16:38:57 +02:00
2017-07-11 11:37:53 +02:00
return EncodeBase64 ( vchSig . data ( ) , vchSig . size ( ) ) ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue getreceivedbyaddress ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) < 1 | | request . params . size ( ) > 2 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2016-11-21 14:39:58 +01:00
" getreceivedbyaddress \" address \" ( minconf ) \n "
" \n Returns the total amount received by the given address in transactions with at least minconf confirmations. \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
2016-12-09 18:06:42 +01:00
" 1. \" address \" (string, required) The bitcoin address for transactions. \n "
2013-10-29 12:29:44 +01:00
" 2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times. \n "
" \n Result: \n "
2015-08-01 21:15:23 +02:00
" amount (numeric) The total amount in " + CURRENCY_UNIT + " received at this address. \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
" \n The amount from transactions with at least 1 confirmation \n "
2017-02-26 14:01:05 +01:00
+ HelpExampleCli ( " getreceivedbyaddress " , " \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \" " ) +
2013-10-29 12:29:44 +01:00
" \n The amount including unconfirmed transactions, zero confirmations \n "
2017-02-26 14:01:05 +01:00
+ HelpExampleCli ( " getreceivedbyaddress " , " \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \" 0 " ) +
2017-07-19 22:50:26 +02:00
" \n The amount with at least 6 confirmations \n "
2017-02-26 14:01:05 +01:00
+ HelpExampleCli ( " getreceivedbyaddress " , " \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \" 6 " ) +
2018-10-02 21:49:18 +02:00
" \n As a JSON-RPC call \n "
2017-02-26 14:01:05 +01:00
+ HelpExampleRpc ( " getreceivedbyaddress " , " \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \" , 6 " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-31 21:30:21 +02:00
LockAnnotation lock ( : : cs_main ) ; // Temporary, for CheckFinalTx below. Removed in upcoming commit.
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2012-08-21 16:38:57 +02:00
// Bitcoin address
2017-08-23 03:02:33 +02:00
CTxDestination dest = DecodeDestination ( request . params [ 0 ] . get_str ( ) ) ;
if ( ! IsValidDestination ( dest ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid Bitcoin address " ) ;
2017-08-23 03:02:33 +02:00
}
CScript scriptPubKey = GetScriptForDestination ( dest ) ;
2016-10-25 10:04:23 +02:00
if ( ! IsMine ( * pwallet , scriptPubKey ) ) {
2017-08-15 17:33:39 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , " Address not found in wallet " ) ;
2016-10-25 10:04:23 +02:00
}
2012-08-21 16:38:57 +02:00
// Minimum confirmations
int nMinDepth = 1 ;
2017-07-10 17:44:39 +02:00
if ( ! request . params [ 1 ] . isNull ( ) )
2016-09-22 09:46:41 +02:00
nMinDepth = request . params [ 1 ] . get_int ( ) ;
2012-08-21 16:38:57 +02:00
// Tally
2014-04-23 00:46:19 +02:00
CAmount nAmount = 0 ;
2018-05-16 00:41:53 +02:00
for ( const std : : pair < const uint256 , CWalletTx > & pairWtx : pwallet - > mapWallet ) {
2016-10-25 10:04:23 +02:00
const CWalletTx & wtx = pairWtx . second ;
2016-11-12 01:54:51 +01:00
if ( wtx . IsCoinBase ( ) | | ! CheckFinalTx ( * wtx . tx ) )
2012-08-21 16:38:57 +02:00
continue ;
2017-06-02 03:18:57 +02:00
for ( const CTxOut & txout : wtx . tx - > vout )
2012-08-21 16:38:57 +02:00
if ( txout . scriptPubKey = = scriptPubKey )
2017-07-31 21:30:21 +02:00
if ( wtx . GetDepthInMainChain ( * locked_chain ) > = nMinDepth )
2012-08-21 16:38:57 +02:00
nAmount + = txout . nValue ;
}
return ValueFromAmount ( nAmount ) ;
}
2018-05-02 17:14:48 +02:00
static UniValue getreceivedbylabel ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) < 1 | | request . params . size ( ) > 2 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2017-10-20 19:27:55 +02:00
" getreceivedbylabel \" label \" ( minconf ) \n "
" \n Returns the total amount received by addresses with <label> in transactions with at least [minconf] confirmations. \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
2017-10-20 19:27:55 +02:00
" 1. \" label \" (string, required) The selected label, may be the default label using \" \" . \n "
2013-10-29 12:29:44 +01:00
" 2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times. \n "
" \n Result: \n "
2017-10-20 19:27:55 +02:00
" amount (numeric) The total amount in " + CURRENCY_UNIT + " received for this label. \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
2017-10-20 19:27:55 +02:00
" \n Amount received by the default label with at least 1 confirmation \n "
+ HelpExampleCli ( " getreceivedbylabel " , " \" \" " ) +
" \n Amount received at the tabby label including unconfirmed amounts with zero confirmations \n "
+ HelpExampleCli ( " getreceivedbylabel " , " \" tabby \" 0 " ) +
2017-07-19 22:50:26 +02:00
" \n The amount with at least 6 confirmations \n "
2017-10-20 19:27:55 +02:00
+ HelpExampleCli ( " getreceivedbylabel " , " \" tabby \" 6 " ) +
2018-10-02 21:49:18 +02:00
" \n As a JSON-RPC call \n "
2017-10-20 19:27:55 +02:00
+ HelpExampleRpc ( " getreceivedbylabel " , " \" tabby \" , 6 " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-31 21:30:21 +02:00
LockAnnotation lock ( : : cs_main ) ; // Temporary, for CheckFinalTx below. Removed in upcoming commit.
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2012-08-21 16:38:57 +02:00
// Minimum confirmations
int nMinDepth = 1 ;
2017-07-10 17:44:39 +02:00
if ( ! request . params [ 1 ] . isNull ( ) )
2016-09-22 09:46:41 +02:00
nMinDepth = request . params [ 1 ] . get_int ( ) ;
2012-08-21 16:38:57 +02:00
2017-10-20 19:27:55 +02:00
// Get the set of pub keys assigned to label
std : : string label = LabelFromValue ( request . params [ 0 ] ) ;
std : : set < CTxDestination > setAddress = pwallet - > GetLabelAddresses ( label ) ;
2012-08-21 16:38:57 +02:00
// Tally
2014-04-23 00:46:19 +02:00
CAmount nAmount = 0 ;
2018-05-16 00:41:53 +02:00
for ( const std : : pair < const uint256 , CWalletTx > & pairWtx : pwallet - > mapWallet ) {
2016-10-25 10:04:23 +02:00
const CWalletTx & wtx = pairWtx . second ;
2016-11-12 01:54:51 +01:00
if ( wtx . IsCoinBase ( ) | | ! CheckFinalTx ( * wtx . tx ) )
2012-08-21 16:38:57 +02:00
continue ;
2017-06-02 03:18:57 +02:00
for ( const CTxOut & txout : wtx . tx - > vout )
2012-08-21 16:38:57 +02:00
{
CTxDestination address ;
2016-10-25 10:04:23 +02:00
if ( ExtractDestination ( txout . scriptPubKey , address ) & & IsMine ( * pwallet , address ) & & setAddress . count ( address ) ) {
2017-07-31 21:30:21 +02:00
if ( wtx . GetDepthInMainChain ( * locked_chain ) > = nMinDepth )
2012-08-21 16:38:57 +02:00
nAmount + = txout . nValue ;
2016-10-25 10:04:23 +02:00
}
2012-08-21 16:38:57 +02:00
}
}
2016-07-08 12:01:39 +02:00
return ValueFromAmount ( nAmount ) ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue getbalance ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2018-06-26 23:21:30 +02:00
if ( request . fHelp | | ( request . params . size ( ) > 3 ) )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2018-11-13 19:42:36 +01:00
" getbalance ( \" dummy \" minconf include_watchonly ) \n "
2018-06-26 23:21:30 +02:00
" \n Returns the total available balance. \n "
" The available balance is what the wallet considers currently spendable, and is \n "
" thus affected by options which limit spendability such as -spendzeroconfchange. \n "
" \n Arguments: \n "
" 1. (dummy) (string, optional) Remains for backward compatibility. Must be excluded or set to \" * \" . \n "
2018-07-27 23:05:24 +02:00
" 2. minconf (numeric, optional, default=0) Only include transactions confirmed at least this many times. \n "
2018-06-26 23:21:30 +02:00
" 3. include_watchonly (bool, optional, default=false) Also include balance in watch-only addresses (see 'importaddress') \n "
2013-10-29 12:29:44 +01:00
" \n Result: \n "
2018-07-27 23:05:24 +02:00
" amount (numeric) The total amount in " + CURRENCY_UNIT + " received for this wallet. \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
2017-07-12 20:40:02 +02:00
" \n The total amount in the wallet with 1 or more confirmations \n "
2013-10-29 12:29:44 +01:00
+ HelpExampleCli ( " getbalance " , " " ) +
2017-07-12 20:40:02 +02:00
" \n The total amount in the wallet at least 6 blocks confirmed \n "
2014-01-29 13:41:34 +01:00
+ HelpExampleCli ( " getbalance " , " \" * \" 6 " ) +
2018-10-02 21:49:18 +02:00
" \n As a JSON-RPC call \n "
2014-12-30 15:32:07 +01:00
+ HelpExampleRpc ( " getbalance " , " \" * \" , 6 " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2018-07-27 23:05:24 +02:00
const UniValue & dummy_value = request . params [ 0 ] ;
if ( ! dummy_value . isNull ( ) & & dummy_value . get_str ( ) ! = " * " ) {
throw JSONRPCError ( RPC_METHOD_DEPRECATED , " dummy first argument must be excluded or set to \" * \" . " ) ;
}
2017-08-15 21:47:27 +02:00
2018-06-26 23:21:30 +02:00
int min_depth = 0 ;
if ( ! request . params [ 1 ] . isNull ( ) ) {
min_depth = request . params [ 1 ] . get_int ( ) ;
}
isminefilter filter = ISMINE_SPENDABLE ;
if ( ! request . params [ 2 ] . isNull ( ) & & request . params [ 2 ] . get_bool ( ) ) {
filter = filter | ISMINE_WATCH_ONLY ;
}
return ValueFromAmount ( pwallet - > GetBalance ( filter , min_depth ) ) ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue getunconfirmedbalance ( const JSONRPCRequest & request )
2013-11-24 12:48:52 +01:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) > 0 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2013-11-24 12:48:52 +01:00
" getunconfirmedbalance \n "
" Returns the server's total unconfirmed balance \n " ) ;
2014-10-19 10:46:17 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2016-10-25 09:45:57 +02:00
return ValueFromAmount ( pwallet - > GetUnconfirmedBalance ( ) ) ;
2013-11-24 12:48:52 +01:00
}
2012-08-21 16:38:57 +02:00
2018-05-02 17:14:48 +02:00
static UniValue sendmany ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2018-07-27 23:05:24 +02:00
if ( request . fHelp | | request . params . size ( ) < 2 | | request . params . size ( ) > 8 )
throw std : : runtime_error (
" sendmany \" \" { \" address \" :amount,...} ( minconf \" comment \" [ \" address \" ,...] replaceable conf_target \" estimate_mode \" ) \n "
2018-04-16 20:01:52 +02:00
" \n Send multiple times. Amounts are double-precision floating point numbers. \n "
+ HelpRequiringPassphrase ( pwallet ) + " \n "
" \n Arguments: \n "
" 1. \" dummy \" (string, required) Must be set to \" \" for backwards compatibility. \n "
" 2. \" amounts \" (string, required) A json object with addresses and amounts \n "
" { \n "
" \" address \" :amount (numeric or string) The bitcoin address is the key, the numeric amount (can be string) in " + CURRENCY_UNIT + " is the value \n "
" ,... \n "
" } \n "
" 3. minconf (numeric, optional, default=1) Only use the balance confirmed at least this many times. \n "
" 4. \" comment \" (string, optional) A comment \n "
" 5. subtractfeefrom (array, optional) A json array with addresses. \n "
" The fee will be equally deducted from the amount of each selected address. \n "
" Those recipients will receive less bitcoins than you enter in their corresponding amount field. \n "
" If no addresses are specified here, the sender pays the fee. \n "
" [ \n "
" \" address \" (string) Subtract fee from this address \n "
" ,... \n "
" ] \n "
" 6. replaceable (boolean, optional) Allow this transaction to be replaced by a transaction with higher fees via BIP 125 \n "
" 7. conf_target (numeric, optional) Confirmation target (in blocks) \n "
" 8. \" estimate_mode \" (string, optional, default=UNSET) The fee estimate mode, must be one of: \n "
" \" UNSET \" \n "
" \" ECONOMICAL \" \n "
" \" CONSERVATIVE \" \n "
" \n Result: \n "
" \" txid \" (string) The transaction id for the send. Only 1 transaction is created regardless of \n "
" the number of addresses. \n "
" \n Examples: \n "
" \n Send two amounts to two different addresses: \n "
+ HelpExampleCli ( " sendmany " , " \" \" \" { \\ \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \\ \" :0.01, \\ \" 1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz \\ \" :0.02} \" " ) +
" \n Send two amounts to two different addresses setting the confirmation and comment: \n "
+ HelpExampleCli ( " sendmany " , " \" \" \" { \\ \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \\ \" :0.01, \\ \" 1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz \\ \" :0.02} \" 6 \" testing \" " ) +
" \n Send two amounts to two different addresses, subtract fee from amount: \n "
+ HelpExampleCli ( " sendmany " , " \" \" \" { \\ \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \\ \" :0.01, \\ \" 1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz \\ \" :0.02} \" 1 \" \" \" [ \\ \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \\ \" , \\ \" 1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz \\ \" ] \" " ) +
2018-10-02 21:49:18 +02:00
" \n As a JSON-RPC call \n "
2018-07-27 23:05:24 +02:00
+ HelpExampleRpc ( " sendmany " , " \" \" , { \" 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XX \" :0.01, \" 1353tsE8YMTA4EuV7dgUXGjNFf9KpVvKHz \" :0.02}, 6, \" testing \" " )
) ;
2012-08-21 16:38:57 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2016-10-25 10:04:23 +02:00
if ( pwallet - > GetBroadcastTransactions ( ) & & ! g_connman ) {
2016-05-25 02:56:17 +02:00
throw JSONRPCError ( RPC_CLIENT_P2P_DISABLED , " Error: Peer-to-peer functionality missing or disabled " ) ;
2016-10-25 10:04:23 +02:00
}
2016-05-25 02:56:17 +02:00
2018-07-27 23:05:24 +02:00
if ( ! request . params [ 0 ] . isNull ( ) & & ! request . params [ 0 ] . get_str ( ) . empty ( ) ) {
2018-04-16 20:01:52 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Dummy value must be set to \" \" " ) ;
}
2016-09-22 09:46:41 +02:00
UniValue sendTo = request . params [ 1 ] . get_obj ( ) ;
2012-08-21 16:38:57 +02:00
int nMinDepth = 1 ;
2017-07-10 17:44:39 +02:00
if ( ! request . params [ 2 ] . isNull ( ) )
2016-09-22 09:46:41 +02:00
nMinDepth = request . params [ 2 ] . get_int ( ) ;
2012-08-21 16:38:57 +02:00
2017-02-02 21:30:03 +01:00
mapValue_t mapValue ;
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 3 ] . isNull ( ) & & ! request . params [ 3 ] . get_str ( ) . empty ( ) )
2017-02-02 21:30:03 +01:00
mapValue [ " comment " ] = request . params [ 3 ] . get_str ( ) ;
2012-08-21 16:38:57 +02:00
2015-05-10 14:48:35 +02:00
UniValue subtractFeeFromAmount ( UniValue : : VARR ) ;
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 4 ] . isNull ( ) )
2016-09-22 09:46:41 +02:00
subtractFeeFromAmount = request . params [ 4 ] . get_array ( ) ;
2014-07-23 14:34:36 +02:00
2017-06-14 21:15:40 +02:00
CCoinControl coin_control ;
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 5 ] . isNull ( ) ) {
2018-04-07 18:12:46 +02:00
coin_control . m_signal_bip125_rbf = request . params [ 5 ] . get_bool ( ) ;
2017-06-14 21:15:40 +02:00
}
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 6 ] . isNull ( ) ) {
2017-07-12 20:42:57 +02:00
coin_control . m_confirm_target = ParseConfirmTarget ( request . params [ 6 ] ) ;
2017-06-14 21:15:40 +02:00
}
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 7 ] . isNull ( ) ) {
2017-06-14 21:15:40 +02:00
if ( ! FeeModeFromString ( request . params [ 7 ] . get_str ( ) , coin_control . m_fee_mode ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid estimate_mode parameter " ) ;
}
}
2017-08-23 03:02:33 +02:00
std : : set < CTxDestination > destinations ;
2017-01-27 02:33:45 +01:00
std : : vector < CRecipient > vecSend ;
2012-08-21 16:38:57 +02:00
2014-04-23 00:46:19 +02:00
CAmount totalAmount = 0 ;
2017-01-27 02:33:45 +01:00
std : : vector < std : : string > keys = sendTo . getKeys ( ) ;
2017-08-23 03:02:33 +02:00
for ( const std : : string & name_ : keys ) {
CTxDestination dest = DecodeDestination ( name_ ) ;
if ( ! IsValidDestination ( dest ) ) {
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , std : : string ( " Invalid Bitcoin address: " ) + name_ ) ;
}
2012-08-21 16:38:57 +02:00
2017-08-23 03:02:33 +02:00
if ( destinations . count ( dest ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , std : : string ( " Invalid parameter, duplicated address: " ) + name_ ) ;
}
destinations . insert ( dest ) ;
2012-08-21 16:38:57 +02:00
2017-08-23 03:02:33 +02:00
CScript scriptPubKey = GetScriptForDestination ( dest ) ;
2014-08-20 21:15:16 +02:00
CAmount nAmount = AmountFromValue ( sendTo [ name_ ] ) ;
2015-06-06 09:41:12 +02:00
if ( nAmount < = 0 )
throw JSONRPCError ( RPC_TYPE_ERROR , " Invalid amount for send " ) ;
2012-08-21 16:38:57 +02:00
totalAmount + = nAmount ;
2014-07-23 14:34:36 +02:00
bool fSubtractFeeFromAmount = false ;
2015-05-10 13:35:44 +02:00
for ( unsigned int idx = 0 ; idx < subtractFeeFromAmount . size ( ) ; idx + + ) {
2015-05-18 14:02:18 +02:00
const UniValue & addr = subtractFeeFromAmount [ idx ] ;
2015-05-10 13:35:44 +02:00
if ( addr . get_str ( ) = = name_ )
2014-07-23 14:34:36 +02:00
fSubtractFeeFromAmount = true ;
2015-05-10 13:35:44 +02:00
}
2014-07-23 14:34:36 +02:00
CRecipient recipient = { scriptPubKey , nAmount , fSubtractFeeFromAmount } ;
vecSend . push_back ( recipient ) ;
2012-08-21 16:38:57 +02:00
}
2016-10-25 09:45:57 +02:00
EnsureWalletIsUnlocked ( pwallet ) ;
2012-08-21 16:38:57 +02:00
// Check funds
2018-07-31 18:23:26 +02:00
if ( totalAmount > pwallet - > GetLegacyBalance ( ISMINE_SPENDABLE , nMinDepth ) ) {
2018-06-18 18:17:42 +02:00
throw JSONRPCError ( RPC_WALLET_INSUFFICIENT_FUNDS , " Wallet has insufficient funds " ) ;
}
2012-08-21 16:38:57 +02:00
2018-03-16 19:33:29 +01:00
// Shuffle recipient list
std : : shuffle ( vecSend . begin ( ) , vecSend . end ( ) , FastRandomContext ( ) ) ;
2012-08-21 16:38:57 +02:00
// Send
2016-10-25 09:45:57 +02:00
CReserveKey keyChange ( pwallet ) ;
2014-04-23 00:46:19 +02:00
CAmount nFeeRequired = 0 ;
2014-07-23 14:34:36 +02:00
int nChangePosRet = - 1 ;
2017-01-27 02:33:45 +01:00
std : : string strFailReason ;
2017-02-02 21:30:03 +01:00
CTransactionRef tx ;
2017-07-31 21:30:21 +02:00
bool fCreated = pwallet - > CreateTransaction ( * locked_chain , vecSend , tx , keyChange , nFeeRequired , nChangePosRet , strFailReason , coin_control ) ;
2012-08-21 16:38:57 +02:00
if ( ! fCreated )
2013-04-25 23:31:22 +02:00
throw JSONRPCError ( RPC_WALLET_INSUFFICIENT_FUNDS , strFailReason ) ;
2016-10-25 20:27:04 +02:00
CValidationState state ;
2018-07-31 18:23:26 +02:00
if ( ! pwallet - > CommitTransaction ( tx , std : : move ( mapValue ) , { } /* orderForm */ , keyChange , g_connman . get ( ) , state ) ) {
2018-02-07 00:47:51 +01:00
strFailReason = strprintf ( " Transaction commit failed:: %s " , FormatStateMessage ( state ) ) ;
2016-10-25 20:27:04 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , strFailReason ) ;
}
2012-08-21 16:38:57 +02:00
2017-02-02 21:30:03 +01:00
return tx - > GetHash ( ) . GetHex ( ) ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue addmultisigaddress ( const JSONRPCRequest & request )
2012-09-10 15:32:40 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2018-01-24 15:26:41 +01:00
if ( request . fHelp | | request . params . size ( ) < 2 | | request . params . size ( ) > 4 ) {
2017-10-20 19:27:55 +02:00
std : : string msg = " addmultisigaddress nrequired [ \" key \" ,...] ( \" label \" \" address_type \" ) \n "
2017-09-08 14:19:25 +02:00
" \n Add a nrequired-to-sign multisignature address to the wallet. Requires a new wallet backup. \n "
2013-10-29 12:29:44 +01:00
" Each key is a Bitcoin address or hex-encoded public key. \n "
2017-06-27 05:34:25 +02:00
" This functionality is only intended for use with non-watchonly addresses. \n "
" See `importaddress` for watchonly p2sh address support. \n "
2017-10-20 19:27:55 +02:00
" If 'label' is specified, assign address to that label. \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
2017-09-29 06:21:28 +02:00
" 1. nrequired (numeric, required) The number of required signatures out of the n keys or addresses. \n "
" 2. \" keys \" (string, required) A json array of bitcoin addresses or hex-encoded public keys \n "
2013-10-29 12:29:44 +01:00
" [ \n "
2017-09-29 06:21:28 +02:00
" \" address \" (string) bitcoin address or hex-encoded public key \n "
2013-10-29 12:29:44 +01:00
" ..., \n "
" ] \n "
2017-10-20 19:27:55 +02:00
" 3. \" label \" (string, optional) A label to assign the addresses to. \n "
2018-01-24 15:26:41 +01:00
" 4. \" address_type \" (string, optional) The address type to use. Options are \" legacy \" , \" p2sh-segwit \" , and \" bech32 \" . Default is set by -addresstype. \n "
2013-10-29 12:29:44 +01:00
" \n Result: \n "
2017-09-29 06:21:28 +02: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 "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
" \n Add a multisig address from 2 addresses \n "
+ HelpExampleCli ( " addmultisigaddress " , " 2 \" [ \\ \" 16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5 \\ \" , \\ \" 171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV \\ \" ] \" " ) +
2018-10-02 21:49:18 +02:00
" \n As a JSON-RPC call \n "
2013-10-29 12:29:44 +01:00
+ HelpExampleRpc ( " addmultisigaddress " , " 2, \" [ \\ \" 16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5 \\ \" , \\ \" 171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV \\ \" ] \" " )
;
2017-01-27 02:33:45 +01:00
throw std : : runtime_error ( msg ) ;
2012-09-10 15:32:40 +02:00
}
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2017-10-20 19:27:55 +02:00
std : : string label ;
2017-08-15 01:44:02 +02:00
if ( ! request . params [ 2 ] . isNull ( ) )
2017-10-20 19:27:55 +02:00
label = LabelFromValue ( request . params [ 2 ] ) ;
2012-08-21 16:38:57 +02:00
2017-09-29 06:21:28 +02:00
int required = request . params [ 0 ] . get_int ( ) ;
// Get the public keys
const UniValue & keys_or_addrs = request . params [ 1 ] . get_array ( ) ;
std : : vector < CPubKey > pubkeys ;
for ( unsigned int i = 0 ; i < keys_or_addrs . size ( ) ; + + i ) {
if ( IsHex ( keys_or_addrs [ i ] . get_str ( ) ) & & ( keys_or_addrs [ i ] . get_str ( ) . length ( ) = = 66 | | keys_or_addrs [ i ] . get_str ( ) . length ( ) = = 130 ) ) {
pubkeys . push_back ( HexToPubKey ( keys_or_addrs [ i ] . get_str ( ) ) ) ;
} else {
pubkeys . push_back ( AddrToPubKey ( pwallet , keys_or_addrs [ i ] . get_str ( ) ) ) ;
}
}
2018-02-11 03:06:35 +01:00
OutputType output_type = pwallet - > m_default_address_type ;
2018-01-24 15:26:41 +01:00
if ( ! request . params [ 3 ] . isNull ( ) ) {
2018-03-19 20:57:11 +01:00
if ( ! ParseOutputType ( request . params [ 3 ] . get_str ( ) , output_type ) ) {
2018-01-24 15:26:41 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , strprintf ( " Unknown address type '%s' " , request . params [ 3 ] . get_str ( ) ) ) ;
}
}
2012-08-21 16:38:57 +02:00
// Construct using pay-to-script-hash:
2017-09-29 06:21:28 +02:00
CScript inner = CreateMultisigRedeemscript ( required , pubkeys ) ;
2018-07-09 11:30:39 +02:00
CTxDestination dest = AddAndGetDestinationForScript ( * pwallet , inner , output_type ) ;
2017-10-20 19:27:55 +02:00
pwallet - > SetAddressBook ( dest , label , " send " ) ;
2017-09-29 06:21:28 +02:00
UniValue result ( UniValue : : VOBJ ) ;
result . pushKV ( " address " , EncodeDestination ( dest ) ) ;
result . pushKV ( " redeemScript " , HexStr ( inner . begin ( ) , inner . end ( ) ) ) ;
return result ;
2012-08-21 16:38:57 +02:00
}
struct tallyitem
{
2014-04-23 00:46:19 +02:00
CAmount nAmount ;
2012-08-21 16:38:57 +02:00
int nConf ;
2017-01-27 02:33:45 +01:00
std : : vector < uint256 > txids ;
2014-05-31 16:57:26 +02:00
bool fIsWatchonly ;
2012-08-21 16:38:57 +02:00
tallyitem ( )
{
nAmount = 0 ;
nConf = std : : numeric_limits < int > : : max ( ) ;
2014-05-31 16:57:26 +02:00
fIsWatchonly = false ;
2012-08-21 16:38:57 +02:00
}
} ;
2017-07-31 21:30:21 +02:00
static UniValue ListReceived ( interfaces : : Chain : : Lock & locked_chain , CWallet * const pwallet , const UniValue & params , bool by_label ) EXCLUSIVE_LOCKS_REQUIRED ( pwallet - > cs_wallet )
2012-08-21 16:38:57 +02:00
{
2017-07-31 21:30:21 +02:00
LockAnnotation lock ( : : cs_main ) ; // Temporary, for CheckFinalTx below. Removed in upcoming commit.
2012-08-21 16:38:57 +02:00
// Minimum confirmations
int nMinDepth = 1 ;
2017-07-10 17:44:39 +02:00
if ( ! params [ 0 ] . isNull ( ) )
2012-08-21 16:38:57 +02:00
nMinDepth = params [ 0 ] . get_int ( ) ;
2017-10-20 19:27:55 +02:00
// Whether to include empty labels
2012-08-21 16:38:57 +02:00
bool fIncludeEmpty = false ;
2017-07-10 17:44:39 +02:00
if ( ! params [ 1 ] . isNull ( ) )
2012-08-21 16:38:57 +02:00
fIncludeEmpty = params [ 1 ] . get_bool ( ) ;
2014-07-01 11:00:22 +02:00
isminefilter filter = ISMINE_SPENDABLE ;
2017-07-10 17:44:39 +02:00
if ( ! params [ 2 ] . isNull ( ) )
2014-05-31 16:57:26 +02:00
if ( params [ 2 ] . get_bool ( ) )
2014-07-01 11:00:22 +02:00
filter = filter | ISMINE_WATCH_ONLY ;
2014-05-31 16:57:26 +02:00
2017-01-09 23:17:48 +01:00
bool has_filtered_address = false ;
CTxDestination filtered_address = CNoDestination ( ) ;
2017-10-20 19:27:55 +02:00
if ( ! by_label & & params . size ( ) > 3 ) {
2017-01-09 23:17:48 +01:00
if ( ! IsValidDestinationString ( params [ 3 ] . get_str ( ) ) ) {
throw JSONRPCError ( RPC_WALLET_ERROR , " address_filter parameter was invalid " ) ;
}
filtered_address = DecodeDestination ( params [ 3 ] . get_str ( ) ) ;
has_filtered_address = true ;
}
2012-08-21 16:38:57 +02:00
// Tally
2017-08-23 03:02:33 +02:00
std : : map < CTxDestination , tallyitem > mapTally ;
2018-05-16 00:41:53 +02:00
for ( const std : : pair < const uint256 , CWalletTx > & pairWtx : pwallet - > mapWallet ) {
2016-10-25 10:04:23 +02:00
const CWalletTx & wtx = pairWtx . second ;
2012-08-21 16:38:57 +02:00
2016-11-12 01:54:51 +01:00
if ( wtx . IsCoinBase ( ) | | ! CheckFinalTx ( * wtx . tx ) )
2012-08-21 16:38:57 +02:00
continue ;
2017-07-31 21:30:21 +02:00
int nDepth = wtx . GetDepthInMainChain ( locked_chain ) ;
2012-08-21 16:38:57 +02:00
if ( nDepth < nMinDepth )
continue ;
2017-06-02 03:18:57 +02:00
for ( const CTxOut & txout : wtx . tx - > vout )
2012-08-21 16:38:57 +02:00
{
CTxDestination address ;
2014-05-31 16:57:26 +02:00
if ( ! ExtractDestination ( txout . scriptPubKey , address ) )
continue ;
2017-01-09 23:17:48 +01:00
if ( has_filtered_address & & ! ( filtered_address = = address ) ) {
continue ;
}
2016-09-09 07:32:12 +02:00
isminefilter mine = IsMine ( * pwallet , address ) ;
2014-06-22 23:29:33 +02:00
if ( ! ( mine & filter ) )
2012-08-21 16:38:57 +02:00
continue ;
tallyitem & item = mapTally [ address ] ;
item . nAmount + = txout . nValue ;
2017-01-27 02:33:45 +01:00
item . nConf = std : : min ( item . nConf , nDepth ) ;
2012-12-13 15:59:10 +01:00
item . txids . push_back ( wtx . GetHash ( ) ) ;
2014-07-01 11:00:22 +02:00
if ( mine & ISMINE_WATCH_ONLY )
2014-05-31 16:57:26 +02:00
item . fIsWatchonly = true ;
2012-08-21 16:38:57 +02:00
}
}
// Reply
2015-05-10 14:48:35 +02:00
UniValue ret ( UniValue : : VARR ) ;
2017-10-20 19:27:55 +02:00
std : : map < std : : string , tallyitem > label_tally ;
2017-01-09 23:17:48 +01:00
// Create mapAddressBook iterator
// If we aren't filtering, go from begin() to end()
auto start = pwallet - > mapAddressBook . begin ( ) ;
auto end = pwallet - > mapAddressBook . end ( ) ;
// If we are filtering, find() the applicable entry
if ( has_filtered_address ) {
start = pwallet - > mapAddressBook . find ( filtered_address ) ;
if ( start ! = end ) {
end = std : : next ( start ) ;
}
}
for ( auto item_it = start ; item_it ! = end ; + + item_it )
{
const CTxDestination & address = item_it - > first ;
2017-10-20 19:27:55 +02:00
const std : : string & label = item_it - > second . name ;
2017-01-09 23:17:48 +01:00
auto it = mapTally . find ( address ) ;
2012-08-21 16:38:57 +02:00
if ( it = = mapTally . end ( ) & & ! fIncludeEmpty )
continue ;
2014-04-23 00:46:19 +02:00
CAmount nAmount = 0 ;
2012-08-21 16:38:57 +02:00
int nConf = std : : numeric_limits < int > : : max ( ) ;
2014-05-31 16:57:26 +02:00
bool fIsWatchonly = false ;
2012-08-21 16:38:57 +02:00
if ( it ! = mapTally . end ( ) )
{
nAmount = ( * it ) . second . nAmount ;
nConf = ( * it ) . second . nConf ;
2014-05-31 16:57:26 +02:00
fIsWatchonly = ( * it ) . second . fIsWatchonly ;
2012-08-21 16:38:57 +02:00
}
2017-10-20 19:27:55 +02:00
if ( by_label )
2012-08-21 16:38:57 +02:00
{
2017-10-20 19:27:55 +02:00
tallyitem & _item = label_tally [ label ] ;
2016-06-11 16:35:19 +02:00
_item . nAmount + = nAmount ;
2017-01-27 02:33:45 +01:00
_item . nConf = std : : min ( _item . nConf , nConf ) ;
2016-06-11 16:35:19 +02:00
_item . fIsWatchonly = fIsWatchonly ;
2012-08-21 16:38:57 +02:00
}
else
{
2015-05-10 14:48:35 +02:00
UniValue obj ( UniValue : : VOBJ ) ;
2014-05-31 16:57:26 +02:00
if ( fIsWatchonly )
2017-09-22 20:04:07 +02:00
obj . pushKV ( " involvesWatchonly " , true ) ;
2017-01-09 23:17:48 +01:00
obj . pushKV ( " address " , EncodeDestination ( address ) ) ;
2017-09-22 20:04:07 +02:00
obj . pushKV ( " amount " , ValueFromAmount ( nAmount ) ) ;
obj . pushKV ( " confirmations " , ( nConf = = std : : numeric_limits < int > : : max ( ) ? 0 : nConf ) ) ;
2017-10-20 19:27:55 +02:00
obj . pushKV ( " label " , label ) ;
2015-05-10 14:48:35 +02:00
UniValue transactions ( UniValue : : VARR ) ;
2012-12-16 20:10:32 +01:00
if ( it ! = mapTally . end ( ) )
2012-12-13 15:59:10 +01:00
{
2017-06-02 03:18:57 +02:00
for ( const uint256 & _item : ( * it ) . second . txids )
2012-12-16 20:10:32 +01:00
{
2016-06-11 16:35:19 +02:00
transactions . push_back ( _item . GetHex ( ) ) ;
2012-12-16 20:10:32 +01:00
}
2012-12-13 15:59:10 +01:00
}
2017-09-22 20:04:07 +02:00
obj . pushKV ( " txids " , transactions ) ;
2012-08-21 16:38:57 +02:00
ret . push_back ( obj ) ;
}
}
2017-10-20 19:27:55 +02:00
if ( by_label )
2012-08-21 16:38:57 +02:00
{
2017-10-20 19:27:55 +02:00
for ( const auto & entry : label_tally )
2012-08-21 16:38:57 +02:00
{
2017-06-04 22:02:43 +02:00
CAmount nAmount = entry . second . nAmount ;
int nConf = entry . second . nConf ;
2015-05-10 14:48:35 +02:00
UniValue obj ( UniValue : : VOBJ ) ;
2017-06-04 22:02:43 +02:00
if ( entry . second . fIsWatchonly )
2017-09-22 20:04:07 +02:00
obj . pushKV ( " involvesWatchonly " , true ) ;
obj . pushKV ( " amount " , ValueFromAmount ( nAmount ) ) ;
obj . pushKV ( " confirmations " , ( nConf = = std : : numeric_limits < int > : : max ( ) ? 0 : nConf ) ) ;
2017-10-20 19:27:55 +02:00
obj . pushKV ( " label " , entry . first ) ;
2012-08-21 16:38:57 +02:00
ret . push_back ( obj ) ;
}
}
return ret ;
}
2018-05-02 17:14:48 +02:00
static UniValue listreceivedbyaddress ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2017-01-09 23:17:48 +01:00
if ( request . fHelp | | request . params . size ( ) > 4 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2017-01-09 23:17:48 +01:00
" listreceivedbyaddress ( minconf include_empty include_watchonly address_filter ) \n "
2013-10-29 12:29:44 +01:00
" \n List balances by receiving address. \n "
" \n Arguments: \n "
2016-12-09 18:06:42 +01:00
" 1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included. \n "
" 2. include_empty (bool, optional, default=false) Whether to include addresses that haven't received any payments. \n "
2017-01-11 14:29:46 +01:00
" 3. include_watchonly (bool, optional, default=false) Whether to include watch-only addresses (see 'importaddress'). \n "
2017-01-09 23:17:48 +01:00
" 4. address_filter (string, optional) If present, only return information on this address. \n "
2013-10-29 12:29:44 +01:00
" \n Result: \n "
" [ \n "
" { \n "
2014-12-30 16:15:40 +01:00
" \" involvesWatchonly \" : true, (bool) Only returned if imported addresses were involved in transaction \n "
2013-10-29 12:29:44 +01:00
" \" address \" : \" receivingaddress \" , (string) The receiving address \n "
2015-08-01 21:15:23 +02:00
" \" amount \" : x.xxx, (numeric) The total amount in " + CURRENCY_UNIT + " received by the address \n "
2014-12-30 15:12:37 +01:00
" \" confirmations \" : n, (numeric) The number of confirmations of the most recent transaction included \n "
2017-10-20 19:27:55 +02:00
" \" label \" : \" label \" , (string) The label of the receiving address. The default label is \" \" . \n "
2017-01-11 14:29:46 +01:00
" \" txids \" : [ \n "
2018-03-30 10:59:37 +02:00
" \" txid \" , (string) The ids of transactions received with the address \n "
2017-01-11 14:29:46 +01:00
" ... \n "
" ] \n "
2013-10-29 12:29:44 +01:00
" } \n "
" ,... \n "
" ] \n "
" \n Examples: \n "
+ HelpExampleCli ( " listreceivedbyaddress " , " " )
+ HelpExampleCli ( " listreceivedbyaddress " , " 6 true " )
2014-05-31 16:57:26 +02:00
+ HelpExampleRpc ( " listreceivedbyaddress " , " 6, true, true " )
2017-01-09 23:17:48 +01:00
+ HelpExampleRpc ( " listreceivedbyaddress " , " 6, true, true, \" 1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd \" " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2017-07-31 21:30:21 +02:00
return ListReceived ( * locked_chain , pwallet , request . params , false ) ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue listreceivedbylabel ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) > 3 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2017-10-20 19:27:55 +02:00
" listreceivedbylabel ( minconf include_empty include_watchonly) \n "
" \n List received transactions by label. \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
2016-12-09 18:06:42 +01:00
" 1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included. \n "
2017-10-20 19:27:55 +02:00
" 2. include_empty (bool, optional, default=false) Whether to include labels that haven't received any payments. \n "
2017-01-11 14:29:46 +01:00
" 3. include_watchonly (bool, optional, default=false) Whether to include watch-only addresses (see 'importaddress'). \n "
2013-10-29 12:29:44 +01:00
" \n Result: \n "
" [ \n "
" { \n "
2014-12-30 16:15:40 +01:00
" \" involvesWatchonly \" : true, (bool) Only returned if imported addresses were involved in transaction \n "
2017-10-20 19:27:55 +02:00
" \" amount \" : x.xxx, (numeric) The total amount received by addresses with this label \n "
2014-12-30 15:12:37 +01:00
" \" confirmations \" : n, (numeric) The number of confirmations of the most recent transaction included \n "
2017-10-20 19:27:55 +02:00
" \" label \" : \" label \" (string) The label of the receiving address. The default label is \" \" . \n "
2013-10-29 12:29:44 +01:00
" } \n "
" ,... \n "
" ] \n "
" \n Examples: \n "
2017-10-20 19:27:55 +02:00
+ HelpExampleCli ( " listreceivedbylabel " , " " )
+ HelpExampleCli ( " listreceivedbylabel " , " 6 true " )
+ HelpExampleRpc ( " listreceivedbylabel " , " 6, true, true " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2017-07-31 21:30:21 +02:00
return ListReceived ( * locked_chain , pwallet , request . params , true ) ;
2012-08-21 16:38:57 +02:00
}
2015-05-13 21:29:19 +02:00
static void MaybePushAddress ( UniValue & entry , const CTxDestination & dest )
2012-09-22 05:22:34 +02:00
{
2017-08-23 03:02:33 +02:00
if ( IsValidDestination ( dest ) ) {
2017-09-22 20:04:07 +02:00
entry . pushKV ( " address " , EncodeDestination ( dest ) ) ;
2017-08-23 03:02:33 +02:00
}
2012-09-22 05:22:34 +02:00
}
2017-01-24 06:27:22 +01:00
/**
* List transactions based on the given criteria .
*
2018-11-12 16:49:39 +01:00
* @ param pwallet The wallet .
* @ param wtx The wallet transaction .
* @ param nMinDepth The minimum confirmation depth .
* @ param fLong Whether to include the JSON version of the transaction .
* @ param ret The UniValue into which the result is stored .
* @ param filter_ismine The " is mine " filter flags .
* @ param filter_label Optional label string to filter incoming transactions .
2017-01-24 06:27:22 +01:00
*/
2018-11-12 16:49:39 +01:00
static void ListTransactions ( interfaces : : Chain : : Lock & locked_chain , CWallet * const pwallet , const CWalletTx & wtx , int nMinDepth , bool fLong , UniValue & ret , const isminefilter & filter_ismine , const std : : string * filter_label )
2012-08-21 16:38:57 +02:00
{
2014-04-23 00:46:19 +02:00
CAmount nFee ;
2017-01-27 02:33:45 +01:00
std : : list < COutputEntry > listReceived ;
std : : list < COutputEntry > listSent ;
2012-08-21 16:38:57 +02:00
2018-11-12 16:49:39 +01:00
wtx . GetAmounts ( listReceived , listSent , nFee , filter_ismine ) ;
2012-08-21 16:38:57 +02:00
2014-07-01 11:00:22 +02:00
bool involvesWatchonly = wtx . IsFromMe ( ISMINE_WATCH_ONLY ) ;
2012-08-21 16:38:57 +02:00
// Sent
2018-10-06 06:48:23 +02:00
if ( ! filter_label )
2012-08-21 16:38:57 +02:00
{
2017-06-02 03:18:57 +02:00
for ( const COutputEntry & s : listSent )
2012-08-21 16:38:57 +02:00
{
2015-05-10 14:48:35 +02:00
UniValue entry ( UniValue : : VOBJ ) ;
2016-10-25 10:04:23 +02:00
if ( involvesWatchonly | | ( : : IsMine ( * pwallet , s . destination ) & ISMINE_WATCH_ONLY ) ) {
2017-09-22 20:04:07 +02:00
entry . pushKV ( " involvesWatchonly " , true ) ;
2016-10-25 10:04:23 +02:00
}
2014-05-30 00:54:00 +02:00
MaybePushAddress ( entry , s . destination ) ;
2017-09-22 20:04:07 +02:00
entry . pushKV ( " category " , " send " ) ;
entry . pushKV ( " amount " , ValueFromAmount ( - s . amount ) ) ;
2016-10-25 10:04:23 +02:00
if ( pwallet - > mapAddressBook . count ( s . destination ) ) {
2017-09-22 20:04:07 +02:00
entry . pushKV ( " label " , pwallet - > mapAddressBook [ s . destination ] . name ) ;
2016-10-25 10:04:23 +02:00
}
2017-09-22 20:04:07 +02:00
entry . pushKV ( " vout " , s . vout ) ;
entry . pushKV ( " fee " , ValueFromAmount ( - nFee ) ) ;
2012-08-21 16:38:57 +02:00
if ( fLong )
2017-07-31 21:30:21 +02:00
WalletTxToJSON ( pwallet - > chain ( ) , locked_chain , wtx , entry ) ;
2017-09-22 20:04:07 +02:00
entry . pushKV ( " abandoned " , wtx . isAbandoned ( ) ) ;
2012-08-21 16:38:57 +02:00
ret . push_back ( entry ) ;
}
}
// Received
2017-07-31 21:30:21 +02:00
if ( listReceived . size ( ) > 0 & & wtx . GetDepthInMainChain ( locked_chain ) > = nMinDepth )
2012-08-21 16:38:57 +02:00
{
2017-06-02 03:18:57 +02:00
for ( const COutputEntry & r : listReceived )
2012-08-21 16:38:57 +02:00
{
2018-07-27 23:05:24 +02:00
std : : string label ;
2016-10-25 10:04:23 +02:00
if ( pwallet - > mapAddressBook . count ( r . destination ) ) {
2018-07-27 23:05:24 +02:00
label = pwallet - > mapAddressBook [ r . destination ] . name ;
2016-10-25 10:04:23 +02:00
}
2018-10-06 06:48:23 +02:00
if ( filter_label & & label ! = * filter_label ) {
continue ;
}
2018-07-27 23:05:24 +02:00
UniValue entry ( UniValue : : VOBJ ) ;
if ( involvesWatchonly | | ( : : IsMine ( * pwallet , r . destination ) & ISMINE_WATCH_ONLY ) ) {
entry . pushKV ( " involvesWatchonly " , true ) ;
}
MaybePushAddress ( entry , r . destination ) ;
if ( wtx . IsCoinBase ( ) )
2012-08-21 16:38:57 +02:00
{
2017-07-31 21:30:21 +02:00
if ( wtx . GetDepthInMainChain ( locked_chain ) < 1 )
2018-07-27 23:05:24 +02:00
entry . pushKV ( " category " , " orphan " ) ;
2017-07-31 21:30:21 +02:00
else if ( wtx . IsImmatureCoinBase ( locked_chain ) )
2018-07-27 23:05:24 +02:00
entry . pushKV ( " category " , " immature " ) ;
2012-06-02 04:33:28 +02:00
else
2018-07-27 23:05:24 +02:00
entry . pushKV ( " category " , " generate " ) ;
}
else
{
entry . pushKV ( " category " , " receive " ) ;
}
entry . pushKV ( " amount " , ValueFromAmount ( r . amount ) ) ;
if ( pwallet - > mapAddressBook . count ( r . destination ) ) {
entry . pushKV ( " label " , label ) ;
2012-08-21 16:38:57 +02:00
}
2018-07-27 23:05:24 +02:00
entry . pushKV ( " vout " , r . vout ) ;
if ( fLong )
2017-07-31 21:30:21 +02:00
WalletTxToJSON ( pwallet - > chain ( ) , locked_chain , wtx , entry ) ;
2018-07-27 23:05:24 +02:00
ret . push_back ( entry ) ;
2012-08-21 16:38:57 +02:00
}
}
}
2016-09-22 09:46:41 +02:00
UniValue listtransactions ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2018-07-27 23:05:24 +02:00
if ( request . fHelp | | request . params . size ( ) > 4 )
throw std : : runtime_error (
2018-10-06 06:48:23 +02:00
" listtransactions ( \" label \" count skip include_watchonly ) \n "
" \n If a label name is provided, this will return only incoming transactions paying to addresses with the specified label. \n "
2018-07-27 23:05:24 +02:00
" \n Returns up to 'count' most recent transactions skipping the first 'from' transactions. \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
2018-10-06 06:48:23 +02:00
" 1. \" label \" (string, optional) If set, should be a valid label name to return only incoming transactions \n "
" with the specified label, or \" * \" to disable filtering and return all transactions. \n "
2013-10-29 12:29:44 +01:00
" 2. count (numeric, optional, default=10) The number of transactions to return \n "
2016-12-09 18:06:42 +01:00
" 3. skip (numeric, optional, default=0) The number of transactions to skip \n "
2017-01-11 14:29:46 +01:00
" 4. include_watchonly (bool, optional, default=false) Include transactions to watch-only addresses (see 'importaddress') \n "
2013-10-29 12:29:44 +01:00
" \n Result: \n "
" [ \n "
" { \n "
2018-04-16 20:01:52 +02:00
" \" address \" : \" address \" , (string) The bitcoin address of the transaction. \n "
" \" category \" : \" send|receive \" , (string) The transaction category. \n "
" \" amount \" : x.xxx, (numeric) The amount in " + CURRENCY_UNIT + " . This is negative for the 'send' category, and is positive \n "
" for the 'receive' category, \n "
" \" label \" : \" label \" , (string) A comment for the address/transaction, if any \n "
" \" vout \" : n, (numeric) the vout value \n "
" \" fee \" : x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + " . This is negative and only available for the \n "
" 'send' category of transactions. \n "
" \" confirmations \" : n, (numeric) The number of confirmations for the transaction. Negative confirmations indicate the \n "
" transaction conflicts with the block chain \n "
" \" trusted \" : xxx, (bool) Whether we consider the outputs of this unconfirmed transaction safe to spend. \n "
" \" blockhash \" : \" hashvalue \" , (string) The block hash containing the transaction. \n "
" \" blockindex \" : n, (numeric) The index of the transaction in the block that includes it. \n "
" \" blocktime \" : xxx, (numeric) The block time in seconds since epoch (1 Jan 1970 GMT). \n "
" \" txid \" : \" transactionid \" , (string) The transaction id. \n "
" \" time \" : xxx, (numeric) The transaction time in seconds since epoch (midnight Jan 1 1970 GMT). \n "
" \" timereceived \" : xxx, (numeric) The time received in seconds since epoch (midnight Jan 1 1970 GMT). \n "
" \" comment \" : \" ... \" , (string) If a comment is associated with the transaction. \n "
" \" bip125-replaceable \" : \" yes|no|unknown \" , (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee); \n "
" may be unknown for unconfirmed transactions not in the mempool \n "
" \" abandoned \" : xxx (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n "
" 'send' category of transactions. \n "
" } \n "
" ] \n "
" \n Examples: \n "
" \n List the most recent 10 transactions in the systems \n "
+ HelpExampleCli ( " listtransactions " , " " ) +
" \n List transactions 100 to 120 \n "
+ HelpExampleCli ( " listtransactions " , " \" * \" 20 100 " ) +
2018-10-02 21:49:18 +02:00
" \n As a JSON-RPC call \n "
2018-07-27 23:05:24 +02:00
+ HelpExampleRpc ( " listtransactions " , " \" * \" , 20, 100 " )
) ;
2012-08-21 16:38:57 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2018-10-06 06:48:23 +02:00
const std : : string * filter_label = nullptr ;
2018-07-27 23:05:24 +02:00
if ( ! request . params [ 0 ] . isNull ( ) & & request . params [ 0 ] . get_str ( ) ! = " * " ) {
2018-10-06 06:48:23 +02:00
filter_label = & request . params [ 0 ] . get_str ( ) ;
if ( filter_label - > empty ( ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Label argument must be a valid label name or \" * \" . " ) ;
}
2018-04-16 20:01:52 +02:00
}
2012-08-21 16:38:57 +02:00
int nCount = 10 ;
2017-07-10 17:44:39 +02:00
if ( ! request . params [ 1 ] . isNull ( ) )
2016-09-22 09:46:41 +02:00
nCount = request . params [ 1 ] . get_int ( ) ;
2012-08-21 16:38:57 +02:00
int nFrom = 0 ;
2017-07-10 17:44:39 +02:00
if ( ! request . params [ 2 ] . isNull ( ) )
2016-09-22 09:46:41 +02:00
nFrom = request . params [ 2 ] . get_int ( ) ;
2014-07-01 11:00:22 +02:00
isminefilter filter = ISMINE_SPENDABLE ;
2017-07-10 17:44:39 +02:00
if ( ! request . params [ 3 ] . isNull ( ) )
2016-09-22 09:46:41 +02:00
if ( request . params [ 3 ] . get_bool ( ) )
2014-07-01 11:00:22 +02:00
filter = filter | ISMINE_WATCH_ONLY ;
2012-08-21 16:38:57 +02:00
if ( nCount < 0 )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Negative count " ) ;
2012-08-21 16:38:57 +02:00
if ( nFrom < 0 )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Negative from " ) ;
2012-08-21 16:38:57 +02:00
2015-05-10 14:48:35 +02:00
UniValue ret ( UniValue : : VARR ) ;
2012-08-21 16:38:57 +02:00
{
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2018-02-02 02:57:29 +01:00
const CWallet : : TxItems & txOrdered = pwallet - > wtxOrdered ;
2012-08-21 16:38:57 +02:00
2018-02-02 02:57:29 +01:00
// iterate backwards until we have nCount items to return:
for ( CWallet : : TxItems : : const_reverse_iterator it = txOrdered . rbegin ( ) ; it ! = txOrdered . rend ( ) ; + + it )
{
2018-07-31 18:23:26 +02:00
CWalletTx * const pwtx = ( * it ) . second ;
2018-10-06 06:48:23 +02:00
ListTransactions ( * locked_chain , pwallet , * pwtx , 0 , true , ret , filter , filter_label ) ;
2018-02-02 02:57:29 +01:00
if ( ( int ) ret . size ( ) > = ( nCount + nFrom ) ) break ;
}
2012-08-21 16:38:57 +02:00
}
2018-02-02 02:57:29 +01:00
2012-08-21 16:38:57 +02:00
// ret is newest to oldest
if ( nFrom > ( int ) ret . size ( ) )
nFrom = ret . size ( ) ;
if ( ( nFrom + nCount ) > ( int ) ret . size ( ) )
nCount = ret . size ( ) - nFrom ;
2014-08-20 21:15:16 +02:00
2017-01-27 02:33:45 +01:00
std : : vector < UniValue > arrTmp = ret . getValues ( ) ;
2014-08-20 21:15:16 +02:00
2017-01-27 02:33:45 +01:00
std : : vector < UniValue > : : iterator first = arrTmp . begin ( ) ;
2012-08-21 16:38:57 +02:00
std : : advance ( first , nFrom ) ;
2017-01-27 02:33:45 +01:00
std : : vector < UniValue > : : iterator last = arrTmp . begin ( ) ;
2012-08-21 16:38:57 +02:00
std : : advance ( last , nFrom + nCount ) ;
2014-08-20 21:15:16 +02:00
if ( last ! = arrTmp . end ( ) ) arrTmp . erase ( last , arrTmp . end ( ) ) ;
if ( first ! = arrTmp . begin ( ) ) arrTmp . erase ( arrTmp . begin ( ) , first ) ;
2012-08-21 16:38:57 +02:00
2014-08-20 21:15:16 +02:00
std : : reverse ( arrTmp . begin ( ) , arrTmp . end ( ) ) ; // Return oldest to newest
ret . clear ( ) ;
2015-05-10 14:48:35 +02:00
ret . setArray ( ) ;
2014-08-20 21:15:16 +02:00
ret . push_backV ( arrTmp ) ;
2012-08-21 16:38:57 +02:00
return ret ;
}
2018-05-02 17:14:48 +02:00
static UniValue listsinceblock ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2017-01-24 06:27:22 +01:00
if ( request . fHelp | | request . params . size ( ) > 4 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2017-01-24 06:27:22 +01:00
" listsinceblock ( \" blockhash \" target_confirmations include_watchonly include_removed ) \n "
" \n Get all transactions in blocks since block [blockhash], or all transactions if omitted. \n "
" If \" blockhash \" is no longer a part of the main chain, transactions from the fork point onward are included. \n "
" Additionally, if include_removed is set, transactions affecting the wallet which were removed are returned in the \" removed \" array. \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
2016-12-09 18:06:42 +01:00
" 1. \" blockhash \" (string, optional) The block hash to list transactions since \n "
2017-07-25 21:25:07 +02:00
" 2. target_confirmations: (numeric, optional, default=1) Return the nth block hash from the main chain. e.g. 1 would mean the best block hash. Note: this is not used as a filter, but only affects [lastblock] in the return value \n "
2017-01-24 06:27:22 +01:00
" 3. include_watchonly: (bool, optional, default=false) Include transactions to watch-only addresses (see 'importaddress') \n "
" 4. include_removed: (bool, optional, default=true) Show transactions that were removed due to a reorg in the \" removed \" array \n "
" (not guaranteed to work on pruned nodes) \n "
2013-10-29 12:29:44 +01:00
" \n Result: \n "
" { \n "
" \" transactions \" : [ \n "
2016-11-21 14:39:58 +01:00
" \" address \" : \" address \" , (string) The bitcoin address of the transaction. Not present for move transactions (category = move). \n "
2013-10-29 12:29:44 +01:00
" \" category \" : \" send|receive \" , (string) The transaction category. 'send' has negative amounts, 'receive' has positive amounts. \n "
2015-08-01 21:15:23 +02:00
" \" amount \" : x.xxx, (numeric) The amount in " + CURRENCY_UNIT + " . This is negative for the 'send' category, and for the 'move' category for moves \n "
2013-10-29 12:29:44 +01:00
" outbound. It is positive for the 'receive' category, and for the 'move' category for inbound funds. \n "
2014-05-30 00:54:00 +02:00
" \" vout \" : n, (numeric) the vout value \n "
2015-08-01 21:15:23 +02:00
" \" fee \" : x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + " . This is negative and only available for the 'send' category of transactions. \n "
2013-10-29 12:29:44 +01:00
" \" confirmations \" : n, (numeric) The number of confirmations for the transaction. Available for 'send' and 'receive' category of transactions. \n "
2016-12-21 02:39:49 +01:00
" When it's < 0, it means the transaction conflicted that many blocks ago. \n "
2013-10-29 12:29:44 +01:00
" \" blockhash \" : \" hashvalue \" , (string) The block hash containing the transaction. Available for 'send' and 'receive' category of transactions. \n "
2016-02-16 20:42:14 +01:00
" \" blockindex \" : n, (numeric) The index of the transaction in the block that includes it. Available for 'send' and 'receive' category of transactions. \n "
2013-10-29 12:29:44 +01:00
" \" blocktime \" : xxx, (numeric) The block time in seconds since epoch (1 Jan 1970 GMT). \n "
2014-05-29 18:17:34 +02:00
" \" txid \" : \" transactionid \" , (string) The transaction id. Available for 'send' and 'receive' category of transactions. \n "
2013-10-29 12:29:44 +01:00
" \" time \" : xxx, (numeric) The transaction time in seconds since epoch (Jan 1 1970 GMT). \n "
" \" timereceived \" : xxx, (numeric) The time received in seconds since epoch (Jan 1 1970 GMT). Available for 'send' and 'receive' category of transactions. \n "
2017-01-11 14:29:46 +01:00
" \" bip125-replaceable \" : \" yes|no|unknown \" , (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee); \n "
" may be unknown for unconfirmed transactions not in the mempool \n "
" \" abandoned \" : xxx, (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the 'send' category of transactions. \n "
2013-10-29 12:29:44 +01:00
" \" comment \" : \" ... \" , (string) If a comment is associated with the transaction. \n "
2014-12-30 15:12:37 +01:00
" \" label \" : \" label \" (string) A comment for the address/transaction, if any \n "
2013-10-29 12:29:44 +01:00
" \" to \" : \" ... \" , (string) If a comment to is associated with the transaction. \n "
2017-01-24 06:27:22 +01:00
" ], \n "
" \" removed \" : [ \n "
" <structure is the same as \" transactions \" above, only present if include_removed=true> \n "
2018-03-18 15:26:45 +01:00
" Note: transactions that were re-added in the active chain will appear as-is in this array, and may thus have a positive confirmation count. \n "
2017-01-24 06:27:22 +01:00
" ], \n "
2017-07-25 21:25:07 +02:00
" \" lastblock \" : \" lastblockhash \" (string) The hash of the block (target_confirmations-1) from the best block on the main chain. This is typically used to feed back into listsinceblock the next time you call it. So you would generally use a target_confirmations of say 6, so you will be continually re-notified of transactions until they've reached 6 confirmations plus any new ones \n "
2013-10-29 12:29:44 +01:00
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " listsinceblock " , " " )
+ HelpExampleCli ( " listsinceblock " , " \" 000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad \" 6 " )
+ HelpExampleRpc ( " listsinceblock " , " \" 000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad \" , 6 " )
) ;
2012-08-21 16:38:57 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2017-08-07 07:36:37 +02:00
const CBlockIndex * pindex = nullptr ; // Block index of the specified block or the common ancestor, if the block provided was in a deactivated chain.
const CBlockIndex * paltindex = nullptr ; // Block index of the specified block, even if it's in a deactivated chain.
2012-08-21 16:38:57 +02:00
int target_confirms = 1 ;
2014-07-01 11:00:22 +02:00
isminefilter filter = ISMINE_SPENDABLE ;
2012-08-21 16:38:57 +02:00
2017-10-26 13:10:59 +02:00
if ( ! request . params [ 0 ] . isNull ( ) & & ! request . params [ 0 ] . get_str ( ) . empty ( ) ) {
2018-06-08 20:16:07 +02:00
uint256 blockId ( ParseHashV ( request . params [ 0 ] , " blockhash " ) ) ;
2012-08-21 16:38:57 +02:00
2018-01-12 01:23:09 +01:00
paltindex = pindex = LookupBlockIndex ( blockId ) ;
if ( ! pindex ) {
2017-10-26 13:10:59 +02:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Block not found " ) ;
}
if ( chainActive [ pindex - > nHeight ] ! = pindex ) {
// the block being asked for is a part of a deactivated chain;
// we don't want to depend on its perceived height in the block
// chain, we want to instead use the last common ancestor
pindex = chainActive . FindFork ( pindex ) ;
2017-01-11 07:31:16 +01:00
}
2012-08-21 16:38:57 +02:00
}
2017-01-24 06:27:22 +01:00
if ( ! request . params [ 1 ] . isNull ( ) ) {
2016-09-22 09:46:41 +02:00
target_confirms = request . params [ 1 ] . get_int ( ) ;
2012-08-21 16:38:57 +02:00
2017-01-24 06:27:22 +01:00
if ( target_confirms < 1 ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid parameter " ) ;
2017-01-24 06:27:22 +01:00
}
2012-08-21 16:38:57 +02:00
}
2017-01-24 06:27:22 +01:00
if ( ! request . params [ 2 ] . isNull ( ) & & request . params [ 2 ] . get_bool ( ) ) {
2017-01-11 07:31:16 +01:00
filter = filter | ISMINE_WATCH_ONLY ;
}
2014-04-29 19:39:01 +02:00
2017-01-24 06:27:22 +01:00
bool include_removed = ( request . params [ 3 ] . isNull ( ) | | request . params [ 3 ] . get_bool ( ) ) ;
2013-10-10 23:07:44 +02:00
int depth = pindex ? ( 1 + chainActive . Height ( ) - pindex - > nHeight ) : - 1 ;
2012-08-21 16:38:57 +02:00
2015-05-10 14:48:35 +02:00
UniValue transactions ( UniValue : : VARR ) ;
2012-08-21 16:38:57 +02:00
2018-05-16 00:41:53 +02:00
for ( const std : : pair < const uint256 , CWalletTx > & pairWtx : pwallet - > mapWallet ) {
2016-10-25 10:04:23 +02:00
CWalletTx tx = pairWtx . second ;
2012-08-21 16:38:57 +02:00
2017-07-31 21:30:21 +02:00
if ( depth = = - 1 | | tx . GetDepthInMainChain ( * locked_chain ) < depth ) {
2018-10-06 06:48:23 +02:00
ListTransactions ( * locked_chain , pwallet , tx , 0 , true , transactions , filter , nullptr /* filter_label */ ) ;
2017-01-24 06:27:22 +01:00
}
}
// when a reorg'd block is requested, we also list any relevant transactions
// in the blocks of the chain that was detached
UniValue removed ( UniValue : : VARR ) ;
while ( include_removed & & paltindex & & paltindex ! = pindex ) {
CBlock block ;
if ( ! ReadBlockFromDisk ( block , paltindex , Params ( ) . GetConsensus ( ) ) ) {
throw JSONRPCError ( RPC_INTERNAL_ERROR , " Can't read block from disk " ) ;
}
for ( const CTransactionRef & tx : block . vtx ) {
2017-08-13 16:04:57 +02:00
auto it = pwallet - > mapWallet . find ( tx - > GetHash ( ) ) ;
if ( it ! = pwallet - > mapWallet . end ( ) ) {
2017-01-24 06:27:22 +01:00
// We want all transactions regardless of confirmation count to appear here,
// even negative confirmation ones, hence the big negative.
2018-10-06 06:48:23 +02:00
ListTransactions ( * locked_chain , pwallet , it - > second , - 100000000 , true , removed , filter , nullptr /* filter_label */ ) ;
2017-01-24 06:27:22 +01:00
}
}
paltindex = paltindex - > pprev ;
2012-08-21 16:38:57 +02:00
}
2013-10-10 23:07:44 +02:00
CBlockIndex * pblockLast = chainActive [ chainActive . Height ( ) + 1 - target_confirms ] ;
2014-12-15 09:11:16 +01:00
uint256 lastblock = pblockLast ? pblockLast - > GetBlockHash ( ) : uint256 ( ) ;
2012-08-21 16:38:57 +02:00
2015-05-10 14:48:35 +02:00
UniValue ret ( UniValue : : VOBJ ) ;
2017-09-22 20:04:07 +02:00
ret . pushKV ( " transactions " , transactions ) ;
if ( include_removed ) ret . pushKV ( " removed " , removed ) ;
ret . pushKV ( " lastblock " , lastblock . GetHex ( ) ) ;
2012-08-21 16:38:57 +02:00
return ret ;
}
2018-05-02 17:14:48 +02:00
static UniValue gettransaction ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) < 1 | | request . params . size ( ) > 2 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2016-11-21 14:32:36 +01:00
" gettransaction \" txid \" ( include_watchonly ) \n "
2013-10-29 12:29:44 +01:00
" \n Get detailed information about in-wallet transaction <txid> \n "
" \n Arguments: \n "
2016-12-09 18:06:42 +01:00
" 1. \" txid \" (string, required) The transaction id \n "
2017-01-11 14:29:46 +01:00
" 2. \" include_watchonly \" (bool, optional, default=false) Whether to include watch-only addresses in balance calculation and details[] \n "
2013-10-29 12:29:44 +01:00
" \n Result: \n "
" { \n "
2015-08-01 21:15:23 +02:00
" \" amount \" : x.xxx, (numeric) The transaction amount in " + CURRENCY_UNIT + " \n "
2017-01-11 14:29:46 +01:00
" \" fee \" : x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + " . This is negative and only available for the \n "
" 'send' category of transactions. \n "
2013-10-29 12:29:44 +01:00
" \" confirmations \" : n, (numeric) The number of confirmations \n "
" \" blockhash \" : \" hash \" , (string) The block hash \n "
2016-02-16 20:42:14 +01:00
" \" blockindex \" : xx, (numeric) The index of the transaction in the block that includes it \n "
2013-10-29 12:29:44 +01:00
" \" blocktime \" : ttt, (numeric) The time in seconds since epoch (1 Jan 1970 GMT) \n "
2014-05-29 18:17:34 +02:00
" \" txid \" : \" transactionid \" , (string) The transaction id. \n "
2013-10-29 12:29:44 +01:00
" \" time \" : ttt, (numeric) The transaction time in seconds since epoch (1 Jan 1970 GMT) \n "
" \" timereceived \" : ttt, (numeric) The time received in seconds since epoch (1 Jan 1970 GMT) \n "
2017-01-11 14:29:46 +01:00
" \" bip125-replaceable \" : \" yes|no|unknown \" , (string) Whether this transaction could be replaced due to BIP125 (replace-by-fee); \n "
2015-12-16 20:57:54 +01:00
" may be unknown for unconfirmed transactions not in the mempool \n "
2013-10-29 12:29:44 +01:00
" \" details \" : [ \n "
" { \n "
2017-01-11 14:29:46 +01:00
" \" address \" : \" address \" , (string) The bitcoin address involved in the transaction \n "
2013-10-29 12:29:44 +01:00
" \" category \" : \" send|receive \" , (string) The category, either 'send' or 'receive' \n "
2014-12-30 15:12:37 +01:00
" \" amount \" : x.xxx, (numeric) The amount in " + CURRENCY_UNIT + " \n "
" \" label \" : \" label \" , (string) A comment for the address/transaction, if any \n "
2014-05-30 00:54:00 +02:00
" \" vout \" : n, (numeric) the vout value \n "
2017-01-11 14:29:46 +01:00
" \" fee \" : x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + " . This is negative and only available for the \n "
" 'send' category of transactions. \n "
" \" abandoned \" : xxx (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n "
2017-01-16 09:00:23 +01:00
" 'send' category of transactions. \n "
2013-10-29 12:29:44 +01:00
" } \n "
" ,... \n "
2014-02-13 17:28:32 +01:00
" ], \n "
" \" hex \" : \" data \" (string) Raw data for transaction \n "
2013-10-29 12:29:44 +01:00
" } \n "
2014-08-09 06:13:06 +02:00
" \n Examples: \n "
2013-10-29 12:29:44 +01:00
+ HelpExampleCli ( " gettransaction " , " \" 1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d \" " )
2014-09-07 14:37:13 +02:00
+ HelpExampleCli ( " gettransaction " , " \" 1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d \" true " )
2013-10-29 12:29:44 +01:00
+ HelpExampleRpc ( " gettransaction " , " \" 1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d \" " )
) ;
2012-08-21 16:38:57 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2018-06-08 20:16:07 +02:00
uint256 hash ( ParseHashV ( request . params [ 0 ] , " txid " ) ) ;
2012-08-21 16:38:57 +02:00
2014-07-01 11:00:22 +02:00
isminefilter filter = ISMINE_SPENDABLE ;
2017-07-10 17:44:39 +02:00
if ( ! request . params [ 1 ] . isNull ( ) )
2016-09-22 09:46:41 +02:00
if ( request . params [ 1 ] . get_bool ( ) )
2014-07-01 11:00:22 +02:00
filter = filter | ISMINE_WATCH_ONLY ;
2014-05-23 00:58:15 +02:00
2015-05-10 14:48:35 +02:00
UniValue entry ( UniValue : : VOBJ ) ;
2017-08-13 16:04:57 +02:00
auto it = pwallet - > mapWallet . find ( hash ) ;
if ( it = = pwallet - > mapWallet . end ( ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid or non-wallet transaction id " ) ;
2016-10-25 10:04:23 +02:00
}
2017-08-13 16:04:57 +02:00
const CWalletTx & wtx = it - > second ;
2012-08-21 16:38:57 +02:00
2017-07-31 21:30:21 +02:00
CAmount nCredit = wtx . GetCredit ( * locked_chain , filter ) ;
2014-04-23 00:46:19 +02:00
CAmount nDebit = wtx . GetDebit ( filter ) ;
CAmount nNet = nCredit - nDebit ;
2016-11-12 01:54:51 +01:00
CAmount nFee = ( wtx . IsFromMe ( filter ) ? wtx . tx - > GetValueOut ( ) - nDebit : 0 ) ;
2012-08-21 16:38:57 +02:00
2017-09-22 20:04:07 +02:00
entry . pushKV ( " amount " , ValueFromAmount ( nNet - nFee ) ) ;
2014-06-19 15:24:17 +02:00
if ( wtx . IsFromMe ( filter ) )
2017-09-22 20:04:07 +02:00
entry . pushKV ( " fee " , ValueFromAmount ( nFee ) ) ;
2012-08-21 16:38:57 +02:00
2017-07-31 21:30:21 +02:00
WalletTxToJSON ( pwallet - > chain ( ) , * locked_chain , wtx , entry ) ;
2012-08-21 16:38:57 +02:00
2015-05-10 14:48:35 +02:00
UniValue details ( UniValue : : VARR ) ;
2018-10-06 06:48:23 +02:00
ListTransactions ( * locked_chain , pwallet , wtx , 0 , false , details , filter , nullptr /* filter_label */ ) ;
2017-09-22 20:04:07 +02:00
entry . pushKV ( " details " , details ) ;
2012-08-21 16:38:57 +02:00
2017-05-09 08:46:26 +02:00
std : : string strHex = EncodeHexTx ( * wtx . tx , RPCSerializationFlags ( ) ) ;
2017-09-22 20:04:07 +02:00
entry . pushKV ( " hex " , strHex ) ;
2014-02-13 17:28:32 +01:00
2012-08-21 16:38:57 +02:00
return entry ;
}
2018-05-02 17:14:48 +02:00
static UniValue abandontransaction ( const JSONRPCRequest & request )
2016-01-07 22:31:27 +01:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2016-01-07 22:31:27 +01:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-07 22:31:27 +01:00
2018-01-27 23:45:32 +01:00
if ( request . fHelp | | request . params . size ( ) ! = 1 ) {
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2016-01-07 22:31:27 +01:00
" abandontransaction \" txid \" \n "
" \n Mark in-wallet transaction <txid> as abandoned \n "
" This will mark this transaction and all its in-wallet descendants as abandoned which will allow \n "
" for their inputs to be respent. It can be used to replace \" stuck \" or evicted transactions. \n "
" It only works on transactions which are not included in a block and are not currently in the mempool. \n "
2018-01-27 23:45:32 +01:00
" It has no effect on transactions which are already abandoned. \n "
2016-01-07 22:31:27 +01:00
" \n Arguments: \n "
" 1. \" txid \" (string, required) The transaction id \n "
" \n Result: \n "
" \n Examples: \n "
+ HelpExampleCli ( " abandontransaction " , " \" 1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d \" " )
+ HelpExampleRpc ( " abandontransaction " , " \" 1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d \" " )
) ;
2018-01-27 23:45:32 +01:00
}
2016-01-07 22:31:27 +01:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2016-01-07 22:31:27 +01:00
2018-06-08 20:16:07 +02:00
uint256 hash ( ParseHashV ( request . params [ 0 ] , " txid " ) ) ;
2016-01-07 22:31:27 +01:00
2016-10-25 10:04:23 +02:00
if ( ! pwallet - > mapWallet . count ( hash ) ) {
2016-01-07 22:31:27 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid or non-wallet transaction id " ) ;
2016-10-25 10:04:23 +02:00
}
2017-07-31 21:30:21 +02:00
if ( ! pwallet - > AbandonTransaction ( * locked_chain , hash ) ) {
2016-01-07 22:31:27 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Transaction not eligible for abandonment " ) ;
2016-10-25 10:04:23 +02:00
}
2016-01-07 22:31:27 +01:00
return NullUniValue ;
}
2012-08-21 16:38:57 +02:00
2018-05-02 17:14:48 +02:00
static UniValue backupwallet ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 1 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2013-10-29 12:29:44 +01:00
" backupwallet \" destination \" \n "
2016-02-26 12:48:53 +01:00
" \n Safely copies current wallet file to destination, which can be a directory or a path with filename. \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
" 1. \" destination \" (string) The destination directory or file \n "
" \n Examples: \n "
+ HelpExampleCli ( " backupwallet " , " \" backup.dat \" " )
+ HelpExampleRpc ( " backupwallet " , " \" backup.dat \" " )
) ;
2012-08-21 16:38:57 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2017-01-27 02:33:45 +01:00
std : : string strDest = request . params [ 0 ] . get_str ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! pwallet - > BackupWallet ( strDest ) ) {
2012-11-27 16:27:54 +01:00
throw JSONRPCError ( RPC_WALLET_ERROR , " Error: Wallet backup failed! " ) ;
2016-10-25 10:04:23 +02:00
}
2012-08-21 16:38:57 +02:00
2014-08-20 21:15:16 +02:00
return NullUniValue ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue keypoolrefill ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) > 1 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2013-10-29 12:29:44 +01:00
" keypoolrefill ( newsize ) \n "
" \n Fills the keypool. "
2016-10-25 09:45:57 +02:00
+ HelpRequiringPassphrase ( pwallet ) + " \n "
2013-10-29 12:29:44 +01:00
" \n Arguments \n "
" 1. newsize (numeric, optional, default=100) The new keypool size \n "
" \n Examples: \n "
+ HelpExampleCli ( " keypoolrefill " , " " )
+ HelpExampleRpc ( " keypoolrefill " , " " )
) ;
2012-08-21 16:38:57 +02:00
2017-05-05 08:53:39 +02:00
if ( pwallet - > IsWalletFlagSet ( WALLET_FLAG_DISABLE_PRIVATE_KEYS ) ) {
throw JSONRPCError ( RPC_WALLET_ERROR , " Error: Private keys are disabled for this wallet " ) ;
}
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2013-10-28 11:58:39 +01:00
// 0 is interpreted by TopUpKeyPool() as the default keypool size given by -keypool
unsigned int kpSize = 0 ;
2017-07-10 17:44:39 +02:00
if ( ! request . params [ 0 ] . isNull ( ) ) {
2016-09-22 09:46:41 +02:00
if ( request . params [ 0 ] . get_int ( ) < 0 )
2013-10-28 11:58:39 +01:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid parameter, expected valid size. " ) ;
2016-09-22 09:46:41 +02:00
kpSize = ( unsigned int ) request . params [ 0 ] . get_int ( ) ;
2013-06-25 22:21:42 +02:00
}
2016-10-25 09:45:57 +02:00
EnsureWalletIsUnlocked ( pwallet ) ;
pwallet - > TopUpKeyPool ( kpSize ) ;
2012-08-21 16:38:57 +02:00
2016-10-25 10:04:23 +02:00
if ( pwallet - > GetKeyPoolSize ( ) < kpSize ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , " Error refreshing keypool. " ) ;
2016-10-25 10:04:23 +02:00
}
2012-08-21 16:38:57 +02:00
2014-08-20 21:15:16 +02:00
return NullUniValue ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue walletpassphrase ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2017-11-01 07:22:41 +01:00
if ( request . fHelp | | request . params . size ( ) ! = 2 ) {
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2013-10-29 12:29:44 +01:00
" walletpassphrase \" passphrase \" timeout \n "
" \n Stores the wallet decryption key in memory for 'timeout' seconds. \n "
" This is needed prior to performing transactions related to private keys such as sending bitcoins \n "
" \n Arguments: \n "
" 1. \" passphrase \" (string, required) The wallet passphrase \n "
2018-04-06 17:54:52 +02:00
" 2. timeout (numeric, required) The time to keep the decryption key in seconds; capped at 100000000 (~3 years). \n "
2014-02-16 10:23:59 +01:00
" \n Note: \n "
" Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock \n "
" time that overrides the old one. \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
2017-07-19 22:50:26 +02:00
" \n Unlock the wallet for 60 seconds \n "
2013-10-29 12:29:44 +01:00
+ HelpExampleCli ( " walletpassphrase " , " \" my pass phrase \" 60 " ) +
" \n Lock the wallet again (before 60 seconds) \n "
+ HelpExampleCli ( " walletlock " , " " ) +
2018-10-02 21:49:18 +02:00
" \n As a JSON-RPC call \n "
2013-10-29 12:29:44 +01:00
+ HelpExampleRpc ( " walletpassphrase " , " \" my pass phrase \" , 60 " )
) ;
2016-10-25 10:04:23 +02:00
}
2013-10-29 12:29:44 +01:00
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2016-10-25 10:04:23 +02:00
if ( ! pwallet - > IsCrypted ( ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_WRONG_ENC_STATE , " Error: running with an unencrypted wallet, but walletpassphrase was called. " ) ;
2016-10-25 10:04:23 +02:00
}
2012-08-21 16:38:57 +02:00
2016-09-22 09:46:41 +02:00
// Note that the walletpassphrase is stored in request.params[0] which is not mlock()ed
2012-08-21 16:38:57 +02:00
SecureString strWalletPass ;
strWalletPass . reserve ( 100 ) ;
// TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
2016-09-22 09:46:41 +02:00
// Alternately, find a way to make request.params[0] mlock()'d to begin with.
strWalletPass = request . params [ 0 ] . get_str ( ) . c_str ( ) ;
2012-08-21 16:38:57 +02:00
2018-01-06 08:07:38 +01:00
// Get the timeout
int64_t nSleepTime = request . params [ 1 ] . get_int64 ( ) ;
// Timeout cannot be negative, otherwise it will relock immediately
if ( nSleepTime < 0 ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Timeout cannot be negative. " ) ;
}
2018-04-06 17:54:52 +02:00
// Clamp timeout
constexpr int64_t MAX_SLEEP_TIME = 100000000 ; // larger values trigger a macos/libevent bug?
if ( nSleepTime > MAX_SLEEP_TIME ) {
nSleepTime = MAX_SLEEP_TIME ;
2018-01-06 08:07:38 +01:00
}
2012-08-21 16:38:57 +02:00
if ( strWalletPass . length ( ) > 0 )
{
2016-10-25 10:04:23 +02:00
if ( ! pwallet - > Unlock ( strWalletPass ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_PASSPHRASE_INCORRECT , " Error: The wallet passphrase entered was incorrect. " ) ;
2016-10-25 10:04:23 +02:00
}
2012-08-21 16:38:57 +02:00
}
else
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2012-08-21 16:38:57 +02:00
" walletpassphrase <passphrase> <timeout> \n "
" Stores the wallet decryption key in memory for <timeout> seconds. " ) ;
2016-10-25 09:45:57 +02:00
pwallet - > TopUpKeyPool ( ) ;
2013-05-07 16:47:00 +02:00
2016-09-09 09:48:10 +02:00
pwallet - > nRelockTime = GetTime ( ) + nSleepTime ;
2018-10-10 01:32:27 +02:00
// Keep a weak pointer to the wallet so that it is possible to unload the
// wallet before the following callback is called. If a valid shared pointer
// is acquired in the callback then the wallet is still loaded.
std : : weak_ptr < CWallet > weak_wallet = wallet ;
RPCRunLater ( strprintf ( " lockwallet(%s) " , pwallet - > GetName ( ) ) , [ weak_wallet ] {
if ( auto shared_wallet = weak_wallet . lock ( ) ) {
LOCK ( shared_wallet - > cs_wallet ) ;
shared_wallet - > Lock ( ) ;
shared_wallet - > nRelockTime = 0 ;
}
} , nSleepTime ) ;
2012-08-21 16:38:57 +02:00
2014-08-20 21:15:16 +02:00
return NullUniValue ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue walletpassphrasechange ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2017-11-01 07:22:41 +01:00
if ( request . fHelp | | request . params . size ( ) ! = 2 ) {
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2013-10-29 12:29:44 +01:00
" walletpassphrasechange \" oldpassphrase \" \" newpassphrase \" \n "
" \n Changes the wallet passphrase from 'oldpassphrase' to 'newpassphrase'. \n "
" \n Arguments: \n "
" 1. \" oldpassphrase \" (string) The current passphrase \n "
" 2. \" newpassphrase \" (string) The new passphrase \n "
" \n Examples: \n "
+ HelpExampleCli ( " walletpassphrasechange " , " \" old one \" \" new one \" " )
+ HelpExampleRpc ( " walletpassphrasechange " , " \" old one \" , \" new one \" " )
) ;
2016-10-25 10:04:23 +02:00
}
2013-10-29 12:29:44 +01:00
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2016-10-25 10:04:23 +02:00
if ( ! pwallet - > IsCrypted ( ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_WRONG_ENC_STATE , " Error: running with an unencrypted wallet, but walletpassphrasechange was called. " ) ;
2016-10-25 10:04:23 +02:00
}
2012-08-21 16:38:57 +02:00
// TODO: get rid of these .c_str() calls by implementing SecureString::operator=(std::string)
2016-09-22 09:46:41 +02:00
// Alternately, find a way to make request.params[0] mlock()'d to begin with.
2012-08-21 16:38:57 +02:00
SecureString strOldWalletPass ;
strOldWalletPass . reserve ( 100 ) ;
2016-09-22 09:46:41 +02:00
strOldWalletPass = request . params [ 0 ] . get_str ( ) . c_str ( ) ;
2012-08-21 16:38:57 +02:00
SecureString strNewWalletPass ;
strNewWalletPass . reserve ( 100 ) ;
2016-09-22 09:46:41 +02:00
strNewWalletPass = request . params [ 1 ] . get_str ( ) . c_str ( ) ;
2012-08-21 16:38:57 +02:00
if ( strOldWalletPass . length ( ) < 1 | | strNewWalletPass . length ( ) < 1 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2012-08-21 16:38:57 +02:00
" walletpassphrasechange <oldpassphrase> <newpassphrase> \n "
" Changes the wallet passphrase from <oldpassphrase> to <newpassphrase>. " ) ;
2016-10-25 10:04:23 +02:00
if ( ! pwallet - > ChangeWalletPassphrase ( strOldWalletPass , strNewWalletPass ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_PASSPHRASE_INCORRECT , " Error: The wallet passphrase entered was incorrect. " ) ;
2016-10-25 10:04:23 +02:00
}
2012-08-21 16:38:57 +02:00
2014-08-20 21:15:16 +02:00
return NullUniValue ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue walletlock ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2017-11-01 07:22:41 +01:00
if ( request . fHelp | | request . params . size ( ) ! = 0 ) {
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2012-08-21 16:38:57 +02:00
" walletlock \n "
2013-10-29 12:29:44 +01:00
" \n Removes the wallet encryption key from memory, locking the wallet. \n "
2012-08-21 16:38:57 +02:00
" After calling this method, you will need to call walletpassphrase again \n "
2013-10-29 12:29:44 +01:00
" before being able to call any methods which require the wallet to be unlocked. \n "
" \n Examples: \n "
" \n Set the passphrase for 2 minutes to perform a transaction \n "
+ HelpExampleCli ( " walletpassphrase " , " \" my pass phrase \" 120 " ) +
" \n Perform a send (requires passphrase set) \n "
+ HelpExampleCli ( " sendtoaddress " , " \" 1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd \" 1.0 " ) +
" \n Clear the passphrase since we are done before 2 minutes is up \n "
+ HelpExampleCli ( " walletlock " , " " ) +
2018-10-02 21:49:18 +02:00
" \n As a JSON-RPC call \n "
2013-10-29 12:29:44 +01:00
+ HelpExampleRpc ( " walletlock " , " " )
) ;
2016-10-25 10:04:23 +02:00
}
2013-10-29 12:29:44 +01:00
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2016-10-25 10:04:23 +02:00
if ( ! pwallet - > IsCrypted ( ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_WRONG_ENC_STATE , " Error: running with an unencrypted wallet, but walletlock was called. " ) ;
2012-08-21 16:38:57 +02:00
}
2016-09-09 09:48:10 +02:00
pwallet - > Lock ( ) ;
pwallet - > nRelockTime = 0 ;
2012-08-21 16:38:57 +02:00
2014-08-20 21:15:16 +02:00
return NullUniValue ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue encryptwallet ( const JSONRPCRequest & request )
2012-08-21 16:38:57 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2017-11-01 07:22:41 +01:00
if ( request . fHelp | | request . params . size ( ) ! = 1 ) {
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2013-10-29 12:29:44 +01:00
" encryptwallet \" passphrase \" \n "
" \n Encrypts the wallet with 'passphrase'. This is for first time encryption. \n "
" After this, any calls that interact with private keys such as sending or signing \n "
" will require the passphrase to be set prior the making these calls. \n "
" Use the walletpassphrase call for this, and then walletlock call. \n "
" If the wallet is already encrypted, use the walletpassphrasechange call. \n "
" \n Arguments: \n "
" 1. \" passphrase \" (string) The pass phrase to encrypt the wallet with. It must be at least 1 character, but should be long. \n "
" \n Examples: \n "
2017-07-19 22:50:26 +02:00
" \n Encrypt your wallet \n "
2013-10-29 12:29:44 +01:00
+ HelpExampleCli ( " encryptwallet " , " \" my pass phrase \" " ) +
" \n Now set the passphrase to use the wallet, such as for signing or sending bitcoin \n "
+ HelpExampleCli ( " walletpassphrase " , " \" my pass phrase \" " ) +
2017-07-19 22:50:26 +02:00
" \n Now we can do something like sign \n "
2016-11-21 14:39:58 +01:00
+ HelpExampleCli ( " signmessage " , " \" address \" \" test message \" " ) +
2013-10-29 12:29:44 +01:00
" \n Now lock the wallet again by removing the passphrase \n "
+ HelpExampleCli ( " walletlock " , " " ) +
2018-10-02 21:49:18 +02:00
" \n As a JSON-RPC call \n "
2013-10-29 12:29:44 +01:00
+ HelpExampleRpc ( " encryptwallet " , " \" my pass phrase \" " )
) ;
2016-10-25 10:04:23 +02:00
}
2013-10-29 12:29:44 +01:00
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2016-10-25 10:04:23 +02:00
if ( pwallet - > IsCrypted ( ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_WRONG_ENC_STATE , " Error: running with an encrypted wallet, but encryptwallet was called. " ) ;
2016-10-25 10:04:23 +02:00
}
2012-08-21 16:38:57 +02:00
// TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
2016-09-22 09:46:41 +02:00
// Alternately, find a way to make request.params[0] mlock()'d to begin with.
2012-08-21 16:38:57 +02:00
SecureString strWalletPass ;
strWalletPass . reserve ( 100 ) ;
2016-09-22 09:46:41 +02:00
strWalletPass = request . params [ 0 ] . get_str ( ) . c_str ( ) ;
2012-08-21 16:38:57 +02:00
if ( strWalletPass . length ( ) < 1 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2012-08-21 16:38:57 +02:00
" encryptwallet <passphrase> \n "
" Encrypts the wallet with <passphrase>. " ) ;
2016-10-25 10:04:23 +02:00
if ( ! pwallet - > EncryptWallet ( strWalletPass ) ) {
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_ENCRYPTION_FAILED , " Error: Failed to encrypt the wallet. " ) ;
2016-10-25 10:04:23 +02:00
}
2012-08-21 16:38:57 +02:00
2018-02-20 22:09:51 +01:00
return " wallet encrypted; The keypool has been flushed and a new HD seed was generated (if you are using HD). You need to make a new backup. " ;
2012-08-21 16:38:57 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue lockunspent ( const JSONRPCRequest & request )
2012-09-27 19:52:09 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) < 1 | | request . params . size ( ) > 2 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2018-10-23 21:22:28 +02:00
RPCHelpMan { " lockunspent " ,
{
{ " unlock " , RPCArg : : Type : : BOOL , false } ,
{ " transactions " , RPCArg : : Type : : ARR ,
{
{ " " , RPCArg : : Type : : OBJ ,
{
{ " txid " , RPCArg : : Type : : STR_HEX , false } ,
{ " vout " , RPCArg : : Type : : NUM , false } ,
} ,
true } ,
} ,
true } ,
} }
. ToString ( ) +
2013-10-29 12:29:44 +01:00
" \n Updates list of temporarily unspendable outputs. \n "
2014-05-09 21:39:24 +02:00
" Temporarily lock (unlock=false) or unlock (unlock=true) specified transaction outputs. \n "
2016-03-23 16:44:18 +01:00
" If no transaction outputs are specified when unlocking then all current locked transaction outputs are unlocked. \n "
2013-10-29 12:29:44 +01:00
" A locked transaction output will not be chosen by automatic coin selection, when spending bitcoins. \n "
" Locks are stored in memory only. Nodes start with zero locked outputs, and the locked output list \n "
" is always cleared (by virtue of process exit) when a node stops or fails. \n "
" Also see the listunspent call \n "
" \n Arguments: \n "
" 1. unlock (boolean, required) Whether to unlock (true) or lock (false) the specified transactions \n "
2016-03-23 16:44:18 +01:00
" 2. \" transactions \" (string, optional) A json array of objects. Each object the txid (string) vout (numeric) \n "
2013-10-29 12:29:44 +01:00
" [ (json array of json objects) \n "
" { \n "
" \" txid \" : \" id \" , (string) The transaction id \n "
" \" vout \" : n (numeric) The output number \n "
" } \n "
" ,... \n "
" ] \n "
" \n Result: \n "
" true|false (boolean) Whether the command was successful or not \n "
" \n Examples: \n "
" \n List the unspent transactions \n "
+ HelpExampleCli ( " listunspent " , " " ) +
" \n Lock an unspent transaction \n "
+ HelpExampleCli ( " lockunspent " , " false \" [{ \\ \" txid \\ \" : \\ \" a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0 \\ \" , \\ \" vout \\ \" :1}] \" " ) +
" \n List the locked transactions \n "
+ HelpExampleCli ( " listlockunspent " , " " ) +
" \n Unlock the transaction again \n "
+ HelpExampleCli ( " lockunspent " , " true \" [{ \\ \" txid \\ \" : \\ \" a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0 \\ \" , \\ \" vout \\ \" :1}] \" " ) +
2018-10-02 21:49:18 +02:00
" \n As a JSON-RPC call \n "
2013-10-29 12:29:44 +01:00
+ HelpExampleRpc ( " lockunspent " , " false, \" [{ \\ \" txid \\ \" : \\ \" a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0 \\ \" , \\ \" vout \\ \" :1}] \" " )
) ;
2012-09-27 19:52:09 +02:00
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2017-08-15 01:44:02 +02:00
RPCTypeCheckArgument ( request . params [ 0 ] , UniValue : : VBOOL ) ;
2012-09-27 19:52:09 +02:00
2016-09-22 09:46:41 +02:00
bool fUnlock = request . params [ 0 ] . get_bool ( ) ;
2012-09-27 19:52:09 +02:00
2017-08-15 01:44:02 +02:00
if ( request . params [ 1 ] . isNull ( ) ) {
2012-09-27 19:52:09 +02:00
if ( fUnlock )
2016-10-25 09:45:57 +02:00
pwallet - > UnlockAllCoins ( ) ;
2012-09-27 19:52:09 +02:00
return true ;
}
2017-08-15 01:44:02 +02:00
RPCTypeCheckArgument ( request . params [ 1 ] , UniValue : : VARR ) ;
2017-08-18 14:21:40 +02:00
const UniValue & output_params = request . params [ 1 ] ;
// Create and validate the COutPoints first.
std : : vector < COutPoint > outputs ;
outputs . reserve ( output_params . size ( ) ) ;
for ( unsigned int idx = 0 ; idx < output_params . size ( ) ; idx + + ) {
const UniValue & o = output_params [ idx ] . get_obj ( ) ;
2012-09-27 19:52:09 +02:00
2016-06-06 17:50:50 +02:00
RPCTypeCheckObj ( o ,
{
{ " txid " , UniValueType ( UniValue : : VSTR ) } ,
{ " vout " , UniValueType ( UniValue : : VNUM ) } ,
} ) ;
2012-09-27 19:52:09 +02:00
2018-06-08 20:16:07 +02:00
const uint256 txid ( ParseHashO ( o , " txid " ) ) ;
2017-08-18 14:21:40 +02:00
const int nOutput = find_value ( o , " vout " ) . get_int ( ) ;
if ( nOutput < 0 ) {
2013-08-06 13:52:38 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid parameter, vout must be positive " ) ;
2017-08-18 14:21:40 +02:00
}
2012-09-27 19:52:09 +02:00
2018-06-08 20:16:07 +02:00
const COutPoint outpt ( txid , nOutput ) ;
2012-09-27 19:52:09 +02:00
2017-08-18 14:21:40 +02:00
const auto it = pwallet - > mapWallet . find ( outpt . hash ) ;
if ( it = = pwallet - > mapWallet . end ( ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid parameter, unknown transaction " ) ;
}
const CWalletTx & trans = it - > second ;
if ( outpt . n > = trans . tx - > vout . size ( ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid parameter, vout index out of bounds " ) ;
}
2017-07-31 21:30:21 +02:00
if ( pwallet - > IsSpent ( * locked_chain , outpt . hash , outpt . n ) ) {
2017-08-18 14:21:40 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid parameter, expected unspent output " ) ;
}
const bool is_locked = pwallet - > IsLockedCoin ( outpt . hash , outpt . n ) ;
if ( fUnlock & & ! is_locked ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid parameter, expected locked output " ) ;
}
if ( ! fUnlock & & is_locked ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid parameter, output already locked " ) ;
}
outputs . push_back ( outpt ) ;
}
// Atomically set (un)locked status for the outputs.
for ( const COutPoint & outpt : outputs ) {
if ( fUnlock ) pwallet - > UnlockCoin ( outpt ) ;
else pwallet - > LockCoin ( outpt ) ;
2012-09-27 19:52:09 +02:00
}
return true ;
}
2018-05-02 17:14:48 +02:00
static UniValue listlockunspent ( const JSONRPCRequest & request )
2012-09-27 19:52:09 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) > 0 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2012-09-27 19:52:09 +02:00
" listlockunspent \n "
2013-10-29 12:29:44 +01:00
" \n Returns list of temporarily unspendable outputs. \n "
" See the lockunspent call to lock and unlock transactions for spending. \n "
" \n Result: \n "
" [ \n "
" { \n "
" \" txid \" : \" transactionid \" , (string) The transaction id locked \n "
" \" vout \" : n (numeric) The vout value \n "
" } \n "
" ,... \n "
" ] \n "
" \n Examples: \n "
" \n List the unspent transactions \n "
+ HelpExampleCli ( " listunspent " , " " ) +
" \n Lock an unspent transaction \n "
+ HelpExampleCli ( " lockunspent " , " false \" [{ \\ \" txid \\ \" : \\ \" a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0 \\ \" , \\ \" vout \\ \" :1}] \" " ) +
" \n List the locked transactions \n "
+ HelpExampleCli ( " listlockunspent " , " " ) +
" \n Unlock the transaction again \n "
+ HelpExampleCli ( " lockunspent " , " true \" [{ \\ \" txid \\ \" : \\ \" a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0 \\ \" , \\ \" vout \\ \" :1}] \" " ) +
2018-10-02 21:49:18 +02:00
" \n As a JSON-RPC call \n "
2013-10-29 12:29:44 +01:00
+ HelpExampleRpc ( " listlockunspent " , " " )
) ;
2012-09-27 19:52:09 +02:00
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2017-01-27 02:33:45 +01:00
std : : vector < COutPoint > vOutpts ;
2016-10-25 09:45:57 +02:00
pwallet - > ListLockedCoins ( vOutpts ) ;
2012-09-27 19:52:09 +02:00
2015-05-10 14:48:35 +02:00
UniValue ret ( UniValue : : VARR ) ;
2012-09-27 19:52:09 +02:00
2018-06-18 07:58:28 +02:00
for ( const COutPoint & outpt : vOutpts ) {
2015-05-10 14:48:35 +02:00
UniValue o ( UniValue : : VOBJ ) ;
2012-09-27 19:52:09 +02:00
2017-09-22 20:04:07 +02:00
o . pushKV ( " txid " , outpt . hash . GetHex ( ) ) ;
o . pushKV ( " vout " , ( int ) outpt . n ) ;
2012-09-27 19:52:09 +02:00
ret . push_back ( o ) ;
}
return ret ;
}
2018-05-02 17:14:48 +02:00
static UniValue settxfee ( const JSONRPCRequest & request )
2013-12-13 16:06:32 +01:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2018-04-07 18:12:46 +02:00
if ( request . fHelp | | request . params . size ( ) < 1 | | request . params . size ( ) > 1 ) {
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2013-12-13 16:06:32 +01:00
" settxfee amount \n "
2018-04-07 18:12:46 +02:00
" \n Set the transaction fee per kB for this wallet. Overrides the global -paytxfee command line parameter. \n "
2013-12-13 16:06:32 +01:00
" \n Arguments: \n "
2016-05-10 17:54:34 +02:00
" 1. amount (numeric or string, required) The transaction fee in " + CURRENCY_UNIT + " /kB \n "
2013-12-13 16:06:32 +01:00
" \n Result \n "
" true|false (boolean) Returns true if successful \n "
" \n Examples: \n "
+ HelpExampleCli ( " settxfee " , " 0.00001 " )
+ HelpExampleRpc ( " settxfee " , " 0.00001 " )
) ;
2018-04-07 18:12:46 +02:00
}
2013-12-13 16:06:32 +01:00
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2016-09-22 09:46:41 +02:00
CAmount nAmount = AmountFromValue ( request . params [ 0 ] ) ;
2018-08-08 17:52:42 +02:00
CFeeRate tx_fee_rate ( nAmount , 1000 ) ;
if ( tx_fee_rate = = 0 ) {
// automatic selection
} else if ( tx_fee_rate < : : minRelayTxFee ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , strprintf ( " txfee cannot be less than min relay tx fee (%s) " , ::minRelayTxFee.ToString())) ;
} else if ( tx_fee_rate < pwallet - > m_min_fee ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , strprintf ( " txfee cannot be less than wallet min fee (%s) " , pwallet->m_min_fee.ToString())) ;
}
2013-12-13 16:06:32 +01:00
2018-08-08 17:52:42 +02:00
pwallet - > m_pay_tx_fee = tx_fee_rate ;
2013-12-13 16:06:32 +01:00
return true ;
}
2018-05-02 17:14:48 +02:00
static UniValue getwalletinfo ( const JSONRPCRequest & request )
2014-02-21 05:56:04 +01:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 0 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2014-02-21 05:56:04 +01:00
" getwalletinfo \n "
" Returns an object containing various wallet state info. \n "
" \n Result: \n "
" { \n "
2017-05-05 08:53:39 +02:00
" \" walletname \" : xxxxx, (string) the wallet name \n "
" \" walletversion \" : xxxxx, (numeric) the wallet version \n "
" \" balance \" : xxxxxxx, (numeric) the total confirmed balance of the wallet in " + CURRENCY_UNIT + " \n "
" \" unconfirmed_balance \" : xxx, (numeric) the total unconfirmed balance of the wallet in " + CURRENCY_UNIT + " \n "
" \" immature_balance \" : xxxxxx, (numeric) the total immature balance of the wallet in " + CURRENCY_UNIT + " \n "
" \" txcount \" : xxxxxxx, (numeric) the total number of transactions in the wallet \n "
" \" keypoololdest \" : xxxxxx, (numeric) the timestamp (seconds since Unix epoch) of the oldest pre-generated key in the key pool \n "
" \" keypoolsize \" : xxxx, (numeric) how many new keys are pre-generated (only counts external keys) \n "
" \" keypoolsize_hd_internal \" : xxxx, (numeric) how many new keys are pre-generated for internal use (used for change outputs, only appears if the wallet is using this feature, otherwise external keys are used) \n "
" \" unlocked_until \" : ttt, (numeric) the timestamp in seconds since epoch (midnight Jan 1 1970 GMT) that the wallet is unlocked for transfers, or 0 if the wallet is locked \n "
" \" paytxfee \" : x.xxxx, (numeric) the transaction fee configuration, set in " + CURRENCY_UNIT + " /kB \n "
" \" hdseedid \" : \" <hash160> \" (string, optional) the Hash160 of the HD seed (only present when HD is enabled) \n "
" \" hdmasterkeyid \" : \" <hash160> \" (string, optional) alias for hdseedid retained for backwards-compatibility. Will be removed in V0.18. \n "
" \" private_keys_enabled \" : true|false (boolean) false if privatekeys are disabled for this wallet (enforced watch-only wallet) \n "
2014-02-21 05:56:04 +01:00
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " getwalletinfo " , " " )
+ HelpExampleRpc ( " getwalletinfo " , " " )
) ;
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2014-10-19 10:46:17 +02:00
2015-05-10 14:48:35 +02:00
UniValue obj ( UniValue : : VOBJ ) ;
2017-01-26 20:52:57 +01:00
2017-03-28 09:18:20 +02:00
size_t kpExternalSize = pwallet - > KeypoolCountExternalKeys ( ) ;
2017-09-22 20:04:07 +02:00
obj . pushKV ( " walletname " , pwallet - > GetName ( ) ) ;
obj . pushKV ( " walletversion " , pwallet - > GetVersion ( ) ) ;
obj . pushKV ( " balance " , ValueFromAmount ( pwallet - > GetBalance ( ) ) ) ;
obj . pushKV ( " unconfirmed_balance " , ValueFromAmount ( pwallet - > GetUnconfirmedBalance ( ) ) ) ;
obj . pushKV ( " immature_balance " , ValueFromAmount ( pwallet - > GetImmatureBalance ( ) ) ) ;
obj . pushKV ( " txcount " , ( int ) pwallet - > mapWallet . size ( ) ) ;
obj . pushKV ( " keypoololdest " , pwallet - > GetOldestKeyPoolTime ( ) ) ;
obj . pushKV ( " keypoolsize " , ( int64_t ) kpExternalSize ) ;
2018-04-04 17:43:45 +02:00
CKeyID seed_id = pwallet - > GetHDChain ( ) . seed_id ;
if ( ! seed_id . IsNull ( ) & & pwallet - > CanSupportFeature ( FEATURE_HD_SPLIT ) ) {
2017-09-22 20:04:07 +02:00
obj . pushKV ( " keypoolsize_hd_internal " , ( int64_t ) ( pwallet - > GetKeyPoolSize ( ) - kpExternalSize ) ) ;
2017-01-10 16:45:30 +01:00
}
2016-10-25 10:04:23 +02:00
if ( pwallet - > IsCrypted ( ) ) {
2017-09-22 20:04:07 +02:00
obj . pushKV ( " unlocked_until " , pwallet - > nRelockTime ) ;
2016-10-25 10:04:23 +02:00
}
2018-04-07 18:12:46 +02:00
obj . pushKV ( " paytxfee " , ValueFromAmount ( pwallet - > m_pay_tx_fee . GetFeePerK ( ) ) ) ;
2018-04-17 23:42:31 +02:00
if ( ! seed_id . IsNull ( ) ) {
2018-04-04 17:43:45 +02:00
obj . pushKV ( " hdseedid " , seed_id . GetHex ( ) ) ;
2018-04-17 23:42:31 +02:00
obj . pushKV ( " hdmasterkeyid " , seed_id . GetHex ( ) ) ;
}
2017-05-05 08:53:39 +02:00
obj . pushKV ( " private_keys_enabled " , ! pwallet - > IsWalletFlagSet ( WALLET_FLAG_DISABLE_PRIVATE_KEYS ) ) ;
2014-02-21 05:56:04 +01:00
return obj ;
}
2015-03-23 18:47:18 +01:00
2018-09-25 00:55:30 +02:00
static UniValue listwalletdir ( const JSONRPCRequest & request )
{
if ( request . fHelp | | request . params . size ( ) ! = 0 ) {
throw std : : runtime_error (
" listwalletdir \n "
" Returns a list of wallets in the wallet directory. \n "
" { \n "
" \" wallets \" : [ (json array of objects) \n "
" { \n "
" \" name \" : \" name \" (string) The wallet name \n "
" } \n "
" ,... \n "
" ] \n "
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " listwalletdir " , " " )
+ HelpExampleRpc ( " listwalletdir " , " " )
) ;
}
UniValue wallets ( UniValue : : VARR ) ;
for ( const auto & path : ListWalletDir ( ) ) {
UniValue wallet ( UniValue : : VOBJ ) ;
wallet . pushKV ( " name " , path . string ( ) ) ;
wallets . push_back ( wallet ) ;
}
UniValue result ( UniValue : : VOBJ ) ;
result . pushKV ( " wallets " , wallets ) ;
return result ;
}
2018-05-02 17:14:48 +02:00
static UniValue listwallets ( const JSONRPCRequest & request )
2017-06-27 15:46:55 +02:00
{
if ( request . fHelp | | request . params . size ( ) ! = 0 )
throw std : : runtime_error (
" listwallets \n "
" Returns a list of currently loaded wallets. \n "
" For full information on the wallet, use \" getwalletinfo \" \n "
" \n Result: \n "
" [ (json array of strings) \n "
" \" walletname \" (string) the wallet name \n "
" ... \n "
" ] \n "
" \n Examples: \n "
+ HelpExampleCli ( " listwallets " , " " )
+ HelpExampleRpc ( " listwallets " , " " )
) ;
UniValue obj ( UniValue : : VARR ) ;
2018-05-22 17:18:07 +02:00
for ( const std : : shared_ptr < CWallet > & wallet : GetWallets ( ) ) {
if ( ! EnsureWalletIsAvailable ( wallet . get ( ) , request . fHelp ) ) {
2017-06-27 15:46:55 +02:00
return NullUniValue ;
}
2018-05-22 17:18:07 +02:00
LOCK ( wallet - > cs_wallet ) ;
2017-06-27 15:46:55 +02:00
2018-05-22 17:18:07 +02:00
obj . push_back ( wallet - > GetName ( ) ) ;
2017-06-27 15:46:55 +02:00
}
return obj ;
}
2018-04-28 23:36:43 +02:00
static UniValue loadwallet ( const JSONRPCRequest & request )
2018-04-18 22:01:39 +02:00
{
if ( request . fHelp | | request . params . size ( ) ! = 1 )
throw std : : runtime_error (
" loadwallet \" filename \" \n "
" \n Loads a wallet from a wallet file or directory. "
" \n Note that all wallet command-line options used when starting bitcoind will be "
" \n applied to the new wallet (eg -zapwallettxes, upgradewallet, rescan, etc). \n "
" \n Arguments: \n "
" 1. \" filename \" (string, required) The wallet directory or .dat file. \n "
" \n Result: \n "
" { \n "
" \" name \" : <wallet_name>, (string) The wallet name if loaded successfully. \n "
" \" warning \" : <warning>, (string) Warning message if wallet was not loaded cleanly. \n "
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " loadwallet " , " \" test.dat \" " )
+ HelpExampleRpc ( " loadwallet " , " \" test.dat \" " )
) ;
2018-09-28 17:50:18 +02:00
WalletLocation location ( request . params [ 0 ] . get_str ( ) ) ;
2018-04-18 22:01:39 +02:00
std : : string error ;
2018-09-28 17:50:18 +02:00
if ( ! location . Exists ( ) ) {
throw JSONRPCError ( RPC_WALLET_NOT_FOUND , " Wallet " + location . GetName ( ) + " not found. " ) ;
} else if ( fs : : is_directory ( location . GetPath ( ) ) ) {
2018-06-28 19:36:28 +02:00
// The given filename is a directory. Check that there's a wallet.dat file.
2018-09-28 17:50:18 +02:00
fs : : path wallet_dat_file = location . GetPath ( ) / " wallet.dat " ;
2018-06-28 19:36:28 +02:00
if ( fs : : symlink_status ( wallet_dat_file ) . type ( ) = = fs : : file_not_found ) {
2018-09-28 17:50:18 +02:00
throw JSONRPCError ( RPC_WALLET_NOT_FOUND , " Directory " + location . GetName ( ) + " does not contain a wallet.dat file. " ) ;
2018-06-28 19:36:28 +02:00
}
2018-04-18 22:01:39 +02:00
}
std : : string warning ;
2017-05-30 21:55:17 +02:00
if ( ! CWallet : : Verify ( * g_rpc_interfaces - > chain , location , false , error , warning ) ) {
2018-04-18 22:01:39 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , " Wallet file verification failed: " + error ) ;
}
2017-05-30 21:55:17 +02:00
std : : shared_ptr < CWallet > const wallet = CWallet : : CreateWalletFromFile ( * g_rpc_interfaces - > chain , location ) ;
2018-04-18 22:01:39 +02:00
if ( ! wallet ) {
throw JSONRPCError ( RPC_WALLET_ERROR , " Wallet loading failed. " ) ;
}
AddWallet ( wallet ) ;
wallet - > postInitProcess ( ) ;
UniValue obj ( UniValue : : VOBJ ) ;
obj . pushKV ( " name " , wallet - > GetName ( ) ) ;
obj . pushKV ( " warning " , warning ) ;
return obj ;
}
2018-04-28 23:36:43 +02:00
static UniValue createwallet ( const JSONRPCRequest & request )
2018-04-23 18:44:22 +02:00
{
2018-06-13 20:35:41 +02:00
if ( request . fHelp | | request . params . size ( ) < 1 | | request . params . size ( ) > 2 ) {
2018-04-23 18:44:22 +02:00
throw std : : runtime_error (
2018-06-13 20:35:41 +02:00
" createwallet \" wallet_name \" ( disable_private_keys ) \n "
2018-04-23 18:44:22 +02:00
" \n Creates and loads a new wallet. \n "
" \n Arguments: \n "
2018-06-13 20:35:41 +02:00
" 1. \" wallet_name \" (string, required) The name for the new wallet. If this is a path, the wallet will be created at the path location. \n "
" 2. disable_private_keys (boolean, optional, default: false) Disable the possibility of private keys (only watchonlys are possible in this mode). \n "
2018-04-23 18:44:22 +02:00
" \n Result: \n "
" { \n "
2018-04-23 19:14:34 +02:00
" \" name \" : <wallet_name>, (string) The wallet name if created successfully. If the wallet was created using a full path, the wallet_name will be the full path. \n "
2018-04-23 18:44:22 +02:00
" \" warning \" : <warning>, (string) Warning message if wallet was not loaded cleanly. \n "
" } \n "
" \n Examples: \n "
2018-04-23 19:14:34 +02:00
+ HelpExampleCli ( " createwallet " , " \" testwallet \" " )
+ HelpExampleRpc ( " createwallet " , " \" testwallet \" " )
2018-04-23 18:44:22 +02:00
) ;
2018-04-23 19:14:34 +02:00
}
2018-04-23 18:44:22 +02:00
std : : string error ;
std : : string warning ;
2018-06-13 20:35:41 +02:00
bool disable_privatekeys = false ;
if ( ! request . params [ 1 ] . isNull ( ) ) {
disable_privatekeys = request . params [ 1 ] . get_bool ( ) ;
}
2018-09-28 17:50:18 +02:00
WalletLocation location ( request . params [ 0 ] . get_str ( ) ) ;
if ( location . Exists ( ) ) {
throw JSONRPCError ( RPC_WALLET_ERROR , " Wallet " + location . GetName ( ) + " already exists. " ) ;
2018-04-23 18:44:22 +02:00
}
// Wallet::Verify will check if we're trying to create a wallet with a duplication name.
2017-05-30 21:55:17 +02:00
if ( ! CWallet : : Verify ( * g_rpc_interfaces - > chain , location , false , error , warning ) ) {
2018-04-23 18:44:22 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , " Wallet file verification failed: " + error ) ;
}
2017-05-30 21:55:17 +02:00
std : : shared_ptr < CWallet > const wallet = CWallet : : CreateWalletFromFile ( * g_rpc_interfaces - > chain , location , ( disable_privatekeys ? ( uint64_t ) WALLET_FLAG_DISABLE_PRIVATE_KEYS : 0 ) ) ;
2018-04-23 18:44:22 +02:00
if ( ! wallet ) {
throw JSONRPCError ( RPC_WALLET_ERROR , " Wallet creation failed. " ) ;
}
AddWallet ( wallet ) ;
wallet - > postInitProcess ( ) ;
UniValue obj ( UniValue : : VOBJ ) ;
obj . pushKV ( " name " , wallet - > GetName ( ) ) ;
obj . pushKV ( " warning " , warning ) ;
return obj ;
}
2018-04-28 23:36:43 +02:00
static UniValue unloadwallet ( const JSONRPCRequest & request )
{
if ( request . fHelp | | request . params . size ( ) > 1 ) {
throw std : : runtime_error (
" unloadwallet ( \" wallet_name \" ) \n "
" Unloads the wallet referenced by the request endpoint otherwise unloads the wallet specified in the argument. \n "
" Specifying the wallet name on a wallet endpoint is invalid. "
" \n Arguments: \n "
" 1. \" wallet_name \" (string, optional) The name of the wallet to unload. \n "
" \n Examples: \n "
+ HelpExampleCli ( " unloadwallet " , " wallet_name " )
+ HelpExampleRpc ( " unloadwallet " , " wallet_name " )
) ;
}
std : : string wallet_name ;
if ( GetWalletNameFromJSONRPCRequest ( request , wallet_name ) ) {
if ( ! request . params [ 0 ] . isNull ( ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Cannot unload the requested wallet " ) ;
}
} else {
wallet_name = request . params [ 0 ] . get_str ( ) ;
}
std : : shared_ptr < CWallet > wallet = GetWallet ( wallet_name ) ;
if ( ! wallet ) {
throw JSONRPCError ( RPC_WALLET_NOT_FOUND , " Requested wallet does not exist or is not loaded " ) ;
}
// Release the "main" shared pointer and prevent further notifications.
// Note that any attempt to load the same wallet would fail until the wallet
// is destroyed (see CheckUniqueFileid).
if ( ! RemoveWallet ( wallet ) ) {
throw JSONRPCError ( RPC_MISC_ERROR , " Requested wallet already unloaded " ) ;
}
UnregisterValidationInterface ( wallet . get ( ) ) ;
// The wallet can be in use so it's not possible to explicitly unload here.
// Just notify the unload intent so that all shared pointers are released.
// The wallet will be destroyed once the last shared pointer is released.
wallet - > NotifyUnload ( ) ;
// There's no point in waiting for the wallet to unload.
// At this point this method should never fail. The unloading could only
// fail due to an unexpected error which would cause a process termination.
return NullUniValue ;
}
2018-05-02 17:14:48 +02:00
static UniValue resendwallettransactions ( const JSONRPCRequest & request )
2015-03-23 18:47:18 +01:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2016-09-22 09:46:41 +02:00
if ( request . fHelp | | request . params . size ( ) ! = 0 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2015-03-23 18:47:18 +01:00
" resendwallettransactions \n "
" Immediately re-broadcast unconfirmed wallet transactions to all peers. \n "
" Intended only for testing; the wallet code periodically re-broadcasts \n "
" automatically. \n "
2017-08-05 19:48:37 +02:00
" Returns an RPC error if -walletbroadcast is set to false. \n "
2015-03-23 18:47:18 +01:00
" Returns array of transaction ids that were re-broadcast. \n "
) ;
2016-05-25 02:56:17 +02:00
if ( ! g_connman )
throw JSONRPCError ( RPC_CLIENT_P2P_DISABLED , " Error: Peer-to-peer functionality missing or disabled " ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2015-03-23 18:47:18 +01:00
2017-08-05 19:48:37 +02:00
if ( ! pwallet - > GetBroadcastTransactions ( ) ) {
2017-08-07 21:50:01 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , " Error: Wallet transaction broadcasting is disabled with -walletbroadcast " ) ;
2017-08-05 19:48:37 +02:00
}
2017-07-31 21:30:21 +02:00
std : : vector < uint256 > txids = pwallet - > ResendWalletTransactionsBefore ( * locked_chain , GetTime ( ) , g_connman . get ( ) ) ;
2015-05-10 14:48:35 +02:00
UniValue result ( UniValue : : VARR ) ;
2017-06-02 03:18:57 +02:00
for ( const uint256 & txid : txids )
2015-03-23 18:47:18 +01:00
{
result . push_back ( txid . ToString ( ) ) ;
}
return result ;
}
2015-04-12 17:56:32 +02:00
2018-05-02 17:14:48 +02:00
static UniValue listunspent ( const JSONRPCRequest & request )
2015-04-12 17:56:32 +02:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 10:04:23 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2015-05-10 13:35:44 +02:00
return NullUniValue ;
2016-10-25 10:04:23 +02:00
}
2016-01-15 01:55:17 +01:00
2017-03-07 19:17:32 +01:00
if ( request . fHelp | | request . params . size ( ) > 5 )
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2018-10-23 21:22:28 +02:00
RPCHelpMan { " listunspent " ,
{
{ " minconf " , RPCArg : : Type : : NUM , true } ,
{ " maxconf " , RPCArg : : Type : : NUM , true } ,
{ " addresses " , RPCArg : : Type : : ARR ,
{
{ " address " , RPCArg : : Type : : STR_HEX , true } ,
} ,
true } ,
{ " include_unsafe " , RPCArg : : Type : : BOOL , true } ,
{ " query_options " , RPCArg : : Type : : OBJ ,
{
{ " minimumAmount " , RPCArg : : Type : : AMOUNT , true } ,
{ " maximumAmount " , RPCArg : : Type : : AMOUNT , true } ,
{ " maximumCount " , RPCArg : : Type : : NUM , true } ,
{ " minimumSumAmount " , RPCArg : : Type : : AMOUNT , true } ,
} ,
true } ,
} }
. ToString ( ) +
2015-04-13 15:04:08 +02:00
" \n Returns array of unspent transaction outputs \n "
" with between minconf and maxconf (inclusive) confirmations. \n "
" Optionally filter to only include txouts paid to specified addresses. \n "
" \n Arguments: \n "
" 1. minconf (numeric, optional, default=1) The minimum confirmations to filter \n "
" 2. maxconf (numeric, optional, default=9999999) The maximum confirmations to filter \n "
2017-03-07 19:17:32 +01:00
" 3. \" addresses \" (string) A json array of bitcoin addresses to filter \n "
2015-04-13 15:04:08 +02:00
" [ \n "
2017-03-07 19:17:32 +01:00
" \" address \" (string) bitcoin address \n "
2015-04-13 15:04:08 +02:00
" ,... \n "
" ] \n "
2017-01-17 16:40:41 +01:00
" 4. include_unsafe (bool, optional, default=true) Include outputs that are not safe to spend \n "
2017-02-23 06:58:31 +01:00
" See description of \" safe \" attribute below. \n "
2017-03-07 19:17:32 +01:00
" 5. query_options (json, optional) JSON with query options \n "
" { \n "
" \" minimumAmount \" (numeric or string, default=0) Minimum value of each UTXO in " + CURRENCY_UNIT + " \n "
" \" maximumAmount \" (numeric or string, default=unlimited) Maximum value of each UTXO in " + CURRENCY_UNIT + " \n "
" \" maximumCount \" (numeric or string, default=unlimited) Maximum number of UTXOs \n "
" \" minimumSumAmount \" (numeric or string, default=unlimited) Minimum sum value of all UTXOs in " + CURRENCY_UNIT + " \n "
" } \n "
2015-04-13 15:04:08 +02:00
" \n Result \n "
" [ (array of json object) \n "
" { \n "
2016-03-08 11:37:18 +01:00
" \" txid \" : \" txid \" , (string) the transaction id \n "
2015-04-13 15:04:08 +02:00
" \" vout \" : n, (numeric) the vout value \n "
2016-03-08 11:37:18 +01:00
" \" address \" : \" address \" , (string) the bitcoin address \n "
2017-10-20 19:27:55 +02:00
" \" label \" : \" label \" , (string) The associated label, or \" \" for the default label \n "
2016-03-08 11:37:18 +01:00
" \" scriptPubKey \" : \" key \" , (string) the script key \n "
2017-02-01 18:18:31 +01:00
" \" amount \" : x.xxx, (numeric) the transaction output amount in " + CURRENCY_UNIT + " \n "
2016-03-14 19:22:11 +01:00
" \" confirmations \" : n, (numeric) The number of confirmations \n "
2016-03-08 11:37:18 +01:00
" \" redeemScript \" : n (string) The redeemScript if scriptPubKey is P2SH \n "
2016-03-14 19:22:11 +01:00
" \" spendable \" : xxx, (bool) Whether we have the private keys to spend this output \n "
2017-02-23 06:58:31 +01:00
" \" solvable \" : xxx, (bool) Whether we know how to spend this output, ignoring the lack of keys \n "
" \" safe \" : xxx (bool) Whether this output is considered safe to spend. Unconfirmed transactions \n "
" from outside keys and unconfirmed replacement transactions are considered unsafe \n "
" and are not eligible for spending by fundrawtransaction and sendtoaddress. \n "
2015-04-13 15:04:08 +02:00
" } \n "
" ,... \n "
" ] \n "
" \n Examples \n "
+ HelpExampleCli ( " listunspent " , " " )
+ HelpExampleCli ( " listunspent " , " 6 9999999 \" [ \\ \" 1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg \\ \" , \\ \" 1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP \\ \" ] \" " )
+ HelpExampleRpc ( " listunspent " , " 6, 9999999 \" [ \\ \" 1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg \\ \" , \\ \" 1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP \\ \" ] \" " )
2017-03-07 19:17:32 +01:00
+ HelpExampleCli ( " listunspent " , " 6 9999999 '[]' true '{ \" minimumAmount \" : 0.005 }' " )
+ HelpExampleRpc ( " listunspent " , " 6, 9999999, [] , true, { \" minimumAmount \" : 0.005 } " )
2015-04-13 15:04:08 +02:00
) ;
2015-04-12 17:56:32 +02:00
int nMinDepth = 1 ;
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 0 ] . isNull ( ) ) {
2017-01-17 16:40:41 +01:00
RPCTypeCheckArgument ( request . params [ 0 ] , UniValue : : VNUM ) ;
2016-09-22 09:46:41 +02:00
nMinDepth = request . params [ 0 ] . get_int ( ) ;
2017-01-17 16:40:41 +01:00
}
2015-04-13 15:04:08 +02:00
2015-04-12 17:56:32 +02:00
int nMaxDepth = 9999999 ;
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 1 ] . isNull ( ) ) {
2017-01-17 16:40:41 +01:00
RPCTypeCheckArgument ( request . params [ 1 ] , UniValue : : VNUM ) ;
2016-09-22 09:46:41 +02:00
nMaxDepth = request . params [ 1 ] . get_int ( ) ;
2017-01-17 16:40:41 +01:00
}
2015-04-13 15:04:08 +02:00
2017-08-23 03:02:33 +02:00
std : : set < CTxDestination > destinations ;
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 2 ] . isNull ( ) ) {
2017-01-17 16:40:41 +01:00
RPCTypeCheckArgument ( request . params [ 2 ] , UniValue : : VARR ) ;
2016-09-22 09:46:41 +02:00
UniValue inputs = request . params [ 2 ] . get_array ( ) ;
2015-05-10 13:35:44 +02:00
for ( unsigned int idx = 0 ; idx < inputs . size ( ) ; idx + + ) {
2015-05-18 14:02:18 +02:00
const UniValue & input = inputs [ idx ] ;
2017-08-23 03:02:33 +02:00
CTxDestination dest = DecodeDestination ( input . get_str ( ) ) ;
if ( ! IsValidDestination ( dest ) ) {
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , std : : string ( " Invalid Bitcoin address: " ) + input . get_str ( ) ) ;
}
if ( ! destinations . insert ( dest ) . second ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , std : : string ( " Invalid parameter, duplicated address: " ) + input . get_str ( ) ) ;
}
2015-04-12 17:56:32 +02:00
}
}
2015-04-13 15:04:08 +02:00
2017-01-17 16:40:41 +01:00
bool include_unsafe = true ;
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 3 ] . isNull ( ) ) {
2017-01-17 16:40:41 +01:00
RPCTypeCheckArgument ( request . params [ 3 ] , UniValue : : VBOOL ) ;
include_unsafe = request . params [ 3 ] . get_bool ( ) ;
}
2017-03-07 19:17:32 +01:00
CAmount nMinimumAmount = 0 ;
CAmount nMaximumAmount = MAX_MONEY ;
CAmount nMinimumSumAmount = MAX_MONEY ;
uint64_t nMaximumCount = 0 ;
2017-07-10 17:44:39 +02:00
if ( ! request . params [ 4 ] . isNull ( ) ) {
2017-03-07 19:17:32 +01:00
const UniValue & options = request . params [ 4 ] . get_obj ( ) ;
if ( options . exists ( " minimumAmount " ) )
nMinimumAmount = AmountFromValue ( options [ " minimumAmount " ] ) ;
if ( options . exists ( " maximumAmount " ) )
nMaximumAmount = AmountFromValue ( options [ " maximumAmount " ] ) ;
if ( options . exists ( " minimumSumAmount " ) )
nMinimumSumAmount = AmountFromValue ( options [ " minimumSumAmount " ] ) ;
if ( options . exists ( " maximumCount " ) )
nMaximumCount = options [ " maximumCount " ] . get_int64 ( ) ;
}
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2015-05-10 14:48:35 +02:00
UniValue results ( UniValue : : VARR ) ;
2017-01-27 02:33:45 +01:00
std : : vector < COutput > vecOutputs ;
2018-05-03 12:31:21 +02:00
{
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2017-07-31 21:30:21 +02:00
pwallet - > AvailableCoins ( * locked_chain , vecOutputs , ! include_unsafe , nullptr , nMinimumAmount , nMaximumAmount , nMinimumSumAmount , nMaximumCount , nMinDepth , nMaxDepth ) ;
2018-05-03 12:31:21 +02:00
}
LOCK ( pwallet - > cs_wallet ) ;
2015-04-13 15:04:08 +02:00
2017-06-02 03:18:57 +02:00
for ( const COutput & out : vecOutputs ) {
2016-03-08 11:37:18 +01:00
CTxDestination address ;
2016-11-12 01:54:51 +01:00
const CScript & scriptPubKey = out . tx - > tx - > vout [ out . i ] . scriptPubKey ;
2016-03-08 11:37:18 +01:00
bool fValidAddress = ExtractDestination ( scriptPubKey , address ) ;
2015-04-13 15:04:08 +02:00
2017-08-23 03:02:33 +02:00
if ( destinations . size ( ) & & ( ! fValidAddress | | ! destinations . count ( address ) ) )
2016-03-08 11:37:18 +01:00
continue ;
2015-04-13 15:04:08 +02:00
2015-05-10 14:48:35 +02:00
UniValue entry ( UniValue : : VOBJ ) ;
2017-09-22 20:04:07 +02:00
entry . pushKV ( " txid " , out . tx - > GetHash ( ) . GetHex ( ) ) ;
entry . pushKV ( " vout " , out . i ) ;
2016-03-08 11:37:18 +01:00
if ( fValidAddress ) {
2017-09-22 20:04:07 +02:00
entry . pushKV ( " address " , EncodeDestination ( address ) ) ;
2016-03-08 11:37:18 +01:00
2018-05-03 12:32:57 +02:00
auto i = pwallet - > mapAddressBook . find ( address ) ;
if ( i ! = pwallet - > mapAddressBook . end ( ) ) {
entry . pushKV ( " label " , i - > second . name ) ;
2016-10-25 10:04:23 +02:00
}
2016-03-08 11:37:18 +01:00
if ( scriptPubKey . IsPayToScriptHash ( ) ) {
2015-04-28 17:38:50 +02:00
const CScriptID & hash = boost : : get < CScriptID > ( address ) ;
2015-04-12 17:56:32 +02:00
CScript redeemScript ;
2016-10-25 10:04:23 +02:00
if ( pwallet - > GetCScript ( hash , redeemScript ) ) {
2017-09-22 20:04:07 +02:00
entry . pushKV ( " redeemScript " , HexStr ( redeemScript . begin ( ) , redeemScript . end ( ) ) ) ;
2016-10-25 10:04:23 +02:00
}
2015-04-12 17:56:32 +02:00
}
}
2016-03-08 11:37:18 +01:00
2017-09-22 20:04:07 +02:00
entry . pushKV ( " scriptPubKey " , HexStr ( scriptPubKey . begin ( ) , scriptPubKey . end ( ) ) ) ;
entry . pushKV ( " amount " , ValueFromAmount ( out . tx - > tx - > vout [ out . i ] . nValue ) ) ;
entry . pushKV ( " confirmations " , out . nDepth ) ;
entry . pushKV ( " spendable " , out . fSpendable ) ;
entry . pushKV ( " solvable " , out . fSolvable ) ;
entry . pushKV ( " safe " , out . fSafe ) ;
2015-04-12 17:56:32 +02:00
results . push_back ( entry ) ;
}
2015-04-13 15:04:08 +02:00
2015-04-12 17:56:32 +02:00
return results ;
2015-06-06 09:41:12 +02:00
}
2015-04-25 03:27:30 +02:00
2018-06-28 02:02:07 +02:00
void FundTransaction ( CWallet * const pwallet , CMutableTransaction & tx , CAmount & fee_out , int & change_position , UniValue options )
2015-04-25 03:27:30 +02:00
{
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-02-02 23:33:24 +01:00
CCoinControl coinControl ;
2018-06-28 02:02:07 +02:00
change_position = - 1 ;
2016-04-06 16:56:14 +02:00
bool lockUnspents = false ;
2016-12-13 22:36:23 +01:00
UniValue subtractFeeFromOutputs ;
2017-01-27 02:33:45 +01:00
std : : set < int > setSubtractFeeFromOutputs ;
2016-03-30 03:04:22 +02:00
2018-06-28 02:02:07 +02:00
if ( ! options . isNull ( ) ) {
if ( options . type ( ) = = UniValue : : VBOOL ) {
2016-03-30 03:04:22 +02:00
// backward compatibility bool only fallback
2018-06-28 02:02:07 +02:00
coinControl . fAllowWatchOnly = options . get_bool ( ) ;
2016-03-30 03:04:22 +02:00
}
else {
2018-06-28 02:02:07 +02:00
RPCTypeCheckArgument ( options , UniValue : : VOBJ ) ;
2016-06-06 17:50:50 +02:00
RPCTypeCheckObj ( options ,
{
{ " changeAddress " , UniValueType ( UniValue : : VSTR ) } ,
{ " changePosition " , UniValueType ( UniValue : : VNUM ) } ,
2018-01-16 01:20:17 +01:00
{ " change_type " , UniValueType ( UniValue : : VSTR ) } ,
2016-06-06 17:50:50 +02:00
{ " includeWatching " , UniValueType ( UniValue : : VBOOL ) } ,
{ " lockUnspents " , UniValueType ( UniValue : : VBOOL ) } ,
{ " feeRate " , UniValueType ( ) } , // will be checked below
2016-12-13 22:36:23 +01:00
{ " subtractFeeFromOutputs " , UniValueType ( UniValue : : VARR ) } ,
2017-06-28 23:53:06 +02:00
{ " replaceable " , UniValueType ( UniValue : : VBOOL ) } ,
2017-06-14 21:15:40 +02:00
{ " conf_target " , UniValueType ( UniValue : : VNUM ) } ,
{ " estimate_mode " , UniValueType ( UniValue : : VSTR ) } ,
2016-06-06 17:50:50 +02:00
} ,
true , true ) ;
2016-03-30 03:04:22 +02:00
if ( options . exists ( " changeAddress " ) ) {
2017-08-23 03:02:33 +02:00
CTxDestination dest = DecodeDestination ( options [ " changeAddress " ] . get_str ( ) ) ;
2016-03-30 03:04:22 +02:00
2017-08-23 03:02:33 +02:00
if ( ! IsValidDestination ( dest ) ) {
2017-02-07 20:27:57 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " changeAddress must be a valid bitcoin address " ) ;
2017-08-23 03:02:33 +02:00
}
2016-03-30 03:04:22 +02:00
2017-08-23 03:02:33 +02:00
coinControl . destChange = dest ;
2016-03-30 03:04:22 +02:00
}
if ( options . exists ( " changePosition " ) )
2018-06-28 02:02:07 +02:00
change_position = options [ " changePosition " ] . get_int ( ) ;
2016-03-30 03:04:22 +02:00
2018-01-16 01:20:17 +01:00
if ( options . exists ( " change_type " ) ) {
if ( options . exists ( " changeAddress " ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Cannot specify both changeAddress and address_type options " ) ;
}
2018-03-19 20:57:11 +01:00
coinControl . m_change_type = pwallet - > m_default_change_type ;
if ( ! ParseOutputType ( options [ " change_type " ] . get_str ( ) , * coinControl . m_change_type ) ) {
2018-01-16 01:20:17 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , strprintf ( " Unknown change type '%s' " , options [ " change_type " ] . get_str ( ) ) ) ;
}
}
2016-03-30 03:04:22 +02:00
if ( options . exists ( " includeWatching " ) )
2017-02-02 23:33:24 +01:00
coinControl . fAllowWatchOnly = options [ " includeWatching " ] . get_bool ( ) ;
2016-04-06 16:56:14 +02:00
if ( options . exists ( " lockUnspents " ) )
lockUnspents = options [ " lockUnspents " ] . get_bool ( ) ;
2016-04-28 22:04:07 +02:00
if ( options . exists ( " feeRate " ) )
2016-05-06 11:01:50 +02:00
{
2017-06-28 23:23:46 +02:00
coinControl . m_feerate = CFeeRate ( AmountFromValue ( options [ " feeRate " ] ) ) ;
2017-02-02 23:33:24 +01:00
coinControl . fOverrideFeeRate = true ;
2016-05-06 11:01:50 +02:00
}
2016-12-13 22:36:23 +01:00
if ( options . exists ( " subtractFeeFromOutputs " ) )
subtractFeeFromOutputs = options [ " subtractFeeFromOutputs " ] . get_array ( ) ;
2017-02-02 23:36:50 +01:00
2017-06-28 23:53:06 +02:00
if ( options . exists ( " replaceable " ) ) {
2018-04-07 18:12:46 +02:00
coinControl . m_signal_bip125_rbf = options [ " replaceable " ] . get_bool ( ) ;
2017-02-02 23:36:50 +01:00
}
2017-06-14 21:15:40 +02:00
if ( options . exists ( " conf_target " ) ) {
2017-07-12 02:59:46 +02:00
if ( options . exists ( " feeRate " ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Cannot specify both conf_target and feeRate " ) ;
}
2017-07-12 20:42:57 +02:00
coinControl . m_confirm_target = ParseConfirmTarget ( options [ " conf_target " ] ) ;
2017-06-14 21:15:40 +02:00
}
if ( options . exists ( " estimate_mode " ) ) {
2017-07-12 02:59:46 +02:00
if ( options . exists ( " feeRate " ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Cannot specify both estimate_mode and feeRate " ) ;
}
2017-06-14 21:15:40 +02:00
if ( ! FeeModeFromString ( options [ " estimate_mode " ] . get_str ( ) , coinControl . m_fee_mode ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid estimate_mode parameter " ) ;
}
}
2016-03-30 03:04:22 +02:00
}
}
2015-04-25 03:27:30 +02:00
2016-11-30 23:50:20 +01:00
if ( tx . vout . size ( ) = = 0 )
2015-11-13 21:52:07 +01:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " TX must have at least one output " ) ;
2018-06-28 02:02:07 +02:00
if ( change_position ! = - 1 & & ( change_position < 0 | | ( unsigned int ) change_position > tx . vout . size ( ) ) )
2016-03-30 03:04:22 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " changePosition out of bounds " ) ;
2015-04-24 06:42:49 +02:00
2016-12-13 22:36:23 +01:00
for ( unsigned int idx = 0 ; idx < subtractFeeFromOutputs . size ( ) ; idx + + ) {
int pos = subtractFeeFromOutputs [ idx ] . get_int ( ) ;
if ( setSubtractFeeFromOutputs . count ( pos ) )
throw JSONRPCError ( RPC_INVALID_PARAMETER , strprintf ( " Invalid parameter, duplicated position: %d " , pos ) ) ;
if ( pos < 0 )
throw JSONRPCError ( RPC_INVALID_PARAMETER , strprintf ( " Invalid parameter, negative position: %d " , pos ) ) ;
if ( pos > = int ( tx . vout . size ( ) ) )
throw JSONRPCError ( RPC_INVALID_PARAMETER , strprintf ( " Invalid parameter, position too large: %d " , pos ) ) ;
setSubtractFeeFromOutputs . insert ( pos ) ;
}
2017-01-27 02:33:45 +01:00
std : : string strFailReason ;
2016-03-30 03:04:22 +02:00
2018-06-28 02:02:07 +02:00
if ( ! pwallet - > FundTransaction ( tx , fee_out , change_position , strFailReason , lockUnspents , setSubtractFeeFromOutputs , coinControl ) ) {
2017-02-09 21:48:39 +01:00
throw JSONRPCError ( RPC_WALLET_ERROR , strFailReason ) ;
2016-10-25 10:04:23 +02:00
}
2018-06-28 02:02:07 +02:00
}
static UniValue fundrawtransaction ( const JSONRPCRequest & request )
{
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
return NullUniValue ;
}
if ( request . fHelp | | request . params . size ( ) < 1 | | request . params . size ( ) > 3 )
throw std : : runtime_error (
" fundrawtransaction \" hexstring \" ( options iswitness ) \n "
" \n Add inputs to a transaction until it has enough in value to meet its out value. \n "
" This will not modify existing inputs, and will add at most one change output to the outputs. \n "
" No existing outputs will be modified unless \" subtractFeeFromOutputs \" is specified. \n "
" Note that inputs which were signed may need to be resigned after completion since in/outputs have been added. \n "
" The inputs added will not be signed, use signrawtransaction for that. \n "
" Note that all existing inputs must have their previous output transaction be in the wallet. \n "
" Note that all inputs selected must be of standard form and P2SH scripts must be \n "
" in the wallet using importaddress or addmultisigaddress (to calculate fees). \n "
" You can see whether this is the case by checking the \" solvable \" field in the listunspent output. \n "
" Only pay-to-pubkey, multisig, and P2SH versions thereof are currently supported for watch-only \n "
" \n Arguments: \n "
" 1. \" hexstring \" (string, required) The hex string of the raw transaction \n "
" 2. options (object, optional) \n "
" { \n "
" \" changeAddress \" (string, optional, default pool address) The bitcoin address to receive the change \n "
" \" changePosition \" (numeric, optional, default random) The index of the change output \n "
" \" change_type \" (string, optional) The output type to use. Only valid if changeAddress is not specified. Options are \" legacy \" , \" p2sh-segwit \" , and \" bech32 \" . Default is set by -changetype. \n "
" \" includeWatching \" (boolean, optional, default false) Also select inputs which are watch only \n "
" \" lockUnspents \" (boolean, optional, default false) Lock selected unspent outputs \n "
" \" feeRate \" (numeric, optional, default not set: makes wallet determine the fee) Set a specific fee rate in " + CURRENCY_UNIT + " /kB \n "
" \" subtractFeeFromOutputs \" (array, optional) A json array of integers. \n "
" The fee will be equally deducted from the amount of each specified output. \n "
" The outputs are specified by their zero-based index, before any change output is added. \n "
" Those recipients will receive less bitcoins than you enter in their corresponding amount field. \n "
" If no outputs are specified here, the sender pays the fee. \n "
" [vout_index,...] \n "
" \" replaceable \" (boolean, optional) Marks this transaction as BIP125 replaceable. \n "
" Allows this transaction to be replaced by a transaction with higher fees \n "
" \" conf_target \" (numeric, optional) Confirmation target (in blocks) \n "
" \" estimate_mode \" (string, optional, default=UNSET) The fee estimate mode, must be one of: \n "
" \" UNSET \" \n "
" \" ECONOMICAL \" \n "
" \" CONSERVATIVE \" \n "
" } \n "
" for backward compatibility: passing in a true instead of an object will result in { \" includeWatching \" :true} \n "
" 3. iswitness (boolean, optional) Whether the transaction hex is a serialized witness transaction \n "
" If iswitness is not present, heuristic tests will be used in decoding \n "
" \n Result: \n "
" { \n "
" \" hex \" : \" value \" , (string) The resulting raw transaction (hex-encoded string) \n "
" \" fee \" : n, (numeric) Fee in " + CURRENCY_UNIT + " the resulting transaction pays \n "
" \" changepos \" : n (numeric) The position of the added change output, or -1 \n "
" } \n "
" \n Examples: \n "
" \n Create a transaction with no inputs \n "
+ HelpExampleCli ( " createrawtransaction " , " \" [] \" \" { \\ \" myaddress \\ \" :0.01} \" " ) +
" \n Add sufficient unsigned inputs to meet the output value \n "
+ HelpExampleCli ( " fundrawtransaction " , " \" rawtransactionhex \" " ) +
" \n Sign the transaction \n "
+ HelpExampleCli ( " signrawtransaction " , " \" fundedtransactionhex \" " ) +
" \n Send the transaction \n "
+ HelpExampleCli ( " sendrawtransaction " , " \" signedtransactionhex \" " )
) ;
RPCTypeCheck ( request . params , { UniValue : : VSTR , UniValueType ( ) , UniValue : : VBOOL } ) ;
// parse hex string from parameter
CMutableTransaction tx ;
bool try_witness = request . params [ 2 ] . isNull ( ) ? true : request . params [ 2 ] . get_bool ( ) ;
bool try_no_witness = request . params [ 2 ] . isNull ( ) ? true : ! request . params [ 2 ] . get_bool ( ) ;
if ( ! DecodeHexTx ( tx , request . params [ 0 ] . get_str ( ) , try_no_witness , try_witness ) ) {
throw JSONRPCError ( RPC_DESERIALIZATION_ERROR , " TX decode failed " ) ;
}
CAmount fee ;
int change_position ;
FundTransaction ( pwallet , tx , fee , change_position , request . params [ 1 ] ) ;
2015-04-25 03:27:30 +02:00
UniValue result ( UniValue : : VOBJ ) ;
2017-09-22 20:04:07 +02:00
result . pushKV ( " hex " , EncodeHexTx ( tx ) ) ;
2018-06-28 02:02:07 +02:00
result . pushKV ( " fee " , ValueFromAmount ( fee ) ) ;
result . pushKV ( " changepos " , change_position ) ;
2015-04-25 03:27:30 +02:00
return result ;
}
2016-01-07 08:33:49 +01:00
2017-06-12 21:23:02 +02:00
UniValue signrawtransactionwithwallet ( const JSONRPCRequest & request )
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2017-06-12 21:23:02 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
return NullUniValue ;
}
if ( request . fHelp | | request . params . size ( ) < 1 | | request . params . size ( ) > 3 )
throw std : : runtime_error (
2018-10-23 21:22:28 +02:00
RPCHelpMan { " signrawtransactionwithwallet " ,
{
{ " hexstring " , RPCArg : : Type : : STR , false } ,
{ " prevtxs " , RPCArg : : Type : : ARR ,
{
{ " " , RPCArg : : Type : : OBJ ,
{
{ " txid " , RPCArg : : Type : : STR_HEX , false } ,
{ " vout " , RPCArg : : Type : : NUM , false } ,
{ " scriptPubKey " , RPCArg : : Type : : STR_HEX , false } ,
{ " redeemScript " , RPCArg : : Type : : STR_HEX , false } ,
{ " amount " , RPCArg : : Type : : AMOUNT , false } ,
} ,
false } ,
} ,
true } ,
{ " sighashtype " , RPCArg : : Type : : STR , true } ,
} }
. ToString ( ) +
2017-06-12 21:23:02 +02:00
" \n Sign inputs for raw transaction (serialized, hex-encoded). \n "
" The second optional argument (may be null) is an array of previous transaction outputs that \n "
" this transaction depends on but may not yet be in the block chain. \n "
+ HelpRequiringPassphrase ( pwallet ) + " \n "
" \n Arguments: \n "
" 1. \" hexstring \" (string, required) The transaction hex string \n "
" 2. \" prevtxs \" (string, optional) An json array of previous dependent transaction outputs \n "
" [ (json array of json objects, or 'null' if none provided) \n "
" { \n "
" \" txid \" : \" id \" , (string, required) The transaction id \n "
" \" vout \" :n, (numeric, required) The output number \n "
" \" scriptPubKey \" : \" hex \" , (string, required) script key \n "
" \" redeemScript \" : \" hex \" , (string, required for P2SH or P2WSH) redeem script \n "
" \" amount \" : value (numeric, required) The amount spent \n "
" } \n "
" ,... \n "
" ] \n "
" 3. \" sighashtype \" (string, optional, default=ALL) The signature hash type. Must be one of \n "
" \" ALL \" \n "
" \" NONE \" \n "
" \" SINGLE \" \n "
" \" ALL|ANYONECANPAY \" \n "
" \" NONE|ANYONECANPAY \" \n "
" \" SINGLE|ANYONECANPAY \" \n "
" \n Result: \n "
" { \n "
" \" hex \" : \" value \" , (string) The hex-encoded raw transaction with signature(s) \n "
" \" complete \" : true|false, (boolean) If the transaction has a complete set of signatures \n "
" \" errors \" : [ (json array of objects) Script verification errors (if there are any) \n "
" { \n "
" \" txid \" : \" hash \" , (string) The hash of the referenced, previous transaction \n "
" \" vout \" : n, (numeric) The index of the output to spent and used as input \n "
" \" scriptSig \" : \" hex \" , (string) The hex-encoded signature script \n "
" \" sequence \" : n, (numeric) Script sequence number \n "
" \" error \" : \" text \" (string) Verification or signing error related to the input \n "
" } \n "
" ,... \n "
" ] \n "
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " signrawtransactionwithwallet " , " \" myhex \" " )
+ HelpExampleRpc ( " signrawtransactionwithwallet " , " \" myhex \" " )
) ;
RPCTypeCheck ( request . params , { UniValue : : VSTR , UniValue : : VARR , UniValue : : VSTR } , true ) ;
CMutableTransaction mtx ;
if ( ! DecodeHexTx ( mtx , request . params [ 0 ] . get_str ( ) , true ) ) {
throw JSONRPCError ( RPC_DESERIALIZATION_ERROR , " TX decode failed " ) ;
}
// Sign the transaction
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2018-09-24 17:10:23 +02:00
EnsureWalletIsUnlocked ( pwallet ) ;
2017-05-30 21:55:17 +02:00
return SignTransaction ( pwallet - > chain ( ) , mtx , request . params [ 1 ] , pwallet , false , request . params [ 2 ] ) ;
2017-06-12 21:23:02 +02:00
}
2018-05-02 17:14:48 +02:00
static UniValue bumpfee ( const JSONRPCRequest & request )
2016-12-09 19:45:27 +01:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-10-25 09:45:57 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) )
2016-12-09 19:45:27 +01:00
return NullUniValue ;
if ( request . fHelp | | request . params . size ( ) < 1 | | request . params . size ( ) > 2 ) {
2017-01-27 02:33:45 +01:00
throw std : : runtime_error (
2016-12-09 19:45:27 +01:00
" bumpfee \" txid \" ( options ) \n "
" \n Bumps the fee of an opt-in-RBF transaction T, replacing it with a new transaction B. \n "
" An opt-in RBF transaction with the given txid must be in the wallet. \n "
" The command will pay the additional fee by decreasing (or perhaps removing) its change output. \n "
" If the change output is not big enough to cover the increased fee, the command will currently fail \n "
" instead of adding new inputs to compensate. (A future implementation could improve this.) \n "
" The command will fail if the wallet or mempool contains a transaction that spends one of T's outputs. \n "
2018-02-02 16:09:04 +01:00
" By default, the new fee will be calculated automatically using estimatesmartfee. \n "
" The user can specify a confirmation target for estimatesmartfee. \n "
2017-05-17 04:13:40 +02:00
" Alternatively, the user can specify totalFee, or use RPC settxfee to set a higher fee rate. \n "
2017-01-20 03:18:46 +01:00
" At a minimum, the new fee rate must be high enough to pay an additional new relay fee (incrementalfee \n "
" returned by getnetworkinfo) to enter the node's mempool. \n "
2016-12-09 19:45:27 +01:00
" \n Arguments: \n "
" 1. txid (string, required) The txid to be bumped \n "
" 2. options (object, optional) \n "
" { \n "
" \" confTarget \" (numeric, optional) Confirmation target (in blocks) \n "
" \" totalFee \" (numeric, optional) Total fee (NOT feerate) to pay, in satoshis. \n "
" In rare cases, the actual fee paid might be slightly higher than the specified \n "
" totalFee if the tx change output has to be removed because it is too close to \n "
" the dust threshold. \n "
" \" replaceable \" (boolean, optional, default true) Whether the new transaction should still be \n "
" marked bip-125 replaceable. If true, the sequence numbers in the transaction will \n "
" be left unchanged from the original. If false, any input sequence numbers in the \n "
" original transaction that were less than 0xfffffffe will be increased to 0xfffffffe \n "
" so the new transaction will not be explicitly bip-125 replaceable (though it may \n "
2017-02-06 15:16:18 +01:00
" still be replaceable in practice, for example if it has unconfirmed ancestors which \n "
2016-12-09 19:45:27 +01:00
" are replaceable). \n "
2017-06-14 21:15:40 +02:00
" \" estimate_mode \" (string, optional, default=UNSET) The fee estimate mode, must be one of: \n "
" \" UNSET \" \n "
" \" ECONOMICAL \" \n "
" \" CONSERVATIVE \" \n "
2016-12-09 19:45:27 +01:00
" } \n "
" \n Result: \n "
" { \n "
" \" txid \" : \" value \" , (string) The id of the new transaction \n "
2017-01-23 19:15:48 +01:00
" \" origfee \" : n, (numeric) Fee of the replaced transaction \n "
" \" fee \" : n, (numeric) Fee of the new transaction \n "
2017-01-26 18:57:42 +01:00
" \" errors \" : [ str... ] (json array of strings) Errors encountered during processing (may be empty) \n "
2016-12-09 19:45:27 +01:00
" } \n "
" \n Examples: \n "
" \n Bump the fee, get the new transaction \' s txid \n " +
HelpExampleCli ( " bumpfee " , " <txid> " ) ) ;
}
2017-06-06 21:15:28 +02:00
RPCTypeCheck ( request . params , { UniValue : : VSTR , UniValue : : VOBJ } ) ;
2018-06-08 20:16:07 +02:00
uint256 hash ( ParseHashV ( request . params [ 0 ] , " txid " ) ) ;
2016-12-09 19:45:27 +01:00
2017-02-02 22:03:55 +01:00
// optional parameters
CAmount totalFee = 0 ;
2017-06-28 23:23:46 +02:00
CCoinControl coin_control ;
2018-04-07 18:12:46 +02:00
coin_control . m_signal_bip125_rbf = true ;
2017-07-10 17:44:39 +02:00
if ( ! request . params [ 1 ] . isNull ( ) ) {
2017-02-02 22:03:55 +01:00
UniValue options = request . params [ 1 ] ;
RPCTypeCheckObj ( options ,
{
{ " confTarget " , UniValueType ( UniValue : : VNUM ) } ,
{ " totalFee " , UniValueType ( UniValue : : VNUM ) } ,
{ " replaceable " , UniValueType ( UniValue : : VBOOL ) } ,
2017-06-14 21:15:40 +02:00
{ " estimate_mode " , UniValueType ( UniValue : : VSTR ) } ,
2017-02-02 22:03:55 +01:00
} ,
true , true ) ;
if ( options . exists ( " confTarget " ) & & options . exists ( " totalFee " ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " confTarget and totalFee options should not both be set. Please provide either a confirmation target for fee estimation or an explicit total fee for the transaction. " ) ;
2017-07-12 20:42:57 +02:00
} else if ( options . exists ( " confTarget " ) ) { // TODO: alias this to conf_target
coin_control . m_confirm_target = ParseConfirmTarget ( options [ " confTarget " ] ) ;
2017-02-02 22:03:55 +01:00
} else if ( options . exists ( " totalFee " ) ) {
totalFee = options [ " totalFee " ] . get_int64 ( ) ;
2017-03-27 13:55:28 +02:00
if ( totalFee < = 0 ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , strprintf ( " Invalid totalFee %s (must be greater than 0) " , FormatMoney(totalFee))) ;
}
2017-02-02 22:03:55 +01:00
}
if ( options . exists ( " replaceable " ) ) {
2018-04-07 18:12:46 +02:00
coin_control . m_signal_bip125_rbf = options [ " replaceable " ] . get_bool ( ) ;
2017-02-02 22:03:55 +01:00
}
2017-06-14 21:15:40 +02:00
if ( options . exists ( " estimate_mode " ) ) {
2017-06-28 23:23:46 +02:00
if ( ! FeeModeFromString ( options [ " estimate_mode " ] . get_str ( ) , coin_control . m_fee_mode ) ) {
2017-06-14 21:15:40 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid estimate_mode parameter " ) ;
}
}
2017-02-02 22:03:55 +01:00
}
2017-09-12 19:05:28 +02:00
// Make sure the results are valid at least up to the most recent block
// the user could have gotten from another RPC command prior to now
pwallet - > BlockUntilSyncedToCurrentChain ( ) ;
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2016-10-25 09:45:57 +02:00
EnsureWalletIsUnlocked ( pwallet ) ;
2016-12-09 19:45:27 +01:00
2017-06-15 16:34:17 +02:00
std : : vector < std : : string > errors ;
CAmount old_fee ;
CAmount new_fee ;
CMutableTransaction mtx ;
feebumper : : Result res = feebumper : : CreateTransaction ( pwallet , hash , coin_control , totalFee , errors , old_fee , new_fee , mtx ) ;
if ( res ! = feebumper : : Result : : OK ) {
2017-03-03 16:15:47 +01:00
switch ( res ) {
2017-09-20 22:19:30 +02:00
case feebumper : : Result : : INVALID_ADDRESS_OR_KEY :
2017-06-15 16:34:17 +02:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , errors [ 0 ] ) ;
2017-03-03 16:15:47 +01:00
break ;
2017-09-20 22:19:30 +02:00
case feebumper : : Result : : INVALID_REQUEST :
2017-06-15 16:34:17 +02:00
throw JSONRPCError ( RPC_INVALID_REQUEST , errors [ 0 ] ) ;
2017-03-03 16:15:47 +01:00
break ;
2017-09-20 22:19:30 +02:00
case feebumper : : Result : : INVALID_PARAMETER :
2017-06-15 16:34:17 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , errors [ 0 ] ) ;
2017-03-03 16:15:47 +01:00
break ;
2017-09-20 22:19:30 +02:00
case feebumper : : Result : : WALLET_ERROR :
2017-06-15 16:34:17 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , errors [ 0 ] ) ;
2017-03-03 16:15:47 +01:00
break ;
default :
2017-06-15 16:34:17 +02:00
throw JSONRPCError ( RPC_MISC_ERROR , errors [ 0 ] ) ;
2017-03-03 16:15:47 +01:00
break ;
2016-12-09 19:45:27 +01:00
}
}
2017-03-03 16:15:47 +01:00
// sign bumped transaction
2017-06-15 16:34:17 +02:00
if ( ! feebumper : : SignTransaction ( pwallet , mtx ) ) {
2017-03-03 16:15:47 +01:00
throw JSONRPCError ( RPC_WALLET_ERROR , " Can't sign transaction. " ) ;
2016-12-09 19:45:27 +01:00
}
2017-03-03 16:15:47 +01:00
// commit the bumped transaction
2017-06-15 16:34:17 +02:00
uint256 txid ;
if ( feebumper : : CommitTransaction ( pwallet , hash , std : : move ( mtx ) , errors , txid ) ! = feebumper : : Result : : OK ) {
throw JSONRPCError ( RPC_WALLET_ERROR , errors [ 0 ] ) ;
2017-01-23 19:15:24 +01:00
}
2016-12-09 19:45:27 +01:00
UniValue result ( UniValue : : VOBJ ) ;
2017-09-22 20:04:07 +02:00
result . pushKV ( " txid " , txid . GetHex ( ) ) ;
result . pushKV ( " origfee " , ValueFromAmount ( old_fee ) ) ;
result . pushKV ( " fee " , ValueFromAmount ( new_fee ) ) ;
2017-06-15 16:34:17 +02:00
UniValue result_errors ( UniValue : : VARR ) ;
for ( const std : : string & error : errors ) {
result_errors . push_back ( error ) ;
}
2017-09-22 20:04:07 +02:00
result . pushKV ( " errors " , result_errors ) ;
2016-12-09 19:45:27 +01:00
return result ;
}
2017-06-27 16:50:44 +02:00
UniValue generate ( const JSONRPCRequest & request )
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2017-06-27 16:50:44 +02:00
2017-06-28 08:32:21 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
2017-06-27 16:50:44 +02:00
return NullUniValue ;
2017-06-28 08:32:21 +02:00
}
2017-06-27 16:50:44 +02:00
2017-06-28 08:32:21 +02:00
if ( request . fHelp | | request . params . size ( ) < 1 | | request . params . size ( ) > 2 ) {
2017-06-27 16:50:44 +02:00
throw std : : runtime_error (
" generate nblocks ( maxtries ) \n "
" \n Mine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet. \n "
" \n Arguments: \n "
" 1. nblocks (numeric, required) How many blocks are generated immediately. \n "
" 2. maxtries (numeric, optional) How many iterations to try (default = 1000000). \n "
" \n Result: \n "
" [ blockhashes ] (array) hashes of blocks generated \n "
" \n Examples: \n "
" \n Generate 11 blocks \n "
+ HelpExampleCli ( " generate " , " 11 " )
) ;
2017-06-28 08:32:21 +02:00
}
2017-06-27 16:50:44 +02:00
2018-10-11 16:14:27 +02:00
if ( ! IsDeprecatedRPCEnabled ( " generate " ) ) {
throw JSONRPCError ( RPC_METHOD_DEPRECATED , " The wallet generate rpc method is deprecated and will be fully removed in v0.19. "
" To use generate in v0.18, restart bitcoind with -deprecatedrpc=generate. \n "
" Clients should transition to using the node rpc method generatetoaddress \n " ) ;
}
2017-06-28 08:32:21 +02:00
int num_generate = request . params [ 0 ] . get_int ( ) ;
uint64_t max_tries = 1000000 ;
2017-08-15 01:38:18 +02:00
if ( ! request . params [ 1 ] . isNull ( ) ) {
2017-06-28 08:32:21 +02:00
max_tries = request . params [ 1 ] . get_int ( ) ;
2017-06-27 16:50:44 +02:00
}
2017-06-28 08:32:21 +02:00
std : : shared_ptr < CReserveScript > coinbase_script ;
pwallet - > GetScriptForMining ( coinbase_script ) ;
2017-06-27 16:50:44 +02:00
// If the keypool is exhausted, no script is returned at all. Catch this.
2017-06-28 08:32:21 +02:00
if ( ! coinbase_script ) {
2017-06-27 16:50:44 +02:00
throw JSONRPCError ( RPC_WALLET_KEYPOOL_RAN_OUT , " Error: Keypool ran out, please call keypoolrefill first " ) ;
2017-06-28 08:32:21 +02:00
}
2017-06-27 16:50:44 +02:00
//throw an error if no script was provided
2017-06-28 08:32:21 +02:00
if ( coinbase_script - > reserveScript . empty ( ) ) {
throw JSONRPCError ( RPC_INTERNAL_ERROR , " No coinbase script available " ) ;
}
2017-06-27 16:50:44 +02:00
2017-06-28 08:32:21 +02:00
return generateBlocks ( coinbase_script , num_generate , max_tries , true ) ;
2017-06-27 16:50:44 +02:00
}
2015-11-19 16:05:37 +01:00
UniValue rescanblockchain ( const JSONRPCRequest & request )
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2015-11-19 16:05:37 +01:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
return NullUniValue ;
}
if ( request . fHelp | | request . params . size ( ) > 2 ) {
throw std : : runtime_error (
" rescanblockchain ( \" start_height \" ) ( \" stop_height \" ) \n "
" \n Rescan the local blockchain for wallet related transactions. \n "
" \n Arguments: \n "
" 1. \" start_height \" (numeric, optional) block height where the rescan should start \n "
" 2. \" stop_height \" (numeric, optional) the last block height that should be scanned \n "
" \n Result: \n "
" { \n "
" \" start_height \" (numeric) The block height where the rescan has started. If omitted, rescan started from the genesis block. \n "
" \" stop_height \" (numeric) The height of the last rescanned block. If omitted, rescan stopped at the chain tip. \n "
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " rescanblockchain " , " 100000 120000 " )
2017-10-14 01:34:04 +02:00
+ HelpExampleRpc ( " rescanblockchain " , " 100000, 120000 " )
2015-11-19 16:05:37 +01:00
) ;
}
2017-12-08 22:07:37 +01:00
WalletRescanReserver reserver ( pwallet ) ;
if ( ! reserver . reserve ( ) ) {
2017-09-08 01:29:59 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , " Wallet is currently rescanning. Abort existing rescan or wait. " ) ;
}
2015-11-19 16:05:37 +01:00
2017-09-08 01:29:59 +02:00
CBlockIndex * pindexStart = nullptr ;
2015-11-19 16:05:37 +01:00
CBlockIndex * pindexStop = nullptr ;
2017-09-08 01:29:59 +02:00
CBlockIndex * pChainTip = nullptr ;
{
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
2017-09-08 01:29:59 +02:00
pindexStart = chainActive . Genesis ( ) ;
pChainTip = chainActive . Tip ( ) ;
2015-11-19 16:05:37 +01:00
2017-09-08 01:29:59 +02:00
if ( ! request . params [ 0 ] . isNull ( ) ) {
pindexStart = chainActive [ request . params [ 0 ] . get_int ( ) ] ;
if ( ! pindexStart ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid start_height " ) ;
}
2015-11-19 16:05:37 +01:00
}
2017-09-08 01:29:59 +02:00
if ( ! request . params [ 1 ] . isNull ( ) ) {
pindexStop = chainActive [ request . params [ 1 ] . get_int ( ) ] ;
if ( ! pindexStop ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid stop_height " ) ;
}
else if ( pindexStop - > nHeight < pindexStart - > nHeight ) {
2018-03-18 15:26:45 +01:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " stop_height must be greater than start_height " ) ;
2017-09-08 01:29:59 +02:00
}
2015-11-19 16:05:37 +01:00
}
}
// We can't rescan beyond non-pruned blocks, stop and throw an error
if ( fPruneMode ) {
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
2017-09-08 01:29:59 +02:00
CBlockIndex * block = pindexStop ? pindexStop : pChainTip ;
2015-11-19 16:05:37 +01:00
while ( block & & block - > nHeight > = pindexStart - > nHeight ) {
if ( ! ( block - > nStatus & BLOCK_HAVE_DATA ) ) {
throw JSONRPCError ( RPC_MISC_ERROR , " Can't rescan beyond pruned data. Use RPC call getblockchaininfo to determine your pruned height. " ) ;
}
block = block - > pprev ;
}
}
2017-12-13 00:13:58 +01:00
CBlockIndex * stopBlock = pwallet - > ScanForWalletTransactions ( pindexStart , pindexStop , reserver , true ) ;
2015-11-19 16:05:37 +01:00
if ( ! stopBlock ) {
if ( pwallet - > IsAbortingRescan ( ) ) {
throw JSONRPCError ( RPC_MISC_ERROR , " Rescan aborted. " ) ;
}
// if we got a nullptr returned, ScanForWalletTransactions did rescan up to the requested stopindex
2017-09-08 01:29:59 +02:00
stopBlock = pindexStop ? pindexStop : pChainTip ;
2015-11-19 16:05:37 +01:00
}
else {
throw JSONRPCError ( RPC_MISC_ERROR , " Rescan failed. Potentially corrupted data files. " ) ;
}
UniValue response ( UniValue : : VOBJ ) ;
response . pushKV ( " start_height " , pindexStart - > nHeight ) ;
response . pushKV ( " stop_height " , stopBlock - > nHeight ) ;
return response ;
}
2017-06-13 03:53:46 +02:00
class DescribeWalletAddressVisitor : public boost : : static_visitor < UniValue >
{
public :
CWallet * const pwallet ;
void ProcessSubScript ( const CScript & subscript , UniValue & obj , bool include_addresses = false ) const
{
// Always present: script type and redeemscript
std : : vector < std : : vector < unsigned char > > solutions_data ;
2018-06-11 07:19:07 +02:00
txnouttype which_type = Solver ( subscript , solutions_data ) ;
2017-06-13 03:53:46 +02:00
obj . pushKV ( " script " , GetTxnOutputType ( which_type ) ) ;
obj . pushKV ( " hex " , HexStr ( subscript . begin ( ) , subscript . end ( ) ) ) ;
CTxDestination embedded ;
UniValue a ( UniValue : : VARR ) ;
if ( ExtractDestination ( subscript , embedded ) ) {
// Only when the script corresponds to an address.
UniValue subobj ( UniValue : : VOBJ ) ;
UniValue detail = DescribeAddress ( embedded ) ;
subobj . pushKVs ( detail ) ;
UniValue wallet_detail = boost : : apply_visitor ( * this , embedded ) ;
subobj . pushKVs ( wallet_detail ) ;
subobj . pushKV ( " address " , EncodeDestination ( embedded ) ) ;
subobj . pushKV ( " scriptPubKey " , HexStr ( subscript . begin ( ) , subscript . end ( ) ) ) ;
// Always report the pubkey at the top level, so that `getnewaddress()['pubkey']` always works.
if ( subobj . exists ( " pubkey " ) ) obj . pushKV ( " pubkey " , subobj [ " pubkey " ] ) ;
obj . pushKV ( " embedded " , std : : move ( subobj ) ) ;
if ( include_addresses ) a . push_back ( EncodeDestination ( embedded ) ) ;
} else if ( which_type = = TX_MULTISIG ) {
// Also report some information on multisig scripts (which do not have a corresponding address).
// TODO: abstract out the common functionality between this logic and ExtractDestinations.
obj . pushKV ( " sigsrequired " , solutions_data [ 0 ] [ 0 ] ) ;
UniValue pubkeys ( UniValue : : VARR ) ;
for ( size_t i = 1 ; i < solutions_data . size ( ) - 1 ; + + i ) {
CPubKey key ( solutions_data [ i ] . begin ( ) , solutions_data [ i ] . end ( ) ) ;
if ( include_addresses ) a . push_back ( EncodeDestination ( key . GetID ( ) ) ) ;
pubkeys . push_back ( HexStr ( key . begin ( ) , key . end ( ) ) ) ;
}
obj . pushKV ( " pubkeys " , std : : move ( pubkeys ) ) ;
}
// The "addresses" field is confusing because it refers to public keys using their P2PKH address.
// For that reason, only add the 'addresses' field when needed for backward compatibility. New applications
// can use the 'embedded'->'address' field for P2SH or P2WSH wrapped addresses, and 'pubkeys' for
// inspecting multisig participants.
if ( include_addresses ) obj . pushKV ( " addresses " , std : : move ( a ) ) ;
}
explicit DescribeWalletAddressVisitor ( CWallet * _pwallet ) : pwallet ( _pwallet ) { }
UniValue operator ( ) ( const CNoDestination & dest ) const { return UniValue ( UniValue : : VOBJ ) ; }
UniValue operator ( ) ( const CKeyID & keyID ) const
{
UniValue obj ( UniValue : : VOBJ ) ;
CPubKey vchPubKey ;
if ( pwallet & & pwallet - > GetPubKey ( keyID , vchPubKey ) ) {
obj . pushKV ( " pubkey " , HexStr ( vchPubKey ) ) ;
obj . pushKV ( " iscompressed " , vchPubKey . IsCompressed ( ) ) ;
}
return obj ;
}
UniValue operator ( ) ( const CScriptID & scriptID ) const
{
UniValue obj ( UniValue : : VOBJ ) ;
CScript subscript ;
if ( pwallet & & pwallet - > GetCScript ( scriptID , subscript ) ) {
ProcessSubScript ( subscript , obj , IsDeprecatedRPCEnabled ( " validateaddress " ) ) ;
}
return obj ;
}
UniValue operator ( ) ( const WitnessV0KeyHash & id ) const
{
UniValue obj ( UniValue : : VOBJ ) ;
CPubKey pubkey ;
if ( pwallet & & pwallet - > GetPubKey ( CKeyID ( id ) , pubkey ) ) {
obj . pushKV ( " pubkey " , HexStr ( pubkey ) ) ;
}
return obj ;
}
UniValue operator ( ) ( const WitnessV0ScriptHash & id ) const
{
UniValue obj ( UniValue : : VOBJ ) ;
CScript subscript ;
CRIPEMD160 hasher ;
uint160 hash ;
hasher . Write ( id . begin ( ) , 32 ) . Finalize ( hash . begin ( ) ) ;
if ( pwallet & & pwallet - > GetCScript ( CScriptID ( hash ) , subscript ) ) {
ProcessSubScript ( subscript , obj ) ;
}
return obj ;
}
UniValue operator ( ) ( const WitnessUnknown & id ) const { return UniValue ( UniValue : : VOBJ ) ; }
} ;
2018-05-02 17:14:48 +02:00
static UniValue DescribeWalletAddress ( CWallet * pwallet , const CTxDestination & dest )
2017-06-13 03:53:46 +02:00
{
UniValue ret ( UniValue : : VOBJ ) ;
UniValue detail = DescribeAddress ( dest ) ;
ret . pushKVs ( detail ) ;
ret . pushKVs ( boost : : apply_visitor ( DescribeWalletAddressVisitor ( pwallet ) , dest ) ) ;
return ret ;
}
2016-03-21 16:55:02 +01:00
/** Convert CAddressBookData to JSON record. */
static UniValue AddressBookDataToJSON ( const CAddressBookData & data , const bool verbose )
{
UniValue ret ( UniValue : : VOBJ ) ;
if ( verbose ) {
ret . pushKV ( " name " , data . name ) ;
}
ret . pushKV ( " purpose " , data . purpose ) ;
return ret ;
}
2017-06-13 03:53:46 +02:00
UniValue getaddressinfo ( const JSONRPCRequest & request )
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2017-06-13 03:53:46 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
return NullUniValue ;
}
if ( request . fHelp | | request . params . size ( ) ! = 1 ) {
throw std : : runtime_error (
" getaddressinfo \" address \" \n "
" \n Return information about the given bitcoin address. Some information requires the address \n "
" to be in the wallet. \n "
" \n Arguments: \n "
" 1. \" address \" (string, required) The bitcoin address to get the information of. \n "
" \n Result: \n "
" { \n "
" \" address \" : \" address \" , (string) The bitcoin address validated \n "
2018-10-02 21:49:18 +02:00
" \" scriptPubKey \" : \" hex \" , (string) The hex-encoded scriptPubKey generated by the address \n "
2017-06-13 03:53:46 +02:00
" \" ismine \" : true|false, (boolean) If the address is yours or not \n "
2018-10-17 03:51:00 +02:00
" \" solvable \" : true|false, (boolean) If the address is solvable by the wallet \n "
2017-06-13 03:53:46 +02:00
" \" iswatchonly \" : true|false, (boolean) If the address is watchonly \n "
" \" isscript \" : true|false, (boolean) If the key is a script \n "
2018-10-05 23:24:06 +02:00
" \" ischange \" : true|false, (boolean) If the address was used for change output \n "
2017-06-13 03:53:46 +02:00
" \" 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 "
" \" script \" : \" type \" (string, optional) The output script type. Only if \" isscript \" is true and the redeemscript is known. Possible types: nonstandard, pubkey, pubkeyhash, scripthash, multisig, nulldata, witness_v0_keyhash, witness_v0_scripthash, witness_unknown \n "
" \" hex \" : \" hex \" , (string, optional) The redeemscript for the p2sh address \n "
" \" pubkeys \" (string, optional) Array of pubkeys associated with the known redeemscript (only if \" script \" is \" multisig \" ) \n "
" [ \n "
" \" pubkey \" \n "
" ,... \n "
" ] \n "
" \" sigsrequired \" : xxxxx (numeric, optional) Number of signatures required to spend multisig output (only if \" script \" is \" multisig \" ) \n "
" \" pubkey \" : \" publickeyhex \" , (string, optional) The hex value of the raw public key, for single-key addresses (possibly embedded in P2SH or P2WSH) \n "
2018-07-27 23:05:24 +02:00
" \" embedded \" : {...}, (object, optional) Information about the address embedded in P2SH or P2WSH, if relevant and known. It includes all getaddressinfo output fields for the embedded address, excluding metadata ( \" timestamp \" , \" hdkeypath \" , \" hdseedid \" ) and relation to the wallet ( \" ismine \" , \" iswatchonly \" ). \n "
2017-06-13 03:53:46 +02:00
" \" iscompressed \" : true|false, (boolean) If the address is compressed \n "
2018-07-27 23:05:24 +02:00
" \" label \" : \" label \" (string) The label associated with the address, \" \" is the default label \n "
2017-06-13 03:53:46 +02:00
" \" timestamp \" : timestamp, (number, optional) The creation time of the key if available in seconds since epoch (Jan 1 1970 GMT) \n "
" \" hdkeypath \" : \" keypath \" (string, optional) The HD keypath if the key is HD and available \n "
2018-04-04 18:47:55 +02:00
" \" hdseedid \" : \" <hash160> \" (string, optional) The Hash160 of the HD seed \n "
2018-04-17 23:42:31 +02:00
" \" hdmasterkeyid \" : \" <hash160> \" (string, optional) alias for hdseedid maintained for backwards compatibility. Will be removed in V0.18. \n "
2016-03-21 16:55:02 +01:00
" \" labels \" (object) Array of labels associated with the address. \n "
" [ \n "
" { (json object of label data) \n "
" \" name \" : \" labelname \" (string) The label \n "
" \" purpose \" : \" string \" (string) Purpose of address ( \" send \" for sending address, \" receive \" for receiving address) \n "
" },... \n "
" ] \n "
2017-06-13 03:53:46 +02:00
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " getaddressinfo " , " \" 1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc \" " )
+ HelpExampleRpc ( " getaddressinfo " , " \" 1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc \" " )
) ;
}
LOCK ( pwallet - > cs_wallet ) ;
UniValue ret ( UniValue : : VOBJ ) ;
CTxDestination dest = DecodeDestination ( request . params [ 0 ] . get_str ( ) ) ;
// Make sure the destination is valid
if ( ! IsValidDestination ( dest ) ) {
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid address " ) ;
}
std : : string currentAddress = EncodeDestination ( dest ) ;
ret . pushKV ( " address " , currentAddress ) ;
CScript scriptPubKey = GetScriptForDestination ( dest ) ;
ret . pushKV ( " scriptPubKey " , HexStr ( scriptPubKey . begin ( ) , scriptPubKey . end ( ) ) ) ;
isminetype mine = IsMine ( * pwallet , dest ) ;
ret . pushKV ( " ismine " , bool ( mine & ISMINE_SPENDABLE ) ) ;
ret . pushKV ( " iswatchonly " , bool ( mine & ISMINE_WATCH_ONLY ) ) ;
2018-10-17 03:51:00 +02:00
ret . pushKV ( " solvable " , IsSolvable ( * pwallet , scriptPubKey ) ) ;
2017-06-13 03:53:46 +02:00
UniValue detail = DescribeWalletAddress ( pwallet , dest ) ;
ret . pushKVs ( detail ) ;
if ( pwallet - > mapAddressBook . count ( dest ) ) {
2018-04-16 20:01:52 +02:00
ret . pushKV ( " label " , pwallet - > mapAddressBook [ dest ] . name ) ;
2017-06-13 03:53:46 +02:00
}
2018-10-05 23:24:06 +02:00
ret . pushKV ( " ischange " , pwallet - > IsChange ( scriptPubKey ) ) ;
2017-06-13 03:53:46 +02:00
const CKeyMetadata * meta = nullptr ;
CKeyID key_id = GetKeyForDestination ( * pwallet , dest ) ;
if ( ! key_id . IsNull ( ) ) {
auto it = pwallet - > mapKeyMetadata . find ( key_id ) ;
if ( it ! = pwallet - > mapKeyMetadata . end ( ) ) {
meta = & it - > second ;
}
}
if ( ! meta ) {
auto it = pwallet - > m_script_metadata . find ( CScriptID ( scriptPubKey ) ) ;
if ( it ! = pwallet - > m_script_metadata . end ( ) ) {
meta = & it - > second ;
}
}
if ( meta ) {
ret . pushKV ( " timestamp " , meta - > nCreateTime ) ;
if ( ! meta - > hdKeypath . empty ( ) ) {
ret . pushKV ( " hdkeypath " , meta - > hdKeypath ) ;
2018-04-04 17:43:45 +02:00
ret . pushKV ( " hdseedid " , meta - > hd_seed_id . GetHex ( ) ) ;
2018-04-17 23:42:31 +02:00
ret . pushKV ( " hdmasterkeyid " , meta - > hd_seed_id . GetHex ( ) ) ;
2017-06-13 03:53:46 +02:00
}
}
2016-03-21 16:55:02 +01:00
// Currently only one label can be associated with an address, return an array
// so the API remains stable if we allow multiple labels to be associated with
// an address.
UniValue labels ( UniValue : : VARR ) ;
std : : map < CTxDestination , CAddressBookData > : : iterator mi = pwallet - > mapAddressBook . find ( dest ) ;
if ( mi ! = pwallet - > mapAddressBook . end ( ) ) {
labels . push_back ( AddressBookDataToJSON ( mi - > second , true ) ) ;
}
ret . pushKV ( " labels " , std : : move ( labels ) ) ;
return ret ;
}
2018-05-02 17:14:48 +02:00
static UniValue getaddressesbylabel ( const JSONRPCRequest & request )
2016-03-21 16:55:02 +01:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-03-21 16:55:02 +01:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
return NullUniValue ;
}
if ( request . fHelp | | request . params . size ( ) ! = 1 )
throw std : : runtime_error (
" getaddressesbylabel \" label \" \n "
" \n Returns the list of addresses assigned the specified label. \n "
" \n Arguments: \n "
" 1. \" label \" (string, required) The label. \n "
" \n Result: \n "
" { (json object with addresses as keys) \n "
" \" address \" : { (json object with information about address) \n "
" \" purpose \" : \" string \" (string) Purpose of address ( \" send \" for sending address, \" receive \" for receiving address) \n "
" },... \n "
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " getaddressesbylabel " , " \" tabby \" " )
+ HelpExampleRpc ( " getaddressesbylabel " , " \" tabby \" " )
) ;
LOCK ( pwallet - > cs_wallet ) ;
std : : string label = LabelFromValue ( request . params [ 0 ] ) ;
// Find all addresses that have the given label
UniValue ret ( UniValue : : VOBJ ) ;
2018-05-16 00:41:53 +02:00
for ( const std : : pair < const CTxDestination , CAddressBookData > & item : pwallet - > mapAddressBook ) {
2016-03-21 16:55:02 +01:00
if ( item . second . name = = label ) {
ret . pushKV ( EncodeDestination ( item . first ) , AddressBookDataToJSON ( item . second , false ) ) ;
}
}
if ( ret . empty ( ) ) {
throw JSONRPCError ( RPC_WALLET_INVALID_LABEL_NAME , std : : string ( " No addresses with label " + label ) ) ;
}
return ret ;
}
2018-05-02 17:14:48 +02:00
static UniValue listlabels ( const JSONRPCRequest & request )
2016-03-21 16:55:02 +01:00
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2016-03-21 16:55:02 +01:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
return NullUniValue ;
}
if ( request . fHelp | | request . params . size ( ) > 1 )
throw std : : runtime_error (
" listlabels ( \" purpose \" ) \n "
" \n Returns the list of all labels, or labels that are assigned to addresses with a specific purpose. \n "
" \n Arguments: \n "
" 1. \" purpose \" (string, optional) Address purpose to list labels for ('send','receive'). An empty string is the same as not providing this argument. \n "
" \n Result: \n "
" [ (json array of string) \n "
" \" label \" , (string) Label name \n "
" ... \n "
" ] \n "
" \n Examples: \n "
" \n List all labels \n "
+ HelpExampleCli ( " listlabels " , " " ) +
" \n List labels that have receiving addresses \n "
+ HelpExampleCli ( " listlabels " , " receive " ) +
" \n List labels that have sending addresses \n "
+ HelpExampleCli ( " listlabels " , " send " ) +
2018-10-02 21:49:18 +02:00
" \n As a JSON-RPC call \n "
2016-03-21 16:55:02 +01:00
+ HelpExampleRpc ( " listlabels " , " receive " )
) ;
LOCK ( pwallet - > cs_wallet ) ;
std : : string purpose ;
if ( ! request . params [ 0 ] . isNull ( ) ) {
purpose = request . params [ 0 ] . get_str ( ) ;
}
// Add to a set to sort by label name, then insert into Univalue array
std : : set < std : : string > label_set ;
2018-05-16 00:41:53 +02:00
for ( const std : : pair < const CTxDestination , CAddressBookData > & entry : pwallet - > mapAddressBook ) {
2016-03-21 16:55:02 +01:00
if ( purpose . empty ( ) | | entry . second . purpose = = purpose ) {
label_set . insert ( entry . second . name ) ;
}
}
UniValue ret ( UniValue : : VARR ) ;
for ( const std : : string & name : label_set ) {
ret . push_back ( name ) ;
}
2017-06-13 03:53:46 +02:00
return ret ;
}
2017-09-12 23:01:12 +02:00
UniValue sethdseed ( const JSONRPCRequest & request )
{
2018-05-22 17:18:07 +02:00
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
2017-09-12 23:01:12 +02:00
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
return NullUniValue ;
}
if ( request . fHelp | | request . params . size ( ) > 2 ) {
throw std : : runtime_error (
" sethdseed ( \" newkeypool \" \" seed \" ) \n "
" \n Set or generate a new HD wallet seed. Non-HD wallets will not be upgraded to being a HD wallet. Wallets that are already \n "
" HD will have a new HD seed set so that new keys added to the keypool will be derived from this new seed. \n "
" \n Note that you will need to MAKE A NEW BACKUP of your wallet after setting the HD wallet seed. \n "
+ HelpRequiringPassphrase ( pwallet ) +
" \n Arguments: \n "
" 1. \" newkeypool \" (boolean, optional, default=true) Whether to flush old unused addresses, including change addresses, from the keypool and regenerate it. \n "
" If true, the next address from getnewaddress and change address from getrawchangeaddress will be from this new seed. \n "
" If false, addresses (including change addresses if the wallet already had HD Chain Split enabled) from the existing \n "
" keypool will be used until it has been depleted. \n "
" 2. \" seed \" (string, optional) The WIF private key to use as the new HD seed; if not provided a random seed will be used. \n "
2018-04-04 17:43:45 +02:00
" The seed value can be retrieved using the dumpwallet command. It is the private key marked hdseed=1 \n "
2017-09-12 23:01:12 +02:00
" \n Examples: \n "
+ HelpExampleCli ( " sethdseed " , " " )
+ HelpExampleCli ( " sethdseed " , " false " )
+ HelpExampleCli ( " sethdseed " , " true \" wifkey \" " )
+ HelpExampleRpc ( " sethdseed " , " true, \" wifkey \" " )
) ;
}
if ( IsInitialBlockDownload ( ) ) {
throw JSONRPCError ( RPC_CLIENT_IN_INITIAL_DOWNLOAD , " Cannot set a new HD seed while still in Initial Block Download " ) ;
}
2017-07-26 16:23:01 +02:00
auto locked_chain = pwallet - > chain ( ) . lock ( ) ;
LOCK ( pwallet - > cs_wallet ) ;
2017-09-12 23:01:12 +02:00
// Do not do anything to non-HD wallets
if ( ! pwallet - > IsHDEnabled ( ) ) {
throw JSONRPCError ( RPC_WALLET_ERROR , " Cannot set a HD seed on a non-HD wallet. Start with -upgradewallet in order to upgrade a non-HD wallet to HD " ) ;
}
EnsureWalletIsUnlocked ( pwallet ) ;
bool flush_key_pool = true ;
if ( ! request . params [ 0 ] . isNull ( ) ) {
flush_key_pool = request . params [ 0 ] . get_bool ( ) ;
}
CPubKey master_pub_key ;
if ( request . params [ 1 ] . isNull ( ) ) {
2018-04-04 17:43:45 +02:00
master_pub_key = pwallet - > GenerateNewSeed ( ) ;
2017-09-12 23:01:12 +02:00
} else {
CKey key = DecodeSecret ( request . params [ 1 ] . get_str ( ) ) ;
if ( ! key . IsValid ( ) ) {
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid private key " ) ;
}
if ( HaveKey ( * pwallet , key ) ) {
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Already have this key (either as an HD seed or as a loose private key) " ) ;
}
2018-04-04 17:43:45 +02:00
master_pub_key = pwallet - > DeriveNewSeed ( key ) ;
2017-09-12 23:01:12 +02:00
}
2018-04-04 17:43:45 +02:00
pwallet - > SetHDSeed ( master_pub_key ) ;
2017-09-12 23:01:12 +02:00
if ( flush_key_pool ) pwallet - > NewKeyPool ( ) ;
return NullUniValue ;
}
2018-07-20 08:15:53 +02:00
void AddKeypathToMap ( const CWallet * pwallet , const CKeyID & keyID , std : : map < CPubKey , KeyOriginInfo > & hd_keypaths )
2018-06-29 04:05:05 +02:00
{
CPubKey vchPubKey ;
if ( ! pwallet - > GetPubKey ( keyID , vchPubKey ) ) {
return ;
}
2018-07-20 08:15:53 +02:00
KeyOriginInfo info ;
2018-07-20 02:56:52 +02:00
if ( ! pwallet - > GetKeyOrigin ( keyID , info ) ) {
throw JSONRPCError ( RPC_INTERNAL_ERROR , " Internal keypath is broken " ) ;
2018-06-29 04:05:05 +02:00
}
2018-07-20 08:15:53 +02:00
hd_keypaths . emplace ( vchPubKey , std : : move ( info ) ) ;
2018-06-29 04:05:05 +02:00
}
2018-10-27 00:24:55 +02:00
bool FillPSBT ( const CWallet * pwallet , PartiallySignedTransaction & psbtx , int sighash_type , bool sign , bool bip32derivs )
2018-06-29 04:05:05 +02:00
{
LOCK ( pwallet - > cs_wallet ) ;
// Get all of the previous transactions
bool complete = true ;
2018-10-27 00:24:55 +02:00
for ( unsigned int i = 0 ; i < psbtx . tx - > vin . size ( ) ; + + i ) {
const CTxIn & txin = psbtx . tx - > vin [ i ] ;
2018-06-29 04:05:05 +02:00
PSBTInput & input = psbtx . inputs . at ( i ) ;
Refactor PSBTInput signing to enforce invariant
Refactor the process of PSBTInput signing to enforce the invariant that
a PSBTInput always has _either_ a witness_utxo or a non_witness_utxo,
never both.
This simplifies the logic of SignPSBTInput slightly, since it no longer
has to deal with the "both" case. When calling it, we now give it, in
order of preference: (1) whichever of the utxo fields was already
present in the PSBT we received, or (2) if neither, the
non_witness_utxo field, which is just a copy of the input transaction,
which we get from the wallet.
SignPSBTInput no longer has to remove one of the two fields; instead, it
will check if we have a witness signature, and if so, it will replace
the non_witness_utxo with the witness_utxo (which is smaller, as it is
just a copy of the output being spent.)
Add PSBTInput::IsSane checks in two more places, which checks for
both utxo fields being present; we will now give an RPC error early on
if we are supplied such a malformed PSBT to fill in.
Also add a check to FillPSBT, to avoid touching any input that is
already signed. (This is now redundant, since we should no longer
potentially harm an already-signed input, but it's harmless.)
fixes #14473
2018-10-27 00:31:41 +02:00
if ( PSBTInputSigned ( input ) ) {
continue ;
}
// Verify input looks sane. This will check that we have at most one uxto, witness or non-witness.
if ( ! input . IsSane ( ) ) {
throw JSONRPCError ( RPC_DESERIALIZATION_ERROR , " PSBT input is not sane. " ) ;
}
// If we have no utxo, grab it from the wallet.
if ( ! input . non_witness_utxo & & input . witness_utxo . IsNull ( ) ) {
const uint256 & txhash = txin . prevout . hash ;
const auto it = pwallet - > mapWallet . find ( txhash ) ;
if ( it ! = pwallet - > mapWallet . end ( ) ) {
const CWalletTx & wtx = it - > second ;
// We only need the non_witness_utxo, which is a superset of the witness_utxo.
// The signing code will switch to the smaller witness_utxo if this is ok.
input . non_witness_utxo = wtx . tx ;
}
2018-06-29 04:05:05 +02:00
}
// Get the Sighash type
if ( sign & & input . sighash_type > 0 & & input . sighash_type ! = sighash_type ) {
throw JSONRPCError ( RPC_DESERIALIZATION_ERROR , " Specified Sighash and sighash in PSBT do not match. " ) ;
}
2018-10-27 00:30:50 +02:00
complete & = SignPSBTInput ( HidingSigningProvider ( pwallet , ! sign , ! bip32derivs ) , psbtx , i , sighash_type ) ;
2018-06-29 04:05:05 +02:00
}
// Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
2018-10-27 00:24:55 +02:00
for ( unsigned int i = 0 ; i < psbtx . tx - > vout . size ( ) ; + + i ) {
const CTxOut & out = psbtx . tx - > vout . at ( i ) ;
2018-06-29 04:05:05 +02:00
PSBTOutput & psbt_out = psbtx . outputs . at ( i ) ;
// Fill a SignatureData with output info
SignatureData sigdata ;
psbt_out . FillSignatureData ( sigdata ) ;
MutableTransactionSignatureCreator creator ( psbtx . tx . get_ptr ( ) , 0 , out . nValue , 1 ) ;
2018-07-20 03:47:24 +02:00
ProduceSignature ( HidingSigningProvider ( pwallet , true , ! bip32derivs ) , creator , out . scriptPubKey , sigdata ) ;
2018-06-29 04:05:05 +02:00
psbt_out . FromSignatureData ( sigdata ) ;
}
return complete ;
}
UniValue walletprocesspsbt ( const JSONRPCRequest & request )
{
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
return NullUniValue ;
}
if ( request . fHelp | | request . params . size ( ) < 1 | | request . params . size ( ) > 4 )
throw std : : runtime_error (
" walletprocesspsbt \" psbt \" ( sign \" sighashtype \" bip32derivs ) \n "
" \n Update a PSBT with input information from our wallet and then sign inputs \n "
" that we can sign for. \n "
+ HelpRequiringPassphrase ( pwallet ) + " \n "
" \n Arguments: \n "
" 1. \" psbt \" (string, required) The transaction base64 string \n "
" 2. sign (boolean, optional, default=true) Also sign the transaction when updating \n "
" 3. \" sighashtype \" (string, optional, default=ALL) The signature hash type to sign with if not specified by the PSBT. Must be one of \n "
" \" ALL \" \n "
" \" NONE \" \n "
" \" SINGLE \" \n "
" \" ALL|ANYONECANPAY \" \n "
" \" NONE|ANYONECANPAY \" \n "
" \" SINGLE|ANYONECANPAY \" \n "
2018-08-25 00:01:27 +02:00
" 4. bip32derivs (boolean, optional, default=false) If true, includes the BIP 32 derivation paths for public keys if we know them \n "
2018-06-29 04:05:05 +02:00
" \n Result: \n "
" { \n "
" \" psbt \" : \" value \" , (string) The base64-encoded partially signed transaction \n "
" \" complete \" : true|false, (boolean) If the transaction has a complete set of signatures \n "
" ] \n "
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " walletprocesspsbt " , " \" psbt \" " )
) ;
RPCTypeCheck ( request . params , { UniValue : : VSTR , UniValue : : VBOOL , UniValue : : VSTR } ) ;
// Unserialize the transaction
PartiallySignedTransaction psbtx ;
std : : string error ;
if ( ! DecodePSBT ( psbtx , request . params [ 0 ] . get_str ( ) , error ) ) {
throw JSONRPCError ( RPC_DESERIALIZATION_ERROR , strprintf ( " TX decode failed %s " , error ) ) ;
}
// Get the sighash type
int nHashType = ParseSighashString ( request . params [ 2 ] ) ;
// Fill transaction with our data and also sign
bool sign = request . params [ 1 ] . isNull ( ) ? true : request . params [ 1 ] . get_bool ( ) ;
bool bip32derivs = request . params [ 3 ] . isNull ( ) ? false : request . params [ 3 ] . get_bool ( ) ;
2018-10-27 00:24:55 +02:00
bool complete = FillPSBT ( pwallet , psbtx , nHashType , sign , bip32derivs ) ;
2018-06-29 04:05:05 +02:00
UniValue result ( UniValue : : VOBJ ) ;
CDataStream ssTx ( SER_NETWORK , PROTOCOL_VERSION ) ;
ssTx < < psbtx ;
2018-10-27 00:18:52 +02:00
result . pushKV ( " psbt " , EncodeBase64 ( ssTx . str ( ) ) ) ;
2018-08-09 18:08:45 +02:00
result . pushKV ( " complete " , complete ) ;
2018-06-29 04:05:05 +02:00
return result ;
}
UniValue walletcreatefundedpsbt ( const JSONRPCRequest & request )
{
std : : shared_ptr < CWallet > const wallet = GetWalletForJSONRPCRequest ( request ) ;
CWallet * const pwallet = wallet . get ( ) ;
if ( ! EnsureWalletIsAvailable ( pwallet , request . fHelp ) ) {
return NullUniValue ;
}
2018-08-15 03:12:33 +02:00
if ( request . fHelp | | request . params . size ( ) < 2 | | request . params . size ( ) > 5 )
2018-06-29 04:05:05 +02:00
throw std : : runtime_error (
2018-10-23 21:22:28 +02:00
RPCHelpMan { " walletcreatefundedpsbt " ,
{
{ " inputs " , RPCArg : : Type : : ARR ,
{
{ " " , RPCArg : : Type : : OBJ ,
{
{ " txid " , RPCArg : : Type : : STR_HEX , false } ,
{ " vout " , RPCArg : : Type : : NUM , false } ,
{ " sequence " , RPCArg : : Type : : NUM , false } ,
} ,
false } ,
} ,
false } ,
{ " outputs " , RPCArg : : Type : : ARR ,
{
{ " " , RPCArg : : Type : : OBJ ,
{
{ " address " , RPCArg : : Type : : AMOUNT , true } ,
} ,
true } ,
{ " " , RPCArg : : Type : : OBJ ,
{
{ " data " , RPCArg : : Type : : STR_HEX , true } ,
} ,
true } ,
} ,
false } ,
{ " locktime " , RPCArg : : Type : : NUM , true } ,
{ " options " , RPCArg : : Type : : OBJ ,
{
{ " changeAddress " , RPCArg : : Type : : STR_HEX , true } ,
{ " changePosition " , RPCArg : : Type : : NUM , true } ,
{ " change_type " , RPCArg : : Type : : STR , true } ,
{ " includeWatching " , RPCArg : : Type : : BOOL , true } ,
{ " lockUnspents " , RPCArg : : Type : : BOOL , true } ,
{ " feeRate " , RPCArg : : Type : : NUM , true } ,
{ " subtractFeeFromOutputs " , RPCArg : : Type : : ARR ,
{
{ " int " , RPCArg : : Type : : NUM , true } ,
} ,
true } ,
{ " replaceable " , RPCArg : : Type : : BOOL , true } ,
{ " conf_target " , RPCArg : : Type : : NUM , true } ,
{ " estimate_mode " , RPCArg : : Type : : STR , true } ,
} ,
true } ,
{ " bip32derivs " , RPCArg : : Type : : BOOL , true } ,
} }
. ToString ( ) +
2018-06-29 04:05:05 +02:00
" \n Creates and funds a transaction in the Partially Signed Transaction format. Inputs will be added if supplied inputs are not enough \n "
" Implements the Creator and Updater roles. \n "
" \n Arguments: \n "
" 1. \" inputs \" (array, required) A json array of json objects \n "
" [ \n "
" { \n "
" \" txid \" : \" id \" , (string, required) The transaction id \n "
" \" vout \" :n, (numeric, required) The output number \n "
" \" sequence \" :n (numeric, optional) The sequence number \n "
" } \n "
" ,... \n "
" ] \n "
" 2. \" outputs \" (array, required) a json array with outputs (key-value pairs) \n "
" [ \n "
" { \n "
" \" address \" : x.xxx, (obj, optional) A key-value pair. The key (string) is the bitcoin address, the value (float or string) is the amount in " + CURRENCY_UNIT + " \n "
" }, \n "
" { \n "
2018-10-02 21:49:18 +02:00
" \" data \" : \" hex \" (obj, optional) A key-value pair. The key must be \" data \" , the value is hex-encoded data \n "
2018-06-29 04:05:05 +02:00
" } \n "
" ,... More key-value pairs of the above form. For compatibility reasons, a dictionary, which holds the key-value pairs directly, is also \n "
" accepted as second parameter. \n "
" ] \n "
" 3. locktime (numeric, optional, default=0) Raw locktime. Non-0 value also locktime-activates inputs \n "
" Allows this transaction to be replaced by a transaction with higher fees. If provided, it is an error if explicit sequence numbers are incompatible. \n "
2018-08-15 03:12:33 +02:00
" 4. options (object, optional) \n "
2018-06-29 04:05:05 +02:00
" { \n "
" \" changeAddress \" (string, optional, default pool address) The bitcoin address to receive the change \n "
" \" changePosition \" (numeric, optional, default random) The index of the change output \n "
" \" change_type \" (string, optional) The output type to use. Only valid if changeAddress is not specified. Options are \" legacy \" , \" p2sh-segwit \" , and \" bech32 \" . Default is set by -changetype. \n "
" \" includeWatching \" (boolean, optional, default false) Also select inputs which are watch only \n "
" \" lockUnspents \" (boolean, optional, default false) Lock selected unspent outputs \n "
" \" feeRate \" (numeric, optional, default not set: makes wallet determine the fee) Set a specific fee rate in " + CURRENCY_UNIT + " /kB \n "
" \" subtractFeeFromOutputs \" (array, optional) A json array of integers. \n "
" The fee will be equally deducted from the amount of each specified output. \n "
" The outputs are specified by their zero-based index, before any change output is added. \n "
" Those recipients will receive less bitcoins than you enter in their corresponding amount field. \n "
" If no outputs are specified here, the sender pays the fee. \n "
" [vout_index,...] \n "
" \" replaceable \" (boolean, optional) Marks this transaction as BIP125 replaceable. \n "
" Allows this transaction to be replaced by a transaction with higher fees \n "
" \" conf_target \" (numeric, optional) Confirmation target (in blocks) \n "
" \" estimate_mode \" (string, optional, default=UNSET) The fee estimate mode, must be one of: \n "
" \" UNSET \" \n "
" \" ECONOMICAL \" \n "
" \" CONSERVATIVE \" \n "
" } \n "
2018-08-25 00:01:27 +02:00
" 5. bip32derivs (boolean, optional, default=false) If true, includes the BIP 32 derivation paths for public keys if we know them \n "
2018-06-29 04:05:05 +02:00
" \n Result: \n "
" { \n "
" \" psbt \" : \" value \" , (string) The resulting raw transaction (base64-encoded string) \n "
" \" fee \" : n, (numeric) Fee in " + CURRENCY_UNIT + " the resulting transaction pays \n "
" \" changepos \" : n (numeric) The position of the added change output, or -1 \n "
" } \n "
" \n Examples: \n "
" \n Create a transaction with no inputs \n "
+ HelpExampleCli ( " walletcreatefundedpsbt " , " \" [{ \\ \" txid \\ \" : \\ \" myid \\ \" , \\ \" vout \\ \" :0}] \" \" [{ \\ \" data \\ \" : \\ \" 00010203 \\ \" }] \" " )
) ;
RPCTypeCheck ( request . params , {
UniValue : : VARR ,
UniValueType ( ) , // ARR or OBJ, checked later
UniValue : : VNUM ,
2018-08-20 18:57:06 +02:00
UniValue : : VOBJ ,
UniValue : : VBOOL
2018-06-29 04:05:05 +02:00
} , true
) ;
CAmount fee ;
int change_position ;
2018-08-15 03:12:33 +02:00
CMutableTransaction rawTx = ConstructTransaction ( request . params [ 0 ] , request . params [ 1 ] , request . params [ 2 ] , request . params [ 3 ] [ " replaceable " ] ) ;
FundTransaction ( pwallet , rawTx , fee , change_position , request . params [ 3 ] ) ;
2018-06-29 04:05:05 +02:00
// Make a blank psbt
2018-10-27 00:26:16 +02:00
PartiallySignedTransaction psbtx ( rawTx ) ;
2018-06-29 04:05:05 +02:00
// Fill transaction with out data but don't sign
2018-08-24 23:03:55 +02:00
bool bip32derivs = request . params [ 4 ] . isNull ( ) ? false : request . params [ 4 ] . get_bool ( ) ;
2018-10-27 00:24:55 +02:00
FillPSBT ( pwallet , psbtx , 1 , false , bip32derivs ) ;
2018-06-29 04:05:05 +02:00
// Serialize the PSBT
CDataStream ssTx ( SER_NETWORK , PROTOCOL_VERSION ) ;
ssTx < < psbtx ;
UniValue result ( UniValue : : VOBJ ) ;
2018-10-27 00:18:52 +02:00
result . pushKV ( " psbt " , EncodeBase64 ( ssTx . str ( ) ) ) ;
2018-06-29 04:05:05 +02:00
result . pushKV ( " fee " , ValueFromAmount ( fee ) ) ;
result . pushKV ( " changepos " , change_position ) ;
return result ;
}
2018-08-07 09:34:45 +02:00
UniValue abortrescan ( const JSONRPCRequest & request ) ; // in rpcdump.cpp
UniValue dumpprivkey ( const JSONRPCRequest & request ) ; // in rpcdump.cpp
UniValue importprivkey ( const JSONRPCRequest & request ) ;
UniValue importaddress ( const JSONRPCRequest & request ) ;
UniValue importpubkey ( const JSONRPCRequest & request ) ;
UniValue dumpwallet ( const JSONRPCRequest & request ) ;
UniValue importwallet ( const JSONRPCRequest & request ) ;
UniValue importprunedfunds ( const JSONRPCRequest & request ) ;
UniValue removeprunedfunds ( const JSONRPCRequest & request ) ;
UniValue importmulti ( const JSONRPCRequest & request ) ;
2016-01-07 08:33:49 +01:00
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-12 21:23:02 +02:00
{ // category name actor (function) argNames
// --------------------- ------------------------ ----------------------- ----------
2018-07-27 23:06:16 +02:00
{ " generating " , " generate " , & generate , { " nblocks " , " maxtries " } } ,
2017-06-12 21:23:02 +02:00
{ " hidden " , " resendwallettransactions " , & resendwallettransactions , { } } ,
2018-07-27 23:06:16 +02:00
{ " rawtransactions " , " fundrawtransaction " , & fundrawtransaction , { " hexstring " , " options " , " iswitness " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " abandontransaction " , & abandontransaction , { " txid " } } ,
{ " wallet " , " abortrescan " , & abortrescan , { } } ,
2018-07-27 23:05:24 +02:00
{ " wallet " , " addmultisigaddress " , & addmultisigaddress , { " nrequired " , " keys " , " label " , " address_type " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " backupwallet " , & backupwallet , { " destination " } } ,
{ " wallet " , " bumpfee " , & bumpfee , { " txid " , " options " } } ,
2018-06-13 20:35:41 +02:00
{ " wallet " , " createwallet " , & createwallet , { " wallet_name " , " disable_private_keys " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " dumpprivkey " , & dumpprivkey , { " address " } } ,
{ " wallet " , " dumpwallet " , & dumpwallet , { " filename " } } ,
{ " wallet " , " encryptwallet " , & encryptwallet , { " passphrase " } } ,
2018-07-27 23:06:16 +02:00
{ " wallet " , " getaddressesbylabel " , & getaddressesbylabel , { " label " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " getaddressinfo " , & getaddressinfo , { " address " } } ,
2018-07-27 23:05:24 +02:00
{ " wallet " , " getbalance " , & getbalance , { " dummy " , " minconf " , " include_watchonly " } } ,
{ " wallet " , " getnewaddress " , & getnewaddress , { " label " , " address_type " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " getrawchangeaddress " , & getrawchangeaddress , { " address_type " } } ,
{ " wallet " , " getreceivedbyaddress " , & getreceivedbyaddress , { " address " , " minconf " } } ,
2018-07-27 23:06:16 +02:00
{ " wallet " , " getreceivedbylabel " , & getreceivedbylabel , { " label " , " minconf " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " gettransaction " , & gettransaction , { " txid " , " include_watchonly " } } ,
{ " wallet " , " getunconfirmedbalance " , & getunconfirmedbalance , { } } ,
{ " wallet " , " getwalletinfo " , & getwalletinfo , { } } ,
2018-07-27 23:06:16 +02:00
{ " wallet " , " importaddress " , & importaddress , { " address " , " label " , " rescan " , " p2sh " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " importmulti " , & importmulti , { " requests " , " options " } } ,
{ " wallet " , " importprivkey " , & importprivkey , { " privkey " , " label " , " rescan " } } ,
{ " wallet " , " importprunedfunds " , & importprunedfunds , { " rawtransaction " , " txoutproof " } } ,
{ " wallet " , " importpubkey " , & importpubkey , { " pubkey " , " label " , " rescan " } } ,
2018-07-27 23:06:16 +02:00
{ " wallet " , " importwallet " , & importwallet , { " filename " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " keypoolrefill " , & keypoolrefill , { " newsize " } } ,
{ " wallet " , " listaddressgroupings " , & listaddressgroupings , { } } ,
2018-07-27 23:06:16 +02:00
{ " wallet " , " listlabels " , & listlabels , { " purpose " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " listlockunspent " , & listlockunspent , { } } ,
2017-01-09 23:17:48 +01:00
{ " wallet " , " listreceivedbyaddress " , & listreceivedbyaddress , { " minconf " , " include_empty " , " include_watchonly " , " address_filter " } } ,
2018-07-27 23:06:16 +02:00
{ " wallet " , " listreceivedbylabel " , & listreceivedbylabel , { " minconf " , " include_empty " , " include_watchonly " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " listsinceblock " , & listsinceblock , { " blockhash " , " target_confirmations " , " include_watchonly " , " include_removed " } } ,
2018-10-06 06:48:23 +02:00
{ " wallet " , " listtransactions " , & listtransactions , { " label|dummy " , " count " , " skip " , " include_watchonly " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " listunspent " , & listunspent , { " minconf " , " maxconf " , " addresses " , " include_unsafe " , " query_options " } } ,
2018-09-25 00:55:30 +02:00
{ " wallet " , " listwalletdir " , & listwalletdir , { } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " listwallets " , & listwallets , { } } ,
2018-04-18 22:01:39 +02:00
{ " wallet " , " loadwallet " , & loadwallet , { " filename " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " lockunspent " , & lockunspent , { " unlock " , " transactions " } } ,
2018-07-27 23:06:16 +02:00
{ " wallet " , " removeprunedfunds " , & removeprunedfunds , { " txid " } } ,
{ " wallet " , " rescanblockchain " , & rescanblockchain , { " start_height " , " stop_height " } } ,
2018-07-27 23:05:24 +02:00
{ " wallet " , " sendmany " , & sendmany , { " dummy " , " amounts " , " minconf " , " comment " , " subtractfeefrom " , " replaceable " , " conf_target " , " estimate_mode " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " sendtoaddress " , & sendtoaddress , { " address " , " amount " , " comment " , " comment_to " , " subtractfeefromamount " , " replaceable " , " conf_target " , " estimate_mode " } } ,
2018-07-27 23:06:16 +02:00
{ " wallet " , " sethdseed " , & sethdseed , { " newkeypool " , " seed " } } ,
{ " wallet " , " setlabel " , & setlabel , { " address " , " label " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " settxfee " , & settxfee , { " amount " } } ,
{ " wallet " , " signmessage " , & signmessage , { " address " , " message " } } ,
{ " wallet " , " signrawtransactionwithwallet " , & signrawtransactionwithwallet , { " hexstring " , " prevtxs " , " sighashtype " } } ,
2018-04-28 23:36:43 +02:00
{ " wallet " , " unloadwallet " , & unloadwallet , { " wallet_name " } } ,
2018-07-27 23:06:16 +02:00
{ " wallet " , " walletcreatefundedpsbt " , & walletcreatefundedpsbt , { " inputs " , " outputs " , " locktime " , " options " , " bip32derivs " } } ,
2017-06-12 21:23:02 +02:00
{ " wallet " , " walletlock " , & walletlock , { } } ,
{ " wallet " , " walletpassphrase " , & walletpassphrase , { " passphrase " , " timeout " } } ,
2018-07-27 23:06:16 +02:00
{ " wallet " , " walletpassphrasechange " , & walletpassphrasechange , { " oldpassphrase " , " newpassphrase " } } ,
{ " wallet " , " walletprocesspsbt " , & walletprocesspsbt , { " psbt " , " sign " , " sighashtype " , " bip32derivs " } } ,
2016-01-07 08:33:49 +01:00
} ;
2018-08-20 14:19:43 +02:00
// clang-format on
2016-01-07 08:33:49 +01:00
2016-06-07 18:42:42 +02:00
void RegisterWalletRPCCommands ( CRPCTable & t )
2016-01-07 08:33:49 +01:00
{
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-01-07 08:33:49 +01:00
}