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 CCryptoKeyStore::CCryptoKeyStore() : fUseCrypto(false)
107 std::string strMalleableKey = GetArg("-masterkey", "");
108 CMalleableKey malleableKey;
109 if (strMalleableKey != "")
110 malleableKey.SetString(strMalleableKey);
112 malleableKey.MakeNewKeys();
114 const CMalleableKeyView& keyView(malleableKey);
115 mapMalleableKeys[keyView] = malleableKey;
118 bool CCryptoKeyStore::SetCrypted()
124 if (!mapKeys.empty())
131 bool CCryptoKeyStore::Lock()
139 fWalletUnlockMintOnly = false;
142 NotifyStatusChanged(this);
146 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
153 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
154 for (; mi != mapCryptedKeys.end(); ++mi)
156 const CPubKey &vchPubKey = (*mi).second.first;
157 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
159 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
161 if (vchSecret.size() != 32)
164 key.SetPubKey(vchPubKey);
165 key.SetSecret(vchSecret);
166 if (key.GetPubKey() == vchPubKey)
171 vMasterKey = vMasterKeyIn;
173 NotifyStatusChanged(this);
177 bool CCryptoKeyStore::AddKey(const CKey& key)
183 script.SetDestination(key.GetPubKey().GetID());
185 if (HaveWatchOnly(script))
189 return CBasicKeyStore::AddKey(key);
194 std::vector<unsigned char> vchCryptedSecret;
195 CPubKey vchPubKey = key.GetPubKey();
197 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
200 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
207 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
214 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
219 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
224 return CBasicKeyStore::GetKey(address, keyOut);
226 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
227 if (mi != mapCryptedKeys.end())
229 const CPubKey &vchPubKey = (*mi).second.first;
230 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
232 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
234 if (vchSecret.size() != 32)
236 keyOut.SetPubKey(vchPubKey);
237 keyOut.SetSecret(vchSecret);
244 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
249 return CKeyStore::GetPubKey(address, vchPubKeyOut);
251 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
252 if (mi != mapCryptedKeys.end())
254 vchPubKeyOut = (*mi).second.first;
261 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
265 if (!mapCryptedKeys.empty() || IsCrypted())
269 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
272 if (!key.SetSecret(mKey.second.first, mKey.second.second))
274 const CPubKey vchPubKey = key.GetPubKey();
275 std::vector<unsigned char> vchCryptedSecret;
277 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
279 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
287 bool CCryptoKeyStore::DecryptKeys(const CKeyingMaterial& vMasterKeyIn)
294 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
295 for (; mi != mapCryptedKeys.end(); ++mi)
297 const CPubKey &vchPubKey = (*mi).second.first;
298 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
300 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
302 if (vchSecret.size() != 32)
305 key.SetPubKey(vchPubKey);
306 key.SetSecret(vchSecret);
307 if (!CBasicKeyStore::AddKey(key))
311 mapCryptedKeys.clear();