@@ -57,7 +57,7 @@ pub struct Miniscript<Pk: MiniscriptKey, Ctx: ScriptContext> {
57
57
///Additional information helpful for extra analysis.
58
58
pub ext : types:: extra_props:: ExtData ,
59
59
/// Context PhantomData. Only accessible inside this crate
60
- pub ( crate ) phantom : PhantomData < Ctx > ,
60
+ phantom : PhantomData < Ctx > ,
61
61
}
62
62
63
63
/// `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> {
116
116
phantom : PhantomData ,
117
117
} )
118
118
}
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
+ }
119
137
}
120
138
121
139
impl < Pk : MiniscriptKey , Ctx : ScriptContext > fmt:: Display for Miniscript < Pk , Ctx > {
@@ -311,15 +329,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
311
329
T : Translator < Pk , Q , FuncError > ,
312
330
{
313
331
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" ) )
323
333
}
324
334
}
325
335
@@ -428,12 +438,7 @@ impl_from_tree!(
428
438
/// should not be called directly; rather go through the descriptor API.
429
439
fn from_tree( top: & expression:: Tree ) -> Result <Miniscript <Pk , Ctx >, Error > {
430
440
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)
437
442
}
438
443
) ;
439
444
@@ -663,30 +668,22 @@ mod tests {
663
668
. unwrap ( ) ;
664
669
let hash = hash160:: Hash :: from_byte_array ( [ 17 ; 20 ] ) ;
665
670
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 > ( ) ,
675
675
phantom : PhantomData ,
676
- } ;
676
+ } ) ) ;
677
+ let pkk_ms: Miniscript < String , Segwitv0 > = Miniscript :: from_ast ( pk_node) . unwrap ( ) ;
677
678
dummy_string_rtt ( pkk_ms, "[B/onduesm]c:[K/onduesm]pk_k(\" \" )" , "pk()" ) ;
678
679
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 > ( ) ,
688
684
phantom : PhantomData ,
689
- } ;
685
+ } ) ) ;
686
+ let pkh_ms: Miniscript < String , Segwitv0 > = Miniscript :: from_ast ( pkh_node) . unwrap ( ) ;
690
687
691
688
let expected_debug = "[B/nduesm]c:[K/nduesm]pk_h(\" \" )" ;
692
689
let expected_display = "pkh()" ;
@@ -701,17 +698,13 @@ mod tests {
701
698
assert_eq ! ( display, expected) ;
702
699
}
703
700
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 > ( ) ,
713
705
phantom : PhantomData ,
714
- } ;
706
+ } ) ) ;
707
+ let pkk_ms: Segwitv0Script = Miniscript :: from_ast ( pkk_node) . unwrap ( ) ;
715
708
716
709
script_rtt (
717
710
pkk_ms,
0 commit comments