1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
// SPDX-License-Identifier: CC0-1.0
//! Taproot Spending Information
//!
//! Provides a structure which can be used to obtain control blocks and other information
//! needed for Taproot spends.
//!
use bitcoin::key::{Parity, TapTweak as _, TweakedPublicKey, UntweakedPublicKey};
use bitcoin::secp256k1::Secp256k1;
use bitcoin::taproot::{ControlBlock, LeafVersion, TapLeafHash, TapNodeHash, TaprootMerkleBranch};
use bitcoin::{Script, ScriptBuf};
use crate::miniscript::context::Tap;
use crate::prelude::Vec;
use crate::sync::Arc;
use crate::{Miniscript, MiniscriptKey, ToPublicKey};
/// Utility structure which maintains a stack of bits (at most 128) using a u128.
///
/// Will panic if the user attempts to push more than 128 bits; we assume in this
/// module that we are starting with a validated [`super::TapTree`] and therefore
/// that this can't happen.
#[derive(Default)]
struct BitStack128 {
inner: u128,
height: u8,
}
impl BitStack128 {
fn push(&mut self, bit: bool) {
if bit {
self.inner |= 1u128 << self.height;
} else {
self.inner &= !(1u128 << self.height);
}
self.height += 1;
}
fn pop(&mut self) -> Option<bool> {
if self.height > 0 {
self.height -= 1;
Some(self.inner & (1u128 << self.height) != 0)
} else {
None
}
}
}
/// A structure which can be used to obtain control blocks and other information
/// needed for Taproot spends.
///
/// Conceptually, this object is a copy of the Taproot tree with each leave annotated
/// with extra information that can be used to compute its control block.
pub struct TrSpendInfo<Pk: MiniscriptKey> {
internal_key: UntweakedPublicKey,
output_key: TweakedPublicKey,
output_key_parity: Parity,
/// The nodes of the tree, in pre-order, i.e. left-to-right depth-first order.
nodes: Vec<TrSpendInfoNode<Pk>>,
}
impl<Pk: ToPublicKey> TrSpendInfo<Pk> {
fn nodes_from_tap_tree(tree: &super::TapTree<Pk>) -> Vec<TrSpendInfoNode<Pk>> {
let mut nodes = vec![];
let mut parent_stack = Vec::with_capacity(128); // FIXME use ArrayVec here
for leaf in tree.leaves() {
let depth = usize::from(leaf.depth());
let script = leaf.miniscript().encode();
let leaf_hash = TapLeafHash::from_script(&script, leaf.leaf_version());
let mut current_hash = TapNodeHash::from(leaf_hash);
// 1. If this node increases our depth, add parents.
while parent_stack.len() < depth {
// When we encounter a leaf we put all of its parent nodes into the
// result. We set the "sibling hash" to a dummy value (specifically,
// `current_hash`, because it's convenient and the right type).
parent_stack.push((false, nodes.len()));
nodes.push(TrSpendInfoNode { sibling_hash: current_hash, leaf_data: None });
}
// If parent_stack.len() < depth then we pushed things onto the stack in
// the previous step so that we now have equality. Meanwhile, it is
// impossible for parent_stack.len() > depth because we pop things off
// the stack in step 3 below.
assert_eq!(depth, parent_stack.len());
// 2. Add the node.
//
// Again, we don't know the sibling hash yet so we use the current hash.
// But this time the current hash isn't an arbitrary dummy value -- in the
// next step we will have an invariant that incomplete nodes' "sibling hashes"
// are set to the nodes' own hashes.
//
// We will use this hash to compute the parent's hash then replace it with
// the actual sibling hash. We do this for every node EXCEPT the root node,
// whose "sibling hash" will then wind up being equal to the Merkle root
// of the whole tree.
nodes.push(TrSpendInfoNode {
sibling_hash: current_hash,
leaf_data: Some(LeafData {
script,
miniscript: Arc::clone(leaf.miniscript()),
leaf_hash,
}),
});
// 3. Recursively complete nodes as long as we are on a right branch.
//
// As described above, for each parent node, we compute its hash and store it
// in `sibling_hash`. At that point we're done with the childrens' hashes so
// we finally replace those with their sibling hashes.
let mut cur_index = nodes.len() - 1;
while let Some((done_left_child, parent_idx)) = parent_stack.pop() {
if done_left_child {
let lchild_hash = nodes[parent_idx + 1].sibling_hash;
// Set current node's "sibling hash" to its own hash.
let new_merkle_root = TapNodeHash::from_node_hashes(lchild_hash, current_hash);
nodes[parent_idx].sibling_hash = new_merkle_root;
// Set the children's sibling hashes to each others' hashes.
nodes[parent_idx + 1].sibling_hash = current_hash;
nodes[cur_index].sibling_hash = lchild_hash;
// Recurse.
current_hash = new_merkle_root;
cur_index = parent_idx;
} else {
// Once we hit a left branch we can't do anything until we see the next leaf.
parent_stack.push((true, parent_idx));
break;
}
}
}
debug_assert_eq!(parent_stack.len(), 0);
debug_assert_ne!(nodes.len(), 0);
nodes
}
/// Constructs a [`TrSpendInfo`] for a [`super::Tr`].
pub fn from_tr(tr: &super::Tr<Pk>) -> Self {
let internal_key = tr.internal_key().to_x_only_pubkey();
let nodes = match tr.tap_tree() {
Some(tree) => Self::nodes_from_tap_tree(tree),
None => vec![],
};
let secp = Secp256k1::verification_only();
let (output_key, output_key_parity) =
internal_key.tap_tweak(&secp, nodes.first().map(|node| node.sibling_hash));
TrSpendInfo { internal_key, output_key, output_key_parity, nodes }
}
/// If this [`TrSpendInfo`] has an associated Taproot tree, return its Merkle root.
pub fn merkle_root(&self) -> Option<TapNodeHash> {
// As described in `nodes_from_tap_tree`, the "sibling hash" of the root node
// is actually the Merkle root of the whole tree.
self.nodes.first().map(|node| node.sibling_hash)
}
/// The internal key of the Taproot output.
///
/// This returns the x-only public key which appears on-chain. For the abstroct
/// public key, use the `internal_key` method on the original [`super::Tr`] used to
/// create this object.
pub fn internal_key(&self) -> UntweakedPublicKey { self.internal_key }
// I don't really like these names, but they're used in rust-bitcoin so we'll stick
// with them and just doc-alias them to better names so they show up in search results.
/// The external key of the Taproot output.
#[doc(alias = "external_key")]
pub fn output_key(&self) -> TweakedPublicKey { self.output_key }
/// The parity of the external key of the Taproot output.
#[doc(alias = "external_key_parity")]
pub fn output_key_parity(&self) -> Parity { self.output_key_parity }
/// An iterator over the leaves of the Taptree.
///
/// This yields the same leaves in the same order as [`super::Tr::leaves`] on the original
/// [`super::Tr`]. However, in addition to yielding the leaves and their depths, it also
/// yields their scripts, leafhashes, and control blocks.
pub fn leaves(&self) -> TrSpendInfoIter<'_, Pk> {
TrSpendInfoIter {
spend_info: self,
index: 0,
merkle_stack: Vec::with_capacity(128),
done_left_stack: BitStack128::default(),
}
}
/// If the Taproot tree is not keyspend-only, converts it to a [`bitcoin::taproot::TapTree`] structure.
///
/// This conversion is not particularly efficient but the resulting data structure is
/// useful for interacting with PSBTs.
pub fn to_tap_tree(&self) -> Option<bitcoin::taproot::TapTree> {
if self.nodes.is_empty() {
return None;
}
let mut builder = bitcoin::taproot::TaprootBuilder::new();
for leaf in self.leaves() {
builder = builder
.add_leaf_with_ver(
leaf.depth(),
ScriptBuf::from(leaf.script()),
leaf.leaf_version(),
)
.expect("iterating through tree in correct DFS order")
}
Some(bitcoin::taproot::TapTree::try_from(builder).expect("tree is complete"))
}
}
/// An internal node of the spend
#[derive(Debug)]
struct TrSpendInfoNode<Pk: MiniscriptKey> {
sibling_hash: TapNodeHash,
leaf_data: Option<LeafData<Pk>>,
}
#[derive(Debug)]
struct LeafData<Pk: MiniscriptKey> {
script: ScriptBuf,
miniscript: Arc<Miniscript<Pk, Tap>>,
leaf_hash: TapLeafHash,
}
/// An iterator over the leaves of a Taproot tree. Produced by [`TrSpendInfo::leaves`].
///
/// This is conceptually similar to [`super::TapTreeIter`], which can be obtained by
/// calling [`super::TapTree::leaves`]. That iterator goes over the leaves of the tree,
/// yielding the Miniscripts of the leaves and their depth.
///
/// This iterator goes over the leaves in the same order, yielding the data that actually
/// goes on chain: their scripts, control blocks, etc.
pub struct TrSpendInfoIter<'sp, Pk: MiniscriptKey> {
spend_info: &'sp TrSpendInfo<Pk>,
index: usize,
merkle_stack: Vec<TapNodeHash>,
done_left_stack: BitStack128,
}
impl<'sp, Pk: MiniscriptKey> Iterator for TrSpendInfoIter<'sp, Pk> {
type Item = TrSpendInfoIterItem<'sp, Pk>;
fn next(&mut self) -> Option<Self::Item> {
while self.index < self.spend_info.nodes.len() {
let current_node = &self.spend_info.nodes[self.index];
if self.index > 0 {
self.merkle_stack.push(current_node.sibling_hash);
}
self.index += 1;
if let Some(ref leaf) = current_node.leaf_data {
// leaf
let mut merkle_stack = self.merkle_stack.clone();
merkle_stack.reverse();
self.merkle_stack.pop();
loop {
match self.done_left_stack.pop() {
None => break, // this leaf is the root node
Some(false) => {
self.done_left_stack.push(true);
break;
}
Some(true) => {
self.merkle_stack.pop();
}
}
}
return Some(TrSpendInfoIterItem {
script: &leaf.script,
miniscript: &leaf.miniscript,
leaf_hash: leaf.leaf_hash,
control_block: ControlBlock {
leaf_version: LeafVersion::TapScript,
output_key_parity: self.spend_info.output_key_parity,
internal_key: self.spend_info.internal_key,
merkle_branch: TaprootMerkleBranch::try_from(merkle_stack)
.expect("merkle stack guaranteed to be within allowable length"),
},
});
} else {
// internal node
self.done_left_stack.push(false);
}
}
None
}
}
/// Item yielded from a [`TrSpendInfoIter`].
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TrSpendInfoIterItem<'tr, Pk: MiniscriptKey> {
script: &'tr Script,
miniscript: &'tr Arc<Miniscript<Pk, Tap>>,
leaf_hash: TapLeafHash,
control_block: ControlBlock,
}
impl<'sp, Pk: MiniscriptKey> TrSpendInfoIterItem<'sp, Pk> {
/// The Tapscript of this leaf.
#[inline]
pub fn script(&self) -> &'sp Script { self.script }
/// The Tapscript of this leaf, in Miniscript form.
#[inline]
pub fn miniscript(&self) -> &'sp Arc<Miniscript<Pk, Tap>> { self.miniscript }
/// The depth of the leaf in the tree.
///
/// This value is returned as `u8` since it is guaranteed to be <= 128 by the Taproot
/// consensus rules.
#[inline]
pub fn depth(&self) -> u8 {
self.control_block.merkle_branch.len() as u8 // cast ok, length limited to 128
}
/// The Tapleaf version of this leaf.
///
/// This function returns a constant value, since there is only one version in use
/// on the Bitcoin network; however, it may be useful to use this method in case
/// you wish to be forward-compatible with future versions supported by this
/// library.
#[inline]
pub fn leaf_version(&self) -> LeafVersion { self.control_block.leaf_version }
/// The hash of this leaf.
///
/// This hash, prefixed with the leaf's [`Self::leaf_version`], is what is directly
/// committed in the Taproot tree.
#[inline]
pub fn leaf_hash(&self) -> TapLeafHash { self.leaf_hash }
/// The control block of this leaf.
///
/// Unlike the other data obtainable from [`TrSpendInfoIterItem`], this one is computed
/// dynamically during iteration and therefore will not outlive the iterator item. See
/// [`Self::into_control_block`], which consumes the iterator item but will give you an
/// owned copy of the control block.
///
/// If you need access to multiple control blocks at once, you may need to `clone` the
/// return value of this method, or call [`Self::into_control_block`], and store the
/// result in a separate container.
#[inline]
pub fn control_block(&self) -> &ControlBlock { &self.control_block }
/// Extract the control block of this leaf, consuming `self`.
#[inline]
pub fn into_control_block(self) -> ControlBlock { self.control_block }
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(PartialEq, Eq, Debug)]
struct ExpectedTree {
internal_key: UntweakedPublicKey,
output_key: TweakedPublicKey,
output_key_parity: Parity,
merkle_root: Option<TapNodeHash>,
}
#[derive(PartialEq, Eq, Debug)]
struct ExpectedLeaf {
leaf_hash: TapLeafHash,
branch: TaprootMerkleBranch,
}
fn test_cases() -> Vec<(String, ExpectedTree, Vec<ExpectedLeaf>)> {
let secp = Secp256k1::verification_only();
let pk = "03cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115"
.parse::<bitcoin::PublicKey>()
.unwrap();
// Hash of the FALSE script
let zero_hash = "e7e4d593fcb72926eedbe0d1e311f41acd6f6ef161dcba081a75168ec4dcd379"
.parse::<TapLeafHash>()
.unwrap();
// Hash of the TRUE script
let one_hash = "a85b2107f791b26a84e7586c28cec7cb61202ed3d01944d832500f363782d675"
.parse::<TapLeafHash>()
.unwrap();
let mut ret = vec![];
// Empty tree
let merkle_root = None;
let internal_key = pk.to_x_only_pubkey();
let (output_key, output_key_parity) = internal_key.tap_tweak(&secp, merkle_root);
ret.push((
format!("tr({pk})"),
ExpectedTree { internal_key, output_key, output_key_parity, merkle_root },
vec![],
));
// Single-leaf tree
let merkle_root = Some(TapNodeHash::from(zero_hash));
let internal_key = pk.to_x_only_pubkey();
let (output_key, output_key_parity) = internal_key.tap_tweak(&secp, merkle_root);
ret.push((
format!("tr({pk},0)"),
ExpectedTree { internal_key, output_key, output_key_parity, merkle_root },
vec![ExpectedLeaf {
leaf_hash: zero_hash,
branch: TaprootMerkleBranch::try_from(vec![]).unwrap(),
}],
));
// Two-leaf tree, repeated leaf
let merkle_root = Some(
"e3208df58f4fae78044357451c8830698300cd7da47cf41957d82ac4ce1dd170"
.parse()
.unwrap(),
);
let internal_key = pk.to_x_only_pubkey();
let (output_key, output_key_parity) = internal_key.tap_tweak(&secp, merkle_root);
ret.push((
format!("tr({pk},{{0,0}})"),
ExpectedTree { internal_key, output_key, output_key_parity, merkle_root },
vec![
ExpectedLeaf {
leaf_hash: zero_hash,
branch: TaprootMerkleBranch::try_from(vec![TapNodeHash::from(zero_hash)])
.unwrap(),
},
ExpectedLeaf {
leaf_hash: zero_hash,
branch: TaprootMerkleBranch::try_from(vec![TapNodeHash::from(zero_hash)])
.unwrap(),
},
],
));
// Two-leaf tree, non-repeated leaf
let merkle_root = Some(
"15526cd6108b4765640abe555e75f4bd11d9b1453b9db4cd36cf4189577a6f63"
.parse()
.unwrap(),
);
let internal_key = pk.to_x_only_pubkey();
let (output_key, output_key_parity) = internal_key.tap_tweak(&secp, merkle_root);
ret.push((
format!("tr({pk},{{0,1}})"),
ExpectedTree { internal_key, output_key, output_key_parity, merkle_root },
vec![
ExpectedLeaf {
leaf_hash: zero_hash,
branch: TaprootMerkleBranch::try_from(vec![TapNodeHash::from(one_hash)])
.unwrap(),
},
ExpectedLeaf {
leaf_hash: one_hash,
branch: TaprootMerkleBranch::try_from(vec![TapNodeHash::from(zero_hash)])
.unwrap(),
},
],
));
// Fuzz test vector 1
let merkle_root = Some(
"d281962c67932b82e19b0da5ea437af316213e24509be0ef1bd7c5ee2b460d79"
.parse()
.unwrap(),
);
let internal_key = pk.to_x_only_pubkey();
let (output_key, output_key_parity) = internal_key.tap_tweak(&secp, merkle_root);
ret.push((
format!("tr({pk},{{0,{{0,tv:0}}}})"),
ExpectedTree { internal_key, output_key, output_key_parity, merkle_root },
vec![
ExpectedLeaf {
leaf_hash: zero_hash,
branch: TaprootMerkleBranch::try_from(vec![
"573d619569d58a36b52187e56f168650ac17f66a9a3afaf054900a04001019b3"
.parse::<TapNodeHash>()
.unwrap(),
])
.unwrap(),
},
ExpectedLeaf {
leaf_hash: zero_hash,
branch: TaprootMerkleBranch::try_from(vec![
"64ac241466a5e7032586718ff7465716f77a88d89946ce472daa4c3d0b81148f"
.parse::<TapNodeHash>()
.unwrap(),
TapNodeHash::from(zero_hash),
])
.unwrap(),
},
ExpectedLeaf {
leaf_hash: "64ac241466a5e7032586718ff7465716f77a88d89946ce472daa4c3d0b81148f"
.parse()
.unwrap(),
branch: TaprootMerkleBranch::try_from(vec![
TapNodeHash::from(zero_hash),
TapNodeHash::from(zero_hash),
])
.unwrap(),
},
],
));
// Fuzz test vector 2
let merkle_root = Some(
"2534e94c6ad06281b61fff86bad38a3911fb13436fb27fed6f5c057e4a71a911"
.parse()
.unwrap(),
);
let internal_key = pk.to_x_only_pubkey();
let (output_key, output_key_parity) = internal_key.tap_tweak(&secp, merkle_root);
ret.push((
format!("tr({pk},{{uuu:0,{{0,uu:0}}}})"),
ExpectedTree { internal_key, output_key, output_key_parity, merkle_root },
vec![
ExpectedLeaf {
leaf_hash: "6498e1d56640a272493d1d87549f3347dc448ca674556a2110cdfe100e3c238b"
.parse()
.unwrap(),
branch: TaprootMerkleBranch::try_from(vec![
"7e3e98bab404812c8eebd21c5d825527676b8e9f261f7ad479f3a08a83a43fb4"
.parse::<TapNodeHash>()
.unwrap(),
])
.unwrap(),
},
ExpectedLeaf {
leaf_hash: zero_hash,
branch: TaprootMerkleBranch::try_from(vec![
"19417c32bc6ca7e0f6e65b006ac305107c6add73c8bef31181037e6faaa55e7f"
.parse::<TapNodeHash>()
.unwrap(),
"6498e1d56640a272493d1d87549f3347dc448ca674556a2110cdfe100e3c238b"
.parse::<TapNodeHash>()
.unwrap(),
])
.unwrap(),
},
ExpectedLeaf {
leaf_hash: "19417c32bc6ca7e0f6e65b006ac305107c6add73c8bef31181037e6faaa55e7f"
.parse()
.unwrap(),
branch: TaprootMerkleBranch::try_from(vec![
TapNodeHash::from(zero_hash),
"6498e1d56640a272493d1d87549f3347dc448ca674556a2110cdfe100e3c238b"
.parse::<TapNodeHash>()
.unwrap(),
])
.unwrap(),
},
],
));
// Fuzz test vector 3
let merkle_root = Some(
"9f4bc03c65a88ffbbb3a8d4fe5e01be608109d9f875f35685d8865e181def26e"
.parse()
.unwrap(),
);
let internal_key = pk.to_x_only_pubkey();
let (output_key, output_key_parity) = internal_key.tap_tweak(&secp, merkle_root);
ret.push((
format!("tr({pk},{{{{0,{{uuu:0,0}}}},{{0,uu:0}}}})"),
ExpectedTree { internal_key, output_key, output_key_parity, merkle_root },
vec![
ExpectedLeaf {
leaf_hash: zero_hash,
branch: TaprootMerkleBranch::try_from(vec![
"57e3b7d414075ff4864deec9efa99db4462c038706306e02c58e02e957c8a51e"
.parse::<TapNodeHash>()
.unwrap(),
"7e3e98bab404812c8eebd21c5d825527676b8e9f261f7ad479f3a08a83a43fb4"
.parse::<TapNodeHash>()
.unwrap(),
])
.unwrap(),
},
ExpectedLeaf {
leaf_hash: "6498e1d56640a272493d1d87549f3347dc448ca674556a2110cdfe100e3c238b"
.parse()
.unwrap(),
branch: TaprootMerkleBranch::try_from(vec![
TapNodeHash::from(zero_hash),
TapNodeHash::from(zero_hash),
"7e3e98bab404812c8eebd21c5d825527676b8e9f261f7ad479f3a08a83a43fb4"
.parse::<TapNodeHash>()
.unwrap(),
])
.unwrap(),
},
ExpectedLeaf {
leaf_hash: zero_hash,
branch: TaprootMerkleBranch::try_from(vec![
"6498e1d56640a272493d1d87549f3347dc448ca674556a2110cdfe100e3c238b"
.parse::<TapNodeHash>()
.unwrap(),
TapNodeHash::from(zero_hash),
"7e3e98bab404812c8eebd21c5d825527676b8e9f261f7ad479f3a08a83a43fb4"
.parse::<TapNodeHash>()
.unwrap(),
])
.unwrap(),
},
ExpectedLeaf {
leaf_hash: zero_hash,
branch: TaprootMerkleBranch::try_from(vec![
"19417c32bc6ca7e0f6e65b006ac305107c6add73c8bef31181037e6faaa55e7f"
.parse::<TapNodeHash>()
.unwrap(),
"e034d7d8b221034861bf3893c63cb0ff60d28a7a00090d0dc57c26fec91983cb"
.parse::<TapNodeHash>()
.unwrap(),
])
.unwrap(),
},
ExpectedLeaf {
leaf_hash: "19417c32bc6ca7e0f6e65b006ac305107c6add73c8bef31181037e6faaa55e7f"
.parse()
.unwrap(),
branch: TaprootMerkleBranch::try_from(vec![
TapNodeHash::from(zero_hash),
"e034d7d8b221034861bf3893c63cb0ff60d28a7a00090d0dc57c26fec91983cb"
.parse::<TapNodeHash>()
.unwrap(),
])
.unwrap(),
},
],
));
ret
}
#[test]
fn spend_info_fixed_vectors() {
for (s, tree, leaves) in test_cases() {
let tr = s
.parse::<crate::descriptor::Tr<bitcoin::PublicKey>>()
.unwrap();
let spend_info = tr.spend_info();
assert_eq!(
spend_info.internal_key(),
tree.internal_key,
"internal key mismatch (left: computed, right: expected)",
);
assert_eq!(
spend_info.merkle_root(),
tree.merkle_root,
"merkle root mismatch (left: computed, right: expected)",
);
assert_eq!(
spend_info.output_key(),
tree.output_key,
"output key mismatch (left: computed, right: expected)",
);
let got_leaves: Vec<_> = spend_info
.leaves()
.map(|leaf| ExpectedLeaf {
leaf_hash: leaf.leaf_hash(),
branch: leaf.control_block().merkle_branch.clone(),
})
.collect();
assert_eq!(got_leaves, leaves, "leaves mismatch (left: computed, right: expected)",);
}
}
}