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 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 AddKey(const CKey& key) =0;
26 // Check whether a key corresponding to a given address is present in the store.
27 virtual bool HaveKey(const CKeyID &address) const =0;
28 virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
29 virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
30 virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
32 // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
33 virtual bool AddCScript(const CScript& redeemScript) =0;
34 virtual bool HaveCScript(const CScriptID &hash) const =0;
35 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
37 virtual bool GetSecret(const CKeyID &address, CSecret& vchSecret, bool &fCompressed) const
40 if (!GetKey(address, key))
42 vchSecret = key.GetSecret(fCompressed);
47 typedef std::map<CKeyID, std::pair<CSecret, bool> > KeyMap;
48 typedef std::map<CScriptID, CScript > ScriptMap;
50 /** Basic key store, that keeps keys in an address->secret map */
51 class CBasicKeyStore : public CKeyStore
58 bool AddKey(const CKey& key);
59 bool HaveKey(const CKeyID &address) const
64 result = (mapKeys.count(address) > 0);
68 void GetKeys(std::set<CKeyID> &setAddress) const
73 KeyMap::const_iterator mi = mapKeys.begin();
74 while (mi != mapKeys.end())
76 setAddress.insert((*mi).first);
81 bool GetKey(const CKeyID &address, CKey &keyOut) const
85 KeyMap::const_iterator mi = mapKeys.find(address);
86 if (mi != mapKeys.end())
89 keyOut.SetSecret((*mi).second.first, (*mi).second.second);
95 virtual bool AddCScript(const CScript& redeemScript);
96 virtual bool HaveCScript(const CScriptID &hash) const;
97 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
100 typedef std::map<CKeyID, std::pair<CPubKey, 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.
105 class CCryptoKeyStore : public CBasicKeyStore
108 CryptedKeyMap mapCryptedKeys;
110 CKeyingMaterial vMasterKey;
112 // if fUseCrypto is true, mapKeys must be empty
113 // if fUseCrypto is false, vMasterKey must be empty
119 // will encrypt previously unencrypted keys
120 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
122 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
125 CCryptoKeyStore() : fUseCrypto(false)
129 bool IsCrypted() const
134 bool IsLocked() const
141 result = vMasterKey.empty();
148 virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
149 bool AddKey(const CKey& key);
150 bool HaveKey(const CKeyID &address) const
155 return CBasicKeyStore::HaveKey(address);
156 return mapCryptedKeys.count(address) > 0;
160 bool GetKey(const CKeyID &address, CKey& keyOut) const;
161 bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
162 void GetKeys(std::set<CKeyID> &setAddress) const
166 CBasicKeyStore::GetKeys(setAddress);
170 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
171 while (mi != mapCryptedKeys.end())
173 setAddress.insert((*mi).first);
178 /* Wallet status (encrypted, locked) changed.
179 * Note: Called without locks held.
181 boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;