Bitcoin Core  0.21.0rc5
P2P Digital Currency
signingprovider.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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:
66  using KeyMap = std::map<CKeyID, CKey>;
67  using ScriptMap = std::map<CScriptID, CScript>;
68 
75 
117 
119 
120 public:
122 
123  virtual bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
124  virtual bool AddKey(const CKey &key) { return AddKeyPubKey(key, key.GetPubKey()); }
125  virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
126  virtual bool HaveKey(const CKeyID &address) const override;
127  virtual std::set<CKeyID> GetKeys() const;
128  virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override;
129  virtual bool AddCScript(const CScript& redeemScript);
130  virtual bool HaveCScript(const CScriptID &hash) const override;
131  virtual std::set<CScriptID> GetCScripts() const;
132  virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override;
133 };
134 
136 CKeyID GetKeyForDestination(const SigningProvider& store, const CTxDestination& dest);
137 
138 #endif // BITCOIN_SCRIPT_SIGNINGPROVIDER_H
An encapsulated private key.
Definition: key.h:28
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:184
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
An encapsulated public key.
Definition: pubkey.h:32
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:89
Fillable signing provider that keeps keys in an address->secret map.
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override
virtual bool AddCScript(const CScript &redeemScript)
std::map< CScriptID, CScript > ScriptMap
KeyMap mapKeys GUARDED_BY(cs_KeyStore)
Map of key id to unencrypted private keys known by the signing provider.
ScriptMap mapScripts GUARDED_BY(cs_KeyStore)
Map of script id to scripts known by the signing provider.
void ImplicitlyLearnRelatedKeyScripts(const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
virtual std::set< CKeyID > GetKeys() const
std::map< CKeyID, CKey > KeyMap
virtual std::set< CScriptID > GetCScripts() const
virtual bool HaveCScript(const CScriptID &hash) const override
virtual bool AddKey(const CKey &key)
RecursiveMutex cs_KeyStore
virtual bool HaveKey(const CKeyID &address) const override
bool GetKey(const CKeyID &keyid, CKey &key) const override
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
const SigningProvider * m_provider
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
HidingSigningProvider(const SigningProvider *provider, bool hide_secret, bool hide_origin)
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
An interface to be implemented by keystores that support signing.
virtual ~SigningProvider()
virtual bool HaveKey(const CKeyID &address) const
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
virtual bool HaveCScript(const CScriptID &scriptid) const
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
virtual bool GetKey(const CKeyID &address, CKey &key) const
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
const SigningProvider & DUMMY_SIGNING_PROVIDER
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
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:214
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > origins
bool GetKey(const CKeyID &keyid, CKey &key) const override
std::map< CKeyID, CPubKey > pubkeys
std::map< CKeyID, CKey > keys
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
std::map< CScriptID, CScript > scripts
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49