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 virtual bool GetSecret(const CBitcoinAddress &address, CSecret& vchSecret, bool &fCompressed) const
35 if (!GetKey(address, key))
37 vchSecret = key.GetSecret(fCompressed);
42 typedef std::map<CBitcoinAddress, std::pair<CSecret, bool> > KeyMap;
43 typedef std::map<uint160, CScript > ScriptMap;
45 /** Basic key store, that keeps keys in an address->secret map */
46 class CBasicKeyStore : public CKeyStore
53 bool AddKey(const CKey& key);
54 bool HaveKey(const CBitcoinAddress &address) const
57 CRITICAL_BLOCK(cs_KeyStore)
58 result = (mapKeys.count(address) > 0);
61 void GetKeys(std::set<CBitcoinAddress> &setAddress) const
64 CRITICAL_BLOCK(cs_KeyStore)
66 KeyMap::const_iterator mi = mapKeys.begin();
67 while (mi != mapKeys.end())
69 setAddress.insert((*mi).first);
74 bool GetKey(const CBitcoinAddress &address, CKey &keyOut) const
76 CRITICAL_BLOCK(cs_KeyStore)
78 KeyMap::const_iterator mi = mapKeys.find(address);
79 if (mi != mapKeys.end())
82 keyOut.SetSecret((*mi).second.first, (*mi).second.second);
88 virtual bool AddCScript(const CScript& redeemScript);
89 virtual bool HaveCScript(const uint160 &hash) const;
90 virtual bool GetCScript(const uint160 &hash, CScript& redeemScriptOut) const;
93 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
95 /** Keystore which keeps the private keys encrypted.
96 * It derives from the basic key store, which is used if no encryption is active.
98 class CCryptoKeyStore : public CBasicKeyStore
101 CryptedKeyMap mapCryptedKeys;
103 CKeyingMaterial vMasterKey;
105 // if fUseCrypto is true, mapKeys must be empty
106 // if fUseCrypto is false, vMasterKey must be empty
112 // will encrypt previously unencrypted keys
113 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
115 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
118 CCryptoKeyStore() : fUseCrypto(false)
122 bool IsCrypted() const
127 bool IsLocked() const
132 CRITICAL_BLOCK(cs_KeyStore)
133 result = vMasterKey.empty();
142 CRITICAL_BLOCK(cs_KeyStore)
148 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
149 bool AddKey(const CKey& key);
150 bool HaveKey(const CBitcoinAddress &address) const
152 CRITICAL_BLOCK(cs_KeyStore)
155 return CBasicKeyStore::HaveKey(address);
156 return mapCryptedKeys.count(address) > 0;
160 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
161 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
162 void GetKeys(std::set<CBitcoinAddress> &setAddress) const
166 CBasicKeyStore::GetKeys(setAddress);
170 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
171 while (mi != mapCryptedKeys.end())
173 setAddress.insert((*mi).first);