Skip to content

Commit 178f12b

Browse files
committed
tr: Harmonize Display and FromStr
Display is the basis for ToString. FromStr expects sim{...} around Simplicity tap leaves. This commit serializes Simplicity leaves in the same way. The commit also tests this behavior.
1 parent 197b195 commit 178f12b

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/descriptor/tr.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl<Pk: MiniscriptKey, Ext: Extension> fmt::Display for TapTree<Pk, Ext> {
234234
match self {
235235
TapTree::Tree(ref left, ref right) => write!(f, "{{{},{}}}", *left, *right),
236236
TapTree::Leaf(ref script) => write!(f, "{}", *script),
237-
TapTree::SimplicityLeaf(ref policy) => write!(f, "{}", policy),
237+
TapTree::SimplicityLeaf(ref policy) => write!(f, "sim{{{}}}", policy),
238238
}
239239
}
240240
}
@@ -949,15 +949,17 @@ mod tests {
949949
fn tr_from_str() {
950950
// Key spend only
951951

952-
let desc = "eltr(internal)";
953-
let desc = Tr::<String, NoExt>::from_str(desc).unwrap();
952+
let desc_str = "eltr(internal)#0aen4jhp";
953+
let desc = Tr::<String, NoExt>::from_str(desc_str).unwrap();
954+
assert_eq!(desc_str, &desc.to_string());
954955
assert_eq!("internal", desc.internal_key);
955956
assert!(desc.iter_scripts().next().is_none());
956957

957958
// Miniscript key spend
958959

959-
let desc = "eltr(internal,pk(a))";
960-
let desc = Tr::<String, NoExt>::from_str(desc).unwrap();
960+
let desc_str = "eltr(internal,pk(a))#vadmk9gd";
961+
let desc = Tr::<String, NoExt>::from_str(desc_str).unwrap();
962+
assert_eq!(desc_str, &desc.to_string());
961963
assert_eq!("internal", desc.internal_key);
962964

963965
let scripts: Vec<_> = desc.iter_scripts().collect();
@@ -967,8 +969,9 @@ mod tests {
967969

968970
// Simplicity key spend
969971

970-
let desc = "eltr(internal,sim{pk(a)})";
971-
let desc = Tr::<String, NoExt>::from_str(desc).unwrap();
972+
let desc_str = "eltr(internal,sim{pk(a)})#duhmnzmm";
973+
let desc = Tr::<String, NoExt>::from_str(desc_str).unwrap();
974+
assert_eq!(desc_str, &desc.to_string());
972975
assert_eq!("internal", desc.internal_key);
973976

974977
let policy = desc.get_simplicity().expect("single simplicity leaf");

0 commit comments

Comments
 (0)