Skip to content

Commit 0112298

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

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
@@ -69,7 +69,7 @@ pub struct Miniscript<Pk: MiniscriptKey, Ctx: ScriptContext> {
6969
///Additional information helpful for extra analysis.
7070
pub ext: types::extra_props::ExtData,
7171
/// Context PhantomData. Only accessible inside this crate
72-
pub(crate) phantom: PhantomData<Ctx>,
72+
phantom: PhantomData<Ctx>,
7373
}
7474

7575
/// `PartialOrd` of `Miniscript` must depend only on node and not the type information.
@@ -128,6 +128,24 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
128128
phantom: PhantomData,
129129
})
130130
}
131+
132+
/// Create a new `Miniscript` from a `Terminal` node and a `Type` annotation
133+
/// This does not check the typing rules. The user is responsible for ensuring
134+
/// that the type provided is correct.
135+
///
136+
/// You should almost always use `Miniscript::from_ast` instead of this function.
137+
pub fn from_components_unchecked(
138+
node: Terminal<Pk, Ctx>,
139+
ty: types::Type,
140+
ext: types::extra_props::ExtData,
141+
) -> Miniscript<Pk, Ctx> {
142+
Miniscript {
143+
node,
144+
ty,
145+
ext,
146+
phantom: PhantomData,
147+
}
148+
}
131149
}
132150

133151
impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Display for Miniscript<Pk, Ctx> {
@@ -329,15 +347,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
329347
T: Translator<Pk, Q, FuncError>,
330348
{
331349
let inner = self.node.real_translate_pk(t)?;
332-
let ms = Miniscript {
333-
//directly copying the type and ext is safe because translating public
334-
//key should not change any properties
335-
ty: self.ty,
336-
ext: self.ext,
337-
node: inner,
338-
phantom: PhantomData,
339-
};
340-
Ok(ms)
350+
Ok(Miniscript::from_ast(inner).expect("This will be removed in the next commit"))
341351
}
342352
}
343353

@@ -446,12 +456,7 @@ impl_from_tree!(
446456
/// should not be called directly; rather go through the descriptor API.
447457
fn from_tree(top: &expression::Tree) -> Result<Miniscript<Pk, Ctx>, Error> {
448458
let inner: Terminal<Pk, Ctx> = expression::FromTree::from_tree(top)?;
449-
Ok(Miniscript {
450-
ty: Type::type_check(&inner, |_| None)?,
451-
ext: ExtData::type_check(&inner, |_| None)?,
452-
node: inner,
453-
phantom: PhantomData,
454-
})
459+
Miniscript::from_ast(inner)
455460
}
456461
);
457462

@@ -670,30 +675,22 @@ mod tests {
670675
.unwrap();
671676
let hash = hash160::Hash::from_inner([17; 20]);
672677

673-
let pkk_ms: Miniscript<DummyKey, Segwitv0> = Miniscript {
674-
node: Terminal::Check(Arc::new(Miniscript {
675-
node: Terminal::PkK(DummyKey),
676-
ty: Type::from_pk_k::<Segwitv0>(),
677-
ext: types::extra_props::ExtData::from_pk_k::<Segwitv0>(),
678-
phantom: PhantomData,
679-
})),
680-
ty: Type::cast_check(Type::from_pk_k::<Segwitv0>()).unwrap(),
681-
ext: ExtData::cast_check(ExtData::from_pk_k::<Segwitv0>()).unwrap(),
678+
let pk_node = Terminal::Check(Arc::new(Miniscript {
679+
node: Terminal::PkK(DummyKey),
680+
ty: Type::from_pk_k::<Segwitv0>(),
681+
ext: types::extra_props::ExtData::from_pk_k::<Segwitv0>(),
682682
phantom: PhantomData,
683-
};
683+
}));
684+
let pkk_ms: Miniscript<DummyKey, Segwitv0> = Miniscript::from_ast(pk_node).unwrap();
684685
dummy_string_rtt(pkk_ms, "[B/onduesm]c:[K/onduesm]pk_k(DummyKey)", "pk()");
685686

686-
let pkh_ms: Miniscript<DummyKey, Segwitv0> = Miniscript {
687-
node: Terminal::Check(Arc::new(Miniscript {
688-
node: Terminal::PkH(DummyKey),
689-
ty: Type::from_pk_h::<Segwitv0>(),
690-
ext: types::extra_props::ExtData::from_pk_h::<Segwitv0>(),
691-
phantom: PhantomData,
692-
})),
693-
ty: Type::cast_check(Type::from_pk_h::<Segwitv0>()).unwrap(),
694-
ext: ExtData::cast_check(ExtData::from_pk_h::<Segwitv0>()).unwrap(),
687+
let pkh_node = Terminal::Check(Arc::new(Miniscript {
688+
node: Terminal::PkH(DummyKey),
689+
ty: Type::from_pk_h::<Segwitv0>(),
690+
ext: types::extra_props::ExtData::from_pk_h::<Segwitv0>(),
695691
phantom: PhantomData,
696-
};
692+
}));
693+
let pkh_ms: Miniscript<DummyKey, Segwitv0> = Miniscript::from_ast(pkh_node).unwrap();
697694

698695
let expected_debug = "[B/nduesm]c:[K/nduesm]pk_h(DummyKey)";
699696
let expected_display = "pkh()";
@@ -708,17 +705,13 @@ mod tests {
708705
assert_eq!(display, expected);
709706
}
710707

711-
let pkk_ms: Segwitv0Script = Miniscript {
712-
node: Terminal::Check(Arc::new(Miniscript {
713-
node: Terminal::PkK(pk),
714-
ty: Type::from_pk_k::<Segwitv0>(),
715-
ext: types::extra_props::ExtData::from_pk_k::<Segwitv0>(),
716-
phantom: PhantomData,
717-
})),
718-
ty: Type::cast_check(Type::from_pk_k::<Segwitv0>()).unwrap(),
719-
ext: ExtData::cast_check(ExtData::from_pk_k::<Segwitv0>()).unwrap(),
708+
let pkk_node = Terminal::Check(Arc::new(Miniscript {
709+
node: Terminal::PkK(pk),
710+
ty: Type::from_pk_k::<Segwitv0>(),
711+
ext: types::extra_props::ExtData::from_pk_k::<Segwitv0>(),
720712
phantom: PhantomData,
721-
};
713+
}));
714+
let pkk_ms: Segwitv0Script = Miniscript::from_ast(pkk_node).unwrap();
722715

723716
script_rtt(
724717
pkk_ms,

src/policy/compiler.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
//!
1919
2020
use core::convert::From;
21-
use core::marker::PhantomData;
2221
use core::{cmp, f64, fmt, hash, mem};
2322
#[cfg(feature = "std")]
2423
use std::error;
@@ -507,12 +506,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> AstElemExt<Pk, Ctx> {
507506
let ext = types::ExtData::type_check(&ast, |_| None)?;
508507
let comp_ext_data = CompilerExtData::type_check(&ast, lookup_ext)?;
509508
Ok(AstElemExt {
510-
ms: Arc::new(Miniscript {
511-
ty,
512-
ext,
513-
node: ast,
514-
phantom: PhantomData,
515-
}),
509+
ms: Arc::new(Miniscript::from_components_unchecked(ast, ty, ext)),
516510
comp_ext_data,
517511
})
518512
}
@@ -535,12 +529,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> AstElemExt<Pk, Ctx> {
535529
let ext = types::ExtData::type_check(&ast, |_| None)?;
536530
let comp_ext_data = CompilerExtData::type_check(&ast, lookup_ext)?;
537531
Ok(AstElemExt {
538-
ms: Arc::new(Miniscript {
539-
ty,
540-
ext,
541-
node: ast,
542-
phantom: PhantomData,
543-
}),
532+
ms: Arc::new(Miniscript::from_components_unchecked(ast, ty, ext)),
544533
comp_ext_data,
545534
})
546535
}
@@ -558,12 +547,11 @@ struct Cast<Pk: MiniscriptKey, Ctx: ScriptContext> {
558547
impl<Pk: MiniscriptKey, Ctx: ScriptContext> Cast<Pk, Ctx> {
559548
fn cast(&self, ast: &AstElemExt<Pk, Ctx>) -> Result<AstElemExt<Pk, Ctx>, ErrorKind> {
560549
Ok(AstElemExt {
561-
ms: Arc::new(Miniscript {
562-
ty: (self.ast_type)(ast.ms.ty)?,
563-
ext: (self.ext_data)(ast.ms.ext)?,
564-
node: (self.node)(Arc::clone(&ast.ms)),
565-
phantom: PhantomData,
566-
}),
550+
ms: Arc::new(Miniscript::from_components_unchecked(
551+
(self.node)(Arc::clone(&ast.ms)),
552+
(self.ast_type)(ast.ms.ty)?,
553+
(self.ext_data)(ast.ms.ext)?,
554+
)),
567555
comp_ext_data: (self.comp_ext_data)(ast.comp_ext_data)?,
568556
})
569557
}

0 commit comments

Comments
 (0)