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