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 CRITICAL_BLOCK(cs_KeyStore)
33 mapKeys[CBitcoinAddress(key.GetPubKey())] = key.GetSecret();
37 bool CBasicKeyStore::AddCScript(const uint160 &hash, const CScript& redeemScript)
39 CRITICAL_BLOCK(cs_KeyStore)
40 mapScripts[hash] = redeemScript;
44 bool CBasicKeyStore::HaveCScript(const uint160& hash) const
47 CRITICAL_BLOCK(cs_KeyStore)
48 result = (mapScripts.count(hash) > 0);
53 bool CBasicKeyStore::GetCScript(const uint160 &hash, CScript& redeemScriptOut) const
55 CRITICAL_BLOCK(cs_KeyStore)
57 ScriptMap::const_iterator mi = mapScripts.find(hash);
58 if (mi != mapScripts.end())
60 redeemScriptOut = (*mi).second;
67 bool CCryptoKeyStore::SetCrypted()
69 CRITICAL_BLOCK(cs_KeyStore)
80 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
86 throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
87 return key.GetPubKey();
90 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
92 CRITICAL_BLOCK(cs_KeyStore)
97 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
98 for (; mi != mapCryptedKeys.end(); ++mi)
100 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
101 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
103 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
106 key.SetSecret(vchSecret);
107 if (key.GetPubKey() == vchPubKey)
111 vMasterKey = vMasterKeyIn;
116 bool CCryptoKeyStore::AddKey(const CKey& key)
118 CRITICAL_BLOCK(cs_KeyStore)
121 return CBasicKeyStore::AddKey(key);
126 std::vector<unsigned char> vchCryptedSecret;
127 std::vector<unsigned char> vchPubKey = key.GetPubKey();
128 if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
131 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
138 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
140 CRITICAL_BLOCK(cs_KeyStore)
145 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
150 bool CCryptoKeyStore::GetSecret(const CBitcoinAddress &address, CSecret& vchSecretOut) const
152 CRITICAL_BLOCK(cs_KeyStore)
155 return CBasicKeyStore::GetSecret(address, vchSecretOut);
157 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
158 if (mi != mapCryptedKeys.end())
160 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
161 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
162 return DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecretOut);
168 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
170 CRITICAL_BLOCK(cs_KeyStore)
173 return CKeyStore::GetPubKey(address, vchPubKeyOut);
175 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
176 if (mi != mapCryptedKeys.end())
178 vchPubKeyOut = (*mi).second.first;
185 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
187 CRITICAL_BLOCK(cs_KeyStore)
189 if (!mapCryptedKeys.empty() || IsCrypted())
194 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
196 if (!key.SetSecret(mKey.second))
198 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
199 std::vector<unsigned char> vchCryptedSecret;
200 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
202 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))