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 virtual bool AddCScript(const uint160 &hash, const std::vector<unsigned char>& data) =0;
35 virtual bool HaveCScript(const uint160 &hash) const =0;
36 virtual bool GetCScript(const uint160 &hash, std::vector<unsigned char>& dataOut) const =0;
38 // Generate a new key, and add it to the store
39 virtual std::vector<unsigned char> GenerateNewKey();
40 virtual bool GetSecret(const CBitcoinAddress &address, CSecret& vchSecret) const
43 if (!GetKey(address, key))
45 vchSecret = key.GetSecret();
50 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
51 typedef std::map<uint160, std::vector<unsigned char> > DataMap;
53 // Basic key store, that keeps keys in an address->secret map
54 class CBasicKeyStore : public CKeyStore
61 bool AddKey(const CKey& key);
62 bool HaveKey(const CBitcoinAddress &address) const
65 CRITICAL_BLOCK(cs_KeyStore)
66 result = (mapKeys.count(address) > 0);
69 void GetKeys(std::set<CBitcoinAddress> &setAddress) const
72 CRITICAL_BLOCK(cs_KeyStore)
74 KeyMap::const_iterator mi = mapKeys.begin();
75 while (mi != mapKeys.end())
77 setAddress.insert((*mi).first);
82 bool GetSecret(const CBitcoinAddress &address, CSecret &vchSecret) const
84 CRITICAL_BLOCK(cs_KeyStore)
86 KeyMap::const_iterator mi = mapKeys.find(address);
87 if (mi != mapKeys.end())
89 vchSecret = (*mi).second;
95 virtual bool AddCScript(const uint160 &hash, const std::vector<unsigned char>& data);
96 virtual bool HaveCScript(const uint160 &hash) const;
97 virtual bool GetCScript(const uint160 &hash, std::vector<unsigned char>& dataOut) const;
100 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
102 // Keystore which keeps the private keys encrypted
103 // It derives from the basic key store, which is used if no encryption is active.
104 class CCryptoKeyStore : public CBasicKeyStore
107 CryptedKeyMap mapCryptedKeys;
109 CKeyingMaterial vMasterKey;
111 // if fUseCrypto is true, mapKeys must be empty
112 // if fUseCrypto is false, vMasterKey must be empty
118 // will encrypt previously unencrypted keys
119 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
121 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
124 CCryptoKeyStore() : fUseCrypto(false)
128 bool IsCrypted() const
133 bool IsLocked() const
138 CRITICAL_BLOCK(cs_KeyStore)
139 result = vMasterKey.empty();
148 CRITICAL_BLOCK(cs_KeyStore)
154 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
155 std::vector<unsigned char> GenerateNewKey();
156 bool AddKey(const CKey& key);
157 bool HaveKey(const CBitcoinAddress &address) const
159 CRITICAL_BLOCK(cs_KeyStore)
162 return CBasicKeyStore::HaveKey(address);
163 return mapCryptedKeys.count(address) > 0;
167 bool GetSecret(const CBitcoinAddress &address, CSecret& vchSecret) const;
168 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
169 void GetKeys(std::set<CBitcoinAddress> &setAddress) const
173 CBasicKeyStore::GetKeys(setAddress);
177 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
178 while (mi != mapCryptedKeys.end())
180 setAddress.insert((*mi).first);