Skip to content

Commit fa2e8b4

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

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
@@ -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

121139
impl<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,

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)