Trait DescriptorTrait

Source
pub trait DescriptorTrait<Pk: MiniscriptKey> {
    // Required methods
    fn sanity_check(&self) -> Result<(), Error>;
    fn address(&self, network: Network) -> Result<Address, Error>
       where Pk: ToPublicKey;
    fn script_pubkey(&self) -> Script
       where Pk: ToPublicKey;
    fn unsigned_script_sig(&self) -> Script
       where Pk: ToPublicKey;
    fn explicit_script(&self) -> Result<Script, Error>
       where Pk: ToPublicKey;
    fn get_satisfaction<S>(
        &self,
        satisfier: S,
    ) -> Result<(Vec<Vec<u8>>, Script), Error>
       where Pk: ToPublicKey,
             S: Satisfier<Pk>;
    fn get_satisfaction_mall<S>(
        &self,
        satisfier: S,
    ) -> Result<(Vec<Vec<u8>>, Script), Error>
       where Pk: ToPublicKey,
             S: Satisfier<Pk>;
    fn max_satisfaction_weight(&self) -> Result<usize, Error>;
    fn script_code(&self) -> Result<Script, Error>
       where Pk: ToPublicKey;

    // Provided method
    fn satisfy<S>(&self, txin: &mut TxIn, satisfier: S) -> Result<(), Error>
       where Pk: ToPublicKey,
             S: Satisfier<Pk> { ... }
}
Expand description

A general trait for Bitcoin descriptor. Offers function for witness cost estimation, script pubkey creation satisfaction using the Satisfier trait.

Required Methods§

Source

fn sanity_check(&self) -> Result<(), Error>

Whether the descriptor is safe Checks whether all the spend paths in the descriptor are possible on the bitcoin network under the current standardness and consensus rules Also checks whether the descriptor requires signauture on all spend paths And whether the script is malleable. In general, all the guarantees of miniscript hold only for safe scripts. All the analysis guarantees of miniscript only hold safe scripts. The signer may not be able to find satisfactions even if one exists

Source

fn address(&self, network: Network) -> Result<Address, Error>
where Pk: ToPublicKey,

Computes the Bitcoin address of the descriptor, if one exists Some descriptors like pk() don’t have any address. Errors:

  • On raw/bare descriptors that don’t have any address
Source

fn script_pubkey(&self) -> Script
where Pk: ToPublicKey,

Computes the scriptpubkey of the descriptor

Source

fn unsigned_script_sig(&self) -> Script
where Pk: ToPublicKey,

Computes the scriptSig that will be in place for an unsigned input spending an output with this descriptor. For pre-segwit descriptors, which use the scriptSig for signatures, this returns the empty script.

This is used in Segwit transactions to produce an unsigned transaction whose txid will not change during signing (since only the witness data will change).

Source

fn explicit_script(&self) -> Result<Script, Error>
where Pk: ToPublicKey,

Computes the “witness script” of the descriptor, i.e. the underlying script before any hashing is done. For Bare, Pkh and Wpkh this is the scriptPubkey; for ShWpkh and Sh this is the redeemScript; for the others it is the witness script. For Tr descriptors, this will error as there is no underlying script

Source

fn get_satisfaction<S>( &self, satisfier: S, ) -> Result<(Vec<Vec<u8>>, Script), Error>
where Pk: ToPublicKey, S: Satisfier<Pk>,

Returns satisfying non-malleable witness and scriptSig with minimum weight to spend an output controlled by the given descriptor if it possible to construct one using the satisfier S.

Source

fn get_satisfaction_mall<S>( &self, satisfier: S, ) -> Result<(Vec<Vec<u8>>, Script), Error>
where Pk: ToPublicKey, S: Satisfier<Pk>,

Returns satisfying, possibly malleable witness and scriptSig to spend an output controlled by the given descriptor if it possible to construct one using the satisfier S.

Source

fn max_satisfaction_weight(&self) -> Result<usize, Error>

Computes an upper bound on the weight of a satisfying witness to the transaction. Assumes all ec-signatures are 73 bytes, including push opcode and sighash suffix. Includes the weight of the VarInts encoding the scriptSig and witness stack length. Returns Error when the descriptor is impossible to safisfy (ex: sh(OP_FALSE))

Source

fn script_code(&self) -> Result<Script, Error>
where Pk: ToPublicKey,

Get the scriptCode of a transaction output.

The scriptCode is the Script of the previous transaction output being serialized in the sighash when evaluating a CHECKSIG & co. OP code. Errors:

  • When the descriptor is Tr

Provided Methods§

Source

fn satisfy<S>(&self, txin: &mut TxIn, satisfier: S) -> Result<(), Error>
where Pk: ToPublicKey, S: Satisfier<Pk>,

Attempts to produce a non-malleable satisfying witness and scriptSig to spend an output controlled by the given descriptor; add the data to a given TxIn output.

Examples found in repository?
examples/sign_multisig.rs (line 123)
26fn main() {
27    // Avoid repeatedly typing a pretty-common descriptor type
28    type BitcoinDescriptor = miniscript::Descriptor<bitcoin::PublicKey>;
29
30    // Transaction which spends some output
31    let mut tx = bitcoin::Transaction {
32        version: 2,
33        lock_time: 0,
34        input: vec![bitcoin::TxIn {
35            previous_output: Default::default(),
36            script_sig: bitcoin::Script::new(),
37            sequence: 0xffffffff,
38            witness: Witness::default(),
39        }],
40        output: vec![bitcoin::TxOut {
41            script_pubkey: bitcoin::Script::new(),
42            value: 100_000_000,
43        }],
44    };
45
46    #[cfg_attr(feature="cargo-fmt", rustfmt_skip)]
47    let public_keys = vec![
48        bitcoin::PublicKey::from_slice(&[2; 33]).expect("key 1"),
49        bitcoin::PublicKey::from_slice(&[
50            0x02,
51            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
52            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
53            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
54            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55        ]).expect("key 2"),
56        bitcoin::PublicKey::from_slice(&[
57            0x03,
58            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
59            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
60            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
61            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62        ]).expect("key 3"),
63    ];
64    let bitcoin_sig = bitcoin::EcdsaSig {
65        // copied at random off the blockchain; this is not actually a valid
66        // signature for this transaction; Miniscript does not verify
67        sig: secp256k1::ecdsa::Signature::from_str(
68            "3045\
69             0221\
70             00f7c3648c390d87578cd79c8016940aa8e3511c4104cb78daa8fb8e429375efc1\
71             0220\
72             531d75c136272f127a5dc14acc0722301cbddc222262934151f140da345af177",
73        )
74        .unwrap(),
75        hash_ty: bitcoin::EcdsaSighashType::All,
76    };
77
78    let descriptor_str = format!(
79        "wsh(multi(2,{},{},{}))",
80        public_keys[0], public_keys[1], public_keys[2],
81    );
82
83    // Descriptor for the output being spent
84    let my_descriptor =
85        BitcoinDescriptor::from_str(&descriptor_str[..]).expect("parse descriptor string");
86
87    // Check weight for witness satisfaction cost ahead of time.
88    // 4(scriptSig length of 0) + 1(witness stack size) + 106(serialized witnessScript)
89    // + 73*2(signature length + signatures + sighash bytes) + 1(dummy byte) = 258
90    assert_eq!(my_descriptor.max_satisfaction_weight().unwrap(), 258);
91
92    // Sometimes it is necessary to have additional information to get the bitcoin::PublicKey
93    // from the MiniscriptKey which can supplied by `to_pk_ctx` parameter. For example,
94    // when calculating the script pubkey of a descriptor with xpubs, the secp context and
95    // child information maybe required.
96
97    // Observe the script properties, just for fun
98    assert_eq!(
99        format!("{:x}", my_descriptor.script_pubkey()),
100        "00200ed49b334a12c37f3df8a2974ad91ff95029215a2b53f78155be737907f06163"
101    );
102
103    assert_eq!(
104        format!(
105            "{:x}",
106            my_descriptor
107                .explicit_script()
108                .expect("wsh descriptors have unique inner script")
109        ),
110        "52\
111         21020202020202020202020202020202020202020202020202020202020202020202\
112         21020102030405060708010203040506070801020304050607080000000000000000\
113         21030102030405060708010203040506070801020304050607080000000000000000\
114         53ae"
115    );
116
117    // Attempt to satisfy at age 0, height 0
118    let original_txin = tx.input[0].clone();
119
120    let mut sigs = HashMap::<bitcoin::PublicKey, miniscript::bitcoin::EcdsaSig>::new();
121
122    // Doesn't work with no signatures
123    assert!(my_descriptor.satisfy(&mut tx.input[0], &sigs).is_err());
124    assert_eq!(tx.input[0], original_txin);
125
126    // ...or one signature...
127    sigs.insert(public_keys[1], bitcoin_sig);
128    assert!(my_descriptor.satisfy(&mut tx.input[0], &sigs).is_err());
129    assert_eq!(tx.input[0], original_txin);
130
131    // ...but two signatures is ok
132    sigs.insert(public_keys[2], bitcoin_sig);
133    assert!(my_descriptor.satisfy(&mut tx.input[0], &sigs).is_ok());
134    assert_ne!(tx.input[0], original_txin);
135    assert_eq!(tx.input[0].witness.len(), 4); // 0, sig, sig, witness script
136
137    // ...and even if we give it a third signature, only two are used
138    sigs.insert(public_keys[0], bitcoin_sig);
139    assert!(my_descriptor.satisfy(&mut tx.input[0], &sigs).is_ok());
140    assert_ne!(tx.input[0], original_txin);
141    assert_eq!(tx.input[0].witness.len(), 4); // 0, sig, sig, witness script
142}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§