Bitcoin Core  24.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-2022 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 <attributes.h>
10 #include <key.h>
11 #include <pubkey.h>
12 #include <script/keyorigin.h>
13 #include <script/script.h>
14 #include <script/standard.h>
15 #include <sync.h>
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  virtual bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const { return false; }
29  virtual bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const { return false; }
30 
31  bool GetKeyByXOnly(const XOnlyPubKey& pubkey, CKey& key) const
32  {
33  for (const auto& id : pubkey.GetKeyIDs()) {
34  if (GetKey(id, key)) return true;
35  }
36  return false;
37  }
38 
39  bool GetPubKeyByXOnly(const XOnlyPubKey& pubkey, CPubKey& out) const
40  {
41  for (const auto& id : pubkey.GetKeyIDs()) {
42  if (GetPubKey(id, out)) return true;
43  }
44  return false;
45  }
46 
47  bool GetKeyOriginByXOnly(const XOnlyPubKey& pubkey, KeyOriginInfo& info) const
48  {
49  for (const auto& id : pubkey.GetKeyIDs()) {
50  if (GetKeyOrigin(id, info)) return true;
51  }
52  return false;
53  }
54 };
55 
57 
59 {
60 private:
61  const bool m_hide_secret;
62  const bool m_hide_origin;
64 
65 public:
66  HidingSigningProvider(const SigningProvider* provider, bool hide_secret, bool hide_origin) : m_hide_secret(hide_secret), m_hide_origin(hide_origin), m_provider(provider) {}
67  bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
68  bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
69  bool GetKey(const CKeyID& keyid, CKey& key) const override;
70  bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
71  bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
72  bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override;
73 };
74 
75 struct FlatSigningProvider final : public SigningProvider
76 {
77  std::map<CScriptID, CScript> scripts;
78  std::map<CKeyID, CPubKey> pubkeys;
79  std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> origins;
80  std::map<CKeyID, CKey> keys;
81  std::map<XOnlyPubKey, TaprootBuilder> tr_trees;
83  bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
84  bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
85  bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
86  bool GetKey(const CKeyID& keyid, CKey& key) const override;
87  bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
88  bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override;
89 
91 };
92 
95 {
96 protected:
97  using KeyMap = std::map<CKeyID, CKey>;
98  using ScriptMap = std::map<CScriptID, CScript>;
99 
106 
148 
150 
151 public:
153 
154  virtual bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
155  virtual bool AddKey(const CKey &key) { return AddKeyPubKey(key, key.GetPubKey()); }
156  virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
157  virtual bool HaveKey(const CKeyID &address) const override;
158  virtual std::set<CKeyID> GetKeys() const;
159  virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override;
160  virtual bool AddCScript(const CScript& redeemScript);
161  virtual bool HaveCScript(const CScriptID &hash) const override;
162  virtual std::set<CScriptID> GetCScripts() const;
163  virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override;
164 };
165 
167 CKeyID GetKeyForDestination(const SigningProvider& store, const CTxDestination& dest);
168 
169 #endif // BITCOIN_SCRIPT_SIGNINGPROVIDER_H
#define LIFETIMEBOUND
Definition: attributes.h:16
An encapsulated private key.
Definition: key.h:27
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:24
An encapsulated public key.
Definition: pubkey.h:34
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:27
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 GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) 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 GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const override
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 GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const
virtual bool HaveCScript(const CScriptID &scriptid) const
bool GetKeyByXOnly(const XOnlyPubKey &pubkey, CKey &key) const
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
bool GetKeyOriginByXOnly(const XOnlyPubKey &pubkey, KeyOriginInfo &info) const
virtual bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const
virtual bool GetKey(const CKeyID &address, CKey &key) const
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
bool GetPubKeyByXOnly(const XOnlyPubKey &pubkey, CPubKey &out) const
Utility class to construct Taproot outputs from internal key and script tree.
Definition: standard.h:227
std::vector< CKeyID > GetKeyIDs() const
Returns a list of CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey.
Definition: pubkey.cpp:189
const SigningProvider & DUMMY_SIGNING_PROVIDER
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:149
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
FlatSigningProvider & Merge(FlatSigningProvider &&b) LIFETIMEBOUND
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > origins
bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const override
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
std::map< XOnlyPubKey, TaprootBuilder > tr_trees
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
Map from output key to Taproot tree (which can then make the TaprootSpendData.
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49