pub enum Policy<Pk: MiniscriptKey> {
    Unsatisfiable,
    Trivial,
    Key(Pk),
    After(PackedLockTime),
    Older(Sequence),
    Sha256(Pk::Sha256),
    Hash256(Pk::Hash256),
    Ripemd160(Pk::Ripemd160),
    Hash160(Pk::Hash160),
    Threshold(usizeVec<Policy<Pk>>),
}
Expand description

Abstract policy which corresponds to the semantics of a Miniscript and which allows complex forms of analysis, e.g. filtering and normalization. Semantic policies store only hashes of keys to ensure that objects representing the same policy are lifted to the same Semantic, regardless of their choice of pk or pk_h nodes.

Variants

Unsatisfiable

Unsatisfiable

Trivial

Trivially satisfiable

Key(Pk)

Signature and public key matching a given hash is required

After(PackedLockTime)

An absolute locktime restriction

Older(Sequence)

A relative locktime restriction

Sha256(Pk::Sha256)

A SHA256 whose preimage must be provided to satisfy the descriptor

Hash256(Pk::Hash256)

A SHA256d whose preimage must be provided to satisfy the descriptor

Ripemd160(Pk::Ripemd160)

A RIPEMD160 whose preimage must be provided to satisfy the descriptor

Hash160(Pk::Hash160)

A HASH160 whose preimage must be provided to satisfy the descriptor

Threshold(usizeVec<Policy<Pk>>)

A set of descriptors, satisfactions must be provided for k of them

Implementations

Construct a Policy::After from n. Helper function equivalent to Policy::After(PackedLockTime::from(LockTime::from_consensus(n))).

Construct a Policy::Older from n. Helper function equivalent to Policy::Older(Sequence::from_consensus(n)).

Convert a policy using one kind of public key to another type of public key

Example
use miniscript::{bitcoin::{hashes::hash160, PublicKey}, policy::semantic::Policy, Translator};
use miniscript::translate_hash_fail;
use std::str::FromStr;
use std::collections::HashMap;
let alice_pk = "02c79ef3ede6d14f72a00d0e49b4becfb152197b64c0707425c4f231df29500ee7";
let bob_pk = "03d008a849fbf474bd17e9d2c1a827077a468150e58221582ec3410ab309f5afe4";
let placeholder_policy = Policy::<String>::from_str("and(pk(alice_pk),pk(bob_pk))").unwrap();

// Information to translator abstract String type keys to concrete bitcoin::PublicKey.
// In practice, wallets would map from String key names to BIP32 keys
struct StrPkTranslator {
    pk_map: HashMap<String, bitcoin::PublicKey>
}

// If we also wanted to provide mapping of other associated types(sha256, older etc),
// we would use the general Translator Trait.
impl Translator<String, bitcoin::PublicKey, ()> for StrPkTranslator {
    fn pk(&mut self, pk: &String) -> Result<bitcoin::PublicKey, ()> {
        self.pk_map.get(pk).copied().ok_or(()) // Dummy Err
    }

   // Handy macro for failing if we encounter any other fragment.
   // also see translate_hash_clone! for cloning instead of failing
    translate_hash_fail!(String, bitcoin::PublicKey, ());
}

let mut pk_map = HashMap::new();
pk_map.insert(String::from("alice_pk"), bitcoin::PublicKey::from_str(alice_pk).unwrap());
pk_map.insert(String::from("bob_pk"), bitcoin::PublicKey::from_str(bob_pk).unwrap());
let mut t = StrPkTranslator { pk_map: pk_map };

let real_policy = placeholder_policy.translate_pk(&mut t).unwrap();

let expected_policy = Policy::from_str(&format!("and(pk({}),pk({}))", alice_pk, bob_pk)).unwrap();
assert_eq!(real_policy, expected_policy);

This function computes whether the current policy entails the second one. A |- B means every satisfaction of A is also a satisfaction of B. This implementation will run slow for larger policies but should be sufficient for most practical policies.

Flatten out trees of Ands and Ors; eliminate Trivial and Unsatisfiables. Does not reorder any branches; use .sort.

Helper function to detect a true/trivial policy This function only checks whether the policy is Policy::Trivial For checking if the normalized form is trivial, the caller is expected to normalize the policy first.

Helper function to detect a false/unsatisfiable policy This function only checks whether the policy is Policy::Unsatisfiable For checking if the normalized form is unsatisfiable, the caller is expected to normalize the policy first.

Returns a list of all relative timelocks, not including 0, which appear in the policy

Returns a list of all absolute timelocks, not including 0, which appear in the policy

Filter a policy by eliminating relative timelock constraints that are not satisfied at the given age.

Filter a policy by eliminating absolute timelock constraints that are not satisfied at the given n (n OP_CHECKLOCKTIMEVERIFY).

Count the number of public keys and keyhashes referenced in a policy. Duplicate keys will be double-counted.

Count the minimum number of public keys for which signatures could be used to satisfy the policy. Returns None if the policy is not satisfiable.

“Sort” a policy to bring it into a canonical form to allow comparisons. Does not allow policies to be compared for functional equivalence; in general this appears to require Gröbner basis techniques that are not implemented.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Run a predicate on every key in the descriptor, returning whether the predicate returned true for every key Read more
Run a predicate on every key in the descriptor, returning whether the predicate returned true for any key Read more
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
Extract a structure from Tree representation
Convert the object into an abstract policy
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.