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::AddCScript(const CScript& redeemScript)
34 if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
35 return error("CBasicKeyStore::AddCScript() : redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
39 mapScripts[redeemScript.GetID()] = redeemScript;
44 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
49 result = (mapScripts.count(hash) > 0);
55 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
59 ScriptMap::const_iterator mi = mapScripts.find(hash);
60 if (mi != mapScripts.end())
62 redeemScriptOut = (*mi).second;
69 bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
73 CTxDestination address;
74 if (ExtractDestination(dest, address)) {
76 CBitcoinAddress(address).GetKeyID(keyID);
81 setWatchOnly.insert(dest);
86 bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
89 setWatchOnly.erase(dest);
93 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
96 return setWatchOnly.count(dest) > 0;
99 bool CBasicKeyStore::HaveWatchOnly() const
102 return (!setWatchOnly.empty());
105 bool CCryptoKeyStore::SetCrypted()
111 if (!mapKeys.empty())
118 bool CCryptoKeyStore::Lock()
126 fWalletUnlockMintOnly = false;
129 NotifyStatusChanged(this);
133 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
140 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
141 for (; mi != mapCryptedKeys.end(); ++mi)
143 const CPubKey &vchPubKey = (*mi).second.first;
144 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
146 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
148 if (vchSecret.size() != 32)
151 key.SetPubKey(vchPubKey);
152 key.SetSecret(vchSecret);
153 if (key.GetPubKey() == vchPubKey)
157 vMasterKey = vMasterKeyIn;
159 NotifyStatusChanged(this);
163 bool CCryptoKeyStore::AddKey(const CKey& key)
169 script.SetDestination(key.GetPubKey().GetID());
171 if (HaveWatchOnly(script))
175 return CBasicKeyStore::AddKey(key);
180 std::vector<unsigned char> vchCryptedSecret;
181 CPubKey vchPubKey = key.GetPubKey();
183 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
186 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
193 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
200 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
205 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
210 return CBasicKeyStore::GetKey(address, keyOut);
212 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
213 if (mi != mapCryptedKeys.end())
215 const CPubKey &vchPubKey = (*mi).second.first;
216 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
218 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
220 if (vchSecret.size() != 32)
222 keyOut.SetPubKey(vchPubKey);
223 keyOut.SetSecret(vchSecret);
230 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
235 return CKeyStore::GetPubKey(address, vchPubKeyOut);
237 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
238 if (mi != mapCryptedKeys.end())
240 vchPubKeyOut = (*mi).second.first;
247 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
251 if (!mapCryptedKeys.empty() || IsCrypted())
255 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
258 if (!key.SetSecret(mKey.second.first, mKey.second.second))
260 const CPubKey vchPubKey = key.GetPubKey();
261 std::vector<unsigned char> vchCryptedSecret;
263 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
265 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
273 bool CCryptoKeyStore::DecryptKeys(const CKeyingMaterial& vMasterKeyIn)
280 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
281 for (; mi != mapCryptedKeys.end(); ++mi)
283 const CPubKey &vchPubKey = (*mi).second.first;
284 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
286 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
288 if (vchSecret.size() != 32)
291 key.SetPubKey(vchPubKey);
292 key.SetSecret(vchSecret);
293 if (!CBasicKeyStore::AddKey(key))
297 mapCryptedKeys.clear();