-
+ 240C2A39BA9A1148912FE1A447784F1918B671BB4589732832FE413EFF3972A1B34C5CCD3E70C6E601CAFBF2EE5BB0306DF12D9E59FEB4FA92221BD821DE186C
bitcoin/src/keystore.h
(0 . 0)(1 . 169)
10156 // /****************************\
10157 // * EXPERIMENTAL BRANCH. *
10158 // * FOR LABORATORY USE ONLY. *
10159 // ********************************
10160 // ************
10161 // **************
10162 // ****************
10163 // **** **** ****
10164 // *** *** ***
10165 // *** *** ***
10166 // *** * * **
10167 // ******** ********
10168 // ******* ******
10169 // *** **
10170 // * ******* **
10171 // ** * * * * *
10172 // ** * * ***
10173 // **** * * * * ****
10174 // **** *** * * ** ***
10175 // **** ********* ******
10176 // ******* ***** *******
10177 // ********* ****** **
10178 // ** ****** ******
10179 // ** ******* **
10180 // ** ******* ***
10181 // **** ******** ************
10182 // ************ ************
10183 // ******** *******
10184 // ****** ****
10185 // *** ***
10186 // ********************************
10187 // Copyright (c) 2009-2010 Satoshi Nakamoto
10188 // Copyright (c) 2011 The Bitcoin developers
10189 // Distributed under the MIT/X11 software license, see the accompanying
10190 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
10191 #ifndef BITCOIN_KEYSTORE_H
10192 #define BITCOIN_KEYSTORE_H
10193
10194 #include "crypter.h"
10195
10196 // A virtual base class for key stores
10197 class CKeyStore
10198 {
10199 protected:
10200 mutable CCriticalSection cs_KeyStore;
10201
10202 public:
10203 // Add a key to the store.
10204 virtual bool AddKey(const CKey& key) =0;
10205
10206 // Check whether a key corresponding to a given address is present in the store.
10207 virtual bool HaveKey(const CBitcoinAddress &address) const =0;
10208
10209 // Retrieve a key corresponding to a given address from the store.
10210 // Return true if succesful.
10211 virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
10212
10213 // Retrieve only the public key corresponding to a given address.
10214 // This may succeed even if GetKey fails (e.g., encrypted wallets)
10215 virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
10216
10217 // Generate a new key, and add it to the store
10218 virtual std::vector<unsigned char> GenerateNewKey();
10219 };
10220
10221 typedef std::map<CBitcoinAddress, CSecret> KeyMap;
10222
10223 // Basic key store, that keeps keys in an address->secret map
10224 class CBasicKeyStore : public CKeyStore
10225 {
10226 protected:
10227 KeyMap mapKeys;
10228
10229 public:
10230 bool AddKey(const CKey& key);
10231 bool HaveKey(const CBitcoinAddress &address) const
10232 {
10233 bool result;
10234 CRITICAL_BLOCK(cs_KeyStore)
10235 result = (mapKeys.count(address) > 0);
10236 return result;
10237 }
10238 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
10239 {
10240 CRITICAL_BLOCK(cs_KeyStore)
10241 {
10242 KeyMap::const_iterator mi = mapKeys.find(address);
10243 if (mi != mapKeys.end())
10244 {
10245 keyOut.SetSecret((*mi).second);
10246 return true;
10247 }
10248 }
10249 return false;
10250 }
10251 };
10252
10253 typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
10254
10255 // Keystore which keeps the private keys encrypted
10256 // It derives from the basic key store, which is used if no encryption is active.
10257 class CCryptoKeyStore : public CBasicKeyStore
10258 {
10259 private:
10260 CryptedKeyMap mapCryptedKeys;
10261
10262 CKeyingMaterial vMasterKey;
10263
10264 // if fUseCrypto is true, mapKeys must be empty
10265 // if fUseCrypto is false, vMasterKey must be empty
10266 bool fUseCrypto;
10267
10268 protected:
10269 bool SetCrypted();
10270
10271 // will encrypt previously unencrypted keys
10272 bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
10273
10274 bool Unlock(const CKeyingMaterial& vMasterKeyIn);
10275
10276 public:
10277 CCryptoKeyStore() : fUseCrypto(false)
10278 {
10279 }
10280
10281 bool IsCrypted() const
10282 {
10283 return fUseCrypto;
10284 }
10285
10286 bool IsLocked() const
10287 {
10288 if (!IsCrypted())
10289 return false;
10290 bool result;
10291 CRITICAL_BLOCK(cs_KeyStore)
10292 result = vMasterKey.empty();
10293 return result;
10294 }
10295
10296 bool Lock()
10297 {
10298 if (!SetCrypted())
10299 return false;
10300
10301 CRITICAL_BLOCK(cs_KeyStore)
10302 vMasterKey.clear();
10303
10304 return true;
10305 }
10306
10307 virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
10308 std::vector<unsigned char> GenerateNewKey();
10309 bool AddKey(const CKey& key);
10310 bool HaveKey(const CBitcoinAddress &address) const
10311 {
10312 CRITICAL_BLOCK(cs_KeyStore)
10313 {
10314 if (!IsCrypted())
10315 return CBasicKeyStore::HaveKey(address);
10316 return mapCryptedKeys.count(address) > 0;
10317 }
10318 return false;
10319 }
10320 bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
10321 bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
10322 };
10323
10324 #endif