Bitcoin Core  0.19.99
P2P Digital Currency
signingprovider.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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_SCRIPT_SIGNINGPROVIDER_H
7 #define BITCOIN_SCRIPT_SIGNINGPROVIDER_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 struct KeyOriginInfo;
16 
19 {
20 public:
21  virtual ~SigningProvider() {}
22  virtual bool GetCScript(const CScriptID &scriptid, CScript& script) const { return false; }
23  virtual bool HaveCScript(const CScriptID &scriptid) const { return false; }
24  virtual bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const { return false; }
25  virtual bool GetKey(const CKeyID &address, CKey& key) const { return false; }
26  virtual bool HaveKey(const CKeyID &address) const { return false; }
27  virtual bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const { return false; }
28 };
29 
31 
33 {
34 private:
35  const bool m_hide_secret;
36  const bool m_hide_origin;
38 
39 public:
40  HidingSigningProvider(const SigningProvider* provider, bool hide_secret, bool hide_origin) : m_hide_secret(hide_secret), m_hide_origin(hide_origin), m_provider(provider) {}
41  bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
42  bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
43  bool GetKey(const CKeyID& keyid, CKey& key) const override;
44  bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
45 };
46 
47 struct FlatSigningProvider final : public SigningProvider
48 {
49  std::map<CScriptID, CScript> scripts;
50  std::map<CKeyID, CPubKey> pubkeys;
51  std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> origins;
52  std::map<CKeyID, CKey> keys;
53 
54  bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
55  bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
56  bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
57  bool GetKey(const CKeyID& keyid, CKey& key) const override;
58 };
59 
61 
64 {
65 protected:
67 
68  using KeyMap = std::map<CKeyID, CKey>;
69  using ScriptMap = std::map<CScriptID, CScript>;
70 
71  KeyMap mapKeys GUARDED_BY(cs_KeyStore);
72  ScriptMap mapScripts GUARDED_BY(cs_KeyStore);
73 
74  void ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
75 
76 public:
77  virtual bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
78  virtual bool AddKey(const CKey &key) { return AddKeyPubKey(key, key.GetPubKey()); }
79  virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
80  virtual bool HaveKey(const CKeyID &address) const override;
81  virtual std::set<CKeyID> GetKeys() const;
82  virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override;
83  virtual bool AddCScript(const CScript& redeemScript);
84  virtual bool HaveCScript(const CScriptID &hash) const override;
85  virtual std::set<CScriptID> GetCScripts() const;
86  virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override;
87 };
88 
91 
92 #endif // BITCOIN_SCRIPT_SIGNINGPROVIDER_H
virtual ~SigningProvider()
std::map< CScriptID, CScript > ScriptMap
CCriticalSection cs_KeyStore
const SigningProvider & DUMMY_SIGNING_PROVIDER
const SigningProvider * m_provider
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:184
virtual bool HaveCScript(const CScriptID &scriptid) const
std::map< CKeyID, CKey > keys
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > origins
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
std::map< CScriptID, CScript > scripts
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
An encapsulated public key.
Definition: pubkey.h:30
Fillable signing provider that keeps keys in an address->secret map.
std::map< CKeyID, CPubKey > pubkeys
virtual bool HaveKey(const CKeyID &address) const
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
virtual bool GetKey(const CKeyID &address, CKey &key) const
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
An interface to be implemented by keystores that support signing.
std::map< CKeyID, CKey > KeyMap
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
#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
HidingSigningProvider(const SigningProvider *provider, bool hide_secret, bool hide_origin)
An encapsulated private key.
Definition: key.h:27
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:139
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
virtual bool AddKey(const CKey &key)