1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 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
13 mutable CCriticalSection cs_KeyStore;
15 virtual bool AddKey(const CKey& key) =0;
16 virtual bool HaveKey(const CBitcoinAddress &address) const =0;
17 virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
18 virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
19 virtual std::vector<unsigned char> GenerateNewKey();
22 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
24 class CBasicKeyStore : public CKeyStore
30 bool AddKey(const CKey& key);
31 bool HaveKey(const CBitcoinAddress &address) const
33 return (mapKeys.count(address) > 0);
35 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
37 KeyMap::const_iterator mi = mapKeys.find(address);
38 if (mi != mapKeys.end())
40 keyOut.SetSecret((*mi).second);
47 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
49 class CCryptoKeyStore : public CBasicKeyStore
52 CryptedKeyMap mapCryptedKeys;
54 CKeyingMaterial vMasterKey;
56 // if fUseCrypto is true, mapKeys must be empty
57 // if fUseCrypto is false, vMasterKey must be empty
71 // will encrypt previously unencrypted keys
72 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
74 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
77 mutable CCriticalSection cs_vMasterKey; //No guarantees master key wont get locked before you can use it, so lock this first
79 CCryptoKeyStore() : fUseCrypto(false)
83 bool IsCrypted() const
92 return vMasterKey.empty();
97 CRITICAL_BLOCK(cs_vMasterKey)
107 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
108 std::vector<unsigned char> GenerateNewKey();
109 bool AddKey(const CKey& key);
110 bool HaveKey(const CBitcoinAddress &address) const
113 return CBasicKeyStore::HaveKey(address);
114 return mapCryptedKeys.count(address) > 0;
116 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
117 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;