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 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
42 throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
43 return key.GetPubKey();
46 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
48 CRITICAL_BLOCK(cs_vMasterKey)
53 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
54 for (; mi != mapCryptedKeys.end(); ++mi)
56 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
57 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
59 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
62 key.SetSecret(vchSecret);
63 if (key.GetPubKey() == vchPubKey)
67 vMasterKey = vMasterKeyIn;
72 bool CCryptoKeyStore::AddKey(const CKey& key)
74 CRITICAL_BLOCK(cs_KeyStore)
75 CRITICAL_BLOCK(cs_vMasterKey)
78 return CBasicKeyStore::AddKey(key);
83 std::vector<unsigned char> vchCryptedSecret;
84 std::vector<unsigned char> vchPubKey = key.GetPubKey();
85 if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
88 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
95 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
97 CRITICAL_BLOCK(cs_KeyStore)
102 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
107 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
109 CRITICAL_BLOCK(cs_vMasterKey)
112 return CBasicKeyStore::GetKey(address, keyOut);
114 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
115 if (mi != mapCryptedKeys.end())
117 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
118 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
120 if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
122 keyOut.SetSecret(vchSecret);
129 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
131 CRITICAL_BLOCK(cs_vMasterKey)
134 return CKeyStore::GetPubKey(address, vchPubKeyOut);
136 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
137 if (mi != mapCryptedKeys.end())
139 vchPubKeyOut = (*mi).second.first;
146 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
148 CRITICAL_BLOCK(cs_KeyStore)
149 CRITICAL_BLOCK(cs_vMasterKey)
151 if (!mapCryptedKeys.empty() || IsCrypted())
156 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
158 if (!key.SetSecret(mKey.second))
160 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
161 std::vector<unsigned char> vchCryptedSecret;
162 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
164 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))