Skip to content

Commit ff365fd

Browse files
taproot descriptor: add roundtrip tests
1 parent d5b764a commit ff365fd

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/descriptor/mod.rs

+52
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};
@@ -168,6 +169,9 @@ pub enum Descriptor<Pk: MiniscriptKey> {
168169
Sh(Sh<Pk>),
169170
/// Pay-to-Witness-ScriptHash with Segwitv0 context
170171
Wsh(Wsh<Pk>),
172+
// /// Pay-to-Taproot with Segwitv0 context
173+
// /// TODO: Update context to Segwitv1
174+
// Tr(Tr<Pk>)
171175
}
172176

173177
/// Descriptor Type of the descriptor
@@ -193,6 +197,8 @@ pub enum DescriptorType {
193197
WshSortedMulti,
194198
/// Sh Wsh Sorted Multi
195199
ShWshSortedMulti,
200+
// /// Tr Descriptor
201+
// Tr
196202
}
197203

198204
impl<Pk: MiniscriptKey> Descriptor<Pk> {
@@ -279,6 +285,12 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
279285
Ok(Descriptor::Wsh(Wsh::new_sortedmulti(k, pks)?))
280286
}
281287

288+
// /// Create new tr descriptor
289+
// /// Errors when miniscript exceeds resource limits under Segwitv0 context
290+
// pub fn new_tr(key: Pk, script: Option<tr::TapTree<Pk>>) -> Result<Self, Error> {
291+
// Ok(Descriptor::Tr(Tr::new(key, script)?))
292+
// }
293+
282294
/// Get the [DescriptorType] of [Descriptor]
283295
pub fn desc_type(&self) -> DescriptorType {
284296
match *self {
@@ -298,6 +310,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
298310
WshInner::SortedMulti(ref _smv) => DescriptorType::WshSortedMulti,
299311
WshInner::Ms(ref _ms) => DescriptorType::Wsh,
300312
},
313+
// Descriptor::Tr(_) => DescriptorType::Tr,
301314
}
302315
}
303316
}
@@ -624,6 +637,7 @@ serde_string_impl_pk!(Descriptor, "a script descriptor");
624637
#[cfg(test)]
625638
mod tests {
626639
use super::checksum::desc_checksum;
640+
use super::tr::Tr;
627641
use super::DescriptorTrait;
628642
use bitcoin::blockdata::opcodes::all::{OP_CLTV, OP_CSV};
629643
use bitcoin::blockdata::script::Instruction;
@@ -1088,6 +1102,44 @@ mod tests {
10881102
assert_eq!(check, &Ok(Instruction::Op(OP_CSV)))
10891103
}
10901104

1105+
#[test]
1106+
fn tr_roundtrip_key() {
1107+
let script = Tr::<DummyKey>::from_str("tr()").unwrap().to_string();
1108+
assert_eq!(script, format!("tr()#x4ml3kxd"))
1109+
}
1110+
1111+
#[test]
1112+
fn tr_roundtrip_script() {
1113+
let descriptor = Tr::<DummyKey>::from_str("tr(,{pk(),pk()})")
1114+
.unwrap()
1115+
.to_string();
1116+
1117+
assert_eq!(descriptor, "tr(,{pk(),pk()})#7dqr6v8r")
1118+
}
1119+
1120+
#[test]
1121+
fn tr_roundtrip_tree() {
1122+
let p1 = "020000000000000000000000000000000000000000000000000000000000000001";
1123+
let p2 = "020000000000000000000000000000000000000000000000000000000000000002";
1124+
let p3 = "020000000000000000000000000000000000000000000000000000000000000003";
1125+
let p4 = "020000000000000000000000000000000000000000000000000000000000000004";
1126+
let p5 = "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31";
1127+
let descriptor = Tr::<PublicKey>::from_str(&format!(
1128+
"tr({},{{pk({}),{{pk({}),or_d(pk({}),pkh({}))}}}})",
1129+
p1, p2, p3, p4, p5
1130+
))
1131+
.unwrap()
1132+
.to_string();
1133+
1134+
assert_eq!(
1135+
descriptor,
1136+
format!(
1137+
"tr({},{{pk({}),{{pk({}),or_d(pk({}),pkh({}))}}}})#fdhmu4fj",
1138+
p1, p2, p3, p4, p5
1139+
)
1140+
)
1141+
}
1142+
10911143
#[test]
10921144
fn roundtrip_tests() {
10931145
let descriptor = Descriptor::<bitcoin::PublicKey>::from_str("multi");

0 commit comments

Comments
 (0)