@@ -18,7 +18,6 @@ use bitcoin::hashes::hash160;
1818use bitcoin:: script;
1919use bitcoin:: taproot:: { LeafVersion , TapLeafHash } ;
2020
21- use self :: analyzable:: ExtParams ;
2221pub use self :: context:: { BareCtx , Legacy , Segwitv0 , Tap } ;
2322use crate :: iter:: TreeLike ;
2423use crate :: prelude:: * ;
@@ -1003,33 +1002,26 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
10031002}
10041003
10051004impl < Pk : FromStrKey , Ctx : ScriptContext > Miniscript < Pk , Ctx > {
1006- /// Attempt to parse an insane(scripts don't clear sanity checks)
1007- /// from string into a Miniscript representation.
1008- /// Use this to parse scripts with repeated pubkeys, timelock mixing, malleable
1009- /// scripts without sig or scripts that can exceed resource limits.
1010- /// Some of the analysis guarantees of miniscript are lost when dealing with
1011- /// insane scripts. In general, in a multi-party setting users should only
1012- /// accept sane scripts.
1013- pub fn from_str_insane ( s : & str ) -> Result < Miniscript < Pk , Ctx > , Error > {
1014- Miniscript :: from_str_ext ( s, & ExtParams :: insane ( ) )
1005+ /// Attempt to parse a Miniscript, checking only for consensus compatibility,
1006+ /// lack of raw pubkeyhashes, and no other checks.
1007+ ///
1008+ /// It is not recommended to use scripts which require this function in order
1009+ /// to parse, especially in a multiparty setting.
1010+ pub fn from_str_insane ( s : & str ) -> Result < Self , Error > {
1011+ let params = ValidationParams { allow_raw_pkh : false , ..Ctx :: CONSENSUS } ;
1012+ Miniscript :: from_str_with_validation_params ( s, & params)
10151013 }
10161014
1017- /// Attempt to parse an Miniscripts that don't follow the spec.
1018- /// Use this to parse scripts with repeated pubkeys, timelock mixing, malleable
1019- /// scripts, raw pubkey hashes without sig or scripts that can exceed resource limits.
1020- ///
1021- /// Use [`ExtParams`] builder to specify the types of non-sane rules to allow while parsing.
1022- pub fn from_str_ext ( s : & str , ext : & ExtParams ) -> Result < Miniscript < Pk , Ctx > , Error > {
1015+ /// Attempt to parse a Miniscript, specifying which validation parameters to apply.
1016+ pub fn from_str_with_validation_params (
1017+ s : & str ,
1018+ params : & ValidationParams ,
1019+ ) -> Result < Self , Error > {
10231020 // This checks for invalid ASCII chars
10241021 let top = expression:: Tree :: from_str ( s) ?;
10251022 let ms: Miniscript < Pk , Ctx > = expression:: FromTree :: from_tree ( top. root ( ) ) ?;
1026- ms. ext_check ( ext) ?;
1027-
1028- if ms. ty . corr . base != types:: Base :: B {
1029- Err ( Error :: NonTopLevel ( format ! ( "{:?}" , ms) ) )
1030- } else {
1031- Ok ( ms)
1032- }
1023+ ms. validate ( params) . map_err ( Error :: Validation ) ?;
1024+ Ok ( ms)
10331025 }
10341026}
10351027
@@ -1247,7 +1239,7 @@ impl<Pk: FromStrKey, Ctx: ScriptContext> str::FromStr for Miniscript<Pk, Ctx> {
12471239 /// See [Miniscript::from_str_insane] to parse scripts from string that
12481240 /// do not clear the [Miniscript::sanity_check] checks.
12491241 fn from_str ( s : & str ) -> Result < Miniscript < Pk , Ctx > , Error > {
1250- let ms = Self :: from_str_ext ( s, & ExtParams :: sane ( ) ) ?;
1242+ let ms = Self :: from_str_with_validation_params ( s, & Ctx :: SANE ) ?;
12511243 Ok ( ms)
12521244 }
12531245}
@@ -1276,13 +1268,12 @@ mod tests {
12761268 use bitcoin:: taproot:: TapLeafHash ;
12771269 use sync:: Arc ;
12781270
1279- use super :: { Miniscript , ScriptContext , Segwitv0 , Tap } ;
1271+ use super :: * ;
12801272 use crate :: miniscript:: { types, Terminal } ;
12811273 use crate :: policy:: Liftable ;
1282- use crate :: prelude:: * ;
12831274 use crate :: test_utils:: { StrKeyTranslator , StrXOnlyKeyTranslator } ;
12841275 use crate :: {
1285- hex_script, BareCtx , Error , ExtParams , Legacy , RelLockTime , Satisfier , ToPublicKey ,
1276+ hex_script, BareCtx , Error , Legacy , RelLockTime , Satisfier , ToPublicKey , ValidationError ,
12861277 } ;
12871278
12881279 type Segwitv0Script = Miniscript < bitcoin:: PublicKey , Segwitv0 > ;
@@ -1912,7 +1903,7 @@ mod tests {
19121903 // Test that parsing raw hash160 from string does not work without extra features
19131904 SegwitMs :: from_str ( ms_str) . unwrap_err ( ) ;
19141905 SegwitMs :: from_str_insane ( ms_str) . unwrap_err ( ) ;
1915- let ms = SegwitMs :: from_str_ext ( ms_str, & ExtParams :: allow_all ( ) ) . unwrap ( ) ;
1906+ let ms = SegwitMs :: from_str_with_validation_params ( ms_str, & ValidationParams :: MAX ) . unwrap ( ) ;
19161907
19171908 let script = ms. encode ( ) ;
19181909 // The same test, but parsing from script. Notice that unlike the previous "insane"
@@ -1951,7 +1942,7 @@ mod tests {
19511942 fn duplicate_keys ( ) {
19521943 // You cannot parse a Miniscript that has duplicate keys
19531944 let err = Miniscript :: < String , Segwitv0 > :: from_str ( "and_v(v:pk(A),pk(A))" ) . unwrap_err ( ) ;
1954- assert ! ( matches!( err, Error :: AnalysisError ( crate :: AnalysisError :: RepeatedPubkeys ) ) ) ;
1945+ assert ! ( matches!( err, Error :: Validation ( ValidationError :: DuplicateKeys ) ) ) ;
19551946
19561947 // ...though you can parse one with from_str_insane
19571948 let ok_insane =
@@ -1974,10 +1965,7 @@ mod tests {
19741965 "and_v(v:and_v(v:older(4194304),pk(A)),and_v(v:older(1),pk(B)))" ,
19751966 )
19761967 . unwrap_err ( ) ;
1977- assert ! ( matches!(
1978- err,
1979- Error :: AnalysisError ( crate :: AnalysisError :: HeightTimelockCombination )
1980- ) ) ;
1968+ assert ! ( matches!( err, Error :: Validation ( ValidationError :: MixedTimeLocks ) ) ) ;
19811969
19821970 // Though you can in an or() rather than and()
19831971 let ok_or = Miniscript :: < String , Segwitv0 > :: from_str (
@@ -2001,6 +1989,15 @@ mod tests {
20011989 ok_insane. lift( ) . unwrap_err( ) ,
20021990 Error :: LiftError ( crate :: policy:: LiftError :: HeightTimelockCombination )
20031991 ) ) ;
1992+ // nor can it have sane rules applied to it
1993+ assert_eq ! (
1994+ ok_insane. validate( & ValidationParams :: SANE ) . unwrap_err( ) ,
1995+ ValidationError :: MixedTimeLocks ,
1996+ ) ;
1997+ assert_eq ! (
1998+ ok_insane. validate( & ValidationParams :: SANE ) . unwrap_err( ) ,
1999+ ValidationError :: MixedTimeLocks ,
2000+ ) ;
20042001 }
20052002
20062003 #[ test]
0 commit comments