1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 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 std::vector<unsigned char> CKeyStore::GenerateNewKey()
17 throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
18 return key.GetPubKey();
21 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
24 if (!GetKey(address, key))
26 vchPubKeyOut = key.GetPubKey();
30 bool CBasicKeyStore::AddKey(const CKey& key)
32 bool fCompressed = false;
33 CSecret secret = key.GetSecret(fCompressed);
34 CRITICAL_BLOCK(cs_KeyStore)
35 mapKeys[CBitcoinAddress(key.GetPubKey())] = make_pair(secret, fCompressed);
39 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
41 CRITICAL_BLOCK(cs_KeyStore)
42 mapScripts[Hash160(redeemScript)] = redeemScript;
46 bool CBasicKeyStore::HaveCScript(const uint160& hash) const
49 CRITICAL_BLOCK(cs_KeyStore)
50 result = (mapScripts.count(hash) > 0);
55 bool CBasicKeyStore::GetCScript(const uint160 &hash, CScript& redeemScriptOut) const
57 CRITICAL_BLOCK(cs_KeyStore)
59 ScriptMap::const_iterator mi = mapScripts.find(hash);
60 if (mi != mapScripts.end())
62 redeemScriptOut = (*mi).second;
69 bool CCryptoKeyStore::SetCrypted()
71 CRITICAL_BLOCK(cs_KeyStore)
82 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
84 CRITICAL_BLOCK(cs_KeyStore)
89 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
90 for (; mi != mapCryptedKeys.end(); ++mi)
92 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
93 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
95 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
98 key.SetPubKey(vchPubKey);
99 key.SetSecret(vchSecret);
100 if (key.GetPubKey() == vchPubKey)
104 vMasterKey = vMasterKeyIn;
109 bool CCryptoKeyStore::AddKey(const CKey& key)
111 CRITICAL_BLOCK(cs_KeyStore)
114 return CBasicKeyStore::AddKey(key);
119 std::vector<unsigned char> vchCryptedSecret;
120 std::vector<unsigned char> vchPubKey = key.GetPubKey();
122 if (!EncryptSecret(vMasterKey, key.GetSecret(fCompressed), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
125 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
132 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
134 CRITICAL_BLOCK(cs_KeyStore)
139 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
144 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
146 CRITICAL_BLOCK(cs_KeyStore)
149 return CBasicKeyStore::GetKey(address, keyOut);
151 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
152 if (mi != mapCryptedKeys.end())
154 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
155 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
157 if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
159 keyOut.SetPubKey(vchPubKey);
160 keyOut.SetSecret(vchSecret);
167 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
169 CRITICAL_BLOCK(cs_KeyStore)
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)
186 CRITICAL_BLOCK(cs_KeyStore)
188 if (!mapCryptedKeys.empty() || IsCrypted())
192 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
195 if (!key.SetSecret(mKey.second.first, false))
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))