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.
11 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &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[CBitcoinAddress(key.GetPubKey())] = make_pair(secret, fCompressed);
31 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
35 mapScripts[Hash160(redeemScript)] = redeemScript;
40 bool CBasicKeyStore::HaveCScript(const uint160& hash) const
45 result = (mapScripts.count(hash) > 0);
51 bool CBasicKeyStore::GetCScript(const uint160 &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::Unlock(const CKeyingMaterial& vMasterKeyIn)
85 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
86 for (; mi != mapCryptedKeys.end(); ++mi)
88 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
89 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
91 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
93 if (vchSecret.size() != 32)
96 key.SetPubKey(vchPubKey);
97 key.SetSecret(vchSecret);
98 if (key.GetPubKey() == vchPubKey)
102 vMasterKey = vMasterKeyIn;
107 bool CCryptoKeyStore::AddKey(const CKey& key)
112 return CBasicKeyStore::AddKey(key);
117 std::vector<unsigned char> vchCryptedSecret;
118 std::vector<unsigned char> vchPubKey = key.GetPubKey();
120 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
123 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
130 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
137 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
142 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &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 std::vector<unsigned char> &vchPubKey = (*mi).second.first;
153 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
155 if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
157 if (vchSecret.size() != 32)
159 keyOut.SetPubKey(vchPubKey);
160 keyOut.SetSecret(vchSecret);
167 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
172 return CKeyStore::GetPubKey(address, vchPubKeyOut);
174 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
175 if (mi != mapCryptedKeys.end())
177 vchPubKeyOut = (*mi).second.first;
184 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
188 if (!mapCryptedKeys.empty() || IsCrypted())
192 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
195 if (!key.SetSecret(mKey.second.first, mKey.second.second))
197 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
198 std::vector<unsigned char> vchCryptedSecret;
200 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
202 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))