1 // Copyright (c) 2009-2011 Satoshi Nakamoto & Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4 #ifndef BITCOIN_KEYSTORE_H
5 #define BITCOIN_KEYSTORE_H
12 mutable CCriticalSection cs_KeyStore;
14 virtual bool AddKey(const CKey& key) =0;
15 virtual bool HaveKey(const std::vector<unsigned char> &vchPubKey) const =0;
16 virtual bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CKey& keyOut) const =0;
17 virtual std::vector<unsigned char> GenerateNewKey();
20 typedef std::map<std::vector<unsigned char>, CPrivKey> KeyMap;
22 class CBasicKeyStore : public CKeyStore
28 bool AddKey(const CKey& key);
29 bool HaveKey(const std::vector<unsigned char> &vchPubKey) const
31 return (mapKeys.count(vchPubKey) > 0);
33 bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CKey& keyOut) const
35 std::map<std::vector<unsigned char>, CPrivKey>::const_iterator mi = mapKeys.find(vchPubKey);
36 if (mi != mapKeys.end())
38 keyOut.SetPrivKey((*mi).second);
45 class CCryptoKeyStore : public CBasicKeyStore
48 std::map<std::vector<unsigned char>, std::vector<unsigned char> > mapCryptedKeys;
50 CKeyingMaterial vMasterKey;
52 // if fUseCrypto is true, mapKeys must be empty
53 // if fUseCrypto is false, vMasterKey must be empty
67 // will encrypt previously unencrypted keys
68 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
70 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
73 mutable CCriticalSection cs_vMasterKey; //No guarantees master key wont get locked before you can use it, so lock this first
75 CCryptoKeyStore() : fUseCrypto(false)
79 bool IsCrypted() const
88 return vMasterKey.empty();
93 CRITICAL_BLOCK(cs_vMasterKey)
103 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
104 std::vector<unsigned char> GenerateNewKey();
105 bool AddKey(const CKey& key);
106 bool HaveKey(const std::vector<unsigned char> &vchPubKey) const
109 return CBasicKeyStore::HaveKey(vchPubKey);
110 return mapCryptedKeys.count(vchPubKey) > 0;
112 bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CKey& keyOut) const;