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 CTxDestination &dest)
70 CBitcoinAddress(dest).GetKeyID(keyID);
75 setWatchOnly.insert(dest);
79 bool CBasicKeyStore::HaveWatchOnly(const CTxDestination &dest) const
82 return setWatchOnly.count(dest) > 0;
85 bool CCryptoKeyStore::SetCrypted()
98 bool CCryptoKeyStore::Lock()
106 fWalletUnlockMintOnly = false;
109 NotifyStatusChanged(this);
113 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
120 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
121 for (; mi != mapCryptedKeys.end(); ++mi)
123 const CPubKey &vchPubKey = (*mi).second.first;
124 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
126 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
128 if (vchSecret.size() != 32)
131 key.SetPubKey(vchPubKey);
132 key.SetSecret(vchSecret);
133 if (key.GetPubKey() == vchPubKey)
137 vMasterKey = vMasterKeyIn;
139 NotifyStatusChanged(this);
143 bool CCryptoKeyStore::AddKey(const CKey& key)
148 CTxDestination address = key.GetPubKey().GetID();
149 if (HaveWatchOnly(address))
153 return CBasicKeyStore::AddKey(key);
158 std::vector<unsigned char> vchCryptedSecret;
159 CPubKey vchPubKey = key.GetPubKey();
161 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
164 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
171 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
178 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
183 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
188 return CBasicKeyStore::GetKey(address, keyOut);
190 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
191 if (mi != mapCryptedKeys.end())
193 const CPubKey &vchPubKey = (*mi).second.first;
194 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
196 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
198 if (vchSecret.size() != 32)
200 keyOut.SetPubKey(vchPubKey);
201 keyOut.SetSecret(vchSecret);
208 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
213 return CKeyStore::GetPubKey(address, vchPubKeyOut);
215 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
216 if (mi != mapCryptedKeys.end())
218 vchPubKeyOut = (*mi).second.first;
225 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
229 if (!mapCryptedKeys.empty() || IsCrypted())
233 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
236 if (!key.SetSecret(mKey.second.first, mKey.second.second))
238 const CPubKey vchPubKey = key.GetPubKey();
239 std::vector<unsigned char> vchCryptedSecret;
241 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
243 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))