@@ -12,6 +12,7 @@ use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParam
12
12
use lightning:: util:: logger:: Logger ;
13
13
use lightning:: util:: ser:: { Readable , ReadableArgs , Writeable } ;
14
14
15
+ use bip39:: Mnemonic ;
15
16
use bitcoin:: hash_types:: { BlockHash , Txid } ;
16
17
use bitcoin:: hashes:: hex:: FromHex ;
17
18
use rand:: { thread_rng, RngCore } ;
@@ -24,6 +25,20 @@ use std::sync::Arc;
24
25
25
26
use super :: KVStore ;
26
27
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
+
27
42
pub ( crate ) fn read_or_generate_seed_file ( keys_seed_path : & str ) -> [ u8 ; WALLET_KEYS_SEED_LEN ] {
28
43
if Path :: new ( & keys_seed_path) . exists ( ) {
29
44
let seed = fs:: read ( keys_seed_path) . expect ( "Failed to read keys seed file" ) ;
@@ -263,3 +278,16 @@ where
263
278
Error :: PersistenceFailed
264
279
} )
265
280
}
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