1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
10 std::vector<unsigned char> CKeyStore::GenerateNewKey()
16 throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
17 return key.GetPubKey();
20 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
23 if (!GetKey(address, key))
25 vchPubKeyOut = key.GetPubKey();
29 bool CBasicKeyStore::AddKey(const CKey& key)
31 CRITICAL_BLOCK(cs_KeyStore)
32 mapKeys[key.GetAddress()] = key.GetSecret();
36 bool CCryptoKeyStore::SetCrypted()
38 CRITICAL_BLOCK(cs_KeyStore)
49 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
55 throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
56 return key.GetPubKey();
59 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
61 CRITICAL_BLOCK(cs_KeyStore)
66 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
67 for (; mi != mapCryptedKeys.end(); ++mi)
69 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
70 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
72 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
75 key.SetSecret(vchSecret);
76 if (key.GetPubKey() == vchPubKey)
80 vMasterKey = vMasterKeyIn;
85 bool CCryptoKeyStore::AddKey(const CKey& key)
87 CRITICAL_BLOCK(cs_KeyStore)
90 return CBasicKeyStore::AddKey(key);
95 std::vector<unsigned char> vchCryptedSecret;
96 std::vector<unsigned char> vchPubKey = key.GetPubKey();
97 if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
100 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
107 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
109 CRITICAL_BLOCK(cs_KeyStore)
114 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
119 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
121 CRITICAL_BLOCK(cs_KeyStore)
124 return CBasicKeyStore::GetKey(address, keyOut);
126 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
127 if (mi != mapCryptedKeys.end())
129 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
130 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
132 if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
134 keyOut.SetSecret(vchSecret);
141 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
143 CRITICAL_BLOCK(cs_KeyStore)
146 return CKeyStore::GetPubKey(address, vchPubKeyOut);
148 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
149 if (mi != mapCryptedKeys.end())
151 vchPubKeyOut = (*mi).second.first;
158 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
160 CRITICAL_BLOCK(cs_KeyStore)
162 if (!mapCryptedKeys.empty() || IsCrypted())
167 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
169 if (!key.SetSecret(mKey.second))
171 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
172 std::vector<unsigned char> vchCryptedSecret;
173 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
175 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))