@@ -57,7 +57,7 @@ pub struct Miniscript<Pk: MiniscriptKey, Ctx: ScriptContext> {
5757 ///Additional information helpful for extra analysis.
5858 pub ext : types:: extra_props:: ExtData ,
5959 /// Context PhantomData. Only accessible inside this crate
60- pub ( crate ) phantom : PhantomData < Ctx > ,
60+ phantom : PhantomData < Ctx > ,
6161}
6262
6363/// `PartialOrd` of `Miniscript` must depend only on node and not the type information.
@@ -116,6 +116,24 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
116116 phantom : PhantomData ,
117117 } )
118118 }
119+
120+ /// Create a new `Miniscript` from a `Terminal` node and a `Type` annotation
121+ /// This does not check the typing rules. The user is responsible for ensuring
122+ /// that the type provided is correct.
123+ ///
124+ /// You should almost always use `Miniscript::from_ast` instead of this function.
125+ pub fn from_components_unchecked (
126+ node : Terminal < Pk , Ctx > ,
127+ ty : types:: Type ,
128+ ext : types:: extra_props:: ExtData ,
129+ ) -> Miniscript < Pk , Ctx > {
130+ Miniscript {
131+ node,
132+ ty,
133+ ext,
134+ phantom : PhantomData ,
135+ }
136+ }
119137}
120138
121139impl < Pk : MiniscriptKey , Ctx : ScriptContext > fmt:: Display for Miniscript < Pk , Ctx > {
@@ -311,15 +329,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
311329 T : Translator < Pk , Q , FuncError > ,
312330 {
313331 let inner = self . node . real_translate_pk ( t) ?;
314- let ms = Miniscript {
315- //directly copying the type and ext is safe because translating public
316- //key should not change any properties
317- ty : self . ty ,
318- ext : self . ext ,
319- node : inner,
320- phantom : PhantomData ,
321- } ;
322- Ok ( ms)
332+ Ok ( Miniscript :: from_ast ( inner) . expect ( "This will be removed in the next commit" ) )
323333 }
324334}
325335
@@ -428,12 +438,7 @@ impl_from_tree!(
428438 /// should not be called directly; rather go through the descriptor API.
429439 fn from_tree( top: & expression:: Tree ) -> Result <Miniscript <Pk , Ctx >, Error > {
430440 let inner: Terminal <Pk , Ctx > = expression:: FromTree :: from_tree( top) ?;
431- Ok ( Miniscript {
432- ty: Type :: type_check( & inner, |_| None ) ?,
433- ext: ExtData :: type_check( & inner, |_| None ) ?,
434- node: inner,
435- phantom: PhantomData ,
436- } )
441+ Miniscript :: from_ast( inner)
437442 }
438443) ;
439444
@@ -663,30 +668,22 @@ mod tests {
663668 . unwrap ( ) ;
664669 let hash = hash160:: Hash :: from_byte_array ( [ 17 ; 20 ] ) ;
665670
666- let pkk_ms: Miniscript < String , Segwitv0 > = Miniscript {
667- node : Terminal :: Check ( Arc :: new ( Miniscript {
668- node : Terminal :: PkK ( String :: from ( "" ) ) ,
669- ty : Type :: from_pk_k :: < Segwitv0 > ( ) ,
670- ext : types:: extra_props:: ExtData :: from_pk_k :: < Segwitv0 > ( ) ,
671- phantom : PhantomData ,
672- } ) ) ,
673- ty : Type :: cast_check ( Type :: from_pk_k :: < Segwitv0 > ( ) ) . unwrap ( ) ,
674- ext : ExtData :: cast_check ( ExtData :: from_pk_k :: < Segwitv0 > ( ) ) . unwrap ( ) ,
671+ let pk_node = Terminal :: Check ( Arc :: new ( Miniscript {
672+ node : Terminal :: PkK ( String :: from ( "" ) ) ,
673+ ty : Type :: from_pk_k :: < Segwitv0 > ( ) ,
674+ ext : types:: extra_props:: ExtData :: from_pk_k :: < Segwitv0 > ( ) ,
675675 phantom : PhantomData ,
676- } ;
676+ } ) ) ;
677+ let pkk_ms: Miniscript < String , Segwitv0 > = Miniscript :: from_ast ( pk_node) . unwrap ( ) ;
677678 dummy_string_rtt ( pkk_ms, "[B/onduesm]c:[K/onduesm]pk_k(\" \" )" , "pk()" ) ;
678679
679- let pkh_ms: Miniscript < String , Segwitv0 > = Miniscript {
680- node : Terminal :: Check ( Arc :: new ( Miniscript {
681- node : Terminal :: PkH ( String :: from ( "" ) ) ,
682- ty : Type :: from_pk_h :: < Segwitv0 > ( ) ,
683- ext : types:: extra_props:: ExtData :: from_pk_h :: < Segwitv0 > ( ) ,
684- phantom : PhantomData ,
685- } ) ) ,
686- ty : Type :: cast_check ( Type :: from_pk_h :: < Segwitv0 > ( ) ) . unwrap ( ) ,
687- ext : ExtData :: cast_check ( ExtData :: from_pk_h :: < Segwitv0 > ( ) ) . unwrap ( ) ,
680+ let pkh_node = Terminal :: Check ( Arc :: new ( Miniscript {
681+ node : Terminal :: PkH ( String :: from ( "" ) ) ,
682+ ty : Type :: from_pk_h :: < Segwitv0 > ( ) ,
683+ ext : types:: extra_props:: ExtData :: from_pk_h :: < Segwitv0 > ( ) ,
688684 phantom : PhantomData ,
689- } ;
685+ } ) ) ;
686+ let pkh_ms: Miniscript < String , Segwitv0 > = Miniscript :: from_ast ( pkh_node) . unwrap ( ) ;
690687
691688 let expected_debug = "[B/nduesm]c:[K/nduesm]pk_h(\" \" )" ;
692689 let expected_display = "pkh()" ;
@@ -701,17 +698,13 @@ mod tests {
701698 assert_eq ! ( display, expected) ;
702699 }
703700
704- let pkk_ms: Segwitv0Script = Miniscript {
705- node : Terminal :: Check ( Arc :: new ( Miniscript {
706- node : Terminal :: PkK ( pk) ,
707- ty : Type :: from_pk_k :: < Segwitv0 > ( ) ,
708- ext : types:: extra_props:: ExtData :: from_pk_k :: < Segwitv0 > ( ) ,
709- phantom : PhantomData ,
710- } ) ) ,
711- ty : Type :: cast_check ( Type :: from_pk_k :: < Segwitv0 > ( ) ) . unwrap ( ) ,
712- ext : ExtData :: cast_check ( ExtData :: from_pk_k :: < Segwitv0 > ( ) ) . unwrap ( ) ,
701+ let pkk_node = Terminal :: Check ( Arc :: new ( Miniscript {
702+ node : Terminal :: PkK ( pk) ,
703+ ty : Type :: from_pk_k :: < Segwitv0 > ( ) ,
704+ ext : types:: extra_props:: ExtData :: from_pk_k :: < Segwitv0 > ( ) ,
713705 phantom : PhantomData ,
714- } ;
706+ } ) ) ;
707+ let pkk_ms: Segwitv0Script = Miniscript :: from_ast ( pkk_node) . unwrap ( ) ;
715708
716709 script_rtt (
717710 pkk_ms,
0 commit comments