Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
keystore.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_KEYSTORE_H
6 #define BITCOIN_KEYSTORE_H
7 
8 #include "crypter.h"
9 #include "sync.h"
10 #include <boost/signals2/signal.hpp>
11 
12 class CScript;
13 
15 class CKeyStore
16 {
17 protected:
19 
20 public:
21  virtual ~CKeyStore() {}
22 
23  // Add a key to the store.
24  virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0;
25  virtual bool AddKey(const CKey &key);
26 
27  // Check whether a key corresponding to a given address is present in the store.
28  virtual bool HaveKey(const CKeyID &address) const =0;
29  virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
30  virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
31  virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
32 
33  // Support for BIP 0013 : see https://en.bitcoin.it/wiki/BIP_0013
34  virtual bool AddCScript(const CScript& redeemScript) =0;
35  virtual bool HaveCScript(const CScriptID &hash) const =0;
36  virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
37 };
38 
39 typedef std::map<CKeyID, CKey> KeyMap;
40 typedef std::map<CScriptID, CScript > ScriptMap;
41 
43 class CBasicKeyStore : public CKeyStore
44 {
45 protected:
48 
49 public:
50  bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
51  bool HaveKey(const CKeyID &address) const
52  {
53  bool result;
54  {
56  result = (mapKeys.count(address) > 0);
57  }
58  return result;
59  }
60  void GetKeys(std::set<CKeyID> &setAddress) const
61  {
62  setAddress.clear();
63  {
65  KeyMap::const_iterator mi = mapKeys.begin();
66  while (mi != mapKeys.end())
67  {
68  setAddress.insert((*mi).first);
69  mi++;
70  }
71  }
72  }
73  bool GetKey(const CKeyID &address, CKey &keyOut) const
74  {
75  {
77  KeyMap::const_iterator mi = mapKeys.find(address);
78  if (mi != mapKeys.end())
79  {
80  keyOut = mi->second;
81  return true;
82  }
83  }
84  return false;
85  }
86  virtual bool AddCScript(const CScript& redeemScript);
87  virtual bool HaveCScript(const CScriptID &hash) const;
88  virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
89 };
90 
91 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
92 
97 {
98 private:
100 
102 
103  // if fUseCrypto is true, mapKeys must be empty
104  // if fUseCrypto is false, vMasterKey must be empty
106 
107 protected:
108  bool SetCrypted();
109 
110  // will encrypt previously unencrypted keys
111  bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
112 
113  bool Unlock(const CKeyingMaterial& vMasterKeyIn);
114 
115 public:
116  CCryptoKeyStore() : fUseCrypto(false)
117  {
118  }
119 
120  bool IsCrypted() const
121  {
122  return fUseCrypto;
123  }
124 
125  bool IsLocked() const
126  {
127  if (!IsCrypted())
128  return false;
129  bool result;
130  {
131  LOCK(cs_KeyStore);
132  result = vMasterKey.empty();
133  }
134  return result;
135  }
136 
137  bool Lock();
138 
139  virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
140  bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
141  bool HaveKey(const CKeyID &address) const
142  {
143  {
144  LOCK(cs_KeyStore);
145  if (!IsCrypted())
146  return CBasicKeyStore::HaveKey(address);
147  return mapCryptedKeys.count(address) > 0;
148  }
149  return false;
150  }
151  bool GetKey(const CKeyID &address, CKey& keyOut) const;
152  bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
153  void GetKeys(std::set<CKeyID> &setAddress) const
154  {
155  if (!IsCrypted())
156  {
157  CBasicKeyStore::GetKeys(setAddress);
158  return;
159  }
160  setAddress.clear();
161  CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
162  while (mi != mapCryptedKeys.end())
163  {
164  setAddress.insert((*mi).first);
165  mi++;
166  }
167  }
168 
169  /* Wallet status (encrypted, locked) changed.
170  * Note: Called without locks held.
171  */
172  boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
173 };
174 
175 #endif
bool IsCrypted() const
Definition: keystore.h:120
CCriticalSection cs_KeyStore
Definition: keystore.h:18
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)=0
bool HaveKey(const CKeyID &address) const
Definition: keystore.h:141
void GetKeys(std::set< CKeyID > &setAddress) const
Definition: keystore.h:60
std::map< CScriptID, CScript > ScriptMap
Definition: keystore.h:40
bool HaveKey(const CKeyID &address) const
Definition: keystore.h:51
bool SetCrypted()
Definition: keystore.cpp:54
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:61
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char > > > CryptedKeyMap
Definition: keystore.h:91
bool EncryptKeys(CKeyingMaterial &vMasterKeyIn)
Definition: keystore.cpp:183
bool IsLocked() const
Definition: keystore.h:125
CKeyingMaterial vMasterKey
Definition: keystore.h:101
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
Definition: keystore.cpp:130
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Definition: keystore.cpp:22
Keystore which keeps the private keys encrypted.
Definition: keystore.h:96
bool GetKey(const CKeyID &address, CKey &keyOut) const
Definition: keystore.cpp:142
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
Definition: keystore.cpp:166
virtual bool AddCScript(const CScript &redeemScript)=0
virtual bool HaveCScript(const CScriptID &hash) const =0
virtual bool AddCScript(const CScript &redeemScript)
Definition: keystore.cpp:29
boost::signals2::signal< void(CCryptoKeyStore *wallet)> NotifyStatusChanged
Definition: keystore.h:172
void GetKeys(std::set< CKeyID > &setAddress) const
Definition: keystore.h:153
bool GetKey(const CKeyID &address, CKey &keyOut) const
Definition: keystore.h:73
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
Definition: keystore.cpp:9
#define LOCK(cs)
Definition: sync.h:108
virtual bool HaveKey(const CKeyID &address) const =0
An encapsulated public key.
Definition: key.h:40
bool Unlock(const CKeyingMaterial &vMasterKeyIn)
Definition: keystore.cpp:79
ScriptMap mapScripts
Definition: keystore.h:47
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const =0
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Definition: keystore.cpp:108
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:18
KeyMap mapKeys
Definition: keystore.h:46
CryptedKeyMap mapCryptedKeys
Definition: keystore.h:99
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:244
A virtual base class for key stores.
Definition: keystore.h:15
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:24
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const
Definition: keystore.cpp:42
virtual ~CKeyStore()
Definition: keystore.h:21
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: key.h:32
virtual void GetKeys(std::set< CKeyID > &setAddress) const =0
An encapsulated private key.
Definition: key.h:172
std::map< CKeyID, CKey > KeyMap
Definition: keystore.h:39
uint32_t hash
Definition: cache.cc:34
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:43
virtual bool HaveCScript(const CScriptID &hash) const
Definition: keystore.cpp:36
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const =0