Skip to content

Commit 0bf8721

Browse files
committed
refactor: replace entropy_bytes with word_count
- Rename MnemonicWordCount to WordCount and update references as needed - Remove need for entropy_bytes in generate_entropy_mnemonic by passing WordCount enum directly to generate() instead - Add rand feature to bip39 dependency
1 parent 05cb330 commit 0bf8721

File tree

5 files changed

+28
-31
lines changed

5 files changed

+28
-31
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ reqwest = { version = "0.12", default-features = false, features = ["json", "rus
5050
rustls = { version = "0.23", default-features = false }
5151
rusqlite = { version = "0.31.0", features = ["bundled"] }
5252
bitcoin = "0.32.7"
53-
bip39 = "2.0.0"
53+
bip39 = { version = "2.0.0", features = ["rand"] }
5454
bip21 = { version = "0.5", features = ["std"], default-features = false }
5555

5656
base64 = { version = "0.22.1", default-features = false, features = ["std"] }

bindings/ldk_node.udl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
namespace ldk_node {
2-
Mnemonic generate_entropy_mnemonic(MnemonicWordCount? word_count);
2+
Mnemonic generate_entropy_mnemonic(WordCount? word_count);
33
Config default_config();
44
};
55

@@ -46,7 +46,7 @@ dictionary LSPS2ServiceConfig {
4646
u64 max_payment_size_msat;
4747
};
4848

49-
enum MnemonicWordCount {
49+
enum WordCount {
5050
"Words12",
5151
"Words15",
5252
"Words18",

src/io/utils.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use crate::io::{
4747
};
4848
use crate::logger::{log_error, LdkLogger, Logger};
4949
use crate::peer_store::PeerStore;
50-
use crate::types::{Broadcaster, DynStore, KeysManager, MnemonicWordCount, Sweeper};
50+
use crate::types::{Broadcaster, DynStore, KeysManager, Sweeper, WordCount};
5151
use crate::wallet::ser::{ChangeSetDeserWrapper, ChangeSetSerWrapper};
5252
use crate::{Error, EventQueue, NodeMetrics, PaymentDetails};
5353

@@ -63,12 +63,9 @@ pub const EXTERNAL_PATHFINDING_SCORES_CACHE_KEY: &str = "external_pathfinding_sc
6363
/// [BIP 39]: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
6464
/// [`Node`]: crate::Node
6565
/// [`Builder::set_entropy_bip39_mnemonic`]: crate::Builder::set_entropy_bip39_mnemonic
66-
pub fn generate_entropy_mnemonic(word_count: Option<MnemonicWordCount>) -> Mnemonic {
67-
let word_count = word_count.unwrap_or(MnemonicWordCount::Words24);
68-
let entropy_bytes = word_count.entropy_bytes();
69-
let mut entropy = vec![0u8; entropy_bytes];
70-
OsRng.try_fill_bytes(&mut entropy).expect("Failed to generate entropy");
71-
Mnemonic::from_entropy(&entropy).unwrap()
66+
pub fn generate_entropy_mnemonic(word_count: Option<WordCount>) -> Mnemonic {
67+
let word_count = word_count.unwrap_or(WordCount::Words24).word_count();
68+
Mnemonic::generate(word_count).expect("Failed to generate mnemonic")
7269
}
7370

7471
pub(crate) fn read_or_generate_seed_file<L: Deref>(
@@ -638,11 +635,11 @@ mod tests {
638635

639636
// Test with different word counts
640637
let word_counts = [
641-
MnemonicWordCount::Words12,
642-
MnemonicWordCount::Words15,
643-
MnemonicWordCount::Words18,
644-
MnemonicWordCount::Words21,
645-
MnemonicWordCount::Words24,
638+
WordCount::Words12,
639+
WordCount::Words15,
640+
WordCount::Words18,
641+
WordCount::Words21,
642+
WordCount::Words24,
646643
];
647644

648645
for word_count in word_counts {
@@ -652,11 +649,11 @@ mod tests {
652649

653650
// Verify expected word count
654651
let expected_words = match word_count {
655-
MnemonicWordCount::Words12 => 12,
656-
MnemonicWordCount::Words15 => 15,
657-
MnemonicWordCount::Words18 => 18,
658-
MnemonicWordCount::Words21 => 21,
659-
MnemonicWordCount::Words24 => 24,
652+
WordCount::Words12 => 12,
653+
WordCount::Words15 => 15,
654+
WordCount::Words18 => 18,
655+
WordCount::Words21 => 21,
656+
WordCount::Words24 => 24,
660657
};
661658
assert_eq!(mnemonic.word_count(), expected_words);
662659
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ use types::{
155155
OnionMessenger, PaymentStore, PeerManager, Router, Scorer, Sweeper, Wallet,
156156
};
157157
pub use types::{
158-
ChannelDetails, CustomTlvRecord, DynStore, MnemonicWordCount, PeerDetails, SyncAndAsyncKVStore,
159-
UserChannelId,
158+
ChannelDetails, CustomTlvRecord, DynStore, PeerDetails, SyncAndAsyncKVStore, UserChannelId,
159+
WordCount,
160160
};
161161
pub use {
162162
bip39, bitcoin, lightning, lightning_invoice, lightning_liquidity, lightning_types, tokio,

src/types.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::payment::PaymentDetails;
3838

3939
/// Supported BIP39 mnemonic word counts for entropy generation.
4040
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41-
pub enum MnemonicWordCount {
41+
pub enum WordCount {
4242
/// 12-word mnemonic (128-bit entropy)
4343
Words12,
4444
/// 15-word mnemonic (160-bit entropy)
@@ -51,15 +51,15 @@ pub enum MnemonicWordCount {
5151
Words24,
5252
}
5353

54-
impl MnemonicWordCount {
55-
/// Returns the entropy size in bytes for the word count.
56-
pub fn entropy_bytes(&self) -> usize {
54+
impl WordCount {
55+
/// Returns the word count as a usize value.
56+
pub fn word_count(&self) -> usize {
5757
match self {
58-
MnemonicWordCount::Words12 => 16, // 128 bits
59-
MnemonicWordCount::Words15 => 20, // 160 bits
60-
MnemonicWordCount::Words18 => 24, // 192 bits
61-
MnemonicWordCount::Words21 => 28, // 224 bits
62-
MnemonicWordCount::Words24 => 32, // 256 bits
58+
WordCount::Words12 => 12,
59+
WordCount::Words15 => 15,
60+
WordCount::Words18 => 18,
61+
WordCount::Words21 => 21,
62+
WordCount::Words24 => 24,
6363
}
6464
}
6565
}

0 commit comments

Comments
 (0)