Skip to content

Commit c907ee0

Browse files
committed
Cleanup Miniscript constructors
Miniscript should only be called with two constructors. from_ast and from_components_unchecked
1 parent a0648b3 commit c907ee0

File tree

2 files changed

+46
-65
lines changed

2 files changed

+46
-65
lines changed

src/miniscript/mod.rs

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub struct Miniscript<Pk: MiniscriptKey, Ctx: ScriptContext> {
5858
///Additional information helpful for extra analysis.
5959
pub ext: types::extra_props::ExtData,
6060
/// Context PhantomData. Only accessible inside this crate
61-
pub(crate) phantom: PhantomData<Ctx>,
61+
phantom: PhantomData<Ctx>,
6262
}
6363

6464
/// `PartialOrd` of `Miniscript` must depend only on node and not the type information.
@@ -117,6 +117,24 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
117117
phantom: PhantomData,
118118
})
119119
}
120+
121+
/// Create a new `Miniscript` from a `Terminal` node and a `Type` annotation
122+
/// This does not check the typing rules. The user is responsible for ensuring
123+
/// that the type provided is correct.
124+
///
125+
/// You should almost always use `Miniscript::from_ast` instead of this function.
126+
pub fn from_components_unchecked(
127+
node: Terminal<Pk, Ctx>,
128+
ty: types::Type,
129+
ext: types::extra_props::ExtData,
130+
) -> Miniscript<Pk, Ctx> {
131+
Miniscript {
132+
node,
133+
ty,
134+
ext,
135+
phantom: PhantomData,
136+
}
137+
}
120138
}
121139

122140
impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Display for Miniscript<Pk, Ctx> {
@@ -318,15 +336,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
318336
T: Translator<Pk, Q, FuncError>,
319337
{
320338
let inner = self.node.real_translate_pk(t)?;
321-
let ms = Miniscript {
322-
//directly copying the type and ext is safe because translating public
323-
//key should not change any properties
324-
ty: self.ty,
325-
ext: self.ext,
326-
node: inner,
327-
phantom: PhantomData,
328-
};
329-
Ok(ms)
339+
Ok(Miniscript::from_ast(inner).expect("This will be removed in the next commit"))
330340
}
331341
}
332342

@@ -435,12 +445,7 @@ impl_from_tree!(
435445
/// should not be called directly; rather go through the descriptor API.
436446
fn from_tree(top: &expression::Tree) -> Result<Miniscript<Pk, Ctx>, Error> {
437447
let inner: Terminal<Pk, Ctx> = expression::FromTree::from_tree(top)?;
438-
Ok(Miniscript {
439-
ty: Type::type_check(&inner, |_| None)?,
440-
ext: ExtData::type_check(&inner, |_| None)?,
441-
node: inner,
442-
phantom: PhantomData,
443-
})
448+
Miniscript::from_ast(inner)
444449
}
445450
);
446451

@@ -659,30 +664,22 @@ mod tests {
659664
.unwrap();
660665
let hash = hash160::Hash::from_inner([17; 20]);
661666

662-
let pkk_ms: Miniscript<String, Segwitv0> = Miniscript {
663-
node: Terminal::Check(Arc::new(Miniscript {
664-
node: Terminal::PkK(String::from("")),
665-
ty: Type::from_pk_k::<Segwitv0>(),
666-
ext: types::extra_props::ExtData::from_pk_k::<Segwitv0>(),
667-
phantom: PhantomData,
668-
})),
669-
ty: Type::cast_check(Type::from_pk_k::<Segwitv0>()).unwrap(),
670-
ext: ExtData::cast_check(ExtData::from_pk_k::<Segwitv0>()).unwrap(),
667+
let pk_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>(),
671671
phantom: PhantomData,
672-
};
672+
}));
673+
let pkk_ms: Miniscript<String, Segwitv0> = Miniscript::from_ast(pk_node).unwrap();
673674
dummy_string_rtt(pkk_ms, "[B/onduesm]c:[K/onduesm]pk_k(\"\")", "pk()");
674675

675-
let pkh_ms: Miniscript<String, Segwitv0> = Miniscript {
676-
node: Terminal::Check(Arc::new(Miniscript {
677-
node: Terminal::PkH(String::from("")),
678-
ty: Type::from_pk_h::<Segwitv0>(),
679-
ext: types::extra_props::ExtData::from_pk_h::<Segwitv0>(),
680-
phantom: PhantomData,
681-
})),
682-
ty: Type::cast_check(Type::from_pk_h::<Segwitv0>()).unwrap(),
683-
ext: ExtData::cast_check(ExtData::from_pk_h::<Segwitv0>()).unwrap(),
676+
let pkh_node = Terminal::Check(Arc::new(Miniscript {
677+
node: Terminal::PkH(String::from("")),
678+
ty: Type::from_pk_h::<Segwitv0>(),
679+
ext: types::extra_props::ExtData::from_pk_h::<Segwitv0>(),
684680
phantom: PhantomData,
685-
};
681+
}));
682+
let pkh_ms: Miniscript<String, Segwitv0> = Miniscript::from_ast(pkh_node).unwrap();
686683

687684
let expected_debug = "[B/nduesm]c:[K/nduesm]pk_h(\"\")";
688685
let expected_display = "pkh()";
@@ -697,17 +694,13 @@ mod tests {
697694
assert_eq!(display, expected);
698695
}
699696

700-
let pkk_ms: Segwitv0Script = Miniscript {
701-
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>(),
705-
phantom: PhantomData,
706-
})),
707-
ty: Type::cast_check(Type::from_pk_k::<Segwitv0>()).unwrap(),
708-
ext: ExtData::cast_check(ExtData::from_pk_k::<Segwitv0>()).unwrap(),
697+
let pkk_node = Terminal::Check(Arc::new(Miniscript {
698+
node: Terminal::PkK(pk),
699+
ty: Type::from_pk_k::<Segwitv0>(),
700+
ext: types::extra_props::ExtData::from_pk_k::<Segwitv0>(),
709701
phantom: PhantomData,
710-
};
702+
}));
703+
let pkk_ms: Segwitv0Script = Miniscript::from_ast(pkk_node).unwrap();
711704

712705
script_rtt(
713706
pkk_ms,

src/policy/compiler.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//!
88
99
use core::convert::From;
10-
use core::marker::PhantomData;
1110
use core::{cmp, f64, fmt, hash, mem};
1211
#[cfg(feature = "std")]
1312
use std::error;
@@ -496,12 +495,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> AstElemExt<Pk, Ctx> {
496495
let ext = types::ExtData::type_check(&ast, |_| None)?;
497496
let comp_ext_data = CompilerExtData::type_check(&ast, lookup_ext)?;
498497
Ok(AstElemExt {
499-
ms: Arc::new(Miniscript {
500-
ty,
501-
ext,
502-
node: ast,
503-
phantom: PhantomData,
504-
}),
498+
ms: Arc::new(Miniscript::from_components_unchecked(ast, ty, ext)),
505499
comp_ext_data,
506500
})
507501
}
@@ -524,12 +518,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> AstElemExt<Pk, Ctx> {
524518
let ext = types::ExtData::type_check(&ast, |_| None)?;
525519
let comp_ext_data = CompilerExtData::type_check(&ast, lookup_ext)?;
526520
Ok(AstElemExt {
527-
ms: Arc::new(Miniscript {
528-
ty,
529-
ext,
530-
node: ast,
531-
phantom: PhantomData,
532-
}),
521+
ms: Arc::new(Miniscript::from_components_unchecked(ast, ty, ext)),
533522
comp_ext_data,
534523
})
535524
}
@@ -547,12 +536,11 @@ struct Cast<Pk: MiniscriptKey, Ctx: ScriptContext> {
547536
impl<Pk: MiniscriptKey, Ctx: ScriptContext> Cast<Pk, Ctx> {
548537
fn cast(&self, ast: &AstElemExt<Pk, Ctx>) -> Result<AstElemExt<Pk, Ctx>, ErrorKind> {
549538
Ok(AstElemExt {
550-
ms: Arc::new(Miniscript {
551-
ty: (self.ast_type)(ast.ms.ty)?,
552-
ext: (self.ext_data)(ast.ms.ext)?,
553-
node: (self.node)(Arc::clone(&ast.ms)),
554-
phantom: PhantomData,
555-
}),
539+
ms: Arc::new(Miniscript::from_components_unchecked(
540+
(self.node)(Arc::clone(&ast.ms)),
541+
(self.ast_type)(ast.ms.ty)?,
542+
(self.ext_data)(ast.ms.ext)?,
543+
)),
556544
comp_ext_data: (self.comp_ext_data)(ast.comp_ext_data)?,
557545
})
558546
}

0 commit comments

Comments
 (0)