Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rust-bip39 and simplify fn Mnemonic::new #227

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/tcharding/rust-bip39.git", branch = "10-26-bump-version" }

[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/tcharding/rust-bip39.git", branch = "10-26-bump-version", features = ["rand"] }

[build-dependencies]
uniffi_build = { version = "0.21.0", features = ["builtin-bindgen"] }
Expand Down
26 changes: 20 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ use bdk::blockchain::{Blockchain as BdkBlockchain, Progress as BdkProgress};
use bdk::database::any::{AnyDatabase, SledDbConfiguration, SqliteDbConfiguration};
use bdk::database::{AnyDatabaseConfig, ConfigurableDatabase};
use bdk::descriptor::DescriptorXKey;
use bdk::keys::bip39::{Language, Mnemonic as BdkMnemonic, WordCount};
use bdk::keys::bip39::{Mnemonic as BdkMnemonic, WordCount};
use bdk::keys::{
DerivableKey, DescriptorPublicKey as BdkDescriptorPublicKey,
DescriptorSecretKey as BdkDescriptorSecretKey, ExtendedKey, GeneratableKey, GeneratedKey,
DescriptorSecretKey as BdkDescriptorSecretKey, ExtendedKey,
};
use bdk::miniscript::BareCtx;
use bdk::psbt::PsbtUtils;
use bdk::wallet::tx_builder::ChangeSpendPolicy;
use bdk::wallet::AddressIndex as BdkAddressIndex;
Expand Down Expand Up @@ -883,9 +882,8 @@ 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 = word_count as usize / 32 * 3;
let mnemonic = BdkMnemonic::generate(word_count).unwrap();
Mnemonic { internal: mnemonic }
}

Expand Down Expand Up @@ -1290,4 +1288,20 @@ 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(' ').count(), count);
}
}
}