Fix broken ExtractAddress (refactored, made callers check for addresses in keystore if they care)

This commit is contained in:
Gavin Andresen 2011-12-22 15:51:44 -05:00
parent ce336fdc21
commit 2e17ac83c6
7 changed files with 20 additions and 18 deletions

View file

@ -689,7 +689,7 @@ Value getreceivedbyaccount(const Array& params, bool fHelp)
BOOST_FOREACH(const CTxOut& txout, wtx.vout) BOOST_FOREACH(const CTxOut& txout, wtx.vout)
{ {
CBitcoinAddress address; 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) if (wtx.GetDepthInMainChain() >= nMinDepth)
nAmount += txout.nValue; nAmount += txout.nValue;
} }
@ -1033,6 +1033,7 @@ Value ListReceived(const Array& params, bool fByAccounts)
for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
{ {
const CWalletTx& wtx = (*it).second; const CWalletTx& wtx = (*it).second;
if (wtx.IsCoinBase() || !wtx.IsFinal()) if (wtx.IsCoinBase() || !wtx.IsFinal())
continue; continue;
@ -1043,7 +1044,7 @@ Value ListReceived(const Array& params, bool fByAccounts)
BOOST_FOREACH(const CTxOut& txout, wtx.vout) BOOST_FOREACH(const CTxOut& txout, wtx.vout)
{ {
CBitcoinAddress address; CBitcoinAddress address;
if (!ExtractAddress(txout.scriptPubKey, pwalletMain, address) || !address.IsValid()) if (!ExtractAddress(txout.scriptPubKey, address) || !pwalletMain->HaveKey(address) || !address.IsValid())
continue; continue;
tallyitem& item = mapTally[address]; tallyitem& item = mapTally[address];
@ -1142,6 +1143,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe
string strSentAccount; string strSentAccount;
list<pair<CBitcoinAddress, int64> > listReceived; list<pair<CBitcoinAddress, int64> > listReceived;
list<pair<CBitcoinAddress, int64> > listSent; list<pair<CBitcoinAddress, int64> > listSent;
wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount); wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount);
bool fAllAccounts = (strAccount == string("*")); bool fAllAccounts = (strAccount == string("*"));
@ -1682,7 +1684,7 @@ Value validateaddress(const Array& params, bool fHelp)
std::vector<CBitcoinAddress> addresses; std::vector<CBitcoinAddress> addresses;
txnouttype whichType; txnouttype whichType;
int nRequired; int nRequired;
ExtractAddresses(subscript, pwalletMain, whichType, addresses, nRequired); ExtractAddresses(subscript, whichType, addresses, nRequired);
ret.push_back(Pair("script", GetTxnOutputType(whichType))); ret.push_back(Pair("script", GetTxnOutputType(whichType)));
Array a; Array a;
BOOST_FOREACH(const CBitcoinAddress& addr, addresses) BOOST_FOREACH(const CBitcoinAddress& addr, addresses)

View file

@ -99,7 +99,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx)
if (wallet->IsMine(txout)) if (wallet->IsMine(txout))
{ {
CBitcoinAddress address; CBitcoinAddress address;
if (ExtractAddress(txout.scriptPubKey, wallet, address)) if (ExtractAddress(txout.scriptPubKey, address) && wallet->HaveKey(address))
{ {
if (wallet->mapAddressBook.count(address)) if (wallet->mapAddressBook.count(address))
{ {
@ -184,7 +184,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx)
{ {
// Offline transaction // Offline transaction
CBitcoinAddress address; CBitcoinAddress address;
if (ExtractAddress(txout.scriptPubKey, 0, address)) if (ExtractAddress(txout.scriptPubKey, address))
{ {
strHTML += tr("<b>To:</b> "); strHTML += tr("<b>To:</b> ");
if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty()) if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty())
@ -271,7 +271,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx)
strHTML += "<li>"; strHTML += "<li>";
const CTxOut &vout = prev.vout[prevout.n]; const CTxOut &vout = prev.vout[prevout.n];
CBitcoinAddress address; CBitcoinAddress address;
if (ExtractAddress(vout.scriptPubKey, 0, address)) if (ExtractAddress(vout.scriptPubKey, address))
{ {
if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty()) if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty())
strHTML += HtmlEscape(wallet->mapAddressBook[address]) + " "; strHTML += HtmlEscape(wallet->mapAddressBook[address]) + " ";

View file

@ -80,7 +80,7 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *
if(wallet->IsMine(txout)) if(wallet->IsMine(txout))
{ {
CBitcoinAddress address; CBitcoinAddress address;
if (ExtractAddress(txout.scriptPubKey, wallet, address)) if (ExtractAddress(txout.scriptPubKey, address) && wallet->HaveKey(address))
{ {
sub.address = address.ToString(); sub.address = address.ToString();
} }
@ -138,7 +138,7 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *
// Sent to Bitcoin Address // Sent to Bitcoin Address
sub.type = TransactionRecord::SendToAddress; sub.type = TransactionRecord::SendToAddress;
CBitcoinAddress address; CBitcoinAddress address;
if (ExtractAddress(txout.scriptPubKey, 0, address)) if (ExtractAddress(txout.scriptPubKey, address))
{ {
sub.address = address.ToString(); sub.address = address.ToString();
} }

View file

@ -1442,7 +1442,7 @@ bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
return false; return false;
} }
bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBitcoinAddress& addressRet) bool ExtractAddress(const CScript& scriptPubKey, CBitcoinAddress& addressRet)
{ {
vector<valtype> vSolutions; vector<valtype> vSolutions;
txnouttype whichType; txnouttype whichType;
@ -1468,7 +1468,7 @@ bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBit
return false; 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(); addressRet.clear();
typeRet = TX_NONSTANDARD; typeRet = TX_NONSTANDARD;

View file

@ -572,8 +572,8 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet); bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
bool IsStandard(const CScript& scriptPubKey); bool IsStandard(const CScript& scriptPubKey);
bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey); bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* pkeystore, CBitcoinAddress& addressRet); bool ExtractAddress(const CScript& scriptPubKey, CBitcoinAddress& addressRet);
bool ExtractAddresses(const CScript& scriptPubKey, const CKeyStore* pkeystore, txnouttype& typeRet, std::vector<CBitcoinAddress>& addressRet, int& nRequiredRet); 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 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); bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int& nSigOpCountRet, int nHashType=0, bool fStrictOpEval=true);

View file

@ -192,7 +192,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1)
BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK(Solver(s, whichType, solutions));
BOOST_CHECK(solutions.size() == 1); BOOST_CHECK(solutions.size() == 1);
CBitcoinAddress addr; CBitcoinAddress addr;
BOOST_CHECK(ExtractAddress(s, &keystore, addr)); BOOST_CHECK(ExtractAddress(s, addr));
BOOST_CHECK(addr == keyaddr[0]); BOOST_CHECK(addr == keyaddr[0]);
BOOST_CHECK(IsMine(keystore, s)); BOOST_CHECK(IsMine(keystore, s));
BOOST_CHECK(!IsMine(emptykeystore, s)); BOOST_CHECK(!IsMine(emptykeystore, s));
@ -205,7 +205,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1)
BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK(Solver(s, whichType, solutions));
BOOST_CHECK(solutions.size() == 1); BOOST_CHECK(solutions.size() == 1);
CBitcoinAddress addr; CBitcoinAddress addr;
BOOST_CHECK(ExtractAddress(s, &keystore, addr)); BOOST_CHECK(ExtractAddress(s, addr));
BOOST_CHECK(addr == keyaddr[0]); BOOST_CHECK(addr == keyaddr[0]);
BOOST_CHECK(IsMine(keystore, s)); BOOST_CHECK(IsMine(keystore, s));
BOOST_CHECK(!IsMine(emptykeystore, s)); BOOST_CHECK(!IsMine(emptykeystore, s));
@ -218,7 +218,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1)
BOOST_CHECK(Solver(s, whichType, solutions)); BOOST_CHECK(Solver(s, whichType, solutions));
BOOST_CHECK_EQUAL(solutions.size(), 4); BOOST_CHECK_EQUAL(solutions.size(), 4);
CBitcoinAddress addr; CBitcoinAddress addr;
BOOST_CHECK(!ExtractAddress(s, &keystore, addr)); BOOST_CHECK(!ExtractAddress(s, addr));
BOOST_CHECK(IsMine(keystore, s)); BOOST_CHECK(IsMine(keystore, s));
BOOST_CHECK(!IsMine(emptykeystore, s)); BOOST_CHECK(!IsMine(emptykeystore, s));
} }
@ -231,7 +231,7 @@ BOOST_AUTO_TEST_CASE(multisig_Solver1)
BOOST_CHECK_EQUAL(solutions.size(), 4); BOOST_CHECK_EQUAL(solutions.size(), 4);
vector<CBitcoinAddress> addrs; vector<CBitcoinAddress> addrs;
int nRequired; 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[0] == keyaddr[0]);
BOOST_CHECK(addrs[1] == keyaddr[1]); BOOST_CHECK(addrs[1] == keyaddr[1]);
BOOST_CHECK(nRequired = 1); BOOST_CHECK(nRequired = 1);

View file

@ -394,7 +394,7 @@ bool CWallet::IsChange(const CTxOut& txout) const
// a better way of identifying which outputs are 'the send' and which are // 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 // 'the change' will need to be implemented (maybe extend CWalletTx to remember
// which output, if any, was change). // which output, if any, was change).
if (ExtractAddress(txout.scriptPubKey, this, address)) if (ExtractAddress(txout.scriptPubKey, address) && HaveKey(address))
CRITICAL_BLOCK(cs_wallet) CRITICAL_BLOCK(cs_wallet)
if (!mapAddressBook.count(address)) if (!mapAddressBook.count(address))
return true; return true;
@ -475,7 +475,7 @@ void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, l
{ {
CBitcoinAddress address; CBitcoinAddress address;
vector<unsigned char> vchPubKey; 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", printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
this->GetHash().ToString().c_str()); this->GetHash().ToString().c_str());