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
13 // A virtual base class for key stores
17 mutable CCriticalSection cs_KeyStore;
20 // Add a key to the store.
21 virtual bool AddKey(const CKey& key) =0;
23 // Check whether a key corresponding to a given address is present in the store.
24 virtual bool HaveKey(const CBitcoinAddress &address) const =0;
25 virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
28 if (!GetSecret(address, vchSecret))
30 if (!keyOut.SetSecret(vchSecret))
34 virtual void GetKeys(std::set<CBitcoinAddress> &setAddress) const =0;
35 virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
37 // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
38 virtual bool AddCScript(const uint160 &hash, const CScript& redeemScript) =0;
39 virtual bool HaveCScript(const uint160 &hash) const =0;
40 virtual bool GetCScript(const uint160 &hash, CScript& redeemScriptOut) const =0;
42 // Generate a new key, and add it to the store
43 virtual std::vector<unsigned char> GenerateNewKey();
44 virtual bool GetSecret(const CBitcoinAddress &address, CSecret& vchSecret) const
47 if (!GetKey(address, key))
49 vchSecret = key.GetSecret();
54 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
55 typedef std::map<uint160, CScript > ScriptMap;
57 // Basic key store, that keeps keys in an address->secret map
58 class CBasicKeyStore : public CKeyStore
65 bool AddKey(const CKey& key);
66 bool HaveKey(const CBitcoinAddress &address) const
69 CRITICAL_BLOCK(cs_KeyStore)
70 result = (mapKeys.count(address) > 0);
73 void GetKeys(std::set<CBitcoinAddress> &setAddress) const
76 CRITICAL_BLOCK(cs_KeyStore)
78 KeyMap::const_iterator mi = mapKeys.begin();
79 while (mi != mapKeys.end())
81 setAddress.insert((*mi).first);
86 bool GetSecret(const CBitcoinAddress &address, CSecret &vchSecret) const
88 CRITICAL_BLOCK(cs_KeyStore)
90 KeyMap::const_iterator mi = mapKeys.find(address);
91 if (mi != mapKeys.end())
93 vchSecret = (*mi).second;
99 virtual bool AddCScript(const uint160 &hash, const CScript& redeemScript);
100 virtual bool HaveCScript(const uint160 &hash) const;
101 virtual bool GetCScript(const uint160 &hash, CScript& redeemScriptOut) const;
104 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
106 // Keystore which keeps the private keys encrypted
107 // It derives from the basic key store, which is used if no encryption is active.
108 class CCryptoKeyStore : public CBasicKeyStore
111 CryptedKeyMap mapCryptedKeys;
113 CKeyingMaterial vMasterKey;
115 // if fUseCrypto is true, mapKeys must be empty
116 // if fUseCrypto is false, vMasterKey must be empty
122 // will encrypt previously unencrypted keys
123 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
125 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
128 CCryptoKeyStore() : fUseCrypto(false)
132 bool IsCrypted() const
137 bool IsLocked() const
142 CRITICAL_BLOCK(cs_KeyStore)
143 result = vMasterKey.empty();
152 CRITICAL_BLOCK(cs_KeyStore)
158 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
159 std::vector<unsigned char> GenerateNewKey();
160 bool AddKey(const CKey& key);
161 bool HaveKey(const CBitcoinAddress &address) const
163 CRITICAL_BLOCK(cs_KeyStore)
166 return CBasicKeyStore::HaveKey(address);
167 return mapCryptedKeys.count(address) > 0;
171 bool GetSecret(const CBitcoinAddress &address, CSecret& vchSecret) const;
172 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
173 void GetKeys(std::set<CBitcoinAddress> &setAddress) const
177 CBasicKeyStore::GetKeys(setAddress);
181 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
182 while (mi != mapCryptedKeys.end())
184 setAddress.insert((*mi).first);