1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 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
11 // A virtual base class for key stores
15 mutable CCriticalSection cs_KeyStore;
18 // Add a key to the store.
19 virtual bool AddKey(const CKey& key) =0;
21 // Check whether a key corresponding to a given address is present in the store.
22 virtual bool HaveKey(const CBitcoinAddress &address) const =0;
23 virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
24 virtual void GetKeys(std::set<CBitcoinAddress> &setAddress) const =0;
25 virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
27 // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
28 virtual bool AddCScript(const CScript& redeemScript) =0;
29 virtual bool HaveCScript(const uint160 &hash) const =0;
30 virtual bool GetCScript(const uint160 &hash, CScript& redeemScriptOut) const =0;
32 // Generate a new key, and add it to the store
33 virtual std::vector<unsigned char> GenerateNewKey();
34 virtual bool GetSecret(const CBitcoinAddress &address, CSecret& vchSecret, bool &fCompressed) const
37 if (!GetKey(address, key))
39 vchSecret = key.GetSecret(fCompressed);
44 typedef std::map<CBitcoinAddress, std::pair<CSecret, bool> > KeyMap;
45 typedef std::map<uint160, CScript > ScriptMap;
47 // Basic key store, that keeps keys in an address->secret map
48 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 GetKey(const CBitcoinAddress &address, CKey &keyOut) const
78 CRITICAL_BLOCK(cs_KeyStore)
80 KeyMap::const_iterator mi = mapKeys.find(address);
81 if (mi != mapKeys.end())
84 keyOut.SetSecret((*mi).second.first, (*mi).second.second);
90 virtual bool AddCScript(const CScript& redeemScript);
91 virtual bool HaveCScript(const uint160 &hash) const;
92 virtual bool GetCScript(const uint160 &hash, CScript& redeemScriptOut) const;
95 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
97 // Keystore which keeps the private keys encrypted
98 // It derives from the basic key store, which is used if no encryption is active.
99 class CCryptoKeyStore : public CBasicKeyStore
102 CryptedKeyMap mapCryptedKeys;
104 CKeyingMaterial vMasterKey;
106 // if fUseCrypto is true, mapKeys must be empty
107 // if fUseCrypto is false, vMasterKey must be empty
113 // will encrypt previously unencrypted keys
114 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
116 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
119 CCryptoKeyStore() : fUseCrypto(false)
123 bool IsCrypted() const
128 bool IsLocked() const
133 CRITICAL_BLOCK(cs_KeyStore)
134 result = vMasterKey.empty();
143 CRITICAL_BLOCK(cs_KeyStore)
149 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
150 bool AddKey(const CKey& key);
151 bool HaveKey(const CBitcoinAddress &address) const
153 CRITICAL_BLOCK(cs_KeyStore)
156 return CBasicKeyStore::HaveKey(address);
157 return mapCryptedKeys.count(address) > 0;
161 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
162 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
163 void GetKeys(std::set<CBitcoinAddress> &setAddress) const
167 CBasicKeyStore::GetKeys(setAddress);
171 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
172 while (mi != mapCryptedKeys.end())
174 setAddress.insert((*mi).first);