1 // Copyright (c) 2009-2011 Satoshi Nakamoto & Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
9 std::vector<unsigned char> CKeyStore::GenerateNewKey()
15 throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
16 return key.GetPubKey();
19 bool CKeyStore::GetPubKey(const uint160 &hashAddress, std::vector<unsigned char> &vchPubKeyOut) const
22 if (!GetKey(hashAddress, key))
24 vchPubKeyOut = key.GetPubKey();
28 bool CBasicKeyStore::AddKey(const CKey& key)
30 CRITICAL_BLOCK(cs_KeyStore)
31 mapKeys[Hash160(key.GetPubKey())] = key.GetSecret();
35 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
41 throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
42 return key.GetPubKey();
45 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
47 CRITICAL_BLOCK(cs_vMasterKey)
52 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
53 for (; mi != mapCryptedKeys.end(); ++mi)
55 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
56 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
58 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
61 key.SetSecret(vchSecret);
62 if (key.GetPubKey() == vchPubKey)
66 vMasterKey = vMasterKeyIn;
71 bool CCryptoKeyStore::AddKey(const CKey& key)
73 CRITICAL_BLOCK(cs_KeyStore)
74 CRITICAL_BLOCK(cs_vMasterKey)
77 return CBasicKeyStore::AddKey(key);
82 std::vector<unsigned char> vchCryptedSecret;
83 std::vector<unsigned char> vchPubKey = key.GetPubKey();
84 if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
87 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
94 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
96 CRITICAL_BLOCK(cs_KeyStore)
101 mapCryptedKeys[Hash160(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
106 bool CCryptoKeyStore::GetKey(const uint160 &hashAddress, CKey& keyOut) const
108 CRITICAL_BLOCK(cs_vMasterKey)
111 return CBasicKeyStore::GetKey(hashAddress, keyOut);
113 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(hashAddress);
114 if (mi != mapCryptedKeys.end())
116 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
117 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
119 if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
121 keyOut.SetSecret(vchSecret);
128 bool CCryptoKeyStore::GetPubKey(const uint160 &hashAddress, std::vector<unsigned char>& vchPubKeyOut) const
130 CRITICAL_BLOCK(cs_vMasterKey)
133 return CKeyStore::GetPubKey(hashAddress, vchPubKeyOut);
135 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(hashAddress);
136 if (mi != mapCryptedKeys.end())
138 vchPubKeyOut = (*mi).second.first;
145 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
147 CRITICAL_BLOCK(cs_KeyStore)
148 CRITICAL_BLOCK(cs_vMasterKey)
150 if (!mapCryptedKeys.empty() || IsCrypted())
155 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
157 if (!key.SetPrivKey(mKey.second))
159 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
160 std::vector<unsigned char> vchCryptedSecret;
161 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
163 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))