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