Skip to content
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ reqwest = { version = "0.12", default-features = false, features = ["json", "rus
rustls = { version = "0.23", default-features = false }
rusqlite = { version = "0.31.0", features = ["bundled"] }
bitcoin = "0.32.7"
bip39 = "2.0.0"
bip39 = { version = "2.0.0", features = ["rand"] }
bip21 = { version = "0.5", features = ["std"], default-features = false }

base64 = { version = "0.22.1", default-features = false, features = ["std"] }
Expand Down
10 changes: 9 additions & 1 deletion bindings/ldk_node.udl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace ldk_node {
Mnemonic generate_entropy_mnemonic();
Mnemonic generate_entropy_mnemonic(WordCount? word_count);
Config default_config();
};

Expand Down Expand Up @@ -46,6 +46,14 @@ dictionary LSPS2ServiceConfig {
u64 max_payment_size_msat;
};

enum WordCount {
"Words12",
"Words15",
"Words18",
"Words21",
"Words24",
};

enum LogLevel {
"Gossip",
"Trace",
Expand Down
44 changes: 35 additions & 9 deletions src/io/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,25 @@ use crate::io::{
};
use crate::logger::{log_error, LdkLogger, Logger};
use crate::peer_store::PeerStore;
use crate::types::{Broadcaster, DynStore, KeysManager, Sweeper};
use crate::types::{Broadcaster, DynStore, KeysManager, Sweeper, WordCount};
use crate::wallet::ser::{ChangeSetDeserWrapper, ChangeSetSerWrapper};
use crate::{Error, EventQueue, NodeMetrics, PaymentDetails};

pub const EXTERNAL_PATHFINDING_SCORES_CACHE_KEY: &str = "external_pathfinding_scores_cache";

/// Generates a random [BIP 39] mnemonic.
/// Generates a random [BIP 39] mnemonic with the specified word count.
///
/// If no word count is specified, defaults to 24 words (256-bit entropy).
///
/// The result may be used to initialize the [`Node`] entropy, i.e., can be given to
/// [`Builder::set_entropy_bip39_mnemonic`].
///
/// [BIP 39]: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
/// [`Node`]: crate::Node
/// [`Builder::set_entropy_bip39_mnemonic`]: crate::Builder::set_entropy_bip39_mnemonic
pub fn generate_entropy_mnemonic() -> Mnemonic {
// bip39::Mnemonic supports 256 bit entropy max
let mut entropy = [0; 32];
OsRng.try_fill_bytes(&mut entropy).expect("Failed to generate entropy");
Mnemonic::from_entropy(&entropy).unwrap()
pub fn generate_entropy_mnemonic(word_count: Option<WordCount>) -> Mnemonic {
let word_count = word_count.unwrap_or(WordCount::Words24).word_count();
Mnemonic::generate(word_count).expect("Failed to generate mnemonic")
}

pub(crate) fn read_or_generate_seed_file<L: Deref>(
Expand Down Expand Up @@ -627,9 +627,35 @@ mod tests {

#[test]
fn mnemonic_to_entropy_to_mnemonic() {
let mnemonic = generate_entropy_mnemonic();

// Test default (24 words)
let mnemonic = generate_entropy_mnemonic(None);
let entropy = mnemonic.to_entropy();
assert_eq!(mnemonic, Mnemonic::from_entropy(&entropy).unwrap());
assert_eq!(mnemonic.word_count(), 24);

// Test with different word counts
let word_counts = [
WordCount::Words12,
WordCount::Words15,
WordCount::Words18,
WordCount::Words21,
WordCount::Words24,
];

for word_count in word_counts {
let mnemonic = generate_entropy_mnemonic(Some(word_count));
let entropy = mnemonic.to_entropy();
assert_eq!(mnemonic, Mnemonic::from_entropy(&entropy).unwrap());

// Verify expected word count
let expected_words = match word_count {
WordCount::Words12 => 12,
WordCount::Words15 => 15,
WordCount::Words18 => 18,
WordCount::Words21 => 21,
WordCount::Words24 => 24,
};
assert_eq!(mnemonic.word_count(), expected_words);
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ use types::{
};
pub use types::{
ChannelDetails, CustomTlvRecord, DynStore, PeerDetails, SyncAndAsyncKVStore, UserChannelId,
WordCount,
};
pub use {
bip39, bitcoin, lightning, lightning_invoice, lightning_liquidity, lightning_types, tokio,
Expand Down
28 changes: 28 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ use crate::logger::Logger;
use crate::message_handler::NodeCustomMessageHandler;
use crate::payment::PaymentDetails;

/// Supported BIP39 mnemonic word counts for entropy generation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WordCount {
/// 12-word mnemonic (128-bit entropy)
Words12,
/// 15-word mnemonic (160-bit entropy)
Words15,
/// 18-word mnemonic (192-bit entropy)
Words18,
/// 21-word mnemonic (224-bit entropy)
Words21,
/// 24-word mnemonic (256-bit entropy)
Words24,
}

impl WordCount {
/// Returns the word count as a usize value.
pub fn word_count(&self) -> usize {
match self {
WordCount::Words12 => 12,
WordCount::Words15 => 15,
WordCount::Words18 => 18,
WordCount::Words21 => 21,
WordCount::Words24 => 24,
}
}
}

/// A supertrait that requires that a type implements both [`KVStore`] and [`KVStoreSync`] at the
/// same time.
pub trait SyncAndAsyncKVStore: KVStore + KVStoreSync {}
Expand Down