raw
genesis                 1 // Copyright (c) 2009-2010 Satoshi Nakamoto
genesis 2 // Copyright (c) 2011 The Bitcoin developers
genesis 3 // Distributed under the MIT/X11 software license, see the accompanying
genesis 4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
genesis 5 #ifndef BITCOIN_KEYSTORE_H
genesis 6 #define BITCOIN_KEYSTORE_H
genesis 7
genesis 8 #include "crypter.h"
genesis 9
genesis 10 // A virtual base class for key stores
genesis 11 class CKeyStore
genesis 12 {
genesis 13 protected:
genesis 14 mutable CCriticalSection cs_KeyStore;
genesis 15
genesis 16 public:
genesis 17 // Add a key to the store.
genesis 18 virtual bool AddKey(const CKey& key) =0;
genesis 19
genesis 20 // Check whether a key corresponding to a given address is present in the store.
genesis 21 virtual bool HaveKey(const CBitcoinAddress &address) const =0;
genesis 22
genesis 23 // Retrieve a key corresponding to a given address from the store.
genesis 24 // Return true if succesful.
genesis 25 virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
genesis 26
genesis 27 // Retrieve only the public key corresponding to a given address.
genesis 28 // This may succeed even if GetKey fails (e.g., encrypted wallets)
genesis 29 virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
genesis 30
genesis 31 // Generate a new key, and add it to the store
genesis 32 virtual std::vector<unsigned char> GenerateNewKey();
genesis 33 };
genesis 34
genesis 35 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
genesis 36
genesis 37 // Basic key store, that keeps keys in an address->secret map
genesis 38 class CBasicKeyStore : public CKeyStore
genesis 39 {
genesis 40 protected:
genesis 41 KeyMap mapKeys;
genesis 42
genesis 43 public:
genesis 44 bool AddKey(const CKey& key);
genesis 45 bool HaveKey(const CBitcoinAddress &address) const
genesis 46 {
genesis 47 bool result;
genesis 48 CRITICAL_BLOCK(cs_KeyStore)
genesis 49 result = (mapKeys.count(address) > 0);
genesis 50 return result;
genesis 51 }
genesis 52 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
genesis 53 {
genesis 54 CRITICAL_BLOCK(cs_KeyStore)
genesis 55 {
genesis 56 KeyMap::const_iterator mi = mapKeys.find(address);
genesis 57 if (mi != mapKeys.end())
genesis 58 {
genesis 59 keyOut.SetSecret((*mi).second);
genesis 60 return true;
genesis 61 }
genesis 62 }
genesis 63 return false;
genesis 64 }
genesis 65 };
genesis 66
genesis 67 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
genesis 68
genesis 69 // Keystore which keeps the private keys encrypted
genesis 70 // It derives from the basic key store, which is used if no encryption is active.
genesis 71 class CCryptoKeyStore : public CBasicKeyStore
genesis 72 {
genesis 73 private:
genesis 74 CryptedKeyMap mapCryptedKeys;
genesis 75
genesis 76 CKeyingMaterial vMasterKey;
genesis 77
genesis 78 // if fUseCrypto is true, mapKeys must be empty
genesis 79 // if fUseCrypto is false, vMasterKey must be empty
genesis 80 bool fUseCrypto;
genesis 81
genesis 82 protected:
genesis 83 bool SetCrypted();
genesis 84
genesis 85 // will encrypt previously unencrypted keys
genesis 86 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
genesis 87
genesis 88 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
genesis 89
genesis 90 public:
genesis 91 CCryptoKeyStore() : fUseCrypto(false)
genesis 92 {
genesis 93 }
genesis 94
genesis 95 bool IsCrypted() const
genesis 96 {
genesis 97 return fUseCrypto;
genesis 98 }
genesis 99
genesis 100 bool IsLocked() const
genesis 101 {
genesis 102 if (!IsCrypted())
genesis 103 return false;
genesis 104 bool result;
genesis 105 CRITICAL_BLOCK(cs_KeyStore)
genesis 106 result = vMasterKey.empty();
genesis 107 return result;
genesis 108 }
genesis 109
genesis 110 bool Lock()
genesis 111 {
genesis 112 if (!SetCrypted())
genesis 113 return false;
genesis 114
genesis 115 CRITICAL_BLOCK(cs_KeyStore)
genesis 116 vMasterKey.clear();
genesis 117
genesis 118 return true;
genesis 119 }
genesis 120
genesis 121 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
genesis 122 std::vector<unsigned char> GenerateNewKey();
genesis 123 bool AddKey(const CKey& key);
genesis 124 bool HaveKey(const CBitcoinAddress &address) const
genesis 125 {
genesis 126 CRITICAL_BLOCK(cs_KeyStore)
genesis 127 {
genesis 128 if (!IsCrypted())
genesis 129 return CBasicKeyStore::HaveKey(address);
genesis 130 return mapCryptedKeys.count(address) > 0;
genesis 131 }
genesis 132 return false;
genesis 133 }
genesis 134 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
genesis 135 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
genesis 136 };
genesis 137
genesis 138 #endif