Skip to content

Commit b79b1a0

Browse files
feat: expose Descriptor::desc_type method
1 parent b9d5b18 commit b79b1a0

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

bdk-ffi/src/bitcoin.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ use std::ops::Deref;
3838
use std::str::FromStr;
3939
use std::sync::{Arc, Mutex};
4040

41+
pub(crate) type DescriptorType = bdk_wallet::miniscript::descriptor::DescriptorType;
42+
4143
/// A reference to an unspent output by TXID and output index.
4244
#[derive(Debug, Clone, Eq, PartialEq, std::hash::Hash, uniffi:: Record)]
4345
pub struct OutPoint {
@@ -725,6 +727,33 @@ pub struct TxMerkleNode(pub(crate) BitcoinDoubleSha256Hash);
725727

726728
impl_hash_like!(TxMerkleNode, BitcoinDoubleSha256Hash);
727729

730+
/// Descriptor Type of the descriptor
731+
#[uniffi::remote(Enum)]
732+
pub enum DescriptorType {
733+
/// Bare descriptor(Contains the native P2pk)
734+
Bare,
735+
/// Pure Sh Descriptor. Does not contain nested Wsh/Wpkh
736+
Sh,
737+
/// Pkh Descriptor
738+
Pkh,
739+
/// Wpkh Descriptor
740+
Wpkh,
741+
/// Wsh
742+
Wsh,
743+
/// Sh Wrapped Wsh
744+
ShWsh,
745+
/// Sh wrapped Wpkh
746+
ShWpkh,
747+
/// Sh Sorted Multi
748+
ShSortedMulti,
749+
/// Wsh Sorted Multi
750+
WshSortedMulti,
751+
/// Sh Wsh Sorted Multi
752+
ShWshSortedMulti,
753+
/// Tr Descriptor
754+
Tr,
755+
}
756+
728757
#[cfg(test)]
729758
mod tests {
730759
use crate::bitcoin::Address;

bdk-ffi/src/descriptor.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::bitcoin::DescriptorId;
2+
use crate::bitcoin::DescriptorType;
23
use crate::error::DescriptorError;
4+
use crate::error::MiniscriptError;
35
use crate::keys::DescriptorPublicKey;
46
use crate::keys::DescriptorSecretKey;
57

@@ -16,7 +18,6 @@ use bdk_wallet::template::{
1618
};
1719
use bdk_wallet::KeychainKind;
1820

19-
use crate::error::MiniscriptError;
2021
use std::fmt::Display;
2122
use std::str::FromStr;
2223
use std::sync::Arc;
@@ -334,6 +335,10 @@ impl Descriptor {
334335
})?;
335336
Ok(weight.to_wu())
336337
}
338+
339+
pub fn desc_type(&self) -> DescriptorType {
340+
self.extended_descriptor.desc_type()
341+
}
337342
}
338343

339344
impl Display for Descriptor {

bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/OfflineDescriptorTest.kt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,51 @@ class OfflineDescriptorTest {
1515
actual = descriptor.toString()
1616
)
1717
}
18+
19+
@Test
20+
fun testDescriptorTypes() {
21+
// Taken from the BIPs: https://github.com/bitcoin/bips/blob/master/bip-0380.mediawiki
22+
23+
val descriptor1 = Descriptor("pk(L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1)", Network.SIGNET)
24+
val descriptor2 = Descriptor("pkh([deadbeef/1/2'/3/4']L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1)", Network.SIGNET)
25+
val descriptor3 = Descriptor("sh(pk(03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd))", Network.SIGNET)
26+
val descriptor4 = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.SIGNET)
27+
val descriptor5 = Descriptor("sh(wpkh(xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi/10/20/30/40/*h))", Network.BITCOIN)
28+
val descriptor6 = Descriptor("multi(1,L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1,5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss)", Network.BITCOIN)
29+
val descriptor7 = Descriptor("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd)", Network.BITCOIN)
30+
val descriptor8 = Descriptor("sh(multi(2,[00000000/111'/222]xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc,xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L/0))", Network.BITCOIN)
31+
32+
assertEquals(
33+
expected = DescriptorType.BARE,
34+
actual = descriptor1.descType()
35+
)
36+
assertEquals(
37+
expected = DescriptorType.PKH,
38+
actual = descriptor2.descType()
39+
)
40+
assertEquals(
41+
expected = DescriptorType.SH,
42+
actual = descriptor3.descType()
43+
)
44+
assertEquals(
45+
expected = DescriptorType.WPKH,
46+
actual = descriptor4.descType()
47+
)
48+
assertEquals(
49+
expected = DescriptorType.SH_WPKH,
50+
actual = descriptor5.descType()
51+
)
52+
assertEquals(
53+
expected = DescriptorType.BARE,
54+
actual = descriptor6.descType()
55+
)
56+
assertEquals(
57+
expected = DescriptorType.TR,
58+
actual = descriptor7.descType()
59+
)
60+
assertEquals(
61+
expected = DescriptorType.SH,
62+
actual = descriptor8.descType()
63+
)
64+
}
1865
}

0 commit comments

Comments
 (0)