Skip to content

Commit 10a8ce4

Browse files
committed
Move entropy-related types to new entropy.rs module
As we're about to expose more entropy-related things, we here introduce a new module and start moving related types there.
1 parent 37045f4 commit 10a8ce4

File tree

5 files changed

+95
-86
lines changed

5 files changed

+95
-86
lines changed

src/entropy.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// This file is Copyright its original authors, visible in version control history.
2+
//
3+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5+
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
6+
// accordance with one or both of these licenses.
7+
8+
//! Contains utilities for configuring and generating entropy.
9+
10+
use bip39::Mnemonic;
11+
12+
/// Generates a random [BIP 39] mnemonic with the specified word count.
13+
///
14+
/// If no word count is specified, defaults to 24 words (256-bit entropy).
15+
///
16+
/// The result may be used to initialize the [`Node`] entropy, i.e., can be given to
17+
/// [`Builder::set_entropy_bip39_mnemonic`].
18+
///
19+
/// [BIP 39]: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
20+
/// [`Node`]: crate::Node
21+
/// [`Builder::set_entropy_bip39_mnemonic`]: crate::Builder::set_entropy_bip39_mnemonic
22+
pub fn generate_entropy_mnemonic(word_count: Option<WordCount>) -> Mnemonic {
23+
let word_count = word_count.unwrap_or(WordCount::Words24).word_count();
24+
Mnemonic::generate(word_count).expect("Failed to generate mnemonic")
25+
}
26+
27+
/// Supported BIP39 mnemonic word counts for entropy generation.
28+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29+
pub enum WordCount {
30+
/// 12-word mnemonic (128-bit entropy)
31+
Words12,
32+
/// 15-word mnemonic (160-bit entropy)
33+
Words15,
34+
/// 18-word mnemonic (192-bit entropy)
35+
Words18,
36+
/// 21-word mnemonic (224-bit entropy)
37+
Words21,
38+
/// 24-word mnemonic (256-bit entropy)
39+
Words24,
40+
}
41+
42+
impl WordCount {
43+
/// Returns the word count as a usize value.
44+
pub fn word_count(&self) -> usize {
45+
match self {
46+
WordCount::Words12 => 12,
47+
WordCount::Words15 => 15,
48+
WordCount::Words18 => 18,
49+
WordCount::Words21 => 21,
50+
WordCount::Words24 => 24,
51+
}
52+
}
53+
}
54+
55+
#[cfg(test)]
56+
mod tests {
57+
use super::*;
58+
59+
#[test]
60+
fn mnemonic_to_entropy_to_mnemonic() {
61+
// Test default (24 words)
62+
let mnemonic = generate_entropy_mnemonic(None);
63+
let entropy = mnemonic.to_entropy();
64+
assert_eq!(mnemonic, Mnemonic::from_entropy(&entropy).unwrap());
65+
assert_eq!(mnemonic.word_count(), 24);
66+
67+
// Test with different word counts
68+
let word_counts = [
69+
WordCount::Words12,
70+
WordCount::Words15,
71+
WordCount::Words18,
72+
WordCount::Words21,
73+
WordCount::Words24,
74+
];
75+
76+
for word_count in word_counts {
77+
let mnemonic = generate_entropy_mnemonic(Some(word_count));
78+
let entropy = mnemonic.to_entropy();
79+
assert_eq!(mnemonic, Mnemonic::from_entropy(&entropy).unwrap());
80+
81+
// Verify expected word count
82+
let expected_words = match word_count {
83+
WordCount::Words12 => 12,
84+
WordCount::Words15 => 15,
85+
WordCount::Words18 => 18,
86+
WordCount::Words21 => 21,
87+
WordCount::Words24 => 24,
88+
};
89+
assert_eq!(mnemonic.word_count(), expected_words);
90+
}
91+
}
92+
}

src/ffi/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub use crate::config::{
4747
default_config, AnchorChannelsConfig, BackgroundSyncConfig, ElectrumSyncConfig,
4848
EsploraSyncConfig, MaxDustHTLCExposure,
4949
};
50+
pub use crate::entropy::{generate_entropy_mnemonic, WordCount};
5051
use crate::error::Error;
5152
pub use crate::graph::{ChannelInfo, ChannelUpdateInfo, NodeAnnouncementInfo, NodeInfo};
5253
pub use crate::liquidity::{LSPS1OrderStatus, LSPS2ServiceConfig};

src/io/utils.rs

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey};
1717
use bdk_chain::tx_graph::ChangeSet as BdkTxGraphChangeSet;
1818
use bdk_chain::ConfirmationBlockTime;
1919
use bdk_wallet::ChangeSet as BdkWalletChangeSet;
20-
use bip39::Mnemonic;
2120
use bitcoin::Network;
2221
use lightning::io::Cursor;
2322
use lightning::ln::msgs::DecodeError;
@@ -47,27 +46,12 @@ use crate::io::{
4746
};
4847
use crate::logger::{log_error, LdkLogger, Logger};
4948
use crate::peer_store::PeerStore;
50-
use crate::types::{Broadcaster, DynStore, KeysManager, Sweeper, WordCount};
49+
use crate::types::{Broadcaster, DynStore, KeysManager, Sweeper};
5150
use crate::wallet::ser::{ChangeSetDeserWrapper, ChangeSetSerWrapper};
5251
use crate::{Error, EventQueue, NodeMetrics, PaymentDetails};
5352

5453
pub const EXTERNAL_PATHFINDING_SCORES_CACHE_KEY: &str = "external_pathfinding_scores_cache";
5554

56-
/// Generates a random [BIP 39] mnemonic with the specified word count.
57-
///
58-
/// If no word count is specified, defaults to 24 words (256-bit entropy).
59-
///
60-
/// The result may be used to initialize the [`Node`] entropy, i.e., can be given to
61-
/// [`Builder::set_entropy_bip39_mnemonic`].
62-
///
63-
/// [BIP 39]: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
64-
/// [`Node`]: crate::Node
65-
/// [`Builder::set_entropy_bip39_mnemonic`]: crate::Builder::set_entropy_bip39_mnemonic
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")
69-
}
70-
7155
pub(crate) fn read_or_generate_seed_file<L: Deref>(
7256
keys_seed_path: &str, logger: L,
7357
) -> std::io::Result<[u8; WALLET_KEYS_SEED_LEN]>
@@ -620,42 +604,3 @@ pub(crate) fn read_bdk_wallet_change_set(
620604
.map(|indexer| change_set.indexer = indexer);
621605
Ok(Some(change_set))
622606
}
623-
624-
#[cfg(test)]
625-
mod tests {
626-
use super::*;
627-
628-
#[test]
629-
fn mnemonic_to_entropy_to_mnemonic() {
630-
// Test default (24 words)
631-
let mnemonic = generate_entropy_mnemonic(None);
632-
let entropy = mnemonic.to_entropy();
633-
assert_eq!(mnemonic, Mnemonic::from_entropy(&entropy).unwrap());
634-
assert_eq!(mnemonic.word_count(), 24);
635-
636-
// Test with different word counts
637-
let word_counts = [
638-
WordCount::Words12,
639-
WordCount::Words15,
640-
WordCount::Words18,
641-
WordCount::Words21,
642-
WordCount::Words24,
643-
];
644-
645-
for word_count in word_counts {
646-
let mnemonic = generate_entropy_mnemonic(Some(word_count));
647-
let entropy = mnemonic.to_entropy();
648-
assert_eq!(mnemonic, Mnemonic::from_entropy(&entropy).unwrap());
649-
650-
// Verify expected word count
651-
let expected_words = match word_count {
652-
WordCount::Words12 => 12,
653-
WordCount::Words15 => 15,
654-
WordCount::Words18 => 18,
655-
WordCount::Words21 => 21,
656-
WordCount::Words24 => 24,
657-
};
658-
assert_eq!(mnemonic.word_count(), expected_words);
659-
}
660-
}
661-
}

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ mod chain;
8383
pub mod config;
8484
mod connection;
8585
mod data_store;
86+
pub mod entropy;
8687
mod error;
8788
mod event;
8889
mod fee_estimator;
@@ -130,7 +131,6 @@ use fee_estimator::{ConfirmationTarget, FeeEstimator, OnchainFeeEstimator};
130131
use ffi::*;
131132
use gossip::GossipSource;
132133
use graph::NetworkGraph;
133-
pub use io::utils::generate_entropy_mnemonic;
134134
use io::utils::write_node_metrics;
135135
use lightning::chain::BestBlock;
136136
use lightning::events::bump_transaction::{Input, Wallet as LdkWallet};
@@ -160,7 +160,6 @@ use types::{
160160
};
161161
pub use types::{
162162
ChannelDetails, CustomTlvRecord, DynStore, PeerDetails, SyncAndAsyncKVStore, UserChannelId,
163-
WordCount,
164163
};
165164
pub use {
166165
bip39, bitcoin, lightning, lightning_invoice, lightning_liquidity, lightning_types, tokio,

src/types.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,6 @@ use crate::logger::Logger;
3636
use crate::message_handler::NodeCustomMessageHandler;
3737
use crate::payment::PaymentDetails;
3838

39-
/// Supported BIP39 mnemonic word counts for entropy generation.
40-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41-
pub enum WordCount {
42-
/// 12-word mnemonic (128-bit entropy)
43-
Words12,
44-
/// 15-word mnemonic (160-bit entropy)
45-
Words15,
46-
/// 18-word mnemonic (192-bit entropy)
47-
Words18,
48-
/// 21-word mnemonic (224-bit entropy)
49-
Words21,
50-
/// 24-word mnemonic (256-bit entropy)
51-
Words24,
52-
}
53-
54-
impl WordCount {
55-
/// Returns the word count as a usize value.
56-
pub fn word_count(&self) -> usize {
57-
match self {
58-
WordCount::Words12 => 12,
59-
WordCount::Words15 => 15,
60-
WordCount::Words18 => 18,
61-
WordCount::Words21 => 21,
62-
WordCount::Words24 => 24,
63-
}
64-
}
65-
}
66-
6739
/// A supertrait that requires that a type implements both [`KVStore`] and [`KVStoreSync`] at the
6840
/// same time.
6941
pub trait SyncAndAsyncKVStore: KVStore + KVStoreSync {}

0 commit comments

Comments
 (0)