Bitcoin Core  31.0.0
P2P Digital Currency
signingprovider.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-present 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 <addresstype.h>
10 #include <attributes.h>
11 #include <key.h>
12 #include <musig.h>
13 #include <pubkey.h>
14 #include <script/keyorigin.h>
15 #include <script/script.h>
16 #include <sync.h>
17 
18 #include <functional>
19 #include <optional>
20 
22 {
23  bool operator()(const std::vector<unsigned char>& a, const std::vector<unsigned char>& b) const
24  {
25  if (a.size() < b.size()) return true;
26  if (a.size() > b.size()) return false;
27  return a < b;
28  }
29 };
30 
32 {
43  std::map<std::pair<std::vector<unsigned char>, int>, std::set<std::vector<unsigned char>, ShortestVectorFirstComparator>> scripts;
45  void Merge(TaprootSpendData other);
46 };
47 
50 {
51 private:
53  struct LeafInfo
54  {
55  std::vector<unsigned char> script;
57  std::vector<uint256> merkle_branch;
58  };
59 
61  struct NodeInfo
62  {
67  std::vector<LeafInfo> leaves;
68  };
70  bool m_valid = true;
71 
107  std::vector<std::optional<NodeInfo>> m_branch;
108 
111  bool m_parity;
112 
114  static NodeInfo Combine(NodeInfo&& a, NodeInfo&& b);
116  void Insert(NodeInfo&& node, int depth);
117 
118 public:
122  TaprootBuilder& Add(int depth, std::span<const unsigned char> script, int leaf_version, bool track = true);
124  TaprootBuilder& AddOmitted(int depth, const uint256& hash);
127  TaprootBuilder& Finalize(const XOnlyPubKey& internal_key);
128 
130  bool IsValid() const { return m_valid; }
132  bool IsComplete() const { return m_valid && (m_branch.size() == 0 || (m_branch.size() == 1 && m_branch[0].has_value())); }
136  static bool ValidDepths(const std::vector<int>& depths);
140  std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>> GetTreeTuples() const;
142  bool HasScripts() const { return !m_branch.empty(); }
143 
144  bool operator==(const TaprootBuilder& other) const { return GetTreeTuples() == other.GetTreeTuples(); }
145 };
146 
153 std::optional<std::vector<std::tuple<int, std::vector<unsigned char>, int>>> InferTaprootTree(const TaprootSpendData& spenddata, const XOnlyPubKey& output);
154 
157 {
158 public:
159  virtual ~SigningProvider() = default;
160  virtual bool GetCScript(const CScriptID &scriptid, CScript& script) const { return false; }
161  virtual bool HaveCScript(const CScriptID &scriptid) const { return false; }
162  virtual bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const { return false; }
163  virtual bool GetKey(const CKeyID &address, CKey& key) const { return false; }
164  virtual bool HaveKey(const CKeyID &address) const { return false; }
165  virtual bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const { return false; }
166  virtual bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const { return false; }
167  virtual bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const { return false; }
168  virtual std::vector<CPubKey> GetMuSig2ParticipantPubkeys(const CPubKey& pubkey) const { return {}; }
169  virtual std::map<CPubKey, std::vector<CPubKey>> GetAllMuSig2ParticipantPubkeys() const {return {}; }
170  virtual void SetMuSig2SecNonce(const uint256& id, MuSig2SecNonce&& nonce) const {}
171  virtual std::optional<std::reference_wrapper<MuSig2SecNonce>> GetMuSig2SecNonce(const uint256& session_id) const { return std::nullopt; }
172  virtual void DeleteMuSig2Session(const uint256& session_id) const {}
173 
174  bool GetKeyByXOnly(const XOnlyPubKey& pubkey, CKey& key) const
175  {
176  for (const auto& id : pubkey.GetKeyIDs()) {
177  if (GetKey(id, key)) return true;
178  }
179  return false;
180  }
181 
182  bool GetPubKeyByXOnly(const XOnlyPubKey& pubkey, CPubKey& out) const
183  {
184  for (const auto& id : pubkey.GetKeyIDs()) {
185  if (GetPubKey(id, out)) return true;
186  }
187  return false;
188  }
189 
190  bool GetKeyOriginByXOnly(const XOnlyPubKey& pubkey, KeyOriginInfo& info) const
191  {
192  for (const auto& id : pubkey.GetKeyIDs()) {
193  if (GetKeyOrigin(id, info)) return true;
194  }
195  return false;
196  }
197 };
198 
200 
202 {
203 private:
204  const bool m_hide_secret;
205  const bool m_hide_origin;
207 
208 public:
209  HidingSigningProvider(const SigningProvider* provider, bool hide_secret, bool hide_origin) : m_hide_secret(hide_secret), m_hide_origin(hide_origin), m_provider(provider) {}
210  bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
211  bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
212  bool GetKey(const CKeyID& keyid, CKey& key) const override;
213  bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
214  bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
215  bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override;
216  std::vector<CPubKey> GetMuSig2ParticipantPubkeys(const CPubKey& pubkey) const override;
217  std::map<CPubKey, std::vector<CPubKey>> GetAllMuSig2ParticipantPubkeys() const override;
218  void SetMuSig2SecNonce(const uint256& id, MuSig2SecNonce&& nonce) const override;
219  std::optional<std::reference_wrapper<MuSig2SecNonce>> GetMuSig2SecNonce(const uint256& session_id) const override;
220  void DeleteMuSig2Session(const uint256& session_id) const override;
221 };
222 
224 {
225  std::map<CScriptID, CScript> scripts;
226  std::map<CKeyID, CPubKey> pubkeys;
227  std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> origins;
228  std::map<CKeyID, CKey> keys;
229  std::map<XOnlyPubKey, TaprootBuilder> tr_trees;
230  std::map<CPubKey, std::vector<CPubKey>> aggregate_pubkeys;
231  std::map<uint256, MuSig2SecNonce>* musig2_secnonces{nullptr};
232 
233  bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
234  bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
235  bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
236  bool HaveKey(const CKeyID &keyid) const override;
237  bool GetKey(const CKeyID& keyid, CKey& key) const override;
238  bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
239  bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override;
240  std::vector<CPubKey> GetMuSig2ParticipantPubkeys(const CPubKey& pubkey) const override;
241  std::map<CPubKey, std::vector<CPubKey>> GetAllMuSig2ParticipantPubkeys() const override;
242  void SetMuSig2SecNonce(const uint256& id, MuSig2SecNonce&& nonce) const override;
243  std::optional<std::reference_wrapper<MuSig2SecNonce>> GetMuSig2SecNonce(const uint256& session_id) const override;
244  void DeleteMuSig2Session(const uint256& session_id) const override;
245 
247 };
248 
251 {
252 protected:
253  using KeyMap = std::map<CKeyID, CKey>;
254  using ScriptMap = std::map<CScriptID, CScript>;
255 
261  KeyMap mapKeys GUARDED_BY(cs_KeyStore);
262 
303  ScriptMap mapScripts GUARDED_BY(cs_KeyStore);
304 
306 
307 public:
309 
310  virtual bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
311  virtual bool AddKey(const CKey &key) { return AddKeyPubKey(key, key.GetPubKey()); }
312  virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
313  virtual bool HaveKey(const CKeyID &address) const override;
314  virtual std::set<CKeyID> GetKeys() const;
315  virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override;
316  virtual bool AddCScript(const CScript& redeemScript);
317  virtual bool HaveCScript(const CScriptID &hash) const override;
318  virtual std::set<CScriptID> GetCScripts() const;
319  virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override;
320 };
321 
323 CKeyID GetKeyForDestination(const SigningProvider& store, const CTxDestination& dest);
324 
327  std::vector<std::unique_ptr<SigningProvider>> m_providers;
328 
329 public:
330  void AddProvider(std::unique_ptr<SigningProvider> provider);
331 
332  bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
333  bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
334  bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
335  bool GetKey(const CKeyID& keyid, CKey& key) const override;
336  bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
337  bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override;
338 };
339 
340 #endif // BITCOIN_SCRIPT_SIGNINGPROVIDER_H
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
void SetMuSig2SecNonce(const uint256 &id, MuSig2SecNonce &&nonce) const override
uint256 hash
Merkle hash of this node.
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
bool HasScripts() const
Returns true if there are any tapscripts.
std::map< CScriptID, CScript > ScriptMap
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
bool GetPubKeyByXOnly(const XOnlyPubKey &pubkey, CPubKey &out) const
std::vector< CPubKey > GetMuSig2ParticipantPubkeys(const CPubKey &pubkey) const override
const SigningProvider & DUMMY_SIGNING_PROVIDER
bool operator==(const TaprootBuilder &other) const
unsigned int nonce
Definition: miner_tests.cpp:82
const SigningProvider * m_provider
RecursiveMutex cs_KeyStore
std::optional< std::reference_wrapper< MuSig2SecNonce > > GetMuSig2SecNonce(const uint256 &session_id) const override
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:183
virtual bool HaveCScript(const CScriptID &scriptid) const
virtual std::vector< CPubKey > GetMuSig2ParticipantPubkeys(const CPubKey &pubkey) const
std::map< CKeyID, CKey > keys
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
virtual bool AddCScript(const CScript &redeemScript)
Information associated with a node in the Merkle tree.
TaprootBuilder & AddOmitted(int depth, const uint256 &hash)
Like Add(), but for a Merkle node with a given hash to the tree.
virtual std::set< CScriptID > GetCScripts() const
std::vector< std::unique_ptr< SigningProvider > > m_providers
virtual std::set< CKeyID > GetKeys() const
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > origins
bool operator()(const std::vector< unsigned char > &a, const std::vector< unsigned char > &b) const
MuSig2SecNonce encapsulates a secret nonce in use in a MuSig2 signing session.
Definition: musig.h:39
virtual void DeleteMuSig2Session(const uint256 &session_id) const
std::vector< unsigned char > script
The script.
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
std::vector< std::tuple< uint8_t, uint8_t, std::vector< unsigned char > > > GetTreeTuples() const
Returns a vector of tuples representing the depth, leaf version, and script.
std::vector< LeafInfo > leaves
Tracked leaves underneath this node (either from the node itself, or its children).
void AddProvider(std::unique_ptr< SigningProvider > provider)
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
virtual std::optional< std::reference_wrapper< MuSig2SecNonce > > GetMuSig2SecNonce(const uint256 &session_id) const
virtual ~SigningProvider()=default
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
std::vector< std::optional< NodeInfo > > m_branch
The current state of the builder.
void DeleteMuSig2Session(const uint256 &session_id) const override
bool GetKeyOriginByXOnly(const XOnlyPubKey &pubkey, KeyOriginInfo &info) const
XOnlyPubKey m_internal_key
The internal key, set when finalizing.
virtual bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const
std::map< uint256, MuSig2SecNonce > * musig2_secnonces
MuSig2 aggregate pubkeys.
#define LIFETIMEBOUND
Definition: attributes.h:16
std::map< std::pair< std::vector< unsigned char >, int >, std::set< std::vector< unsigned char >, ShortestVectorFirstComparator > > scripts
Map from (script, leaf_version) to (sets of) control blocks.
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
std::map< CScriptID, CScript > scripts
bool HaveKey(const CKeyID &keyid) const override
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
TaprootBuilder & Finalize(const XOnlyPubKey &internal_key)
Finalize the construction.
XOnlyPubKey m_output_key
The output key, computed when finalizing.
An encapsulated public key.
Definition: pubkey.h:33
bool GetKey(const CKeyID &keyid, CKey &key) const override
Fillable signing provider that keeps keys in an address->secret map.
std::map< CKeyID, CPubKey > pubkeys
virtual bool HaveKey(const CKeyID &address) const
WitnessV1Taproot GetOutput()
Compute scriptPubKey (after Finalize()).
virtual std::map< CPubKey, std::vector< CPubKey > > GetAllMuSig2ParticipantPubkeys() const
void SetMuSig2SecNonce(const uint256 &id, MuSig2SecNonce &&nonce) const override
void ImplicitlyLearnRelatedKeyScripts(const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
A signing provider to be used to interface with multiple signing providers at once.
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
virtual bool GetKey(const CKeyID &address, CKey &key) const
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override
FlatSigningProvider & Merge(FlatSigningProvider &&b) LIFETIMEBOUND
std::map< CPubKey, std::vector< CPubKey > > GetAllMuSig2ParticipantPubkeys() const override
std::map< CPubKey, std::vector< CPubKey > > aggregate_pubkeys
Map from output key to Taproot tree (which can then make the TaprootSpendData.
TaprootSpendData GetSpendData() const
Compute spending data (after Finalize()).
Definition: messages.h:21
bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const override
Utility class to construct Taproot outputs from internal key and script tree.
bool m_parity
The tweak parity, computed when finalizing.
void Insert(NodeInfo &&node, int depth)
Insert information about a node at a certain depth, and propagate information up. ...
256-bit opaque blob.
Definition: uint256.h:195
TaprootBuilder & Add(int depth, std::span< const unsigned char > script, int leaf_version, bool track=true)
Add a new script at a certain depth in the tree.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
bool GetKey(const CKeyID &keyid, CKey &key) const override
static bool ValidDepths(const std::vector< int > &depths)
Check if a list of depths is legal (will lead to IsComplete()).
bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const override
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:404
XOnlyPubKey internal_key
The BIP341 internal key.
bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const override
std::vector< uint256 > merkle_branch
The hashing partners above this leaf.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
std::optional< std::vector< std::tuple< int, std::vector< unsigned char >, int > > > InferTaprootTree(const TaprootSpendData &spenddata, const XOnlyPubKey &output)
Given a TaprootSpendData and the output key, reconstruct its script tree.
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:23
std::optional< std::reference_wrapper< MuSig2SecNonce > > GetMuSig2SecNonce(const uint256 &session_id) const override
int leaf_version
The leaf version for that script.
virtual bool HaveCScript(const CScriptID &hash) const override
A reference to a CScript: the Hash160 of its serialization.
Definition: script.h:593
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
HidingSigningProvider(const SigningProvider *provider, bool hide_secret, bool hide_origin)
bool IsComplete() const
Return whether there were either no leaves, or the leaves form a Huffman tree.
std::vector< CPubKey > GetMuSig2ParticipantPubkeys(const CPubKey &pubkey) const override
An encapsulated private key.
Definition: key.h:35
bool GetKeyByXOnly(const XOnlyPubKey &pubkey, CKey &key) const
bool GetKey(const CKeyID &keyid, CKey &key) const override
virtual void SetMuSig2SecNonce(const uint256 &id, MuSig2SecNonce &&nonce) const
static NodeInfo Combine(NodeInfo &&a, NodeInfo &&b)
Combine information about a parent Merkle tree node from its child nodes.
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
bool IsValid() const
Return true if so far all input was valid.
std::map< CPubKey, std::vector< CPubKey > > GetAllMuSig2ParticipantPubkeys() const override
virtual bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
Information about a tracked leaf in the Merkle tree.
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:214
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
virtual bool AddKey(const CKey &key)
KeyMap mapKeys GUARDED_BY(cs_KeyStore)
Map of key id to unencrypted private keys known by the signing provider.
std::map< XOnlyPubKey, TaprootBuilder > tr_trees
void Merge(TaprootSpendData other)
Merge other TaprootSpendData (for the same scriptPubKey) into this.
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
bool m_valid
Whether the builder is in a valid state so far.
void DeleteMuSig2Session(const uint256 &session_id) const override
virtual bool HaveKey(const CKeyID &address) const override