Enum sapio_miniscript::descriptor::Descriptor
source · pub enum Descriptor<Pk: MiniscriptKey> {
Bare(Bare<Pk>),
Pkh(Pkh<Pk>),
Wpkh(Wpkh<Pk>),
Sh(Sh<Pk>),
Wsh(Wsh<Pk>),
Tr(Tr<Pk>),
}
Expand description
Script descriptor
Variants§
Bare(Bare<Pk>)
A raw scriptpubkey (including pay-to-pubkey) under Legacy context
Pkh(Pkh<Pk>)
Pay-to-PubKey-Hash
Wpkh(Wpkh<Pk>)
Pay-to-Witness-PubKey-Hash
Sh(Sh<Pk>)
Pay-to-ScriptHash(includes nested wsh/wpkh/sorted multi)
Wsh(Wsh<Pk>)
Pay-to-Witness-ScriptHash with Segwitv0 context
Tr(Tr<Pk>)
Pay-to-Taproot
Implementations§
source§impl<Pk: MiniscriptKey> Descriptor<Pk>
impl<Pk: MiniscriptKey> Descriptor<Pk>
sourcepub fn new_wpkh(pk: Pk) -> Result<Self, Error>
pub fn new_wpkh(pk: Pk) -> Result<Self, Error>
Create a new Wpkh descriptor Will return Err if uncompressed key is used
sourcepub fn new_sh_wpkh(pk: Pk) -> Result<Self, Error>
pub fn new_sh_wpkh(pk: Pk) -> Result<Self, Error>
Create a new sh wrapped wpkh from Pk
.
Errors when uncompressed keys are supplied
sourcepub fn new_sh(ms: Miniscript<Pk, Legacy>) -> Result<Self, Error>
pub fn new_sh(ms: Miniscript<Pk, Legacy>) -> Result<Self, Error>
Create a new sh for a given redeem script Errors when miniscript exceeds resource limits under p2sh context or does not type check at the top level
sourcepub fn new_wsh(ms: Miniscript<Pk, Segwitv0>) -> Result<Self, Error>
pub fn new_wsh(ms: Miniscript<Pk, Segwitv0>) -> Result<Self, Error>
Create a new wsh descriptor from witness script Errors when miniscript exceeds resource limits under p2sh context or does not type check at the top level
sourcepub fn new_sh_wsh(ms: Miniscript<Pk, Segwitv0>) -> Result<Self, Error>
pub fn new_sh_wsh(ms: Miniscript<Pk, Segwitv0>) -> Result<Self, Error>
Create a new sh wrapped wsh descriptor with witness script Errors when miniscript exceeds resource limits under wsh context or does not type check at the top level
sourcepub fn new_bare(ms: Miniscript<Pk, BareCtx>) -> Result<Self, Error>
pub fn new_bare(ms: Miniscript<Pk, BareCtx>) -> Result<Self, Error>
Create a new bare descriptor from witness script Errors when miniscript exceeds resource limits under bare context or does not type check at the top level
sourcepub fn new_sh_with_wpkh(wpkh: Wpkh<Pk>) -> Self
pub fn new_sh_with_wpkh(wpkh: Wpkh<Pk>) -> Self
Create a new sh wrapper for the given wpkh descriptor
sourcepub fn new_sh_with_wsh(wsh: Wsh<Pk>) -> Self
pub fn new_sh_with_wsh(wsh: Wsh<Pk>) -> Self
Create a new sh wrapper for the given wsh descriptor
sourcepub fn new_sh_sortedmulti(k: usize, pks: Vec<Pk>) -> Result<Self, Error>
pub fn new_sh_sortedmulti(k: usize, pks: Vec<Pk>) -> Result<Self, Error>
Create a new sh sortedmulti descriptor with threshold k
and Vec of pks
.
Errors when miniscript exceeds resource limits under p2sh context
sourcepub fn new_sh_wsh_sortedmulti(k: usize, pks: Vec<Pk>) -> Result<Self, Error>
pub fn new_sh_wsh_sortedmulti(k: usize, pks: Vec<Pk>) -> Result<Self, Error>
Create a new sh wrapped wsh sortedmulti descriptor from threshold
k
and Vec of pks
Errors when miniscript exceeds resource limits under segwit context
sourcepub fn new_wsh_sortedmulti(k: usize, pks: Vec<Pk>) -> Result<Self, Error>
pub fn new_wsh_sortedmulti(k: usize, pks: Vec<Pk>) -> Result<Self, Error>
Create a new wsh sorted multi descriptor Errors when miniscript exceeds resource limits under p2sh context
sourcepub fn new_tr(key: Pk, script: Option<TapTree<Pk>>) -> Result<Self, Error>
pub fn new_tr(key: Pk, script: Option<TapTree<Pk>>) -> Result<Self, Error>
Create new tr descriptor Errors when miniscript exceeds resource limits under Tap context
sourcepub fn desc_type(&self) -> DescriptorType
pub fn desc_type(&self) -> DescriptorType
Get the DescriptorType of Descriptor
Examples found in repository?
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
fn main() {
let my_descriptor = miniscript::Descriptor::<bitcoin::PublicKey>::from_str(
"wsh(c:pk_k(020202020202020202020202020202020202020202020202020202020202020202))",
)
.unwrap();
// Check whether the descriptor is safe
// This checks whether all spend paths are accessible in bitcoin network.
// It maybe possible that some of the spend require more than 100 elements in Wsh scripts
// Or they contain a combination of timelock and heightlock.
assert!(my_descriptor.sanity_check().is_ok());
// Compute the script pubkey. As mentioned in the documentation, script_pubkey only fails
// for Tr descriptors that don't have some pre-computed data
assert_eq!(
format!("{:x}", my_descriptor.script_pubkey()),
"0020daef16dd7c946a3e735a6e43310cb2ce33dfd14a04f76bf8241a16654cb2f0f9"
);
// Another way to compute script pubkey
// We can also compute the type of descriptor
let desc_type = my_descriptor.desc_type();
assert_eq!(desc_type, DescriptorType::Wsh);
// Since we know the type of descriptor, we can get the Wsh struct from Descriptor
// This allows us to call infallible methods for getting script pubkey
if let Descriptor::Wsh(wsh) = &my_descriptor {
assert_eq!(
format!("{:x}", wsh.spk()),
"0020daef16dd7c946a3e735a6e43310cb2ce33dfd14a04f76bf8241a16654cb2f0f9"
);
} else {
// We checked for the descriptor type earlier
}
// Get the inner script inside the descriptor
assert_eq!(
format!(
"{:x}",
my_descriptor
.explicit_script()
.expect("Wsh descriptors have inner scripts")
),
"21020202020202020202020202020202020202020202020202020202020202020202ac"
);
let desc = miniscript::Descriptor::<bitcoin::PublicKey>::from_str(
"sh(wsh(c:pk_k(020202020202020202020202020202020202020202020202020202020202020202)))",
)
.unwrap();
assert!(desc.desc_type() == DescriptorType::ShWsh);
}
sourcepub fn into_pre_taproot_desc(self) -> Result<PreTaprootDescriptor<Pk>, Self>
pub fn into_pre_taproot_desc(self) -> Result<PreTaprootDescriptor<Pk>, Self>
.
Convert a Descriptor into pretaproot::PreTaprootDescriptor
Examples
use std::str::FromStr;
use miniscript::descriptor::Descriptor;
use miniscript::{PreTaprootDescriptor, PreTaprootDescriptorTrait};
use miniscript::bitcoin;
// A descriptor with a string generic
let desc = Descriptor::<bitcoin::PublicKey>::from_str("wpkh(02e18f242c8b0b589bfffeac30e1baa80a60933a649c7fb0f1103e78fbf58aa0ed)")
.expect("Valid segwitv0 descriptor");
let pre_tap_desc = desc.into_pre_taproot_desc().expect("Wsh is pre taproot");
// Now the script code and explicit script no longer fail on longer fail
// on PreTaprootDescriptor using PreTaprootDescriptorTrait
let script_code = pre_tap_desc.script_code();
assert_eq!(script_code.to_string(),
"Script(OP_DUP OP_HASH160 OP_PUSHBYTES_20 62107d047e8818b594303fe0657388cc4fc8771f OP_EQUALVERIFY OP_CHECKSIG)"
);
Errors
This function will return an error if descriptor is not a pre taproot descriptor.
source§impl Descriptor<DescriptorPublicKey>
impl Descriptor<DescriptorPublicKey>
sourcepub fn is_deriveable(&self) -> bool
pub fn is_deriveable(&self) -> bool
Whether or not the descriptor has any wildcards
sourcepub fn derive(&self, index: u32) -> Descriptor<DescriptorPublicKey>
pub fn derive(&self, index: u32) -> Descriptor<DescriptorPublicKey>
Derives all wildcard keys in the descriptor using the supplied index
Panics if given an index ≥ 2^31
In most cases, you would want to use Self::derived_descriptor
directly to obtain
a Descriptor<bitcoin::PublicKey>
sourcepub fn derived_descriptor<C: Verification>(
&self,
secp: &Secp256k1<C>,
index: u32
) -> Result<Descriptor<PublicKey>, ConversionError>
pub fn derived_descriptor<C: Verification>( &self, secp: &Secp256k1<C>, index: u32 ) -> Result<Descriptor<PublicKey>, ConversionError>
Derive a Descriptor
with a concrete bitcoin::PublicKey
at a given index
Removes all extended pubkeys and wildcards from the descriptor and only leaves
concrete bitcoin::PublicKey
. All [crate::XOnlyKey
]s are converted to bitcoin::PublicKey
by adding a default(0x02) y-coordinate. For crate::descriptor::Tr
descriptor,
spend info is also cached.
Examples
use miniscript::descriptor::{Descriptor, DescriptorPublicKey};
use miniscript::bitcoin::secp256k1;
use std::str::FromStr;
// test from bip 86
let secp = secp256k1::Secp256k1::verification_only();
let descriptor = Descriptor::<DescriptorPublicKey>::from_str("tr(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*)")
.expect("Valid ranged descriptor");
let result = descriptor.derived_descriptor(&secp, 0).expect("Non-hardened derivation");
assert_eq!(result.to_string(), "tr(03cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115)#6qm9h8ym");
Errors
This function will return an error if hardened derivation is attempted.
Examples found in repository?
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
fn main() {
// For deriving from descriptors, we need to provide a secp context
let secp_ctx = secp256k1::Secp256k1::verification_only();
// P2WSH and single xpubs
let addr_one = Descriptor::<DescriptorPublicKey>::from_str(
"wsh(sortedmulti(1,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH))",
)
.unwrap()
.translate_pk2(|xpk| xpk.derive_public_key(&secp_ctx))
.unwrap()
.address(bitcoin::Network::Bitcoin).unwrap();
let addr_two = Descriptor::<DescriptorPublicKey>::from_str(
"wsh(sortedmulti(1,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB))",
)
.unwrap()
.translate_pk2(|xpk| xpk.derive_public_key(&secp_ctx))
.unwrap()
.address(bitcoin::Network::Bitcoin).unwrap();
let expected = bitcoin::Address::from_str(
"bc1qpq2cfgz5lktxzr5zqv7nrzz46hsvq3492ump9pz8rzcl8wqtwqcspx5y6a",
)
.unwrap();
assert_eq!(addr_one, expected);
assert_eq!(addr_two, expected);
// P2WSH-P2SH and ranged xpubs
let addr_one = Descriptor::<DescriptorPublicKey>::from_str(
"sh(wsh(sortedmulti(1,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/1/0/*,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH/0/0/*)))",
)
.unwrap()
.derived_descriptor(&secp_ctx, 5)
.unwrap()
.address(bitcoin::Network::Bitcoin).unwrap();
let addr_two = Descriptor::<DescriptorPublicKey>::from_str(
"sh(wsh(sortedmulti(1,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH/0/0/*,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/1/0/*)))",
)
.unwrap()
.derived_descriptor(&secp_ctx, 5)
.unwrap()
.address(bitcoin::Network::Bitcoin).unwrap();
let expected = bitcoin::Address::from_str("325zcVBN5o2eqqqtGwPjmtDd8dJRyYP82s").unwrap();
assert_eq!(addr_one, expected);
assert_eq!(addr_two, expected);
}
sourcepub fn parse_descriptor<C: Signing>(
secp: &Secp256k1<C>,
s: &str
) -> Result<(Descriptor<DescriptorPublicKey>, KeyMap), Error>
pub fn parse_descriptor<C: Signing>( secp: &Secp256k1<C>, s: &str ) -> Result<(Descriptor<DescriptorPublicKey>, KeyMap), Error>
Parse a descriptor that may contain secret keys
Internally turns every secret key found into the corresponding public key and then returns a a descriptor that only contains public keys and a map to lookup the secret key given a public key.
sourcepub fn to_string_with_secret(&self, key_map: &KeyMap) -> String
pub fn to_string_with_secret(&self, key_map: &KeyMap) -> String
Serialize a descriptor to string with its secret keys
Trait Implementations§
source§impl<Pk: Clone + MiniscriptKey> Clone for Descriptor<Pk>
impl<Pk: Clone + MiniscriptKey> Clone for Descriptor<Pk>
source§fn clone(&self) -> Descriptor<Pk>
fn clone(&self) -> Descriptor<Pk>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<Pk: MiniscriptKey> Debug for Descriptor<Pk>
impl<Pk: MiniscriptKey> Debug for Descriptor<Pk>
source§impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk>
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk>
source§fn sanity_check(&self) -> Result<(), Error>
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,
fn address(&self, network: Network) -> Result<Address, Error>where
Pk: ToPublicKey,
Computes the Bitcoin address of the descriptor, if one exists
source§fn script_pubkey(&self) -> Scriptwhere
Pk: ToPublicKey,
fn script_pubkey(&self) -> Scriptwhere
Pk: ToPublicKey,
Computes the scriptpubkey of the descriptor
source§fn unsigned_script_sig(&self) -> Scriptwhere
Pk: ToPublicKey,
fn unsigned_script_sig(&self) -> Scriptwhere
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,
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.
Errors:
- When the descriptor is Tr
source§fn get_satisfaction<S>(
&self,
satisfier: S
) -> Result<(Vec<Vec<u8>>, Script), Error>where
Pk: ToPublicKey,
S: Satisfier<Pk>,
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 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>,
fn get_satisfaction_mall<S>(
&self,
satisfier: S
) -> Result<(Vec<Vec<u8>>, Script), Error>where
Pk: ToPublicKey,
S: Satisfier<Pk>,
Returns a possilbly mallable satisfying non-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>
fn max_satisfaction_weight(&self) -> Result<usize, Error>
Computes an upper bound on the weight of a satisfying witness to the transaction. Assumes all signatures are 73 bytes, including push opcode and sighash suffix. Includes the weight of the VarInts encoding the scriptSig and witness stack length.
source§fn script_code(&self) -> Result<Script, Error>where
Pk: ToPublicKey,
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.
Returns Error for Tr descriptors
source§impl<Pk: MiniscriptKey> Display for Descriptor<Pk>
impl<Pk: MiniscriptKey> Display for Descriptor<Pk>
source§impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk>
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk>
source§impl<Pk: MiniscriptKey> From<Bare<Pk>> for Descriptor<Pk>
impl<Pk: MiniscriptKey> From<Bare<Pk>> for Descriptor<Pk>
source§impl<Pk: MiniscriptKey> From<Pkh<Pk>> for Descriptor<Pk>
impl<Pk: MiniscriptKey> From<Pkh<Pk>> for Descriptor<Pk>
source§impl<Pk: MiniscriptKey> From<Sh<Pk>> for Descriptor<Pk>
impl<Pk: MiniscriptKey> From<Sh<Pk>> for Descriptor<Pk>
source§impl<Pk: MiniscriptKey> From<Tr<Pk>> for Descriptor<Pk>
impl<Pk: MiniscriptKey> From<Tr<Pk>> for Descriptor<Pk>
source§impl<Pk: MiniscriptKey> From<Wpkh<Pk>> for Descriptor<Pk>
impl<Pk: MiniscriptKey> From<Wpkh<Pk>> for Descriptor<Pk>
source§impl<Pk: MiniscriptKey> From<Wsh<Pk>> for Descriptor<Pk>
impl<Pk: MiniscriptKey> From<Wsh<Pk>> for Descriptor<Pk>
source§impl<Pk> FromStr for Descriptor<Pk>
impl<Pk> FromStr for Descriptor<Pk>
source§impl<Pk> FromTree for Descriptor<Pk>
impl<Pk> FromTree for Descriptor<Pk>
source§impl<Pk: Hash + MiniscriptKey> Hash for Descriptor<Pk>
impl<Pk: Hash + MiniscriptKey> Hash for Descriptor<Pk>
source§impl<Pk: MiniscriptKey> Liftable<Pk> for Descriptor<Pk>
impl<Pk: MiniscriptKey> Liftable<Pk> for Descriptor<Pk>
source§impl<Pk: Ord + MiniscriptKey> Ord for Descriptor<Pk>
impl<Pk: Ord + MiniscriptKey> Ord for Descriptor<Pk>
source§fn cmp(&self, other: &Descriptor<Pk>) -> Ordering
fn cmp(&self, other: &Descriptor<Pk>) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<Pk: PartialEq + MiniscriptKey> PartialEq for Descriptor<Pk>
impl<Pk: PartialEq + MiniscriptKey> PartialEq for Descriptor<Pk>
source§fn eq(&self, other: &Descriptor<Pk>) -> bool
fn eq(&self, other: &Descriptor<Pk>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<Pk: PartialOrd + MiniscriptKey> PartialOrd for Descriptor<Pk>
impl<Pk: PartialOrd + MiniscriptKey> PartialOrd for Descriptor<Pk>
source§fn partial_cmp(&self, other: &Descriptor<Pk>) -> Option<Ordering>
fn partial_cmp(&self, other: &Descriptor<Pk>) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Descriptor<P>
impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Descriptor<P>
source§fn translate_pk<Fpk, Fpkh, E>(
&self,
translatefpk: Fpk,
translatefpkh: Fpkh
) -> Result<Descriptor<Q>, E>
fn translate_pk<Fpk, Fpkh, E>( &self, translatefpk: Fpk, translatefpkh: Fpkh ) -> Result<Descriptor<Q>, E>
Convert a descriptor using abstract keys to one using specific keys This will panic if translatefpk returns an uncompressed key when converting to a Segwit descriptor. To prevent this panic, ensure translatefpk returns an error in this case instead.