diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2011-07-05 16:42:32 +0200 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2011-07-17 12:07:59 +0200 |
commit | 03fbd7904903928b0d1c8542a3d597aaf5bdd31b (patch) | |
tree | 562344a9b651ea1b2cc3f7ffd348377e5bed6090 /src/keystore.cpp | |
parent | 133ccbe4087514501dec1f7496c62489437f0db8 (diff) |
get rid of mapPubKeys
Make CKeyStore's interface work on uint160's instead of pubkeys, so
no separate global mapPubKeys is necessary anymore.
Diffstat (limited to 'src/keystore.cpp')
-rw-r--r-- | src/keystore.cpp | 58 |
1 files changed, 40 insertions, 18 deletions
diff --git a/src/keystore.cpp b/src/keystore.cpp index de13958a8b..9e1dab65fd 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -16,14 +16,19 @@ std::vector<unsigned char> CKeyStore::GenerateNewKey() return key.GetPubKey(); } +bool CKeyStore::GetPubKey(const uint160 &hashAddress, std::vector<unsigned char> &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<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[Hash160(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret); } return true; } -bool CCryptoKeyStore::GetPrivKey(const std::vector<unsigned char> &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<unsigned char>, std::vector<unsigned char> >::const_iterator mi = mapCryptedKeys.find(vchPubKey); + CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(hashAddress); 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 uint160 &hashAddress, std::vector<unsigned char>& 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<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(); |