Skip to content

Commit

Permalink
Update rust-bip39 and simplify fn Mnemonic::new
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Nov 9, 2022
1 parent 0648075 commit 39ecd6c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ default-members = [".", "bdk-ffi-bindgen"]
crate-type = ["staticlib", "cdylib"]
name = "bdkffi"

[patch.crates-io]
bip39 = { git = "https://github.com/rust-bitcoin/rust-bip39.git", branch = "master" }

[dependencies]
bdk = { version = "0.24", features = ["all-keys", "use-esplora-ureq", "sqlite-bundled"] }

uniffi_macros = { version = "0.21.0", features = ["builtin-bindgen"] }
uniffi = { version = "0.21.0", features = ["builtin-bindgen"] }
bip39 = { git = "https://github.com/rust-bitcoin/rust-bip39.git", branch = "master", features = ["rand"] }

[build-dependencies]
uniffi_build = { version = "0.21.0", features = ["builtin-bindgen"] }
Expand Down
31 changes: 28 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,9 +883,14 @@ struct Mnemonic {
impl Mnemonic {
/// Generates Mnemonic with a random entropy
fn new(word_count: WordCount) -> Self {
let generated_key: GeneratedKey<_, BareCtx> =
BdkMnemonic::generate((word_count, Language::English)).unwrap();
let mnemonic = BdkMnemonic::parse_in(Language::English, generated_key.to_string()).unwrap();
let word_count = match word_count {
WordCount::Words12 => 12,
WordCount::Words15 => 15,
WordCount::Words18 => 18,
WordCount::Words21 => 21,
WordCount::Words24 => 24,
};
let mnemonic = BdkMnemonic::generate(word_count).unwrap();
Mnemonic { internal: mnemonic }
}

Expand Down Expand Up @@ -1095,6 +1100,7 @@ mod test {
use crate::*;
use bdk::bitcoin::Address;
use bdk::bitcoin::Network::Testnet;
use bdk::keys::bip39::WordCount::Words12;
use bdk::wallet::get_funded_wallet;
use std::str::FromStr;
use std::sync::Mutex;
Expand Down Expand Up @@ -1290,4 +1296,23 @@ mod test {
assert!(tx_builder_result.psbt.fee_amount().is_some());
assert_eq!(tx_builder_result.psbt.fee_amount().unwrap(), 220);
}

#[test]
fn test_generate_mnemonic() {
const WORD_COUNTS: [(WordCount, usize); 5] = [
(WordCount::Words12, 12),
(WordCount::Words15, 15),
(WordCount::Words18, 18),
(WordCount::Words21, 21),
(WordCount::Words24, 24),
];

for (word_count, count) in WORD_COUNTS {
let mnemonic = Mnemonic::new(word_count);
assert_eq!(
mnemonic.as_string().split(' ').collect::<Vec<&str>>().len(),
count
);
}
}
}

0 comments on commit 39ecd6c

Please sign in to comment.