Skip to content

Commit afed38f

Browse files
committed
Add Tr descriptor
1 parent fc49ba0 commit afed38f

File tree

4 files changed

+406
-8
lines changed

4 files changed

+406
-8
lines changed

src/descriptor/mod.rs

+26
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ mod segwitv0;
4646
mod sh;
4747
mod sortedmulti;
4848
mod tr;
49+
4950
// Descriptor Exports
5051
pub use self::bare::{Bare, Pkh};
5152
pub use self::segwitv0::{Wpkh, Wsh, WshInner};
@@ -177,6 +178,8 @@ pub enum Descriptor<Pk: MiniscriptKey> {
177178
Sh(Sh<Pk>),
178179
/// Pay-to-Witness-ScriptHash with Segwitv0 context
179180
Wsh(Wsh<Pk>),
181+
/// Pay-to-Taproot
182+
Tr(Tr<Pk>),
180183
}
181184

182185
/// Descriptor Type of the descriptor
@@ -202,6 +205,8 @@ pub enum DescriptorType {
202205
WshSortedMulti,
203206
/// Sh Wsh Sorted Multi
204207
ShWshSortedMulti,
208+
/// Tr Descriptor
209+
Tr,
205210
}
206211

207212
impl<Pk: MiniscriptKey> Descriptor<Pk> {
@@ -288,6 +293,12 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
288293
Ok(Descriptor::Wsh(Wsh::new_sortedmulti(k, pks)?))
289294
}
290295

296+
/// Create new tr descriptor
297+
/// Errors when miniscript exceeds resource limits under Segwitv0 context
298+
pub fn new_tr(key: Pk, script: Option<tr::TapTree<Pk>>) -> Result<Self, Error> {
299+
Ok(Descriptor::Tr(Tr::new(key, script)?))
300+
}
301+
291302
/// Get the [DescriptorType] of [Descriptor]
292303
pub fn desc_type(&self) -> DescriptorType {
293304
match *self {
@@ -307,6 +318,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
307318
WshInner::SortedMulti(ref _smv) => DescriptorType::WshSortedMulti,
308319
WshInner::Ms(ref _ms) => DescriptorType::Wsh,
309320
},
321+
Descriptor::Tr(ref _tr) => DescriptorType::Tr,
310322
}
311323
}
312324
}
@@ -343,6 +355,9 @@ impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Descriptor<P> {
343355
Descriptor::Wsh(ref wsh) => {
344356
Descriptor::Wsh(wsh.translate_pk(&mut translatefpk, &mut translatefpkh)?)
345357
}
358+
Descriptor::Tr(ref tr) => {
359+
Descriptor::Tr(tr.translate_pk(&mut translatefpk, &mut translatefpkh)?)
360+
}
346361
};
347362
Ok(desc)
348363
}
@@ -364,6 +379,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
364379
Descriptor::Wpkh(ref wpkh) => wpkh.sanity_check(),
365380
Descriptor::Wsh(ref wsh) => wsh.sanity_check(),
366381
Descriptor::Sh(ref sh) => sh.sanity_check(),
382+
Descriptor::Tr(ref tr) => tr.sanity_check(),
367383
}
368384
}
369385
/// Computes the Bitcoin address of the descriptor, if one exists
@@ -377,6 +393,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
377393
Descriptor::Wpkh(ref wpkh) => wpkh.address(network),
378394
Descriptor::Wsh(ref wsh) => wsh.address(network),
379395
Descriptor::Sh(ref sh) => sh.address(network),
396+
Descriptor::Tr(ref tr) => tr.address(network),
380397
}
381398
}
382399

@@ -391,6 +408,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
391408
Descriptor::Wpkh(ref wpkh) => wpkh.script_pubkey(),
392409
Descriptor::Wsh(ref wsh) => wsh.script_pubkey(),
393410
Descriptor::Sh(ref sh) => sh.script_pubkey(),
411+
Descriptor::Tr(ref tr) => tr.script_pubkey(),
394412
}
395413
}
396414

@@ -412,6 +430,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
412430
Descriptor::Wpkh(ref wpkh) => wpkh.unsigned_script_sig(),
413431
Descriptor::Wsh(ref wsh) => wsh.unsigned_script_sig(),
414432
Descriptor::Sh(ref sh) => sh.unsigned_script_sig(),
433+
Descriptor::Tr(ref tr) => tr.unsigned_script_sig(),
415434
}
416435
}
417436

@@ -431,6 +450,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
431450
Descriptor::Wpkh(ref wpkh) => wpkh.explicit_script(),
432451
Descriptor::Wsh(ref wsh) => wsh.explicit_script(),
433452
Descriptor::Sh(ref sh) => sh.explicit_script(),
453+
Descriptor::Tr(ref tr) => tr.explicit_script(),
434454
}
435455
}
436456

@@ -448,6 +468,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
448468
Descriptor::Wpkh(ref wpkh) => wpkh.get_satisfaction(satisfier),
449469
Descriptor::Wsh(ref wsh) => wsh.get_satisfaction(satisfier),
450470
Descriptor::Sh(ref sh) => sh.get_satisfaction(satisfier),
471+
Descriptor::Tr(ref tr) => tr.get_satisfaction(satisfier),
451472
}
452473
}
453474

@@ -462,6 +483,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
462483
Descriptor::Wpkh(ref wpkh) => wpkh.max_satisfaction_weight(),
463484
Descriptor::Wsh(ref wsh) => wsh.max_satisfaction_weight(),
464485
Descriptor::Sh(ref sh) => sh.max_satisfaction_weight(),
486+
Descriptor::Tr(ref tr) => tr.max_satisfaction_weight(),
465487
}
466488
}
467489

@@ -480,6 +502,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
480502
Descriptor::Wpkh(ref wpkh) => wpkh.script_code(),
481503
Descriptor::Wsh(ref wsh) => wsh.script_code(),
482504
Descriptor::Sh(ref sh) => sh.script_code(),
505+
Descriptor::Tr(ref tr) => tr.script_code(),
483506
}
484507
}
485508
}
@@ -496,6 +519,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
496519
Descriptor::Wpkh(ref wpkh) => wpkh.for_each_key(pred),
497520
Descriptor::Wsh(ref wsh) => wsh.for_each_key(pred),
498521
Descriptor::Sh(ref sh) => sh.for_each_key(pred),
522+
Descriptor::Tr(ref tr) => tr.for_each_key(pred),
499523
}
500524
}
501525
}
@@ -615,6 +639,7 @@ impl<Pk: MiniscriptKey> fmt::Debug for Descriptor<Pk> {
615639
Descriptor::Wpkh(ref wpkh) => write!(f, "{:?}", wpkh),
616640
Descriptor::Sh(ref sub) => write!(f, "{:?}", sub),
617641
Descriptor::Wsh(ref sub) => write!(f, "{:?}", sub),
642+
Descriptor::Tr(ref tr) => write!(f, "{:?}", tr),
618643
}
619644
}
620645
}
@@ -627,6 +652,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Descriptor<Pk> {
627652
Descriptor::Wpkh(ref wpkh) => write!(f, "{}", wpkh),
628653
Descriptor::Sh(ref sub) => write!(f, "{}", sub),
629654
Descriptor::Wsh(ref sub) => write!(f, "{}", sub),
655+
Descriptor::Tr(ref tr) => write!(f, "{}", tr),
630656
}
631657
}
632658
}

0 commit comments

Comments
 (0)