Bitcoin Core  0.18.99
P2P Digital Currency
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or
9 #include <key.h>
10 #include <pubkey.h>
11 #include <script/script.h>
12 #include <script/sign.h>
13 #include <script/standard.h>
14 #include <sync.h>
16 #include <boost/signals2/signal.hpp>
19 class CKeyStore : public SigningProvider
20 {
21 public:
23  virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) =0;
26  virtual bool HaveKey(const CKeyID &address) const =0;
27  virtual std::set<CKeyID> GetKeys() const =0;
30  virtual bool AddCScript(const CScript& redeemScript) =0;
31  virtual bool HaveCScript(const CScriptID &hash) const =0;
32  virtual std::set<CScriptID> GetCScripts() const =0;
35  virtual bool AddWatchOnly(const CScript &dest) =0;
36  virtual bool RemoveWatchOnly(const CScript &dest) =0;
37  virtual bool HaveWatchOnly(const CScript &dest) const =0;
38  virtual bool HaveWatchOnly() const =0;
39 };
42 class CBasicKeyStore : public CKeyStore
43 {
44 protected:
47  using KeyMap = std::map<CKeyID, CKey>;
48  using WatchKeyMap = std::map<CKeyID, CPubKey>;
49  using ScriptMap = std::map<CScriptID, CScript>;
50  using WatchOnlySet = std::set<CScript>;
52  KeyMap mapKeys GUARDED_BY(cs_KeyStore);
53  WatchKeyMap mapWatchKeys GUARDED_BY(cs_KeyStore);
54  ScriptMap mapScripts GUARDED_BY(cs_KeyStore);
55  WatchOnlySet setWatchOnly GUARDED_BY(cs_KeyStore);
57  void ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
59 public:
60  bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
61  bool AddKey(const CKey &key) { return AddKeyPubKey(key, key.GetPubKey()); }
62  bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
63  bool HaveKey(const CKeyID &address) const override;
64  std::set<CKeyID> GetKeys() const override;
65  bool GetKey(const CKeyID &address, CKey &keyOut) const override;
66  bool AddCScript(const CScript& redeemScript) override;
67  bool HaveCScript(const CScriptID &hash) const override;
68  std::set<CScriptID> GetCScripts() const override;
69  bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override;
71  bool AddWatchOnly(const CScript &dest) override;
72  bool RemoveWatchOnly(const CScript &dest) override;
73  bool HaveWatchOnly(const CScript &dest) const override;
74  bool HaveWatchOnly() const override;
75 };
78 CKeyID GetKeyForDestination(const CKeyStore& store, const CTxDestination& dest);
81 bool HaveKey(const CKeyStore& store, const CKey& key);
std::map< CKeyID, CKey > KeyMap
Definition: keystore.h:47
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)=0
Add a key to the store.
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:184
CCriticalSection cs_KeyStore
Definition: keystore.h:45
virtual bool AddWatchOnly(const CScript &dest)=0
Support for Watch-only addresses.
virtual std::set< CScriptID > GetCScripts() const =0
virtual bool AddCScript(const CScript &redeemScript)=0
Support for BIP 0013 : see
virtual bool HaveCScript(const CScriptID &hash) const =0
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
Definition: sign.h:54
std::set< CScript > WatchOnlySet
Definition: keystore.h:50
bool AddKey(const CKey &key)
Definition: keystore.h:61
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:30
virtual bool RemoveWatchOnly(const CScript &dest)=0
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
Definition: sign.h:53
virtual bool GetKey(const CKeyID &address, CKey &key) const
Definition: sign.h:55
Definition: threadsafety.h:51
std::map< CScriptID, CScript > ScriptMap
Definition: keystore.h:49
An interface to be implemented by keystores that support signing.
Definition: sign.h:49
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
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:20
std::map< CKeyID, CPubKey > WatchKeyMap
Definition: keystore.h:48
#define GUARDED_BY(x)
Definition: threadsafety.h:38
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:22
CKeyID GetKeyForDestination(const CKeyStore &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
Definition: keystore.cpp:177
An encapsulated private key.
Definition: key.h:27
virtual std::set< CKeyID > GetKeys() const =0
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:139
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:42