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 COPYING 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))
74 if (vchSecret.size() != 32)
77 key.SetSecret(vchSecret);
78 if (key.GetPubKey() == vchPubKey)
82 vMasterKey = vMasterKeyIn;
87 bool CCryptoKeyStore::AddKey(const CKey& key)
89 CRITICAL_BLOCK(cs_KeyStore)
92 return CBasicKeyStore::AddKey(key);
97 std::vector<unsigned char> vchCryptedSecret;
98 std::vector<unsigned char> vchPubKey = key.GetPubKey();
99 if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
102 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
109 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
111 CRITICAL_BLOCK(cs_KeyStore)
116 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
121 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
123 CRITICAL_BLOCK(cs_KeyStore)
126 return CBasicKeyStore::GetKey(address, keyOut);
128 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
129 if (mi != mapCryptedKeys.end())
131 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
132 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
134 if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
136 if (vchSecret.size() != 32)
138 keyOut.SetSecret(vchSecret);
145 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
147 CRITICAL_BLOCK(cs_KeyStore)
150 return CKeyStore::GetPubKey(address, vchPubKeyOut);
152 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
153 if (mi != mapCryptedKeys.end())
155 vchPubKeyOut = (*mi).second.first;
162 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
164 CRITICAL_BLOCK(cs_KeyStore)
166 if (!mapCryptedKeys.empty() || IsCrypted())
171 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
173 if (!key.SetSecret(mKey.second))
175 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
176 std::vector<unsigned char> vchCryptedSecret;
177 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
179 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))