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 license.txt or http://www.opensource.org/licenses/mit-license.php.
9 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &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[CBitcoinAddress(key.GetPubKey())] = make_pair(secret, fCompressed);
29 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
33 mapScripts[Hash160(redeemScript)] = redeemScript;
38 bool CBasicKeyStore::HaveCScript(const uint160& hash) const
43 result = (mapScripts.count(hash) > 0);
49 bool CBasicKeyStore::GetCScript(const uint160 &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::Unlock(const CKeyingMaterial& vMasterKeyIn)
83 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
84 for (; mi != mapCryptedKeys.end(); ++mi)
86 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
87 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
89 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
91 if (vchSecret.size() != 32)
94 key.SetPubKey(vchPubKey);
95 key.SetSecret(vchSecret);
96 if (key.GetPubKey() == vchPubKey)
100 vMasterKey = vMasterKeyIn;
105 bool CCryptoKeyStore::AddKey(const CKey& key)
110 return CBasicKeyStore::AddKey(key);
115 std::vector<unsigned char> vchCryptedSecret;
116 std::vector<unsigned char> vchPubKey = key.GetPubKey();
118 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
121 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
128 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
135 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
140 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
145 return CBasicKeyStore::GetKey(address, keyOut);
147 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
148 if (mi != mapCryptedKeys.end())
150 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
151 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
153 if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
155 if (vchSecret.size() != 32)
157 keyOut.SetPubKey(vchPubKey);
158 keyOut.SetSecret(vchSecret);
165 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
170 return CKeyStore::GetPubKey(address, vchPubKeyOut);
172 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
173 if (mi != mapCryptedKeys.end())
175 vchPubKeyOut = (*mi).second.first;
182 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
186 if (!mapCryptedKeys.empty() || IsCrypted())
190 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
193 if (!key.SetSecret(mKey.second.first, mKey.second.second))
195 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
196 std::vector<unsigned char> vchCryptedSecret;
198 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
200 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))