Skip to content

Expose BIP39 Mnemonic generation in bindings #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
1 change: 1 addition & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace ldk_node {
Mnemonic generate_entropy_mnemonic();
};

dictionary Config {
Expand Down
28 changes: 28 additions & 0 deletions src/io/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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");
Expand Down Expand Up @@ -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());
}
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::*};

Expand All @@ -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};
Expand Down