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 CCryptoKeyStore::SetCrypted()
78 bool CCryptoKeyStore::Lock()
86 fWalletUnlockMintOnly = false;
89 NotifyStatusChanged(this);
93 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
100 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
101 for (; mi != mapCryptedKeys.end(); ++mi)
103 const CPubKey &vchPubKey = (*mi).second.first;
104 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
106 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
108 if (vchSecret.size() != 32)
111 key.SetPubKey(vchPubKey);
112 key.SetSecret(vchSecret);
113 if (key.GetPubKey() == vchPubKey)
117 vMasterKey = vMasterKeyIn;
119 NotifyStatusChanged(this);
123 bool CCryptoKeyStore::AddKey(const CKey& key)
128 return CBasicKeyStore::AddKey(key);
133 std::vector<unsigned char> vchCryptedSecret;
134 CPubKey vchPubKey = key.GetPubKey();
136 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
139 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
146 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
153 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
158 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
163 return CBasicKeyStore::GetKey(address, keyOut);
165 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
166 if (mi != mapCryptedKeys.end())
168 const CPubKey &vchPubKey = (*mi).second.first;
169 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
171 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
173 if (vchSecret.size() != 32)
175 keyOut.SetPubKey(vchPubKey);
176 keyOut.SetSecret(vchSecret);
183 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
188 return CKeyStore::GetPubKey(address, vchPubKeyOut);
190 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
191 if (mi != mapCryptedKeys.end())
193 vchPubKeyOut = (*mi).second.first;
200 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
204 if (!mapCryptedKeys.empty() || IsCrypted())
208 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
211 if (!key.SetSecret(mKey.second.first, mKey.second.second))
213 const CPubKey vchPubKey = key.GetPubKey();
214 std::vector<unsigned char> vchCryptedSecret;
216 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
218 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))