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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// Miniscript
// Written in 2018 by
//     Andrew Poelstra <apoelstra@wpsoftware.net>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//

//! # Script Policies
//!
//! Tools for representing Bitcoin scriptpubkeys as abstract spending policies.
//! These may be compiled to Miniscript, which contains extra information to
//! describe the exact representation as Bitcoin script.
//!
//! The format represents EC public keys abstractly to allow wallets to replace
//! these with BIP32 paths, pay-to-contract instructions, etc.
//!

#[cfg(feature="compiler")]
pub mod compiler;

use std::{cmp, fmt, mem};
use std::str::FromStr;

use bitcoin_hashes::hex::FromHex;
use bitcoin_hashes::sha256;

#[cfg(feature="compiler")]
use miniscript::Miniscript;
use Error;
use errstr;
use expression;

/// Script descriptor
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Policy<P> {
    /// A public key which must sign to satisfy the descriptor
    Key(P),
    /// A set of keys, signatures must be provided for `k` of them
    Multi(usize, Vec<P>),
    /// A SHA256 whose preimage must be provided to satisfy the descriptor
    Hash(sha256::Hash),
    /// A locktime restriction
    Time(u32),
    /// A set of descriptors, satisfactions must be provided for `k` of them
    Threshold(usize, Vec<Policy<P>>),
    /// A list of descriptors, all of which must be satisfied
    And(Box<Policy<P>>, Box<Policy<P>>),
    /// A pair of descriptors, one of which must be satisfied
    Or(Box<Policy<P>>, Box<Policy<P>>),
    /// Same as `Or`, but the second option is assumed to never be taken for costing purposes
    AsymmetricOr(Box<Policy<P>>, Box<Policy<P>>),
}

impl<P: Clone + fmt::Debug> Policy<P> {
    /// Compile the descriptor into an optimized `Miniscript` representation
    #[cfg(feature="compiler")]
    pub fn compile(&self) -> Miniscript<P> {
        let t = {
            let node = compiler::CompiledNode::from_policy(self);
            node.best_t(1.0, 0.0)
        };
        println!("offending {:?}", t.ast);
        Miniscript::from(t.ast)
    }

    /// Abstract the policy into an "abstract policy" which can be filtered and analyzed
    pub fn abstract_policy(&self) -> AbstractPolicy<P> {
        match *self {
            Policy::Key(ref p) => AbstractPolicy::Key(p.clone()),
            Policy::Multi(k, ref keys) => {
                AbstractPolicy::Threshold(
                    k,
                    keys
                        .iter()
                        .map(|key| AbstractPolicy::Key(key.clone()))
                        .collect(),
                )
            }
            Policy::Hash(hash) => AbstractPolicy::Hash(hash),
            Policy::Time(k) => AbstractPolicy::Time(k),
            Policy::Threshold(k, ref subs) => AbstractPolicy::Threshold(
                k,
                subs
                    .iter()
                    .map(|sub| sub.abstract_policy())
                    .collect(),
            ),
            Policy::And(ref left, ref right) => AbstractPolicy::And(
                Box::new(left.abstract_policy()),
                Box::new(right.abstract_policy()),
            ),
            Policy::Or(ref left, ref right) |
            Policy::AsymmetricOr(ref left, ref right) => AbstractPolicy::Or(
                Box::new(left.abstract_policy()),
                Box::new(right.abstract_policy()),
            ),
        }
    }
}

impl<P> Policy<P> {
    /// Convert a policy using abstract keys to one using specific keys
    pub fn translate<F, Q, E>(&self, mut translatefn: F) -> Result<Policy<Q>, E>
        where F: FnMut(&P) -> Result<Q, E>
    {
        match *self {
            Policy::Key(ref pk) => translatefn(pk).map(Policy::Key),
            Policy::Multi(k, ref pks) => {
                let new_pks: Result<Vec<Q>, _> = pks.iter().map(translatefn).collect();
                new_pks.map(|ok| Policy::Multi(k, ok))
            }
            Policy::Hash(ref h) => Ok(Policy::Hash(h.clone())),
            Policy::Time(n) => Ok(Policy::Time(n)),
            Policy::Threshold(k, ref subs) => {
                let new_subs: Result<Vec<Policy<Q>>, _> = subs.iter().map(
                    |sub| sub.translate(&mut translatefn)
                ).collect();
                new_subs.map(|ok| Policy::Threshold(k, ok))
            }
            Policy::And(ref left, ref right) => {
                Ok(Policy::And(
                    Box::new(left.translate(&mut translatefn)?),
                    Box::new(right.translate(translatefn)?),
                ))
            }
            Policy::Or(ref left, ref right) => {
                Ok(Policy::Or(
                    Box::new(left.translate(&mut translatefn)?),
                    Box::new(right.translate(translatefn)?),
                ))
            }
            Policy::AsymmetricOr(ref left, ref right) => {
                Ok(Policy::AsymmetricOr(
                    Box::new(left.translate(&mut translatefn)?),
                    Box::new(right.translate(translatefn)?),
                ))
            }
        }
    }
}

impl<P: fmt::Debug> fmt::Debug for Policy<P> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Policy::Key(ref pk) => write!(f, "pk({:?})", pk),
            Policy::Multi(k, ref pks) => {
                write!(f, "multi({}", k)?;
                for pk in pks {
                    write!(f, ",{:?},", pk)?;
                }
                f.write_str(")")
            }
            Policy::Hash(ref h) => write!(f, "hash({:x})", h),
            Policy::Time(n) => write!(f, "time({})", n),
            Policy::Threshold(k, ref subs) => {
                write!(f, "thres({}", k)?;
                for sub in subs {
                    write!(f, ",{:?}", sub)?;
                }
                f.write_str(")")
            }
            Policy::And(ref left, ref right) => write!(f, "and({:?},{:?})", left, right),
            Policy::Or(ref left, ref right) => write!(f, "or({:?},{:?})", left, right),
            Policy::AsymmetricOr(ref left, ref right) => write!(f, "aor({:?} {:?})", left, right),
        }
    }
}

impl<P: fmt::Display> fmt::Display for Policy<P> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Policy::Key(ref pk) => write!(f, "pk({})", pk),
            Policy::Multi(k, ref pks) => {
                write!(f, "multi({}", k)?;
                for pk in pks {
                    write!(f, ",{},", pk)?;
                }
                f.write_str(")")
            }
            Policy::Hash(ref h) => write!(f, "hash({:x})", h),
            Policy::Time(n) => write!(f, "time({})", n),
            Policy::Threshold(k, ref subs) => {
                write!(f, "thres({}", k)?;
                for sub in subs {
                    write!(f, ",{}", sub)?;
                }
                f.write_str(")")
            }
            Policy::And(ref left, ref right) => write!(f, "and({},{})", left, right),
            Policy::Or(ref left, ref right) => write!(f, "or({},{})", left, right),
            Policy::AsymmetricOr(ref left, ref right) => write!(f, "aor({},{})", left, right),
        }
    }
}

impl<P: FromStr> FromStr for Policy<P>
    where P::Err: ToString + fmt::Debug
{
    type Err = Error;
    fn from_str(s: &str) -> Result<Policy<P>, Error> {
        let tree = expression::Tree::from_str(s)?;
        expression::FromTree::from_tree(&tree)
    }
}

impl<P: FromStr> expression::FromTree for Policy<P>
    where P::Err: ToString + fmt::Debug
{
    fn from_tree(top: &expression::Tree) -> Result<Policy<P>, Error> {
        match (top.name, top.args.len() as u32) {
            ("pk", 1) => expression::terminal(
                &top.args[0],
                |pk| P::from_str(pk).map(Policy::Key)
            ),
            ("multi", nkeys) => {
                for arg in &top.args {
                    if !arg.args.is_empty() {
                        return Err(errstr(arg.args[0].name));
                    }
                }

                let thresh = expression::parse_num(top.args[0].name)?;
                if thresh >= nkeys {
                    return Err(errstr("higher threshold than there were keys in multi"));
                }

                let mut keys = Vec::with_capacity(top.args.len() - 1);
                for arg in &top.args[1..] {
                    match P::from_str(arg.name) {
                        Ok(pk) => keys.push(pk),
                        Err(e) => return Err(Error::Unexpected(e.to_string())),
                    }
                }
                Ok(Policy::Multi(thresh as usize, keys))
            }
            ("hash", 1) => {
                expression::terminal(
                    &top.args[0],
                    |x| sha256::Hash::from_hex(x).map(Policy::Hash)
                )
            }
            ("time", 1) => {
                expression::terminal(
                    &top.args[0],
                    |x| expression::parse_num(x).map(Policy::Time)
                )
            }
            ("thres", nsubs) => {
                if !top.args[0].args.is_empty() {
                    return Err(errstr(top.args[0].args[0].name));
                }

                let thresh = expression::parse_num(top.args[0].name)?;
                if thresh >= nsubs {
                    return Err(errstr(top.args[0].name));
                }

                let mut subs = Vec::with_capacity(top.args.len() - 1);
                for arg in &top.args[1..] {
                    subs.push(Policy::from_tree(arg)?);
                }
                Ok(Policy::Threshold(thresh as usize, subs))
            }
            ("and", 2) => {
                Ok(Policy::And(
                    Box::new(Policy::from_tree(&top.args[0])?),
                    Box::new(Policy::from_tree(&top.args[1])?),
                ))
            }
            ("or", 2) => {
                Ok(Policy::Or(
                    Box::new(Policy::from_tree(&top.args[0])?),
                    Box::new(Policy::from_tree(&top.args[1])?),
                ))
            }
            ("aor", 2) => {
                Ok(Policy::AsymmetricOr(
                    Box::new(Policy::from_tree(&top.args[0])?),
                    Box::new(Policy::from_tree(&top.args[1])?),
                ))
            }
            _ => Err(errstr(top.name))
        }
    }
}


/// An "abstract" script policy that does not distinguish between functionally
/// equivalent things. Designed to be filterable and analyzable, and to be
/// created from either concrete `Policy`s or even-concreter `Miniscript`s.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum AbstractPolicy<P> {
    /// No requirements for satisfaction
    True,
    /// A public key which must sign to satisfy the descriptor
    Key(P),
    /// A SHA256 whose preimage must be provided to satisfy the descriptor
    Hash(sha256::Hash),
    /// A locktime restriction
    Time(u32),
    /// A set of descriptors, satisfactions must be provided for `k` of them
    Threshold(usize, Vec<AbstractPolicy<P>>),
    /// A list of descriptors, all of which must be satisfied
    And(Box<AbstractPolicy<P>>, Box<AbstractPolicy<P>>),
    /// A pair of descriptors, one of which must be satisfied
    Or(Box<AbstractPolicy<P>>, Box<AbstractPolicy<P>>),
}

impl<P> AbstractPolicy<P> {
    /// Helper function to do the recursion in `timelocks`.
    fn real_timelocks(&self) -> Vec<u32> {
        match *self {
            AbstractPolicy::True |
            AbstractPolicy::Key(..) |
            AbstractPolicy::Hash(..) => vec![],
            AbstractPolicy::Time(t) => vec![t],
            AbstractPolicy::Threshold(_k, ref subs) => {
                subs.iter().fold(
                    vec![],
                    |mut acc, x| {
                        acc.extend(x.real_timelocks());
                        acc
                    },
                )
            },
            AbstractPolicy::And(ref l, ref r) | AbstractPolicy::Or(ref l, ref r) => {
                let mut ret = vec![];
                ret.extend(l.real_timelocks());
                ret.extend(r.real_timelocks());
                ret
            },
        }
    }

    /// Returns a list of all timelocks, not including 0, which appear in the policy
    pub fn timelocks(&self) -> Vec<u32> {
        let mut ret = self.real_timelocks();
        ret.sort();
        ret.dedup();
        ret
    }

    /// Filter an abstract policy by eliminating any timelock constraints.
    /// If, after filtering, the policy cannot be satisfied, this function
    /// returns `None`.
    pub fn before_time(mut self, time: u32) -> Option<AbstractPolicy<P>> {
        self = match self {
            AbstractPolicy::Time(t) => {
                if t > time {
                    return None;
                }
                AbstractPolicy::Time(t)
            }
            AbstractPolicy::Threshold(k, subs) => {
                let subs: Vec<_> = subs
                    .into_iter()
                    .filter_map(|sub| sub.before_time(time))
                    .collect();
                if k > subs.len() {
                    return None;
                }
                AbstractPolicy::Threshold(k, subs)
            },
            AbstractPolicy::And(x, y) => {
                match (x.before_time(time), y.before_time(time)) {
                    (Some(x), Some(y)) => AbstractPolicy::And(Box::new(x), Box::new(y)),
                    _ => return None,
                }
            }
            AbstractPolicy::Or(x, y) => {
                match (x.before_time(time), y.before_time(time)) {
                    (None, None) => return None,
                    (Some(x), None) | (None, Some(x)) => x,
                    (Some(x), Some(y)) => AbstractPolicy::Or(Box::new(x), Box::new(y)),
                }
            }
            x => x,
        };
        Some(self)
    }

    /// Count the number of public keys referenced in a policy. Note that duplicate keys
    /// will be double-counted.
    pub fn n_keys(&self) -> usize {
        match *self {
            AbstractPolicy::True => 0,
            AbstractPolicy::Key(..) => 1,
            AbstractPolicy::Hash(..) | AbstractPolicy::Time(..) => 0,
            AbstractPolicy::Threshold(_, ref subs) => {
                subs.iter().map(|sub| sub.n_keys()).sum::<usize>()
            }
            AbstractPolicy::And(ref x, ref y) | AbstractPolicy::Or(ref x, ref y) => {
                x.n_keys() + y.n_keys()
            }
        }
    }

    /// Count the minimum number of public keys for which signatures could be used
    /// to satisfy the policy.
    pub fn minimum_n_keys(&self) -> usize {
        match *self {
            AbstractPolicy::True => 0,
            AbstractPolicy::Key(..) => 1,
            AbstractPolicy::Hash(..) | AbstractPolicy::Time(..) => 0,
            AbstractPolicy::Threshold(k, ref subs) => {
                let mut sublens: Vec<usize> = subs.iter().map(|sub| sub.minimum_n_keys()).collect();
                sublens.sort();
                sublens[0..k].iter().cloned().sum::<usize>()
            }
            AbstractPolicy::And(ref x, ref y) => {
                x.minimum_n_keys() + y.minimum_n_keys()
            }
            AbstractPolicy::Or(ref x, ref y) => {
                cmp::min(x.minimum_n_keys(), y.minimum_n_keys())
            }
        }
    }
}

impl<P: Ord> AbstractPolicy<P> {
    /// "Sort" a policy to bring it into a canonical form to allow comparisons.
    /// This does **not** allow policies to be compared for functional equivalence;
    /// in general this appears to require Gröbner basis techniques that are not
    /// implemented.
    pub fn sort(self) -> AbstractPolicy<P> {
        match self {
            AbstractPolicy::And(mut x, mut y) => {
                if x > y {
                    mem::swap(&mut x, &mut y);
                }
                AbstractPolicy::Or(x, y)
            }
            AbstractPolicy::Or(mut x, mut y) => {
                if x > y {
                    mem::swap(&mut x, &mut y);
                }
                AbstractPolicy::Or(x, y)
            }
            AbstractPolicy::Threshold(k, mut subs) => {
                subs.sort();
                AbstractPolicy::Threshold(k, subs)
            }
            x => x,
        }
    }
}

#[cfg(test)]
mod tests {
    use bitcoin::PublicKey;
    use std::str::FromStr;

    #[cfg(feature = "compiler")]
    use secp256k1;
    #[cfg(feature = "compiler")]
    use bitcoin::blockdata::{opcodes, script};
    #[cfg(feature = "compiler")]
    use bitcoin::{Script, SigHashType};
    #[cfg(feature = "compiler")]
    use NO_HASHES;

    use super::*;

    #[cfg(feature = "compiler")]
    fn pubkeys_and_a_sig(n: usize) -> (Vec<PublicKey>, secp256k1::Signature) {
        let mut ret = Vec::with_capacity(n);
        let secp = secp256k1::Secp256k1::new();
        let mut sk = [0; 32];
        for i in 1..n+1 {
            sk[0] = i as u8;
            sk[1] = (i >> 8) as u8;
            sk[2] = (i >> 16) as u8;

            let pk = PublicKey {
                key: secp256k1::PublicKey::from_secret_key(
                    &secp,
                    &secp256k1::SecretKey::from_slice(&sk[..]).expect("secret key"),
                ),
                compressed: true,
            };
            ret.push(pk);
        }
        let sig = secp.sign(
            &secp256k1::Message::from_slice(&sk[..]).expect("secret key"),
            &secp256k1::SecretKey::from_slice(&sk[..]).expect("secret key"),
        );
        (ret, sig)
    }

    #[cfg(feature="compiler")]
    #[test]
    fn compile() {
        let (keys, sig) = pubkeys_and_a_sig(10);
        let policy: Policy<PublicKey> = Policy::Time(100);
        let desc = policy.compile();
        assert_eq!(desc.encode(), Script::from(vec![0x01, 0x64, 0xb2]));

        let policy = Policy::Key(keys[0].clone());
        let desc = policy.compile();
        assert_eq!(
            desc.encode(),
            script::Builder::new()
                .push_key(&keys[0])
                .push_opcode(opcodes::all::OP_CHECKSIG)
                .into_script()
        );

        // CSV reordering trick
        let policy = Policy::And(
            // nb the compiler will reorder this because it can avoid the DROP if it ends with the CSV
            Box::new(Policy::Time(10000)),
            Box::new(Policy::Multi(2, keys[5..8].to_owned())),
        );
        let desc = policy.compile();
        assert_eq!(
            desc.encode(),
            script::Builder::new()
                .push_opcode(opcodes::all::OP_PUSHNUM_2)
                .push_key(&keys[5])
                .push_key(&keys[6])
                .push_key(&keys[7])
                .push_opcode(opcodes::all::OP_PUSHNUM_3)
                .push_opcode(opcodes::all::OP_CHECKMULTISIGVERIFY)
                .push_int(10000)
                .push_opcode(opcodes::OP_CSV)
                .into_script()
        );

        // Liquid policy
        let policy = Policy::AsymmetricOr(
            Box::new(Policy::Multi(3, keys[0..5].to_owned())),
            Box::new(Policy::And(
                Box::new(Policy::Time(10000)),
                Box::new(Policy::Multi(2, keys[5..8].to_owned())),
            )),
        );
        let desc = policy.compile();
        assert_eq!(
            desc.encode(),
            script::Builder::new()
                .push_opcode(opcodes::all::OP_PUSHNUM_3)
                .push_key(&keys[0])
                .push_key(&keys[1])
                .push_key(&keys[2])
                .push_key(&keys[3])
                .push_key(&keys[4])
                .push_opcode(opcodes::all::OP_PUSHNUM_5)
                .push_opcode(opcodes::all::OP_CHECKMULTISIG)
                .push_opcode(opcodes::all::OP_IFDUP)
                .push_opcode(opcodes::all::OP_NOTIF)
                    .push_opcode(opcodes::all::OP_PUSHNUM_2)
                    .push_key(&keys[5])
                    .push_key(&keys[6])
                    .push_key(&keys[7])
                    .push_opcode(opcodes::all::OP_PUSHNUM_3)
                    .push_opcode(opcodes::all::OP_CHECKMULTISIGVERIFY)
                    .push_int(10000)
                    .push_opcode(opcodes::OP_CSV)
                .push_opcode(opcodes::all::OP_ENDIF)
                .into_script()
        );

        let mut abs = policy.abstract_policy();
        assert_eq!(abs.n_keys(), 8);
        assert_eq!(abs.minimum_n_keys(), 2);
        abs = abs.before_time(10000).unwrap();
        assert_eq!(abs.n_keys(), 8);
        assert_eq!(abs.minimum_n_keys(), 2);
        abs = abs.before_time(9999).unwrap();
        assert_eq!(abs.n_keys(), 5);
        assert_eq!(abs.minimum_n_keys(), 3);
        abs = abs.before_time(0).unwrap();
        assert_eq!(abs.n_keys(), 5);
        assert_eq!(abs.minimum_n_keys(), 3);

        assert_eq!(
            &desc.public_keys()[..],
            &keys[0..8]
        );

        let mut sigvec = sig.serialize_der();
        sigvec.push(1); // sighash all

        let badfn = |_: &PublicKey| None;
        let keyfn = |_: &PublicKey| Some((sig.clone(), SigHashType::All));

        let leftfn = |pk: &PublicKey| {
            for (n, target) in keys.iter().enumerate() {
                if pk == target && n < 5 {
                    return Some((sig.clone(), SigHashType::All));
                }
            }
            None
        };

        assert!(desc.satisfy(Some(&badfn), NO_HASHES, 0).is_err());
        assert!(desc.satisfy(Some(&keyfn), NO_HASHES, 0).is_ok());
        assert!(desc.satisfy(Some(&leftfn), NO_HASHES, 0).is_ok());

        assert_eq!(
            desc.satisfy(Some(&leftfn), NO_HASHES, 0).unwrap(),
            vec![
                // sat for left branch
                vec![],
                sigvec.clone(),
                sigvec.clone(),
                sigvec.clone(),
            ]
        );

        assert_eq!(
            desc.satisfy(Some(&keyfn), NO_HASHES, 10000).unwrap(),
            vec![
                // sat for right branch
                vec![],
                sigvec.clone(),
                sigvec.clone(),
                // dissat for left branch
                vec![],
                vec![],
                vec![],
                vec![],
            ]
        );
    }

    #[test]
    fn parse_descriptor() {
        assert!(Policy::<PublicKey>::from_str("(").is_err());
        assert!(Policy::<PublicKey>::from_str("(x()").is_err());
        assert!(Policy::<PublicKey>::from_str("(\u{7f}()3").is_err());
        assert!(Policy::<PublicKey>::from_str("pk()").is_err());

        assert!(Policy::<PublicKey>::from_str("pk(020000000000000000000000000000000000000000000000000000000000000002)").is_ok());
    }

    #[test]
    fn abstract_policy() {
        let policy = Policy::<String>::from_str("pk()").unwrap();
        let abs = policy.abstract_policy();
        assert_eq!(abs, AbstractPolicy::Key("".to_owned()));
        assert_eq!(abs.timelocks(), vec![]);
        assert_eq!(abs.clone().before_time(0), Some(abs.clone()));
        assert_eq!(abs.clone().before_time(10000), Some(abs.clone()));
        assert_eq!(abs.n_keys(), 1);
        assert_eq!(abs.minimum_n_keys(), 1);

        let policy = Policy::<String>::from_str("time(1000)").unwrap();
        let abs = policy.abstract_policy();
        assert_eq!(abs, AbstractPolicy::Time(1000));
        assert_eq!(abs.timelocks(), vec![1000]);
        assert_eq!(abs.clone().before_time(0), None);
        assert_eq!(abs.clone().before_time(999), None);
        assert_eq!(abs.clone().before_time(1000), Some(abs.clone()));
        assert_eq!(abs.clone().before_time(10000), Some(abs.clone()));
        assert_eq!(abs.n_keys(), 0);
        assert_eq!(abs.minimum_n_keys(), 0);

        let policy = Policy::<String>::from_str("or(pk(),time(1000))").unwrap();
        let abs = policy.abstract_policy();
        assert_eq!(
            abs,
            AbstractPolicy::Or(
                Box::new(AbstractPolicy::Key("".to_owned())),
                Box::new(AbstractPolicy::Time(1000)),
            )
        );
        assert_eq!(abs.timelocks(), vec![1000]);
        assert_eq!(
            abs.clone().before_time(0),
            Some(AbstractPolicy::Key("".to_owned()))
        );
        assert_eq!(
            abs.clone().before_time(999),
            Some(AbstractPolicy::Key("".to_owned()))
        );
        assert_eq!(abs.clone().before_time(1000), Some(abs.clone()));
        assert_eq!(abs.clone().before_time(10000), Some(abs.clone()));
        assert_eq!(abs.n_keys(), 1);
        assert_eq!(abs.minimum_n_keys(), 0);

        let policy = Policy::<String>::from_str("thres(\
            2,time(1000),time(10000),time(1000),time(2000),time(2000)\
        )").unwrap();
        let abs = policy.abstract_policy();
        assert_eq!(
            abs,
            AbstractPolicy::Threshold(2, vec![
                AbstractPolicy::Time(1000),
                AbstractPolicy::Time(10000),
                AbstractPolicy::Time(1000),
                AbstractPolicy::Time(2000),
                AbstractPolicy::Time(2000),
            ])
        );
        assert_eq!(abs.timelocks(), vec![1000, 2000, 10000]); //sorted and dedup'd
    }
}