Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
signingprovider.cpp
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
7
8#include <musig.h>
10#include <script/keyorigin.h>
11#include <util/check.h>
12#include <util/log.h>
13
14#include <algorithm>
15#include <cstddef>
16#include <variant>
17
19
20template<typename M, typename K, typename V>
21bool LookupHelper(const M& map, const K& key, V& value)
22{
23 auto it = map.find(key);
24 if (it != map.end()) {
25 value = it->second;
26 return true;
27 }
28 return false;
29}
30
35
37{
38 return m_provider->GetPubKey(keyid, pubkey);
39}
40
42{
43 if (m_hide_secret) return false;
44 return m_provider->GetKey(keyid, key);
45}
46
48{
49 if (m_hide_origin) return false;
50 return m_provider->GetKeyOrigin(keyid, info);
51}
52
61std::vector<CPubKey> HidingSigningProvider::GetMuSig2ParticipantPubkeys(const CPubKey& pubkey) const
62{
63 if (m_hide_origin) return {};
65}
66
67std::map<CPubKey, std::vector<CPubKey>> HidingSigningProvider::GetAllMuSig2ParticipantPubkeys() const
68{
70}
71
76
77std::optional<std::reference_wrapper<MuSig2SecNonce>> HidingSigningProvider::GetMuSig2SecNonce(const uint256& session_id) const
78{
79 return m_provider->GetMuSig2SecNonce(session_id);
80}
81
83{
85}
86
88bool FlatSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const { return LookupHelper(pubkeys, keyid, pubkey); }
90{
91 std::pair<CPubKey, KeyOriginInfo> out;
92 bool ret = LookupHelper(origins, keyid, out);
93 if (ret) info = std::move(out.second);
94 return ret;
95}
97{
98 CKey key;
99 return LookupHelper(keys, keyid, key);
100}
101bool FlatSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const { return LookupHelper(keys, keyid, key); }
115
116std::vector<CPubKey> FlatSigningProvider::GetMuSig2ParticipantPubkeys(const CPubKey& pubkey) const
117{
118 std::vector<CPubKey> participant_pubkeys;
120 return participant_pubkeys;
121}
122
123std::map<CPubKey, std::vector<CPubKey>> FlatSigningProvider::GetAllMuSig2ParticipantPubkeys() const
124{
125 return aggregate_pubkeys;
126}
127
129{
130 if (!Assume(musig2_secnonces)) return;
131 auto [it, inserted] = musig2_secnonces->try_emplace(session_id, std::move(nonce));
132 // No secnonce should exist for this session yet.
134}
135
136std::optional<std::reference_wrapper<MuSig2SecNonce>> FlatSigningProvider::GetMuSig2SecNonce(const uint256& session_id) const
137{
138 if (!Assume(musig2_secnonces)) return std::nullopt;
139 const auto& it = musig2_secnonces->find(session_id);
140 if (it == musig2_secnonces->end()) return std::nullopt;
141 return it->second;
142}
143
145{
146 if (!Assume(musig2_secnonces)) return;
147 musig2_secnonces->erase(session_id);
148}
149
151{
152 scripts.merge(b.scripts);
153 pubkeys.merge(b.pubkeys);
154 keys.merge(b.keys);
155 origins.merge(b.origins);
156 tr_trees.merge(b.tr_trees);
157 aggregate_pubkeys.merge(b.aggregate_pubkeys);
158 // We shouldn't be merging 2 different sessions, just overwrite with b's sessions.
160 return *this;
161}
162
164{
166 CKeyID key_id = pubkey.GetID();
167 // This adds the redeemscripts necessary to detect P2WPKH and P2SH-P2WPKH
168 // outputs. Technically P2WPKH outputs don't have a redeemscript to be
169 // spent. However, our current IsMine logic requires the corresponding
170 // P2SH-P2WPKH redeemscript to be present in the wallet in order to accept
171 // payment even to P2WPKH outputs.
172 // Also note that having superfluous scripts in the keystore never hurts.
173 // They're only used to guide recursion in signing and IsMine logic - if
174 // a script is present but we can't do anything with it, it has no effect.
175 // "Implicitly" refers to fact that scripts are derived automatically from
176 // existing keys, and are present in memory, even without being explicitly
177 // loaded (e.g. from a file).
178 if (pubkey.IsCompressed()) {
180 // This does not use AddCScript, as it may be overridden.
181 CScriptID id(script);
182 mapScripts[id] = std::move(script);
183 }
184}
185
187{
188 CKey key;
189 if (!GetKey(address, key)) {
190 return false;
191 }
192 vchPubKeyOut = key.GetPubKey();
193 return true;
194}
195
197{
199 mapKeys[pubkey.GetID()] = key;
201 return true;
202}
203
204bool FillableSigningProvider::HaveKey(const CKeyID &address) const
205{
207 return mapKeys.contains(address);
208}
209
210std::set<CKeyID> FillableSigningProvider::GetKeys() const
211{
213 std::set<CKeyID> set_address;
214 for (const auto& mi : mapKeys) {
215 set_address.insert(mi.first);
216 }
217 return set_address;
218}
219
221{
223 KeyMap::const_iterator mi = mapKeys.find(address);
224 if (mi != mapKeys.end()) {
225 keyOut = mi->second;
226 return true;
227 }
228 return false;
229}
230
232{
234 LogInfo("FillableSigningProvider::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE); // TODO: remove, callers should take care of checking input / reporting errors
235 return false;
236 }
237
240 return true;
241}
242
244{
246 return mapScripts.contains(hash);
247}
248
250{
252 ScriptMap::const_iterator mi = mapScripts.find(hash);
253 if (mi != mapScripts.end())
254 {
255 redeemScriptOut = (*mi).second;
256 return true;
257 }
258 return false;
259}
260
262{
263 // Only supports destinations which map to single public keys:
264 // P2PKH, P2WPKH, P2SH-P2WPKH, P2TR
265 if (auto id = std::get_if<PKHash>(&dest)) {
266 return ToKeyID(*id);
267 }
268 if (auto witness_id = std::get_if<WitnessV0KeyHash>(&dest)) {
269 return ToKeyID(*witness_id);
270 }
271 if (auto script_hash = std::get_if<ScriptHash>(&dest)) {
276 if (auto inner_witness_id = std::get_if<WitnessV0KeyHash>(&inner_dest)) {
277 return ToKeyID(*inner_witness_id);
278 }
279 }
280 }
281 if (auto output_key = std::get_if<WitnessV1Taproot>(&dest)) {
283 CPubKey pub;
285 && !spenddata.internal_key.IsNull()
286 && spenddata.merkle_root.IsNull()
287 && store.GetPubKeyByXOnly(spenddata.internal_key, pub)) {
288 return pub.GetID();
289 }
290 }
291 return CKeyID();
292}
293
294void MultiSigningProvider::AddProvider(std::unique_ptr<SigningProvider> provider)
295{
296 m_providers.push_back(std::move(provider));
297}
298
300{
301 for (const auto& provider: m_providers) {
302 if (provider->GetCScript(scriptid, script)) return true;
303 }
304 return false;
305}
306
308{
309 for (const auto& provider: m_providers) {
310 if (provider->GetPubKey(keyid, pubkey)) return true;
311 }
312 return false;
313}
314
315
317{
318 for (const auto& provider: m_providers) {
319 if (provider->GetKeyOrigin(keyid, info)) return true;
320 }
321 return false;
322}
323
325{
326 for (const auto& provider: m_providers) {
327 if (provider->GetKey(keyid, key)) return true;
328 }
329 return false;
330}
331
333{
334 for (const auto& provider: m_providers) {
335 if (provider->GetTaprootSpendData(output_key, spenddata)) return true;
336 }
337 return false;
338}
339
341{
342 for (const auto& provider: m_providers) {
343 if (provider->GetTaprootBuilder(output_key, builder)) return true;
344 }
345 return false;
346}
347
349{
351 /* Iterate over all tracked leaves in a, add b's hash to their Merkle branch, and move them to ret. */
352 for (auto& leaf : a.leaves) {
353 leaf.merkle_branch.push_back(b.hash);
354 ret.leaves.emplace_back(std::move(leaf));
355 }
356 /* Iterate over all tracked leaves in b, add a's hash to their Merkle branch, and move them to ret. */
357 for (auto& leaf : b.leaves) {
358 leaf.merkle_branch.push_back(a.hash);
359 ret.leaves.emplace_back(std::move(leaf));
360 }
361 ret.hash = ComputeTapbranchHash(a.hash, b.hash);
362 return ret;
363}
364
366{
367 // TODO: figure out how to better deal with conflicting information
368 // being merged.
369 if (internal_key.IsNull() && !other.internal_key.IsNull()) {
371 }
372 if (merkle_root.IsNull() && !other.merkle_root.IsNull()) {
373 merkle_root = other.merkle_root;
374 }
375 for (auto& [key, control_blocks] : other.scripts) {
376 scripts[key].merge(std::move(control_blocks));
377 }
378}
379
381{
382 assert(depth >= 0 && (size_t)depth <= TAPROOT_CONTROL_MAX_NODE_COUNT);
383 /* We cannot insert a leaf at a lower depth while a deeper branch is unfinished. Doing
384 * so would mean the Add() invocations do not correspond to a DFS traversal of a
385 * binary tree. */
386 if ((size_t)depth + 1 < m_branch.size()) {
387 m_valid = false;
388 return;
389 }
390 /* As long as an entry in the branch exists at the specified depth, combine it and propagate up.
391 * The 'node' variable is overwritten here with the newly combined node. */
392 while (m_valid && m_branch.size() > (size_t)depth && m_branch[depth].has_value()) {
393 node = Combine(std::move(node), std::move(*m_branch[depth]));
394 m_branch.pop_back();
395 if (depth == 0) m_valid = false; /* Can't propagate further up than the root */
396 --depth;
397 }
398 if (m_valid) {
399 /* Make sure the branch is big enough to place the new node. */
400 if (m_branch.size() <= (size_t)depth) m_branch.resize((size_t)depth + 1);
401 assert(!m_branch[depth].has_value());
402 m_branch[depth] = std::move(node);
403 }
404}
405
406/*static*/ bool TaprootBuilder::ValidDepths(const std::vector<int>& depths)
407{
408 std::vector<bool> branch;
409 for (int depth : depths) {
410 // This inner loop corresponds to effectively the same logic on branch
411 // as what Insert() performs on the m_branch variable. Instead of
412 // storing a NodeInfo object, just remember whether or not there is one
413 // at that depth.
414 if (depth < 0 || (size_t)depth > TAPROOT_CONTROL_MAX_NODE_COUNT) return false;
415 if ((size_t)depth + 1 < branch.size()) return false;
416 while (branch.size() > (size_t)depth && branch[depth]) {
417 branch.pop_back();
418 if (depth == 0) return false;
419 --depth;
420 }
421 if (branch.size() <= (size_t)depth) branch.resize((size_t)depth + 1);
422 assert(!branch[depth]);
423 branch[depth] = true;
424 }
425 // And this check corresponds to the IsComplete() check on m_branch.
426 return branch.size() == 0 || (branch.size() == 1 && branch[0]);
427}
428
429TaprootBuilder& TaprootBuilder::Add(int depth, std::span<const unsigned char> script, int leaf_version, bool track)
430{
431 assert((leaf_version & ~TAPROOT_LEAF_MASK) == 0);
432 if (!IsValid()) return *this;
433 /* Construct NodeInfo object with leaf hash and (if track is true) also leaf information. */
435 node.hash = ComputeTapleafHash(leaf_version, script);
436 if (track) node.leaves.emplace_back(LeafInfo{std::vector<unsigned char>(script.begin(), script.end()), leaf_version, {}});
437 /* Insert into the branch. */
438 Insert(std::move(node), depth);
439 return *this;
440}
441
443{
444 if (!IsValid()) return *this;
445 /* Construct NodeInfo object with the hash directly, and insert it into the branch. */
447 node.hash = hash;
448 Insert(std::move(node), depth);
449 return *this;
450}
451
453{
454 /* Can only call this function when IsComplete() is true. */
456 m_internal_key = internal_key;
457 auto ret = m_internal_key.CreateTapTweak(m_branch.size() == 0 ? nullptr : &m_branch[0]->hash);
458 assert(ret.has_value());
459 std::tie(m_output_key, m_parity) = *ret;
460 return *this;
461}
462
464
466{
470 spd.merkle_root = m_branch.size() == 0 ? uint256() : m_branch[0]->hash;
471 spd.internal_key = m_internal_key;
472 if (m_branch.size()) {
473 // If any script paths exist, they have been combined into the root m_branch[0]
474 // by now. Compute the control block for each of its tracked leaves, and put them in
475 // spd.scripts.
476 for (const auto& leaf : m_branch[0]->leaves) {
477 std::vector<unsigned char> control_block;
479 control_block[0] = leaf.leaf_version | (m_parity ? 1 : 0);
480 std::copy(m_internal_key.begin(), m_internal_key.end(), control_block.begin() + 1);
481 if (leaf.merkle_branch.size()) {
482 std::copy(leaf.merkle_branch[0].begin(),
483 leaf.merkle_branch[0].begin() + TAPROOT_CONTROL_NODE_SIZE * leaf.merkle_branch.size(),
485 }
486 spd.scripts[{leaf.script, leaf.leaf_version}].insert(std::move(control_block));
487 }
488 }
489 return spd;
490}
491
492std::optional<std::vector<std::tuple<int, std::vector<unsigned char>, int>>> InferTaprootTree(const TaprootSpendData& spenddata, const XOnlyPubKey& output)
493{
494 // Verify that the output matches the assumed Merkle root and internal key.
495 auto tweak = spenddata.internal_key.CreateTapTweak(spenddata.merkle_root.IsNull() ? nullptr : &spenddata.merkle_root);
496 if (!tweak || tweak->first != output) return std::nullopt;
497 // If the Merkle root is 0, the tree is empty, and we're done.
498 std::vector<std::tuple<int, std::vector<unsigned char>, int>> ret;
499 if (spenddata.merkle_root.IsNull()) return ret;
500
502 struct TreeNode {
504 uint256 hash;
506 std::unique_ptr<TreeNode> sub[2];
509 const std::pair<std::vector<unsigned char>, int>* leaf = nullptr;
511 bool explored = false;
513 bool inner;
515 bool done = false;
516 };
517
518 // Build tree from the provided branches.
519 TreeNode root;
520 root.hash = spenddata.merkle_root;
521 for (const auto& [key, control_blocks] : spenddata.scripts) {
522 const auto& [script, leaf_ver] = key;
523 for (const auto& control : control_blocks) {
524 // Skip script records with nonsensical leaf version.
525 if (leaf_ver < 0 || leaf_ver >= 0x100 || leaf_ver & 1) continue;
526 // Skip script records with invalid control block sizes.
528 ((control.size() - TAPROOT_CONTROL_BASE_SIZE) % TAPROOT_CONTROL_NODE_SIZE) != 0) continue;
529 // Skip script records that don't match the control block.
530 if ((control[0] & TAPROOT_LEAF_MASK) != leaf_ver) continue;
531 // Skip script records that don't match the provided Merkle root.
534 if (merkle_root != spenddata.merkle_root) continue;
535
536 TreeNode* node = &root;
538 for (size_t depth = 0; depth < levels; ++depth) {
539 // Can't descend into a node which we already know is a leaf.
540 if (node->explored && !node->inner) return std::nullopt;
541
542 // Extract partner hash from Merkle branch in control block.
543 uint256 hash;
544 std::copy(control.begin() + TAPROOT_CONTROL_BASE_SIZE + (levels - 1 - depth) * TAPROOT_CONTROL_NODE_SIZE,
546 hash.begin());
547
548 if (node->sub[0]) {
549 // Descend into the existing left or right branch.
550 bool desc = false;
551 for (int i = 0; i < 2; ++i) {
552 if (node->sub[i]->hash == hash || (node->sub[i]->hash.IsNull() && node->sub[1-i]->hash != hash)) {
553 node->sub[i]->hash = hash;
554 node = &*node->sub[1-i];
555 desc = true;
556 break;
557 }
558 }
559 if (!desc) return std::nullopt; // This probably requires a hash collision to hit.
560 } else {
561 // We're in an unexplored node. Create subtrees and descend.
562 node->explored = true;
563 node->inner = true;
564 node->sub[0] = std::make_unique<TreeNode>();
565 node->sub[1] = std::make_unique<TreeNode>();
566 node->sub[1]->hash = hash;
567 node = &*node->sub[0];
568 }
569 }
570 // Cannot turn a known inner node into a leaf.
571 if (node->sub[0]) return std::nullopt;
572 node->explored = true;
573 node->inner = false;
574 node->leaf = &key;
575 node->hash = leaf_hash;
576 }
577 }
578
579 // Recursive processing to turn the tree into flattened output. Use an explicit stack here to avoid
580 // overflowing the call stack (the tree may be 128 levels deep).
581 std::vector<TreeNode*> stack{&root};
582 while (!stack.empty()) {
583 TreeNode& node = *stack.back();
584 if (!node.explored) {
585 // Unexplored node, which means the tree is incomplete.
586 return std::nullopt;
587 } else if (!node.inner) {
588 // Leaf node; produce output.
589 ret.emplace_back(stack.size() - 1, node.leaf->first, node.leaf->second);
590 node.done = true;
591 stack.pop_back();
592 } else if (node.sub[0]->done && !node.sub[1]->done && !node.sub[1]->explored && !node.sub[1]->hash.IsNull() &&
593 ComputeTapbranchHash(node.sub[1]->hash, node.sub[1]->hash) == node.hash) {
594 // Whenever there are nodes with two identical subtrees under it, we run into a problem:
595 // the control blocks for the leaves underneath those will be identical as well, and thus
596 // they will all be matched to the same path in the tree. The result is that at the location
597 // where the duplicate occurred, the left child will contain a normal tree that can be explored
598 // and processed, but the right one will remain unexplored.
599 //
600 // This situation can be detected, by encountering an inner node with unexplored right subtree
601 // with known hash, and H_TapBranch(hash, hash) is equal to the parent node (this node)'s hash.
602 //
603 // To deal with this, simply process the left tree a second time (set its done flag to false;
604 // noting that the done flag of its children have already been set to false after processing
605 // those). To avoid ending up in an infinite loop, set the done flag of the right (unexplored)
606 // subtree to true.
607 node.sub[0]->done = false;
608 node.sub[1]->done = true;
609 } else if (node.sub[0]->done && node.sub[1]->done) {
610 // An internal node which we're finished with.
611 node.sub[0]->done = false;
612 node.sub[1]->done = false;
613 node.done = true;
614 stack.pop_back();
615 } else if (!node.sub[0]->done) {
616 // An internal node whose left branch hasn't been processed yet. Do so first.
617 stack.push_back(&*node.sub[0]);
618 } else if (!node.sub[1]->done) {
619 // An internal node whose right branch hasn't been processed yet. Do so first.
620 stack.push_back(&*node.sub[1]);
621 }
622 }
623
624 return ret;
625}
626
627std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>> TaprootBuilder::GetTreeTuples() const
628{
630 std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>> tuples;
631 if (m_branch.size()) {
632 const auto& leaves = m_branch[0]->leaves;
633 for (const auto& leaf : leaves) {
634 assert(leaf.merkle_branch.size() <= TAPROOT_CONTROL_MAX_NODE_COUNT);
635 uint8_t depth = (uint8_t)leaf.merkle_branch.size();
636 uint8_t leaf_ver = (uint8_t)leaf.leaf_version;
637 tuples.emplace_back(depth, leaf_ver, leaf.script);
638 }
639 }
640 return tuples;
641}
CScriptID ToScriptID(const ScriptHash &script_hash)
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
CKeyID ToKeyID(const PKHash &key_hash)
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
int ret
#define Assert(val)
Identity function.
Definition check.h:119
#define Assume(val)
Assume is the identity function.
Definition check.h:131
An encapsulated private key.
Definition key.h:46
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:29
An encapsulated public key.
Definition pubkey.h:43
bool IsCompressed() const
Check whether this is a compressed public key.
Definition pubkey.h:209
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition pubkey.h:169
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
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)
void ImplicitlyLearnRelatedKeyScripts(const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
virtual std::set< CKeyID > GetKeys() const
virtual bool HaveCScript(const CScriptID &hash) const override
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
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
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 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 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
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.
static bool ValidDepths(const std::vector< int > &depths)
Check if a list of depths is legal (will lead to IsComplete()).
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.
const unsigned char * end() const
Definition pubkey.h:305
bool IsNull() const
Test whether this is the 0 key (the result of default construction).
Definition pubkey.h:259
const unsigned char * begin() const
Definition pubkey.h:304
std::optional< std::pair< XOnlyPubKey, bool > > CreateTapTweak(const uint256 *merkle_root) const
Construct a Taproot tweaked output point with this point as internal key.
Definition pubkey.cpp:265
bool IsFullyValid() const
Determine if this pubkey is fully valid.
Definition pubkey.cpp:230
constexpr bool IsNull() const
Definition uint256.h:50
static constexpr unsigned int size()
Definition uint256.h:107
constexpr unsigned char * begin()
Definition uint256.h:101
256-bit opaque blob.
Definition uint256.h:196
uint256 ComputeTapbranchHash(std::span< const unsigned char > a, std::span< const unsigned char > b)
Compute the BIP341 tapbranch hash from two branches.
uint256 ComputeTaprootMerkleRoot(std::span< const unsigned char > control, const uint256 &tapleaf_hash)
Compute the BIP341 taproot script tree Merkle root from control block and leaf hash.
uint256 ComputeTapleafHash(uint8_t leaf_version, std::span< const unsigned char > script)
Compute the BIP341 tapleaf hash from leaf version & script.
constexpr uint8_t TAPROOT_LEAF_MASK
constexpr size_t TAPROOT_CONTROL_NODE_SIZE
constexpr size_t TAPROOT_CONTROL_MAX_NODE_COUNT
constexpr size_t TAPROOT_CONTROL_MAX_SIZE
constexpr size_t TAPROOT_CONTROL_BASE_SIZE
#define LogInfo(...)
Definition log.h:136
unsigned int nonce
static int tweak(const secp256k1_context *ctx, secp256k1_xonly_pubkey *agg_pk, secp256k1_musig_keyagg_cache *cache)
Definition musig.c:64
constexpr unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition script.h:29
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
bool LookupHelper(const M &map, const K &key, V &value)
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
Information about a tracked leaf in the Merkle tree.
Information associated with a node in the Merkle tree.
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.
#define LOCK(cs)
Definition sync.h:268
#define AssertLockHeld(cs)
Definition sync.h:144
FuzzedDataProvider provider
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())