Skip to content

Commit 71f9c91

Browse files
committed
Add Tr descriptor
Fix TapTree iter depth
1 parent 9c51588 commit 71f9c91

File tree

4 files changed

+440
-15
lines changed

4 files changed

+440
-15
lines changed

src/descriptor/mod.rs

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

3232
use bitcoin::blockdata::witness::Witness;
33-
use bitcoin::secp256k1;
34-
use bitcoin::{self, Script};
33+
use bitcoin::{self, secp256k1, Script};
3534

3635
use self::checksum::verify_checksum;
3736
use expression;
@@ -47,6 +46,7 @@ mod segwitv0;
4746
mod sh;
4847
mod sortedmulti;
4948
mod tr;
49+
5050
// Descriptor Exports
5151
pub use self::bare::{Bare, Pkh};
5252
pub use self::segwitv0::{Wpkh, Wsh, WshInner};
@@ -186,6 +186,8 @@ pub enum Descriptor<Pk: MiniscriptKey> {
186186
Sh(Sh<Pk>),
187187
/// Pay-to-Witness-ScriptHash with Segwitv0 context
188188
Wsh(Wsh<Pk>),
189+
/// Pay-to-Taproot
190+
Tr(Tr<Pk>),
189191
}
190192

191193
/// Descriptor Type of the descriptor
@@ -211,6 +213,8 @@ pub enum DescriptorType {
211213
WshSortedMulti,
212214
/// Sh Wsh Sorted Multi
213215
ShWshSortedMulti,
216+
/// Tr Descriptor
217+
Tr,
214218
}
215219

216220
impl<Pk: MiniscriptKey> Descriptor<Pk> {
@@ -297,6 +301,12 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
297301
Ok(Descriptor::Wsh(Wsh::new_sortedmulti(k, pks)?))
298302
}
299303

304+
/// Create new tr descriptor
305+
/// Errors when miniscript exceeds resource limits under Segwitv0 context
306+
pub fn new_tr(key: Pk, script: Option<tr::TapTree<Pk>>) -> Result<Self, Error> {
307+
Ok(Descriptor::Tr(Tr::new(key, script)?))
308+
}
309+
300310
/// Get the [DescriptorType] of [Descriptor]
301311
pub fn desc_type(&self) -> DescriptorType {
302312
match *self {
@@ -316,6 +326,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
316326
WshInner::SortedMulti(ref _smv) => DescriptorType::WshSortedMulti,
317327
WshInner::Ms(ref _ms) => DescriptorType::Wsh,
318328
},
329+
Descriptor::Tr(ref _tr) => DescriptorType::Tr,
319330
}
320331
}
321332
}
@@ -352,6 +363,9 @@ impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Descriptor<P> {
352363
Descriptor::Wsh(ref wsh) => {
353364
Descriptor::Wsh(wsh.translate_pk(&mut translatefpk, &mut translatefpkh)?)
354365
}
366+
Descriptor::Tr(ref tr) => {
367+
Descriptor::Tr(tr.translate_pk(&mut translatefpk, &mut translatefpkh)?)
368+
}
355369
};
356370
Ok(desc)
357371
}
@@ -373,6 +387,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
373387
Descriptor::Wpkh(ref wpkh) => wpkh.sanity_check(),
374388
Descriptor::Wsh(ref wsh) => wsh.sanity_check(),
375389
Descriptor::Sh(ref sh) => sh.sanity_check(),
390+
Descriptor::Tr(ref tr) => tr.sanity_check(),
376391
}
377392
}
378393
/// Computes the Bitcoin address of the descriptor, if one exists
@@ -386,6 +401,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
386401
Descriptor::Wpkh(ref wpkh) => wpkh.address(network),
387402
Descriptor::Wsh(ref wsh) => wsh.address(network),
388403
Descriptor::Sh(ref sh) => sh.address(network),
404+
Descriptor::Tr(ref tr) => tr.address(network),
389405
}
390406
}
391407

@@ -400,6 +416,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
400416
Descriptor::Wpkh(ref wpkh) => wpkh.script_pubkey(),
401417
Descriptor::Wsh(ref wsh) => wsh.script_pubkey(),
402418
Descriptor::Sh(ref sh) => sh.script_pubkey(),
419+
Descriptor::Tr(ref tr) => tr.script_pubkey(),
403420
}
404421
}
405422

@@ -421,6 +438,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
421438
Descriptor::Wpkh(ref wpkh) => wpkh.unsigned_script_sig(),
422439
Descriptor::Wsh(ref wsh) => wsh.unsigned_script_sig(),
423440
Descriptor::Sh(ref sh) => sh.unsigned_script_sig(),
441+
Descriptor::Tr(ref tr) => tr.unsigned_script_sig(),
424442
}
425443
}
426444

@@ -440,6 +458,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
440458
Descriptor::Wpkh(ref wpkh) => wpkh.explicit_script(),
441459
Descriptor::Wsh(ref wsh) => wsh.explicit_script(),
442460
Descriptor::Sh(ref sh) => sh.explicit_script(),
461+
Descriptor::Tr(ref tr) => tr.explicit_script(),
443462
}
444463
}
445464

@@ -457,6 +476,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
457476
Descriptor::Wpkh(ref wpkh) => wpkh.get_satisfaction(satisfier),
458477
Descriptor::Wsh(ref wsh) => wsh.get_satisfaction(satisfier),
459478
Descriptor::Sh(ref sh) => sh.get_satisfaction(satisfier),
479+
Descriptor::Tr(ref tr) => tr.get_satisfaction(satisfier),
460480
}
461481
}
462482

@@ -474,6 +494,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
474494
Descriptor::Wpkh(ref wpkh) => wpkh.get_satisfaction_mall(satisfier),
475495
Descriptor::Wsh(ref wsh) => wsh.get_satisfaction_mall(satisfier),
476496
Descriptor::Sh(ref sh) => sh.get_satisfaction_mall(satisfier),
497+
Descriptor::Tr(ref tr) => tr.get_satisfaction_mall(satisfier),
477498
}
478499
}
479500

@@ -488,6 +509,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
488509
Descriptor::Wpkh(ref wpkh) => wpkh.max_satisfaction_weight(),
489510
Descriptor::Wsh(ref wsh) => wsh.max_satisfaction_weight(),
490511
Descriptor::Sh(ref sh) => sh.max_satisfaction_weight(),
512+
Descriptor::Tr(ref tr) => tr.max_satisfaction_weight(),
491513
}
492514
}
493515

@@ -506,6 +528,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
506528
Descriptor::Wpkh(ref wpkh) => wpkh.script_code(),
507529
Descriptor::Wsh(ref wsh) => wsh.script_code(),
508530
Descriptor::Sh(ref sh) => sh.script_code(),
531+
Descriptor::Tr(ref tr) => tr.script_code(),
509532
}
510533
}
511534
}
@@ -522,6 +545,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
522545
Descriptor::Wpkh(ref wpkh) => wpkh.for_each_key(pred),
523546
Descriptor::Wsh(ref wsh) => wsh.for_each_key(pred),
524547
Descriptor::Sh(ref sh) => sh.for_each_key(pred),
548+
Descriptor::Tr(ref tr) => tr.for_each_key(pred),
525549
}
526550
}
527551
}
@@ -641,6 +665,7 @@ impl<Pk: MiniscriptKey> fmt::Debug for Descriptor<Pk> {
641665
Descriptor::Wpkh(ref wpkh) => write!(f, "{:?}", wpkh),
642666
Descriptor::Sh(ref sub) => write!(f, "{:?}", sub),
643667
Descriptor::Wsh(ref sub) => write!(f, "{:?}", sub),
668+
Descriptor::Tr(ref tr) => write!(f, "{:?}", tr),
644669
}
645670
}
646671
}
@@ -653,6 +678,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Descriptor<Pk> {
653678
Descriptor::Wpkh(ref wpkh) => write!(f, "{}", wpkh),
654679
Descriptor::Sh(ref sub) => write!(f, "{}", sub),
655680
Descriptor::Wsh(ref sub) => write!(f, "{}", sub),
681+
Descriptor::Tr(ref tr) => write!(f, "{}", tr),
656682
}
657683
}
658684
}

0 commit comments

Comments
 (0)