Skip to content

Commit 0076e0d

Browse files
committed
feat: Expose DerivationPath methods
1 parent a81c547 commit 0076e0d

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

bdk-ffi/src/keys.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use crate::error::{Bip32Error, Bip39Error, DescriptorKeyError};
22

3+
use bdk_wallet::bitcoin::Network;
34
use bdk_wallet::bitcoin::bip32::DerivationPath as BdkDerivationPath;
45
use bdk_wallet::bitcoin::key::Secp256k1;
56
use bdk_wallet::bitcoin::secp256k1::rand;
67
use bdk_wallet::bitcoin::secp256k1::rand::Rng;
7-
use bdk_wallet::bitcoin::Network;
88
use bdk_wallet::keys::bip39::WordCount;
99
use bdk_wallet::keys::bip39::{Language, Mnemonic as BdkMnemonic};
1010
use bdk_wallet::keys::{
1111
DerivableKey, DescriptorPublicKey as BdkDescriptorPublicKey,
1212
DescriptorSecretKey as BdkDescriptorSecretKey, ExtendedKey, GeneratableKey, GeneratedKey,
1313
};
14-
use bdk_wallet::miniscript::descriptor::{DescriptorXKey, Wildcard};
1514
use bdk_wallet::miniscript::BareCtx;
15+
use bdk_wallet::miniscript::descriptor::{DescriptorXKey, Wildcard};
1616

1717
use std::fmt::Display;
1818
use std::str::FromStr;
@@ -76,6 +76,43 @@ impl DerivationPath {
7676
.map(DerivationPath)
7777
.map_err(Bip32Error::from)
7878
}
79+
80+
/// Returns derivation path for a master key (i.e. empty derivation path)
81+
#[uniffi::constructor]
82+
pub fn master() -> Arc<Self> {
83+
Arc::new(DerivationPath {
84+
inner_mutex: Mutex::new(BdkDerivationPath::master()),
85+
})
86+
}
87+
88+
/// Returns whether derivation path represents master key (i.e. it's length
89+
/// is empty). True for `m` path.
90+
pub fn is_master(&self) -> bool {
91+
self.get_derivation_path().is_master()
92+
}
93+
94+
/// Returns length of the derivation path
95+
pub fn len(&self) -> u64 {
96+
self.get_derivation_path().len() as u64
97+
}
98+
99+
/// Returns `true` if the derivation path is empty
100+
pub fn is_empty(&self) -> bool {
101+
self.get_derivation_path().is_empty()
102+
}
103+
}
104+
105+
impl Display for DerivationPath {
106+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
107+
let path = self.get_derivation_path();
108+
write!(f, "{}", path)
109+
}
110+
}
111+
112+
impl DerivationPath {
113+
pub(crate) fn get_derivation_path(&self) -> MutexGuard<'_, BdkDerivationPath> {
114+
self.inner_mutex.lock().expect("derivation path")
115+
}
79116
}
80117

81118
/// A descriptor containing secret data.

0 commit comments

Comments
 (0)