Bitcoin Core  21.99.0
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 
74  KeyMap mapKeys GUARDED_BY(cs_KeyStore);
75 
116  ScriptMap mapScripts GUARDED_BY(cs_KeyStore);
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
FillableSigningProvider::KeyMap
std::map< CKeyID, CKey > KeyMap
Definition: signingprovider.h:66
FillableSigningProvider::ScriptMap
std::map< CScriptID, CScript > ScriptMap
Definition: signingprovider.h:67
SigningProvider::~SigningProvider
virtual ~SigningProvider()
Definition: signingprovider.h:21
FillableSigningProvider::GetKeys
virtual std::set< CKeyID > GetKeys() const
Definition: signingprovider.cpp:119
HidingSigningProvider::m_provider
const SigningProvider * m_provider
Definition: signingprovider.h:37
SigningProvider::HaveCScript
virtual bool HaveCScript(const CScriptID &scriptid) const
Definition: signingprovider.h:23
sync.h
FillableSigningProvider
Fillable signing provider that keeps keys in an address->secret map.
Definition: signingprovider.h:63
FlatSigningProvider::keys
std::map< CKeyID, CKey > keys
Definition: signingprovider.h:52
SigningProvider
An interface to be implemented by keystores that support signing.
Definition: signingprovider.h:18
HidingSigningProvider::HidingSigningProvider
HidingSigningProvider(const SigningProvider *provider, bool hide_secret, bool hide_origin)
Definition: signingprovider.h:40
AnnotatedMixin< std::recursive_mutex >
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
pubkey.h
SigningProvider::GetPubKey
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
Definition: signingprovider.h:24
FillableSigningProvider::AddKey
virtual bool AddKey(const CKey &key)
Definition: signingprovider.h:124
FillableSigningProvider::ImplicitlyLearnRelatedKeyScripts
void ImplicitlyLearnRelatedKeyScripts(const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
Definition: signingprovider.cpp:72
FlatSigningProvider::GetKeyOrigin
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
Definition: signingprovider.cpp:49
FillableSigningProvider::GetCScripts
virtual std::set< CScriptID > GetCScripts() const
Definition: signingprovider.cpp:156
FlatSigningProvider::scripts
std::map< CScriptID, CScript > scripts
Definition: signingprovider.h:49
SigningProvider::HaveKey
virtual bool HaveKey(const CKeyID &address) const
Definition: signingprovider.h:26
HidingSigningProvider::GetKeyOrigin
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
Definition: signingprovider.cpp:41
FlatSigningProvider::origins
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > origins
Definition: signingprovider.h:51
HidingSigningProvider::m_hide_secret
const bool m_hide_secret
Definition: signingprovider.h:35
FillableSigningProvider::GetPubKey
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
Definition: signingprovider.cpp:95
SigningProvider::GetKeyOrigin
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
Definition: signingprovider.h:27
Merge
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
Definition: signingprovider.cpp:58
DUMMY_SIGNING_PROVIDER
const SigningProvider & DUMMY_SIGNING_PROVIDER
Definition: signingprovider.cpp:12
standard.h
FillableSigningProvider::HaveCScript
virtual bool HaveCScript(const CScriptID &hash) const override
Definition: signingprovider.cpp:150
SigningProvider::GetCScript
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
Definition: signingprovider.h:22
FlatSigningProvider::pubkeys
std::map< CKeyID, CPubKey > pubkeys
Definition: signingprovider.h:50
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:184
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
key.h
FlatSigningProvider::GetKey
bool GetKey(const CKeyID &keyid, CKey &key) const override
Definition: signingprovider.cpp:56
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
CKey
An encapsulated private key.
Definition: key.h:27
FillableSigningProvider::GetKey
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override
Definition: signingprovider.cpp:129
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
HidingSigningProvider::GetPubKey
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
Definition: signingprovider.cpp:30
FillableSigningProvider::AddKeyPubKey
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Definition: signingprovider.cpp:105
HidingSigningProvider::m_hide_origin
const bool m_hide_origin
Definition: signingprovider.h:36
SigningProvider::GetKey
virtual bool GetKey(const CKeyID &address, CKey &key) const
Definition: signingprovider.h:25
CTxDestination
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:212
FillableSigningProvider::GetCScript
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
Definition: signingprovider.cpp:166
FillableSigningProvider::AddCScript
virtual bool AddCScript(const CScript &redeemScript)
Definition: signingprovider.cpp:140
GetKeyForDestination
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
Definition: signingprovider.cpp:178
script.h
HidingSigningProvider::GetCScript
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
Definition: signingprovider.cpp:25
FlatSigningProvider::GetCScript
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
Definition: signingprovider.cpp:47
FillableSigningProvider::GUARDED_BY
KeyMap mapKeys GUARDED_BY(cs_KeyStore)
Map of key id to unencrypted private keys known by the signing provider.
FillableSigningProvider::HaveKey
virtual bool HaveKey(const CKeyID &address) const override
Definition: signingprovider.cpp:113
FillableSigningProvider::cs_KeyStore
RecursiveMutex cs_KeyStore
Definition: signingprovider.h:121
HidingSigningProvider
Definition: signingprovider.h:32
FlatSigningProvider::GetPubKey
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
Definition: signingprovider.cpp:48
HidingSigningProvider::GetKey
bool GetKey(const CKeyID &keyid, CKey &key) const override
Definition: signingprovider.cpp:35
CScriptID
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:86
KeyOriginInfo
Definition: keyorigin.h:11
FlatSigningProvider
Definition: signingprovider.h:47