Skip to content

Commit 691dfb4

Browse files
committed
feat: Expose DerivationPath methods
1 parent 8a2946c commit 691dfb4

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

bdk-ffi/src/keys.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use bdk_wallet::miniscript::BareCtx;
1717
use std::fmt::Display;
1818
use std::ops::Deref;
1919
use std::str::FromStr;
20-
use std::sync::{Arc, Mutex};
20+
use std::sync::{Arc, Mutex, MutexGuard};
2121

2222
/// A mnemonic seed phrase to recover a BIP-32 wallet.
2323
#[derive(uniffi::Object)]
@@ -81,6 +81,43 @@ impl DerivationPath {
8181
})
8282
.map_err(Bip32Error::from)
8383
}
84+
85+
/// Returns derivation path for a master key (i.e. empty derivation path)
86+
#[uniffi::constructor]
87+
pub fn master() -> Arc<Self> {
88+
Arc::new(DerivationPath {
89+
inner_mutex: Mutex::new(BdkDerivationPath::master()),
90+
})
91+
}
92+
93+
/// Returns whether derivation path represents master key (i.e. it's length
94+
/// is empty). True for `m` path.
95+
pub fn is_master(&self) -> bool {
96+
self.get_derivation_path().is_master()
97+
}
98+
99+
/// Returns length of the derivation path
100+
pub fn len(&self) -> u64 {
101+
self.get_derivation_path().len() as u64
102+
}
103+
104+
/// Returns `true` if the derivation path is empty
105+
pub fn is_empty(&self) -> bool {
106+
self.get_derivation_path().is_empty()
107+
}
108+
}
109+
110+
impl Display for DerivationPath {
111+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
112+
let path = self.get_derivation_path();
113+
write!(f, "{}", path)
114+
}
115+
}
116+
117+
impl DerivationPath {
118+
pub(crate) fn get_derivation_path(&self) -> MutexGuard<'_, BdkDerivationPath> {
119+
self.inner_mutex.lock().expect("derivation path")
120+
}
84121
}
85122

86123
/// A descriptor containing secret data.

0 commit comments

Comments
 (0)