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[CBitcoinAddress(key.GetPubKey())] = key.GetSecret();
36 bool CBasicKeyStore::AddCScript(const uint160 &hash, const std::vector<unsigned char>& data)
38 CRITICAL_BLOCK(cs_KeyStore)
43 bool CBasicKeyStore::HaveCScript(const uint160& hash) const
46 CRITICAL_BLOCK(cs_KeyStore)
47 result = (mapData.count(hash) > 0);
52 bool CBasicKeyStore::GetCScript(const uint160 &hash, std::vector<unsigned char>& dataOut) const
54 CRITICAL_BLOCK(cs_KeyStore)
56 DataMap::const_iterator mi = mapData.find(hash);
57 if (mi != mapData.end())
59 dataOut = (*mi).second;
66 bool CCryptoKeyStore::SetCrypted()
68 CRITICAL_BLOCK(cs_KeyStore)
79 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
85 throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
86 return key.GetPubKey();
89 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
91 CRITICAL_BLOCK(cs_KeyStore)
96 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
97 for (; mi != mapCryptedKeys.end(); ++mi)
99 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
100 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
102 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
105 key.SetSecret(vchSecret);
106 if (key.GetPubKey() == vchPubKey)
110 vMasterKey = vMasterKeyIn;
115 bool CCryptoKeyStore::AddKey(const CKey& key)
117 CRITICAL_BLOCK(cs_KeyStore)
120 return CBasicKeyStore::AddKey(key);
125 std::vector<unsigned char> vchCryptedSecret;
126 std::vector<unsigned char> vchPubKey = key.GetPubKey();
127 if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
130 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
137 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
139 CRITICAL_BLOCK(cs_KeyStore)
144 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
149 bool CCryptoKeyStore::GetSecret(const CBitcoinAddress &address, CSecret& vchSecretOut) const
151 CRITICAL_BLOCK(cs_KeyStore)
154 return CBasicKeyStore::GetSecret(address, vchSecretOut);
156 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
157 if (mi != mapCryptedKeys.end())
159 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
160 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
161 return DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecretOut);
167 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
169 CRITICAL_BLOCK(cs_KeyStore)
172 return CKeyStore::GetPubKey(address, vchPubKeyOut);
174 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
175 if (mi != mapCryptedKeys.end())
177 vchPubKeyOut = (*mi).second.first;
184 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
186 CRITICAL_BLOCK(cs_KeyStore)
188 if (!mapCryptedKeys.empty() || IsCrypted())
193 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
195 if (!key.SetSecret(mKey.second))
197 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
198 std::vector<unsigned char> vchCryptedSecret;
199 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
201 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))