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.
13 std::vector<unsigned char> CKeyStore::GenerateNewKey()
19 throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
20 return key.GetPubKey();
23 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
26 if (!GetKey(address, key))
28 vchPubKeyOut = key.GetPubKey();
32 bool CBasicKeyStore::AddKey(const CKey& key)
34 CRITICAL_BLOCK(cs_KeyStore)
35 mapKeys[CBitcoinAddress(key.GetPubKey())] = key.GetSecret();
39 bool CBasicKeyStore::AddCScript(const uint160 &hash, const CScript& redeemScript)
41 CRITICAL_BLOCK(cs_KeyStore)
42 mapScripts[hash] = 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 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
88 throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
89 return key.GetPubKey();
92 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
94 CRITICAL_BLOCK(cs_KeyStore)
99 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
100 for (; mi != mapCryptedKeys.end(); ++mi)
102 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
103 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
105 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
108 key.SetSecret(vchSecret);
109 if (key.GetPubKey() == vchPubKey)
113 vMasterKey = vMasterKeyIn;
118 bool CCryptoKeyStore::AddKey(const CKey& key)
120 CRITICAL_BLOCK(cs_KeyStore)
123 return CBasicKeyStore::AddKey(key);
128 std::vector<unsigned char> vchCryptedSecret;
129 std::vector<unsigned char> vchPubKey = key.GetPubKey();
130 if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
133 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
140 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
142 CRITICAL_BLOCK(cs_KeyStore)
147 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
152 bool CCryptoKeyStore::GetSecret(const CBitcoinAddress &address, CSecret& vchSecretOut) const
154 CRITICAL_BLOCK(cs_KeyStore)
157 return CBasicKeyStore::GetSecret(address, vchSecretOut);
159 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
160 if (mi != mapCryptedKeys.end())
162 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
163 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
164 return DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecretOut);
170 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
172 CRITICAL_BLOCK(cs_KeyStore)
175 return CKeyStore::GetPubKey(address, vchPubKeyOut);
177 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
178 if (mi != mapCryptedKeys.end())
180 vchPubKeyOut = (*mi).second.first;
187 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
189 CRITICAL_BLOCK(cs_KeyStore)
191 if (!mapCryptedKeys.empty() || IsCrypted())
196 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
198 if (!key.SetSecret(mKey.second))
200 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
201 std::vector<unsigned char> vchCryptedSecret;
202 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
204 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))