Skip to content

Commit acced27

Browse files
committed
Add parse insane API for Tr descriptors
All other descriptor APIs only deal with sane miniscripts
1 parent e09a173 commit acced27

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/descriptor/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,8 @@ impl_from_str!(
10221022
Ok(tr) => Ok(Descriptor::Tr(tr)),
10231023
Err(_) => {
10241024
// Try parsing with extensions
1025-
let tr = Tr::<Pk, T>::from_str(s)?;
1025+
// descriptors are always parsed insane. This will improve once we use upstream ExtParams Api.
1026+
let tr = Tr::<Pk, T>::from_str_insane(s)?;
10261027
Ok(Descriptor::TrExt(tr))
10271028
}
10281029
}

src/descriptor/tr.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Tapscript
22

33
use std::cmp::{self, max};
4-
use std::str::FromStr;
54
use std::sync::{Arc, Mutex};
65
use std::{fmt, hash};
76

@@ -417,7 +416,7 @@ impl_block_str!(
417416
fn parse_tr_script_spend(tree: &expression::Tree,) -> Result<TapTree<Pk, Ext>, Error> {
418417
match tree {
419418
expression::Tree { name, args } if !name.is_empty() && args.is_empty() => {
420-
let script = Miniscript::<Pk, Tap, Ext>::from_str(name)?;
419+
let script = Miniscript::<Pk, Tap, Ext>::from_str_insane(name)?;
421420
Ok(TapTree::Leaf(Arc::new(script)))
422421
}
423422
expression::Tree { name, args } if name.is_empty() && args.len() == 2 => {
@@ -484,6 +483,20 @@ impl_from_str!(
484483
=> Ext; Extension,
485484
type Err = Error;,
486485
fn from_str(s: &str) -> Result<Self, Self::Err> {
486+
let res = Self::from_str_insane(s)?;
487+
if res.iter_scripts().any(|(_, ms)| ms.sanity_check().is_err()) {
488+
return Err(Error::BadDescriptor("Sanity check failed".to_string()));
489+
}
490+
Ok(res)
491+
}
492+
);
493+
494+
#[rustfmt::skip]
495+
impl_block_str!(
496+
Tr<Pk, Ext>,
497+
=> Ext; Extension,
498+
/// Parse taproot descriptors without any sanity checks
499+
pub fn from_str_insane(s: &str,) -> Result<Tr<Pk, Ext>, Error> {
487500
let desc_str = verify_checksum(s)?;
488501
let top = parse_tr_tree(desc_str)?;
489502
Self::from_tree(&top)
@@ -742,6 +755,7 @@ where
742755
mod tests {
743756
use super::*;
744757
use crate::{ForEachKey, NoExt};
758+
use core::str::FromStr;
745759

746760
#[test]
747761
fn test_for_each() {

0 commit comments

Comments
 (0)