]> Git Repo - VerusCoin.git/blob - src/keystore.h
Test
[VerusCoin.git] / src / keystore.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #ifndef BITCOIN_KEYSTORE_H
7 #define BITCOIN_KEYSTORE_H
8
9 #include "key.h"
10 #include "pubkey.h"
11 #include "script/script.h"
12 #include "script/standard.h"
13 #include "sync.h"
14 #include "zcash/Address.hpp"
15 #include "zcash/NoteEncryption.hpp"
16
17 #include <boost/signals2/signal.hpp>
18 #include <boost/variant.hpp>
19
20 /** A virtual base class for key stores */
21 class CKeyStore
22 {
23 protected:
24     mutable CCriticalSection cs_KeyStore;
25     mutable CCriticalSection cs_SpendingKeyStore;
26
27 public:
28     virtual ~CKeyStore() {}
29
30     //! Add a key to the store.
31     virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0;
32     virtual bool AddKey(const CKey &key);
33
34     //! Check whether a key corresponding to a given address is present in the store.
35     virtual bool HaveKey(const CKeyID &address) const =0;
36     virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
37     virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
38     virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
39
40     //! Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki
41     virtual bool AddCScript(const CScript& redeemScript) =0;
42     virtual bool HaveCScript(const CScriptID &hash) const =0;
43     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
44
45     //! Support for Watch-only addresses
46     virtual bool AddWatchOnly(const CScript &dest) =0;
47     virtual bool RemoveWatchOnly(const CScript &dest) =0;
48     virtual bool HaveWatchOnly(const CScript &dest) const =0;
49     virtual bool HaveWatchOnly() const =0;
50
51     //! Add a spending key to the store.
52     virtual bool AddSpendingKey(const libzcash::SpendingKey &sk) =0;
53
54     //! Check whether a spending key corresponding to a given payment address is present in the store.
55     virtual bool HaveSpendingKey(const libzcash::PaymentAddress &address) const =0;
56     virtual bool GetSpendingKey(const libzcash::PaymentAddress &address, libzcash::SpendingKey& skOut) const =0;
57     virtual void GetPaymentAddresses(std::set<libzcash::PaymentAddress> &setAddress) const =0;
58 };
59
60 typedef std::map<CKeyID, CKey> KeyMap;
61 typedef std::map<CScriptID, CScript > ScriptMap;
62 typedef std::set<CScript> WatchOnlySet;
63 typedef std::map<libzcash::PaymentAddress, libzcash::SpendingKey> SpendingKeyMap;
64 typedef std::map<libzcash::PaymentAddress, ZCNoteDecryption> NoteDecryptorMap;
65
66 /** Basic key store, that keeps keys in an address->secret map */
67 class CBasicKeyStore : public CKeyStore
68 {
69 protected:
70     KeyMap mapKeys;
71     ScriptMap mapScripts;
72     WatchOnlySet setWatchOnly;
73     SpendingKeyMap mapSpendingKeys;
74     NoteDecryptorMap mapNoteDecryptors;
75
76 public:
77     bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
78     bool HaveKey(const CKeyID &address) const
79     {
80         bool result;
81         {
82             LOCK(cs_KeyStore);
83             result = (mapKeys.count(address) > 0);
84         }
85         return result;
86     }
87     void GetKeys(std::set<CKeyID> &setAddress) const
88     {
89         setAddress.clear();
90         {
91             LOCK(cs_KeyStore);
92             KeyMap::const_iterator mi = mapKeys.begin();
93             while (mi != mapKeys.end())
94             {
95                 setAddress.insert((*mi).first);
96                 mi++;
97             }
98         }
99     }
100     bool GetKey(const CKeyID &address, CKey &keyOut) const
101     {
102         {
103             LOCK(cs_KeyStore);
104             KeyMap::const_iterator mi = mapKeys.find(address);
105             if (mi != mapKeys.end())
106             {
107                 keyOut = mi->second;
108                 return true;
109             }
110         }
111         return false;
112     }
113     virtual bool AddCScript(const CScript& redeemScript);
114     virtual bool HaveCScript(const CScriptID &hash) const;
115     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
116
117     virtual bool AddWatchOnly(const CScript &dest);
118     virtual bool RemoveWatchOnly(const CScript &dest);
119     virtual bool HaveWatchOnly(const CScript &dest) const;
120     virtual bool HaveWatchOnly() const;
121
122     bool AddSpendingKey(const libzcash::SpendingKey &sk);
123     bool HaveSpendingKey(const libzcash::PaymentAddress &address) const
124     {
125         bool result;
126         {
127             LOCK(cs_SpendingKeyStore);
128             result = (mapSpendingKeys.count(address) > 0);
129         }
130         return result;
131     }
132     bool GetSpendingKey(const libzcash::PaymentAddress &address, libzcash::SpendingKey &skOut) const
133     {
134         {
135             LOCK(cs_SpendingKeyStore);
136             SpendingKeyMap::const_iterator mi = mapSpendingKeys.find(address);
137             if (mi != mapSpendingKeys.end())
138             {
139                 skOut = mi->second;
140                 return true;
141             }
142         }
143         return false;
144     }
145     bool GetNoteDecryptor(const libzcash::PaymentAddress &address, ZCNoteDecryption &decOut) const
146     {
147         {
148             LOCK(cs_SpendingKeyStore);
149             NoteDecryptorMap::const_iterator mi = mapNoteDecryptors.find(address);
150             if (mi != mapNoteDecryptors.end())
151             {
152                 decOut = mi->second;
153                 return true;
154             }
155         }
156         return false;
157     }
158     void GetPaymentAddresses(std::set<libzcash::PaymentAddress> &setAddress) const
159     {
160         setAddress.clear();
161         {
162             LOCK(cs_SpendingKeyStore);
163             SpendingKeyMap::const_iterator mi = mapSpendingKeys.begin();
164             while (mi != mapSpendingKeys.end())
165             {
166                 setAddress.insert((*mi).first);
167                 mi++;
168             }
169         }
170     }
171 };
172
173 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
174 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
175 typedef std::map<libzcash::PaymentAddress, std::vector<unsigned char> > CryptedSpendingKeyMap;
176
177 #endif // BITCOIN_KEYSTORE_H
This page took 0.036134 seconds and 4 git commands to generate.