-
+ 247C94EA309F95DDC3B90DCD41DD9212BD5269E8772816BB272A1152422CC1C50BEAD6370495043A9ABC924E119C926D55ECD7F39D1022C7D4EB50718188A641
bitcoin/src/keystore.h
(0 . 0)(1 . 138)
9164 // Copyright (c) 2009-2010 Satoshi Nakamoto
9165 // Copyright (c) 2011 The Bitcoin developers
9166 // Distributed under the MIT/X11 software license, see the accompanying
9167 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
9168 #ifndef BITCOIN_KEYSTORE_H
9169 #define BITCOIN_KEYSTORE_H
9170
9171 #include "crypter.h"
9172
9173 // A virtual base class for key stores
9174 class CKeyStore
9175 {
9176 protected:
9177 mutable CCriticalSection cs_KeyStore;
9178
9179 public:
9180 // Add a key to the store.
9181 virtual bool AddKey(const CKey& key) =0;
9182
9183 // Check whether a key corresponding to a given address is present in the store.
9184 virtual bool HaveKey(const CBitcoinAddress &address) const =0;
9185
9186 // Retrieve a key corresponding to a given address from the store.
9187 // Return true if succesful.
9188 virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
9189
9190 // Retrieve only the public key corresponding to a given address.
9191 // This may succeed even if GetKey fails (e.g., encrypted wallets)
9192 virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
9193
9194 // Generate a new key, and add it to the store
9195 virtual std::vector<unsigned char> GenerateNewKey();
9196 };
9197
9198 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
9199
9200 // Basic key store, that keeps keys in an address->secret map
9201 class CBasicKeyStore : public CKeyStore
9202 {
9203 protected:
9204 KeyMap mapKeys;
9205
9206 public:
9207 bool AddKey(const CKey& key);
9208 bool HaveKey(const CBitcoinAddress &address) const
9209 {
9210 bool result;
9211 CRITICAL_BLOCK(cs_KeyStore)
9212 result = (mapKeys.count(address) > 0);
9213 return result;
9214 }
9215 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
9216 {
9217 CRITICAL_BLOCK(cs_KeyStore)
9218 {
9219 KeyMap::const_iterator mi = mapKeys.find(address);
9220 if (mi != mapKeys.end())
9221 {
9222 keyOut.SetSecret((*mi).second);
9223 return true;
9224 }
9225 }
9226 return false;
9227 }
9228 };
9229
9230 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
9231
9232 // Keystore which keeps the private keys encrypted
9233 // It derives from the basic key store, which is used if no encryption is active.
9234 class CCryptoKeyStore : public CBasicKeyStore
9235 {
9236 private:
9237 CryptedKeyMap mapCryptedKeys;
9238
9239 CKeyingMaterial vMasterKey;
9240
9241 // if fUseCrypto is true, mapKeys must be empty
9242 // if fUseCrypto is false, vMasterKey must be empty
9243 bool fUseCrypto;
9244
9245 protected:
9246 bool SetCrypted();
9247
9248 // will encrypt previously unencrypted keys
9249 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
9250
9251 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
9252
9253 public:
9254 CCryptoKeyStore() : fUseCrypto(false)
9255 {
9256 }
9257
9258 bool IsCrypted() const
9259 {
9260 return fUseCrypto;
9261 }
9262
9263 bool IsLocked() const
9264 {
9265 if (!IsCrypted())
9266 return false;
9267 bool result;
9268 CRITICAL_BLOCK(cs_KeyStore)
9269 result = vMasterKey.empty();
9270 return result;
9271 }
9272
9273 bool Lock()
9274 {
9275 if (!SetCrypted())
9276 return false;
9277
9278 CRITICAL_BLOCK(cs_KeyStore)
9279 vMasterKey.clear();
9280
9281 return true;
9282 }
9283
9284 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
9285 std::vector<unsigned char> GenerateNewKey();
9286 bool AddKey(const CKey& key);
9287 bool HaveKey(const CBitcoinAddress &address) const
9288 {
9289 CRITICAL_BLOCK(cs_KeyStore)
9290 {
9291 if (!IsCrypted())
9292 return CBasicKeyStore::HaveKey(address);
9293 return mapCryptedKeys.count(address) > 0;
9294 }
9295 return false;
9296 }
9297 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
9298 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
9299 };
9300
9301 #endif