@@ -47,25 +47,25 @@ use crate::io::{
4747} ;
4848use crate :: logger:: { log_error, LdkLogger , Logger } ;
4949use crate :: peer_store:: PeerStore ;
50- use crate :: types:: { Broadcaster , DynStore , KeysManager , Sweeper } ;
50+ use crate :: types:: { Broadcaster , DynStore , KeysManager , Sweeper , WordCount } ;
5151use crate :: wallet:: ser:: { ChangeSetDeserWrapper , ChangeSetSerWrapper } ;
5252use crate :: { Error , EventQueue , NodeMetrics , PaymentDetails } ;
5353
5454pub const EXTERNAL_PATHFINDING_SCORES_CACHE_KEY : & str = "external_pathfinding_scores_cache" ;
5555
56- /// Generates a random [BIP 39] mnemonic.
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).
5759///
5860/// The result may be used to initialize the [`Node`] entropy, i.e., can be given to
5961/// [`Builder::set_entropy_bip39_mnemonic`].
6062///
6163/// [BIP 39]: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
6264/// [`Node`]: crate::Node
6365/// [`Builder::set_entropy_bip39_mnemonic`]: crate::Builder::set_entropy_bip39_mnemonic
64- pub fn generate_entropy_mnemonic ( ) -> Mnemonic {
65- // bip39::Mnemonic supports 256 bit entropy max
66- let mut entropy = [ 0 ; 32 ] ;
67- OsRng . try_fill_bytes ( & mut entropy) . expect ( "Failed to generate entropy" ) ;
68- 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" )
6969}
7070
7171pub ( crate ) fn read_or_generate_seed_file < L : Deref > (
@@ -627,9 +627,35 @@ mod tests {
627627
628628 #[ test]
629629 fn mnemonic_to_entropy_to_mnemonic ( ) {
630- let mnemonic = generate_entropy_mnemonic ( ) ;
631-
630+ // Test default (24 words)
631+ let mnemonic = generate_entropy_mnemonic ( None ) ;
632632 let entropy = mnemonic. to_entropy ( ) ;
633633 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+ }
634660 }
635661}
0 commit comments