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.
10 std::vector<unsigned char> CKeyStore::GenerateNewKey()
16 throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
17 return key.GetPubKey();
20 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
23 if (!GetKey(address, key))
25 vchPubKeyOut = key.GetPubKey();
29 bool CBasicKeyStore::AddKey(const CKey& key)
31 CRITICAL_BLOCK(cs_KeyStore)
32 mapKeys[key.GetAddress()] = key.GetSecret();
36 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
42 throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
43 return key.GetPubKey();
46 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
48 CRITICAL_BLOCK(cs_KeyStore)
53 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
54 for (; mi != mapCryptedKeys.end(); ++mi)
56 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
57 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
59 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
62 key.SetSecret(vchSecret);
63 if (key.GetPubKey() == vchPubKey)
67 vMasterKey = vMasterKeyIn;
72 bool CCryptoKeyStore::AddKey(const CKey& key)
74 CRITICAL_BLOCK(cs_KeyStore)
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[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
106 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
108 CRITICAL_BLOCK(cs_KeyStore)
111 return CBasicKeyStore::GetKey(address, keyOut);
113 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
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 CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
130 CRITICAL_BLOCK(cs_KeyStore)
133 return CKeyStore::GetPubKey(address, vchPubKeyOut);
135 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
136 if (mi != mapCryptedKeys.end())
138 vchPubKeyOut = (*mi).second.first;
145 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
147 CRITICAL_BLOCK(cs_KeyStore)
149 if (!mapCryptedKeys.empty() || IsCrypted())
154 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
156 if (!key.SetSecret(mKey.second))
158 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
159 std::vector<unsigned char> vchCryptedSecret;
160 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
162 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))