X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fkeystore.cpp;h=6cf557fafed595c9fe9062ba70015484b58c079a;hb=93db3fceac1bfe274bc0fd906428a20e709e2da5;hp=de13958a8bcfb676322330f2f7edc814bb69675d;hpb=0efda1a79eab6dd2e101edc615c6dd14138c31a2;p=novacoin.git diff --git a/src/keystore.cpp b/src/keystore.cpp index de13958..6cf557f 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -1,4 +1,5 @@ -// Copyright (c) 2009-2011 Satoshi Nakamoto & Bitcoin developers +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2011 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. @@ -16,13 +17,31 @@ std::vector CKeyStore::GenerateNewKey() return key.GetPubKey(); } +bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector &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[CBitcoinAddress(key.GetPubKey())] = key.GetSecret(); + return true; +} + +bool CCryptoKeyStore::SetCrypted() +{ CRITICAL_BLOCK(cs_KeyStore) { - mapKeys[key.GetPubKey()] = key.GetPrivKey(); - mapPubKeys[Hash160(key.GetPubKey())] = key.GetPubKey(); + if (fUseCrypto) + return true; + if (!mapKeys.empty()) + return false; + fUseCrypto = true; } return true; } @@ -39,16 +58,16 @@ std::vector CCryptoKeyStore::GenerateNewKey() bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn) { - CRITICAL_BLOCK(cs_vMasterKey) + CRITICAL_BLOCK(cs_KeyStore) { 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; @@ -66,7 +85,6 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn) bool CCryptoKeyStore::AddKey(const CKey& key) { CRITICAL_BLOCK(cs_KeyStore) - CRITICAL_BLOCK(cs_vMasterKey) { if (!IsCrypted()) return CBasicKeyStore::AddKey(key); @@ -88,33 +106,45 @@ 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[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret); } return true; } -bool CCryptoKeyStore::GetPrivKey(const std::vector &vchPubKey, CKey& keyOut) const +bool CCryptoKeyStore::GetSecret(const CBitcoinAddress &address, CSecret& vchSecretOut) const { - CRITICAL_BLOCK(cs_vMasterKey) + CRITICAL_BLOCK(cs_KeyStore) { if (!IsCrypted()) - return CBasicKeyStore::GetPrivKey(vchPubKey, keyOut); + return CBasicKeyStore::GetSecret(address, vchSecretOut); - std::map, std::vector >::const_iterator mi = mapCryptedKeys.find(vchPubKey); + CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); if (mi != mapCryptedKeys.end()) { - const std::vector &vchCryptedSecret = (*mi).second; - CSecret vchSecret; - if (!DecryptSecret(vMasterKey, (*mi).second, Hash((*mi).first.begin(), (*mi).first.end()), vchSecret)) - return false; - keyOut.SetSecret(vchSecret); + const std::vector &vchPubKey = (*mi).second.first; + const std::vector &vchCryptedSecret = (*mi).second.second; + return DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecretOut); + } + } + return false; +} + +bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector& vchPubKeyOut) const +{ + CRITICAL_BLOCK(cs_KeyStore) + { + if (!IsCrypted()) + return CKeyStore::GetPubKey(address, vchPubKeyOut); + + CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); + if (mi != mapCryptedKeys.end()) + { + vchPubKeyOut = (*mi).second.first; return true; } } @@ -124,7 +154,6 @@ bool CCryptoKeyStore::GetPrivKey(const std::vector &vchPubKey, CK bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn) { CRITICAL_BLOCK(cs_KeyStore) - CRITICAL_BLOCK(cs_vMasterKey) { if (!mapCryptedKeys.empty() || IsCrypted()) return false; @@ -133,12 +162,13 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn) CKey key; BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys) { - if (!key.SetPrivKey(mKey.second)) + if (!key.SetSecret(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();