Permalink
Browse files

Merge pull request #403 from sipa/cbitcoinaddress

keys indexed by address + introduced CBitcoinaddress
  • Loading branch information...
2 parents b6d19ae + ca40e58 commit a9ba47101a46533ff0418d6868ebc9bf5c889818 Jeff Garzik committed Jul 24, 2011
Showing with 353 additions and 311 deletions.
  1. +133 −37 src/base58.h
  2. +0 −1 src/init.cpp
  3. +5 −0 src/key.h
  4. +40 −18 src/keystore.cpp
  5. +17 −13 src/keystore.h
  6. +0 −3 src/main.cpp
  7. +0 −2 src/main.h
  8. +66 −80 src/rpc.cpp
  9. +16 −53 src/script.cpp
  10. +6 −25 src/script.h
  11. +39 −41 src/ui.cpp
  12. +22 −29 src/wallet.cpp
  13. +9 −9 src/wallet.h
View
@@ -159,52 +159,148 @@ inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>
-#define ADDRESSVERSION ((unsigned char)(fTestNet ? 111 : 0))
-
-inline std::string Hash160ToAddress(uint160 hash160)
+class CBase58Data
{
- // add 1-byte version number to the front
- std::vector<unsigned char> vch(1, ADDRESSVERSION);
- vch.insert(vch.end(), UBEGIN(hash160), UEND(hash160));
- return EncodeBase58Check(vch);
-}
+protected:
+ unsigned char nVersion;
+ std::vector<unsigned char> vchData;
-inline bool AddressToHash160(const char* psz, uint160& hash160Ret)
-{
- std::vector<unsigned char> 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);
-}
+ CBase58Data()
+ {
+ nVersion = 0;
+ vchData.clear();
+ }
-inline bool AddressToHash160(const std::string& str, uint160& hash160Ret)
-{
- return AddressToHash160(str.c_str(), hash160Ret);
-}
+ ~CBase58Data()
+ {
+ if (!vchData.empty())
+ memset(&vchData[0], 0, vchData.size());
+ }
-inline bool IsValidBitcoinAddress(const char* psz)
-{
- uint160 hash160;
- return AddressToHash160(psz, hash160);
-}
+ void SetData(int nVersionIn, const void* pdata, size_t nSize)
+ {
+ nVersion = nVersionIn;
+ vchData.resize(nSize);
+ if (!vchData.empty())
+ memcpy(&vchData[0], pdata, nSize);
+ }
-inline bool IsValidBitcoinAddress(const std::string& str)
-{
- return IsValidBitcoinAddress(str.c_str());
-}
+ void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend)
+ {
+ SetData(nVersionIn, (void*)pbegin, pend - pbegin);
+ }
+
+public:
+ bool SetString(const char* psz)
+ {
+ std::vector<unsigned char> vchTemp;
+ DecodeBase58Check(psz, vchTemp);
+ if (vchTemp.empty())
+ {
+ vchData.clear();
+ nVersion = 0;
+ return false;
+ }
+ nVersion = vchTemp[0];
+ vchData.resize(vchTemp.size() - 1);
+ if (!vchData.empty())
+ memcpy(&vchData[0], &vchTemp[1], vchData.size());
+ memset(&vchTemp[0], 0, vchTemp.size());
+ return true;
+ }
+
+ bool SetString(const std::string& str)
+ {
+ return SetString(str.c_str());
+ }
+ std::string ToString() const
+ {
+ std::vector<unsigned char> vch(1, nVersion);
+ vch.insert(vch.end(), vchData.begin(), vchData.end());
+ return EncodeBase58Check(vch);
+ }
+
+ int CompareTo(const CBase58Data& b58) const
+ {
+ 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; }
+};
-inline std::string PubKeyToAddress(const std::vector<unsigned char>& vchPubKey)
+class CBitcoinAddress : public CBase58Data
{
- return Hash160ToAddress(Hash160(vchPubKey));
-}
+public:
+ bool SetHash160(const uint160& hash160)
+ {
+ SetData(fTestNet ? 111 : 0, &hash160, 20);
+ }
+
+ bool SetPubKey(const std::vector<unsigned char>& vchPubKey)
+ {
+ return SetHash160(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()
+ {
+ }
+
+ CBitcoinAddress(uint160 hash160In)
+ {
+ SetHash160(hash160In);
+ }
+
+ CBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
+ {
+ SetPubKey(vchPubKey);
+ }
+
+ CBitcoinAddress(const std::string& strAddress)
+ {
+ SetString(strAddress);
+ }
+
+ CBitcoinAddress(const char* pszAddress)
+ {
+ SetString(pszAddress);
+ }
+
+ uint160 GetHash160() const
+ {
+ assert(vchData.size() == 20);
+ uint160 hash160;
+ memcpy(&hash160, &vchData[0], 20);
+ return hash160;
+ }
+};
#endif
View
@@ -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());
View
@@ -220,6 +220,11 @@ class CKey
return false;
return true;
}
+
+ CBitcoinAddress GetAddress() const
+ {
+ return CBitcoinAddress(GetPubKey());
+ }
};
#endif
View
@@ -16,14 +16,19 @@ std::vector<unsigned char> CKeyStore::GenerateNewKey()
return key.GetPubKey();
}
+bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
+{
+ CKey key;
+ if (!GetKey(address, 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[key.GetAddress()] = key.GetSecret();
return true;
}
@@ -44,11 +49,11 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
if (!SetCrypted())
return false;
- std::map<std::vector<unsigned char>, std::vector<unsigned char> >::const_iterator mi = mapCryptedKeys.begin();
+ CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
for (; mi != mapCryptedKeys.end(); ++mi)
{
- const std::vector<unsigned char> &vchPubKey = (*mi).first;
- const std::vector<unsigned char> &vchCryptedSecret = (*mi).second;
+ const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
+ const std::vector<unsigned char> &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<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
{
- CRITICAL_BLOCK(cs_mapPubKeys)
CRITICAL_BLOCK(cs_KeyStore)
{
if (!SetCrypted())
return false;
- mapCryptedKeys[vchPubKey] = vchCryptedSecret;
- mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
+ mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
}
return true;
}
-bool CCryptoKeyStore::GetPrivKey(const std::vector<unsigned char> &vchPubKey, CKey& keyOut) const
+bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
{
CRITICAL_BLOCK(cs_vMasterKey)
{
if (!IsCrypted())
- return CBasicKeyStore::GetPrivKey(vchPubKey, keyOut);
+ return CBasicKeyStore::GetKey(address, keyOut);
- std::map<std::vector<unsigned char>, std::vector<unsigned char> >::const_iterator mi = mapCryptedKeys.find(vchPubKey);
+ CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
if (mi != mapCryptedKeys.end())
{
- const std::vector<unsigned char> &vchCryptedSecret = (*mi).second;
+ const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
+ const std::vector<unsigned char> &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<unsigned char> &vchPubKey, CK
return false;
}
+bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
+{
+ CRITICAL_BLOCK(cs_vMasterKey)
+ {
+ if (!IsCrypted())
+ return CKeyStore::GetPubKey(address, vchPubKeyOut);
+
+ CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
+ 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<unsigned char> vchPubKey = key.GetPubKey();
std::vector<unsigned char> 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();
View
@@ -12,12 +12,13 @@ class CKeyStore
mutable CCriticalSection cs_KeyStore;
virtual bool AddKey(const CKey& key) =0;
- virtual bool HaveKey(const std::vector<unsigned char> &vchPubKey) const =0;
- virtual bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CKey& keyOut) const =0;
+ 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<unsigned char>& vchPubKeyOut) const;
virtual std::vector<unsigned char> GenerateNewKey();
};
-typedef std::map<std::vector<unsigned char>, CPrivKey> KeyMap;
+typedef std::map<CBitcoinAddress, CSecret> KeyMap;
class CBasicKeyStore : public CKeyStore
{
@@ -26,26 +27,28 @@ class CBasicKeyStore : public CKeyStore
public:
bool AddKey(const CKey& key);
- bool HaveKey(const std::vector<unsigned char> &vchPubKey) const
+ bool HaveKey(const CBitcoinAddress &address) const
{
- return (mapKeys.count(vchPubKey) > 0);
+ return (mapKeys.count(address) > 0);
}
- bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CKey& keyOut) const
+ bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
{
- std::map<std::vector<unsigned char>, CPrivKey>::const_iterator mi = mapKeys.find(vchPubKey);
+ KeyMap::const_iterator mi = mapKeys.find(address);
if (mi != mapKeys.end())
{
- keyOut.SetPrivKey((*mi).second);
+ keyOut.SetSecret((*mi).second);
return true;
}
return false;
}
};
+typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
+
class CCryptoKeyStore : public CBasicKeyStore
{
private:
- std::map<std::vector<unsigned char>, std::vector<unsigned char> > mapCryptedKeys;
+ CryptedKeyMap mapCryptedKeys;
CKeyingMaterial vMasterKey;
@@ -103,13 +106,14 @@ class CCryptoKeyStore : public CBasicKeyStore
virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
std::vector<unsigned char> GenerateNewKey();
bool AddKey(const CKey& key);
- bool HaveKey(const std::vector<unsigned char> &vchPubKey) const
+ bool HaveKey(const CBitcoinAddress &address) const
{
if (!IsCrypted())
- return CBasicKeyStore::HaveKey(vchPubKey);
- return mapCryptedKeys.count(vchPubKey) > 0;
+ return CBasicKeyStore::HaveKey(address);
+ return mapCryptedKeys.count(address) > 0;
}
- bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CKey& keyOut) const;
+ bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
+ bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
};
#endif
Oops, something went wrong.

0 comments on commit a9ba471

Please sign in to comment.