genesis 1
genesis 2
genesis 3
genesis 4
genesis 5 #ifndef BITCOIN_KEYSTORE_H
genesis 6 #define BITCOIN_KEYSTORE_H
genesis 7
genesis 8 #include "crypter.h"
genesis 9
genesis 10
genesis 11 class CKeyStore
genesis 12 {
genesis 13 protected:
genesis 14 mutable CCriticalSection cs_KeyStore;
genesis 15
genesis 16 public:
genesis 17
genesis 18 virtual bool AddKey(const CKey& key) =0;
genesis 19
genesis 20
genesis 21 virtual bool HaveKey(const CBitcoinAddress &address) const =0;
genesis 22
genesis 23
genesis 24
genesis 25 virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
genesis 26
genesis 27
genesis 28
genesis 29 virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
genesis 30
mod6_privkey_tools 31 virtual bool GetSecret(const CBitcoinAddress &address, CSecret& vchSecret) const
mod6_privkey_tools 32 {
mod6_privkey_tools 33 CKey key;
mod6_privkey_tools 34 if (!GetKey(address, key))
mod6_privkey_tools 35 return false;
mod6_privkey_tools 36 vchSecret = key.GetSecret();
mod6_privkey_tools 37 return true;
mod6_privkey_tools 38 }
mod6_privkey_tools 39
genesis 40
genesis 41 virtual std::vector<unsigned char> GenerateNewKey();
genesis 42 };
genesis 43
genesis 44 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
genesis 45
genesis 46
genesis 47 class CBasicKeyStore : public CKeyStore
genesis 48 {
genesis 49 protected:
genesis 50 KeyMap mapKeys;
genesis 51
genesis 52 public:
genesis 53 bool AddKey(const CKey& key);
genesis 54 bool HaveKey(const CBitcoinAddress &address) const
genesis 55 {
genesis 56 bool result;
genesis 57 CRITICAL_BLOCK(cs_KeyStore)
genesis 58 result = (mapKeys.count(address) > 0);
genesis 59 return result;
genesis 60 }
genesis 61 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
genesis 62 {
genesis 63 CRITICAL_BLOCK(cs_KeyStore)
genesis 64 {
genesis 65 KeyMap::const_iterator mi = mapKeys.find(address);
genesis 66 if (mi != mapKeys.end())
genesis 67 {
genesis 68 keyOut.SetSecret((*mi).second);
genesis 69 return true;
genesis 70 }
genesis 71 }
genesis 72 return false;
genesis 73 }
genesis 74 };
genesis 75
genesis 76 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
genesis 77
genesis 78
genesis 79
genesis 80 class CCryptoKeyStore : public CBasicKeyStore
genesis 81 {
genesis 82 private:
genesis 83 CryptedKeyMap mapCryptedKeys;
genesis 84
genesis 85 CKeyingMaterial vMasterKey;
genesis 86
genesis 87
genesis 88
genesis 89 bool fUseCrypto;
genesis 90
genesis 91 protected:
genesis 92 bool SetCrypted();
genesis 93
genesis 94
genesis 95 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
genesis 96
genesis 97 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
genesis 98
genesis 99 public:
genesis 100 CCryptoKeyStore() : fUseCrypto(false)
genesis 101 {
genesis 102 }
genesis 103
genesis 104 bool IsCrypted() const
genesis 105 {
genesis 106 return fUseCrypto;
genesis 107 }
genesis 108
genesis 109 bool IsLocked() const
genesis 110 {
genesis 111 if (!IsCrypted())
genesis 112 return false;
genesis 113 bool result;
genesis 114 CRITICAL_BLOCK(cs_KeyStore)
genesis 115 result = vMasterKey.empty();
genesis 116 return result;
genesis 117 }
genesis 118
genesis 119 bool Lock()
genesis 120 {
genesis 121 if (!SetCrypted())
genesis 122 return false;
genesis 123
genesis 124 CRITICAL_BLOCK(cs_KeyStore)
genesis 125 vMasterKey.clear();
genesis 126
genesis 127 return true;
genesis 128 }
genesis 129
genesis 130 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
genesis 131 std::vector<unsigned char> GenerateNewKey();
genesis 132 bool AddKey(const CKey& key);
genesis 133 bool HaveKey(const CBitcoinAddress &address) const
genesis 134 {
genesis 135 CRITICAL_BLOCK(cs_KeyStore)
genesis 136 {
genesis 137 if (!IsCrypted())
genesis 138 return CBasicKeyStore::HaveKey(address);
genesis 139 return mapCryptedKeys.count(address) > 0;
genesis 140 }
genesis 141 return false;
genesis 142 }
genesis 143 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
genesis 144 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
genesis 145 };
genesis 146
genesis 147 #endif