From 2e17ac83c65b65fe2037b8c8941c25e288905903 Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Thu, 22 Dec 2011 15:51:44 -0500 Subject: [PATCH] Fix broken ExtractAddress (refactored, made callers check for addresses in keystore if they care) --- src/bitcoinrpc.cpp | 8 +++++--- src/qt/transactiondesc.cpp | 6 +++--- src/qt/transactionrecord.cpp | 4 ++-- src/script.cpp | 4 ++-- src/script.h | 4 ++-- src/test/multisig_tests.cpp | 8 ++++---- src/wallet.cpp | 4 ++-- 7 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index bed90d4f..c1e8ef76 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -689,7 +689,7 @@ Value getreceivedbyaccount(const Array& params, bool fHelp) BOOST_FOREACH(const CTxOut& txout, wtx.vout) { CBitcoinAddress address; - if (ExtractAddress(txout.scriptPubKey, pwalletMain, address) && setAddress.count(address)) + if (ExtractAddress(txout.scriptPubKey, address) && pwalletMain->HaveKey(address) && setAddress.count(address)) if (wtx.GetDepthInMainChain() >= nMinDepth) nAmount += txout.nValue; } @@ -1033,6 +1033,7 @@ Value ListReceived(const Array& params, bool fByAccounts) for (map::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) { const CWalletTx& wtx = (*it).second; + if (wtx.IsCoinBase() || !wtx.IsFinal()) continue; @@ -1043,7 +1044,7 @@ Value ListReceived(const Array& params, bool fByAccounts) BOOST_FOREACH(const CTxOut& txout, wtx.vout) { CBitcoinAddress address; - if (!ExtractAddress(txout.scriptPubKey, pwalletMain, address) || !address.IsValid()) + if (!ExtractAddress(txout.scriptPubKey, address) || !pwalletMain->HaveKey(address) || !address.IsValid()) continue; tallyitem& item = mapTally[address]; @@ -1142,6 +1143,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe string strSentAccount; list > listReceived; list > listSent; + wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount); bool fAllAccounts = (strAccount == string("*")); @@ -1682,7 +1684,7 @@ Value validateaddress(const Array& params, bool fHelp) std::vector addresses; txnouttype whichType; int nRequired; - ExtractAddresses(subscript, pwalletMain, whichType, addresses, nRequired); + ExtractAddresses(subscript, whichType, addresses, nRequired); ret.push_back(Pair("script", GetTxnOutputType(whichType))); Array a; BOOST_FOREACH(const CBitcoinAddress& addr, addresses) diff --git a/src/qt/transactiondesc.cpp b/src/qt/transactiondesc.cpp index 6ca3ac8c..821ef981 100644 --- a/src/qt/transactiondesc.cpp +++ b/src/qt/transactiondesc.cpp @@ -99,7 +99,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) if (wallet->IsMine(txout)) { CBitcoinAddress address; - if (ExtractAddress(txout.scriptPubKey, wallet, address)) + if (ExtractAddress(txout.scriptPubKey, address) && wallet->HaveKey(address)) { if (wallet->mapAddressBook.count(address)) { @@ -184,7 +184,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) { // Offline transaction CBitcoinAddress address; - if (ExtractAddress(txout.scriptPubKey, 0, address)) + if (ExtractAddress(txout.scriptPubKey, address)) { strHTML += tr("To: "); if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty()) @@ -271,7 +271,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) strHTML += "
  • "; const CTxOut &vout = prev.vout[prevout.n]; CBitcoinAddress address; - if (ExtractAddress(vout.scriptPubKey, 0, address)) + if (ExtractAddress(vout.scriptPubKey, address)) { if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty()) strHTML += HtmlEscape(wallet->mapAddressBook[address]) + " "; diff --git a/src/qt/transactionrecord.cpp b/src/qt/transactionrecord.cpp index 53cd35b2..f3dbd45c 100644 --- a/src/qt/transactionrecord.cpp +++ b/src/qt/transactionrecord.cpp @@ -80,7 +80,7 @@ QList TransactionRecord::decomposeTransaction(const CWallet * if(wallet->IsMine(txout)) { CBitcoinAddress address; - if (ExtractAddress(txout.scriptPubKey, wallet, address)) + if (ExtractAddress(txout.scriptPubKey, address) && wallet->HaveKey(address)) { sub.address = address.ToString(); } @@ -138,7 +138,7 @@ QList TransactionRecord::decomposeTransaction(const CWallet * // Sent to Bitcoin Address sub.type = TransactionRecord::SendToAddress; CBitcoinAddress address; - if (ExtractAddress(txout.scriptPubKey, 0, address)) + if (ExtractAddress(txout.scriptPubKey, address)) { sub.address = address.ToString(); } diff --git a/src/script.cpp b/src/script.cpp index 467cb2f0..0a1b3bb3 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -1442,7 +1442,7 @@ bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey) return false; } -bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBitcoinAddress& addressRet) +bool ExtractAddress(const CScript& scriptPubKey, CBitcoinAddress& addressRet) { vector vSolutions; txnouttype whichType; @@ -1468,7 +1468,7 @@ bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBit return false; } -bool ExtractAddresses(const CScript& scriptPubKey, const CKeyStore* keystore, txnouttype& typeRet, vector& addressRet, int& nRequiredRet) +bool ExtractAddresses(const CScript& scriptPubKey, txnouttype& typeRet, vector& addressRet, int& nRequiredRet) { addressRet.clear(); typeRet = TX_NONSTANDARD; diff --git a/src/script.h b/src/script.h index b671e159..0080f5b7 100644 --- a/src/script.h +++ b/src/script.h @@ -572,8 +572,8 @@ bool EvalScript(std::vector >& stack, const CScript& bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector >& vSolutionsRet); bool IsStandard(const CScript& scriptPubKey); bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey); -bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* pkeystore, CBitcoinAddress& addressRet); -bool ExtractAddresses(const CScript& scriptPubKey, const CKeyStore* pkeystore, txnouttype& typeRet, std::vector& addressRet, int& nRequiredRet); +bool ExtractAddress(const CScript& scriptPubKey, CBitcoinAddress& addressRet); +bool ExtractAddresses(const CScript& scriptPubKey, txnouttype& typeRet, std::vector& addressRet, int& nRequiredRet); bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript()); bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int& nSigOpCountRet, int nHashType=0, bool fStrictOpEval=true); diff --git a/src/test/multisig_tests.cpp b/src/test/multisig_tests.cpp index 58f62b95..742083f3 100644 --- a/src/test/multisig_tests.cpp +++ b/src/test/multisig_tests.cpp @@ -192,7 +192,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK(solutions.size() == 1); CBitcoinAddress addr; - BOOST_CHECK(ExtractAddress(s, &keystore, addr)); + BOOST_CHECK(ExtractAddress(s, addr)); BOOST_CHECK(addr == keyaddr[0]); BOOST_CHECK(IsMine(keystore, s)); BOOST_CHECK(!IsMine(emptykeystore, s)); @@ -205,7 +205,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK(solutions.size() == 1); CBitcoinAddress addr; - BOOST_CHECK(ExtractAddress(s, &keystore, addr)); + BOOST_CHECK(ExtractAddress(s, addr)); BOOST_CHECK(addr == keyaddr[0]); BOOST_CHECK(IsMine(keystore, s)); BOOST_CHECK(!IsMine(emptykeystore, s)); @@ -218,7 +218,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK_EQUAL(solutions.size(), 4); CBitcoinAddress addr; - BOOST_CHECK(!ExtractAddress(s, &keystore, addr)); + BOOST_CHECK(!ExtractAddress(s, addr)); BOOST_CHECK(IsMine(keystore, s)); BOOST_CHECK(!IsMine(emptykeystore, s)); } @@ -231,7 +231,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1) BOOST_CHECK_EQUAL(solutions.size(), 4); vector addrs; int nRequired; - BOOST_CHECK(ExtractAddresses(s, &keystore, whichType, addrs, nRequired)); + BOOST_CHECK(ExtractAddresses(s, whichType, addrs, nRequired)); BOOST_CHECK(addrs[0] == keyaddr[0]); BOOST_CHECK(addrs[1] == keyaddr[1]); BOOST_CHECK(nRequired = 1); diff --git a/src/wallet.cpp b/src/wallet.cpp index c35081ce..28d15ef2 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -394,7 +394,7 @@ bool CWallet::IsChange(const CTxOut& txout) const // a better way of identifying which outputs are 'the send' and which are // 'the change' will need to be implemented (maybe extend CWalletTx to remember // which output, if any, was change). - if (ExtractAddress(txout.scriptPubKey, this, address)) + if (ExtractAddress(txout.scriptPubKey, address) && HaveKey(address)) CRITICAL_BLOCK(cs_wallet) if (!mapAddressBook.count(address)) return true; @@ -475,7 +475,7 @@ void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, l { CBitcoinAddress address; vector vchPubKey; - if (!ExtractAddress(txout.scriptPubKey, NULL, address)) + if (!ExtractAddress(txout.scriptPubKey, address)) { printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n", this->GetHash().ToString().c_str());