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 CMalleableKey& mKey)
36 mapMalleableKeys[CMalleableKeyView(mKey)] = mKey;
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.SetPubKey(vchPubKey);
161 key.SetSecret(vchSecret);
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))
203 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
210 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
215 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
220 return CBasicKeyStore::GetKey(address, keyOut);
222 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
223 if (mi != mapCryptedKeys.end())
225 const CPubKey &vchPubKey = (*mi).second.first;
226 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
228 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
230 if (vchSecret.size() != 32)
232 keyOut.SetPubKey(vchPubKey);
233 keyOut.SetSecret(vchSecret);
240 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
245 return CKeyStore::GetPubKey(address, vchPubKeyOut);
247 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
248 if (mi != mapCryptedKeys.end())
250 vchPubKeyOut = (*mi).second.first;
257 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
261 if (!mapCryptedKeys.empty() || IsCrypted())
265 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
268 if (!key.SetSecret(mKey.second.first, mKey.second.second))
270 const CPubKey vchPubKey = key.GetPubKey();
271 std::vector<unsigned char> vchCryptedSecret;
273 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
275 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
283 bool CCryptoKeyStore::DecryptKeys(const CKeyingMaterial& vMasterKeyIn)
290 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
291 for (; mi != mapCryptedKeys.end(); ++mi)
293 const CPubKey &vchPubKey = (*mi).second.first;
294 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
296 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
298 if (vchSecret.size() != 32)
301 key.SetPubKey(vchPubKey);
302 key.SetSecret(vchSecret);
303 if (!CBasicKeyStore::AddKey(key))
307 mapCryptedKeys.clear();