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.
9 extern bool fWalletUnlockMintOnly;
11 bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
14 if (!GetKey(address, key))
16 vchPubKeyOut = key.GetPubKey();
20 bool CBasicKeyStore::AddKey(const CKey& key)
22 bool fCompressed = false;
23 CSecret secret = key.GetSecret(fCompressed);
26 mapKeys[key.GetPubKey().GetID()] = make_pair(secret, fCompressed);
31 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
35 mapScripts[redeemScript.GetID()] = redeemScript;
40 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
45 result = (mapScripts.count(hash) > 0);
51 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
55 ScriptMap::const_iterator mi = mapScripts.find(hash);
56 if (mi != mapScripts.end())
58 redeemScriptOut = (*mi).second;
65 bool CBasicKeyStore::AddWatchOnly(const CTxDestination &dest)
68 setWatchOnly.insert(dest);
72 bool CBasicKeyStore::HaveWatchOnly(const CTxDestination &dest) const
75 return setWatchOnly.count(dest) > 0;
78 bool CCryptoKeyStore::SetCrypted()
91 bool CCryptoKeyStore::Lock()
99 fWalletUnlockMintOnly = false;
102 NotifyStatusChanged(this);
106 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
113 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
114 for (; mi != mapCryptedKeys.end(); ++mi)
116 const CPubKey &vchPubKey = (*mi).second.first;
117 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
119 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
121 if (vchSecret.size() != 32)
124 key.SetPubKey(vchPubKey);
125 key.SetSecret(vchSecret);
126 if (key.GetPubKey() == vchPubKey)
130 vMasterKey = vMasterKeyIn;
132 NotifyStatusChanged(this);
136 bool CCryptoKeyStore::AddKey(const CKey& key)
141 return CBasicKeyStore::AddKey(key);
146 std::vector<unsigned char> vchCryptedSecret;
147 CPubKey vchPubKey = key.GetPubKey();
149 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
152 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
159 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
166 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
171 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
176 return CBasicKeyStore::GetKey(address, keyOut);
178 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
179 if (mi != mapCryptedKeys.end())
181 const CPubKey &vchPubKey = (*mi).second.first;
182 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
184 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
186 if (vchSecret.size() != 32)
188 keyOut.SetPubKey(vchPubKey);
189 keyOut.SetSecret(vchSecret);
196 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
201 return CKeyStore::GetPubKey(address, vchPubKeyOut);
203 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
204 if (mi != mapCryptedKeys.end())
206 vchPubKeyOut = (*mi).second.first;
213 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
217 if (!mapCryptedKeys.empty() || IsCrypted())
221 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
224 if (!key.SetSecret(mKey.second.first, mKey.second.second))
226 const CPubKey vchPubKey = key.GetPubKey();
227 std::vector<unsigned char> vchCryptedSecret;
229 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
231 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))