2012-08-21 16:38:57 +02:00
// Copyright (c) 2010 Satoshi Nakamoto
2014-02-08 22:50:24 +01:00
// Copyright (c) 2009-2014 The Bitcoin developers
2015-03-18 00:06:58 +01:00
// Copyright (c) 2014-2015 The Dash developers
2012-08-21 16:38:57 +02:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2013-04-13 07:13:08 +02:00
# include "base58.h"
2013-11-20 14:18:57 +01:00
# include "rpcserver.h"
2012-08-21 16:38:57 +02:00
# include "init.h"
2013-04-13 07:13:08 +02:00
# include "net.h"
# include "netbase.h"
# include "util.h"
# include "wallet.h"
# include "walletdb.h"
2014-12-26 12:53:29 +01:00
# include "keepass.h"
2013-04-13 07:13:08 +02:00
# include <stdint.h>
# include <boost/assign/list_of.hpp>
# include "json/json_spirit_utils.h"
# include "json/json_spirit_value.h"
2012-08-21 16:38:57 +02:00
using namespace std ;
2012-09-27 19:52:09 +02:00
using namespace boost ;
using namespace boost : : assign ;
using namespace json_spirit ;
2012-08-21 16:38:57 +02:00
2013-04-13 07:13:08 +02:00
int64_t nWalletUnlockTime ;
2012-08-21 16:38:57 +02:00
static CCriticalSection cs_nWalletUnlockTime ;
2012-08-24 08:44:51 +02:00
std : : string HelpRequiringPassphrase ( )
2012-08-21 16:38:57 +02:00
{
2013-08-25 06:00:02 +02:00
return pwalletMain & & pwalletMain - > 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
: " " ;
}
2012-08-24 08:44:51 +02:00
void EnsureWalletIsUnlocked ( )
2012-08-21 16:38:57 +02:00
{
if ( pwalletMain - > IsLocked ( ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_UNLOCK_NEEDED , " Error: Please enter the wallet passphrase with walletpassphrase first. " ) ;
2012-08-21 16:38:57 +02:00
}
void WalletTxToJSON ( const CWalletTx & wtx , Object & entry )
{
2015-02-05 16:52:02 +01:00
int confirms = wtx . GetDepthInMainChain ( false ) ;
2012-08-21 16:38:57 +02:00
entry . push_back ( Pair ( " confirmations " , confirms ) ) ;
2012-06-02 04:33:28 +02:00
if ( wtx . IsCoinBase ( ) )
entry . push_back ( Pair ( " generated " , true ) ) ;
2014-02-12 19:43:07 +01:00
if ( confirms > 0 )
2012-08-21 16:38:57 +02:00
{
entry . push_back ( Pair ( " blockhash " , wtx . hashBlock . GetHex ( ) ) ) ;
entry . push_back ( Pair ( " blockindex " , wtx . nIndex ) ) ;
2014-05-05 20:08:13 +02:00
entry . push_back ( Pair ( " blocktime " , ( int64_t ) ( mapBlockIndex [ wtx . hashBlock ] - > nTime ) ) ) ;
2012-08-21 16:38:57 +02:00
}
2014-02-14 02:12:51 +01:00
uint256 hash = wtx . GetHash ( ) ;
entry . push_back ( Pair ( " txid " , hash . GetHex ( ) ) ) ;
Array conflicts ;
BOOST_FOREACH ( const uint256 & conflict , wtx . GetConflicts ( ) )
conflicts . push_back ( conflict . GetHex ( ) ) ;
entry . push_back ( Pair ( " walletconflicts " , conflicts ) ) ;
2014-05-05 20:08:13 +02:00
entry . push_back ( Pair ( " time " , wtx . GetTxTime ( ) ) ) ;
entry . push_back ( Pair ( " timereceived " , ( int64_t ) wtx . nTimeReceived ) ) ;
2012-08-21 16:38:57 +02:00
BOOST_FOREACH ( const PAIRTYPE ( string , string ) & item , wtx . mapValue )
entry . push_back ( Pair ( item . first , item . second ) ) ;
}
string AccountFromValue ( const Value & value )
{
string strAccount = value . get_str ( ) ;
if ( strAccount = = " * " )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_INVALID_ACCOUNT_NAME , " Invalid account name " ) ;
2012-08-21 16:38:57 +02:00
return strAccount ;
}
Value getnewaddress ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) > 1 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" getnewaddress ( \" account \" ) \n "
2015-03-18 00:06:58 +01:00
" \n Returns a new Dash address for receiving payments. \n "
2013-10-29 12:29:44 +01:00
" If 'account' is specified (recommended), it is added to the address book \n "
" so payments received with the address will be credited to 'account'. \n "
" \n Arguments: \n "
" 1. \" account \" (string, optional) The account name for the address to be linked to. if not provided, the default account \" \" is used. It can also be set to the empty string \" \" to represent the default account. The account does not need to exist, it will be created if there is no account by the given name. \n "
" \n Result: \n "
2015-03-19 15:15:08 +01:00
" \" dashaddress \" (string) The new dash address \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
+ HelpExampleCli ( " getnewaddress " , " " )
+ HelpExampleCli ( " getnewaddress " , " \" \" " )
+ HelpExampleCli ( " getnewaddress " , " \" myaccount \" " )
+ HelpExampleRpc ( " getnewaddress " , " \" myaccount \" " )
) ;
2012-08-21 16:38:57 +02:00
// Parse the account first so we don't generate a key if there's an error
string strAccount ;
if ( params . size ( ) > 0 )
strAccount = AccountFromValue ( params [ 0 ] ) ;
if ( ! pwalletMain - > IsLocked ( ) )
pwalletMain - > TopUpKeyPool ( ) ;
// Generate a new key that is added to wallet
CPubKey newKey ;
2013-08-23 21:54:50 +02:00
if ( ! pwalletMain - > 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 " ) ;
2012-08-21 16:38:57 +02:00
CKeyID keyID = newKey . GetID ( ) ;
2013-07-22 08:50:39 +02:00
pwalletMain - > SetAddressBook ( keyID , strAccount , " receive " ) ;
2012-08-21 16:38:57 +02:00
return CBitcoinAddress ( keyID ) . ToString ( ) ;
}
CBitcoinAddress GetAccountAddress ( string strAccount , bool bForceNew = false )
{
CWalletDB walletdb ( pwalletMain - > strWalletFile ) ;
CAccount account ;
walletdb . ReadAccount ( strAccount , account ) ;
bool bKeyUsed = false ;
// Check if the current key has been used
if ( account . vchPubKey . IsValid ( ) )
{
CScript scriptPubKey ;
scriptPubKey . SetDestination ( account . vchPubKey . GetID ( ) ) ;
for ( map < uint256 , CWalletTx > : : iterator it = pwalletMain - > mapWallet . begin ( ) ;
it ! = pwalletMain - > mapWallet . end ( ) & & account . vchPubKey . IsValid ( ) ;
+ + it )
{
const CWalletTx & wtx = ( * it ) . second ;
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
if ( txout . scriptPubKey = = scriptPubKey )
bKeyUsed = true ;
}
}
// Generate a new key
if ( ! account . vchPubKey . IsValid ( ) | | bForceNew | | bKeyUsed )
{
2013-08-23 21:54:50 +02:00
if ( ! pwalletMain - > GetKeyFromPool ( account . vchPubKey ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_KEYPOOL_RAN_OUT , " Error: Keypool ran out, please call keypoolrefill first " ) ;
2012-08-21 16:38:57 +02:00
2013-07-22 08:50:39 +02:00
pwalletMain - > SetAddressBook ( account . vchPubKey . GetID ( ) , strAccount , " receive " ) ;
2012-08-21 16:38:57 +02:00
walletdb . WriteAccount ( strAccount , account ) ;
}
return CBitcoinAddress ( account . vchPubKey . GetID ( ) ) ;
}
Value getaccountaddress ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 1 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" getaccountaddress \" account \" \n "
2015-03-18 00:06:58 +01:00
" \n Returns the current Dash address for receiving payments to this account. \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
" 1. \" account \" (string, required) The account name for the address. It can also be set to the empty string \" \" to represent the default account. The account does not need to exist, it will be created and a new address created if there is no account by the given name. \n "
" \n Result: \n "
2015-03-19 15:15:08 +01:00
" \" dashaddress \" (string) The account dash address \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
+ HelpExampleCli ( " getaccountaddress " , " " )
+ HelpExampleCli ( " getaccountaddress " , " \" \" " )
+ HelpExampleCli ( " getaccountaddress " , " \" myaccount \" " )
+ HelpExampleRpc ( " getaccountaddress " , " \" myaccount \" " )
) ;
2012-08-21 16:38:57 +02:00
// Parse the account first so we don't generate a key if there's an error
string strAccount = AccountFromValue ( params [ 0 ] ) ;
Value ret ;
ret = GetAccountAddress ( strAccount ) . ToString ( ) ;
return ret ;
}
2013-08-28 22:29:52 +02:00
Value getrawchangeaddress ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) > 1 )
throw runtime_error (
" getrawchangeaddress \n "
2015-03-18 00:06:58 +01:00
" \n Returns a new Dash address, for receiving change. \n "
2013-10-29 12:29:44 +01:00
" This is for use with raw transactions, NOT normal use. \n "
" \n Result: \n "
" \" address \" (string) The address \n "
" \n Examples: \n "
+ HelpExampleCli ( " getrawchangeaddress " , " " )
+ HelpExampleRpc ( " getrawchangeaddress " , " " )
) ;
2013-08-28 22:29:52 +02:00
if ( ! pwalletMain - > IsLocked ( ) )
pwalletMain - > TopUpKeyPool ( ) ;
CReserveKey reservekey ( pwalletMain ) ;
CPubKey vchPubKey ;
if ( ! reservekey . GetReservedKey ( vchPubKey ) )
throw JSONRPCError ( RPC_WALLET_ERROR , " Error: Unable to obtain key for change " ) ;
reservekey . KeepKey ( ) ;
CKeyID keyID = vchPubKey . GetID ( ) ;
return CBitcoinAddress ( keyID ) . ToString ( ) ;
}
2012-08-21 16:38:57 +02:00
Value setaccount ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) < 1 | | params . size ( ) > 2 )
throw runtime_error (
2015-03-19 15:15:08 +01:00
" setaccount \" dashaddress \" \" account \" \n "
2013-10-29 12:29:44 +01:00
" \n Sets the account associated with the given address. \n "
" \n Arguments: \n "
2015-03-19 15:15:08 +01:00
" 1. \" dashaddress \" (string, required) The dash address to be associated with an account. \n "
2013-10-29 12:29:44 +01:00
" 2. \" account \" (string, required) The account to assign the address to. \n "
" \n Examples: \n "
2014-12-10 00:22:01 +01:00
+ HelpExampleCli ( " setaccount " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" \" tabby \" " )
+ HelpExampleRpc ( " setaccount " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" , \" tabby \" " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
CBitcoinAddress address ( params [ 0 ] . get_str ( ) ) ;
if ( ! address . IsValid ( ) )
2015-03-18 00:06:58 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid Dash address " ) ;
2012-08-21 16:38:57 +02:00
string strAccount ;
if ( params . size ( ) > 1 )
strAccount = AccountFromValue ( params [ 1 ] ) ;
// Detect when changing the account of an address that is the 'unused current key' of another account:
if ( pwalletMain - > mapAddressBook . count ( address . Get ( ) ) )
{
2013-07-15 07:20:50 +02:00
string strOldAccount = pwalletMain - > mapAddressBook [ address . Get ( ) ] . name ;
2012-08-21 16:38:57 +02:00
if ( address = = GetAccountAddress ( strOldAccount ) )
GetAccountAddress ( strOldAccount , true ) ;
}
2013-07-22 08:50:39 +02:00
pwalletMain - > SetAddressBook ( address . Get ( ) , strAccount , " receive " ) ;
2012-08-21 16:38:57 +02:00
return Value : : null ;
}
Value getaccount ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 1 )
throw runtime_error (
2015-03-19 15:15:08 +01:00
" getaccount \" dashaddress \" \n "
2013-10-29 12:29:44 +01:00
" \n Returns the account associated with the given address. \n "
" \n Arguments: \n "
2015-03-19 15:15:08 +01:00
" 1. \" dashaddress \" (string, required) The dash address for account lookup. \n "
2013-10-29 12:29:44 +01:00
" \n Result: \n "
" \" accountname \" (string) the account address \n "
" \n Examples: \n "
2014-12-10 00:22:01 +01:00
+ HelpExampleCli ( " getaccount " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" " )
+ HelpExampleRpc ( " getaccount " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
CBitcoinAddress address ( params [ 0 ] . get_str ( ) ) ;
if ( ! address . IsValid ( ) )
2015-03-18 00:06:58 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid Dash address " ) ;
2012-08-21 16:38:57 +02:00
string strAccount ;
2013-07-15 07:20:50 +02:00
map < CTxDestination , CAddressBookData > : : iterator mi = pwalletMain - > mapAddressBook . find ( address . Get ( ) ) ;
if ( mi ! = pwalletMain - > mapAddressBook . end ( ) & & ! ( * mi ) . second . name . empty ( ) )
strAccount = ( * mi ) . second . name ;
2012-08-21 16:38:57 +02:00
return strAccount ;
}
Value getaddressesbyaccount ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 1 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" getaddressesbyaccount \" account \" \n "
" \n Returns the list of addresses for the given account. \n "
" \n Arguments: \n "
" 1. \" account \" (string, required) The account name. \n "
" \n Result: \n "
" [ (json array of string) \n "
2015-03-19 15:15:08 +01:00
" \" dashaddress \" (string) a dash address associated with the given account \n "
2013-10-29 12:29:44 +01:00
" ,... \n "
" ] \n "
" \n Examples: \n "
+ HelpExampleCli ( " getaddressesbyaccount " , " \" tabby \" " )
+ HelpExampleRpc ( " getaddressesbyaccount " , " \" tabby \" " )
) ;
2012-08-21 16:38:57 +02:00
string strAccount = AccountFromValue ( params [ 0 ] ) ;
// Find all addresses that have the given account
Array ret ;
2013-07-15 07:20:50 +02:00
BOOST_FOREACH ( const PAIRTYPE ( CBitcoinAddress , CAddressBookData ) & item , pwalletMain - > mapAddressBook )
2012-08-21 16:38:57 +02:00
{
const CBitcoinAddress & address = item . first ;
2013-07-15 07:20:50 +02:00
const string & strName = item . second . name ;
2012-08-21 16:38:57 +02:00
if ( strName = = strAccount )
ret . push_back ( address . ToString ( ) ) ;
}
return ret ;
}
Value sendtoaddress ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) < 2 | | params . size ( ) > 4 )
throw runtime_error (
2015-03-19 15:15:08 +01:00
" sendtoaddress \" dashaddress \" amount ( \" comment \" \" comment-to \" ) \n "
2013-10-29 12:29:44 +01:00
" \n Sent an amount to a given address. The amount is a real and is rounded to the nearest 0.00000001 \n "
+ HelpRequiringPassphrase ( ) +
" \n Arguments: \n "
2015-03-19 15:15:08 +01:00
" 1. \" dashaddress \" (string, required) The dash address to send to. \n "
2013-10-29 12:29:44 +01:00
" 2. \" amount \" (numeric, required) The amount in btc to send. eg 0.1 \n "
" 3. \" comment \" (string, optional) A comment used to store what the transaction is for. \n "
" This is not part of the transaction, just kept in your wallet. \n "
" 4. \" comment-to \" (string, optional) A comment to store the name of the person or organization \n "
" to which you're sending the transaction. This is not part of the \n "
" transaction, just kept in your wallet. \n "
" \n Result: \n "
2014-05-29 18:17:34 +02:00
" \" transactionid \" (string) The transaction id. \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
2014-12-12 12:44:25 +01:00
+ HelpExampleCli ( " sendtoaddress " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" 0.1 " )
+ HelpExampleCli ( " sendtoaddress " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" 0.1 \" donation \" \" seans outpost \" " )
+ HelpExampleRpc ( " sendtoaddress " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" , 0.1, \" donation \" , \" seans outpost \" " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
CBitcoinAddress address ( params [ 0 ] . get_str ( ) ) ;
if ( ! address . IsValid ( ) )
2015-03-18 00:06:58 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid Dash address " ) ;
2012-08-21 16:38:57 +02:00
// Amount
2013-04-13 07:13:08 +02:00
int64_t nAmount = AmountFromValue ( params [ 1 ] ) ;
2012-08-21 16:38:57 +02:00
// Wallet comments
CWalletTx wtx ;
if ( params . size ( ) > 2 & & params [ 2 ] . type ( ) ! = null_type & & ! params [ 2 ] . get_str ( ) . empty ( ) )
wtx . mapValue [ " comment " ] = params [ 2 ] . get_str ( ) ;
if ( params . size ( ) > 3 & & params [ 3 ] . type ( ) ! = null_type & & ! params [ 3 ] . get_str ( ) . empty ( ) )
wtx . mapValue [ " to " ] = params [ 3 ] . get_str ( ) ;
2013-10-28 11:58:39 +01:00
EnsureWalletIsUnlocked ( ) ;
2012-08-21 16:38:57 +02:00
string strError = pwalletMain - > SendMoneyToDestination ( address . Get ( ) , nAmount , wtx ) ;
if ( strError ! = " " )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , strError ) ;
2012-08-21 16:38:57 +02:00
return wtx . GetHash ( ) . GetHex ( ) ;
}
2012-08-01 18:48:42 +02:00
Value listaddressgroupings ( const Array & params , bool fHelp )
{
if ( fHelp )
2012-08-20 19:43:33 +02:00
throw runtime_error (
" 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 "
2015-03-19 15:15:08 +01:00
" \" dashaddress \" , (string) The dash address \n "
2013-10-29 12:29:44 +01:00
" amount, (numeric) The amount in btc \n "
" \" account \" (string, optional) The account \n "
" ] \n "
" ,... \n "
" ] \n "
" ,... \n "
" ] \n "
" \n Examples: \n "
+ HelpExampleCli ( " listaddressgroupings " , " " )
+ HelpExampleRpc ( " listaddressgroupings " , " " )
) ;
2012-08-01 18:48:42 +02:00
Array jsonGroupings ;
2013-04-13 07:13:08 +02:00
map < CTxDestination , int64_t > balances = pwalletMain - > GetAddressBalances ( ) ;
2012-08-20 19:43:33 +02:00
BOOST_FOREACH ( set < CTxDestination > grouping , pwalletMain - > GetAddressGroupings ( ) )
2012-08-01 18:48:42 +02:00
{
Array jsonGrouping ;
2012-08-20 19:43:33 +02:00
BOOST_FOREACH ( CTxDestination address , grouping )
2012-08-01 18:48:42 +02:00
{
Array addressInfo ;
2012-08-20 19:43:33 +02:00
addressInfo . push_back ( CBitcoinAddress ( address ) . ToString ( ) ) ;
2012-08-01 18:48:42 +02:00
addressInfo . push_back ( ValueFromAmount ( balances [ address ] ) ) ;
{
LOCK ( pwalletMain - > cs_wallet ) ;
if ( pwalletMain - > mapAddressBook . find ( CBitcoinAddress ( address ) . Get ( ) ) ! = pwalletMain - > mapAddressBook . end ( ) )
2013-07-15 07:20:50 +02:00
addressInfo . push_back ( pwalletMain - > mapAddressBook . find ( CBitcoinAddress ( address ) . Get ( ) ) - > second . name ) ;
2012-08-01 18:48:42 +02:00
}
jsonGrouping . push_back ( addressInfo ) ;
}
jsonGroupings . push_back ( jsonGrouping ) ;
}
return jsonGroupings ;
}
2012-08-21 16:38:57 +02:00
Value signmessage ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 2 )
throw runtime_error (
2015-03-19 15:15:08 +01:00
" signmessage \" dashaddress \" \" message \" \n "
2013-10-29 12:29:44 +01:00
" \n Sign a message with the private key of an address "
+ HelpRequiringPassphrase ( ) + " \n "
" \n Arguments: \n "
2015-03-19 15:15:08 +01:00
" 1. \" dashaddress \" (string, required) The dash 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 "
2014-12-10 00:22:01 +01:00
+ HelpExampleCli ( " signmessage " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" \" my message \" " ) +
2013-10-29 12:29:44 +01:00
" \n Verify the signature \n "
2014-12-10 00:22:01 +01:00
+ HelpExampleCli ( " verifymessage " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" \" signature \" \" my message \" " ) +
2013-10-29 12:29:44 +01:00
" \n As json rpc \n "
2014-12-10 00:22:01 +01:00
+ HelpExampleRpc ( " signmessage " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" , \" my message \" " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
EnsureWalletIsUnlocked ( ) ;
string strAddress = params [ 0 ] . get_str ( ) ;
string strMessage = params [ 1 ] . get_str ( ) ;
CBitcoinAddress addr ( strAddress ) ;
if ( ! addr . IsValid ( ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_TYPE_ERROR , " Invalid address " ) ;
2012-08-21 16:38:57 +02:00
CKeyID keyID ;
if ( ! addr . GetKeyID ( keyID ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_TYPE_ERROR , " Address does not refer to key " ) ;
2012-08-21 16:38:57 +02:00
CKey key ;
if ( ! pwalletMain - > GetKey ( keyID , key ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , " Private key not available " ) ;
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 ;
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
return EncodeBase64 ( & vchSig [ 0 ] , vchSig . size ( ) ) ;
}
Value getreceivedbyaddress ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) < 1 | | params . size ( ) > 2 )
throw runtime_error (
2015-03-19 15:15:08 +01:00
" getreceivedbyaddress \" dashaddress \" ( minconf ) \n "
" \n Returns the total amount received by the given dashaddress in transactions with at least minconf confirmations. \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
2015-03-19 15:15:08 +01:00
" 1. \" dashaddress \" (string, required) The dash 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 "
" amount (numeric) The total amount in btc received at this address. \n "
" \n Examples: \n "
" \n The amount from transactions with at least 1 confirmation \n "
2014-12-10 00:22:01 +01:00
+ HelpExampleCli ( " getreceivedbyaddress " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" " ) +
2013-10-29 12:29:44 +01:00
" \n The amount including unconfirmed transactions, zero confirmations \n "
2014-12-10 00:22:01 +01:00
+ HelpExampleCli ( " getreceivedbyaddress " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" 0 " ) +
2013-10-29 12:29:44 +01:00
" \n The amount with at least 6 confirmation, very safe \n "
2014-12-10 00:22:01 +01:00
+ HelpExampleCli ( " getreceivedbyaddress " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" 6 " ) +
2013-10-29 12:29:44 +01:00
" \n As a json rpc call \n "
2014-12-10 00:22:01 +01:00
+ HelpExampleRpc ( " getreceivedbyaddress " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" , 6 " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
2015-03-18 00:06:58 +01:00
// Dash address
2012-08-21 16:38:57 +02:00
CBitcoinAddress address = CBitcoinAddress ( params [ 0 ] . get_str ( ) ) ;
CScript scriptPubKey ;
if ( ! address . IsValid ( ) )
2015-03-18 00:06:58 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid Dash address " ) ;
2012-08-21 16:38:57 +02:00
scriptPubKey . SetDestination ( address . Get ( ) ) ;
if ( ! IsMine ( * pwalletMain , scriptPubKey ) )
return ( double ) 0.0 ;
// Minimum confirmations
int nMinDepth = 1 ;
if ( params . size ( ) > 1 )
nMinDepth = params [ 1 ] . get_int ( ) ;
// Tally
2013-04-13 07:13:08 +02:00
int64_t nAmount = 0 ;
2012-08-21 16:38:57 +02:00
for ( map < uint256 , CWalletTx > : : iterator it = pwalletMain - > mapWallet . begin ( ) ; it ! = pwalletMain - > mapWallet . end ( ) ; + + it )
{
const CWalletTx & wtx = ( * it ) . second ;
2013-01-08 13:17:15 +01:00
if ( wtx . IsCoinBase ( ) | | ! IsFinalTx ( wtx ) )
2012-08-21 16:38:57 +02:00
continue ;
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
if ( txout . scriptPubKey = = scriptPubKey )
if ( wtx . GetDepthInMainChain ( ) > = nMinDepth )
nAmount + = txout . nValue ;
}
return ValueFromAmount ( nAmount ) ;
}
Value getreceivedbyaccount ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) < 1 | | params . size ( ) > 2 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" getreceivedbyaccount \" account \" ( minconf ) \n "
" \n Returns the total amount received by addresses with <account> in transactions with at least [minconf] confirmations. \n "
" \n Arguments: \n "
" 1. \" account \" (string, required) The selected account, may be the default account using \" \" . \n "
" 2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times. \n "
" \n Result: \n "
" amount (numeric) The total amount in btc received for this account. \n "
" \n Examples: \n "
" \n Amount received by the default account with at least 1 confirmation \n "
+ HelpExampleCli ( " getreceivedbyaccount " , " \" \" " ) +
" \n Amount received at the tabby account including unconfirmed amounts with zero confirmations \n "
+ HelpExampleCli ( " getreceivedbyaccount " , " \" tabby \" 0 " ) +
" \n The amount with at least 6 confirmation, very safe \n "
+ HelpExampleCli ( " getreceivedbyaccount " , " \" tabby \" 6 " ) +
" \n As a json rpc call \n "
+ HelpExampleRpc ( " getreceivedbyaccount " , " \" tabby \" , 6 " )
) ;
2012-08-21 16:38:57 +02:00
// Minimum confirmations
int nMinDepth = 1 ;
if ( params . size ( ) > 1 )
nMinDepth = params [ 1 ] . get_int ( ) ;
// Get the set of pub keys assigned to account
string strAccount = AccountFromValue ( params [ 0 ] ) ;
2013-07-16 01:01:09 +02:00
set < CTxDestination > setAddress = pwalletMain - > GetAccountAddresses ( strAccount ) ;
2012-08-21 16:38:57 +02:00
// Tally
2013-04-13 07:13:08 +02:00
int64_t nAmount = 0 ;
2012-08-21 16:38:57 +02:00
for ( map < uint256 , CWalletTx > : : iterator it = pwalletMain - > mapWallet . begin ( ) ; it ! = pwalletMain - > mapWallet . end ( ) ; + + it )
{
const CWalletTx & wtx = ( * it ) . second ;
2013-01-08 13:17:15 +01:00
if ( wtx . IsCoinBase ( ) | | ! IsFinalTx ( wtx ) )
2012-08-21 16:38:57 +02:00
continue ;
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
{
CTxDestination address ;
if ( ExtractDestination ( txout . scriptPubKey , address ) & & IsMine ( * pwalletMain , address ) & & setAddress . count ( address ) )
if ( wtx . GetDepthInMainChain ( ) > = nMinDepth )
nAmount + = txout . nValue ;
}
}
return ( double ) nAmount / ( double ) COIN ;
}
2013-04-13 07:13:08 +02:00
int64_t GetAccountBalance ( CWalletDB & walletdb , const string & strAccount , int nMinDepth )
2012-08-21 16:38:57 +02:00
{
2013-04-13 07:13:08 +02:00
int64_t nBalance = 0 ;
2012-08-21 16:38:57 +02:00
// Tally wallet transactions
for ( map < uint256 , CWalletTx > : : iterator it = pwalletMain - > mapWallet . begin ( ) ; it ! = pwalletMain - > mapWallet . end ( ) ; + + it )
{
const CWalletTx & wtx = ( * it ) . second ;
2014-02-15 22:38:28 +01:00
if ( ! IsFinalTx ( wtx ) | | wtx . GetBlocksToMaturity ( ) > 0 | | wtx . GetDepthInMainChain ( ) < 0 )
2012-08-21 16:38:57 +02:00
continue ;
2013-04-13 07:13:08 +02:00
int64_t nReceived , nSent , nFee ;
2012-06-02 04:33:28 +02:00
wtx . GetAccountAmounts ( strAccount , nReceived , nSent , nFee ) ;
2012-08-21 16:38:57 +02:00
if ( nReceived ! = 0 & & wtx . GetDepthInMainChain ( ) > = nMinDepth )
nBalance + = nReceived ;
2012-06-02 04:33:28 +02:00
nBalance - = nSent + nFee ;
2012-08-21 16:38:57 +02:00
}
// Tally internal accounting entries
nBalance + = walletdb . GetAccountCreditDebit ( strAccount ) ;
return nBalance ;
}
2013-04-13 07:13:08 +02:00
int64_t GetAccountBalance ( const string & strAccount , int nMinDepth )
2012-08-21 16:38:57 +02:00
{
CWalletDB walletdb ( pwalletMain - > strWalletFile ) ;
return GetAccountBalance ( walletdb , strAccount , nMinDepth ) ;
}
Value getbalance ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) > 2 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" getbalance ( \" account \" minconf ) \n "
" \n If account is not specified, returns the server's total available balance. \n "
" If account is specified, returns the balance in the account. \n "
" Note that the account \" \" is not the same as leaving the parameter out. \n "
" The server total may be different to the balance in the default \" \" account. \n "
" \n Arguments: \n "
2014-01-29 13:41:34 +01:00
" 1. \" account \" (string, optional) The selected account, or \" * \" for entire wallet. It may be the default account 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 "
" amount (numeric) The total amount in btc received for this account. \n "
" \n Examples: \n "
" \n The total amount in the server across all accounts \n "
+ HelpExampleCli ( " getbalance " , " " ) +
2014-01-29 13:41:34 +01:00
" \n The total amount in the server across all accounts, with at least 5 confirmations \n "
+ HelpExampleCli ( " getbalance " , " \" * \" 6 " ) +
2013-10-29 12:29:44 +01:00
" \n The total amount in the default account with at least 1 confirmation \n "
+ HelpExampleCli ( " getbalance " , " \" \" " ) +
" \n The total amount in the account named tabby with at least 6 confirmations \n "
+ HelpExampleCli ( " getbalance " , " \" tabby \" 6 " ) +
" \n As a json rpc call \n "
+ HelpExampleRpc ( " getbalance " , " \" tabby \" , 6 " )
) ;
2012-08-21 16:38:57 +02:00
if ( params . size ( ) = = 0 )
return ValueFromAmount ( pwalletMain - > GetBalance ( ) ) ;
int nMinDepth = 1 ;
if ( params . size ( ) > 1 )
nMinDepth = params [ 1 ] . get_int ( ) ;
if ( params [ 0 ] . get_str ( ) = = " * " ) {
// Calculate total balance a different way from GetBalance()
// (GetBalance() sums up all unspent TxOuts)
2013-02-04 20:04:26 +01:00
// getbalance and getbalance '*' 0 should return the same number
2013-04-13 07:13:08 +02:00
int64_t nBalance = 0 ;
2012-08-21 16:38:57 +02:00
for ( map < uint256 , CWalletTx > : : iterator it = pwalletMain - > mapWallet . begin ( ) ; it ! = pwalletMain - > mapWallet . end ( ) ; + + it )
{
const CWalletTx & wtx = ( * it ) . second ;
2014-02-14 02:12:51 +01:00
if ( ! wtx . IsTrusted ( ) | | wtx . GetBlocksToMaturity ( ) > 0 )
2012-08-21 16:38:57 +02:00
continue ;
2013-04-13 07:13:08 +02:00
int64_t allFee ;
2012-08-21 16:38:57 +02:00
string strSentAccount ;
2013-04-13 07:13:08 +02:00
list < pair < CTxDestination , int64_t > > listReceived ;
list < pair < CTxDestination , int64_t > > listSent ;
2012-06-02 04:33:28 +02:00
wtx . GetAmounts ( listReceived , listSent , allFee , strSentAccount ) ;
2012-08-21 16:38:57 +02:00
if ( wtx . GetDepthInMainChain ( ) > = nMinDepth )
{
2013-04-13 07:13:08 +02:00
BOOST_FOREACH ( const PAIRTYPE ( CTxDestination , int64_t ) & r , listReceived )
2012-08-21 16:38:57 +02:00
nBalance + = r . second ;
}
2013-04-13 07:13:08 +02:00
BOOST_FOREACH ( const PAIRTYPE ( CTxDestination , int64_t ) & r , listSent )
2012-08-21 16:38:57 +02:00
nBalance - = r . second ;
nBalance - = allFee ;
}
return ValueFromAmount ( nBalance ) ;
}
string strAccount = AccountFromValue ( params [ 0 ] ) ;
2013-04-13 07:13:08 +02:00
int64_t nBalance = GetAccountBalance ( strAccount , nMinDepth ) ;
2012-08-21 16:38:57 +02:00
return ValueFromAmount ( nBalance ) ;
}
2013-11-24 12:48:52 +01:00
Value getunconfirmedbalance ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) > 0 )
throw runtime_error (
" getunconfirmedbalance \n "
" Returns the server's total unconfirmed balance \n " ) ;
return ValueFromAmount ( pwalletMain - > GetUnconfirmedBalance ( ) ) ;
}
2012-08-21 16:38:57 +02:00
Value movecmd ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) < 3 | | params . size ( ) > 5 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" move \" fromaccount \" \" toaccount \" amount ( minconf \" comment \" ) \n "
" \n Move a specified amount from one account in your wallet to another. \n "
" \n Arguments: \n "
" 1. \" fromaccount \" (string, required) The name of the account to move funds from. May be the default account using \" \" . \n "
" 2. \" toaccount \" (string, required) The name of the account to move funds to. May be the default account using \" \" . \n "
" 3. minconf (numeric, optional, default=1) Only use funds with at least this many confirmations. \n "
" 4. \" comment \" (string, optional) An optional comment, stored in the wallet only. \n "
" \n Result: \n "
" true|false (boolean) true if successfull. \n "
" \n Examples: \n "
" \n Move 0.01 btc from the default account to the account named tabby \n "
+ HelpExampleCli ( " move " , " \" \" \" tabby \" 0.01 " ) +
" \n Move 0.01 btc timotei to akiko with a comment and funds have 6 confirmations \n "
+ HelpExampleCli ( " move " , " \" timotei \" \" akiko \" 0.01 6 \" happy birthday! \" " ) +
" \n As a json rpc call \n "
+ HelpExampleRpc ( " move " , " \" timotei \" , \" akiko \" , 0.01, 6, \" happy birthday! \" " )
) ;
2012-08-21 16:38:57 +02:00
string strFrom = AccountFromValue ( params [ 0 ] ) ;
string strTo = AccountFromValue ( params [ 1 ] ) ;
2013-04-13 07:13:08 +02:00
int64_t nAmount = AmountFromValue ( params [ 2 ] ) ;
2012-08-21 16:38:57 +02:00
if ( params . size ( ) > 3 )
// unused parameter, used to be nMinDepth, keep type-checking it though
( void ) params [ 3 ] . get_int ( ) ;
string strComment ;
if ( params . size ( ) > 4 )
strComment = params [ 4 ] . get_str ( ) ;
CWalletDB walletdb ( pwalletMain - > strWalletFile ) ;
if ( ! walletdb . TxnBegin ( ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_DATABASE_ERROR , " database error " ) ;
2012-08-21 16:38:57 +02:00
2013-04-13 07:13:08 +02:00
int64_t nNow = GetAdjustedTime ( ) ;
2012-08-21 16:38:57 +02:00
// Debit
CAccountingEntry debit ;
2012-11-13 23:52:37 +01:00
debit . nOrderPos = pwalletMain - > IncOrderPosNext ( & walletdb ) ;
2012-08-21 16:38:57 +02:00
debit . strAccount = strFrom ;
debit . nCreditDebit = - nAmount ;
debit . nTime = nNow ;
debit . strOtherAccount = strTo ;
debit . strComment = strComment ;
walletdb . WriteAccountingEntry ( debit ) ;
// Credit
CAccountingEntry credit ;
2012-11-13 23:52:37 +01:00
credit . nOrderPos = pwalletMain - > IncOrderPosNext ( & walletdb ) ;
2012-08-21 16:38:57 +02:00
credit . strAccount = strTo ;
credit . nCreditDebit = nAmount ;
credit . nTime = nNow ;
credit . strOtherAccount = strFrom ;
credit . strComment = strComment ;
walletdb . WriteAccountingEntry ( credit ) ;
if ( ! walletdb . TxnCommit ( ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_DATABASE_ERROR , " database error " ) ;
2012-08-21 16:38:57 +02:00
return true ;
}
Value sendfrom ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) < 3 | | params . size ( ) > 6 )
throw runtime_error (
2015-03-19 15:15:08 +01:00
" sendfrom \" fromaccount \" \" todashaddress \" amount ( minconf \" comment \" \" comment-to \" ) \n "
" \n Sent an amount from an account to a dash address. \n "
2013-10-29 12:29:44 +01:00
" The amount is a real and is rounded to the nearest 0.00000001. "
+ HelpRequiringPassphrase ( ) + " \n "
" \n Arguments: \n "
" 1. \" fromaccount \" (string, required) The name of the account to send funds from. May be the default account using \" \" . \n "
2015-03-19 15:15:08 +01:00
" 2. \" todashaddress \" (string, required) The dash address to send funds to. \n "
2013-10-29 12:29:44 +01:00
" 3. amount (numeric, required) The amount in btc. (transaction fee is added on top). \n "
" 4. minconf (numeric, optional, default=1) Only use funds with at least this many confirmations. \n "
" 5. \" comment \" (string, optional) A comment used to store what the transaction is for. \n "
" This is not part of the transaction, just kept in your wallet. \n "
" 6. \" comment-to \" (string, optional) An optional comment to store the name of the person or organization \n "
" to which you're sending the transaction. This is not part of the transaction, \n "
" it is just kept in your wallet. \n "
" \n Result: \n "
2014-05-29 18:17:34 +02:00
" \" transactionid \" (string) The transaction id. \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
" \n Send 0.01 btc from the default account to the address, must have at least 1 confirmation \n "
2014-12-12 12:44:25 +01:00
+ HelpExampleCli ( " sendfrom " , " \" \" \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" 0.01 " ) +
2013-10-29 12:29:44 +01:00
" \n Send 0.01 from the tabby account to the given address, funds must have at least 6 confirmations \n "
2014-12-12 12:44:25 +01:00
+ HelpExampleCli ( " sendfrom " , " \" tabby \" \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" 0.01 6 \" donation \" \" seans outpost \" " ) +
2013-10-29 12:29:44 +01:00
" \n As a json rpc call \n "
2014-12-12 12:44:25 +01:00
+ HelpExampleRpc ( " sendfrom " , " \" tabby \" , \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" , 0.01, 6, \" donation \" , \" seans outpost \" " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
string strAccount = AccountFromValue ( params [ 0 ] ) ;
CBitcoinAddress address ( params [ 1 ] . get_str ( ) ) ;
if ( ! address . IsValid ( ) )
2015-03-18 00:06:58 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid Dash address " ) ;
2013-04-13 07:13:08 +02:00
int64_t nAmount = AmountFromValue ( params [ 2 ] ) ;
2012-08-21 16:38:57 +02:00
int nMinDepth = 1 ;
if ( params . size ( ) > 3 )
nMinDepth = params [ 3 ] . get_int ( ) ;
CWalletTx wtx ;
wtx . strFromAccount = strAccount ;
if ( params . size ( ) > 4 & & params [ 4 ] . type ( ) ! = null_type & & ! params [ 4 ] . get_str ( ) . empty ( ) )
wtx . mapValue [ " comment " ] = params [ 4 ] . get_str ( ) ;
if ( params . size ( ) > 5 & & params [ 5 ] . type ( ) ! = null_type & & ! params [ 5 ] . get_str ( ) . empty ( ) )
wtx . mapValue [ " to " ] = params [ 5 ] . get_str ( ) ;
EnsureWalletIsUnlocked ( ) ;
// Check funds
2013-04-13 07:13:08 +02:00
int64_t nBalance = GetAccountBalance ( strAccount , nMinDepth ) ;
2012-08-21 16:38:57 +02:00
if ( nAmount > nBalance )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_INSUFFICIENT_FUNDS , " Account has insufficient funds " ) ;
2012-08-21 16:38:57 +02:00
// Send
string strError = pwalletMain - > SendMoneyToDestination ( address . Get ( ) , nAmount , wtx ) ;
if ( strError ! = " " )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , strError ) ;
2012-08-21 16:38:57 +02:00
return wtx . GetHash ( ) . GetHex ( ) ;
}
Value sendmany ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) < 2 | | params . size ( ) > 4 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" sendmany \" fromaccount \" { \" address \" :amount,...} ( minconf \" comment \" ) \n "
" \n Send multiple times. Amounts are double-precision floating point numbers. "
+ HelpRequiringPassphrase ( ) + " \n "
" \n Arguments: \n "
" 1. \" fromaccount \" (string, required) The account to send the funds from, can be \" \" for the default account \n "
" 2. \" amounts \" (string, required) A json object with addresses and amounts \n "
" { \n "
2015-03-19 15:15:08 +01:00
" \" address \" :amount (numeric) The dash address is the key, the numeric amount in btc is the value \n "
2013-10-29 12:29:44 +01:00
" ,... \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 "
" \n Result: \n "
" \" transactionid \" (string) The transaction id for the send. Only 1 transaction is created regardless of \n "
2014-05-29 18:17:34 +02:00
" the number of addresses. \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
" \n Send two amounts to two different addresses: \n "
2014-12-12 12:44:25 +01:00
+ HelpExampleCli ( " sendmany " , " \" tabby \" \" { \\ \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \\ \" :0.01, \\ \" XuQQkwA4FYkq2XERzMY2CiAZhJTEDAbtcg \\ \" :0.02} \" " ) +
2013-10-29 12:29:44 +01:00
" \n Send two amounts to two different addresses setting the confirmation and comment: \n "
2014-12-12 12:44:25 +01:00
+ HelpExampleCli ( " sendmany " , " \" tabby \" \" { \\ \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \\ \" :0.01, \\ \" XuQQkwA4FYkq2XERzMY2CiAZhJTEDAbtcg \\ \" :0.02} \" 6 \" testing \" " ) +
2013-10-29 12:29:44 +01:00
" \n As a json rpc call \n "
2014-12-12 12:44:25 +01:00
+ HelpExampleRpc ( " sendmany " , " \" tabby \" , \" { \\ \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \\ \" :0.01, \\ \" XuQQkwA4FYkq2XERzMY2CiAZhJTEDAbtcg \\ \" :0.02} \" , 6, \" testing \" " )
2013-10-29 12:29:44 +01:00
) ;
2012-08-21 16:38:57 +02:00
string strAccount = AccountFromValue ( params [ 0 ] ) ;
Object sendTo = params [ 1 ] . get_obj ( ) ;
int nMinDepth = 1 ;
if ( params . size ( ) > 2 )
nMinDepth = params [ 2 ] . get_int ( ) ;
CWalletTx wtx ;
wtx . strFromAccount = strAccount ;
if ( params . size ( ) > 3 & & params [ 3 ] . type ( ) ! = null_type & & ! params [ 3 ] . get_str ( ) . empty ( ) )
wtx . mapValue [ " comment " ] = params [ 3 ] . get_str ( ) ;
set < CBitcoinAddress > setAddress ;
2013-04-13 07:13:08 +02:00
vector < pair < CScript , int64_t > > vecSend ;
2012-08-21 16:38:57 +02:00
2013-04-13 07:13:08 +02:00
int64_t totalAmount = 0 ;
2012-08-21 16:38:57 +02:00
BOOST_FOREACH ( const Pair & s , sendTo )
{
CBitcoinAddress address ( s . name_ ) ;
if ( ! address . IsValid ( ) )
2015-03-18 00:06:58 +01:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , string ( " Invalid Dash address: " ) + s . name_ ) ;
2012-08-21 16:38:57 +02:00
if ( setAddress . count ( address ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , string ( " Invalid parameter, duplicated address: " ) + s . name_ ) ;
2012-08-21 16:38:57 +02:00
setAddress . insert ( address ) ;
CScript scriptPubKey ;
scriptPubKey . SetDestination ( address . Get ( ) ) ;
2013-04-13 07:13:08 +02:00
int64_t nAmount = AmountFromValue ( s . value_ ) ;
2012-08-21 16:38:57 +02:00
totalAmount + = nAmount ;
vecSend . push_back ( make_pair ( scriptPubKey , nAmount ) ) ;
}
EnsureWalletIsUnlocked ( ) ;
// Check funds
2013-04-13 07:13:08 +02:00
int64_t nBalance = GetAccountBalance ( strAccount , nMinDepth ) ;
2012-08-21 16:38:57 +02:00
if ( totalAmount > nBalance )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_INSUFFICIENT_FUNDS , " Account has insufficient funds " ) ;
2012-08-21 16:38:57 +02:00
// Send
CReserveKey keyChange ( pwalletMain ) ;
2013-04-13 07:13:08 +02:00
int64_t nFeeRequired = 0 ;
2013-04-25 23:31:22 +02:00
string strFailReason ;
bool fCreated = pwalletMain - > CreateTransaction ( vecSend , wtx , keyChange , nFeeRequired , strFailReason ) ;
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 ) ;
2012-08-21 16:38:57 +02:00
if ( ! pwalletMain - > CommitTransaction ( wtx , keyChange ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , " Transaction commit failed " ) ;
2012-08-21 16:38:57 +02:00
return wtx . GetHash ( ) . GetHex ( ) ;
}
2013-12-13 16:21:38 +01:00
// Defined in rpcmisc.cpp
2014-03-11 03:43:15 +01:00
extern CScript _createmultisig_redeemScript ( const Array & params ) ;
2012-09-10 15:32:40 +02:00
Value addmultisigaddress ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) < 2 | | params . size ( ) > 3 )
{
2013-10-29 12:29:44 +01:00
string msg = " addmultisigaddress nrequired [ \" key \" ,...] ( \" account \" ) \n "
" \n Add a nrequired-to-sign multisignature address to the wallet. \n "
2015-03-18 00:06:58 +01:00
" Each key is a Dash address or hex-encoded public key. \n "
2013-10-29 12:29:44 +01:00
" If 'account' is specified, assign address to that account. \n "
" \n Arguments: \n "
" 1. nrequired (numeric, required) The number of required signatures out of the n keys or addresses. \n "
2015-03-19 15:15:08 +01:00
" 2. \" keysobject \" (string, required) A json array of dash addresses or hex-encoded public keys \n "
2013-10-29 12:29:44 +01:00
" [ \n "
2015-03-19 15:15:08 +01:00
" \" address \" (string) dash address or hex-encoded public key \n "
2013-10-29 12:29:44 +01:00
" ..., \n "
" ] \n "
" 3. \" account \" (string, optional) An account to assign the addresses to. \n "
" \n Result: \n "
2015-03-19 15:15:08 +01:00
" \" dashaddress \" (string) A dash address associated with the keys. \n "
2013-10-29 12:29:44 +01:00
" \n Examples: \n "
" \n Add a multisig address from 2 addresses \n "
2014-12-10 00:22:01 +01:00
+ HelpExampleCli ( " addmultisigaddress " , " 2 \" [ \\ \" Xt4qk9uKvQYAonVGSZNXqxeDmtjaEWgfrs \\ \" , \\ \" XoSoWQkpgLpppPoyyzbUFh1fq2RBvW6UK1 \\ \" ] \" " ) +
2013-10-29 12:29:44 +01:00
" \n As json rpc call \n "
2014-12-10 00:22:01 +01:00
+ HelpExampleRpc ( " addmultisigaddress " , " 2, \" [ \\ \" Xt4qk9uKvQYAonVGSZNXqxeDmtjaEWgfrs \\ \" , \\ \" XoSoWQkpgLpppPoyyzbUFh1fq2RBvW6UK1 \\ \" ] \" " )
2013-10-29 12:29:44 +01:00
;
2012-09-10 15:32:40 +02:00
throw runtime_error ( msg ) ;
}
string strAccount ;
if ( params . size ( ) > 2 )
strAccount = AccountFromValue ( params [ 2 ] ) ;
2012-08-21 16:38:57 +02:00
// Construct using pay-to-script-hash:
2014-03-11 03:43:15 +01:00
CScript inner = _createmultisig_redeemScript ( params ) ;
2012-08-21 16:38:57 +02:00
CScriptID innerID = inner . GetID ( ) ;
pwalletMain - > AddCScript ( inner ) ;
2013-07-22 08:50:39 +02:00
pwalletMain - > SetAddressBook ( innerID , strAccount , " send " ) ;
2012-08-21 16:38:57 +02:00
return CBitcoinAddress ( innerID ) . ToString ( ) ;
}
struct tallyitem
{
2013-04-13 07:13:08 +02:00
int64_t nAmount ;
2012-08-21 16:38:57 +02:00
int nConf ;
2012-12-13 15:59:10 +01:00
vector < uint256 > txids ;
2012-08-21 16:38:57 +02:00
tallyitem ( )
{
nAmount = 0 ;
nConf = std : : numeric_limits < int > : : max ( ) ;
}
} ;
Value ListReceived ( const Array & params , bool fByAccounts )
{
// Minimum confirmations
int nMinDepth = 1 ;
if ( params . size ( ) > 0 )
nMinDepth = params [ 0 ] . get_int ( ) ;
// Whether to include empty accounts
bool fIncludeEmpty = false ;
if ( params . size ( ) > 1 )
fIncludeEmpty = params [ 1 ] . get_bool ( ) ;
// Tally
map < CBitcoinAddress , tallyitem > mapTally ;
for ( map < uint256 , CWalletTx > : : iterator it = pwalletMain - > mapWallet . begin ( ) ; it ! = pwalletMain - > mapWallet . end ( ) ; + + it )
{
const CWalletTx & wtx = ( * it ) . second ;
2013-01-08 13:17:15 +01:00
if ( wtx . IsCoinBase ( ) | | ! IsFinalTx ( wtx ) )
2012-08-21 16:38:57 +02:00
continue ;
int nDepth = wtx . GetDepthInMainChain ( ) ;
if ( nDepth < nMinDepth )
continue ;
BOOST_FOREACH ( const CTxOut & txout , wtx . vout )
{
CTxDestination address ;
if ( ! ExtractDestination ( txout . scriptPubKey , address ) | | ! IsMine ( * pwalletMain , address ) )
continue ;
tallyitem & item = mapTally [ address ] ;
item . nAmount + = txout . nValue ;
item . nConf = min ( item . nConf , nDepth ) ;
2012-12-13 15:59:10 +01:00
item . txids . push_back ( wtx . GetHash ( ) ) ;
2012-08-21 16:38:57 +02:00
}
}
// Reply
Array ret ;
map < string , tallyitem > mapAccountTally ;
2013-07-15 07:20:50 +02:00
BOOST_FOREACH ( const PAIRTYPE ( CBitcoinAddress , CAddressBookData ) & item , pwalletMain - > mapAddressBook )
2012-08-21 16:38:57 +02:00
{
const CBitcoinAddress & address = item . first ;
2013-07-15 07:20:50 +02:00
const string & strAccount = item . second . name ;
2012-08-21 16:38:57 +02:00
map < CBitcoinAddress , tallyitem > : : iterator it = mapTally . find ( address ) ;
if ( it = = mapTally . end ( ) & & ! fIncludeEmpty )
continue ;
2013-04-13 07:13:08 +02:00
int64_t nAmount = 0 ;
2012-08-21 16:38:57 +02:00
int nConf = std : : numeric_limits < int > : : max ( ) ;
if ( it ! = mapTally . end ( ) )
{
nAmount = ( * it ) . second . nAmount ;
nConf = ( * it ) . second . nConf ;
}
if ( fByAccounts )
{
tallyitem & item = mapAccountTally [ strAccount ] ;
item . nAmount + = nAmount ;
item . nConf = min ( item . nConf , nConf ) ;
}
else
{
Object obj ;
obj . push_back ( Pair ( " address " , address . ToString ( ) ) ) ;
obj . push_back ( Pair ( " account " , strAccount ) ) ;
obj . push_back ( Pair ( " amount " , ValueFromAmount ( nAmount ) ) ) ;
obj . push_back ( Pair ( " confirmations " , ( nConf = = std : : numeric_limits < int > : : max ( ) ? 0 : nConf ) ) ) ;
2012-12-13 15:59:10 +01:00
Array transactions ;
2012-12-16 20:10:32 +01:00
if ( it ! = mapTally . end ( ) )
2012-12-13 15:59:10 +01:00
{
2012-12-16 20:10:32 +01:00
BOOST_FOREACH ( const uint256 & item , ( * it ) . second . txids )
{
transactions . push_back ( item . GetHex ( ) ) ;
}
2012-12-13 15:59:10 +01:00
}
obj . push_back ( Pair ( " txids " , transactions ) ) ;
2012-08-21 16:38:57 +02:00
ret . push_back ( obj ) ;
}
}
if ( fByAccounts )
{
for ( map < string , tallyitem > : : iterator it = mapAccountTally . begin ( ) ; it ! = mapAccountTally . end ( ) ; + + it )
{
2013-04-13 07:13:08 +02:00
int64_t nAmount = ( * it ) . second . nAmount ;
2012-08-21 16:38:57 +02:00
int nConf = ( * it ) . second . nConf ;
Object obj ;
obj . push_back ( Pair ( " account " , ( * it ) . first ) ) ;
obj . push_back ( Pair ( " amount " , ValueFromAmount ( nAmount ) ) ) ;
obj . push_back ( Pair ( " confirmations " , ( nConf = = std : : numeric_limits < int > : : max ( ) ? 0 : nConf ) ) ) ;
ret . push_back ( obj ) ;
}
}
return ret ;
}
Value listreceivedbyaddress ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) > 2 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" listreceivedbyaddress ( minconf includeempty ) \n "
" \n List balances by receiving address. \n "
" \n Arguments: \n "
" 1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included. \n "
" 2. includeempty (numeric, optional, dafault=false) Whether to include addresses that haven't received any payments. \n "
" \n Result: \n "
" [ \n "
" { \n "
" \" address \" : \" receivingaddress \" , (string) The receiving address \n "
" \" account \" : \" accountname \" , (string) The account of the receiving address. The default account is \" \" . \n "
" \" amount \" : x.xxx, (numeric) The total amount in btc received by the address \n "
" \" confirmations \" : n (numeric) The number of confirmations of the most recent transaction included \n "
" } \n "
" ,... \n "
" ] \n "
" \n Examples: \n "
+ HelpExampleCli ( " listreceivedbyaddress " , " " )
+ HelpExampleCli ( " listreceivedbyaddress " , " 6 true " )
+ HelpExampleRpc ( " listreceivedbyaddress " , " 6, true " )
) ;
2012-08-21 16:38:57 +02:00
return ListReceived ( params , false ) ;
}
Value listreceivedbyaccount ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) > 2 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" listreceivedbyaccount ( minconf includeempty ) \n "
" \n List balances by account. \n "
" \n Arguments: \n "
" 1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included. \n "
" 2. includeempty (boolean, optional, default=false) Whether to include accounts that haven't received any payments. \n "
" \n Result: \n "
" [ \n "
" { \n "
" \" account \" : \" accountname \" , (string) The account name of the receiving account \n "
" \" amount \" : x.xxx, (numeric) The total amount received by addresses with this account \n "
" \" confirmations \" : n (numeric) The number of confirmations of the most recent transaction included \n "
" } \n "
" ,... \n "
" ] \n "
" \n Examples: \n "
+ HelpExampleCli ( " listreceivedbyaccount " , " " )
+ HelpExampleCli ( " listreceivedbyaccount " , " 6 true " )
+ HelpExampleRpc ( " listreceivedbyaccount " , " 6, true " )
) ;
2012-08-21 16:38:57 +02:00
return ListReceived ( params , true ) ;
}
2012-09-22 05:22:34 +02:00
static void MaybePushAddress ( Object & entry , const CTxDestination & dest )
{
CBitcoinAddress addr ;
if ( addr . Set ( dest ) )
entry . push_back ( Pair ( " address " , addr . ToString ( ) ) ) ;
}
2012-08-21 16:38:57 +02:00
void ListTransactions ( const CWalletTx & wtx , const string & strAccount , int nMinDepth , bool fLong , Array & ret )
{
2013-04-13 07:13:08 +02:00
int64_t nFee ;
2012-08-21 16:38:57 +02:00
string strSentAccount ;
2013-04-13 07:13:08 +02:00
list < pair < CTxDestination , int64_t > > listReceived ;
list < pair < CTxDestination , int64_t > > listSent ;
2012-08-21 16:38:57 +02:00
2012-06-02 04:33:28 +02:00
wtx . GetAmounts ( listReceived , listSent , nFee , strSentAccount ) ;
2012-08-21 16:38:57 +02:00
bool fAllAccounts = ( strAccount = = string ( " * " ) ) ;
// Sent
if ( ( ! listSent . empty ( ) | | nFee ! = 0 ) & & ( fAllAccounts | | strAccount = = strSentAccount ) )
{
2013-04-13 07:13:08 +02:00
BOOST_FOREACH ( const PAIRTYPE ( CTxDestination , int64_t ) & s , listSent )
2012-08-21 16:38:57 +02:00
{
Object entry ;
entry . push_back ( Pair ( " account " , strSentAccount ) ) ;
2012-09-22 05:22:34 +02:00
MaybePushAddress ( entry , s . first ) ;
2015-02-08 15:10:44 +01:00
std : : map < std : : string , std : : string > : : const_iterator it = wtx . mapValue . find ( " DS " ) ;
entry . push_back ( Pair ( " category " , ( it ! = wtx . mapValue . end ( ) & & it - > second = = " 1 " ) ? " darksent " : " send " ) ) ;
2012-08-21 16:38:57 +02:00
entry . push_back ( Pair ( " amount " , ValueFromAmount ( - s . second ) ) ) ;
entry . push_back ( Pair ( " fee " , ValueFromAmount ( - nFee ) ) ) ;
if ( fLong )
WalletTxToJSON ( wtx , entry ) ;
ret . push_back ( entry ) ;
}
}
// Received
if ( listReceived . size ( ) > 0 & & wtx . GetDepthInMainChain ( ) > = nMinDepth )
{
2013-04-13 07:13:08 +02:00
BOOST_FOREACH ( const PAIRTYPE ( CTxDestination , int64_t ) & r , listReceived )
2012-08-21 16:38:57 +02:00
{
string account ;
if ( pwalletMain - > mapAddressBook . count ( r . first ) )
2013-07-15 07:20:50 +02:00
account = pwalletMain - > mapAddressBook [ r . first ] . name ;
2012-08-21 16:38:57 +02:00
if ( fAllAccounts | | ( account = = strAccount ) )
{
Object entry ;
entry . push_back ( Pair ( " account " , account ) ) ;
2012-09-22 05:22:34 +02:00
MaybePushAddress ( entry , r . first ) ;
2012-06-02 04:33:28 +02:00
if ( wtx . IsCoinBase ( ) )
{
if ( wtx . GetDepthInMainChain ( ) < 1 )
entry . push_back ( Pair ( " category " , " orphan " ) ) ;
else if ( wtx . GetBlocksToMaturity ( ) > 0 )
entry . push_back ( Pair ( " category " , " immature " ) ) ;
else
entry . push_back ( Pair ( " category " , " generate " ) ) ;
}
else
2014-02-12 19:43:07 +01:00
{
2014-02-21 16:31:49 +01:00
entry . push_back ( Pair ( " category " , " receive " ) ) ;
2014-02-12 19:43:07 +01:00
}
2012-08-21 16:38:57 +02:00
entry . push_back ( Pair ( " amount " , ValueFromAmount ( r . second ) ) ) ;
if ( fLong )
WalletTxToJSON ( wtx , entry ) ;
ret . push_back ( entry ) ;
}
}
}
}
void AcentryToJSON ( const CAccountingEntry & acentry , const string & strAccount , Array & ret )
{
bool fAllAccounts = ( strAccount = = string ( " * " ) ) ;
if ( fAllAccounts | | acentry . strAccount = = strAccount )
{
Object entry ;
entry . push_back ( Pair ( " account " , acentry . strAccount ) ) ;
entry . push_back ( Pair ( " category " , " move " ) ) ;
2014-05-05 20:08:13 +02:00
entry . push_back ( Pair ( " time " , acentry . nTime ) ) ;
2012-08-21 16:38:57 +02:00
entry . push_back ( Pair ( " amount " , ValueFromAmount ( acentry . nCreditDebit ) ) ) ;
entry . push_back ( Pair ( " otheraccount " , acentry . strOtherAccount ) ) ;
entry . push_back ( Pair ( " comment " , acentry . strComment ) ) ;
ret . push_back ( entry ) ;
}
}
Value listtransactions ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) > 3 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" listtransactions ( \" account \" count from ) \n "
" \n Returns up to 'count' most recent transactions skipping the first 'from' transactions for account 'account'. \n "
" \n Arguments: \n "
" 1. \" account \" (string, optional) The account name. If not included, it will list all transactions for all accounts. \n "
" If \" \" is set, it will list transactions for the default account. \n "
" 2. count (numeric, optional, default=10) The number of transactions to return \n "
" 3. from (numeric, optional, default=0) The number of transactions to skip \n "
" \n Result: \n "
" [ \n "
" { \n "
" \" account \" : \" accountname \" , (string) The account name associated with the transaction. \n "
" It will be \" \" for the default account. \n "
2015-03-19 15:15:08 +01:00
" \" address \" : \" dashaddress \" , (string) The dash address of the transaction. Not present for \n "
2013-10-29 12:29:44 +01:00
" move transactions (category = move). \n "
" \" category \" : \" send|receive|move \" , (string) The transaction category. 'move' is a local (off blockchain) \n "
" transaction between accounts, and not associated with an address, \n "
" transaction id or block. 'send' and 'receive' transactions are \n "
" associated with an address, transaction id and block details \n "
" \" amount \" : x.xxx, (numeric) The amount in btc. This is negative for the 'send' category, and for the \n "
" 'move' category for moves outbound. It is positive for the 'receive' category, \n "
" and for the 'move' category for inbound funds. \n "
" \" fee \" : x.xxx, (numeric) The amount of the fee in btc. This is negative and only available for the \n "
" 'send' category of transactions. \n "
" \" confirmations \" : n, (numeric) The number of confirmations for the transaction. Available for 'send' and \n "
" 'receive' category of transactions. \n "
" \" blockhash \" : \" hashvalue \" , (string) The block hash containing the transaction. Available for 'send' and 'receive' \n "
" category of transactions. \n "
" \" blockindex \" : n, (numeric) The block index containing the transaction. Available for 'send' and 'receive' \n "
" category of transactions. \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 (midnight Jan 1 1970 GMT). \n "
" \" timereceived \" : xxx, (numeric) The time received in seconds since epoch (midnight Jan 1 1970 GMT). Available \n "
" for 'send' and 'receive' category of transactions. \n "
" \" comment \" : \" ... \" , (string) If a comment is associated with the transaction. \n "
" \" otheraccount \" : \" accountname \" , (string) For the 'move' category of transactions, the account the funds came \n "
" from (for receiving funds, positive amounts), or went to (for sending funds, \n "
" negative amounts). \n "
" } \n "
" ] \n "
" \n Examples: \n "
" \n List the most recent 10 transactions in the systems \n "
+ HelpExampleCli ( " listtransactions " , " " ) +
" \n List the most recent 10 transactions for the tabby account \n "
+ HelpExampleCli ( " listtransactions " , " \" tabby \" " ) +
" \n List transactions 100 to 120 from the tabby account \n "
+ HelpExampleCli ( " listtransactions " , " \" tabby \" 20 100 " ) +
" \n As a json rpc call \n "
+ HelpExampleRpc ( " listtransactions " , " \" tabby \" , 20, 100 " )
) ;
2012-08-21 16:38:57 +02:00
string strAccount = " * " ;
if ( params . size ( ) > 0 )
strAccount = params [ 0 ] . get_str ( ) ;
int nCount = 10 ;
if ( params . size ( ) > 1 )
nCount = params [ 1 ] . get_int ( ) ;
int nFrom = 0 ;
if ( params . size ( ) > 2 )
nFrom = params [ 2 ] . get_int ( ) ;
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
Array ret ;
2012-09-02 00:07:47 +02:00
std : : list < CAccountingEntry > acentries ;
CWallet : : TxItems txOrdered = pwalletMain - > OrderedTxItems ( acentries , strAccount ) ;
2012-08-21 16:38:57 +02:00
// iterate backwards until we have nCount items to return:
2012-05-28 20:45:12 +02:00
for ( CWallet : : TxItems : : reverse_iterator it = txOrdered . rbegin ( ) ; it ! = txOrdered . rend ( ) ; + + it )
2012-08-21 16:38:57 +02:00
{
CWalletTx * const pwtx = ( * it ) . second . first ;
if ( pwtx ! = 0 )
ListTransactions ( * pwtx , strAccount , 0 , true , ret ) ;
CAccountingEntry * const pacentry = ( * it ) . second . second ;
if ( pacentry ! = 0 )
AcentryToJSON ( * pacentry , strAccount , ret ) ;
if ( ( int ) ret . size ( ) > = ( nCount + nFrom ) ) break ;
}
// ret is newest to oldest
if ( nFrom > ( int ) ret . size ( ) )
nFrom = ret . size ( ) ;
if ( ( nFrom + nCount ) > ( int ) ret . size ( ) )
nCount = ret . size ( ) - nFrom ;
Array : : iterator first = ret . begin ( ) ;
std : : advance ( first , nFrom ) ;
Array : : iterator last = ret . begin ( ) ;
std : : advance ( last , nFrom + nCount ) ;
if ( last ! = ret . end ( ) ) ret . erase ( last , ret . end ( ) ) ;
if ( first ! = ret . begin ( ) ) ret . erase ( ret . begin ( ) , first ) ;
std : : reverse ( ret . begin ( ) , ret . end ( ) ) ; // Return oldest to newest
return ret ;
}
Value listaccounts ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) > 1 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" listaccounts ( minconf ) \n "
" \n Returns Object that has account names as keys, account balances as values. \n "
" \n Arguments: \n "
" 1. minconf (numeric, optional, default=1) Only onclude transactions with at least this many confirmations \n "
" \n Result: \n "
" { (json object where keys are account names, and values are numeric balances \n "
" \" account \" : x.xxx, (numeric) The property name is the account name, and the value is the total balance for the account. \n "
" ... \n "
" } \n "
" \n Examples: \n "
" \n List account balances where there at least 1 confirmation \n "
+ HelpExampleCli ( " listaccounts " , " " ) +
" \n List account balances including zero confirmation transactions \n "
+ HelpExampleCli ( " listaccounts " , " 0 " ) +
" \n List account balances for 6 or more confirmations \n "
+ HelpExampleCli ( " listaccounts " , " 6 " ) +
" \n As json rpc call \n "
+ HelpExampleRpc ( " listaccounts " , " 6 " )
) ;
2012-08-21 16:38:57 +02:00
int nMinDepth = 1 ;
if ( params . size ( ) > 0 )
nMinDepth = params [ 0 ] . get_int ( ) ;
2013-04-13 07:13:08 +02:00
map < string , int64_t > mapAccountBalances ;
2013-07-15 07:20:50 +02:00
BOOST_FOREACH ( const PAIRTYPE ( CTxDestination , CAddressBookData ) & entry , pwalletMain - > mapAddressBook ) {
2012-08-21 16:38:57 +02:00
if ( IsMine ( * pwalletMain , entry . first ) ) // This address belongs to me
2013-07-15 07:20:50 +02:00
mapAccountBalances [ entry . second . name ] = 0 ;
2012-08-21 16:38:57 +02:00
}
for ( map < uint256 , CWalletTx > : : iterator it = pwalletMain - > mapWallet . begin ( ) ; it ! = pwalletMain - > mapWallet . end ( ) ; + + it )
{
const CWalletTx & wtx = ( * it ) . second ;
2013-04-13 07:13:08 +02:00
int64_t nFee ;
2012-08-21 16:38:57 +02:00
string strSentAccount ;
2013-04-13 07:13:08 +02:00
list < pair < CTxDestination , int64_t > > listReceived ;
list < pair < CTxDestination , int64_t > > listSent ;
2014-02-15 22:38:28 +01:00
int nDepth = wtx . GetDepthInMainChain ( ) ;
if ( wtx . GetBlocksToMaturity ( ) > 0 | | nDepth < 0 )
2014-02-14 02:12:51 +01:00
continue ;
2012-06-02 04:33:28 +02:00
wtx . GetAmounts ( listReceived , listSent , nFee , strSentAccount ) ;
2012-08-21 16:38:57 +02:00
mapAccountBalances [ strSentAccount ] - = nFee ;
2013-04-13 07:13:08 +02:00
BOOST_FOREACH ( const PAIRTYPE ( CTxDestination , int64_t ) & s , listSent )
2012-08-21 16:38:57 +02:00
mapAccountBalances [ strSentAccount ] - = s . second ;
2014-02-15 22:38:28 +01:00
if ( nDepth > = nMinDepth )
2012-08-21 16:38:57 +02:00
{
2013-04-13 07:13:08 +02:00
BOOST_FOREACH ( const PAIRTYPE ( CTxDestination , int64_t ) & r , listReceived )
2012-08-21 16:38:57 +02:00
if ( pwalletMain - > mapAddressBook . count ( r . first ) )
2013-07-15 07:20:50 +02:00
mapAccountBalances [ pwalletMain - > mapAddressBook [ r . first ] . name ] + = r . second ;
2012-08-21 16:38:57 +02:00
else
mapAccountBalances [ " " ] + = r . second ;
}
}
list < CAccountingEntry > acentries ;
CWalletDB ( pwalletMain - > strWalletFile ) . ListAccountCreditDebit ( " * " , acentries ) ;
BOOST_FOREACH ( const CAccountingEntry & entry , acentries )
mapAccountBalances [ entry . strAccount ] + = entry . nCreditDebit ;
Object ret ;
2013-04-13 07:13:08 +02:00
BOOST_FOREACH ( const PAIRTYPE ( string , int64_t ) & accountBalance , mapAccountBalances ) {
2012-08-21 16:38:57 +02:00
ret . push_back ( Pair ( accountBalance . first , ValueFromAmount ( accountBalance . second ) ) ) ;
}
return ret ;
}
Value listsinceblock ( const Array & params , bool fHelp )
{
if ( fHelp )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" listsinceblock ( \" blockhash \" target-confirmations ) \n "
" \n Get all transactions in blocks since block [blockhash], or all transactions if omitted \n "
" \n Arguments: \n "
" 1. \" blockhash \" (string, optional) The block hash to list transactions since \n "
" 2. target-confirmations: (numeric, optional) The confirmations required, must be 1 or more \n "
" \n Result: \n "
" { \n "
" \" transactions \" : [ \n "
" \" account \" : \" accountname \" , (string) The account name associated with the transaction. Will be \" \" for the default account. \n "
2015-03-19 15:15:08 +01:00
" \" address \" : \" dashaddress \" , (string) The dash 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 "
" \" amount \" : x.xxx, (numeric) The amount in btc. This is negative for the 'send' category, and for the 'move' category for moves \n "
" outbound. It is positive for the 'receive' category, and for the 'move' category for inbound funds. \n "
" \" fee \" : x.xxx, (numeric) The amount of the fee in btc. This is negative and only available for the 'send' category of transactions. \n "
" \" confirmations \" : n, (numeric) The number of confirmations for the transaction. Available for 'send' and 'receive' category of transactions. \n "
" \" blockhash \" : \" hashvalue \" , (string) The block hash containing the transaction. Available for 'send' and 'receive' category of transactions. \n "
" \" blockindex \" : n, (numeric) The block index containing the transaction. Available for 'send' and 'receive' category of transactions. \n "
" \" 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 "
" \" comment \" : \" ... \" , (string) If a comment is associated with the transaction. \n "
" \" to \" : \" ... \" , (string) If a comment to is associated with the transaction. \n "
" ], \n "
" \" lastblock \" : \" lastblockhash \" (string) The hash of the last block \n "
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " listsinceblock " , " " )
+ HelpExampleCli ( " listsinceblock " , " \" 000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad \" 6 " )
+ HelpExampleRpc ( " listsinceblock " , " \" 000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad \" , 6 " )
) ;
2012-08-21 16:38:57 +02:00
CBlockIndex * pindex = NULL ;
int target_confirms = 1 ;
if ( params . size ( ) > 0 )
{
uint256 blockId = 0 ;
blockId . SetHex ( params [ 0 ] . get_str ( ) ) ;
2013-10-12 15:18:08 +02:00
std : : map < uint256 , CBlockIndex * > : : iterator it = mapBlockIndex . find ( blockId ) ;
if ( it ! = mapBlockIndex . end ( ) )
pindex = it - > second ;
2012-08-21 16:38:57 +02:00
}
if ( params . size ( ) > 1 )
{
target_confirms = params [ 1 ] . get_int ( ) ;
if ( target_confirms < 1 )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid parameter " ) ;
2012-08-21 16:38:57 +02:00
}
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
Array transactions ;
for ( map < uint256 , CWalletTx > : : iterator it = pwalletMain - > mapWallet . begin ( ) ; it ! = pwalletMain - > mapWallet . end ( ) ; it + + )
{
CWalletTx tx = ( * it ) . second ;
if ( depth = = - 1 | | tx . GetDepthInMainChain ( ) < depth )
ListTransactions ( tx , " * " , 0 , true , transactions ) ;
}
2013-10-10 23:07:44 +02:00
CBlockIndex * pblockLast = chainActive [ chainActive . Height ( ) + 1 - target_confirms ] ;
uint256 lastblock = pblockLast ? pblockLast - > GetBlockHash ( ) : 0 ;
2012-08-21 16:38:57 +02:00
Object ret ;
ret . push_back ( Pair ( " transactions " , transactions ) ) ;
ret . push_back ( Pair ( " lastblock " , lastblock . GetHex ( ) ) ) ;
return ret ;
}
Value gettransaction ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 1 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" gettransaction \" txid \" \n "
" \n Get detailed information about in-wallet transaction <txid> \n "
" \n Arguments: \n "
" 1. \" txid \" (string, required) The transaction id \n "
" \n Result: \n "
" { \n "
" \" amount \" : x.xxx, (numeric) The transaction amount in btc \n "
" \" confirmations \" : n, (numeric) The number of confirmations \n "
" \" blockhash \" : \" hash \" , (string) The block hash \n "
" \" blockindex \" : xx, (numeric) The block index \n "
" \" 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 "
" \" details \" : [ \n "
" { \n "
" \" account \" : \" accountname \" , (string) The account name involved in the transaction, can be \" \" for the default account. \n "
2015-03-19 15:15:08 +01:00
" \" address \" : \" dashaddress \" , (string) The dash 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 "
" \" amount \" : x.xxx (numeric) The amount in btc \n "
" } \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 "
" \n bExamples \n "
+ HelpExampleCli ( " gettransaction " , " \" 1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d \" " )
+ HelpExampleRpc ( " gettransaction " , " \" 1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d \" " )
) ;
2012-08-21 16:38:57 +02:00
uint256 hash ;
hash . SetHex ( params [ 0 ] . get_str ( ) ) ;
Object entry ;
if ( ! pwalletMain - > mapWallet . count ( hash ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid or non-wallet transaction id " ) ;
2012-08-21 16:38:57 +02:00
const CWalletTx & wtx = pwalletMain - > mapWallet [ hash ] ;
2013-04-13 07:13:08 +02:00
int64_t nCredit = wtx . GetCredit ( ) ;
int64_t nDebit = wtx . GetDebit ( ) ;
int64_t nNet = nCredit - nDebit ;
2013-11-11 07:03:51 +01:00
int64_t nFee = ( wtx . IsFromMe ( ) ? wtx . GetValueOut ( ) - nDebit : 0 ) ;
2012-08-21 16:38:57 +02:00
entry . push_back ( Pair ( " amount " , ValueFromAmount ( nNet - nFee ) ) ) ;
if ( wtx . IsFromMe ( ) )
entry . push_back ( Pair ( " fee " , ValueFromAmount ( nFee ) ) ) ;
WalletTxToJSON ( wtx , entry ) ;
Array details ;
ListTransactions ( wtx , " * " , 0 , false , details ) ;
entry . push_back ( Pair ( " details " , details ) ) ;
2014-02-13 17:28:32 +01:00
CDataStream ssTx ( SER_NETWORK , PROTOCOL_VERSION ) ;
2014-02-17 08:53:16 +01:00
ssTx < < static_cast < CTransaction > ( wtx ) ;
2014-02-13 17:28:32 +01:00
string strHex = HexStr ( ssTx . begin ( ) , ssTx . end ( ) ) ;
entry . push_back ( Pair ( " hex " , strHex ) ) ;
2012-08-21 16:38:57 +02:00
return entry ;
}
Value backupwallet ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 1 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" backupwallet \" destination \" \n "
" \n Safely copies wallet.dat to destination, which can be a directory or a path with filename. \n "
" \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
string strDest = params [ 0 ] . get_str ( ) ;
2012-11-27 16:27:54 +01:00
if ( ! BackupWallet ( * pwalletMain , strDest ) )
throw JSONRPCError ( RPC_WALLET_ERROR , " Error: Wallet backup failed! " ) ;
2012-08-21 16:38:57 +02:00
return Value : : null ;
}
Value keypoolrefill ( const Array & params , bool fHelp )
{
2013-06-25 22:21:42 +02:00
if ( fHelp | | params . size ( ) > 1 )
2012-08-21 16:38:57 +02:00
throw runtime_error (
2013-10-29 12:29:44 +01:00
" keypoolrefill ( newsize ) \n "
" \n Fills the keypool. "
+ HelpRequiringPassphrase ( ) + " \n "
" \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
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 ;
2013-06-25 22:21:42 +02:00
if ( params . size ( ) > 0 ) {
if ( params [ 0 ] . get_int ( ) < 0 )
2013-10-28 11:58:39 +01:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid parameter, expected valid size. " ) ;
kpSize = ( unsigned int ) params [ 0 ] . get_int ( ) ;
2013-06-25 22:21:42 +02:00
}
2012-08-21 16:38:57 +02:00
EnsureWalletIsUnlocked ( ) ;
2013-06-25 22:21:42 +02:00
pwalletMain - > TopUpKeyPool ( kpSize ) ;
2012-08-21 16:38:57 +02:00
2013-06-25 22:21:42 +02:00
if ( pwalletMain - > GetKeyPoolSize ( ) < kpSize )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_ERROR , " Error refreshing keypool. " ) ;
2012-08-21 16:38:57 +02:00
return Value : : null ;
}
2013-05-07 16:47:00 +02:00
static void LockWallet ( CWallet * pWallet )
2012-08-21 16:38:57 +02:00
{
2013-05-07 16:47:00 +02:00
LOCK ( cs_nWalletUnlockTime ) ;
nWalletUnlockTime = 0 ;
pWallet - > Lock ( ) ;
2012-08-21 16:38:57 +02:00
}
Value walletpassphrase ( const Array & params , bool fHelp )
{
2014-12-09 02:17:57 +01:00
if ( pwalletMain - > IsCrypted ( ) & & ( fHelp | | params . size ( ) < 2 | | params . size ( ) > 3 ) )
2012-08-21 16:38:57 +02:00
throw runtime_error (
2015-03-05 18:42:50 +01:00
" walletpassphrase \" passphrase \" timeout ( anonymizeonly ) \n "
2013-10-29 12:29:44 +01:00
" \n Stores the wallet decryption key in memory for 'timeout' seconds. \n "
2015-03-19 15:15:08 +01:00
" This is needed prior to performing transactions related to private keys such as sending dashs \n "
2013-10-29 12:29:44 +01:00
" \n Arguments: \n "
" 1. \" passphrase \" (string, required) The wallet passphrase \n "
" 2. timeout (numeric, required) The time to keep the decryption key in seconds. \n "
2014-12-11 19:43:21 +01:00
" 3. anonymizeonly (boolean, optional, default=flase) If is true sending functions are disabled. "
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 "
2014-12-11 22:46:43 +01:00
" \n Unlock the wallet for 60 seconds \n "
2013-10-29 12:29:44 +01:00
+ HelpExampleCli ( " walletpassphrase " , " \" my pass phrase \" 60 " ) +
2014-12-11 22:46:43 +01:00
" \n Unlock the wallet for 60 seconds but allow Darksend mixing only \n "
2014-12-11 19:43:21 +01:00
+ HelpExampleCli ( " walletpassphrase " , " \" my pass phrase \" 60 true " ) +
2013-10-29 12:29:44 +01:00
" \n Lock the wallet again (before 60 seconds) \n "
+ HelpExampleCli ( " walletlock " , " " ) +
" \n As json rpc call \n "
+ HelpExampleRpc ( " walletpassphrase " , " \" my pass phrase \" , 60 " )
) ;
2012-08-21 16:38:57 +02:00
if ( fHelp )
return true ;
if ( ! pwalletMain - > 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. " ) ;
2012-08-21 16:38:57 +02:00
// Note that the walletpassphrase is stored in params[0] which is not mlock()ed
SecureString strWalletPass ;
strWalletPass . reserve ( 100 ) ;
// TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
// Alternately, find a way to make params[0] mlock()'d to begin with.
strWalletPass = params [ 0 ] . get_str ( ) . c_str ( ) ;
2014-12-11 19:43:21 +01:00
bool anonymizeOnly = false ;
if ( params . size ( ) = = 3 )
anonymizeOnly = params [ 2 ] . get_bool ( ) ;
2014-12-09 02:17:57 +01:00
2014-12-11 19:43:21 +01:00
if ( ! pwalletMain - > IsLocked ( ) & & pwalletMain - > fWalletUnlockAnonymizeOnly & & anonymizeOnly )
throw JSONRPCError ( RPC_WALLET_ALREADY_UNLOCKED , " Error: Wallet is already unlocked. " ) ;
2014-12-09 02:17:57 +01:00
2014-12-11 19:43:21 +01:00
if ( ! pwalletMain - > Unlock ( strWalletPass , anonymizeOnly ) )
throw JSONRPCError ( RPC_WALLET_PASSPHRASE_INCORRECT , " Error: The wallet passphrase entered was incorrect. " ) ;
2012-08-21 16:38:57 +02:00
2013-05-07 16:47:00 +02:00
pwalletMain - > TopUpKeyPool ( ) ;
2013-04-13 07:13:08 +02:00
int64_t nSleepTime = params [ 1 ] . get_int64 ( ) ;
2013-05-07 16:47:00 +02:00
LOCK ( cs_nWalletUnlockTime ) ;
nWalletUnlockTime = GetTime ( ) + nSleepTime ;
RPCRunLater ( " lockwallet " , boost : : bind ( LockWallet , pwalletMain ) , nSleepTime ) ;
2012-08-21 16:38:57 +02:00
return Value : : null ;
}
Value walletpassphrasechange ( const Array & params , bool fHelp )
{
if ( pwalletMain - > IsCrypted ( ) & & ( fHelp | | params . size ( ) ! = 2 ) )
throw 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 \" " )
) ;
2012-08-21 16:38:57 +02:00
if ( fHelp )
return true ;
if ( ! pwalletMain - > 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. " ) ;
2012-08-21 16:38:57 +02:00
// TODO: get rid of these .c_str() calls by implementing SecureString::operator=(std::string)
// Alternately, find a way to make params[0] mlock()'d to begin with.
SecureString strOldWalletPass ;
strOldWalletPass . reserve ( 100 ) ;
strOldWalletPass = params [ 0 ] . get_str ( ) . c_str ( ) ;
SecureString strNewWalletPass ;
strNewWalletPass . reserve ( 100 ) ;
strNewWalletPass = params [ 1 ] . get_str ( ) . c_str ( ) ;
if ( strOldWalletPass . length ( ) < 1 | | strNewWalletPass . length ( ) < 1 )
throw runtime_error (
" walletpassphrasechange <oldpassphrase> <newpassphrase> \n "
" Changes the wallet passphrase from <oldpassphrase> to <newpassphrase>. " ) ;
if ( ! pwalletMain - > ChangeWalletPassphrase ( strOldWalletPass , strNewWalletPass ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_PASSPHRASE_INCORRECT , " Error: The wallet passphrase entered was incorrect. " ) ;
2012-08-21 16:38:57 +02:00
return Value : : null ;
}
Value walletlock ( const Array & params , bool fHelp )
{
if ( pwalletMain - > IsCrypted ( ) & & ( fHelp | | params . size ( ) ! = 0 ) )
throw runtime_error (
" 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 "
2014-12-12 12:44:25 +01:00
+ HelpExampleCli ( " sendtoaddress " , " \" XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwg \" 1.0 " ) +
2013-10-29 12:29:44 +01:00
" \n Clear the passphrase since we are done before 2 minutes is up \n "
+ HelpExampleCli ( " walletlock " , " " ) +
" \n As json rpc call \n "
+ HelpExampleRpc ( " walletlock " , " " )
) ;
2012-08-21 16:38:57 +02:00
if ( fHelp )
return true ;
if ( ! pwalletMain - > 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
{
LOCK ( cs_nWalletUnlockTime ) ;
pwalletMain - > Lock ( ) ;
nWalletUnlockTime = 0 ;
}
return Value : : null ;
}
Value encryptwallet ( const Array & params , bool fHelp )
{
if ( ! pwalletMain - > IsCrypted ( ) & & ( fHelp | | params . size ( ) ! = 1 ) )
throw 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 "
" Note that this will shutdown the server. \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 "
" \n Encrypt you wallet \n "
+ HelpExampleCli ( " encryptwallet " , " \" my pass phrase \" " ) +
2015-03-19 15:15:08 +01:00
" \n Now set the passphrase to use the wallet, such as for signing or sending dash \n "
2013-10-29 12:29:44 +01:00
+ HelpExampleCli ( " walletpassphrase " , " \" my pass phrase \" " ) +
" \n Now we can so something like sign \n "
2015-03-19 15:15:08 +01:00
+ HelpExampleCli ( " signmessage " , " \" dashaddress \" \" test message \" " ) +
2013-10-29 12:29:44 +01:00
" \n Now lock the wallet again by removing the passphrase \n "
+ HelpExampleCli ( " walletlock " , " " ) +
" \n As a json rpc call \n "
+ HelpExampleRpc ( " encryptwallet " , " \" my pass phrase \" " )
) ;
2012-08-21 16:38:57 +02:00
if ( fHelp )
return true ;
if ( pwalletMain - > 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. " ) ;
2012-08-21 16:38:57 +02:00
// TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
// Alternately, find a way to make params[0] mlock()'d to begin with.
SecureString strWalletPass ;
strWalletPass . reserve ( 100 ) ;
strWalletPass = params [ 0 ] . get_str ( ) . c_str ( ) ;
if ( strWalletPass . length ( ) < 1 )
throw runtime_error (
" encryptwallet <passphrase> \n "
" Encrypts the wallet with <passphrase>. " ) ;
if ( ! pwalletMain - > EncryptWallet ( strWalletPass ) )
2012-10-04 09:34:44 +02:00
throw JSONRPCError ( RPC_WALLET_ENCRYPTION_FAILED , " Error: Failed to encrypt the wallet. " ) ;
2012-08-21 16:38:57 +02:00
// BDB seems to have a bad habit of writing old data into
// slack space in .dat files; that is bad if the old data is
2012-08-23 14:19:13 +02:00
// unencrypted private keys. So:
2012-08-21 16:38:57 +02:00
StartShutdown ( ) ;
2015-03-19 15:15:08 +01:00
return " wallet encrypted; dash server stopping, restart to run with encrypted wallet. The keypool has been flushed, you need to make a new backup. " ;
2012-08-21 16:38:57 +02:00
}
2012-09-27 19:52:09 +02:00
Value lockunspent ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) < 1 | | params . size ( ) > 2 )
throw runtime_error (
2013-10-29 12:29:44 +01:00
" lockunspent unlock [{ \" txid \" : \" txid \" , \" vout \" :n},...] \n "
" \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 "
2015-03-19 15:15:08 +01:00
" A locked transaction output will not be chosen by automatic coin selection, when spending dashs. \n "
2013-10-29 12:29:44 +01:00
" 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 "
" 2. \" transactions \" (string, required) A json array of objects. Each object the txid (string) vout (numeric) \n "
" [ (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}] \" " ) +
" \n As a json rpc call \n "
+ HelpExampleRpc ( " lockunspent " , " false, \" [{ \\ \" txid \\ \" : \\ \" a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0 \\ \" , \\ \" vout \\ \" :1}] \" " )
) ;
2012-09-27 19:52:09 +02:00
if ( params . size ( ) = = 1 )
RPCTypeCheck ( params , list_of ( bool_type ) ) ;
else
RPCTypeCheck ( params , list_of ( bool_type ) ( array_type ) ) ;
bool fUnlock = params [ 0 ] . get_bool ( ) ;
if ( params . size ( ) = = 1 ) {
if ( fUnlock )
pwalletMain - > UnlockAllCoins ( ) ;
return true ;
}
Array outputs = params [ 1 ] . get_array ( ) ;
BOOST_FOREACH ( Value & output , outputs )
{
if ( output . type ( ) ! = obj_type )
2013-08-06 13:52:38 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid parameter, expected object " ) ;
2012-09-27 19:52:09 +02:00
const Object & o = output . get_obj ( ) ;
RPCTypeCheck ( o , map_list_of ( " txid " , str_type ) ( " vout " , int_type ) ) ;
string txid = find_value ( o , " txid " ) . get_str ( ) ;
if ( ! IsHex ( txid ) )
2013-08-06 13:52:38 +02:00
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Invalid parameter, expected hex txid " ) ;
2012-09-27 19:52:09 +02:00
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 " ) ;
2012-09-27 19:52:09 +02:00
COutPoint outpt ( uint256 ( txid ) , nOutput ) ;
if ( fUnlock )
pwalletMain - > UnlockCoin ( outpt ) ;
else
pwalletMain - > LockCoin ( outpt ) ;
}
return true ;
}
Value listlockunspent ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) > 0 )
throw runtime_error (
" 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}] \" " ) +
" \n As a json rpc call \n "
+ HelpExampleRpc ( " listlockunspent " , " " )
) ;
2012-09-27 19:52:09 +02:00
vector < COutPoint > vOutpts ;
pwalletMain - > ListLockedCoins ( vOutpts ) ;
Array ret ;
BOOST_FOREACH ( COutPoint & outpt , vOutpts ) {
Object o ;
o . push_back ( Pair ( " txid " , outpt . hash . GetHex ( ) ) ) ;
o . push_back ( Pair ( " vout " , ( int ) outpt . n ) ) ;
ret . push_back ( o ) ;
}
return ret ;
}
2013-12-13 16:06:32 +01:00
Value settxfee ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) < 1 | | params . size ( ) > 1 )
throw runtime_error (
" settxfee amount \n "
2014-01-30 14:13:30 +01:00
" \n Set the transaction fee per kB. \n "
2013-12-13 16:06:32 +01:00
" \n Arguments: \n "
2014-01-30 14:13:30 +01:00
" 1. amount (numeric, required) The transaction fee in BTC/kB rounded to the nearest 0.00000001 \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 " )
) ;
// Amount
int64_t nAmount = 0 ;
if ( params [ 0 ] . get_real ( ) ! = 0.0 )
nAmount = AmountFromValue ( params [ 0 ] ) ; // rejects 0.0 amounts
nTransactionFee = nAmount ;
return true ;
}
2014-02-21 05:56:04 +01:00
Value getwalletinfo ( const Array & params , bool fHelp )
{
if ( fHelp | | params . size ( ) ! = 0 )
throw runtime_error (
" getwalletinfo \n "
" Returns an object containing various wallet state info. \n "
" \n Result: \n "
" { \n "
" \" walletversion \" : xxxxx, (numeric) the wallet version \n "
2015-03-19 15:15:08 +01:00
" \" balance \" : xxxxxxx, (numeric) the total dash balance of the wallet \n "
2014-02-21 05:56:04 +01:00
" \" txcount \" : xxxxxxx, (numeric) the total number of transactions in the wallet \n "
" \" keypoololdest \" : xxxxxx, (numeric) the timestamp (seconds since GMT epoch) of the oldest pre-generated key in the key pool \n "
" \" keypoolsize \" : xxxx, (numeric) how many new keys are pre-generated \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 "
" } \n "
" \n Examples: \n "
+ HelpExampleCli ( " getwalletinfo " , " " )
+ HelpExampleRpc ( " getwalletinfo " , " " )
) ;
Object obj ;
obj . push_back ( Pair ( " walletversion " , pwalletMain - > GetVersion ( ) ) ) ;
obj . push_back ( Pair ( " balance " , ValueFromAmount ( pwalletMain - > GetBalance ( ) ) ) ) ;
obj . push_back ( Pair ( " txcount " , ( int ) pwalletMain - > mapWallet . size ( ) ) ) ;
2014-05-05 20:08:13 +02:00
obj . push_back ( Pair ( " keypoololdest " , pwalletMain - > GetOldestKeyPoolTime ( ) ) ) ;
2014-02-21 05:56:04 +01:00
obj . push_back ( Pair ( " keypoolsize " , ( int ) pwalletMain - > GetKeyPoolSize ( ) ) ) ;
if ( pwalletMain - > IsCrypted ( ) )
2014-05-05 20:08:13 +02:00
obj . push_back ( Pair ( " unlocked_until " , nWalletUnlockTime ) ) ;
2014-02-21 05:56:04 +01:00
return obj ;
}
2014-12-26 12:53:29 +01:00
Value keepass ( const Array & params , bool fHelp ) {
string strCommand ;
if ( params . size ( ) > = 1 )
strCommand = params [ 0 ] . get_str ( ) ;
if ( fHelp | |
( strCommand ! = " genkey " & & strCommand ! = " init " & & strCommand ! = " setpassphrase " ) )
throw runtime_error (
" keepass <genkey|init|setpassphrase> \n " ) ;
if ( strCommand = = " genkey " )
{
SecureString result ;
// Generate RSA key
//std::string keePassKey = CKeePassIntegrator::generateKey();
//return keePassKey;
SecureString sKey = CKeePassIntegrator : : generateKeePassKey ( ) ;
result = " Generated Key: " ;
result + = sKey ;
return result . c_str ( ) ;
}
else if ( strCommand = = " init " )
{
// Generate base64 encoded 256 bit RSA key and associate with KeePassHttp
SecureString result ;
SecureString sKey ;
std : : string sId ;
std : : string sErrorMessage ;
keePassInt . rpcAssociate ( sId , sKey ) ;
result = " Association successful. Id: " ;
result + = sId . c_str ( ) ;
result + = " - Key: " ;
result + = sKey . c_str ( ) ;
return result . c_str ( ) ;
}
else if ( strCommand = = " setpassphrase " )
{
if ( params . size ( ) ! = 2 ) {
return " setlogin: invalid number of parameters. Requires a passphrase " ;
}
SecureString sPassphrase = SecureString ( params [ 1 ] . get_str ( ) . c_str ( ) ) ;
keePassInt . updatePassphrase ( sPassphrase ) ;
return " setlogin: Updated credentials. " ;
}
return " Invalid command " ;
}