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 // Add a key to the store.
18 virtual bool AddKey(const CKey& key) =0;
20 // Check whether a key corresponding to a given address is present in the store.
21 virtual bool HaveKey(const CBitcoinAddress &address) const =0;
22 virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
25 if (!GetSecret(address, vchSecret))
27 if (!keyOut.SetSecret(vchSecret))
31 virtual void GetKeys(std::set<CBitcoinAddress> &setAddress) const =0;
32 virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
34 // Generate a new key, and add it to the store
35 virtual std::vector<unsigned char> GenerateNewKey();
36 virtual bool GetSecret(const CBitcoinAddress &address, CSecret& vchSecret) const
39 if (!GetKey(address, key))
41 vchSecret = key.GetSecret();
46 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
48 // Basic key store, that keeps keys in an address->secret map
49 class CBasicKeyStore : public CKeyStore
55 bool AddKey(const CKey& key);
56 bool HaveKey(const CBitcoinAddress &address) const
59 CRITICAL_BLOCK(cs_KeyStore)
60 result = (mapKeys.count(address) > 0);
63 void GetKeys(std::set<CBitcoinAddress> &setAddress) const
66 CRITICAL_BLOCK(cs_KeyStore)
68 KeyMap::const_iterator mi = mapKeys.begin();
69 while (mi != mapKeys.end())
71 setAddress.insert((*mi).first);
76 bool GetSecret(const CBitcoinAddress &address, CSecret &vchSecret) const
78 CRITICAL_BLOCK(cs_KeyStore)
80 KeyMap::const_iterator mi = mapKeys.find(address);
81 if (mi != mapKeys.end())
83 vchSecret = (*mi).second;
91 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
93 // Keystore which keeps the private keys encrypted
94 // It derives from the basic key store, which is used if no encryption is active.
95 class CCryptoKeyStore : public CBasicKeyStore
98 CryptedKeyMap mapCryptedKeys;
100 CKeyingMaterial vMasterKey;
102 // if fUseCrypto is true, mapKeys must be empty
103 // if fUseCrypto is false, vMasterKey must be empty
109 // will encrypt previously unencrypted keys
110 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
112 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
115 CCryptoKeyStore() : fUseCrypto(false)
119 bool IsCrypted() const
124 bool IsLocked() const
129 CRITICAL_BLOCK(cs_KeyStore)
130 result = vMasterKey.empty();
139 CRITICAL_BLOCK(cs_KeyStore)
145 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
146 std::vector<unsigned char> GenerateNewKey();
147 bool AddKey(const CKey& key);
148 bool HaveKey(const CBitcoinAddress &address) const
150 CRITICAL_BLOCK(cs_KeyStore)
153 return CBasicKeyStore::HaveKey(address);
154 return mapCryptedKeys.count(address) > 0;
158 bool GetSecret(const CBitcoinAddress &address, CSecret& vchSecret) const;
159 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
160 void GetKeys(std::set<CBitcoinAddress> &setAddress) const
164 CBasicKeyStore::GetKeys(setAddress);
168 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
169 while (mi != mapCryptedKeys.end())
171 setAddress.insert((*mi).first);