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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! 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 bitcoin::hashes::Hash;
use elements::script::Builder;
use elements::{secp256k1_zkp, Transaction, TxOut};

use crate::expression::Tree;
use crate::interpreter::{self, Stack};
use crate::miniscript::context::ScriptContextError;
use crate::miniscript::lex::TokenIter;
use crate::miniscript::satisfy::Satisfaction;
use crate::miniscript::types::{Correctness, ExtData, Malleability};
use crate::policy::Liftable;
use crate::{policy, Error, ExtTranslator, MiniscriptKey, Satisfier, ToPublicKey, TranslateExt};

#[allow(unused_imports)]
mod arith;
mod csfs;
mod index_ops;
mod introspect_ops;
mod outputs_pref;
pub mod param;
mod tx_ver;

pub use arith::{Arith, EvalError, Expr, ExprInner};
pub use csfs::{CheckSigFromStack, CsfsKey, CsfsMsg};
pub use index_ops::IdxExpr;
pub use introspect_ops::{AssetExpr, CovOps, Spk, SpkExpr, ValueExpr};

pub use self::outputs_pref::LegacyOutputsPref;
pub use self::param::{ArgFromStr, CovExtArgs, ExtParam, NoExtParam};
pub use self::tx_ver::LegacyVerEq;

/// Failed to extract a token from an iterator of tokens
pub struct FromTokenIterError;

/// Extensions to elements-miniscript.
/// Refer to implementations(unimplemented!) for example and tutorials
pub trait Extension: Clone + Eq + Ord + fmt::Debug + fmt::Display + hash::Hash {
    /// 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;

    /// 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>;

    /// Validity rules for fragment in tap context
    fn tap_ctx_checks(&self) -> Result<(), ScriptContextError> {
        Ok(())
    }

    /// 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, FromTokenIterError>;
}

/// Support for parsing/serializing/satisfaction of extensions.
/// [`Extension`] trait reasons about extension in abstract way whereas
/// this trait reasons about the concrete data structures.
/// Extension is similar to [`MiniscriptKey`], whereas ParseableExt is similar to
/// [`ToPublicKey`].
//
// Come up with better name for this trait
pub trait ParseableExt:
    Extension + Clone + Eq + Ord + fmt::Debug + fmt::Display + hash::Hash
{
    /// 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, FromTokenIterError>;

    /// 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 Ok(true) when the ext fragment is satisfied.
    /// Output Ok(false) when the ext fragment is dissatisfied,
    /// Output Some(Err) when there is an error in interpreter value.
    fn evaluate(
        &self,
        stack: &mut Stack,
        txenv: Option<&TxEnv>,
    ) -> Result<bool, interpreter::Error>;

    /// Encoding of the current fragment
    fn push_to_builder(&self, builder: Builder) -> Builder;

    /// 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<Pk, 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<Pk, S>(&self, _sat: &S) -> Satisfaction
    where
        Pk: ToPublicKey,
        S: Satisfier<Pk>;
}

/// 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 Extension for NoExt {
    fn corr_prop(&self) -> Correctness {
        match *self {}
    }

    fn mall_prop(&self) -> Malleability {
        match *self {}
    }

    fn extra_prop(&self) -> ExtData {
        match *self {}
    }

    fn script_size(&self) -> usize {
        match *self {}
    }

    fn from_name_tree(_name: &str, _children: &[Tree<'_>]) -> Result<Self, FromTokenIterError> {
        // No extensions should not parse any extensions from String
        Err(FromTokenIterError)
    }

    fn segwit_ctx_checks(&self) -> Result<(), ScriptContextError> {
        Ok(())
    }
}

impl ParseableExt for NoExt {
    fn satisfy<Pk, S>(&self, _sat: &S) -> Satisfaction
    where
        Pk: ToPublicKey,
        S: Satisfier<Pk>,
    {
        match *self {}
    }

    fn dissatisfy<Pk, S>(&self, _sat: &S) -> Satisfaction
    where
        Pk: ToPublicKey,
        S: Satisfier<Pk>,
    {
        match *self {}
    }

    fn evaluate(
        &self,
        _stack: &mut Stack,
        _txenv: Option<&TxEnv>,
    ) -> Result<bool, interpreter::Error> {
        match *self {}
    }

    fn push_to_builder(&self, _builder: Builder) -> Builder {
        match *self {}
    }

    fn from_token_iter(_tokens: &mut TokenIter<'_>) -> Result<Self, FromTokenIterError> {
        // No extensions should return Err on parsing
        Err(FromTokenIterError)
    }
}

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<PExt, QExt> TranslateExt<PExt, QExt> for NoExt
where
    PExt: Extension,
    QExt: Extension,
{
    type Output = NoExt;

    fn translate_ext<T, E>(&self, _t: &mut T) -> Result<Self::Output, E>
    where
        T: ExtTranslator<PExt, QExt, E>,
    {
        match *self {}
    }
}

/// All known Extensions for elements-miniscript
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub enum CovenantExt<T: ExtParam> {
    /// Version Equal
    LegacyVerEq(LegacyVerEq),
    /// Outputs Prefix equal
    LegacyOutputsPref(LegacyOutputsPref),
    /// CSFS
    Csfs(CheckSigFromStack<T>),
    /// Arith opcodes
    Arith(Arith<T>),
    /// Cov opcodes
    Introspect(CovOps<T>),
}

// Apply the function on each arm
macro_rules! all_arms_fn {
    ($slf: ident, $trt: ident, $f: ident, $($args:ident, )* ) => {
        match $slf {
            CovenantExt::LegacyVerEq(v) => <LegacyVerEq as $trt>::$f(v, $($args, )*),
            CovenantExt::LegacyOutputsPref(p) => <LegacyOutputsPref as $trt>::$f(p, $($args, )*),
            CovenantExt::Csfs(csfs) => csfs.$f($($args, )*),
            CovenantExt::Arith(e) => e.$f($($args, )*),
            CovenantExt::Introspect(e) => e.$f($($args, )*),
        }
    };
}

// try all extensions one by one
// Self::$f(args)
macro_rules! try_from_arms {
    ( $trt: ident, $ext_arg: ident, $f: ident, $($args: ident, )*) => {
        if let Ok(v) = <LegacyVerEq as $trt>::$f($($args, )*) {
            Ok(CovenantExt::LegacyVerEq(v))
        } else if let Ok(v) = <LegacyOutputsPref as $trt>::$f($($args, )*) {
            Ok(CovenantExt::LegacyOutputsPref(v))
        } else if let Ok(v) = <CheckSigFromStack<$ext_arg> as $trt>::$f($($args, )*) {
            Ok(CovenantExt::Csfs(v))
        } else if let Ok(v) = <Arith<$ext_arg> as $trt>::$f($($args, )*) {
            Ok(CovenantExt::Arith(v))
        } else if let Ok(v) = <CovOps<$ext_arg> as $trt>::$f($($args, )*) {
            Ok(CovenantExt::Introspect(v))
        }else {
            Err(FromTokenIterError)
        }
    };
}

impl<T: ExtParam> Extension for CovenantExt<T> {
    fn corr_prop(&self) -> Correctness {
        all_arms_fn!(self, Extension, corr_prop,)
    }

    fn mall_prop(&self) -> Malleability {
        all_arms_fn!(self, Extension, mall_prop,)
    }

    fn extra_prop(&self) -> ExtData {
        all_arms_fn!(self, Extension, extra_prop,)
    }

    fn script_size(&self) -> usize {
        all_arms_fn!(self, Extension, script_size,)
    }

    fn from_name_tree(name: &str, children: &[Tree<'_>]) -> Result<Self, FromTokenIterError> {
        try_from_arms!(Extension, T, from_name_tree, name, children,)
    }

    fn segwit_ctx_checks(&self) -> Result<(), ScriptContextError> {
        all_arms_fn!(self, Extension, segwit_ctx_checks,)
    }
}

impl ParseableExt for CovenantExt<CovExtArgs> {
    fn satisfy<Pk, S>(&self, sat: &S) -> Satisfaction
    where
        Pk: ToPublicKey,
        S: Satisfier<Pk>,
    {
        all_arms_fn!(self, ParseableExt, satisfy, sat,)
    }

    fn dissatisfy<Pk, S>(&self, sat: &S) -> Satisfaction
    where
        Pk: ToPublicKey,
        S: Satisfier<Pk>,
    {
        all_arms_fn!(self, ParseableExt, dissatisfy, sat,)
    }

    fn evaluate(
        &self,
        stack: &mut Stack,
        txenv: Option<&TxEnv>,
    ) -> Result<bool, interpreter::Error> {
        all_arms_fn!(self, ParseableExt, evaluate, stack, txenv,)
    }

    fn push_to_builder(&self, builder: Builder) -> Builder {
        all_arms_fn!(self, ParseableExt, push_to_builder, builder,)
    }

    fn from_token_iter(tokens: &mut TokenIter<'_>) -> Result<Self, FromTokenIterError> {
        try_from_arms!(ParseableExt, CovExtArgs, from_token_iter, tokens,)
    }
}

impl<T: ExtParam> fmt::Display for CovenantExt<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CovenantExt::LegacyVerEq(v) => v.fmt(f),
            CovenantExt::LegacyOutputsPref(p) => p.fmt(f),
            CovenantExt::Csfs(c) => c.fmt(f),
            CovenantExt::Arith(e) => e.fmt(f),
            CovenantExt::Introspect(e) => e.fmt(f),
        }
    }
}

impl<PArg, QArg> TranslateExt<CovenantExt<PArg>, CovenantExt<QArg>> for CovenantExt<PArg>
where
    CovenantExt<PArg>: Extension,
    CovenantExt<QArg>: Extension,
    PArg: ExtParam,
    QArg: ExtParam,
{
    type Output = CovenantExt<QArg>;

    fn translate_ext<T, E>(&self, t: &mut T) -> Result<Self::Output, E>
    where
        T: ExtTranslator<CovenantExt<PArg>, CovenantExt<QArg>, E>,
    {
        t.ext(self)
    }
}

/// A satisfier for Covenant descriptors
/// that can do transaction introspection
/// 'tx denotes the lifetime of the transaction
/// being satisfied and 'ptx denotes the lifetime
/// of the previous transaction inputs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TxEnv<'tx, 'ptx> {
    /// The transaction being spent
    tx: &'tx Transaction,
    /// Spent utxos
    spent_utxos: &'ptx [TxOut],
    /// The input index being spent
    idx: usize,
}

impl<'tx, 'ptx> TxEnv<'tx, 'ptx> {
    /// Returns None when spent_utos.len() != tx.input.len()
    pub fn new(tx: &'tx Transaction, spent_utxos: &'ptx [TxOut], idx: usize) -> Option<Self> {
        if tx.input.len() != spent_utxos.len() {
            None
        } else {
            Some(Self {
                tx,
                spent_utxos,
                idx,
            })
        }
    }

    /// Obtains the tx
    pub fn tx(&self) -> &Transaction {
        self.tx
    }

    /// Obtains the spend utxos
    pub fn spent_utxos(&self) -> &[TxOut] {
        self.spent_utxos
    }

    /// Obtains the current input index
    pub fn idx(&self) -> usize {
        self.idx
    }
}

impl<'tx, 'ptx, Pk: ToPublicKey> Satisfier<Pk> for TxEnv<'tx, 'ptx> {
    fn lookup_tx(&self) -> Option<&elements::Transaction> {
        Some(self.tx)
    }

    fn lookup_spent_utxos(&self) -> Option<&[elements::TxOut]> {
        Some(self.spent_utxos)
    }

    fn lookup_curr_inp(&self) -> Option<usize> {
        Some(self.idx)
    }
}

/// API to check sig from fragment `price_oracle_1`
pub fn check_sig_price_oracle_1<C: secp256k1_zkp::Verification>(
    secp: &secp256k1_zkp::Secp256k1<C>,
    sig: &elements::secp256k1_zkp::schnorr::Signature,
    pk: &elements::secp256k1_zkp::XOnlyPublicKey,
    timestamp: u64,
    price: u64,
) -> bool {
    let mut buf = Vec::with_capacity(16);
    buf.extend(&timestamp.to_le_bytes());
    buf.extend(&price.to_le_bytes());
    let sha_msg = elements::hashes::sha256::Hash::hash(&buf);

    let msg = elements::secp256k1_zkp::Message::from_slice(&sha_msg[..]).unwrap();
    secp.verify_schnorr(sig, &msg, pk).is_ok()
}

/// [`secp256k1_zkp::Message`] for fragment `price_oracle_1`.
/// To be used in for signing with schnorr signatures.
pub fn sighash_msg_price_oracle_1(timestamp: u64, price: u64) -> secp256k1_zkp::Message {
    let mut buf = Vec::with_capacity(16);
    buf.extend(&timestamp.to_le_bytes());
    buf.extend(&price.to_le_bytes());
    let sha_msg = elements::hashes::sha256::Hash::hash(&buf);

    elements::secp256k1_zkp::Message::from_slice(&sha_msg[..]).unwrap()
}