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 CBasicKeyStore::AddKey(const CKey& key)
21 CRITICAL_BLOCK(cs_mapPubKeys)
22 CRITICAL_BLOCK(cs_KeyStore)
24 mapKeys[key.GetPubKey()] = key.GetPrivKey();
25 mapPubKeys[Hash160(key.GetPubKey())] = key.GetPubKey();
30 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
36 throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
37 return key.GetPubKey();
40 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
42 CRITICAL_BLOCK(cs_vMasterKey)
47 std::map<std::vector<unsigned char>, std::vector<unsigned char> >::const_iterator mi = mapCryptedKeys.begin();
48 for (; mi != mapCryptedKeys.end(); ++mi)
50 const std::vector<unsigned char> &vchPubKey = (*mi).first;
51 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second;
53 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
56 key.SetSecret(vchSecret);
57 if (key.GetPubKey() == vchPubKey)
61 vMasterKey = vMasterKeyIn;
66 bool CCryptoKeyStore::AddKey(const CKey& key)
68 CRITICAL_BLOCK(cs_KeyStore)
69 CRITICAL_BLOCK(cs_vMasterKey)
72 return CBasicKeyStore::AddKey(key);
77 std::vector<unsigned char> vchCryptedSecret;
78 std::vector<unsigned char> vchPubKey = key.GetPubKey();
79 if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
82 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
89 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
91 CRITICAL_BLOCK(cs_mapPubKeys)
92 CRITICAL_BLOCK(cs_KeyStore)
97 mapCryptedKeys[vchPubKey] = vchCryptedSecret;
98 mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
103 bool CCryptoKeyStore::GetPrivKey(const std::vector<unsigned char> &vchPubKey, CKey& keyOut) const
105 CRITICAL_BLOCK(cs_vMasterKey)
108 return CBasicKeyStore::GetPrivKey(vchPubKey, keyOut);
110 std::map<std::vector<unsigned char>, std::vector<unsigned char> >::const_iterator mi = mapCryptedKeys.find(vchPubKey);
111 if (mi != mapCryptedKeys.end())
113 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second;
115 if (!DecryptSecret(vMasterKey, (*mi).second, Hash((*mi).first.begin(), (*mi).first.end()), vchSecret))
117 keyOut.SetSecret(vchSecret);
124 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
126 CRITICAL_BLOCK(cs_KeyStore)
127 CRITICAL_BLOCK(cs_vMasterKey)
129 if (!mapCryptedKeys.empty() || IsCrypted())
134 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
136 if (!key.SetPrivKey(mKey.second))
138 std::vector<unsigned char> vchCryptedSecret;
139 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(mKey.first.begin(), mKey.first.end()), vchCryptedSecret))
141 if (!AddCryptedKey(mKey.first, vchCryptedSecret))