Bitcoin Core 31.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-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 <pubkey.h>
13#include <script/keyorigin.h>
14#include <script/script.h>
15#include <sync.h>
16#include <uint256.h>
17
18#include <compare>
19#include <cstdint>
20#include <functional>
21#include <map>
22#include <memory>
23#include <optional>
24#include <set>
25#include <span>
26#include <tuple>
27#include <utility>
28#include <variant>
29#include <vector>
30
31class MuSig2SecNonce;
32
34{
35 bool operator()(const std::vector<unsigned char>& a, const std::vector<unsigned char>& b) const
36 {
37 if (a.size() < b.size()) return true;
38 if (a.size() > b.size()) return false;
39 return a < b;
40 }
41};
42
44{
55 std::map<std::pair<std::vector<unsigned char>, int>, std::set<std::vector<unsigned char>, ShortestVectorFirstComparator>> scripts;
57 void Merge(TaprootSpendData other);
58};
59
62{
63private:
65 struct LeafInfo
66 {
67 std::vector<unsigned char> script;
69 std::vector<uint256> merkle_branch;
70 };
71
73 struct NodeInfo
74 {
79 std::vector<LeafInfo> leaves;
80 };
82 bool m_valid = true;
83
119 std::vector<std::optional<NodeInfo>> m_branch;
120
123 bool m_parity;
124
126 static NodeInfo Combine(NodeInfo&& a, NodeInfo&& b);
128 void Insert(NodeInfo&& node, int depth);
129
130public:
134 TaprootBuilder& Add(int depth, std::span<const unsigned char> script, int leaf_version, bool track = true);
136 TaprootBuilder& AddOmitted(int depth, const uint256& hash);
139 TaprootBuilder& Finalize(const XOnlyPubKey& internal_key);
140
142 bool IsValid() const { return m_valid; }
144 bool IsComplete() const { return m_valid && (m_branch.size() == 0 || (m_branch.size() == 1 && m_branch[0].has_value())); }
148 static bool ValidDepths(const std::vector<int>& depths);
152 std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>> GetTreeTuples() const;
154 bool HasScripts() const { return !m_branch.empty(); }
155
156 bool operator==(const TaprootBuilder& other) const { return GetTreeTuples() == other.GetTreeTuples(); }
157};
158
165std::optional<std::vector<std::tuple<int, std::vector<unsigned char>, int>>> InferTaprootTree(const TaprootSpendData& spenddata, const XOnlyPubKey& output);
166
169{
170public:
171 virtual ~SigningProvider() = default;
172 virtual bool GetCScript(const CScriptID &scriptid, CScript& script) const { return false; }
173 virtual bool HaveCScript(const CScriptID &scriptid) const { return false; }
174 virtual bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const { return false; }
175 virtual bool GetKey(const CKeyID &address, CKey& key) const { return false; }
176 virtual bool HaveKey(const CKeyID &address) const { return false; }
177 virtual bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const { return false; }
178 virtual bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const { return false; }
179 virtual bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const { return false; }
180 virtual std::vector<CPubKey> GetMuSig2ParticipantPubkeys(const CPubKey& pubkey) const { return {}; }
181 virtual std::map<CPubKey, std::vector<CPubKey>> GetAllMuSig2ParticipantPubkeys() const {return {}; }
182 virtual void SetMuSig2SecNonce(const uint256& id, MuSig2SecNonce&& nonce) const {}
183 virtual std::optional<std::reference_wrapper<MuSig2SecNonce>> GetMuSig2SecNonce(const uint256& session_id) const { return std::nullopt; }
184 virtual void DeleteMuSig2Session(const uint256& session_id) const {}
185
186 bool GetKeyByXOnly(const XOnlyPubKey& pubkey, CKey& key) const
187 {
188 for (const auto& id : pubkey.GetKeyIDs()) {
189 if (GetKey(id, key)) return true;
190 }
191 return false;
192 }
193
194 bool GetPubKeyByXOnly(const XOnlyPubKey& pubkey, CPubKey& out) const
195 {
196 for (const auto& id : pubkey.GetKeyIDs()) {
197 if (GetPubKey(id, out)) return true;
198 }
199 return false;
200 }
201
202 bool GetKeyOriginByXOnly(const XOnlyPubKey& pubkey, KeyOriginInfo& info) const
203 {
204 for (const auto& id : pubkey.GetKeyIDs()) {
205 if (GetKeyOrigin(id, info)) return true;
206 }
207 return false;
208 }
209};
210
212
214{
215private:
216 const bool m_hide_secret;
217 const bool m_hide_origin;
219
220public:
221 HidingSigningProvider(const SigningProvider* provider, bool hide_secret, bool hide_origin) : m_hide_secret(hide_secret), m_hide_origin(hide_origin), m_provider(provider) {}
222 bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
223 bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
224 bool GetKey(const CKeyID& keyid, CKey& key) const override;
225 bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
226 bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
227 bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override;
228 std::vector<CPubKey> GetMuSig2ParticipantPubkeys(const CPubKey& pubkey) const override;
229 std::map<CPubKey, std::vector<CPubKey>> GetAllMuSig2ParticipantPubkeys() const override;
230 void SetMuSig2SecNonce(const uint256& id, MuSig2SecNonce&& nonce) const override;
231 std::optional<std::reference_wrapper<MuSig2SecNonce>> GetMuSig2SecNonce(const uint256& session_id) const override;
232 void DeleteMuSig2Session(const uint256& session_id) const override;
233};
234
236{
237 std::map<CScriptID, CScript> scripts;
238 std::map<CKeyID, CPubKey> pubkeys;
239 std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> origins;
240 std::map<CKeyID, CKey> keys;
241 std::map<XOnlyPubKey, TaprootBuilder> tr_trees;
242 std::map<CPubKey, std::vector<CPubKey>> aggregate_pubkeys;
243 std::map<uint256, MuSig2SecNonce>* musig2_secnonces{nullptr};
244
245 bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
246 bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
247 bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
248 bool HaveKey(const CKeyID &keyid) const override;
249 bool GetKey(const CKeyID& keyid, CKey& key) const override;
250 bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
251 bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override;
252 std::vector<CPubKey> GetMuSig2ParticipantPubkeys(const CPubKey& pubkey) const override;
253 std::map<CPubKey, std::vector<CPubKey>> GetAllMuSig2ParticipantPubkeys() const override;
254 void SetMuSig2SecNonce(const uint256& id, MuSig2SecNonce&& nonce) const override;
255 std::optional<std::reference_wrapper<MuSig2SecNonce>> GetMuSig2SecNonce(const uint256& session_id) const override;
256 void DeleteMuSig2Session(const uint256& session_id) const override;
257
259};
260
263{
264protected:
265 using KeyMap = std::map<CKeyID, CKey>;
266 using ScriptMap = std::map<CScriptID, CScript>;
267
274
316
318
319public:
321
322 virtual bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
323 virtual bool AddKey(const CKey &key) { return AddKeyPubKey(key, key.GetPubKey()); }
324 virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
325 virtual bool HaveKey(const CKeyID &address) const override;
326 virtual std::set<CKeyID> GetKeys() const;
327 virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override;
328 virtual bool AddCScript(const CScript& redeemScript);
329 virtual bool HaveCScript(const CScriptID &hash) const override;
330 virtual std::set<CScriptID> GetCScripts() const;
331 virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override;
332};
333
336
339 std::vector<std::unique_ptr<SigningProvider>> m_providers;
340
341public:
342 void AddProvider(std::unique_ptr<SigningProvider> provider);
343
344 bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
345 bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
346 bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
347 bool GetKey(const CKeyID& keyid, CKey& key) const override;
348 bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
349 bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override;
350};
351
352#endif // BITCOIN_SCRIPT_SIGNINGPROVIDER_H
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
#define LIFETIMEBOUND
Definition: attributes.h:16
An encapsulated private key.
Definition: key.h:37
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:182
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:406
A reference to a CScript: the Hash160 of its serialization.
Definition: script.h:597
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
std::map< CPubKey, std::vector< CPubKey > > GetAllMuSig2ParticipantPubkeys() const override
std::optional< std::reference_wrapper< MuSig2SecNonce > > GetMuSig2SecNonce(const uint256 &session_id) const override
void SetMuSig2SecNonce(const uint256 &id, MuSig2SecNonce &&nonce) const override
bool GetKey(const CKeyID &keyid, CKey &key) const override
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
void DeleteMuSig2Session(const uint256 &session_id) 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
std::vector< CPubKey > GetMuSig2ParticipantPubkeys(const CPubKey &pubkey) const override
MuSig2SecNonce encapsulates a secret nonce in use in a MuSig2 signing session.
Definition: musig.h:41
A signing provider to be used to interface with multiple signing providers at once.
bool GetKey(const CKeyID &keyid, CKey &key) const override
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
std::vector< std::unique_ptr< SigningProvider > > m_providers
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const override
void AddProvider(std::unique_ptr< SigningProvider > provider)
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
An interface to be implemented by keystores that support signing.
virtual bool HaveKey(const CKeyID &address) const
virtual std::optional< std::reference_wrapper< MuSig2SecNonce > > GetMuSig2SecNonce(const uint256 &session_id) 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
virtual ~SigningProvider()=default
bool GetKeyByXOnly(const XOnlyPubKey &pubkey, CKey &key) const
virtual std::vector< CPubKey > GetMuSig2ParticipantPubkeys(const CPubKey &pubkey) const
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
virtual void SetMuSig2SecNonce(const uint256 &id, MuSig2SecNonce &&nonce) const
bool GetKeyOriginByXOnly(const XOnlyPubKey &pubkey, KeyOriginInfo &info) const
virtual void DeleteMuSig2Session(const uint256 &session_id) 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
virtual std::map< CPubKey, std::vector< CPubKey > > GetAllMuSig2ParticipantPubkeys() const
Utility class to construct Taproot outputs from internal key and script tree.
WitnessV1Taproot GetOutput()
Compute scriptPubKey (after Finalize()).
static NodeInfo Combine(NodeInfo &&a, NodeInfo &&b)
Combine information about a parent Merkle tree node from its child nodes.
TaprootSpendData GetSpendData() const
Compute spending data (after Finalize()).
bool IsComplete() const
Return whether there were either no leaves, or the leaves form a Huffman tree.
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.
bool HasScripts() const
Returns true if there are any tapscripts.
static bool ValidDepths(const std::vector< int > &depths)
Check if a list of depths is legal (will lead to IsComplete()).
bool operator==(const TaprootBuilder &other) const
void Insert(NodeInfo &&node, int depth)
Insert information about a node at a certain depth, and propagate information up.
XOnlyPubKey m_internal_key
The internal key, set when finalizing.
XOnlyPubKey m_output_key
The output key, computed when finalizing.
bool IsValid() const
Return true if so far all input was valid.
std::vector< std::optional< NodeInfo > > m_branch
The current state of the builder.
TaprootBuilder & AddOmitted(int depth, const uint256 &hash)
Like Add(), but for a Merkle node with a given hash to the tree.
TaprootBuilder & Finalize(const XOnlyPubKey &internal_key)
Finalize the construction.
bool m_parity
The tweak parity, computed when finalizing.
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.
bool m_valid
Whether the builder is in a valid state so far.
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
256-bit opaque blob.
Definition: uint256.h:196
unsigned int nonce
Definition: miner_tests.cpp:99
Definition: messages.h:21
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.
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::map< CPubKey, std::vector< CPubKey > > GetAllMuSig2ParticipantPubkeys() const override
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
FlatSigningProvider & Merge(FlatSigningProvider &&b) LIFETIMEBOUND
std::optional< std::reference_wrapper< MuSig2SecNonce > > GetMuSig2SecNonce(const uint256 &session_id) const override
void SetMuSig2SecNonce(const uint256 &id, MuSig2SecNonce &&nonce) const override
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< CPubKey, std::vector< CPubKey > > aggregate_pubkeys
Map from output key to Taproot tree (which can then make the TaprootSpendData.
std::map< uint256, MuSig2SecNonce > * musig2_secnonces
MuSig2 aggregate pubkeys.
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
void DeleteMuSig2Session(const uint256 &session_id) const override
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
std::vector< CPubKey > GetMuSig2ParticipantPubkeys(const CPubKey &pubkey) const override
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
bool HaveKey(const CKeyID &keyid) const override
bool operator()(const std::vector< unsigned char > &a, const std::vector< unsigned char > &b) const
Information about a tracked leaf in the Merkle tree.
std::vector< uint256 > merkle_branch
The hashing partners above this leaf.
int leaf_version
The leaf version for that script.
std::vector< unsigned char > script
The script.
Information associated with a node in the Merkle tree.
uint256 hash
Merkle hash of this node.
std::vector< LeafInfo > leaves
Tracked leaves underneath this node (either from the node itself, or its children).
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
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.
void Merge(TaprootSpendData other)
Merge other TaprootSpendData (for the same scriptPubKey) into this.
XOnlyPubKey internal_key
The BIP341 internal key.
FuzzedDataProvider provider
Definition: dbwrapper.cpp:388
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49