From f3119076fb8c5dc4f1acd65b2f6681a6a61af92c Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Mon, 7 Mar 2016 07:04:09 +0300 Subject: [PATCH] COunt received coins for owned pubkey-pair addresses properly --- src/base58.h | 2 ++ src/rpcwallet.cpp | 18 +++++++++--------- src/script.cpp | 7 +++++++ src/script.h | 1 + 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/base58.h b/src/base58.h index d5df354..2b6e326 100644 --- a/src/base58.h +++ b/src/base58.h @@ -82,6 +82,8 @@ public: * The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key. * Script-hash-addresses have version 5 (or 196 testnet). * The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script. + * Pubkey-pair-addresses have version 1 (or 6 testnet) + * The data vector contains a serialized copy of two compressed ECDSA secp256k1 public keys. */ class CBitcoinAddress; class CBitcoinAddressVisitor : public boost::static_visitor diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index 7ad807e..5ef253f 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -463,16 +463,14 @@ Value getreceivedbyaddress(const Array& params, bool fHelp) { if (fHelp || params.size() < 1 || params.size() > 2) throw runtime_error( - "getreceivedbyaddress [minconf=1]\n" - "Returns the total amount received by in transactions with at least [minconf] confirmations."); + "getreceivedbyaddress [minconf=1]\n" + "Returns the total amount received by in transactions with at least [minconf] confirmations."); // Bitcoin address CBitcoinAddress address = CBitcoinAddress(params[0].get_str()); - CScript scriptPubKey; if (!address.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address"); - scriptPubKey.SetDestination(address.Get()); - if (!IsMine(*pwalletMain,scriptPubKey)) + if (!IsMine(*pwalletMain,address)) return 0.0; // Minimum confirmations @@ -480,24 +478,26 @@ Value getreceivedbyaddress(const Array& params, bool fHelp) if (params.size() > 1) nMinDepth = params[1].get_int(); - // Tally int64_t nAmount = 0; for (map::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) { const CWalletTx& wtx = (*it).second; if (wtx.IsCoinBase() || wtx.IsCoinStake() || !wtx.IsFinal()) continue; - BOOST_FOREACH(const CTxOut& txout, wtx.vout) - if (txout.scriptPubKey == scriptPubKey) + { + CBitcoinAddress addressRet; + if (!pwalletMain->ExtractAddress(txout.scriptPubKey, addressRet)) + continue; + if (addressRet == address) if (wtx.GetDepthInMainChain() >= nMinDepth) nAmount += txout.nValue; + } } return ValueFromAmount(nAmount); } - void GetAccountAddresses(string strAccount, set& setAddress) { BOOST_FOREACH(const PAIRTYPE(CTxDestination, string)& item, pwalletMain->mapAddressBook) diff --git a/src/script.cpp b/src/script.cpp index ceacdc3..2212fe1 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -1647,6 +1647,13 @@ isminetype IsMine(const CKeyStore &keystore, const CTxDestination& dest) return IsMine(keystore, script); } +isminetype IsMine(const CKeyStore &keystore, const CBitcoinAddress& dest) +{ + CScript script; + script.SetAddress(dest); + return IsMine(keystore, script); +} + isminetype IsMine(const CKeyStore &keystore, const CScript& scriptPubKey) { vector vSolutions; diff --git a/src/script.h b/src/script.h index af523e6..7177339 100644 --- a/src/script.h +++ b/src/script.h @@ -638,6 +638,7 @@ int ScriptSigArgsExpected(txnouttype t, const std::vector &vKeys); bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet); bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector& addressRet, int& nRequiredRet); -- 1.7.1