changeset 1618:e16276182f0f draft

Fix broken ExtractAddress (refactored, made callers check for addresses in keystore if they care)
author Gavin Andresen <gavinandresen@gmail.com>
date Thu, 22 Dec 2011 15:51:44 -0500
parents a7ebd4cb36df
children 0ad7bd17b899 13f309535363
files src/bitcoinrpc.cpp src/qt/transactiondesc.cpp src/qt/transactionrecord.cpp src/script.cpp src/script.h src/test/multisig_tests.cpp src/wallet.cpp
diffstat 7 files changed, 20 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/bitcoinrpc.cpp
+++ b/src/bitcoinrpc.cpp
@@ -689,7 +689,7 @@
         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 @@
     for (map<uint256, CWalletTx>::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 @@
         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 @@
     string strSentAccount;
     list<pair<CBitcoinAddress, int64> > listReceived;
     list<pair<CBitcoinAddress, int64> > listSent;
+
     wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount);
 
     bool fAllAccounts = (strAccount == string("*"));
@@ -1682,7 +1684,7 @@
             std::vector<CBitcoinAddress> 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)
--- a/src/qt/transactiondesc.cpp
+++ b/src/qt/transactiondesc.cpp
@@ -99,7 +99,7 @@
                     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 @@
                     {
                         // Offline transaction
                         CBitcoinAddress address;
-                        if (ExtractAddress(txout.scriptPubKey, 0, address))
+                        if (ExtractAddress(txout.scriptPubKey, address))
                         {
                             strHTML += tr("<b>To:</b> ");
                             if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty())
@@ -271,7 +271,7 @@
                             strHTML += "<li>";
                             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]) + " ";
--- a/src/qt/transactionrecord.cpp
+++ b/src/qt/transactionrecord.cpp
@@ -80,7 +80,7 @@
                     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 @@
                         // 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();
                         }
--- a/src/script.cpp
+++ b/src/script.cpp
@@ -1442,7 +1442,7 @@
     return false;
 }
 
-bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBitcoinAddress& addressRet)
+bool ExtractAddress(const CScript& scriptPubKey, CBitcoinAddress& addressRet)
 {
     vector<valtype> vSolutions;
     txnouttype whichType;
@@ -1468,7 +1468,7 @@
     return false;
 }
 
-bool ExtractAddresses(const CScript& scriptPubKey, const CKeyStore* keystore, txnouttype& typeRet, vector<CBitcoinAddress>& addressRet, int& nRequiredRet)
+bool ExtractAddresses(const CScript& scriptPubKey, txnouttype& typeRet, vector<CBitcoinAddress>& addressRet, int& nRequiredRet)
 {
     addressRet.clear();
     typeRet = TX_NONSTANDARD;
--- a/src/script.h
+++ b/src/script.h
@@ -572,8 +572,8 @@
 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& 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<CBitcoinAddress>& addressRet, int& nRequiredRet);
+bool ExtractAddress(const CScript& scriptPubKey, CBitcoinAddress& addressRet);
+bool ExtractAddresses(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CBitcoinAddress>& 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);
 
--- a/src/test/multisig_tests.cpp
+++ b/src/test/multisig_tests.cpp
@@ -192,7 +192,7 @@
         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_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_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_CHECK_EQUAL(solutions.size(), 4);
         vector<CBitcoinAddress> 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);
--- a/src/wallet.cpp
+++ b/src/wallet.cpp
@@ -394,7 +394,7 @@
     // 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 @@
     {
         CBitcoinAddress address;
         vector<unsigned char> 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());