-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmod.rs
350 lines (298 loc) · 9.88 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//! Extensions to elements-miniscript
//! Users should implement the [`Extension`] trait to extend miniscript to have newer leaf nodes
//! Look at examples for implementation of ver_eq fragment
use std::{fmt, hash};
use elements::script::Builder;
use policy;
use Error;
use MiniscriptKey;
use ToPublicKey;
use {ForEach, TranslatePk};
use {expression::Tree, policy::Liftable, Satisfier};
use miniscript::{
context::ScriptContextError,
lex::TokenIter,
satisfy::Satisfaction,
types::{Correctness, ExtData, Malleability},
};
use interpreter::{self, Stack};
mod outputs_pref;
mod tx_ver;
pub use self::outputs_pref::OutputsPref;
pub use self::tx_ver::VerEq;
/// Extensions to elements-miniscript.
/// Refer to implementations(unimplemented!) for example and tutorials
pub trait Extension<Pk: MiniscriptKey>:
Clone + Eq + Ord + fmt::Debug + fmt::Display + hash::Hash + Liftable<Pk>
{
/// Calculate the correctness property for the leaf fragment.
/// See miniscript reference for more info on different types
fn corr_prop(&self) -> Correctness;
/// Calculate the malleability property for the leaf fragment.
/// See miniscript reference for more info on different types
fn mall_prop(&self) -> Malleability;
/// Calculate the Extra properties property for the leaf fragment.
/// See current implementation for different fragments in extra_props.rs
fn extra_prop(&self) -> ExtData;
/// Produce a satisfaction for this from satisfier.
/// See satisfaction code in satisfy.rs for example
/// Note that the [`Satisfaction`] struct also covers the case when
/// satisfaction is impossible/unavailable
fn satisfy<S>(&self, _sat: &S) -> Satisfaction
where
Pk: ToPublicKey,
S: Satisfier<Pk>;
/// Produce a satisfaction for this from satisfier.
/// See satisfaction code in satisfy.rs for example
/// Note that the [`Satisfaction`] struct also covers the case when
/// dissatisfaction is impossible/unavailable
fn dissatisfy<S>(&self, _sat: &S) -> Satisfaction
where
Pk: ToPublicKey,
S: Satisfier<Pk>;
/// Check if the predicate holds for all keys
fn real_for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, _pred: &mut F) -> bool
where
Pk: 'a,
Pk::Hash: 'a;
/// Encoding of the current fragment
fn push_to_builder(&self, builder: Builder) -> Builder
where
Pk: ToPublicKey;
/// Get the script size of the current fragment
fn script_size(&self) -> usize;
/// Validity rules for fragment in segwit context
fn segwit_ctx_checks(&self) -> Result<(), ScriptContextError> {
Ok(())
}
//unimplemented: Add checks after we introduce Tap ctx
/// Parse the terminal from [`TokenIter`]. Implementers of this trait are responsible
/// for making sure tokens is mutated correctly. If parsing is not successful, the tokens
/// should not be consumed.
fn from_token_iter(_tokens: &mut TokenIter) -> Result<Self, ()>;
/// Create an instance of this object from a Tree with root name and children as
/// Vec<Tree>.
// Ideally, we would want a FromTree implementation here, but that is not possible
// as we would need to create a new Tree by removing wrappers from root.
fn from_name_tree(_name: &str, children: &[Tree]) -> Result<Self, ()>;
/// Interpreter support
/// Evaluate the fragment based on inputs from stack. If an implementation of this
/// is provided the user can use the interpreter API to parse scripts from blockchain
/// and check which constraints are satisfied
/// Output [`None`] when the ext fragment is dissatisfied, output Some(Err) when there is
/// an error in interpreter value. Finally, if the evaluation is successful output Some(Ok())
/// After taproot this should also access to the transaction data
fn evaluate<'intp, 'txin>(
&'intp self,
stack: &mut Stack<'txin>,
) -> Option<Result<(), interpreter::Error>>;
}
/// No Extensions for elements-miniscript
/// All the implementations for the this function are unreachable
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub enum NoExt {}
impl<Pk: MiniscriptKey> Extension<Pk> for NoExt {
fn corr_prop(&self) -> Correctness {
match *self {}
}
fn mall_prop(&self) -> Malleability {
match *self {}
}
fn extra_prop(&self) -> ExtData {
match *self {}
}
fn satisfy<S>(&self, _sat: &S) -> Satisfaction
where
Pk: ToPublicKey,
S: Satisfier<Pk>,
{
match *self {}
}
fn dissatisfy<S>(&self, _sat: &S) -> Satisfaction
where
Pk: ToPublicKey,
S: Satisfier<Pk>,
{
match *self {}
}
fn real_for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, _pred: &mut F) -> bool
where
Pk: 'a,
Pk::Hash: 'a,
{
match *self {}
}
fn push_to_builder(&self, _builder: Builder) -> Builder
where
Pk: ToPublicKey,
{
match *self {}
}
fn script_size(&self) -> usize {
match *self {}
}
fn from_token_iter(_tokens: &mut TokenIter) -> Result<Self, ()> {
// No extensions should return Err on parsing
Err(())
}
fn from_name_tree(_name: &str, _children: &[Tree]) -> Result<Self, ()> {
// No extensions should not parse any extensions from String
Err(())
}
fn evaluate<'intp, 'txin>(
&'intp self,
_stack: &mut Stack<'txin>,
) -> Option<Result<(), interpreter::Error>> {
match *self {}
}
}
impl<Pk: MiniscriptKey> Liftable<Pk> for NoExt {
fn lift(&self) -> Result<policy::Semantic<Pk>, Error> {
match *self {}
}
}
impl fmt::Display for NoExt {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {}
}
}
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for NoExt {
type Output = NoExt;
fn translate_pk<Fpk, Fpkh, E>(
&self,
mut _translatefpk: Fpk,
_translatefpkh: Fpkh,
) -> Result<Self::Output, E>
where
Fpk: FnMut(&P) -> Result<Q, E>,
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
Q: MiniscriptKey,
{
match *self {}
}
}
/// All known Extensions for elements-miniscript
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub enum CovenantExt {
/// Version Equal
VerEq(VerEq),
/// Outputs Prefix equal
OutputsPref(OutputsPref),
}
// Apply the function on each arm
macro_rules! all_arms_fn {
($slf: ident, $f: ident, $($args:ident, )* ) => {
match $slf {
CovenantExt::VerEq(v) => <VerEq as Extension<Pk>>::$f(v, $($args, )*),
CovenantExt::OutputsPref(p) => <OutputsPref as Extension<Pk>>::$f(p, $($args, )*),
}
};
}
// try all extensions one by one
// Self::$f(args)
macro_rules! try_from_arms {
($f: ident, $($args: ident, )*) => {
if let Ok(v) = <VerEq as Extension<Pk>>::$f($($args, )*) {
Ok(CovenantExt::VerEq(v))
} else if let Ok(v) = <OutputsPref as Extension<Pk>>::$f($($args, )*) {
Ok(CovenantExt::OutputsPref(v))
} else {
Err(())
}
};
}
impl<Pk> Extension<Pk> for CovenantExt
where
Pk: MiniscriptKey,
{
fn corr_prop(&self) -> Correctness {
all_arms_fn!(self, corr_prop,)
}
fn mall_prop(&self) -> Malleability {
all_arms_fn!(self, mall_prop,)
}
fn extra_prop(&self) -> ExtData {
all_arms_fn!(self, extra_prop,)
}
fn satisfy<S>(&self, sat: &S) -> Satisfaction
where
Pk: ToPublicKey,
S: Satisfier<Pk>,
{
all_arms_fn!(self, satisfy, sat,)
}
fn dissatisfy<S>(&self, sat: &S) -> Satisfaction
where
Pk: ToPublicKey,
S: Satisfier<Pk>,
{
all_arms_fn!(self, dissatisfy, sat,)
}
fn real_for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: &mut F) -> bool
where
Pk: 'a,
Pk::Hash: 'a,
{
all_arms_fn!(self, real_for_each_key, pred,)
}
fn push_to_builder(&self, builder: Builder) -> Builder
where
Pk: ToPublicKey,
{
all_arms_fn!(self, push_to_builder, builder,)
}
fn script_size(&self) -> usize {
all_arms_fn!(self, script_size,)
}
fn from_token_iter(tokens: &mut TokenIter) -> Result<Self, ()> {
try_from_arms!(from_token_iter, tokens,)
}
fn from_name_tree(name: &str, children: &[Tree]) -> Result<Self, ()> {
try_from_arms!(from_name_tree, name, children,)
}
fn evaluate<'intp, 'txin>(
&'intp self,
stack: &mut Stack<'txin>,
) -> Option<Result<(), interpreter::Error>> {
all_arms_fn!(self, evaluate, stack,)
}
}
impl fmt::Display for CovenantExt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CovenantExt::VerEq(v) => v.fmt(f),
CovenantExt::OutputsPref(p) => p.fmt(f),
}
}
}
impl<Pk: MiniscriptKey> Liftable<Pk> for CovenantExt {
fn lift(&self) -> Result<policy::Semantic<Pk>, Error> {
match self {
CovenantExt::VerEq(v) => v.lift(),
CovenantExt::OutputsPref(p) => p.lift(),
}
}
}
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for CovenantExt {
type Output = CovenantExt;
fn translate_pk<Fpk, Fpkh, E>(
&self,
translatefpk: Fpk,
translatefpkh: Fpkh,
) -> Result<Self::Output, E>
where
Fpk: FnMut(&P) -> Result<Q, E>,
Fpkh: FnMut(&P::Hash) -> Result<Q::Hash, E>,
Q: MiniscriptKey,
{
let ext = match self {
CovenantExt::VerEq(v) => {
CovenantExt::VerEq(v.translate_pk(translatefpk, translatefpkh)?)
}
CovenantExt::OutputsPref(p) => {
CovenantExt::OutputsPref(p.translate_pk(translatefpk, translatefpkh)?)
}
};
Ok(ext)
}
}