1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_KEYSTORE_H
6 #define BITCOIN_KEYSTORE_H
10 #include <boost/signals2/signal.hpp>
14 /** A virtual base class for key stores */
18 mutable CCriticalSection cs_KeyStore;
21 virtual ~CKeyStore() {}
23 // Add a key to the store.
24 virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0;
25 virtual bool AddKey(const CKey &key);
27 // Check whether a key corresponding to a given address is present in the store.
28 virtual bool HaveKey(const CKeyID &address) const =0;
29 virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
30 virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
31 virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
33 // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
34 virtual bool AddCScript(const CScript& redeemScript) =0;
35 virtual bool HaveCScript(const CScriptID &hash) const =0;
36 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
39 typedef std::map<CKeyID, CKey> KeyMap;
40 typedef std::map<CScriptID, CScript > ScriptMap;
42 /** Basic key store, that keeps keys in an address->secret map */
43 class CBasicKeyStore : public CKeyStore
50 bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
51 bool HaveKey(const CKeyID &address) const
56 result = (mapKeys.count(address) > 0);
60 void GetKeys(std::set<CKeyID> &setAddress) const
65 KeyMap::const_iterator mi = mapKeys.begin();
66 while (mi != mapKeys.end())
68 setAddress.insert((*mi).first);
73 bool GetKey(const CKeyID &address, CKey &keyOut) const
77 KeyMap::const_iterator mi = mapKeys.find(address);
78 if (mi != mapKeys.end())
86 virtual bool AddCScript(const CScript& redeemScript);
87 virtual bool HaveCScript(const CScriptID &hash) const;
88 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
91 typedef std::map<CKeyID, std::pair<CPubKey, 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.
96 class CCryptoKeyStore : public CBasicKeyStore
99 CryptedKeyMap mapCryptedKeys;
101 CKeyingMaterial vMasterKey;
103 // if fUseCrypto is true, mapKeys must be empty
104 // if fUseCrypto is false, vMasterKey must be empty
110 // will encrypt previously unencrypted keys
111 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
113 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
116 CCryptoKeyStore() : fUseCrypto(false)
120 bool IsCrypted() const
125 bool IsLocked() const
132 result = vMasterKey.empty();
139 virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
140 bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
141 bool HaveKey(const CKeyID &address) const
146 return CBasicKeyStore::HaveKey(address);
147 return mapCryptedKeys.count(address) > 0;
151 bool GetKey(const CKeyID &address, CKey& keyOut) const;
152 bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
153 void GetKeys(std::set<CKeyID> &setAddress) const
157 CBasicKeyStore::GetKeys(setAddress);
161 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
162 while (mi != mapCryptedKeys.end())
164 setAddress.insert((*mi).first);
169 /* Wallet status (encrypted, locked) changed.
170 * Note: Called without locks held.
172 boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;