From 03fbd7904903928b0d1c8542a3d597aaf5bdd31b Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 5 Jul 2011 16:42:32 +0200 Subject: [PATCH 1/4] get rid of mapPubKeys Make CKeyStore's interface work on uint160's instead of pubkeys, so no separate global mapPubKeys is necessary anymore. --- src/init.cpp | 1 - src/keystore.cpp | 58 +++++++++++++++++++++++++++++-------------- src/keystore.h | 30 +++++++++++++---------- src/main.cpp | 3 --- src/main.h | 2 -- src/rpc.cpp | 6 ++--- src/script.cpp | 64 +++++++++++++----------------------------------- src/script.h | 3 +-- src/ui.cpp | 25 ++++++++++--------- src/wallet.cpp | 9 +++---- src/wallet.h | 6 ++--- 11 files changed, 97 insertions(+), 110 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index fd1d8d3cd..266c99202 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -425,7 +425,6 @@ bool AppInit2(int argc, char* argv[]) printf("mapBlockIndex.size() = %d\n", mapBlockIndex.size()); printf("nBestHeight = %d\n", nBestHeight); printf("setKeyPool.size() = %d\n", pwalletMain->setKeyPool.size()); - printf("mapPubKeys.size() = %d\n", mapPubKeys.size()); printf("mapWallet.size() = %d\n", pwalletMain->mapWallet.size()); printf("mapAddressBook.size() = %d\n", pwalletMain->mapAddressBook.size()); diff --git a/src/keystore.cpp b/src/keystore.cpp index de13958a8..9e1dab65f 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -16,14 +16,19 @@ std::vector CKeyStore::GenerateNewKey() return key.GetPubKey(); } +bool CKeyStore::GetPubKey(const uint160 &hashAddress, std::vector &vchPubKeyOut) const +{ + CKey key; + if (!GetKey(hashAddress, key)) + return false; + vchPubKeyOut = key.GetPubKey(); + return true; +} + bool CBasicKeyStore::AddKey(const CKey& key) { - CRITICAL_BLOCK(cs_mapPubKeys) CRITICAL_BLOCK(cs_KeyStore) - { - mapKeys[key.GetPubKey()] = key.GetPrivKey(); - mapPubKeys[Hash160(key.GetPubKey())] = key.GetPubKey(); - } + mapKeys[Hash160(key.GetPubKey())] = key.GetSecret(); return true; } @@ -44,11 +49,11 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn) if (!SetCrypted()) return false; - std::map, std::vector >::const_iterator mi = mapCryptedKeys.begin(); + CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin(); for (; mi != mapCryptedKeys.end(); ++mi) { - const std::vector &vchPubKey = (*mi).first; - const std::vector &vchCryptedSecret = (*mi).second; + const std::vector &vchPubKey = (*mi).second.first; + const std::vector &vchCryptedSecret = (*mi).second.second; CSecret vchSecret; if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret)) return false; @@ -88,31 +93,30 @@ bool CCryptoKeyStore::AddKey(const CKey& key) bool CCryptoKeyStore::AddCryptedKey(const std::vector &vchPubKey, const std::vector &vchCryptedSecret) { - CRITICAL_BLOCK(cs_mapPubKeys) CRITICAL_BLOCK(cs_KeyStore) { if (!SetCrypted()) return false; - mapCryptedKeys[vchPubKey] = vchCryptedSecret; - mapPubKeys[Hash160(vchPubKey)] = vchPubKey; + mapCryptedKeys[Hash160(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret); } return true; } -bool CCryptoKeyStore::GetPrivKey(const std::vector &vchPubKey, CKey& keyOut) const +bool CCryptoKeyStore::GetKey(const uint160 &hashAddress, CKey& keyOut) const { CRITICAL_BLOCK(cs_vMasterKey) { if (!IsCrypted()) - return CBasicKeyStore::GetPrivKey(vchPubKey, keyOut); + return CBasicKeyStore::GetKey(hashAddress, keyOut); - std::map, std::vector >::const_iterator mi = mapCryptedKeys.find(vchPubKey); + CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(hashAddress); if (mi != mapCryptedKeys.end()) { - const std::vector &vchCryptedSecret = (*mi).second; + const std::vector &vchPubKey = (*mi).second.first; + const std::vector &vchCryptedSecret = (*mi).second.second; CSecret vchSecret; - if (!DecryptSecret(vMasterKey, (*mi).second, Hash((*mi).first.begin(), (*mi).first.end()), vchSecret)) + if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret)) return false; keyOut.SetSecret(vchSecret); return true; @@ -121,6 +125,23 @@ bool CCryptoKeyStore::GetPrivKey(const std::vector &vchPubKey, CK return false; } +bool CCryptoKeyStore::GetPubKey(const uint160 &hashAddress, std::vector& vchPubKeyOut) const +{ + CRITICAL_BLOCK(cs_vMasterKey) + { + if (!IsCrypted()) + return CKeyStore::GetPubKey(hashAddress, vchPubKeyOut); + + CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(hashAddress); + if (mi != mapCryptedKeys.end()) + { + vchPubKeyOut = (*mi).second.first; + return true; + } + } + return false; +} + bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn) { CRITICAL_BLOCK(cs_KeyStore) @@ -135,10 +156,11 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn) { if (!key.SetPrivKey(mKey.second)) return false; + const std::vector vchPubKey = key.GetPubKey(); std::vector vchCryptedSecret; - if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(mKey.first.begin(), mKey.first.end()), vchCryptedSecret)) + if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret)) return false; - if (!AddCryptedKey(mKey.first, vchCryptedSecret)) + if (!AddCryptedKey(vchPubKey, vchCryptedSecret)) return false; } mapKeys.clear(); diff --git a/src/keystore.h b/src/keystore.h index 0dc09f05b..976c09533 100644 --- a/src/keystore.h +++ b/src/keystore.h @@ -12,12 +12,13 @@ public: mutable CCriticalSection cs_KeyStore; virtual bool AddKey(const CKey& key) =0; - virtual bool HaveKey(const std::vector &vchPubKey) const =0; - virtual bool GetPrivKey(const std::vector &vchPubKey, CKey& keyOut) const =0; + virtual bool HaveKey(const uint160 &hashAddress) const =0; + virtual bool GetKey(const uint160 &hashAddress, CKey& keyOut) const =0; + virtual bool GetPubKey(const uint160 &hashAddress, std::vector& vchPubKeyOut) const; virtual std::vector GenerateNewKey(); }; -typedef std::map, CPrivKey> KeyMap; +typedef std::map KeyMap; class CBasicKeyStore : public CKeyStore { @@ -26,26 +27,28 @@ protected: public: bool AddKey(const CKey& key); - bool HaveKey(const std::vector &vchPubKey) const + bool HaveKey(const uint160 &hashAddress) const { - return (mapKeys.count(vchPubKey) > 0); + return (mapKeys.count(hashAddress) > 0); } - bool GetPrivKey(const std::vector &vchPubKey, CKey& keyOut) const + bool GetKey(const uint160 &hashAddress, CKey& keyOut) const { - std::map, CPrivKey>::const_iterator mi = mapKeys.find(vchPubKey); + KeyMap::const_iterator mi = mapKeys.find(hashAddress); if (mi != mapKeys.end()) { - keyOut.SetPrivKey((*mi).second); + keyOut.SetSecret((*mi).second); return true; } return false; } }; +typedef std::map, std::vector > > CryptedKeyMap; + class CCryptoKeyStore : public CBasicKeyStore { private: - std::map, std::vector > mapCryptedKeys; + CryptedKeyMap mapCryptedKeys; CKeyingMaterial vMasterKey; @@ -103,13 +106,14 @@ public: virtual bool AddCryptedKey(const std::vector &vchPubKey, const std::vector &vchCryptedSecret); std::vector GenerateNewKey(); bool AddKey(const CKey& key); - bool HaveKey(const std::vector &vchPubKey) const + bool HaveKey(const uint160 &hashAddress) const { if (!IsCrypted()) - return CBasicKeyStore::HaveKey(vchPubKey); - return mapCryptedKeys.count(vchPubKey) > 0; + return CBasicKeyStore::HaveKey(hashAddress); + return mapCryptedKeys.count(hashAddress) > 0; } - bool GetPrivKey(const std::vector &vchPubKey, CKey& keyOut) const; + bool GetKey(const uint160 &hashAddress, CKey& keyOut) const; + bool GetPubKey(const uint160 &hashAddress, std::vector& vchPubKeyOut) const; }; #endif diff --git a/src/main.cpp b/src/main.cpp index e3ad35044..fa893e593 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,9 +21,6 @@ set setpwalletRegistered; CCriticalSection cs_main; -CCriticalSection cs_mapPubKeys; -map > mapPubKeys; - map mapTransactions; CCriticalSection cs_mapTransactions; unsigned int nTransactionsUpdated = 0; diff --git a/src/main.h b/src/main.h index d34f68f9d..9d6de52fa 100644 --- a/src/main.h +++ b/src/main.h @@ -1568,7 +1568,5 @@ public: extern std::map mapTransactions; -extern std::map > mapPubKeys; -extern CCriticalSection cs_mapPubKeys; #endif diff --git a/src/rpc.cpp b/src/rpc.cpp index fa14fc0b2..469805827 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -967,7 +967,7 @@ Value ListReceived(const Array& params, bool fByAccounts) { // Only counting our own bitcoin addresses and not ip addresses uint160 hash160 = txout.scriptPubKey.GetBitcoinAddressHash160(); - if (hash160 == 0 || !mapPubKeys.count(hash160)) // IsMine + if (hash160 == 0 || !pwalletMain->HaveKey(hash160)) // IsMine continue; tallyitem& item = mapTally[hash160]; @@ -1242,7 +1242,7 @@ Value listaccounts(const Array& params, bool fHelp) { BOOST_FOREACH(const PAIRTYPE(string, string)& entry, pwalletMain->mapAddressBook) { uint160 hash160; - if(AddressToHash160(entry.first, hash160) && mapPubKeys.count(hash160)) // This address belongs to me + if(AddressToHash160(entry.first, hash160) && pwalletMain->HaveKey(hash160)) // This address belongs to me mapAccountBalances[entry.second] = 0; } @@ -1564,7 +1564,7 @@ Value validateaddress(const Array& params, bool fHelp) // version of the address: string currentAddress = Hash160ToAddress(hash160); ret.push_back(Pair("address", currentAddress)); - ret.push_back(Pair("ismine", (mapPubKeys.count(hash160) > 0))); + ret.push_back(Pair("ismine", (pwalletMain->HaveKey(hash160) > 0))); CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { if (pwalletMain->mapAddressBook.count(currentAddress)) diff --git a/src/script.cpp b/src/script.cpp index 654aaa10e..b92f7b08b 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -1041,7 +1041,9 @@ bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash // Sign const valtype& vchPubKey = item.second; CKey key; - if (!keystore.GetPrivKey(vchPubKey, key)) + if (!keystore.GetKey(Hash160(vchPubKey), key)) + return false; + if (key.GetPubKey() != vchPubKey) return false; if (hash != 0) { @@ -1055,12 +1057,8 @@ bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash else if (item.first == OP_PUBKEYHASH) { // Sign and give pubkey - map::iterator mi = mapPubKeys.find(uint160(item.second)); - if (mi == mapPubKeys.end()) - return false; - const vector& vchPubKey = (*mi).second; CKey key; - if (!keystore.GetPrivKey(vchPubKey, key)) + if (!keystore.GetKey(uint160(item.second), key)) return false; if (hash != 0) { @@ -1068,7 +1066,7 @@ bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash if (!key.Sign(hash, vchSig)) return false; vchSig.push_back((unsigned char)nHashType); - scriptSigRet << vchSig << vchPubKey; + scriptSigRet << vchSig << key.GetPubKey(); } } else @@ -1102,19 +1100,16 @@ bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey) { if (item.first == OP_PUBKEY) { - // Sign const valtype& vchPubKey = item.second; - if (!keystore.HaveKey(vchPubKey)) + vector vchPubKeyFound; + if (!keystore.GetPubKey(Hash160(vchPubKey), vchPubKeyFound)) + return false; + if (vchPubKeyFound != vchPubKey) return false; } else if (item.first == OP_PUBKEYHASH) { - // Sign and give pubkey - map::iterator mi = mapPubKeys.find(uint160(item.second)); - if (mi == mapPubKeys.end()) - return false; - const vector& vchPubKey = (*mi).second; - if (!keystore.HaveKey(vchPubKey)) + if (!keystore.HaveKey(uint160(item.second))) return false; } else @@ -1128,33 +1123,28 @@ bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey) } -bool ExtractPubKey(const CScript& scriptPubKey, const CKeyStore* keystore, vector& vchPubKeyRet) +bool ExtractHash160(const CScript& scriptPubKey, const CKeyStore* keystore, uint160& hash160Ret) { - vchPubKeyRet.clear(); - vector > vSolution; if (!Solver(scriptPubKey, vSolution)) return false; - CRITICAL_BLOCK(cs_mapPubKeys) + CRITICAL_BLOCK(keystore->cs_KeyStore) { BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution) { - valtype vchPubKey; + uint160 hash160; if (item.first == OP_PUBKEY) { - vchPubKey = item.second; + hash160 = Hash160(item.second); } else if (item.first == OP_PUBKEYHASH) { - map::iterator mi = mapPubKeys.find(uint160(item.second)); - if (mi == mapPubKeys.end()) - continue; - vchPubKey = (*mi).second; + hash160 = uint160(item.second); } - if (keystore == NULL || keystore->HaveKey(vchPubKey)) + if (keystore == NULL || keystore->HaveKey(hash160)) { - vchPubKeyRet = vchPubKey; + hash160Ret = hash160; return true; } } @@ -1163,26 +1153,6 @@ bool ExtractPubKey(const CScript& scriptPubKey, const CKeyStore* keystore, vecto } -bool ExtractHash160(const CScript& scriptPubKey, uint160& hash160Ret) -{ - hash160Ret = 0; - - vector > vSolution; - if (!Solver(scriptPubKey, vSolution)) - return false; - - BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution) - { - if (item.first == OP_PUBKEYHASH) - { - hash160Ret = uint160(item.second); - return true; - } - } - return false; -} - - bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, int nHashType) { vector > stack; diff --git a/src/script.h b/src/script.h index 2a36db2fa..be950f300 100644 --- a/src/script.h +++ b/src/script.h @@ -710,8 +710,7 @@ public: bool IsStandard(const CScript& scriptPubKey); bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey); -bool ExtractPubKey(const CScript& scriptPubKey, const CKeyStore* pkeystore, std::vector& vchPubKeyRet); -bool ExtractHash160(const CScript& scriptPubKey, uint160& hash160Ret); +bool ExtractHash160(const CScript& scriptPubKey, const CKeyStore* pkeystore, uint160& hash160Ret); 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 nHashType=0); diff --git a/src/ui.cpp b/src/ui.cpp index eae0a4f4c..fba8ed806 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -238,9 +238,10 @@ void SetDefaultReceivingAddress(const string& strAddress) uint160 hash160; if (!AddressToHash160(strAddress, hash160)) return; - if (!mapPubKeys.count(hash160)) + vector vchPubKey; + if (!pwalletMain->GetPubKey(hash160, vchPubKey)) return; - pwalletMain->SetDefaultKey(mapPubKeys[hash160]); + pwalletMain->SetDefaultKey(vchPubKey); pframeMain->m_textCtrlAddress->SetValue(strAddress); } } @@ -703,15 +704,15 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) { if (pwalletMain->IsMine(txout)) { - vector vchPubKey; - if (ExtractPubKey(txout.scriptPubKey, pwalletMain, vchPubKey)) + uint160 hash160; + if (ExtractHash160(txout.scriptPubKey, pwalletMain, hash160)) { CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { //strDescription += _("Received payment to "); //strDescription += _("Received with address "); strDescription += _("Received with: "); - string strAddress = PubKeyToAddress(vchPubKey); + string strAddress = Hash160ToAddress(hash160); map::iterator mi = pwalletMain->mapAddressBook.find(strAddress); if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty()) { @@ -786,7 +787,7 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) { // Sent to Bitcoin Address uint160 hash160; - if (ExtractHash160(txout.scriptPubKey, hash160)) + if (ExtractHash160(txout.scriptPubKey, pwalletMain, hash160)) strAddress = Hash160ToAddress(hash160); } @@ -1502,10 +1503,10 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails { if (pwalletMain->IsMine(txout)) { - vector vchPubKey; - if (ExtractPubKey(txout.scriptPubKey, pwalletMain, vchPubKey)) + uint160 hash160; + if (ExtractHash160(txout.scriptPubKey, pwalletMain, hash160)) { - string strAddress = PubKeyToAddress(vchPubKey); + string strAddress = Hash160ToAddress(hash160); if (pwalletMain->mapAddressBook.count(strAddress)) { strHTML += string() + _("From: ") + _("unknown") + "
"; @@ -1589,7 +1590,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails { // Offline transaction uint160 hash160; - if (ExtractHash160(txout.scriptPubKey, hash160)) + if (ExtractHash160(txout.scriptPubKey, pwalletMain, hash160)) { string strAddress = Hash160ToAddress(hash160); strHTML += _("To: "); @@ -2630,7 +2631,7 @@ CAddressBookDialog::CAddressBookDialog(wxWindow* parent, const wxString& strInit string strAddress = item.first; string strName = item.second; uint160 hash160; - bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160)); + bool fMine = (AddressToHash160(strAddress, hash160) && pwalletMain->HaveKey(hash160)); wxListCtrl* plistCtrl = fMine ? m_listCtrlReceiving : m_listCtrlSending; int nIndex = InsertLine(plistCtrl, strName, strAddress); if (strAddress == (fMine ? strDefaultReceiving : string(strInitSelected))) @@ -2741,7 +2742,7 @@ void CAddressBookDialog::OnButtonCopy(wxCommandEvent& event) bool CAddressBookDialog::CheckIfMine(const string& strAddress, const string& strTitle) { uint160 hash160; - bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160)); + bool fMine = (AddressToHash160(strAddress, hash160) && pwalletMain->HaveKey(hash160)); if (fMine) wxMessageBox(_("This is one of your own addresses for receiving payments and cannot be entered in the address book. "), strTitle); return fMine; diff --git a/src/wallet.cpp b/src/wallet.cpp index 2ee918fda..ba5018639 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -124,7 +124,6 @@ public: bool CWallet::EncryptWallet(const string& strWalletPassphrase) { - CRITICAL_BLOCK(cs_mapPubKeys) CRITICAL_BLOCK(cs_KeyStore) CRITICAL_BLOCK(cs_vMasterKey) CRITICAL_BLOCK(cs_pwalletdbEncryption) @@ -439,10 +438,8 @@ void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, l string address; uint160 hash160; vector vchPubKey; - if (ExtractHash160(txout.scriptPubKey, hash160)) + if (ExtractHash160(txout.scriptPubKey, pwallet, hash160)) address = Hash160ToAddress(hash160); - else if (ExtractPubKey(txout.scriptPubKey, NULL, vchPubKey)) - address = PubKeyToAddress(vchPubKey); else { printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n", @@ -1136,7 +1133,7 @@ int CWallet::LoadWallet(bool& fFirstRunRet) return nLoadWalletRet; fFirstRunRet = vchDefaultKey.empty(); - if (!HaveKey(vchDefaultKey)) + if (!HaveKey(Hash160(vchDefaultKey))) { // Create new keyUser and set as default key RandAddSeedPerfmon(); @@ -1263,7 +1260,7 @@ void CWallet::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool) setKeyPool.erase(setKeyPool.begin()); if (!walletdb.ReadPool(nIndex, keypool)) throw runtime_error("ReserveKeyFromKeyPool() : read failed"); - if (!HaveKey(keypool.vchPubKey)) + if (!HaveKey(Hash160(keypool.vchPubKey))) throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool"); assert(!keypool.vchPubKey.empty()); printf("keypool reserve %"PRI64d"\n", nIndex); diff --git a/src/wallet.h b/src/wallet.h index c19b3f43c..277d0b1e7 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -104,10 +104,10 @@ public: } bool IsChange(const CTxOut& txout) const { - std::vector vchPubKey; - if (ExtractPubKey(txout.scriptPubKey, this, vchPubKey)) + uint160 hash160; + if (ExtractHash160(txout.scriptPubKey, this, hash160)) CRITICAL_BLOCK(cs_mapAddressBook) - if (!mapAddressBook.count(PubKeyToAddress(vchPubKey))) + if (!mapAddressBook.count(Hash160ToAddress(hash160))) return true; return false; } From 2ffba736e9102d016b96c2e5de2ce7757e612667 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 5 Jul 2011 20:53:43 +0200 Subject: [PATCH 2/4] Use CBitcoinAddress instead of string/uint160 Instead of conversion functions between pubkey/uint160/address in base58.h, have a fully fledged class CBitcoinAddress (CAddress was already taken) to represent addresses. --- src/base58.h | 145 ++++++++++++++++++++++++++++++++++------------ src/key.h | 5 ++ src/keystore.cpp | 20 +++---- src/keystore.h | 28 ++++----- src/rpc.cpp | 146 +++++++++++++++++++++-------------------------- src/script.cpp | 15 ++--- src/script.h | 30 ++-------- src/ui.cpp | 77 ++++++++++++------------- src/wallet.cpp | 44 +++++++------- src/wallet.h | 18 +++--- 10 files changed, 278 insertions(+), 250 deletions(-) diff --git a/src/base58.h b/src/base58.h index c2729d477..816193eae 100644 --- a/src/base58.h +++ b/src/base58.h @@ -159,52 +159,121 @@ inline bool DecodeBase58Check(const std::string& str, std::vector -#define ADDRESSVERSION ((unsigned char)(fTestNet ? 111 : 0)) - -inline std::string Hash160ToAddress(uint160 hash160) +class CBitcoinAddress { - // add 1-byte version number to the front - std::vector vch(1, ADDRESSVERSION); - vch.insert(vch.end(), UBEGIN(hash160), UEND(hash160)); - return EncodeBase58Check(vch); -} +protected: + unsigned char nVersion; + std::vector vchData; -inline bool AddressToHash160(const char* psz, uint160& hash160Ret) -{ - std::vector vch; - if (!DecodeBase58Check(psz, vch)) - return false; - if (vch.empty()) - return false; - unsigned char nVersion = vch[0]; - if (vch.size() != sizeof(hash160Ret) + 1) - return false; - memcpy(&hash160Ret, &vch[1], sizeof(hash160Ret)); - return (nVersion <= ADDRESSVERSION); -} +public: + bool SetAddress(const uint160& hash160) + { + nVersion = fTestNet ? 111 : 0; + vchData.resize(20); + memcpy(&vchData[0], &hash160, 20); + return true; + } -inline bool AddressToHash160(const std::string& str, uint160& hash160Ret) -{ - return AddressToHash160(str.c_str(), hash160Ret); -} + bool SetAddress(const char* pszAddress) + { + std::vector vchTemp; + DecodeBase58Check(pszAddress, vchTemp); + if (vchTemp.empty()) + { + vchData.clear(); + nVersion = 0; + return false; + } + nVersion = vchTemp[0]; + vchData.resize(vchTemp.size() - 1); + memcpy(&vchData[0], &vchTemp[1], vchData.size()); + return true; + } -inline bool IsValidBitcoinAddress(const char* psz) -{ - uint160 hash160; - return AddressToHash160(psz, hash160); -} + bool SetAddress(const std::string& strAddress) + { + return SetAddress(strAddress.c_str()); + } -inline bool IsValidBitcoinAddress(const std::string& str) -{ - return IsValidBitcoinAddress(str.c_str()); -} + bool SetAddress(const std::vector& vchPubKey) + { + return SetAddress(Hash160(vchPubKey)); + } + + bool IsValid() const + { + int nExpectedSize = 20; + bool fExpectTestNet = false; + switch(nVersion) + { + case 0: + break; + + case 111: + fExpectTestNet = true; + break; + default: + return false; + } + return fExpectTestNet == fTestNet && vchData.size() == nExpectedSize; + } + CBitcoinAddress() + { + nVersion = 0; + vchData.clear(); + } + + CBitcoinAddress(uint160 hash160In) + { + SetAddress(hash160In); + } + CBitcoinAddress(const std::vector& vchPubKey) + { + SetAddress(vchPubKey); + } -inline std::string PubKeyToAddress(const std::vector& vchPubKey) -{ - return Hash160ToAddress(Hash160(vchPubKey)); -} + CBitcoinAddress(const std::string& strAddress) + { + SetAddress(strAddress); + } + + CBitcoinAddress(const char* pszAddress) + { + SetAddress(pszAddress); + } + + std::string ToString() const + { + std::vector vch(1, nVersion); + vch.insert(vch.end(), vchData.begin(), vchData.end()); + return EncodeBase58Check(vch); + } + + uint160 GetHash160() const + { + assert(vchData.size() == 20); + uint160 hash160; + memcpy(&hash160, &vchData[0], 20); + return hash160; + } + + int CompareTo(const CBitcoinAddress& address) const + { + if (nVersion < address.nVersion) return -1; + if (nVersion < address.nVersion) return 1; + if (vchData < address.vchData) return -1; + if (vchData > address.vchData) return 1; + return 0; + } + + bool operator==(const CBitcoinAddress& address) const { return CompareTo(address) == 0; } + bool operator<=(const CBitcoinAddress& address) const { return CompareTo(address) <= 0; } + bool operator>=(const CBitcoinAddress& address) const { return CompareTo(address) >= 0; } + bool operator< (const CBitcoinAddress& address) const { return CompareTo(address) < 0; } + bool operator> (const CBitcoinAddress& address) const { return CompareTo(address) > 0; } +}; #endif diff --git a/src/key.h b/src/key.h index c43e4ee23..3c14cfedd 100644 --- a/src/key.h +++ b/src/key.h @@ -220,6 +220,11 @@ public: return false; return true; } + + CBitcoinAddress GetAddress() const + { + return CBitcoinAddress(GetPubKey()); + } }; #endif diff --git a/src/keystore.cpp b/src/keystore.cpp index 9e1dab65f..2125d8472 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -16,10 +16,10 @@ std::vector CKeyStore::GenerateNewKey() return key.GetPubKey(); } -bool CKeyStore::GetPubKey(const uint160 &hashAddress, std::vector &vchPubKeyOut) const +bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector &vchPubKeyOut) const { CKey key; - if (!GetKey(hashAddress, key)) + if (!GetKey(address, key)) return false; vchPubKeyOut = key.GetPubKey(); return true; @@ -28,7 +28,7 @@ bool CKeyStore::GetPubKey(const uint160 &hashAddress, std::vector bool CBasicKeyStore::AddKey(const CKey& key) { CRITICAL_BLOCK(cs_KeyStore) - mapKeys[Hash160(key.GetPubKey())] = key.GetSecret(); + mapKeys[key.GetAddress()] = key.GetSecret(); return true; } @@ -98,19 +98,19 @@ bool CCryptoKeyStore::AddCryptedKey(const std::vector &vchPubKey, if (!SetCrypted()) return false; - mapCryptedKeys[Hash160(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret); + mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret); } return true; } -bool CCryptoKeyStore::GetKey(const uint160 &hashAddress, CKey& keyOut) const +bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const { CRITICAL_BLOCK(cs_vMasterKey) { if (!IsCrypted()) - return CBasicKeyStore::GetKey(hashAddress, keyOut); + return CBasicKeyStore::GetKey(address, keyOut); - CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(hashAddress); + CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); if (mi != mapCryptedKeys.end()) { const std::vector &vchPubKey = (*mi).second.first; @@ -125,14 +125,14 @@ bool CCryptoKeyStore::GetKey(const uint160 &hashAddress, CKey& keyOut) const return false; } -bool CCryptoKeyStore::GetPubKey(const uint160 &hashAddress, std::vector& vchPubKeyOut) const +bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector& vchPubKeyOut) const { CRITICAL_BLOCK(cs_vMasterKey) { if (!IsCrypted()) - return CKeyStore::GetPubKey(hashAddress, vchPubKeyOut); + return CKeyStore::GetPubKey(address, vchPubKeyOut); - CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(hashAddress); + CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); if (mi != mapCryptedKeys.end()) { vchPubKeyOut = (*mi).second.first; diff --git a/src/keystore.h b/src/keystore.h index 976c09533..436053a9e 100644 --- a/src/keystore.h +++ b/src/keystore.h @@ -12,13 +12,13 @@ public: mutable CCriticalSection cs_KeyStore; virtual bool AddKey(const CKey& key) =0; - virtual bool HaveKey(const uint160 &hashAddress) const =0; - virtual bool GetKey(const uint160 &hashAddress, CKey& keyOut) const =0; - virtual bool GetPubKey(const uint160 &hashAddress, std::vector& vchPubKeyOut) const; + virtual bool HaveKey(const CBitcoinAddress &address) const =0; + virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0; + virtual bool GetPubKey(const CBitcoinAddress &address, std::vector& vchPubKeyOut) const; virtual std::vector GenerateNewKey(); }; -typedef std::map KeyMap; +typedef std::map KeyMap; class CBasicKeyStore : public CKeyStore { @@ -27,13 +27,13 @@ protected: public: bool AddKey(const CKey& key); - bool HaveKey(const uint160 &hashAddress) const + bool HaveKey(const CBitcoinAddress &address) const { - return (mapKeys.count(hashAddress) > 0); + return (mapKeys.count(address) > 0); } - bool GetKey(const uint160 &hashAddress, CKey& keyOut) const + bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const { - KeyMap::const_iterator mi = mapKeys.find(hashAddress); + KeyMap::const_iterator mi = mapKeys.find(address); if (mi != mapKeys.end()) { keyOut.SetSecret((*mi).second); @@ -43,7 +43,7 @@ public: } }; -typedef std::map, std::vector > > CryptedKeyMap; +typedef std::map, std::vector > > CryptedKeyMap; class CCryptoKeyStore : public CBasicKeyStore { @@ -106,14 +106,14 @@ public: virtual bool AddCryptedKey(const std::vector &vchPubKey, const std::vector &vchCryptedSecret); std::vector GenerateNewKey(); bool AddKey(const CKey& key); - bool HaveKey(const uint160 &hashAddress) const + bool HaveKey(const CBitcoinAddress &address) const { if (!IsCrypted()) - return CBasicKeyStore::HaveKey(hashAddress); - return mapCryptedKeys.count(hashAddress) > 0; + return CBasicKeyStore::HaveKey(address); + return mapCryptedKeys.count(address) > 0; } - bool GetKey(const uint160 &hashAddress, CKey& keyOut) const; - bool GetPubKey(const uint160 &hashAddress, std::vector& vchPubKeyOut) const; + bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const; + bool GetPubKey(const CBitcoinAddress &address, std::vector& vchPubKeyOut) const; }; #endif diff --git a/src/rpc.cpp b/src/rpc.cpp index 469805827..92096b460 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -342,7 +342,7 @@ Value getnewaddress(const Array& params, bool fHelp) strAccount = AccountFromValue(params[0]); // Generate a new key that is added to wallet - string strAddress = PubKeyToAddress(pwalletMain->GetOrReuseKeyFromPool()); + string strAddress = CBitcoinAddress(pwalletMain->GetOrReuseKeyFromPool()).ToString(); // This could be done in the same main CS as GetKeyFromKeyPool. CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) @@ -353,7 +353,7 @@ Value getnewaddress(const Array& params, bool fHelp) // requires cs_main, cs_mapWallet, cs_mapAddressBook locks -string GetAccountAddress(string strAccount, bool bForceNew=false) +CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false) { string strAddress; @@ -393,16 +393,14 @@ string GetAccountAddress(string strAccount, bool bForceNew=false) else { account.vchPubKey = pwalletMain->GetOrReuseKeyFromPool(); - string strAddress = PubKeyToAddress(account.vchPubKey); + string strAddress = CBitcoinAddress(account.vchPubKey).ToString(); pwalletMain->SetAddressBookName(strAddress, strAccount); walletdb.WriteAccount(strAccount, account); } } } - strAddress = PubKeyToAddress(account.vchPubKey); - - return strAddress; + return CBitcoinAddress(account.vchPubKey); } Value getaccountaddress(const Array& params, bool fHelp) @@ -421,7 +419,7 @@ Value getaccountaddress(const Array& params, bool fHelp) CRITICAL_BLOCK(pwalletMain->cs_mapWallet) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { - ret = GetAccountAddress(strAccount); + ret = GetAccountAddress(strAccount).ToString(); } return ret; @@ -437,9 +435,8 @@ Value setaccount(const Array& params, bool fHelp) "Sets the account associated with the given address."); string strAddress = params[0].get_str(); - uint160 hash160; - bool isValid = AddressToHash160(strAddress, hash160); - if (!isValid) + CBitcoinAddress address(strAddress); + if (!address.IsValid()) throw JSONRPCError(-5, "Invalid bitcoin address"); @@ -452,10 +449,10 @@ Value setaccount(const Array& params, bool fHelp) CRITICAL_BLOCK(pwalletMain->cs_mapWallet) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { - if (pwalletMain->mapAddressBook.count(strAddress)) + if (pwalletMain->mapAddressBook.count(address)) { - string strOldAccount = pwalletMain->mapAddressBook[strAddress]; - if (strAddress == GetAccountAddress(strOldAccount)) + string strOldAccount = pwalletMain->mapAddressBook[address]; + if (address == GetAccountAddress(strOldAccount)) GetAccountAddress(strOldAccount, true); } @@ -474,11 +471,12 @@ Value getaccount(const Array& params, bool fHelp) "Returns the account associated with the given address."); string strAddress = params[0].get_str(); + CBitcoinAddress address(strAddress); string strAccount; CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { - map::iterator mi = pwalletMain->mapAddressBook.find(strAddress); + map::iterator mi = pwalletMain->mapAddressBook.find(address); if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty()) strAccount = (*mi).second; } @@ -499,17 +497,12 @@ Value getaddressesbyaccount(const Array& params, bool fHelp) Array ret; CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { - BOOST_FOREACH(const PAIRTYPE(string, string)& item, pwalletMain->mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook) { - const string& strAddress = item.first; + const CBitcoinAddress& address = item.first; const string& strName = item.second; if (strName == strAccount) - { - // We're only adding valid bitcoin addresses and not ip addresses - CScript scriptPubKey; - if (scriptPubKey.SetBitcoinAddress(strAddress)) - ret.push_back(strAddress); - } + ret.push_back(address.ToString()); } } return ret; @@ -578,10 +571,11 @@ Value getreceivedbyaddress(const Array& params, bool fHelp) "Returns the total amount received by in transactions with at least [minconf] confirmations."); // Bitcoin address - string strAddress = params[0].get_str(); + CBitcoinAddress address = CBitcoinAddress(params[0].get_str()); CScript scriptPubKey; - if (!scriptPubKey.SetBitcoinAddress(strAddress)) + if (!address.IsValid()) throw JSONRPCError(-5, "Invalid bitcoin address"); + scriptPubKey.SetBitcoinAddress(address); if (!IsMine(*pwalletMain,scriptPubKey)) return (double)0.0; @@ -611,22 +605,16 @@ Value getreceivedbyaddress(const Array& params, bool fHelp) } -void GetAccountPubKeys(string strAccount, set& setPubKey) +void GetAccountAddresses(string strAccount, set& setAddress) { CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { - BOOST_FOREACH(const PAIRTYPE(string, string)& item, pwalletMain->mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook) { - const string& strAddress = item.first; + const CBitcoinAddress& address = item.first; const string& strName = item.second; if (strName == strAccount) - { - // We're only counting our own valid bitcoin addresses and not ip addresses - CScript scriptPubKey; - if (scriptPubKey.SetBitcoinAddress(strAddress)) - if (IsMine(*pwalletMain,scriptPubKey)) - setPubKey.insert(scriptPubKey); - } + setAddress.insert(address); } } } @@ -646,8 +634,8 @@ Value getreceivedbyaccount(const Array& params, bool fHelp) // Get the set of pub keys that have the label string strAccount = AccountFromValue(params[0]); - set setPubKey; - GetAccountPubKeys(strAccount, setPubKey); + set setAddress; + GetAccountAddresses(strAccount, setAddress); // Tally int64 nAmount = 0; @@ -660,9 +648,12 @@ Value getreceivedbyaccount(const Array& params, bool fHelp) continue; BOOST_FOREACH(const CTxOut& txout, wtx.vout) - if (setPubKey.count(txout.scriptPubKey)) + { + CBitcoinAddress address; + if (ExtractAddress(txout.scriptPubKey, pwalletMain, address) && setAddress.count(address)) if (wtx.GetDepthInMainChain() >= nMinDepth) nAmount += txout.nValue; + } } } @@ -733,13 +724,13 @@ Value getbalance(const Array& params, bool fHelp) int64 allGeneratedImmature, allGeneratedMature, allFee; allGeneratedImmature = allGeneratedMature = allFee = 0; string strSentAccount; - list > listReceived; - list > listSent; + list > listReceived; + list > listSent; wtx.GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount); if (wtx.GetDepthInMainChain() >= nMinDepth) - BOOST_FOREACH(const PAIRTYPE(string,int64)& r, listReceived) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress,int64)& r, listReceived) nBalance += r.second; - BOOST_FOREACH(const PAIRTYPE(string,int64)& r, listSent) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress,int64)& r, listSent) nBalance -= r.second; nBalance -= allFee; nBalance += allGeneratedMature; @@ -874,22 +865,22 @@ Value sendmany(const Array& params, bool fHelp) if (params.size() > 3 && params[3].type() != null_type && !params[3].get_str().empty()) wtx.mapValue["comment"] = params[3].get_str(); - set setAddress; + set setAddress; vector > vecSend; int64 totalAmount = 0; BOOST_FOREACH(const Pair& s, sendTo) { - uint160 hash160; - string strAddress = s.name_; + CBitcoinAddress address(s.name_); + if (!address.IsValid()) + throw JSONRPCError(-5, string("Invalid bitcoin address:")+s.name_); - if (setAddress.count(strAddress)) - throw JSONRPCError(-8, string("Invalid parameter, duplicated address: ")+strAddress); - setAddress.insert(strAddress); + if (setAddress.count(address)) + throw JSONRPCError(-8, string("Invalid parameter, duplicated address: ")+s.name_); + setAddress.insert(address); CScript scriptPubKey; - if (!scriptPubKey.SetBitcoinAddress(strAddress)) - throw JSONRPCError(-5, string("Invalid bitcoin address:")+strAddress); + scriptPubKey.SetBitcoinAddress(address); int64 nAmount = AmountFromValue(s.value_); totalAmount += nAmount; @@ -950,7 +941,7 @@ Value ListReceived(const Array& params, bool fByAccounts) fIncludeEmpty = params[1].get_bool(); // Tally - map mapTally; + map mapTally; CRITICAL_BLOCK(pwalletMain->cs_mapWallet) { for (map::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) @@ -965,12 +956,11 @@ Value ListReceived(const Array& params, bool fByAccounts) BOOST_FOREACH(const CTxOut& txout, wtx.vout) { - // Only counting our own bitcoin addresses and not ip addresses - uint160 hash160 = txout.scriptPubKey.GetBitcoinAddressHash160(); - if (hash160 == 0 || !pwalletMain->HaveKey(hash160)) // IsMine + CBitcoinAddress address; + if (!ExtractAddress(txout.scriptPubKey, pwalletMain, address) || !address.IsValid()) continue; - tallyitem& item = mapTally[hash160]; + tallyitem& item = mapTally[address]; item.nAmount += txout.nValue; item.nConf = min(item.nConf, nDepth); } @@ -982,14 +972,11 @@ Value ListReceived(const Array& params, bool fByAccounts) map mapAccountTally; CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { - BOOST_FOREACH(const PAIRTYPE(string, string)& item, pwalletMain->mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook) { - const string& strAddress = item.first; + const CBitcoinAddress& address = item.first; const string& strAccount = item.second; - uint160 hash160; - if (!AddressToHash160(strAddress, hash160)) - continue; - map::iterator it = mapTally.find(hash160); + map::iterator it = mapTally.find(address); if (it == mapTally.end() && !fIncludeEmpty) continue; @@ -1010,7 +997,7 @@ Value ListReceived(const Array& params, bool fByAccounts) else { Object obj; - obj.push_back(Pair("address", strAddress)); + obj.push_back(Pair("address", address.ToString())); obj.push_back(Pair("account", strAccount)); obj.push_back(Pair("label", strAccount)); // deprecated obj.push_back(Pair("amount", ValueFromAmount(nAmount))); @@ -1073,8 +1060,8 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe { int64 nGeneratedImmature, nGeneratedMature, nFee; string strSentAccount; - list > listReceived; - list > listSent; + list > listReceived; + list > listSent; wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount); bool fAllAccounts = (strAccount == string("*")); @@ -1102,11 +1089,11 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe // Sent if ((!listSent.empty() || nFee != 0) && (fAllAccounts || strAccount == strSentAccount)) { - BOOST_FOREACH(const PAIRTYPE(string, int64)& s, listSent) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, int64)& s, listSent) { Object entry; entry.push_back(Pair("account", strSentAccount)); - entry.push_back(Pair("address", s.first)); + entry.push_back(Pair("address", s.first.ToString())); entry.push_back(Pair("category", "send")); entry.push_back(Pair("amount", ValueFromAmount(-s.second))); entry.push_back(Pair("fee", ValueFromAmount(-nFee))); @@ -1120,7 +1107,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { - BOOST_FOREACH(const PAIRTYPE(string, int64)& r, listReceived) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, int64)& r, listReceived) { string account; if (pwalletMain->mapAddressBook.count(r.first)) @@ -1129,7 +1116,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe { Object entry; entry.push_back(Pair("account", account)); - entry.push_back(Pair("address", r.first)); + entry.push_back(Pair("address", r.first.ToString())); entry.push_back(Pair("category", "receive")); entry.push_back(Pair("amount", ValueFromAmount(r.second))); if (fLong) @@ -1240,9 +1227,8 @@ Value listaccounts(const Array& params, bool fHelp) CRITICAL_BLOCK(pwalletMain->cs_mapWallet) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { - BOOST_FOREACH(const PAIRTYPE(string, string)& entry, pwalletMain->mapAddressBook) { - uint160 hash160; - if(AddressToHash160(entry.first, hash160) && pwalletMain->HaveKey(hash160)) // This address belongs to me + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& entry, pwalletMain->mapAddressBook) { + if (pwalletMain->HaveKey(entry.first)) // This address belongs to me mapAccountBalances[entry.second] = 0; } @@ -1251,16 +1237,16 @@ Value listaccounts(const Array& params, bool fHelp) const CWalletTx& wtx = (*it).second; int64 nGeneratedImmature, nGeneratedMature, nFee; string strSentAccount; - list > listReceived; - list > listSent; + list > listReceived; + list > listSent; wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount); mapAccountBalances[strSentAccount] -= nFee; - BOOST_FOREACH(const PAIRTYPE(string, int64)& s, listSent) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, int64)& s, listSent) mapAccountBalances[strSentAccount] -= s.second; if (wtx.GetDepthInMainChain() >= nMinDepth) { mapAccountBalances[""] += nGeneratedMature; - BOOST_FOREACH(const PAIRTYPE(string, int64)& r, listReceived) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, int64)& r, listReceived) if (pwalletMain->mapAddressBook.count(r.first)) mapAccountBalances[pwalletMain->mapAddressBook[r.first]] += r.second; else @@ -1553,8 +1539,8 @@ Value validateaddress(const Array& params, bool fHelp) "Return information about ."); string strAddress = params[0].get_str(); - uint160 hash160; - bool isValid = AddressToHash160(strAddress, hash160); + CBitcoinAddress address(strAddress); + bool isValid = address.IsValid(); Object ret; ret.push_back(Pair("isvalid", isValid)); @@ -1562,13 +1548,13 @@ Value validateaddress(const Array& params, bool fHelp) { // Call Hash160ToAddress() so we always return current ADDRESSVERSION // version of the address: - string currentAddress = Hash160ToAddress(hash160); + string currentAddress = address.ToString(); ret.push_back(Pair("address", currentAddress)); - ret.push_back(Pair("ismine", (pwalletMain->HaveKey(hash160) > 0))); + ret.push_back(Pair("ismine", (pwalletMain->HaveKey(address) > 0))); CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { - if (pwalletMain->mapAddressBook.count(currentAddress)) - ret.push_back(Pair("account", pwalletMain->mapAddressBook[currentAddress])); + if (pwalletMain->mapAddressBook.count(address)) + ret.push_back(Pair("account", pwalletMain->mapAddressBook[address])); } } return ret; diff --git a/src/script.cpp b/src/script.cpp index b92f7b08b..f91760084 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -1123,7 +1123,7 @@ bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey) } -bool ExtractHash160(const CScript& scriptPubKey, const CKeyStore* keystore, uint160& hash160Ret) +bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBitcoinAddress& addressRet) { vector > vSolution; if (!Solver(scriptPubKey, vSolution)) @@ -1135,18 +1135,11 @@ bool ExtractHash160(const CScript& scriptPubKey, const CKeyStore* keystore, uint { uint160 hash160; if (item.first == OP_PUBKEY) - { - hash160 = Hash160(item.second); - } + addressRet.SetAddress(item.second); else if (item.first == OP_PUBKEYHASH) - { - hash160 = uint160(item.second); - } - if (keystore == NULL || keystore->HaveKey(hash160)) - { - hash160Ret = hash160; + addressRet.SetAddress(uint160(item.second)); + if (keystore == NULL || keystore->HaveKey(addressRet)) return true; - } } } return false; diff --git a/src/script.h b/src/script.h index be950f300..9d94e3f5c 100644 --- a/src/script.h +++ b/src/script.h @@ -622,7 +622,7 @@ public: } - uint160 GetBitcoinAddressHash160() const + CBitcoinAddress GetBitcoinAddress() const { opcodetype opcode; std::vector vch; @@ -634,36 +634,18 @@ public: if (!GetOp(pc, opcode, vch) || opcode != OP_EQUALVERIFY) return 0; if (!GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG) return 0; if (pc != end()) return 0; - return hash160; + return CBitcoinAddress(hash160); } - std::string GetBitcoinAddress() const - { - uint160 hash160 = GetBitcoinAddressHash160(); - if (hash160 == 0) - return ""; - return Hash160ToAddress(hash160); - } - - void SetBitcoinAddress(const uint160& hash160) + void SetBitcoinAddress(const CBitcoinAddress& address) { this->clear(); - *this << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG; + *this << OP_DUP << OP_HASH160 << address.GetHash160() << OP_EQUALVERIFY << OP_CHECKSIG; } void SetBitcoinAddress(const std::vector& vchPubKey) { - SetBitcoinAddress(Hash160(vchPubKey)); - } - - bool SetBitcoinAddress(const std::string& strAddress) - { - this->clear(); - uint160 hash160; - if (!AddressToHash160(strAddress, hash160)) - return false; - SetBitcoinAddress(hash160); - return true; + SetBitcoinAddress(CBitcoinAddress(vchPubKey)); } @@ -710,7 +692,7 @@ public: bool IsStandard(const CScript& scriptPubKey); bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey); -bool ExtractHash160(const CScript& scriptPubKey, const CKeyStore* pkeystore, uint160& hash160Ret); +bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* pkeystore, CBitcoinAddress& addressRet); 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 nHashType=0); diff --git a/src/ui.cpp b/src/ui.cpp index fba8ed806..7d06caaec 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -235,11 +235,11 @@ void SetDefaultReceivingAddress(const string& strAddress) return; if (strAddress != pframeMain->m_textCtrlAddress->GetValue()) { - uint160 hash160; - if (!AddressToHash160(strAddress, hash160)) + CBitcoinAddress address(strAddress); + if (!address.IsValid()) return; vector vchPubKey; - if (!pwalletMain->GetPubKey(hash160, vchPubKey)) + if (!pwalletMain->GetPubKey(address, vchPubKey)) return; pwalletMain->SetDefaultKey(vchPubKey); pframeMain->m_textCtrlAddress->SetValue(strAddress); @@ -367,7 +367,7 @@ CMainFrame::CMainFrame(wxWindow* parent) : CMainFrameBase(parent) // Fill your address text box vector vchPubKey; if (CWalletDB(pwalletMain->strWalletFile,"r").ReadDefaultKey(vchPubKey)) - m_textCtrlAddress->SetValue(PubKeyToAddress(vchPubKey)); + m_textCtrlAddress->SetValue(CBitcoinAddress(vchPubKey).ToString()); if (pwalletMain->IsCrypted()) m_menuOptions->Remove(m_menuOptionsEncryptWallet); @@ -704,24 +704,23 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) { if (pwalletMain->IsMine(txout)) { - uint160 hash160; - if (ExtractHash160(txout.scriptPubKey, pwalletMain, hash160)) + CBitcoinAddress address; + if (ExtractAddress(txout.scriptPubKey, pwalletMain, address)) { CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { //strDescription += _("Received payment to "); //strDescription += _("Received with address "); strDescription += _("Received with: "); - string strAddress = Hash160ToAddress(hash160); - map::iterator mi = pwalletMain->mapAddressBook.find(strAddress); + map::iterator mi = pwalletMain->mapAddressBook.find(address); if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty()) { string strLabel = (*mi).second; - strDescription += strAddress.substr(0,12) + "... "; + strDescription += address.ToString().substr(0,12) + "... "; strDescription += "(" + strLabel + ")"; } else - strDescription += strAddress; + strDescription += address.ToString(); } } break; @@ -786,9 +785,9 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) else { // Sent to Bitcoin Address - uint160 hash160; - if (ExtractHash160(txout.scriptPubKey, pwalletMain, hash160)) - strAddress = Hash160ToAddress(hash160); + CBitcoinAddress address; + if (ExtractAddress(txout.scriptPubKey, pwalletMain, address)) + strAddress = address.ToString(); } string strDescription = _("To: "); @@ -1115,7 +1114,7 @@ void CMainFrame::OnPaintListCtrl(wxPaintEvent& event) m_statusBar->SetStatusText(strStatus, 2); // Update receiving address - string strDefaultAddress = PubKeyToAddress(pwalletMain->vchDefaultKey); + string strDefaultAddress = CBitcoinAddress(pwalletMain->vchDefaultKey).ToString(); if (m_textCtrlAddress->GetValue() != strDefaultAddress) m_textCtrlAddress->SetValue(strDefaultAddress); } @@ -1394,7 +1393,7 @@ void CMainFrame::OnButtonNew(wxCommandEvent& event) return; // Generate new key - strAddress = PubKeyToAddress(pwalletMain->GetOrReuseKeyFromPool()); + strAddress = CBitcoinAddress(pwalletMain->GetOrReuseKeyFromPool()).ToString(); if (fWasLocked) pwalletMain->Lock(); @@ -1503,17 +1502,16 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails { if (pwalletMain->IsMine(txout)) { - uint160 hash160; - if (ExtractHash160(txout.scriptPubKey, pwalletMain, hash160)) + CBitcoinAddress address; + if (ExtractAddress(txout.scriptPubKey, pwalletMain, address)) { - string strAddress = Hash160ToAddress(hash160); - if (pwalletMain->mapAddressBook.count(strAddress)) + if (pwalletMain->mapAddressBook.count(address)) { strHTML += string() + _("From: ") + _("unknown") + "
"; strHTML += _("To: "); - strHTML += HtmlEscape(strAddress); - if (!pwalletMain->mapAddressBook[strAddress].empty()) - strHTML += _(" (yours, label: ") + pwalletMain->mapAddressBook[strAddress] + ")"; + strHTML += HtmlEscape(address.ToString()); + if (!pwalletMain->mapAddressBook[address].empty()) + strHTML += _(" (yours, label: ") + pwalletMain->mapAddressBook[address] + ")"; else strHTML += _(" (yours)"); strHTML += "
"; @@ -1589,13 +1587,13 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails if (wtx.mapValue["to"].empty()) { // Offline transaction - uint160 hash160; - if (ExtractHash160(txout.scriptPubKey, pwalletMain, hash160)) + CBitcoinAddress address; + if (ExtractAddress(txout.scriptPubKey, pwalletMain, address)) { - string strAddress = Hash160ToAddress(hash160); + string strAddress = address.ToString(); strHTML += _("To: "); - if (pwalletMain->mapAddressBook.count(strAddress) && !pwalletMain->mapAddressBook[strAddress].empty()) - strHTML += pwalletMain->mapAddressBook[strAddress] + " "; + if (pwalletMain->mapAddressBook.count(address) && !pwalletMain->mapAddressBook[address].empty()) + strHTML += pwalletMain->mapAddressBook[address] + " "; strHTML += strAddress; strHTML += "
"; } @@ -2156,8 +2154,8 @@ void CSendDialog::OnButtonSend(wxCommandEvent& event) } // Parse bitcoin address - uint160 hash160; - bool fBitcoinAddress = AddressToHash160(strAddress, hash160); + CBitcoinAddress address(strAddress); + bool fBitcoinAddress = address.IsValid(); if (fBitcoinAddress) { @@ -2170,7 +2168,7 @@ void CSendDialog::OnButtonSend(wxCommandEvent& event) // Send to bitcoin address CScript scriptPubKey; - scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG; + scriptPubKey.SetBitcoinAddress(address); string strError = pwalletMain->SendMoney(scriptPubKey, nValue, wtx, true); if (strError == "") @@ -2214,7 +2212,7 @@ void CSendDialog::OnButtonSend(wxCommandEvent& event) } CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) - if (!pwalletMain->mapAddressBook.count(strAddress)) + if (!pwalletMain->mapAddressBook.count(address)) pwalletMain->SetAddressBookName(strAddress, ""); EndModal(true); @@ -2626,15 +2624,14 @@ CAddressBookDialog::CAddressBookDialog(wxWindow* parent, const wxString& strInit CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) { string strDefaultReceiving = (string)pframeMain->m_textCtrlAddress->GetValue(); - BOOST_FOREACH(const PAIRTYPE(string, string)& item, pwalletMain->mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook) { - string strAddress = item.first; + const CBitcoinAddress& address = item.first; string strName = item.second; - uint160 hash160; - bool fMine = (AddressToHash160(strAddress, hash160) && pwalletMain->HaveKey(hash160)); + bool fMine = pwalletMain->HaveKey(address); wxListCtrl* plistCtrl = fMine ? m_listCtrlReceiving : m_listCtrlSending; - int nIndex = InsertLine(plistCtrl, strName, strAddress); - if (strAddress == (fMine ? strDefaultReceiving : string(strInitSelected))) + int nIndex = InsertLine(plistCtrl, strName, address.ToString()); + if (address.ToString() == (fMine ? strDefaultReceiving : string(strInitSelected))) plistCtrl->SetItemState(nIndex, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED); } } @@ -2741,8 +2738,8 @@ void CAddressBookDialog::OnButtonCopy(wxCommandEvent& event) bool CAddressBookDialog::CheckIfMine(const string& strAddress, const string& strTitle) { - uint160 hash160; - bool fMine = (AddressToHash160(strAddress, hash160) && pwalletMain->HaveKey(hash160)); + CBitcoinAddress address(strAddress); + bool fMine = address.IsValid() && pwalletMain->HaveKey(address); if (fMine) wxMessageBox(_("This is one of your own addresses for receiving payments and cannot be entered in the address book. "), strTitle); return fMine; @@ -2828,7 +2825,7 @@ void CAddressBookDialog::OnButtonNew(wxCommandEvent& event) return; // Generate new key - strAddress = PubKeyToAddress(pwalletMain->GetOrReuseKeyFromPool()); + strAddress = CBitcoinAddress(pwalletMain->GetOrReuseKeyFromPool()).ToString(); if (fWasLocked) pwalletMain->Lock(); diff --git a/src/wallet.cpp b/src/wallet.cpp index ba5018639..74f8b9f67 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -270,7 +270,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn) if (txout.scriptPubKey == scriptDefaultKey) { SetDefaultKey(GetOrReuseKeyFromPool()); - SetAddressBookName(PubKeyToAddress(vchDefaultKey), ""); + SetAddressBookName(CBitcoinAddress(vchDefaultKey), ""); } } @@ -406,8 +406,8 @@ int CWalletTx::GetRequestCount() const return nRequests; } -void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, list >& listReceived, - list >& listSent, int64& nFee, string& strSentAccount) const +void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, list >& listReceived, + list >& listSent, int64& nFee, string& strSentAccount) const { nGeneratedImmature = nGeneratedMature = nFee = 0; listReceived.clear(); @@ -435,12 +435,9 @@ void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, l // but non-standard clients might (so return a list of address/amount pairs) BOOST_FOREACH(const CTxOut& txout, vout) { - string address; - uint160 hash160; + CBitcoinAddress address; vector vchPubKey; - if (ExtractHash160(txout.scriptPubKey, pwallet, hash160)) - address = Hash160ToAddress(hash160); - else + if (!ExtractAddress(txout.scriptPubKey, pwallet, address)) { printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n", this->GetHash().ToString().c_str()); @@ -468,25 +465,25 @@ void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nGenerated, i int64 allGeneratedImmature, allGeneratedMature, allFee; allGeneratedImmature = allGeneratedMature = allFee = 0; string strSentAccount; - list > listReceived; - list > listSent; + list > listReceived; + list > listSent; GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount); if (strAccount == "") nGenerated = allGeneratedMature; if (strAccount == strSentAccount) { - BOOST_FOREACH(const PAIRTYPE(string,int64)& s, listSent) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress,int64)& s, listSent) nSent += s.second; nFee = allFee; } CRITICAL_BLOCK(pwallet->cs_mapAddressBook) { - BOOST_FOREACH(const PAIRTYPE(string,int64)& r, listReceived) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress,int64)& r, listReceived) { if (pwallet->mapAddressBook.count(r.first)) { - map::const_iterator mi = pwallet->mapAddressBook.find(r.first); + map::const_iterator mi = pwallet->mapAddressBook.find(r.first); if (mi != pwallet->mapAddressBook.end() && (*mi).second == strAccount) nReceived += r.second; } @@ -955,7 +952,7 @@ bool CWallet::CreateTransaction(const vector >& vecSend, CW // Fill a vout to ourself, using same address type as the payment CScript scriptChange; - if (vecSend[0].first.GetBitcoinAddressHash160() != 0) + if (vecSend[0].first.GetBitcoinAddress().IsValid()) scriptChange.SetBitcoinAddress(vchPubKey); else scriptChange << vchPubKey << OP_CHECKSIG; @@ -1104,7 +1101,7 @@ string CWallet::SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, // requires cs_main lock -string CWallet::SendMoneyToBitcoinAddress(string strAddress, int64 nValue, CWalletTx& wtxNew, bool fAskFee) +string CWallet::SendMoneyToBitcoinAddress(const CBitcoinAddress& address, int64 nValue, CWalletTx& wtxNew, bool fAskFee) { // Check amount if (nValue <= 0) @@ -1114,8 +1111,7 @@ string CWallet::SendMoneyToBitcoinAddress(string strAddress, int64 nValue, CWall // Parse bitcoin address CScript scriptPubKey; - if (!scriptPubKey.SetBitcoinAddress(strAddress)) - return _("Invalid bitcoin address"); + scriptPubKey.SetBitcoinAddress(address); return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee); } @@ -1139,7 +1135,7 @@ int CWallet::LoadWallet(bool& fFirstRunRet) RandAddSeedPerfmon(); SetDefaultKey(GetOrReuseKeyFromPool()); - if (!SetAddressBookName(PubKeyToAddress(vchDefaultKey), "")) + if (!SetAddressBookName(CBitcoinAddress(vchDefaultKey), "")) return DB_LOAD_FAIL; } @@ -1148,20 +1144,20 @@ int CWallet::LoadWallet(bool& fFirstRunRet) } -bool CWallet::SetAddressBookName(const string& strAddress, const string& strName) +bool CWallet::SetAddressBookName(const CBitcoinAddress& address, const string& strName) { - mapAddressBook[strAddress] = strName; + mapAddressBook[address] = strName; if (!fFileBacked) return false; - return CWalletDB(strWalletFile).WriteName(strAddress, strName); + return CWalletDB(strWalletFile).WriteName(address.ToString(), strName); } -bool CWallet::DelAddressBookName(const string& strAddress) +bool CWallet::DelAddressBookName(const CBitcoinAddress& address) { - mapAddressBook.erase(strAddress); + mapAddressBook.erase(address); if (!fFileBacked) return false; - return CWalletDB(strWalletFile).EraseName(strAddress); + return CWalletDB(strWalletFile).EraseName(address.ToString()); } diff --git a/src/wallet.h b/src/wallet.h index 277d0b1e7..51dfa5df5 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -53,7 +53,7 @@ public: std::map mapRequestCount; mutable CCriticalSection cs_mapRequestCount; - std::map mapAddressBook; + std::map mapAddressBook; mutable CCriticalSection cs_mapAddressBook; std::vector vchDefaultKey; @@ -81,7 +81,7 @@ public: bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey); bool BroadcastTransaction(CWalletTx& wtxNew); std::string SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false); - std::string SendMoneyToBitcoinAddress(std::string strAddress, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false); + std::string SendMoneyToBitcoinAddress(const CBitcoinAddress& address, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false); bool TopUpKeyPool(); void ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool); @@ -104,10 +104,10 @@ public: } bool IsChange(const CTxOut& txout) const { - uint160 hash160; - if (ExtractHash160(txout.scriptPubKey, this, hash160)) + CBitcoinAddress address; + if (ExtractAddress(txout.scriptPubKey, this, address)) CRITICAL_BLOCK(cs_mapAddressBook) - if (!mapAddressBook.count(Hash160ToAddress(hash160))) + if (!mapAddressBook.count(address)) return true; return false; } @@ -171,10 +171,10 @@ public: // bool BackupWallet(const std::string& strDest); // requires cs_mapAddressBook lock - bool SetAddressBookName(const std::string& strAddress, const std::string& strName); + bool SetAddressBookName(const CBitcoinAddress& address, const std::string& strName); // requires cs_mapAddressBook lock - bool DelAddressBookName(const std::string& strAddress); + bool DelAddressBookName(const CBitcoinAddress& address); void UpdatedTransaction(const uint256 &hashTx) { @@ -464,8 +464,8 @@ public: return nChangeCached; } - void GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, std::list >& listReceived, - std::list >& listSent, int64& nFee, std::string& strSentAccount) const; + void GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, std::list >& listReceived, + std::list >& listSent, int64& nFee, std::string& strSentAccount) const; void GetAccountAmounts(const std::string& strAccount, int64& nGenerated, int64& nReceived, int64& nSent, int64& nFee) const; From cb61b8dc4ce2f24332fdfe9b47e5f87905a9da71 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Mon, 11 Jul 2011 11:09:00 +0200 Subject: [PATCH 3/4] split off CBase58Data from CBitcoinAddress Split off features unrelated to addresses from CBitcoinAddress to CBase58Data, so they can be reused. --- src/base58.h | 106 ++++++++++++++++++++++++++++++------------------- src/script.cpp | 4 +- 2 files changed, 67 insertions(+), 43 deletions(-) diff --git a/src/base58.h b/src/base58.h index 816193eae..985b0447c 100644 --- a/src/base58.h +++ b/src/base58.h @@ -159,25 +159,40 @@ inline bool DecodeBase58Check(const std::string& str, std::vector -class CBitcoinAddress +class CBase58Data { protected: unsigned char nVersion; std::vector vchData; -public: - bool SetAddress(const uint160& hash160) + CBase58Data() { - nVersion = fTestNet ? 111 : 0; - vchData.resize(20); - memcpy(&vchData[0], &hash160, 20); - return true; + nVersion = 0; + vchData.clear(); + } + + ~CBase58Data() + { + memset(&vchData[0], 0, vchData.size()); + } + + void SetData(int nVersionIn, const void* pdata, size_t nSize) + { + nVersion = nVersionIn; + vchData.resize(nSize); + memcpy(&vchData[0], pdata, nSize); + } + + void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend) + { + SetData(nVersionIn, (void*)pbegin, pend - pbegin); } - bool SetAddress(const char* pszAddress) +public: + bool SetString(const char* psz) { std::vector vchTemp; - DecodeBase58Check(pszAddress, vchTemp); + DecodeBase58Check(psz, vchTemp); if (vchTemp.empty()) { vchData.clear(); @@ -187,17 +202,50 @@ public: nVersion = vchTemp[0]; vchData.resize(vchTemp.size() - 1); memcpy(&vchData[0], &vchTemp[1], vchData.size()); + memset(&vchTemp[0], 0, vchTemp.size()); return true; } - bool SetAddress(const std::string& strAddress) + bool SetString(const std::string& str) + { + return SetString(str.c_str()); + } + + std::string ToString() const { - return SetAddress(strAddress.c_str()); + std::vector vch(1, nVersion); + vch.insert(vch.end(), vchData.begin(), vchData.end()); + return EncodeBase58Check(vch); } - bool SetAddress(const std::vector& vchPubKey) + int CompareTo(const CBase58Data& b58) const { - return SetAddress(Hash160(vchPubKey)); + if (nVersion < b58.nVersion) return -1; + if (nVersion < b58.nVersion) return 1; + if (vchData < b58.vchData) return -1; + if (vchData > b58.vchData) return 1; + return 0; + } + + bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; } + bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; } + bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; } + bool operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; } + bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; } +}; + + +class CBitcoinAddress : public CBase58Data +{ +public: + bool SetHash160(const uint160& hash160) + { + SetData(fTestNet ? 111 : 0, &hash160, 20); + } + + bool SetPubKey(const std::vector& vchPubKey) + { + return SetHash160(Hash160(vchPubKey)); } bool IsValid() const @@ -221,35 +269,26 @@ public: CBitcoinAddress() { - nVersion = 0; - vchData.clear(); } CBitcoinAddress(uint160 hash160In) { - SetAddress(hash160In); + SetHash160(hash160In); } CBitcoinAddress(const std::vector& vchPubKey) { - SetAddress(vchPubKey); + SetPubKey(vchPubKey); } CBitcoinAddress(const std::string& strAddress) { - SetAddress(strAddress); + SetString(strAddress); } CBitcoinAddress(const char* pszAddress) { - SetAddress(pszAddress); - } - - std::string ToString() const - { - std::vector vch(1, nVersion); - vch.insert(vch.end(), vchData.begin(), vchData.end()); - return EncodeBase58Check(vch); + SetString(pszAddress); } uint160 GetHash160() const @@ -259,21 +298,6 @@ public: memcpy(&hash160, &vchData[0], 20); return hash160; } - - int CompareTo(const CBitcoinAddress& address) const - { - if (nVersion < address.nVersion) return -1; - if (nVersion < address.nVersion) return 1; - if (vchData < address.vchData) return -1; - if (vchData > address.vchData) return 1; - return 0; - } - - bool operator==(const CBitcoinAddress& address) const { return CompareTo(address) == 0; } - bool operator<=(const CBitcoinAddress& address) const { return CompareTo(address) <= 0; } - bool operator>=(const CBitcoinAddress& address) const { return CompareTo(address) >= 0; } - bool operator< (const CBitcoinAddress& address) const { return CompareTo(address) < 0; } - bool operator> (const CBitcoinAddress& address) const { return CompareTo(address) > 0; } }; #endif diff --git a/src/script.cpp b/src/script.cpp index f91760084..652240f68 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -1135,9 +1135,9 @@ bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBit { uint160 hash160; if (item.first == OP_PUBKEY) - addressRet.SetAddress(item.second); + addressRet.SetPubKey(item.second); else if (item.first == OP_PUBKEYHASH) - addressRet.SetAddress(uint160(item.second)); + addressRet.SetHash160((uint160)item.second); if (keystore == NULL || keystore->HaveKey(addressRet)) return true; } From 03f8b545655bd1a6ee6db7e2e6d14586900cacb1 Mon Sep 17 00:00:00 2001 From: Abraham Jewowich Date: Mon, 18 Jul 2011 06:26:54 -0700 Subject: [PATCH 4/4] Fix bug with accessing vchData[0] when vchData is empty. Fix typo in CBase58Data::CompareTo --- src/base58.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/base58.h b/src/base58.h index 985b0447c..266412c86 100644 --- a/src/base58.h +++ b/src/base58.h @@ -173,14 +173,16 @@ protected: ~CBase58Data() { - memset(&vchData[0], 0, vchData.size()); + if (!vchData.empty()) + memset(&vchData[0], 0, vchData.size()); } void SetData(int nVersionIn, const void* pdata, size_t nSize) { nVersion = nVersionIn; vchData.resize(nSize); - memcpy(&vchData[0], pdata, nSize); + if (!vchData.empty()) + memcpy(&vchData[0], pdata, nSize); } void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend) @@ -201,7 +203,8 @@ public: } nVersion = vchTemp[0]; vchData.resize(vchTemp.size() - 1); - memcpy(&vchData[0], &vchTemp[1], vchData.size()); + if (!vchData.empty()) + memcpy(&vchData[0], &vchTemp[1], vchData.size()); memset(&vchTemp[0], 0, vchTemp.size()); return true; } @@ -221,7 +224,7 @@ public: int CompareTo(const CBase58Data& b58) const { if (nVersion < b58.nVersion) return -1; - if (nVersion < b58.nVersion) return 1; + if (nVersion > b58.nVersion) return 1; if (vchData < b58.vchData) return -1; if (vchData > b58.vchData) return 1; return 0;