Raven Core  3.0.0
P2P Digital Currency
keystore.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef RAVEN_KEYSTORE_H
8 #define RAVEN_KEYSTORE_H
9 
10 #include "key.h"
11 #include "pubkey.h"
12 #include "script/script.h"
13 #include "script/standard.h"
14 #include "sync.h"
15 
16 #include <boost/signals2/signal.hpp>
17 
19 class CKeyStore
20 {
21 protected:
23 
24 public:
25  virtual ~CKeyStore() {}
26 
28  virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0;
29  virtual bool AddKey(const CKey &key);
30 
32  virtual bool HaveKey(const CKeyID &address) const =0;
33  virtual bool GetKey(const CKeyID &address, CKey& keyOut) const =0;
34  virtual std::set<CKeyID> GetKeys() const =0;
35  virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const =0;
36 
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 
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<CKeyID, CPubKey> WatchKeyMap;
51 typedef std::map<CScriptID, CScript > ScriptMap;
52 typedef std::set<CScript> WatchOnlySet;
53 
55 class CBasicKeyStore : public CKeyStore
56 {
57 protected:
62 
63 public:
64  bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
65  bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
66  bool HaveKey(const CKeyID &address) const override
67  {
68  bool result;
69  {
71  result = (mapKeys.count(address) > 0);
72  }
73  return result;
74  }
75  std::set<CKeyID> GetKeys() const override
76  {
78  std::set<CKeyID> set_address;
79  for (const auto& mi : mapKeys) {
80  set_address.insert(mi.first);
81  }
82  return set_address;
83  }
84  bool GetKey(const CKeyID &address, CKey &keyOut) const override
85  {
86  {
88  KeyMap::const_iterator mi = mapKeys.find(address);
89  if (mi != mapKeys.end())
90  {
91  keyOut = mi->second;
92  return true;
93  }
94  }
95  return false;
96  }
97  bool AddCScript(const CScript& redeemScript) override;
98  bool HaveCScript(const CScriptID &hash) const override;
99  bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override;
100 
101  bool AddWatchOnly(const CScript &dest) override;
102  bool RemoveWatchOnly(const CScript &dest) override;
103  bool HaveWatchOnly(const CScript &dest) const override;
104  bool HaveWatchOnly() const override;
105 };
106 
107 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
108 typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
109 
110 #endif // RAVEN_KEYSTORE_H
CCriticalSection cs_KeyStore
Definition: keystore.h:22
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)=0
Add a key to the store.
std::map< CScriptID, CScript > ScriptMap
Definition: keystore.h:51
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: keystore.h:107
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char > > > CryptedKeyMap
Definition: keystore.h:108
virtual bool AddWatchOnly(const CScript &dest)=0
Support for Watch-only addresses.
std::set< CScript > WatchOnlySet
Definition: keystore.h:52
virtual bool AddCScript(const CScript &redeemScript)=0
Support for BIP 0013 : see https://github.com/raven/bips/blob/master/bip-0013.mediawiki.
virtual bool HaveCScript(const CScriptID &hash) const =0
WatchKeyMap mapWatchKeys
Definition: keystore.h:59
#define LOCK(cs)
Definition: sync.h:176
virtual bool HaveKey(const CKeyID &address) const =0
Check whether a key corresponding to a given address is present in the store.
An encapsulated public key.
Definition: pubkey.h:40
virtual bool RemoveWatchOnly(const CScript &dest)=0
ScriptMap mapScripts
Definition: keystore.h:60
std::map< CKeyID, CPubKey > WatchKeyMap
Definition: keystore.h:50
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const =0
WatchOnlySet setWatchOnly
Definition: keystore.h:61
bool GetKey(const CKeyID &address, CKey &keyOut) const override
Definition: keystore.h:84
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:13
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const =0
KeyMap mapKeys
Definition: keystore.h:58
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
virtual bool HaveWatchOnly() const =0
A virtual base class for key stores.
Definition: keystore.h:19
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:30
virtual ~CKeyStore()
Definition: keystore.h:25
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:23
An encapsulated private key.
Definition: key.h:36
virtual std::set< CKeyID > GetKeys() const =0
bool HaveKey(const CKeyID &address) const override
Check whether a key corresponding to a given address is present in the store.
Definition: keystore.h:66
std::map< CKeyID, CKey > KeyMap
Definition: keystore.h:49
std::set< CKeyID > GetKeys() const override
Definition: keystore.h:75
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:55
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...
Definition: sync.h:92
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const =0