bdk_chain/descriptor_ext.rs
1use crate::miniscript::{Descriptor, DescriptorPublicKey};
2use bitcoin::hashes::{hash_newtype, sha256, Hash};
3use bitcoin::Amount;
4
5hash_newtype! {
6 /// Represents the unique ID of a descriptor.
7 ///
8 /// This is useful for having a fixed-length unique representation of a descriptor,
9 /// in particular, we use it to persist application state changes related to the
10 /// descriptor without having to re-write the whole descriptor each time.
11 ///
12 pub struct DescriptorId(pub sha256::Hash);
13}
14
15/// A trait to extend the functionality of a miniscript descriptor.
16pub trait DescriptorExt {
17 /// Returns the minimum [`Amount`] at which an output is broadcast-able.
18 /// Panics if the descriptor wildcard is hardened.
19 fn dust_value(&self) -> Amount;
20
21 /// Returns the descriptor ID, calculated as the sha256 hash of the spk derived from the
22 /// descriptor at index 0.
23 fn descriptor_id(&self) -> DescriptorId;
24}
25
26impl DescriptorExt for Descriptor<DescriptorPublicKey> {
27 fn dust_value(&self) -> Amount {
28 self.at_derivation_index(0)
29 .expect("descriptor can't have hardened derivation")
30 .script_pubkey()
31 .minimal_non_dust()
32 }
33
34 fn descriptor_id(&self) -> DescriptorId {
35 let spk = self.at_derivation_index(0).unwrap().script_pubkey();
36 DescriptorId(sha256::Hash::hash(spk.as_bytes()))
37 }
38}