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)
36 mapScripts[redeemScript.GetID()] = redeemScript;
41 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
46 result = (mapScripts.count(hash) > 0);
52 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
56 ScriptMap::const_iterator mi = mapScripts.find(hash);
57 if (mi != mapScripts.end())
59 redeemScriptOut = (*mi).second;
66 bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
70 CTxDestination address;
71 if (ExtractDestination(dest, address)) {
73 CBitcoinAddress(address).GetKeyID(keyID);
78 setWatchOnly.insert(dest);
83 bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
86 setWatchOnly.erase(dest);
90 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
93 return setWatchOnly.count(dest) > 0;
96 bool CBasicKeyStore::HaveWatchOnly() const
99 return (!setWatchOnly.empty());
102 bool CCryptoKeyStore::SetCrypted()
108 if (!mapKeys.empty())
115 bool CCryptoKeyStore::Lock()
123 fWalletUnlockMintOnly = false;
126 NotifyStatusChanged(this);
130 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
137 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
138 for (; mi != mapCryptedKeys.end(); ++mi)
140 const CPubKey &vchPubKey = (*mi).second.first;
141 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
143 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
145 if (vchSecret.size() != 32)
148 key.SetPubKey(vchPubKey);
149 key.SetSecret(vchSecret);
150 if (key.GetPubKey() == vchPubKey)
154 vMasterKey = vMasterKeyIn;
156 NotifyStatusChanged(this);
160 bool CCryptoKeyStore::AddKey(const CKey& key)
166 script.SetDestination(key.GetPubKey().GetID());
168 if (HaveWatchOnly(script))
172 return CBasicKeyStore::AddKey(key);
177 std::vector<unsigned char> vchCryptedSecret;
178 CPubKey vchPubKey = key.GetPubKey();
180 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
183 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
190 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
197 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
202 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
207 return CBasicKeyStore::GetKey(address, keyOut);
209 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
210 if (mi != mapCryptedKeys.end())
212 const CPubKey &vchPubKey = (*mi).second.first;
213 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
215 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
217 if (vchSecret.size() != 32)
219 keyOut.SetPubKey(vchPubKey);
220 keyOut.SetSecret(vchSecret);
227 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
232 return CKeyStore::GetPubKey(address, vchPubKeyOut);
234 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
235 if (mi != mapCryptedKeys.end())
237 vchPubKeyOut = (*mi).second.first;
244 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
248 if (!mapCryptedKeys.empty() || IsCrypted())
252 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
255 if (!key.SetSecret(mKey.second.first, mKey.second.second))
257 const CPubKey vchPubKey = key.GetPubKey();
258 std::vector<unsigned char> vchCryptedSecret;
260 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
262 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))