X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fkeystore.h;h=8d445befea3e76bea698d600ff6db2d8e41ee567;hb=4e87d341f75f13bbd7d108c31c03886fbc4df56f;hp=6080d7d7f5f9cf1ba9290ad71d5f8b9a81e434e0;hpb=98705aa51cbfee81ecd2498a014c285ac677ba69;p=novacoin.git diff --git a/src/keystore.h b/src/keystore.h index 6080d7d..8d445be 100644 --- a/src/keystore.h +++ b/src/keystore.h @@ -4,12 +4,28 @@ #ifndef BITCOIN_KEYSTORE_H #define BITCOIN_KEYSTORE_H +#include "crypter.h" + class CKeyStore { public: - std::map, CPrivKey> mapKeys; - mutable CCriticalSection cs_mapKeys; - virtual bool AddKey(const CKey& key); + mutable CCriticalSection cs_KeyStore; + + virtual bool AddKey(const CKey& key) =0; + virtual bool HaveKey(const std::vector &vchPubKey) const =0; + virtual bool GetPrivKey(const std::vector &vchPubKey, CPrivKey& keyOut) const =0; + virtual std::vector GenerateNewKey(); +}; + +typedef std::map, CPrivKey> KeyMap; + +class CBasicKeyStore : public CKeyStore +{ +protected: + KeyMap mapKeys; + +public: + bool AddKey(const CKey& key); bool HaveKey(const std::vector &vchPubKey) const { return (mapKeys.count(vchPubKey) > 0); @@ -24,7 +40,76 @@ public: } return false; } +}; + +class CCryptoKeyStore : public CBasicKeyStore +{ +private: + std::map, std::vector > mapCryptedKeys; + + CKeyingMaterial vMasterKey; + + // if fUseCrypto is true, mapKeys must be empty + // if fUseCrypto is false, vMasterKey must be empty + bool fUseCrypto; + +protected: + bool SetCrypted() + { + if (fUseCrypto) + return true; + if (!mapKeys.empty()) + return false; + fUseCrypto = true; + return true; + } + + // will encrypt previously unencrypted keys + bool EncryptKeys(CKeyingMaterial& vMasterKeyIn); + + bool Unlock(const CKeyingMaterial& vMasterKeyIn); + +public: + mutable CCriticalSection cs_vMasterKey; //No guarantees master key wont get locked before you can use it, so lock this first + + CCryptoKeyStore() : fUseCrypto(false) + { + } + + bool IsCrypted() const + { + return fUseCrypto; + } + + bool IsLocked() const + { + if (!IsCrypted()) + return false; + return vMasterKey.empty(); + } + + bool Lock() + { + CRITICAL_BLOCK(cs_vMasterKey) + { + if (!SetCrypted()) + return false; + + vMasterKey.clear(); + } + return true; + } + + virtual bool AddCryptedKey(const std::vector &vchPubKey, const std::vector &vchCryptedSecret); std::vector GenerateNewKey(); + bool AddKey(const CKey& key); + bool HaveKey(const std::vector &vchPubKey) const + { + if (!IsCrypted()) + return CBasicKeyStore::HaveKey(vchPubKey); + return mapCryptedKeys.count(vchPubKey) > 0; + } + bool GetPrivKey(const std::vector &vchPubKey, CPrivKey& keyOut) const; }; #endif