]> Git Repo - VerusCoin.git/blob - src/keystore.h
Merge pull request #5481
[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 "sync.h"
12
13 #include <boost/signals2/signal.hpp>
14 #include <boost/variant.hpp>
15
16 class CScript;
17 class CScriptID;
18
19 /** A virtual base class for key stores */
20 class CKeyStore
21 {
22 protected:
23     mutable CCriticalSection cs_KeyStore;
24
25 public:
26     virtual ~CKeyStore() {}
27
28     //! Add a key to the store.
29     virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0;
30     virtual bool AddKey(const CKey &key);
31
32     //! Check whether a key corresponding to a given address is present in the store.
33     virtual bool HaveKey(const CKeyID &address) const =0;
34     virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
35     virtual void GetKeys(std::set<CKeyID> &setAddress) const =0;
36     virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
37
38     //! Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki
39     virtual bool AddCScript(const CScript& redeemScript) =0;
40     virtual bool HaveCScript(const CScriptID &hash) const =0;
41     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const =0;
42
43     //! Support for Watch-only addresses
44     virtual bool AddWatchOnly(const CScript &dest) =0;
45     virtual bool RemoveWatchOnly(const CScript &dest) =0;
46     virtual bool HaveWatchOnly(const CScript &dest) const =0;
47     virtual bool HaveWatchOnly() const =0;
48 };
49
50 typedef std::map<CKeyID, CKey> KeyMap;
51 typedef std::map<CScriptID, CScript > ScriptMap;
52 typedef std::set<CScript> WatchOnlySet;
53
54 /** Basic key store, that keeps keys in an address->secret map */
55 class CBasicKeyStore : public CKeyStore
56 {
57 protected:
58     KeyMap mapKeys;
59     ScriptMap mapScripts;
60     WatchOnlySet setWatchOnly;
61
62 public:
63     bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
64     bool HaveKey(const CKeyID &address) const
65     {
66         bool result;
67         {
68             LOCK(cs_KeyStore);
69             result = (mapKeys.count(address) > 0);
70         }
71         return result;
72     }
73     void GetKeys(std::set<CKeyID> &setAddress) const
74     {
75         setAddress.clear();
76         {
77             LOCK(cs_KeyStore);
78             KeyMap::const_iterator mi = mapKeys.begin();
79             while (mi != mapKeys.end())
80             {
81                 setAddress.insert((*mi).first);
82                 mi++;
83             }
84         }
85     }
86     bool GetKey(const CKeyID &address, CKey &keyOut) const
87     {
88         {
89             LOCK(cs_KeyStore);
90             KeyMap::const_iterator mi = mapKeys.find(address);
91             if (mi != mapKeys.end())
92             {
93                 keyOut = mi->second;
94                 return true;
95             }
96         }
97         return false;
98     }
99     virtual bool AddCScript(const CScript& redeemScript);
100     virtual bool HaveCScript(const CScriptID &hash) const;
101     virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const;
102
103     virtual bool AddWatchOnly(const CScript &dest);
104     virtual bool RemoveWatchOnly(const CScript &dest);
105     virtual bool HaveWatchOnly(const CScript &dest) const;
106     virtual bool HaveWatchOnly() const;
107 };
108
109 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
110 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
111
112 #endif // BITCOIN_KEYSTORE_H
This page took 0.025855 seconds and 4 git commands to generate.