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;
16 virtual bool AddKey(const CKey& key) =0;
17 virtual bool HaveKey(const CBitcoinAddress &address) const =0;
18 virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
19 virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
20 virtual std::vector<unsigned char> GenerateNewKey();
23 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
25 class CBasicKeyStore : public CKeyStore
31 bool AddKey(const CKey& key);
32 bool HaveKey(const CBitcoinAddress &address) const
35 CRITICAL_BLOCK(cs_KeyStore)
36 result = (mapKeys.count(address) > 0);
39 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
41 CRITICAL_BLOCK(cs_KeyStore)
43 KeyMap::const_iterator mi = mapKeys.find(address);
44 if (mi != mapKeys.end())
46 keyOut.SetSecret((*mi).second);
54 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
56 class CCryptoKeyStore : public CBasicKeyStore
59 CryptedKeyMap mapCryptedKeys;
61 CKeyingMaterial vMasterKey;
63 // if fUseCrypto is true, mapKeys must be empty
64 // 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 CCryptoKeyStore() : fUseCrypto(false)
80 bool IsCrypted() const
90 CRITICAL_BLOCK(cs_KeyStore)
91 result = vMasterKey.empty();
100 CRITICAL_BLOCK(cs_KeyStore)
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
111 CRITICAL_BLOCK(cs_KeyStore)
114 return CBasicKeyStore::HaveKey(address);
115 return mapCryptedKeys.count(address) > 0;
119 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
120 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;