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
78 // will encrypt previously unencrypted keys
79 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
81 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
84 CCryptoKeyStore() : fUseCrypto(false)
88 bool IsCrypted() const
98 CRITICAL_BLOCK(cs_KeyStore)
99 result = vMasterKey.empty();
108 CRITICAL_BLOCK(cs_KeyStore)
114 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
115 std::vector<unsigned char> GenerateNewKey();
116 bool AddKey(const CKey& key);
117 bool HaveKey(const CBitcoinAddress &address) const
119 CRITICAL_BLOCK(cs_KeyStore)
122 return CBasicKeyStore::HaveKey(address);
123 return mapCryptedKeys.count(address) > 0;
126 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
127 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;