Skip to content

Commit 58e06a9

Browse files
committed
refactor: use macros for Mmenomic and DerivationPath
1 parent 74bb68d commit 58e06a9

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

bdk-ffi/src/bdk.udl

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -442,26 +442,6 @@ dictionary Condition {
442442
LockTime? timelock;
443443
};
444444

445-
// ------------------------------------------------------------------------
446-
// bdk crate - descriptor module
447-
// ------------------------------------------------------------------------
448-
449-
[Traits=(Display)]
450-
interface Mnemonic {
451-
constructor(WordCount word_count);
452-
453-
[Name=from_string, Throws=Bip39Error]
454-
constructor(string mnemonic);
455-
456-
[Name=from_entropy, Throws=Bip39Error]
457-
constructor(sequence<u8> entropy);
458-
};
459-
460-
interface DerivationPath {
461-
[Throws=Bip32Error]
462-
constructor(string path);
463-
};
464-
465445
// ------------------------------------------------------------------------
466446
// bdk-ffi-defined types
467447
// ------------------------------------------------------------------------

bdk-ffi/src/keys.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ use std::ops::Deref;
1919
use std::str::FromStr;
2020
use std::sync::{Arc, Mutex};
2121

22+
/// A mnemonic seed phrase to recover a BIP-32 wallet.
23+
#[derive(uniffi::Object)]
24+
#[uniffi::export(Display)]
2225
pub struct Mnemonic(BdkMnemonic);
2326

27+
#[uniffi::export]
2428
impl Mnemonic {
29+
/// Generate a mnemonic given a word count.
30+
#[uniffi::constructor]
2531
pub fn new(word_count: WordCount) -> Self {
2632
// TODO 4: I DON'T KNOW IF THIS IS A DECENT WAY TO GENERATE ENTROPY PLEASE CONFIRM
2733
let mut rng = rand::thread_rng();
@@ -33,13 +39,18 @@ impl Mnemonic {
3339
let mnemonic = BdkMnemonic::parse_in(Language::English, generated_key.to_string()).unwrap();
3440
Mnemonic(mnemonic)
3541
}
36-
42+
/// Parse a string as a mnemonic seed phrase.
43+
#[uniffi::constructor]
3744
pub fn from_string(mnemonic: String) -> Result<Self, Bip39Error> {
3845
BdkMnemonic::from_str(&mnemonic)
3946
.map(Mnemonic)
4047
.map_err(Bip39Error::from)
4148
}
4249

50+
/// Construct a mnemonic given an array of bytes. Note that using weak entropy will result in a loss
51+
/// of funds. To ensure the entropy is generated properly, read about your operating
52+
/// system specific ways to generate secure random numbers.
53+
#[uniffi::constructor]
4354
pub fn from_entropy(entropy: Vec<u8>) -> Result<Self, Bip39Error> {
4455
BdkMnemonic::from_entropy(entropy.as_slice())
4556
.map(Mnemonic)
@@ -53,11 +64,16 @@ impl Display for Mnemonic {
5364
}
5465
}
5566

67+
/// A BIP-32 derivation path.
68+
#[derive(uniffi::Object)]
5669
pub struct DerivationPath {
5770
inner_mutex: Mutex<BdkDerivationPath>,
5871
}
5972

73+
#[uniffi::export]
6074
impl DerivationPath {
75+
/// Parse a string as a BIP-32 derivation path.
76+
#[uniffi::constructor]
6177
pub fn new(path: String) -> Result<Self, Bip32Error> {
6278
BdkDerivationPath::from_str(&path)
6379
.map(|x| DerivationPath {

bdk-ffi/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ use crate::error::PsbtFinalizeError;
3333
use crate::error::PsbtParseError;
3434
use crate::error::RequestBuilderError;
3535
use crate::error::TransactionError;
36-
use crate::keys::DerivationPath;
37-
use crate::keys::Mnemonic;
3836
use crate::types::ChainPosition;
3937
use crate::types::Condition;
4038
use crate::types::FullScanRequest;

0 commit comments

Comments
 (0)