1 // Copyright (c) 2009-2011 Satoshi Nakamoto & Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4 #ifndef BITCOIN_KEYSTORE_H
5 #define BITCOIN_KEYSTORE_H
7 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CMasterKey;
12 mutable CCriticalSection cs_KeyStore;
14 virtual bool AddKey(const CKey& key) =0;
15 virtual bool HaveKey(const std::vector<unsigned char> &vchPubKey) const =0;
16 virtual bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CPrivKey& keyOut) const =0;
17 virtual std::vector<unsigned char> GenerateNewKey();
20 class CBasicKeyStore : public CKeyStore
23 std::map<std::vector<unsigned char>, CPrivKey> mapKeys;
26 bool AddKey(const CKey& key);
27 bool HaveKey(const std::vector<unsigned char> &vchPubKey) const
29 return (mapKeys.count(vchPubKey) > 0);
31 bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CPrivKey& keyOut) const
33 std::map<std::vector<unsigned char>, CPrivKey>::const_iterator mi = mapKeys.find(vchPubKey);
34 if (mi != mapKeys.end())
36 keyOut = (*mi).second;
43 class CCryptoKeyStore : public CBasicKeyStore
46 std::map<std::vector<unsigned char>, std::vector<unsigned char> > mapCryptedKeys;
48 CMasterKey vMasterKey;
50 // if fUseCrypto is true, mapKeys must be empty
51 // if fUseCrypto is false, vMasterKey must be empty
55 bool IsCrypted() const
69 // will encrypt previously unencrypted keys
70 bool GenerateMasterKey();
72 bool GetMasterKey(CMasterKey &vMasterKeyOut) const
78 vMasterKeyOut = vMasterKey;
81 bool Unlock(const CMasterKey& vMasterKeyIn);
84 CCryptoKeyStore() : fUseCrypto(false)
92 return vMasterKey.empty();
102 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
103 bool AddKey(const CKey& key);
104 bool HaveKey(const std::vector<unsigned char> &vchPubKey) const
107 return CBasicKeyStore::HaveKey(vchPubKey);
108 return mapCryptedKeys.count(vchPubKey) > 0;
110 bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CPrivKey& keyOut) const;