Bitcoin Core 28.99.0
P2P Digital Currency
signingprovider.cpp
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#include <script/keyorigin.h>
9
10#include <logging.h>
11
13
14template<typename M, typename K, typename V>
15bool LookupHelper(const M& map, const K& key, V& value)
16{
17 auto it = map.find(key);
18 if (it != map.end()) {
19 value = it->second;
20 return true;
21 }
22 return false;
23}
24
26{
27 return m_provider->GetCScript(scriptid, script);
28}
29
30bool HidingSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const
31{
32 return m_provider->GetPubKey(keyid, pubkey);
33}
34
35bool HidingSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const
36{
37 if (m_hide_secret) return false;
38 return m_provider->GetKey(keyid, key);
39}
40
42{
43 if (m_hide_origin) return false;
44 return m_provider->GetKeyOrigin(keyid, info);
45}
46
48{
49 return m_provider->GetTaprootSpendData(output_key, spenddata);
50}
52{
53 return m_provider->GetTaprootBuilder(output_key, builder);
54}
55
56bool FlatSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const { return LookupHelper(scripts, scriptid, script); }
57bool FlatSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const { return LookupHelper(pubkeys, keyid, pubkey); }
59{
60 std::pair<CPubKey, KeyOriginInfo> out;
61 bool ret = LookupHelper(origins, keyid, out);
62 if (ret) info = std::move(out.second);
63 return ret;
64}
65bool FlatSigningProvider::HaveKey(const CKeyID &keyid) const
66{
67 CKey key;
68 return LookupHelper(keys, keyid, key);
69}
70bool FlatSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const { return LookupHelper(keys, keyid, key); }
72{
73 TaprootBuilder builder;
74 if (LookupHelper(tr_trees, output_key, builder)) {
75 spenddata = builder.GetSpendData();
76 return true;
77 }
78 return false;
79}
81{
82 return LookupHelper(tr_trees, output_key, builder);
83}
84
86{
87 scripts.merge(b.scripts);
88 pubkeys.merge(b.pubkeys);
89 keys.merge(b.keys);
90 origins.merge(b.origins);
91 tr_trees.merge(b.tr_trees);
92 return *this;
93}
94
96{
98 CKeyID key_id = pubkey.GetID();
99 // This adds the redeemscripts necessary to detect P2WPKH and P2SH-P2WPKH
100 // outputs. Technically P2WPKH outputs don't have a redeemscript to be
101 // spent. However, our current IsMine logic requires the corresponding
102 // P2SH-P2WPKH redeemscript to be present in the wallet in order to accept
103 // payment even to P2WPKH outputs.
104 // Also note that having superfluous scripts in the keystore never hurts.
105 // They're only used to guide recursion in signing and IsMine logic - if
106 // a script is present but we can't do anything with it, it has no effect.
107 // "Implicitly" refers to fact that scripts are derived automatically from
108 // existing keys, and are present in memory, even without being explicitly
109 // loaded (e.g. from a file).
110 if (pubkey.IsCompressed()) {
112 // This does not use AddCScript, as it may be overridden.
113 CScriptID id(script);
114 mapScripts[id] = std::move(script);
115 }
116}
117
118bool FillableSigningProvider::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
119{
120 CKey key;
121 if (!GetKey(address, key)) {
122 return false;
123 }
124 vchPubKeyOut = key.GetPubKey();
125 return true;
126}
127
129{
131 mapKeys[pubkey.GetID()] = key;
133 return true;
134}
135
136bool FillableSigningProvider::HaveKey(const CKeyID &address) const
137{
139 return mapKeys.count(address) > 0;
140}
141
142std::set<CKeyID> FillableSigningProvider::GetKeys() const
143{
145 std::set<CKeyID> set_address;
146 for (const auto& mi : mapKeys) {
147 set_address.insert(mi.first);
148 }
149 return set_address;
150}
151
152bool FillableSigningProvider::GetKey(const CKeyID &address, CKey &keyOut) const
153{
155 KeyMap::const_iterator mi = mapKeys.find(address);
156 if (mi != mapKeys.end()) {
157 keyOut = mi->second;
158 return true;
159 }
160 return false;
161}
162
164{
165 if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE) {
166 LogError("FillableSigningProvider::AddCScript(): redeemScripts > %i bytes are invalid\n", MAX_SCRIPT_ELEMENT_SIZE);
167 return false;
168 }
169
171 mapScripts[CScriptID(redeemScript)] = redeemScript;
172 return true;
173}
174
176{
178 return mapScripts.count(hash) > 0;
179}
180
181std::set<CScriptID> FillableSigningProvider::GetCScripts() const
182{
184 std::set<CScriptID> set_script;
185 for (const auto& mi : mapScripts) {
186 set_script.insert(mi.first);
187 }
188 return set_script;
189}
190
191bool FillableSigningProvider::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
192{
194 ScriptMap::const_iterator mi = mapScripts.find(hash);
195 if (mi != mapScripts.end())
196 {
197 redeemScriptOut = (*mi).second;
198 return true;
199 }
200 return false;
201}
202
204{
205 // Only supports destinations which map to single public keys:
206 // P2PKH, P2WPKH, P2SH-P2WPKH, P2TR
207 if (auto id = std::get_if<PKHash>(&dest)) {
208 return ToKeyID(*id);
209 }
210 if (auto witness_id = std::get_if<WitnessV0KeyHash>(&dest)) {
211 return ToKeyID(*witness_id);
212 }
213 if (auto script_hash = std::get_if<ScriptHash>(&dest)) {
215 CScriptID script_id = ToScriptID(*script_hash);
216 CTxDestination inner_dest;
217 if (store.GetCScript(script_id, script) && ExtractDestination(script, inner_dest)) {
218 if (auto inner_witness_id = std::get_if<WitnessV0KeyHash>(&inner_dest)) {
219 return ToKeyID(*inner_witness_id);
220 }
221 }
222 }
223 if (auto output_key = std::get_if<WitnessV1Taproot>(&dest)) {
224 TaprootSpendData spenddata;
225 CPubKey pub;
226 if (store.GetTaprootSpendData(*output_key, spenddata)
227 && !spenddata.internal_key.IsNull()
228 && spenddata.merkle_root.IsNull()
229 && store.GetPubKeyByXOnly(spenddata.internal_key, pub)) {
230 return pub.GetID();
231 }
232 }
233 return CKeyID();
234}
235
236void MultiSigningProvider::AddProvider(std::unique_ptr<SigningProvider> provider)
237{
238 m_providers.push_back(std::move(provider));
239}
240
242{
243 for (const auto& provider: m_providers) {
244 if (provider->GetCScript(scriptid, script)) return true;
245 }
246 return false;
247}
248
249bool MultiSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const
250{
251 for (const auto& provider: m_providers) {
252 if (provider->GetPubKey(keyid, pubkey)) return true;
253 }
254 return false;
255}
256
257
259{
260 for (const auto& provider: m_providers) {
261 if (provider->GetKeyOrigin(keyid, info)) return true;
262 }
263 return false;
264}
265
266bool MultiSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const
267{
268 for (const auto& provider: m_providers) {
269 if (provider->GetKey(keyid, key)) return true;
270 }
271 return false;
272}
273
275{
276 for (const auto& provider: m_providers) {
277 if (provider->GetTaprootSpendData(output_key, spenddata)) return true;
278 }
279 return false;
280}
281
283{
284 for (const auto& provider: m_providers) {
285 if (provider->GetTaprootBuilder(output_key, builder)) return true;
286 }
287 return false;
288}
289
291{
293 /* Iterate over all tracked leaves in a, add b's hash to their Merkle branch, and move them to ret. */
294 for (auto& leaf : a.leaves) {
295 leaf.merkle_branch.push_back(b.hash);
296 ret.leaves.emplace_back(std::move(leaf));
297 }
298 /* Iterate over all tracked leaves in b, add a's hash to their Merkle branch, and move them to ret. */
299 for (auto& leaf : b.leaves) {
300 leaf.merkle_branch.push_back(a.hash);
301 ret.leaves.emplace_back(std::move(leaf));
302 }
303 ret.hash = ComputeTapbranchHash(a.hash, b.hash);
304 return ret;
305}
306
308{
309 // TODO: figure out how to better deal with conflicting information
310 // being merged.
311 if (internal_key.IsNull() && !other.internal_key.IsNull()) {
313 }
314 if (merkle_root.IsNull() && !other.merkle_root.IsNull()) {
315 merkle_root = other.merkle_root;
316 }
317 for (auto& [key, control_blocks] : other.scripts) {
318 scripts[key].merge(std::move(control_blocks));
319 }
320}
321
323{
324 assert(depth >= 0 && (size_t)depth <= TAPROOT_CONTROL_MAX_NODE_COUNT);
325 /* We cannot insert a leaf at a lower depth while a deeper branch is unfinished. Doing
326 * so would mean the Add() invocations do not correspond to a DFS traversal of a
327 * binary tree. */
328 if ((size_t)depth + 1 < m_branch.size()) {
329 m_valid = false;
330 return;
331 }
332 /* As long as an entry in the branch exists at the specified depth, combine it and propagate up.
333 * The 'node' variable is overwritten here with the newly combined node. */
334 while (m_valid && m_branch.size() > (size_t)depth && m_branch[depth].has_value()) {
335 node = Combine(std::move(node), std::move(*m_branch[depth]));
336 m_branch.pop_back();
337 if (depth == 0) m_valid = false; /* Can't propagate further up than the root */
338 --depth;
339 }
340 if (m_valid) {
341 /* Make sure the branch is big enough to place the new node. */
342 if (m_branch.size() <= (size_t)depth) m_branch.resize((size_t)depth + 1);
343 assert(!m_branch[depth].has_value());
344 m_branch[depth] = std::move(node);
345 }
346}
347
348/*static*/ bool TaprootBuilder::ValidDepths(const std::vector<int>& depths)
349{
350 std::vector<bool> branch;
351 for (int depth : depths) {
352 // This inner loop corresponds to effectively the same logic on branch
353 // as what Insert() performs on the m_branch variable. Instead of
354 // storing a NodeInfo object, just remember whether or not there is one
355 // at that depth.
356 if (depth < 0 || (size_t)depth > TAPROOT_CONTROL_MAX_NODE_COUNT) return false;
357 if ((size_t)depth + 1 < branch.size()) return false;
358 while (branch.size() > (size_t)depth && branch[depth]) {
359 branch.pop_back();
360 if (depth == 0) return false;
361 --depth;
362 }
363 if (branch.size() <= (size_t)depth) branch.resize((size_t)depth + 1);
364 assert(!branch[depth]);
365 branch[depth] = true;
366 }
367 // And this check corresponds to the IsComplete() check on m_branch.
368 return branch.size() == 0 || (branch.size() == 1 && branch[0]);
369}
370
371TaprootBuilder& TaprootBuilder::Add(int depth, Span<const unsigned char> script, int leaf_version, bool track)
372{
373 assert((leaf_version & ~TAPROOT_LEAF_MASK) == 0);
374 if (!IsValid()) return *this;
375 /* Construct NodeInfo object with leaf hash and (if track is true) also leaf information. */
377 node.hash = ComputeTapleafHash(leaf_version, script);
378 if (track) node.leaves.emplace_back(LeafInfo{std::vector<unsigned char>(script.begin(), script.end()), leaf_version, {}});
379 /* Insert into the branch. */
380 Insert(std::move(node), depth);
381 return *this;
382}
383
385{
386 if (!IsValid()) return *this;
387 /* Construct NodeInfo object with the hash directly, and insert it into the branch. */
389 node.hash = hash;
390 Insert(std::move(node), depth);
391 return *this;
392}
393
395{
396 /* Can only call this function when IsComplete() is true. */
398 m_internal_key = internal_key;
399 auto ret = m_internal_key.CreateTapTweak(m_branch.size() == 0 ? nullptr : &m_branch[0]->hash);
400 assert(ret.has_value());
401 std::tie(m_output_key, m_parity) = *ret;
402 return *this;
403}
404
406
408{
412 spd.merkle_root = m_branch.size() == 0 ? uint256() : m_branch[0]->hash;
414 if (m_branch.size()) {
415 // If any script paths exist, they have been combined into the root m_branch[0]
416 // by now. Compute the control block for each of its tracked leaves, and put them in
417 // spd.scripts.
418 for (const auto& leaf : m_branch[0]->leaves) {
419 std::vector<unsigned char> control_block;
420 control_block.resize(TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * leaf.merkle_branch.size());
421 control_block[0] = leaf.leaf_version | (m_parity ? 1 : 0);
422 std::copy(m_internal_key.begin(), m_internal_key.end(), control_block.begin() + 1);
423 if (leaf.merkle_branch.size()) {
424 std::copy(leaf.merkle_branch[0].begin(),
425 leaf.merkle_branch[0].begin() + TAPROOT_CONTROL_NODE_SIZE * leaf.merkle_branch.size(),
426 control_block.begin() + TAPROOT_CONTROL_BASE_SIZE);
427 }
428 spd.scripts[{leaf.script, leaf.leaf_version}].insert(std::move(control_block));
429 }
430 }
431 return spd;
432}
433
434std::optional<std::vector<std::tuple<int, std::vector<unsigned char>, int>>> InferTaprootTree(const TaprootSpendData& spenddata, const XOnlyPubKey& output)
435{
436 // Verify that the output matches the assumed Merkle root and internal key.
437 auto tweak = spenddata.internal_key.CreateTapTweak(spenddata.merkle_root.IsNull() ? nullptr : &spenddata.merkle_root);
438 if (!tweak || tweak->first != output) return std::nullopt;
439 // If the Merkle root is 0, the tree is empty, and we're done.
440 std::vector<std::tuple<int, std::vector<unsigned char>, int>> ret;
441 if (spenddata.merkle_root.IsNull()) return ret;
442
444 struct TreeNode {
446 uint256 hash;
448 std::unique_ptr<TreeNode> sub[2];
451 const std::pair<std::vector<unsigned char>, int>* leaf = nullptr;
453 bool explored = false;
455 bool inner;
457 bool done = false;
458 };
459
460 // Build tree from the provided branches.
461 TreeNode root;
462 root.hash = spenddata.merkle_root;
463 for (const auto& [key, control_blocks] : spenddata.scripts) {
464 const auto& [script, leaf_ver] = key;
465 for (const auto& control : control_blocks) {
466 // Skip script records with nonsensical leaf version.
467 if (leaf_ver < 0 || leaf_ver >= 0x100 || leaf_ver & 1) continue;
468 // Skip script records with invalid control block sizes.
469 if (control.size() < TAPROOT_CONTROL_BASE_SIZE || control.size() > TAPROOT_CONTROL_MAX_SIZE ||
470 ((control.size() - TAPROOT_CONTROL_BASE_SIZE) % TAPROOT_CONTROL_NODE_SIZE) != 0) continue;
471 // Skip script records that don't match the control block.
472 if ((control[0] & TAPROOT_LEAF_MASK) != leaf_ver) continue;
473 // Skip script records that don't match the provided Merkle root.
474 const uint256 leaf_hash = ComputeTapleafHash(leaf_ver, script);
475 const uint256 merkle_root = ComputeTaprootMerkleRoot(control, leaf_hash);
476 if (merkle_root != spenddata.merkle_root) continue;
477
478 TreeNode* node = &root;
479 size_t levels = (control.size() - TAPROOT_CONTROL_BASE_SIZE) / TAPROOT_CONTROL_NODE_SIZE;
480 for (size_t depth = 0; depth < levels; ++depth) {
481 // Can't descend into a node which we already know is a leaf.
482 if (node->explored && !node->inner) return std::nullopt;
483
484 // Extract partner hash from Merkle branch in control block.
485 uint256 hash;
486 std::copy(control.begin() + TAPROOT_CONTROL_BASE_SIZE + (levels - 1 - depth) * TAPROOT_CONTROL_NODE_SIZE,
487 control.begin() + TAPROOT_CONTROL_BASE_SIZE + (levels - depth) * TAPROOT_CONTROL_NODE_SIZE,
488 hash.begin());
489
490 if (node->sub[0]) {
491 // Descend into the existing left or right branch.
492 bool desc = false;
493 for (int i = 0; i < 2; ++i) {
494 if (node->sub[i]->hash == hash || (node->sub[i]->hash.IsNull() && node->sub[1-i]->hash != hash)) {
495 node->sub[i]->hash = hash;
496 node = &*node->sub[1-i];
497 desc = true;
498 break;
499 }
500 }
501 if (!desc) return std::nullopt; // This probably requires a hash collision to hit.
502 } else {
503 // We're in an unexplored node. Create subtrees and descend.
504 node->explored = true;
505 node->inner = true;
506 node->sub[0] = std::make_unique<TreeNode>();
507 node->sub[1] = std::make_unique<TreeNode>();
508 node->sub[1]->hash = hash;
509 node = &*node->sub[0];
510 }
511 }
512 // Cannot turn a known inner node into a leaf.
513 if (node->sub[0]) return std::nullopt;
514 node->explored = true;
515 node->inner = false;
516 node->leaf = &key;
517 node->hash = leaf_hash;
518 }
519 }
520
521 // Recursive processing to turn the tree into flattened output. Use an explicit stack here to avoid
522 // overflowing the call stack (the tree may be 128 levels deep).
523 std::vector<TreeNode*> stack{&root};
524 while (!stack.empty()) {
525 TreeNode& node = *stack.back();
526 if (!node.explored) {
527 // Unexplored node, which means the tree is incomplete.
528 return std::nullopt;
529 } else if (!node.inner) {
530 // Leaf node; produce output.
531 ret.emplace_back(stack.size() - 1, node.leaf->first, node.leaf->second);
532 node.done = true;
533 stack.pop_back();
534 } else if (node.sub[0]->done && !node.sub[1]->done && !node.sub[1]->explored && !node.sub[1]->hash.IsNull() &&
535 ComputeTapbranchHash(node.sub[1]->hash, node.sub[1]->hash) == node.hash) {
536 // Whenever there are nodes with two identical subtrees under it, we run into a problem:
537 // the control blocks for the leaves underneath those will be identical as well, and thus
538 // they will all be matched to the same path in the tree. The result is that at the location
539 // where the duplicate occurred, the left child will contain a normal tree that can be explored
540 // and processed, but the right one will remain unexplored.
541 //
542 // This situation can be detected, by encountering an inner node with unexplored right subtree
543 // with known hash, and H_TapBranch(hash, hash) is equal to the parent node (this node)'s hash.
544 //
545 // To deal with this, simply process the left tree a second time (set its done flag to false;
546 // noting that the done flag of its children have already been set to false after processing
547 // those). To avoid ending up in an infinite loop, set the done flag of the right (unexplored)
548 // subtree to true.
549 node.sub[0]->done = false;
550 node.sub[1]->done = true;
551 } else if (node.sub[0]->done && node.sub[1]->done) {
552 // An internal node which we're finished with.
553 node.sub[0]->done = false;
554 node.sub[1]->done = false;
555 node.done = true;
556 stack.pop_back();
557 } else if (!node.sub[0]->done) {
558 // An internal node whose left branch hasn't been processed yet. Do so first.
559 stack.push_back(&*node.sub[0]);
560 } else if (!node.sub[1]->done) {
561 // An internal node whose right branch hasn't been processed yet. Do so first.
562 stack.push_back(&*node.sub[1]);
563 }
564 }
565
566 return ret;
567}
568
569std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>> TaprootBuilder::GetTreeTuples() const
570{
572 std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>> tuples;
573 if (m_branch.size()) {
574 const auto& leaves = m_branch[0]->leaves;
575 for (const auto& leaf : leaves) {
576 assert(leaf.merkle_branch.size() <= TAPROOT_CONTROL_MAX_NODE_COUNT);
577 uint8_t depth = (uint8_t)leaf.merkle_branch.size();
578 uint8_t leaf_ver = (uint8_t)leaf.leaf_version;
579 tuples.emplace_back(depth, leaf_ver, leaf.script);
580 }
581 }
582 return tuples;
583}
CScriptID ToScriptID(const ScriptHash &script_hash)
Definition: addresstype.cpp:39
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
Definition: addresstype.cpp:49
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
CKeyID ToKeyID(const PKHash &key_hash)
Definition: addresstype.cpp:29
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:140
int ret
An encapsulated private key.
Definition: key.h:35
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
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:204
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:164
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
A reference to a CScript: the Hash160 of its serialization.
Definition: script.h:602
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 std::set< CScriptID > GetCScripts() const
virtual bool HaveCScript(const CScriptID &hash) const override
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
bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const override
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
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 GetCScript(const CScriptID &scriptid, CScript &script) const
virtual bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) 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.
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.
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.
TaprootBuilder & Add(int depth, Span< const unsigned char > script, int leaf_version, bool track=true)
Add a new script at a certain depth in the tree.
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:296
bool IsNull() const
Test whether this is the 0 key (the result of default construction).
Definition: pubkey.h:254
const unsigned char * begin() const
Definition: pubkey.h:295
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:259
bool IsFullyValid() const
Determine if this pubkey is fully valid.
Definition: pubkey.cpp:224
constexpr bool IsNull() const
Definition: uint256.h:48
constexpr unsigned char * begin()
Definition: uint256.h:115
size_type size() const
Definition: prevector.h:294
256-bit opaque blob.
Definition: uint256.h:201
uint256 ComputeTaprootMerkleRoot(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, Span< const unsigned char > script)
Compute the BIP341 tapleaf hash from leaf version & script.
uint256 ComputeTapbranchHash(Span< const unsigned char > a, Span< const unsigned char > b)
Compute the BIP341 tapbranch hash from two branches.
static constexpr uint8_t TAPROOT_LEAF_MASK
Definition: interpreter.h:231
static constexpr size_t TAPROOT_CONTROL_NODE_SIZE
Definition: interpreter.h:234
static constexpr size_t TAPROOT_CONTROL_MAX_NODE_COUNT
Definition: interpreter.h:235
static constexpr size_t TAPROOT_CONTROL_MAX_SIZE
Definition: interpreter.h:236
static constexpr size_t TAPROOT_CONTROL_BASE_SIZE
Definition: interpreter.h:233
#define LogError(...)
Definition: logging.h:263
static int tweak(const secp256k1_context *ctx, secp256k1_xonly_pubkey *agg_pk, secp256k1_musig_keyagg_cache *cache)
Definition: musig.c:63
Definition: messages.h:20
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:28
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).
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
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.
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:257
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())