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 COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_KEYSTORE_H
6 #define BITCOIN_KEYSTORE_H
13 mutable CCriticalSection cs_KeyStore;
16 virtual ~CKeyStore() {}
18 virtual bool AddKey(const CKey& key) =0;
19 virtual bool HaveKey(const CBitcoinAddress &address) const =0;
20 virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
21 virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
22 virtual std::vector<unsigned char> GenerateNewKey();
25 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
27 class CBasicKeyStore : public CKeyStore
33 bool AddKey(const CKey& key);
34 bool HaveKey(const CBitcoinAddress &address) const
37 CRITICAL_BLOCK(cs_KeyStore)
38 result = (mapKeys.count(address) > 0);
41 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
43 CRITICAL_BLOCK(cs_KeyStore)
45 KeyMap::const_iterator mi = mapKeys.find(address);
46 if (mi != mapKeys.end())
48 keyOut.SetSecret((*mi).second);
56 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
58 class CCryptoKeyStore : public CBasicKeyStore
61 CryptedKeyMap mapCryptedKeys;
63 CKeyingMaterial vMasterKey;
65 // if fUseCrypto is true, mapKeys must be empty
66 // if fUseCrypto is false, vMasterKey must be empty
72 // will encrypt previously unencrypted keys
73 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
75 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
78 CCryptoKeyStore() : fUseCrypto(false)
82 bool IsCrypted() const
92 CRITICAL_BLOCK(cs_KeyStore)
93 result = vMasterKey.empty();
102 CRITICAL_BLOCK(cs_KeyStore)
108 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
109 std::vector<unsigned char> GenerateNewKey();
110 bool AddKey(const CKey& key);
111 bool HaveKey(const CBitcoinAddress &address) const
113 CRITICAL_BLOCK(cs_KeyStore)
116 return CBasicKeyStore::HaveKey(address);
117 return mapCryptedKeys.count(address) > 0;
121 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
122 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;