@@ -12,6 +12,7 @@ use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParam
1212use lightning:: util:: logger:: Logger ;
1313use lightning:: util:: ser:: { Readable , ReadableArgs , Writeable } ;
1414
15+ use bip39:: Mnemonic ;
1516use bitcoin:: hash_types:: { BlockHash , Txid } ;
1617use bitcoin:: hashes:: hex:: FromHex ;
1718use rand:: { thread_rng, RngCore } ;
@@ -24,6 +25,20 @@ use std::sync::Arc;
2425
2526use super :: KVStore ;
2627
28+ /// Generates a random [BIP 39] mnemonic.
29+ ///
30+ /// The result may be used to initialize the [`Node`] entropy, i.e., can be given to
31+ /// [`Builder::set_entropy_bip39_mnemonic`].
32+ ///
33+ /// [BIP 39]: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
34+ /// [`Builder::set_entropy_bip39_mnemonic`]: crate::Builder::set_entropy_bip39_mnemonic
35+ pub fn generate_entropy_mnemonic ( ) -> Mnemonic {
36+ // bip39::Mnemonic supports 256 bit entropy max
37+ let mut entropy = [ 0 ; 32 ] ;
38+ thread_rng ( ) . fill_bytes ( & mut entropy) ;
39+ Mnemonic :: from_entropy ( & entropy) . unwrap ( )
40+ }
41+
2742pub ( crate ) fn read_or_generate_seed_file ( keys_seed_path : & str ) -> [ u8 ; WALLET_KEYS_SEED_LEN ] {
2843 if Path :: new ( & keys_seed_path) . exists ( ) {
2944 let seed = fs:: read ( keys_seed_path) . expect ( "Failed to read keys seed file" ) ;
@@ -263,3 +278,16 @@ where
263278 Error :: PersistenceFailed
264279 } )
265280}
281+
282+ #[ cfg( test) ]
283+ mod tests {
284+ use super :: * ;
285+
286+ #[ test]
287+ fn mnemonic_to_entropy_to_mnemonic ( ) {
288+ let mnemonic = generate_entropy_mnemonic ( ) ;
289+
290+ let entropy = mnemonic. to_entropy ( ) ;
291+ assert_eq ! ( mnemonic, Mnemonic :: from_entropy( & entropy) . unwrap( ) ) ;
292+ }
293+ }
0 commit comments