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 bool CKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
12 if (!GetKey(address, key))
14 vchPubKeyOut = key.GetPubKey();
18 bool CBasicKeyStore::AddKey(const CKey& key)
20 bool fCompressed = false;
21 CSecret secret = key.GetSecret(fCompressed);
24 mapKeys[key.GetPubKey().GetID()] = make_pair(secret, fCompressed);
29 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
33 mapScripts[redeemScript.GetID()] = redeemScript;
38 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
43 result = (mapScripts.count(hash) > 0);
49 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
53 ScriptMap::const_iterator mi = mapScripts.find(hash);
54 if (mi != mapScripts.end())
56 redeemScriptOut = (*mi).second;
63 bool CCryptoKeyStore::SetCrypted()
76 bool CCryptoKeyStore::Lock()
86 NotifyStatusChanged(this);
90 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
97 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
98 for (; mi != mapCryptedKeys.end(); ++mi)
100 const CPubKey &vchPubKey = (*mi).second.first;
101 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
103 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
105 if (vchSecret.size() != 32)
108 key.SetPubKey(vchPubKey);
109 key.SetSecret(vchSecret);
110 if (key.GetPubKey() == vchPubKey)
114 vMasterKey = vMasterKeyIn;
116 NotifyStatusChanged(this);
120 bool CCryptoKeyStore::AddKey(const CKey& key)
125 return CBasicKeyStore::AddKey(key);
130 std::vector<unsigned char> vchCryptedSecret;
131 CPubKey vchPubKey = key.GetPubKey();
133 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
136 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
143 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
150 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
155 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
160 return CBasicKeyStore::GetKey(address, keyOut);
162 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
163 if (mi != mapCryptedKeys.end())
165 const CPubKey &vchPubKey = (*mi).second.first;
166 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
168 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
170 if (vchSecret.size() != 32)
172 keyOut.SetPubKey(vchPubKey);
173 keyOut.SetSecret(vchSecret);
180 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
185 return CKeyStore::GetPubKey(address, vchPubKeyOut);
187 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
188 if (mi != mapCryptedKeys.end())
190 vchPubKeyOut = (*mi).second.first;
197 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
201 if (!mapCryptedKeys.empty() || IsCrypted())
205 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
208 if (!key.SetSecret(mKey.second.first, mKey.second.second))
210 const CPubKey vchPubKey = key.GetPubKey();
211 std::vector<unsigned char> vchCryptedSecret;
213 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), vchPubKey.GetHash(), vchCryptedSecret))
215 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))