diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2011-07-26 15:38:31 +0200 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2011-07-26 16:47:23 +0200 |
commit | 491ad6db507776054c38230387f384991f42ad29 (patch) | |
tree | 7d708a59cb5f61f9d970950c528889dec8fde8d5 /src/keystore.cpp | |
parent | c0b892fee89cba7d1fda5fda2d2fc7e966643be1 (diff) | |
parent | a9ba47101a46533ff0418d6868ebc9bf5c889818 (diff) |
Merge remote branch 'upstream/master'
Conflicts:
src/bitcoinrpc.cpp
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..2125d8472a 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 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(); |