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 CBitcoinAddress &address) const =0;
16 virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
17 virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
18 virtual std::vector<unsigned char> GenerateNewKey();
21 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
23 class CBasicKeyStore : public CKeyStore
29 bool AddKey(const CKey& key);
30 bool HaveKey(const CBitcoinAddress &address) const
32 return (mapKeys.count(address) > 0);
34 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
36 KeyMap::const_iterator mi = mapKeys.find(address);
37 if (mi != mapKeys.end())
39 keyOut.SetSecret((*mi).second);
46 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
48 class CCryptoKeyStore : public CBasicKeyStore
51 CryptedKeyMap mapCryptedKeys;
53 CKeyingMaterial vMasterKey;
55 // if fUseCrypto is true, mapKeys must be empty
56 // if fUseCrypto is false, vMasterKey must be empty
70 // will encrypt previously unencrypted keys
71 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
73 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
76 mutable CCriticalSection cs_vMasterKey; //No guarantees master key wont get locked before you can use it, so lock this first
78 CCryptoKeyStore() : fUseCrypto(false)
82 bool IsCrypted() const
91 return vMasterKey.empty();
96 CRITICAL_BLOCK(cs_vMasterKey)
106 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
107 std::vector<unsigned char> GenerateNewKey();
108 bool AddKey(const CKey& key);
109 bool HaveKey(const CBitcoinAddress &address) const
112 return CBasicKeyStore::HaveKey(address);
113 return mapCryptedKeys.count(address) > 0;
115 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
116 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;