|
1 | 1 | use crate::error::{Bip32Error, Bip39Error, DescriptorKeyError}; |
2 | 2 |
|
| 3 | +use bdk_wallet::bitcoin::Network; |
3 | 4 | use bdk_wallet::bitcoin::bip32::DerivationPath as BdkDerivationPath; |
4 | 5 | use bdk_wallet::bitcoin::key::Secp256k1; |
5 | 6 | use bdk_wallet::bitcoin::secp256k1::rand; |
6 | 7 | use bdk_wallet::bitcoin::secp256k1::rand::Rng; |
7 | | -use bdk_wallet::bitcoin::Network; |
8 | 8 | use bdk_wallet::keys::bip39::WordCount; |
9 | 9 | use bdk_wallet::keys::bip39::{Language, Mnemonic as BdkMnemonic}; |
10 | 10 | use bdk_wallet::keys::{ |
11 | 11 | DerivableKey, DescriptorPublicKey as BdkDescriptorPublicKey, |
12 | 12 | DescriptorSecretKey as BdkDescriptorSecretKey, ExtendedKey, GeneratableKey, GeneratedKey, |
13 | 13 | }; |
14 | | -use bdk_wallet::miniscript::descriptor::{DescriptorXKey, Wildcard}; |
15 | 14 | use bdk_wallet::miniscript::BareCtx; |
| 15 | +use bdk_wallet::miniscript::descriptor::{DescriptorXKey, Wildcard}; |
16 | 16 |
|
17 | 17 | use std::fmt::Display; |
18 | 18 | use std::str::FromStr; |
@@ -76,6 +76,43 @@ impl DerivationPath { |
76 | 76 | .map(DerivationPath) |
77 | 77 | .map_err(Bip32Error::from) |
78 | 78 | } |
| 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 | + } |
79 | 116 | } |
80 | 117 |
|
81 | 118 | /// A descriptor containing secret data. |
|
0 commit comments