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 key.SetCompressedPubKey(vchPubKey.IsCompressed());
162 if (key.GetPubKey() == vchPubKey)
167 vMasterKey = vMasterKeyIn;
169 NotifyStatusChanged(this);
173 bool CCryptoKeyStore::AddKey(const CKey& key)
179 script.SetDestination(key.GetPubKey().GetID());
181 if (HaveWatchOnly(script))
185 return CBasicKeyStore::AddKey(key);
190 std::vector<unsigned char> vchCryptedSecret;
191 CPubKey vchPubKey = key.GetPubKey();
193 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
196 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
202 bool CCryptoKeyStore::AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH)
207 return CBasicKeyStore::AddMalleableKey(keyView, vchSecretH);
213 keyH.SetSecret(vchSecretH, true);
215 std::vector<unsigned char> vchCryptedSecretH;
216 if (!EncryptSecret(vMasterKey, vchSecretH, keyH.GetPubKey().GetHash(), vchCryptedSecretH))
219 if (!AddCryptedMalleableKey(keyView, vchCryptedSecretH))
225 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
232 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
237 bool CCryptoKeyStore::AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char> &vchCryptedSecretH)
244 mapCryptedMalleableKeys[CMalleableKeyView(keyView)] = vchCryptedSecretH;
249 bool CCryptoKeyStore::CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const
254 return CBasicKeyStore::CreatePrivKey(pubKeyVariant, R, privKey);
256 for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
258 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
260 const CPubKey H = mi->first.GetMalleablePubKey().GetH();
263 if (!DecryptSecret(vMasterKey, mi->second, H.GetHash(), vchSecretH))
265 if (vchSecretH.size() != 32)
268 CMalleableKey mKey = mi->first.GetMalleableKey(vchSecretH);
269 return mKey.CheckKeyVariant(R, pubKeyVariant, privKey);;
277 bool CCryptoKeyStore::GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const
282 return CBasicKeyStore::GetMalleableKey(keyView, mKey);
283 CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.find(keyView);
284 if (mi != mapCryptedMalleableKeys.end())
286 const CPubKey H = keyView.GetMalleablePubKey().GetH();
289 if (!DecryptSecret(vMasterKey, mi->second, H.GetHash(), vchSecretH))
292 if (vchSecretH.size() != 32)
294 mKey = mi->first.GetMalleableKey(vchSecretH);
302 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
307 return CBasicKeyStore::GetKey(address, keyOut);
309 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
310 if (mi != mapCryptedKeys.end())
312 const CPubKey &vchPubKey = (*mi).second.first;
313 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
315 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
317 if (vchSecret.size() != 32)
319 keyOut.SetSecret(vchSecret);
320 keyOut.SetCompressedPubKey(vchPubKey.IsCompressed());
327 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
332 return CKeyStore::GetPubKey(address, vchPubKeyOut);
334 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
335 if (mi != mapCryptedKeys.end())
337 vchPubKeyOut = (*mi).second.first;
344 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
348 if (!mapCryptedKeys.empty() || IsCrypted())
352 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
355 if (!key.SetSecret(mKey.second.first, mKey.second.second))
357 const CPubKey vchPubKey = key.GetPubKey();
358 std::vector<unsigned char> vchCryptedSecret;
360 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
362 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
367 BOOST_FOREACH(MalleableKeyMap::value_type& mKey, mapMalleableKeys)
369 const CPubKey vchPubKeyH = mKey.first.GetMalleablePubKey().GetH();
370 std::vector<unsigned char> vchCryptedSecretH;
371 if (!EncryptSecret(vMasterKeyIn, mKey.second, vchPubKeyH.GetHash(), vchCryptedSecretH))
373 if (!AddCryptedMalleableKey(mKey.first, vchCryptedSecretH))
376 mapMalleableKeys.clear();
381 bool CCryptoKeyStore::DecryptKeys(const CKeyingMaterial& vMasterKeyIn)
388 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
389 for (; mi != mapCryptedKeys.end(); ++mi)
391 const CPubKey &vchPubKey = (*mi).second.first;
392 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
394 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
396 if (vchSecret.size() != 32)
399 key.SetSecret(vchSecret);
400 key.SetCompressedPubKey(vchPubKey.IsCompressed());
401 if (!CBasicKeyStore::AddKey(key))
405 mapCryptedKeys.clear();
407 CryptedMalleableKeyMap::const_iterator mi2 = mapCryptedMalleableKeys.begin();
408 for(; mi2 != mapCryptedMalleableKeys.end(); ++mi2)
410 const CPubKey vchPubKeyH = mi2->first.GetMalleablePubKey().GetH();
413 if(!DecryptSecret(vMasterKeyIn, mi2->second, vchPubKeyH.GetHash(), vchSecretH))
415 if (vchSecretH.size() != 32)
418 if (!CBasicKeyStore::AddMalleableKey(mi2->first, vchSecretH))
421 mapCryptedMalleableKeys.clear();