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 CKeyStore::AddKey(const CKey &key) {
19 return AddKeyPubKey(key, key.GetPubKey());
22 bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
25 mapKeys[pubkey.GetID()] = key;
29 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
32 mapScripts[redeemScript.GetID()] = redeemScript;
36 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
39 return mapScripts.count(hash) > 0;
42 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
45 ScriptMap::const_iterator mi = mapScripts.find(hash);
46 if (mi != mapScripts.end())
48 redeemScriptOut = (*mi).second;
54 bool CCryptoKeyStore::SetCrypted()
65 bool CCryptoKeyStore::Lock()
75 NotifyStatusChanged(this);
79 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
86 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
87 for (; mi != mapCryptedKeys.end(); ++mi)
89 const CPubKey &vchPubKey = (*mi).second.first;
90 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
91 CKeyingMaterial vchSecret;
92 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
94 if (vchSecret.size() != 32)
97 key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
98 if (key.GetPubKey() == vchPubKey)
102 vMasterKey = vMasterKeyIn;
104 NotifyStatusChanged(this);
108 bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
113 return CBasicKeyStore::AddKeyPubKey(key, pubkey);
118 std::vector<unsigned char> vchCryptedSecret;
119 CKeyingMaterial vchSecret(key.begin(), key.end());
120 if (!EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCryptedSecret))
123 if (!AddCryptedKey(pubkey, vchCryptedSecret))
130 bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
137 mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
142 bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
147 return CBasicKeyStore::GetKey(address, keyOut);
149 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
150 if (mi != mapCryptedKeys.end())
152 const CPubKey &vchPubKey = (*mi).second.first;
153 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
154 CKeyingMaterial vchSecret;
155 if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
157 if (vchSecret.size() != 32)
159 keyOut.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
166 bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
171 return CKeyStore::GetPubKey(address, vchPubKeyOut);
173 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
174 if (mi != mapCryptedKeys.end())
176 vchPubKeyOut = (*mi).second.first;
183 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
187 if (!mapCryptedKeys.empty() || IsCrypted())
191 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
193 const CKey &key = mKey.second;
194 CPubKey vchPubKey = key.GetPubKey();
195 CKeyingMaterial vchSecret(key.begin(), key.end());
196 std::vector<unsigned char> vchCryptedSecret;
197 if (!EncryptSecret(vMasterKeyIn, vchSecret, vchPubKey.GetHash(), vchCryptedSecret))
199 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))