From fd61d6f5068cf92d34569862b4225f177049a4f0 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Mon, 14 May 2012 19:07:52 +0200 Subject: Encapsulate public keys in CPubKey --- src/wallet.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/wallet.cpp') diff --git a/src/wallet.cpp b/src/wallet.cpp index 62f663c0d..2f8d7c05a 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -16,7 +16,7 @@ using namespace std; // mapWallet // -std::vector CWallet::GenerateNewKey() +CPubKey CWallet::GenerateNewKey() { bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets @@ -44,7 +44,7 @@ bool CWallet::AddKey(const CKey& key) return true; } -bool CWallet::AddCryptedKey(const vector &vchPubKey, const vector &vchCryptedSecret) +bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const vector &vchCryptedSecret) { if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret)) return false; @@ -366,7 +366,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn) { if (txout.scriptPubKey == scriptDefaultKey) { - std::vector newDefaultKey; + CPubKey newDefaultKey; if (GetKeyFromPool(newDefaultKey, false)) { SetDefaultKey(newDefaultKey); @@ -1095,7 +1095,7 @@ bool CWallet::CreateTransaction(const vector >& vecSend, CW // post-backup change. // Reserve a new key pair from key pool - vector vchPubKey = reservekey.GetReservedKey(); + CPubKey vchPubKey = reservekey.GetReservedKey(); // assert(mapKeys.count(vchPubKey)); // Fill a vout to ourself @@ -1278,7 +1278,7 @@ int CWallet::LoadWallet(bool& fFirstRunRet) if (nLoadWalletRet != DB_LOAD_OK) return nLoadWalletRet; - fFirstRunRet = vchDefaultKey.empty(); + fFirstRunRet = !vchDefaultKey.IsValid(); CreateThread(ThreadFlushWalletDB, &strWalletFile); return DB_LOAD_OK; @@ -1332,7 +1332,7 @@ bool CWallet::GetTransaction(const uint256 &hashTx, CWalletTx& wtx) return false; } -bool CWallet::SetDefaultKey(const std::vector &vchPubKey) +bool CWallet::SetDefaultKey(const CPubKey &vchPubKey) { if (fFileBacked) { @@ -1408,7 +1408,7 @@ bool CWallet::TopUpKeyPool() void CWallet::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool) { nIndex = -1; - keypool.vchPubKey.clear(); + keypool.vchPubKey = CPubKey(); { LOCK(cs_wallet); @@ -1425,9 +1425,9 @@ void CWallet::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool) setKeyPool.erase(setKeyPool.begin()); if (!walletdb.ReadPool(nIndex, keypool)) throw runtime_error("ReserveKeyFromKeyPool() : read failed"); - if (!HaveKey(Hash160(keypool.vchPubKey))) + if (!HaveKey(keypool.vchPubKey.GetID())) throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool"); - assert(!keypool.vchPubKey.empty()); + assert(keypool.vchPubKey.IsValid()); printf("keypool reserve %"PRI64d"\n", nIndex); } } @@ -1468,7 +1468,7 @@ void CWallet::ReturnKey(int64 nIndex) printf("keypool return %"PRI64d"\n", nIndex); } -bool CWallet::GetKeyFromPool(vector& result, bool fAllowReuse) +bool CWallet::GetKeyFromPool(CPubKey& result, bool fAllowReuse) { int64 nIndex = 0; CKeyPool keypool; @@ -1477,7 +1477,7 @@ bool CWallet::GetKeyFromPool(vector& result, bool fAllowReuse) ReserveKeyFromKeyPool(nIndex, keypool); if (nIndex == -1) { - if (fAllowReuse && !vchDefaultKey.empty()) + if (fAllowReuse && vchDefaultKey.IsValid()) { result = vchDefaultKey; return true; @@ -1503,7 +1503,7 @@ int64 CWallet::GetOldestKeyPoolTime() return keypool.nTime; } -vector CReserveKey::GetReservedKey() +CPubKey CReserveKey::GetReservedKey() { if (nIndex == -1) { @@ -1517,7 +1517,7 @@ vector CReserveKey::GetReservedKey() vchPubKey = pwallet->vchDefaultKey; } } - assert(!vchPubKey.empty()); + assert(vchPubKey.IsValid()); return vchPubKey; } @@ -1526,7 +1526,7 @@ void CReserveKey::KeepKey() if (nIndex != -1) pwallet->KeepKey(nIndex); nIndex = -1; - vchPubKey.clear(); + vchPubKey = CPubKey(); } void CReserveKey::ReturnKey() @@ -1534,7 +1534,7 @@ void CReserveKey::ReturnKey() if (nIndex != -1) pwallet->ReturnKey(nIndex); nIndex = -1; - vchPubKey.clear(); + vchPubKey = CPubKey(); } void CWallet::GetAllReserveAddresses(set& setAddress) @@ -1550,7 +1550,7 @@ void CWallet::GetAllReserveAddresses(set& setAddress) if (!walletdb.ReadPool(id, keypool)) throw runtime_error("GetAllReserveKeyHashes() : read failed"); CBitcoinAddress address(keypool.vchPubKey); - assert(!keypool.vchPubKey.empty()); + assert(keypool.vchPubKey.IsValid()); if (!HaveKey(address)) throw runtime_error("GetAllReserveKeyHashes() : unknown key in key pool"); setAddress.insert(address); -- cgit v1.2.3 From 1025440184ef100a22d07c7bb543ee45cf169d64 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Mon, 14 May 2012 23:44:52 +0200 Subject: Refactor: split CKeyID/CScriptID/CTxDestination from CBitcoinAddress This introduces internal types: * CKeyID: reference (hash160) of a key * CScriptID: reference (hash160) of a script * CTxDestination: a boost::variant of the former two CBitcoinAddress is retrofitted to be a Base58 encoding of a CTxDestination. This allows all internal code to only use the internal types, and only have RPC and GUI depend on the base58 code. Furthermore, the header dependencies are a lot saner now. base58.h is at the top (right below rpc and gui) instead of at the bottom. For the rest: wallet -> script -> keystore -> key. Only keystore still requires a forward declaration of CScript. Solving that would require splitting script into two layers. --- src/wallet.cpp | 56 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) (limited to 'src/wallet.cpp') diff --git a/src/wallet.cpp b/src/wallet.cpp index 2f8d7c05a..3c4aeb4ea 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -7,6 +7,7 @@ #include "walletdb.h" #include "crypter.h" #include "ui_interface.h" +#include "base58.h" using namespace std; @@ -361,7 +362,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn) #ifndef QT_GUI // If default receiving address gets used, replace it with a new one CScript scriptDefaultKey; - scriptDefaultKey.SetBitcoinAddress(vchDefaultKey); + scriptDefaultKey.SetDestination(vchDefaultKey.GetID()); BOOST_FOREACH(const CTxOut& txout, wtx.vout) { if (txout.scriptPubKey == scriptDefaultKey) @@ -370,7 +371,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn) if (GetKeyFromPool(newDefaultKey, false)) { SetDefaultKey(newDefaultKey); - SetAddressBookName(CBitcoinAddress(vchDefaultKey), ""); + SetAddressBookName(vchDefaultKey.GetID(), ""); } } } @@ -455,7 +456,7 @@ int64 CWallet::GetDebit(const CTxIn &txin) const bool CWallet::IsChange(const CTxOut& txout) const { - CBitcoinAddress address; + CTxDestination address; // TODO: fix handling of 'change' outputs. The assumption is that any // payment to a TX_PUBKEYHASH that is mine but isn't in the address book @@ -464,7 +465,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, address) && HaveKey(address)) + if (ExtractDestination(txout.scriptPubKey, address) && ::IsMine(*this, address)) { LOCK(cs_wallet); if (!mapAddressBook.count(address)) @@ -517,8 +518,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(); @@ -545,13 +546,12 @@ void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, l // Sent/received. BOOST_FOREACH(const CTxOut& txout, vout) { - CBitcoinAddress address; + CTxDestination address; vector vchPubKey; - if (!ExtractAddress(txout.scriptPubKey, address)) + if (!ExtractDestination(txout.scriptPubKey, address)) { printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n", this->GetHash().ToString().c_str()); - address = " unknown "; } // Don't report 'change' txouts @@ -575,25 +575,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(CBitcoinAddress,int64)& s, listSent) + BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64)& s, listSent) nSent += s.second; nFee = allFee; } { LOCK(pwallet->cs_wallet); - BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress,int64)& r, listReceived) + BOOST_FOREACH(const PAIRTYPE(CTxDestination,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; } @@ -1102,7 +1102,7 @@ bool CWallet::CreateTransaction(const vector >& vecSend, CW // TODO: pass in scriptChange instead of reservekey so // change transaction isn't always pay-to-bitcoin-address CScript scriptChange; - scriptChange.SetBitcoinAddress(vchPubKey); + scriptChange.SetDestination(vchPubKey.GetID()); // Insert change txn at random position: vector::iterator position = wtxNew.vout.begin()+GetRandInt(wtxNew.vout.size()); @@ -1240,7 +1240,7 @@ string CWallet::SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, -string CWallet::SendMoneyToBitcoinAddress(const CBitcoinAddress& address, int64 nValue, CWalletTx& wtxNew, bool fAskFee) +string CWallet::SendMoneyToDestination(const CTxDestination& address, int64 nValue, CWalletTx& wtxNew, bool fAskFee) { // Check amount if (nValue <= 0) @@ -1250,7 +1250,7 @@ string CWallet::SendMoneyToBitcoinAddress(const CBitcoinAddress& address, int64 // Parse Bitcoin address CScript scriptPubKey; - scriptPubKey.SetBitcoinAddress(address); + scriptPubKey.SetDestination(address); return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee); } @@ -1285,23 +1285,23 @@ int CWallet::LoadWallet(bool& fFirstRunRet) } -bool CWallet::SetAddressBookName(const CBitcoinAddress& address, const string& strName) +bool CWallet::SetAddressBookName(const CTxDestination& address, const string& strName) { - std::map::iterator mi = mapAddressBook.find(address); + std::map::iterator mi = mapAddressBook.find(address); mapAddressBook[address] = strName; - NotifyAddressBookChanged(this, address.ToString(), strName, HaveKey(address), (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED); + NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address), (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED); if (!fFileBacked) return false; - return CWalletDB(strWalletFile).WriteName(address.ToString(), strName); + return CWalletDB(strWalletFile).WriteName(CBitcoinAddress(address).ToString(), strName); } -bool CWallet::DelAddressBookName(const CBitcoinAddress& address) +bool CWallet::DelAddressBookName(const CTxDestination& address) { mapAddressBook.erase(address); - NotifyAddressBookChanged(this, address.ToString(), "", HaveKey(address), CT_DELETED); + NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), CT_DELETED); if (!fFileBacked) return false; - return CWalletDB(strWalletFile).EraseName(address.ToString()); + return CWalletDB(strWalletFile).EraseName(CBitcoinAddress(address).ToString()); } @@ -1537,7 +1537,7 @@ void CReserveKey::ReturnKey() vchPubKey = CPubKey(); } -void CWallet::GetAllReserveAddresses(set& setAddress) +void CWallet::GetAllReserveKeys(set& setAddress) { setAddress.clear(); @@ -1549,11 +1549,11 @@ void CWallet::GetAllReserveAddresses(set& setAddress) CKeyPool keypool; if (!walletdb.ReadPool(id, keypool)) throw runtime_error("GetAllReserveKeyHashes() : read failed"); - CBitcoinAddress address(keypool.vchPubKey); assert(keypool.vchPubKey.IsValid()); - if (!HaveKey(address)) + CKeyID keyID = keypool.vchPubKey.GetID(); + if (!HaveKey(keyID)) throw runtime_error("GetAllReserveKeyHashes() : unknown key in key pool"); - setAddress.insert(address); + setAddress.insert(keyID); } } -- cgit v1.2.3