Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ use std::ops::Deref;
use std::str::FromStr;
use std::sync::{Arc, Mutex};

pub(crate) type DescriptorType = bdk_wallet::miniscript::descriptor::DescriptorType;

/// A reference to an unspent output by TXID and output index.
#[derive(Debug, Clone, Eq, PartialEq, std::hash::Hash, uniffi:: Record)]
pub struct OutPoint {
Expand Down Expand Up @@ -725,6 +727,33 @@ pub struct TxMerkleNode(pub(crate) BitcoinDoubleSha256Hash);

impl_hash_like!(TxMerkleNode, BitcoinDoubleSha256Hash);

/// Descriptor Type of the descriptor
#[uniffi::remote(Enum)]
pub enum DescriptorType {
/// Bare descriptor(Contains the native P2pk)
Bare,
/// Pure Sh Descriptor. Does not contain nested Wsh/Wpkh
Sh,
/// Pkh Descriptor
Pkh,
/// Wpkh Descriptor
Wpkh,
/// Wsh
Wsh,
/// Sh Wrapped Wsh
ShWsh,
/// Sh wrapped Wpkh
ShWpkh,
/// Sh Sorted Multi
ShSortedMulti,
/// Wsh Sorted Multi
WshSortedMulti,
/// Sh Wsh Sorted Multi
ShWshSortedMulti,
/// Tr Descriptor
Tr,
}

#[cfg(test)]
mod tests {
use crate::bitcoin::Address;
Expand Down
7 changes: 6 additions & 1 deletion bdk-ffi/src/descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::bitcoin::DescriptorId;
use crate::bitcoin::DescriptorType;
use crate::error::DescriptorError;
use crate::error::MiniscriptError;
use crate::keys::DescriptorPublicKey;
use crate::keys::DescriptorSecretKey;

Expand All @@ -16,7 +18,6 @@ use bdk_wallet::template::{
};
use bdk_wallet::KeychainKind;

use crate::error::MiniscriptError;
use std::fmt::Display;
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -334,6 +335,10 @@ impl Descriptor {
})?;
Ok(weight.to_wu())
}

pub fn desc_type(&self) -> DescriptorType {
self.extended_descriptor.desc_type()
}
}

impl Display for Descriptor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,51 @@ class OfflineDescriptorTest {
actual = descriptor.toString()
)
}

@Test
fun testDescriptorTypes() {
// Taken from the BIPs: https://github.com/bitcoin/bips/blob/master/bip-0380.mediawiki

val descriptor1 = Descriptor("pk(L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1)", Network.SIGNET)
val descriptor2 = Descriptor("pkh([deadbeef/1/2'/3/4']L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1)", Network.SIGNET)
val descriptor3 = Descriptor("sh(pk(03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd))", Network.SIGNET)
val descriptor4 = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.SIGNET)
val descriptor5 = Descriptor("sh(wpkh(xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi/10/20/30/40/*h))", Network.BITCOIN)
val descriptor6 = Descriptor("multi(1,L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1,5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss)", Network.BITCOIN)
val descriptor7 = Descriptor("tr(a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd)", Network.BITCOIN)
val descriptor8 = Descriptor("sh(multi(2,[00000000/111'/222]xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc,xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L/0))", Network.BITCOIN)

assertEquals(
expected = DescriptorType.BARE,
actual = descriptor1.descType()
)
assertEquals(
expected = DescriptorType.PKH,
actual = descriptor2.descType()
)
assertEquals(
expected = DescriptorType.SH,
actual = descriptor3.descType()
)
assertEquals(
expected = DescriptorType.WPKH,
actual = descriptor4.descType()
)
assertEquals(
expected = DescriptorType.SH_WPKH,
actual = descriptor5.descType()
)
assertEquals(
expected = DescriptorType.BARE,
actual = descriptor6.descType()
)
assertEquals(
expected = DescriptorType.TR,
actual = descriptor7.descType()
)
assertEquals(
expected = DescriptorType.SH,
actual = descriptor8.descType()
)
}
}