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);
82 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
85 return setWatchOnly.count(dest) > 0;
88 bool CCryptoKeyStore::SetCrypted()
101 bool CCryptoKeyStore::Lock()
109 fWalletUnlockMintOnly = false;
112 NotifyStatusChanged(this);
116 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
123 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
124 for (; mi != mapCryptedKeys.end(); ++mi)
126 const CPubKey &vchPubKey = (*mi).second.first;
127 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
129 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
131 if (vchSecret.size() != 32)
134 key.SetPubKey(vchPubKey);
135 key.SetSecret(vchSecret);
136 if (key.GetPubKey() == vchPubKey)
140 vMasterKey = vMasterKeyIn;
142 NotifyStatusChanged(this);
146 bool CCryptoKeyStore::AddKey(const CKey& key)
152 script.SetDestination(key.GetPubKey().GetID());
154 if (HaveWatchOnly(script))
158 return CBasicKeyStore::AddKey(key);
163 std::vector<unsigned char> vchCryptedSecret;
164 CPubKey vchPubKey = key.GetPubKey();
166 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
169 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
176 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
183 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
188 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
193 return CBasicKeyStore::GetKey(address, keyOut);
195 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
196 if (mi != mapCryptedKeys.end())
198 const CPubKey &vchPubKey = (*mi).second.first;
199 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
201 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
203 if (vchSecret.size() != 32)
205 keyOut.SetPubKey(vchPubKey);
206 keyOut.SetSecret(vchSecret);
213 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
218 return CKeyStore::GetPubKey(address, vchPubKeyOut);
220 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
221 if (mi != mapCryptedKeys.end())
223 vchPubKeyOut = (*mi).second.first;
230 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
234 if (!mapCryptedKeys.empty() || IsCrypted())
238 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
241 if (!key.SetSecret(mKey.second.first, mKey.second.second))
243 const CPubKey vchPubKey = key.GetPubKey();
244 std::vector<unsigned char> vchCryptedSecret;
246 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
248 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))