1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 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 extern bool fWalletUnlockMintOnly;
12 bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
15 if (!GetKey(address, key))
17 vchPubKeyOut = key.GetPubKey();
21 bool CBasicKeyStore::AddKey(const CKey& key)
23 bool fCompressed = false;
24 CSecret secret = key.GetSecret(fCompressed);
27 mapKeys[key.GetPubKey().GetID()] = make_pair(secret, fCompressed);
32 bool CBasicKeyStore::AddMalleableKey(const CMalleableKeyView& keyView, const CSecret& vchSecretH)
36 mapMalleableKeys[CMalleableKeyView(keyView)] = vchSecretH;
41 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
43 if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
44 return error("CBasicKeyStore::AddCScript() : redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
48 mapScripts[redeemScript.GetID()] = redeemScript;
53 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
58 result = (mapScripts.count(hash) > 0);
64 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
68 ScriptMap::const_iterator mi = mapScripts.find(hash);
69 if (mi != mapScripts.end())
71 redeemScriptOut = (*mi).second;
78 bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
82 CTxDestination address;
83 if (ExtractDestination(dest, address)) {
85 CBitcoinAddress(address).GetKeyID(keyID);
90 setWatchOnly.insert(dest);
95 bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
98 setWatchOnly.erase(dest);
102 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
105 return setWatchOnly.count(dest) > 0;
108 bool CBasicKeyStore::HaveWatchOnly() const
111 return (!setWatchOnly.empty());
114 bool CCryptoKeyStore::SetCrypted()
120 if (!mapKeys.empty())
127 bool CCryptoKeyStore::Lock()
135 fWalletUnlockMintOnly = false;
138 NotifyStatusChanged(this);
142 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
149 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
150 for (; mi != mapCryptedKeys.end(); ++mi)
152 const CPubKey &vchPubKey = (*mi).second.first;
153 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
155 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
157 if (vchSecret.size() != 32)
160 key.SetSecret(vchSecret);
161 if (vchPubKey.size() == 33)
162 key.SetCompressedPubKey();
163 if (key.GetPubKey() == vchPubKey)
168 vMasterKey = vMasterKeyIn;
170 NotifyStatusChanged(this);
174 bool CCryptoKeyStore::AddKey(const CKey& key)
180 script.SetDestination(key.GetPubKey().GetID());
182 if (HaveWatchOnly(script))
186 return CBasicKeyStore::AddKey(key);
191 std::vector<unsigned char> vchCryptedSecret;
192 CPubKey vchPubKey = key.GetPubKey();
194 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
197 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
203 bool CCryptoKeyStore::AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH)
208 return CBasicKeyStore::AddMalleableKey(keyView, vchSecretH);
214 keyH.SetSecret(vchSecretH, true);
216 std::vector<unsigned char> vchCryptedSecretH;
217 if (!EncryptSecret(vMasterKey, vchSecretH, keyH.GetPubKey().GetHash(), vchCryptedSecretH))
220 if (!AddCryptedMalleableKey(keyView, vchCryptedSecretH))
226 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
233 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
238 bool CCryptoKeyStore::AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char> &vchCryptedSecretH)
245 mapCryptedMalleableKeys[CMalleableKeyView(keyView)] = vchCryptedSecretH;
250 bool CCryptoKeyStore::CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const
255 return CBasicKeyStore::CreatePrivKey(pubKeyVariant, R, privKey);
257 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
259 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
261 const CPubKey H = mi->first.GetMalleablePubKey().GetH();
264 if (!DecryptSecret(vMasterKey, mi->second, H.GetHash(), vchSecretH))
266 if (vchSecretH.size() != 32)
269 CMalleableKey mKey = mi->first.GetMalleableKey(vchSecretH);
270 return mKey.CheckKeyVariant(R, pubKeyVariant, privKey);;
278 bool CCryptoKeyStore::GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const
283 return CBasicKeyStore::GetMalleableKey(keyView, mKey);
284 CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.find(keyView);
285 if (mi != mapCryptedMalleableKeys.end())
287 const CPubKey H = keyView.GetMalleablePubKey().GetH();
290 if (!DecryptSecret(vMasterKey, mi->second, H.GetHash(), vchSecretH))
293 if (vchSecretH.size() != 32)
295 mKey = mi->first.GetMalleableKey(vchSecretH);
303 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
308 return CBasicKeyStore::GetKey(address, keyOut);
310 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
311 if (mi != mapCryptedKeys.end())
313 const CPubKey &vchPubKey = (*mi).second.first;
314 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
316 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
318 if (vchSecret.size() != 32)
320 keyOut.SetSecret(vchSecret);
321 if (vchPubKey.size() == 33)
322 keyOut.SetCompressedPubKey();
329 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
334 return CKeyStore::GetPubKey(address, vchPubKeyOut);
336 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
337 if (mi != mapCryptedKeys.end())
339 vchPubKeyOut = (*mi).second.first;
346 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
350 if (!mapCryptedKeys.empty() || IsCrypted())
354 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
357 if (!key.SetSecret(mKey.second.first, mKey.second.second))
359 const CPubKey vchPubKey = key.GetPubKey();
360 std::vector<unsigned char> vchCryptedSecret;
362 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
364 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
369 BOOST_FOREACH(MalleableKeyMap::value_type& mKey, mapMalleableKeys)
371 const CPubKey vchPubKeyH = mKey.first.GetMalleablePubKey().GetH();
372 std::vector<unsigned char> vchCryptedSecretH;
373 if (!EncryptSecret(vMasterKeyIn, mKey.second, vchPubKeyH.GetHash(), vchCryptedSecretH))
375 if (!AddCryptedMalleableKey(mKey.first, vchCryptedSecretH))
378 mapMalleableKeys.clear();
383 bool CCryptoKeyStore::DecryptKeys(const CKeyingMaterial& vMasterKeyIn)
390 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
391 for (; mi != mapCryptedKeys.end(); ++mi)
393 const CPubKey &vchPubKey = (*mi).second.first;
394 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
396 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
398 if (vchSecret.size() != 32)
401 key.SetSecret(vchSecret);
402 if (vchPubKey.size() == 33)
403 key.SetCompressedPubKey();
404 if (!CBasicKeyStore::AddKey(key))
408 mapCryptedKeys.clear();
410 CryptedMalleableKeyMap::const_iterator mi2 = mapCryptedMalleableKeys.begin();
411 for(; mi2 != mapCryptedMalleableKeys.end(); ++mi2)
413 const CPubKey vchPubKeyH = mi2->first.GetMalleablePubKey().GetH();
416 if(!DecryptSecret(vMasterKeyIn, mi2->second, vchPubKeyH.GetHash(), vchSecretH))
418 if (vchSecretH.size() != 32)
421 if (!CBasicKeyStore::AddMalleableKey(mi2->first, vchSecretH))
424 mapCryptedMalleableKeys.clear();