Skip to content

Commit bf49604

Browse files
committed
Add Tr descriptor
Fix TapTree iter depth
1 parent 7aa607e commit bf49604

File tree

4 files changed

+441
-15
lines changed

4 files changed

+441
-15
lines changed

src/descriptor/mod.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ use std::{
2929
str::{self, FromStr},
3030
};
3131

32-
use bitcoin::secp256k1;
33-
use bitcoin::{self, Script};
32+
use bitcoin::{self, secp256k1, Script};
3433

3534
use self::checksum::verify_checksum;
3635
use expression;
@@ -46,6 +45,7 @@ mod segwitv0;
4645
mod sh;
4746
mod sortedmulti;
4847
mod tr;
48+
4949
// Descriptor Exports
5050
pub use self::bare::{Bare, Pkh};
5151
pub use self::segwitv0::{Wpkh, Wsh, WshInner};
@@ -185,6 +185,8 @@ pub enum Descriptor<Pk: MiniscriptKey> {
185185
Sh(Sh<Pk>),
186186
/// Pay-to-Witness-ScriptHash with Segwitv0 context
187187
Wsh(Wsh<Pk>),
188+
/// Pay-to-Taproot
189+
Tr(Tr<Pk>),
188190
}
189191

190192
/// Descriptor Type of the descriptor
@@ -210,6 +212,8 @@ pub enum DescriptorType {
210212
WshSortedMulti,
211213
/// Sh Wsh Sorted Multi
212214
ShWshSortedMulti,
215+
/// Tr Descriptor
216+
Tr,
213217
}
214218

215219
impl<Pk: MiniscriptKey> Descriptor<Pk> {
@@ -296,6 +300,12 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
296300
Ok(Descriptor::Wsh(Wsh::new_sortedmulti(k, pks)?))
297301
}
298302

303+
/// Create new tr descriptor
304+
/// Errors when miniscript exceeds resource limits under Segwitv0 context
305+
pub fn new_tr(key: Pk, script: Option<tr::TapTree<Pk>>) -> Result<Self, Error> {
306+
Ok(Descriptor::Tr(Tr::new(key, script)?))
307+
}
308+
299309
/// Get the [DescriptorType] of [Descriptor]
300310
pub fn desc_type(&self) -> DescriptorType {
301311
match *self {
@@ -315,6 +325,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
315325
WshInner::SortedMulti(ref _smv) => DescriptorType::WshSortedMulti,
316326
WshInner::Ms(ref _ms) => DescriptorType::Wsh,
317327
},
328+
Descriptor::Tr(ref _tr) => DescriptorType::Tr,
318329
}
319330
}
320331
}
@@ -351,6 +362,9 @@ impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Descriptor<P> {
351362
Descriptor::Wsh(ref wsh) => {
352363
Descriptor::Wsh(wsh.translate_pk(&mut translatefpk, &mut translatefpkh)?)
353364
}
365+
Descriptor::Tr(ref tr) => {
366+
Descriptor::Tr(tr.translate_pk(&mut translatefpk, &mut translatefpkh)?)
367+
}
354368
};
355369
Ok(desc)
356370
}
@@ -372,6 +386,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
372386
Descriptor::Wpkh(ref wpkh) => wpkh.sanity_check(),
373387
Descriptor::Wsh(ref wsh) => wsh.sanity_check(),
374388
Descriptor::Sh(ref sh) => sh.sanity_check(),
389+
Descriptor::Tr(ref tr) => tr.sanity_check(),
375390
}
376391
}
377392
/// Computes the Bitcoin address of the descriptor, if one exists
@@ -385,6 +400,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
385400
Descriptor::Wpkh(ref wpkh) => wpkh.address(network),
386401
Descriptor::Wsh(ref wsh) => wsh.address(network),
387402
Descriptor::Sh(ref sh) => sh.address(network),
403+
Descriptor::Tr(ref tr) => tr.address(network),
388404
}
389405
}
390406

@@ -399,6 +415,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
399415
Descriptor::Wpkh(ref wpkh) => wpkh.script_pubkey(),
400416
Descriptor::Wsh(ref wsh) => wsh.script_pubkey(),
401417
Descriptor::Sh(ref sh) => sh.script_pubkey(),
418+
Descriptor::Tr(ref tr) => tr.script_pubkey(),
402419
}
403420
}
404421

@@ -420,6 +437,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
420437
Descriptor::Wpkh(ref wpkh) => wpkh.unsigned_script_sig(),
421438
Descriptor::Wsh(ref wsh) => wsh.unsigned_script_sig(),
422439
Descriptor::Sh(ref sh) => sh.unsigned_script_sig(),
440+
Descriptor::Tr(ref tr) => tr.unsigned_script_sig(),
423441
}
424442
}
425443

@@ -439,6 +457,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
439457
Descriptor::Wpkh(ref wpkh) => wpkh.explicit_script(),
440458
Descriptor::Wsh(ref wsh) => wsh.explicit_script(),
441459
Descriptor::Sh(ref sh) => sh.explicit_script(),
460+
Descriptor::Tr(ref tr) => tr.explicit_script(),
442461
}
443462
}
444463

@@ -456,6 +475,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
456475
Descriptor::Wpkh(ref wpkh) => wpkh.get_satisfaction(satisfier),
457476
Descriptor::Wsh(ref wsh) => wsh.get_satisfaction(satisfier),
458477
Descriptor::Sh(ref sh) => sh.get_satisfaction(satisfier),
478+
Descriptor::Tr(ref tr) => tr.get_satisfaction(satisfier),
459479
}
460480
}
461481

@@ -473,6 +493,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
473493
Descriptor::Wpkh(ref wpkh) => wpkh.get_satisfaction_mall(satisfier),
474494
Descriptor::Wsh(ref wsh) => wsh.get_satisfaction_mall(satisfier),
475495
Descriptor::Sh(ref sh) => sh.get_satisfaction_mall(satisfier),
496+
Descriptor::Tr(ref tr) => tr.get_satisfaction_mall(satisfier),
476497
}
477498
}
478499

@@ -487,6 +508,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
487508
Descriptor::Wpkh(ref wpkh) => wpkh.max_satisfaction_weight(),
488509
Descriptor::Wsh(ref wsh) => wsh.max_satisfaction_weight(),
489510
Descriptor::Sh(ref sh) => sh.max_satisfaction_weight(),
511+
Descriptor::Tr(ref tr) => tr.max_satisfaction_weight(),
490512
}
491513
}
492514

@@ -505,6 +527,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
505527
Descriptor::Wpkh(ref wpkh) => wpkh.script_code(),
506528
Descriptor::Wsh(ref wsh) => wsh.script_code(),
507529
Descriptor::Sh(ref sh) => sh.script_code(),
530+
Descriptor::Tr(ref tr) => tr.script_code(),
508531
}
509532
}
510533
}
@@ -521,6 +544,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
521544
Descriptor::Wpkh(ref wpkh) => wpkh.for_each_key(pred),
522545
Descriptor::Wsh(ref wsh) => wsh.for_each_key(pred),
523546
Descriptor::Sh(ref sh) => sh.for_each_key(pred),
547+
Descriptor::Tr(ref tr) => tr.for_each_key(pred),
524548
}
525549
}
526550
}
@@ -640,6 +664,7 @@ impl<Pk: MiniscriptKey> fmt::Debug for Descriptor<Pk> {
640664
Descriptor::Wpkh(ref wpkh) => write!(f, "{:?}", wpkh),
641665
Descriptor::Sh(ref sub) => write!(f, "{:?}", sub),
642666
Descriptor::Wsh(ref sub) => write!(f, "{:?}", sub),
667+
Descriptor::Tr(ref tr) => write!(f, "{:?}", tr),
643668
}
644669
}
645670
}
@@ -652,6 +677,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Descriptor<Pk> {
652677
Descriptor::Wpkh(ref wpkh) => write!(f, "{}", wpkh),
653678
Descriptor::Sh(ref sub) => write!(f, "{}", sub),
654679
Descriptor::Wsh(ref sub) => write!(f, "{}", sub),
680+
Descriptor::Tr(ref tr) => write!(f, "{}", tr),
655681
}
656682
}
657683
}

0 commit comments

Comments
 (0)