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
10 // A virtual base class for key stores
14 mutable CCriticalSection cs_KeyStore;
17 // Add a key to the store.
18 virtual bool AddKey(const CKey& key) =0;
20 // Check whether a key corresponding to a given address is present in the store.
21 virtual bool HaveKey(const CBitcoinAddress &address) const =0;
23 // Retrieve a key corresponding to a given address from the store.
24 // Return true if succesful.
25 virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
27 // Retrieve only the public key corresponding to a given address.
28 // This may succeed even if GetKey fails (e.g., encrypted wallets)
29 virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
31 // Generate a new key, and add it to the store
32 virtual std::vector<unsigned char> GenerateNewKey();
35 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
37 // Basic key store, that keeps keys in an address->secret map
38 class CBasicKeyStore : public CKeyStore
44 bool AddKey(const CKey& key);
45 bool HaveKey(const CBitcoinAddress &address) const
48 CRITICAL_BLOCK(cs_KeyStore)
49 result = (mapKeys.count(address) > 0);
52 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
54 CRITICAL_BLOCK(cs_KeyStore)
56 KeyMap::const_iterator mi = mapKeys.find(address);
57 if (mi != mapKeys.end())
59 keyOut.SetSecret((*mi).second);
67 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
69 // Keystore which keeps the private keys encrypted
70 // It derives from the basic key store, which is used if no encryption is active.
71 class CCryptoKeyStore : public CBasicKeyStore
74 CryptedKeyMap mapCryptedKeys;
76 CKeyingMaterial vMasterKey;
78 // if fUseCrypto is true, mapKeys must be empty
79 // if fUseCrypto is false, vMasterKey must be empty
85 // will encrypt previously unencrypted keys
86 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
88 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
91 CCryptoKeyStore() : fUseCrypto(false)
95 bool IsCrypted() const
100 bool IsLocked() const
105 CRITICAL_BLOCK(cs_KeyStore)
106 result = vMasterKey.empty();
115 CRITICAL_BLOCK(cs_KeyStore)
121 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
122 std::vector<unsigned char> GenerateNewKey();
123 bool AddKey(const CKey& key);
124 bool HaveKey(const CBitcoinAddress &address) const
126 CRITICAL_BLOCK(cs_KeyStore)
129 return CBasicKeyStore::HaveKey(address);
130 return mapCryptedKeys.count(address) > 0;
134 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
135 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;