@@ -2960,9 +2960,10 @@ UniValue z_listaddresses(const UniValue& params, bool fHelp)
if ( fHelp | | params . size ( ) > 1 )
throw runtime_error (
" z_listaddresses \n "
" z_listaddresses ( includeWatchonly ) \n "
" \n Returns the list of zaddr belonging to the wallet. \n "
" \n Arguments: \n "
" 1. includeWatchonly (bool, optional, default=false) Also include watchonly addresses (see 'z_importviewingkey') \n "
" \n Result: \n "
" [ (json array of string) \n "
" \" zaddr \" (string) a zaddr belonging to the wallet \n "
@@ -2975,16 +2976,23 @@ UniValue z_listaddresses(const UniValue& params, bool fHelp)
LOCK2 ( cs_main , pwalletMain - > cs_wallet ) ;
bool fIncludeWatchonly = false ;
if ( params . size ( ) > 0 ) {
fIncludeWatchonly = params [ 0 ] . get_bool ( ) ;
}
UniValue ret ( UniValue : : VARR ) ;
std : : set < libzcash : : PaymentAddress > addresses ;
pwalletMain - > GetPaymentAddresses ( addresses ) ;
for ( auto addr : addresses ) {
ret . push_back ( CZCPaymentAddress ( addr ) . ToString ( ) ) ;
if ( fIncludeWatchonly | | pwalletMain - > HaveSpendingKey ( addr ) ) {
ret . push_back ( CZCPaymentAddress ( addr ) . ToString ( ) ) ;
}
}
return ret ;
}
CAmount getBalanceTaddr ( std : : string transparentAddress , int minDepth = 1 ) {
CAmount getBalanceTaddr ( std : : string transparentAddress , int minDepth = 1 , bool ignoreUnspendable = true ) {
set < CBitcoinAddress > setAddress ;
vector < COutput > vecOutputs ;
CAmount balance = 0 ;
@@ -3006,6 +3014,10 @@ CAmount getBalanceTaddr(std::string transparentAddress, int minDepth=1) {
continue ;
}
if ( ignoreUnspendable & & ! out . fSpendable ) {
continue ;
}
if ( setAddress . size ( ) ) {
CTxDestination address ;
if ( ! ExtractDestination ( out . tx - > vout [ out . i ] . scriptPubKey , address ) ) {
@@ -3023,11 +3035,11 @@ CAmount getBalanceTaddr(std::string transparentAddress, int minDepth=1) {
return balance ;
}
CAmount getBalanceZaddr ( std : : string address , int minDepth = 1 ) {
CAmount getBalanceZaddr ( std : : string address , int minDepth = 1 , bool ignoreUnspendable = true ) {
CAmount balance = 0 ;
std : : vector < CNotePlaintextEntry > entries ;
LOCK2 ( cs_main , pwalletMain - > cs_wallet ) ;
pwalletMain - > GetFilteredNotes ( entries , address , minDepth ) ;
pwalletMain - > GetFilteredNotes ( entries , address , minDepth , true , ignoreUnspendable );
for ( auto & entry : entries ) {
balance + = CAmount ( entry . plaintext . value ) ;
}
@@ -3076,14 +3088,14 @@ UniValue z_listreceivedbyaddress(const UniValue& params, bool fHelp)
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid zaddr. " ) ;
}
if ( ! pwalletMain - > HaveSpendingKey ( zaddr ) ) {
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " From address does not belong to this node, zaddr spending key not found. " ) ;
if ( ! ( pwalletMain - > HaveSpendingKey ( zaddr ) | | pwalletMain - > HaveViewingKey ( zaddr ) ) ) {
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " From address does not belong to this node, zaddr spending key or viewing key not found. " ) ;
}
UniValue result ( UniValue : : VARR ) ;
std : : vector < CNotePlaintextEntry > entries ;
pwalletMain - > GetFilteredNotes ( entries , fromaddress , nMinDepth , false ) ;
pwalletMain - > GetFilteredNotes ( entries , fromaddress , nMinDepth , false , false );
for ( CNotePlaintextEntry & entry : entries ) {
UniValue obj ( UniValue : : VOBJ ) ;
obj . push_back ( Pair ( " txid " , entry . jsop . hash . ToString ( ) ) ) ;
@@ -3142,16 +3154,16 @@ UniValue z_getbalance(const UniValue& params, bool fHelp)
} catch ( const std : : runtime_error & ) {
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " Invalid from address, should be a taddr or zaddr. " ) ;
}
if ( ! pwalletMain - > HaveSpendingKey ( zaddr ) ) {
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " From address does not belong to this node, zaddr spending key not found. " ) ;
if ( ! ( pwalletMain - > HaveSpendingKey ( zaddr ) | | pwalletMain - > HaveViewingKey ( zaddr ) ) ) {
throw JSONRPCError ( RPC_INVALID_ADDRESS_OR_KEY , " From address does not belong to this node, zaddr spending key or viewing key not found. " ) ;
}
}
CAmount nBalance = 0 ;
if ( fromTaddr ) {
nBalance = getBalanceTaddr ( fromaddress , nMinDepth ) ;
nBalance = getBalanceTaddr ( fromaddress , nMinDepth , false );
} else {
nBalance = getBalanceZaddr ( fromaddress , nMinDepth ) ;
nBalance = getBalanceZaddr ( fromaddress , nMinDepth , false );
}
return ValueFromAmount ( nBalance ) ;
@@ -3163,12 +3175,13 @@ UniValue z_gettotalbalance(const UniValue& params, bool fHelp)
if ( ! EnsureWalletIsAvailable ( fHelp ) )
return NullUniValue ;
if ( fHelp | | params . size ( ) > 1 )
if ( fHelp | | params . size ( ) > 2 )
throw runtime_error (
" z_gettotalbalance ( minconf ) \n "
" z_gettotalbalance ( minconf includeWatchonly ) \n "
" \n Return the total value of funds stored in the node’ s wallet. \n "
" \n Arguments: \n "
" 1. minconf (numeric, optional, default=1) Only include private and transparent transactions confirmed at least this many times. \n "
" 2. includeWatchonly (bool, optional, default=false) Also include balance in watchonly addresses (see 'importaddress' and 'z_importviewingkey') \n "
" \n Result: \n "
" { \n "
" \" transparent \" : xxxxx, (numeric) the total balance of transparent funds \n "
@@ -3187,19 +3200,24 @@ UniValue z_gettotalbalance(const UniValue& params, bool fHelp)
LOCK2 ( cs_main , pwalletMain - > cs_wallet ) ;
int nMinDepth = 1 ;
if ( params . size ( ) = = 1 ) {
if ( params . size ( ) > 0 ) {
nMinDepth = params [ 0 ] . get_int ( ) ;
}
if ( nMinDepth < 0 ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " Minimum number of confirmations cannot be less than 0 " ) ;
}
bool fIncludeWatchonly = false ;
if ( params . size ( ) > 1 ) {
fIncludeWatchonly = params [ 1 ] . get_bool ( ) ;
}
// getbalance and "getbalance * 1 true" should return the same number
// but they don't because wtx.GetAmounts() does not handle tx where there are no outputs
// pwalletMain->GetBalance() does not accept min depth parameter
// so we use our own method to get balance of utxos.
CAmount nBalance = getBalanceTaddr ( " " , nMinDepth ) ;
CAmount nPrivateBalance = getBalanceZaddr ( " " , nMinDepth ) ;
CAmount nBalance = getBalanceTaddr ( " " , nMinDepth , ! fIncludeWatchonly );
CAmount nPrivateBalance = getBalanceZaddr ( " " , nMinDepth , ! fIncludeWatchonly );
CAmount nTotalBalance = nBalance + nPrivateBalance ;
UniValue result ( UniValue : : VOBJ ) ;
result . push_back ( Pair ( " transparent " , FormatMoney ( nBalance ) ) ) ;