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 auto secret = key.GetSecret(fCompressed);
27 mapKeys[key.GetPubKey().GetID()] = { 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 auto 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 bool keyPass = false;
150 bool keyFail = false;
152 // Check regular key pairs
154 auto mi = mapCryptedKeys.begin();
155 for (; mi != mapCryptedKeys.end(); ++mi)
157 const auto &vchPubKey = (*mi).second.first;
158 const auto &vchCryptedSecret = (*mi).second.second;
160 if (!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
165 if (vchSecret.size() != 32)
171 if (fDecryptionThoroughlyChecked)
176 // Check malleable key pairs
178 if (keyPass && !keyFail)
180 auto mi = mapCryptedMalleableKeys.begin();
181 for(; mi != mapCryptedMalleableKeys.end(); ++mi)
183 const auto &H = mi->first.GetMalleablePubKey().GetH();
185 if (!DecryptSecret(vMasterKeyIn, mi->second, H.GetHash(), vchSecretH))
190 if (vchSecretH.size() != 32)
196 if (fDecryptionThoroughlyChecked)
202 if (keyPass && keyFail)
204 printf("The wallet is probably corrupted: Some keys decrypt but not all.\n");
207 if (keyFail || !keyPass)
209 vMasterKey = vMasterKeyIn;
210 fDecryptionThoroughlyChecked = true;
212 NotifyStatusChanged(this);
217 bool CCryptoKeyStore::AddKey(const CKey& key)
223 script.SetDestination(key.GetPubKey().GetID());
225 if (HaveWatchOnly(script))
229 return CBasicKeyStore::AddKey(key);
234 std::vector<unsigned char> vchCryptedSecret;
235 auto vchPubKey = key.GetPubKey();
237 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
240 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
246 bool CCryptoKeyStore::AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH)
251 return CBasicKeyStore::AddMalleableKey(keyView, vchSecretH);
257 keyH.SetSecret(vchSecretH, true);
259 std::vector<unsigned char> vchCryptedSecretH;
260 if (!EncryptSecret(vMasterKey, vchSecretH, keyH.GetPubKey().GetHash(), vchCryptedSecretH))
263 if (!AddCryptedMalleableKey(keyView, vchCryptedSecretH))
269 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
276 mapCryptedKeys[vchPubKey.GetID()] = { vchPubKey, vchCryptedSecret };
281 bool CCryptoKeyStore::AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char> &vchCryptedSecretH)
288 mapCryptedMalleableKeys[CMalleableKeyView(keyView)] = vchCryptedSecretH;
293 bool CCryptoKeyStore::CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const
298 return CBasicKeyStore::CreatePrivKey(pubKeyVariant, R, privKey);
300 for (auto mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++)
302 if (mi->first.CheckKeyVariant(R, pubKeyVariant))
304 const CPubKey H = mi->first.GetMalleablePubKey().GetH();
307 if (!DecryptSecret(vMasterKey, mi->second, H.GetHash(), vchSecretH))
309 if (vchSecretH.size() != 32)
312 CMalleableKey mKey = mi->first.GetMalleableKey(vchSecretH);
313 return mKey.CheckKeyVariant(R, pubKeyVariant, privKey);;
321 bool CCryptoKeyStore::GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const
326 return CBasicKeyStore::GetMalleableKey(keyView, mKey);
327 auto mi = mapCryptedMalleableKeys.find(keyView);
328 if (mi != mapCryptedMalleableKeys.end())
330 const CPubKey H = keyView.GetMalleablePubKey().GetH();
333 if (!DecryptSecret(vMasterKey, mi->second, H.GetHash(), vchSecretH))
336 if (vchSecretH.size() != 32)
338 mKey = mi->first.GetMalleableKey(vchSecretH);
346 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
351 return CBasicKeyStore::GetKey(address, keyOut);
353 auto mi = mapCryptedKeys.find(address);
354 if (mi != mapCryptedKeys.end())
356 const CPubKey &vchPubKey = (*mi).second.first;
357 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
359 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
361 if (vchSecret.size() != 32)
363 keyOut.SetSecret(vchSecret);
364 keyOut.SetCompressedPubKey(vchPubKey.IsCompressed());
371 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
376 return CKeyStore::GetPubKey(address, vchPubKeyOut);
378 auto mi = mapCryptedKeys.find(address);
379 if (mi != mapCryptedKeys.end())
381 vchPubKeyOut = (*mi).second.first;
388 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
392 if (!mapCryptedKeys.empty() || IsCrypted())
396 for(auto& mKey : mapKeys)
399 if (!key.SetSecret(mKey.second.first, mKey.second.second))
401 const auto vchPubKey = key.GetPubKey();
402 std::vector<unsigned char> vchCryptedSecret;
404 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
406 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
411 for(auto& mKey : mapMalleableKeys)
413 const auto vchPubKeyH = mKey.first.GetMalleablePubKey().GetH();
414 std::vector<unsigned char> vchCryptedSecretH;
415 if (!EncryptSecret(vMasterKeyIn, mKey.second, vchPubKeyH.GetHash(), vchCryptedSecretH))
417 if (!AddCryptedMalleableKey(mKey.first, vchCryptedSecretH))
420 mapMalleableKeys.clear();
425 bool CCryptoKeyStore::DecryptKeys(const CKeyingMaterial& vMasterKeyIn)
432 auto mi = mapCryptedKeys.begin();
433 for (; mi != mapCryptedKeys.end(); ++mi)
435 const auto &vchPubKey = (*mi).second.first;
436 const auto &vchCryptedSecret = (*mi).second.second;
438 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
440 if (vchSecret.size() != 32)
443 key.SetSecret(vchSecret);
444 key.SetCompressedPubKey(vchPubKey.IsCompressed());
445 if (!CBasicKeyStore::AddKey(key))
449 mapCryptedKeys.clear();
451 auto mi2 = mapCryptedMalleableKeys.begin();
452 for(; mi2 != mapCryptedMalleableKeys.end(); ++mi2)
454 const auto vchPubKeyH = mi2->first.GetMalleablePubKey().GetH();
457 if(!DecryptSecret(vMasterKeyIn, mi2->second, vchPubKeyH.GetHash(), vchSecretH))
459 if (vchSecretH.size() != 32)
462 if (!CBasicKeyStore::AddMalleableKey(mi2->first, vchSecretH))
465 mapCryptedMalleableKeys.clear();