From 291e9feecec4df0dea75ed85674cd136fadaef19 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Thu, 25 May 2023 15:53:48 +0200 Subject: [PATCH] Expose BIP39 Mnemonic generation in bindings We already re-export the `bip39` crate to allow users to generate Mnemonics. However, doing the same is not trivial in bindings. Here we expose a simple convenience function that returns a random mnemonic, allowig users to generate them without relying on a third-party library. --- bindings/ldk_node.udl | 1 + src/io/utils.rs | 28 ++++++++++++++++++++++++++++ src/lib.rs | 4 +++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/bindings/ldk_node.udl b/bindings/ldk_node.udl index 9ceae441b..12cc81df5 100644 --- a/bindings/ldk_node.udl +++ b/bindings/ldk_node.udl @@ -1,4 +1,5 @@ namespace ldk_node { + Mnemonic generate_entropy_mnemonic(); }; dictionary Config { diff --git a/src/io/utils.rs b/src/io/utils.rs index 08cb0bbd9..107ca3589 100644 --- a/src/io/utils.rs +++ b/src/io/utils.rs @@ -12,6 +12,7 @@ use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParam use lightning::util::logger::Logger; use lightning::util::ser::{Readable, ReadableArgs, Writeable}; +use bip39::Mnemonic; use bitcoin::hash_types::{BlockHash, Txid}; use bitcoin::hashes::hex::FromHex; use rand::{thread_rng, RngCore}; @@ -24,6 +25,20 @@ use std::sync::Arc; use super::KVStore; +/// Generates a random [BIP 39] mnemonic. +/// +/// 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 +/// [`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]; + thread_rng().fill_bytes(&mut entropy); + Mnemonic::from_entropy(&entropy).unwrap() +} + pub(crate) fn read_or_generate_seed_file(keys_seed_path: &str) -> [u8; WALLET_KEYS_SEED_LEN] { if Path::new(&keys_seed_path).exists() { let seed = fs::read(keys_seed_path).expect("Failed to read keys seed file"); @@ -236,3 +251,16 @@ where Error::PersistenceFailed }) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn mnemonic_to_entropy_to_mnemonic() { + let mnemonic = generate_entropy_mnemonic(); + + let entropy = mnemonic.to_entropy(); + assert_eq!(mnemonic, Mnemonic::from_entropy(&entropy).unwrap()); + } +} diff --git a/src/lib.rs b/src/lib.rs index 3a0526f23..b9336ace1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,7 +93,6 @@ mod wallet; pub use bip39; pub use bitcoin; pub use lightning; -use lightning::ln::msgs::RoutingMessageHandler; pub use lightning_invoice; pub use error::Error as NodeError; @@ -102,6 +101,8 @@ use error::Error; pub use event::Event; pub use types::NetAddress; +pub use io::utils::generate_entropy_mnemonic; + #[cfg(feature = "uniffi")] use {bitcoin::OutPoint, lightning::ln::PaymentSecret, uniffi_types::*}; @@ -126,6 +127,7 @@ use lightning::chain::{chainmonitor, BestBlock, Confirm, Watch}; use lightning::ln::channelmanager::{ self, ChainParameters, ChannelManagerReadArgs, PaymentId, RecipientOnionFields, Retry, }; +use lightning::ln::msgs::RoutingMessageHandler; use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler}; use lightning::ln::{PaymentHash, PaymentPreimage}; use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters};