Skip to content

Commit 8a78025

Browse files
committed
miniscript: add constructors for all the terminals
All of the terminals in Miniscript can be constructed infallibly. Add constructors for this so we don't need to go through Miniscript::from_ast and deal with an impossible-to-hit error path.
1 parent db7b25c commit 8a78025

File tree

1 file changed

+113
-1
lines changed

1 file changed

+113
-1
lines changed

src/miniscript/mod.rs

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ mod private {
5656
pub use crate::miniscript::context::ScriptContext;
5757
use crate::miniscript::types;
5858
use crate::prelude::sync::Arc;
59-
use crate::{Error, MiniscriptKey, Terminal, MAX_RECURSION_DEPTH};
59+
use crate::{AbsLockTime, Error, MiniscriptKey, RelLockTime, Terminal, MAX_RECURSION_DEPTH};
6060

6161
/// The top-level miniscript abstract syntax tree (AST).
6262
pub struct Miniscript<Pk: MiniscriptKey, Ctx: ScriptContext> {
@@ -157,6 +157,118 @@ mod private {
157157
phantom: PhantomData,
158158
};
159159

160+
/// The `pk` combinator, which is an alias for `c:pk_k`.
161+
pub fn pk(pk: Pk) -> Self {
162+
let inner = Arc::new(Self::pk_k(pk));
163+
Self {
164+
ty: types::Type::cast_check(inner.ty).unwrap(),
165+
ext: types::extra_props::ExtData::cast_check(inner.ext),
166+
node: Terminal::Check(inner),
167+
phantom: PhantomData,
168+
}
169+
}
170+
171+
/// The `pkh` combinator, which is an alias for `c:pk_h`.
172+
pub fn pkh(pk: Pk) -> Self {
173+
let inner = Arc::new(Self::pk_h(pk));
174+
Self {
175+
ty: types::Type::cast_check(inner.ty).unwrap(),
176+
ext: types::extra_props::ExtData::cast_check(inner.ext),
177+
node: Terminal::Check(inner),
178+
phantom: PhantomData,
179+
}
180+
}
181+
182+
/// The `pk_k` combinator.
183+
pub fn pk_k(pk: Pk) -> Self {
184+
Self {
185+
node: Terminal::PkK(pk),
186+
ty: types::Type::pk_k(),
187+
ext: types::extra_props::ExtData::pk_k::<Ctx>(),
188+
phantom: PhantomData,
189+
}
190+
}
191+
192+
/// The `pk_h` combinator.
193+
pub fn pk_h(pk: Pk) -> Self {
194+
Self {
195+
node: Terminal::PkH(pk),
196+
ty: types::Type::pk_h(),
197+
ext: types::extra_props::ExtData::pk_h::<Ctx>(),
198+
phantom: PhantomData,
199+
}
200+
}
201+
202+
/// The `expr_raw_pkh` combinator.
203+
pub fn expr_raw_pkh(hash: bitcoin::hashes::hash160::Hash) -> Self {
204+
Self {
205+
node: Terminal::RawPkH(hash),
206+
ty: types::Type::pk_h(),
207+
ext: types::extra_props::ExtData::pk_h::<Ctx>(),
208+
phantom: PhantomData,
209+
}
210+
}
211+
212+
/// The `after` combinator.
213+
pub fn after(time: AbsLockTime) -> Self {
214+
Self {
215+
node: Terminal::After(time),
216+
ty: types::Type::time(),
217+
ext: types::extra_props::ExtData::after(time),
218+
phantom: PhantomData,
219+
}
220+
}
221+
222+
/// The `older` combinator.
223+
pub fn older(time: RelLockTime) -> Self {
224+
Self {
225+
node: Terminal::Older(time),
226+
ty: types::Type::time(),
227+
ext: types::extra_props::ExtData::older(time),
228+
phantom: PhantomData,
229+
}
230+
}
231+
232+
/// The `sha256` combinator.
233+
pub const fn sha256(hash: Pk::Sha256) -> Self {
234+
Self {
235+
node: Terminal::Sha256(hash),
236+
ty: types::Type::hash(),
237+
ext: types::extra_props::ExtData::sha256(),
238+
phantom: PhantomData,
239+
}
240+
}
241+
242+
/// The `hash256` combinator.
243+
pub const fn hash256(hash: Pk::Hash256) -> Self {
244+
Self {
245+
node: Terminal::Hash256(hash),
246+
ty: types::Type::hash(),
247+
ext: types::extra_props::ExtData::hash256(),
248+
phantom: PhantomData,
249+
}
250+
}
251+
252+
/// The `ripemd160` combinator.
253+
pub const fn ripemd160(hash: Pk::Ripemd160) -> Self {
254+
Self {
255+
node: Terminal::Ripemd160(hash),
256+
ty: types::Type::hash(),
257+
ext: types::extra_props::ExtData::ripemd160(),
258+
phantom: PhantomData,
259+
}
260+
}
261+
262+
/// The `hash160` combinator.
263+
pub const fn hash160(hash: Pk::Hash160) -> Self {
264+
Self {
265+
node: Terminal::Hash160(hash),
266+
ty: types::Type::hash(),
267+
ext: types::extra_props::ExtData::hash160(),
268+
phantom: PhantomData,
269+
}
270+
}
271+
160272
/// Add type information(Type and Extdata) to Miniscript based on
161273
/// `AstElem` fragment. Dependent on display and clone because of Error
162274
/// Display code of type_check.

0 commit comments

Comments
 (0)