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.
5 #ifndef BITCOIN_KEYSTORE_H
6 #define BITCOIN_KEYSTORE_H
10 // A virtual base class for key stores
14 mutable CCriticalSection cs_KeyStore;
17 virtual ~CKeyStore() {}
19 // Add a key to the store.
20 virtual bool AddKey(const CKey& key) =0;
22 // Check whether a key corresponding to a given address is present in the store.
23 virtual bool HaveKey(const CBitcoinAddress &address) const =0;
25 // Retrieve a key corresponding to a given address from the store.
26 // Return true if succesful.
27 virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
29 // Retrieve only the public key corresponding to a given address.
30 // This may succeed even if GetKey fails (e.g., encrypted wallets)
31 virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
33 // Generate a new key, and add it to the store
34 virtual std::vector<unsigned char> GenerateNewKey();
37 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
39 // Basic key store, that keeps keys in an address->secret map
40 class CBasicKeyStore : public CKeyStore
46 bool AddKey(const CKey& key);
47 bool HaveKey(const CBitcoinAddress &address) const
50 CRITICAL_BLOCK(cs_KeyStore)
51 result = (mapKeys.count(address) > 0);
54 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
56 CRITICAL_BLOCK(cs_KeyStore)
58 KeyMap::const_iterator mi = mapKeys.find(address);
59 if (mi != mapKeys.end())
61 keyOut.SetSecret((*mi).second);
69 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
71 // Keystore which keeps the private keys encrypted
72 // It derives from the basic key store, which is used if no encryption is active.
73 class CCryptoKeyStore : public CBasicKeyStore
76 CryptedKeyMap mapCryptedKeys;
78 CKeyingMaterial vMasterKey;
80 // if fUseCrypto is true, mapKeys must be empty
81 // if fUseCrypto is false, vMasterKey must be empty
87 // will encrypt previously unencrypted keys
88 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
90 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
93 CCryptoKeyStore() : fUseCrypto(false)
97 bool IsCrypted() const
102 bool IsLocked() const
107 CRITICAL_BLOCK(cs_KeyStore)
108 result = vMasterKey.empty();
117 CRITICAL_BLOCK(cs_KeyStore)
123 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
124 std::vector<unsigned char> GenerateNewKey();
125 bool AddKey(const CKey& key);
126 bool HaveKey(const CBitcoinAddress &address) const
128 CRITICAL_BLOCK(cs_KeyStore)
131 return CBasicKeyStore::HaveKey(address);
132 return mapCryptedKeys.count(address) > 0;
136 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
137 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;