Skip to content

Commit 57b5a96

Browse files
committed
Make mnemonic RNG selection explicit
Use direct OS randomness for the server's one-time mnemonic generation and an explicit OsRng in the E2E setup. A thread-local generator is not needed for these infrequent operations. AI-Assisted: OpenAI Codex
1 parent 7ffabc1 commit 57b5a96

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

e2e-tests/tests/e2e.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use e2e_tests::{
1717
LdkServerHandle, TestBitcoind,
1818
};
1919
use hex_conservative::{DisplayHex, FromHex};
20+
use ldk_node::bip39::{rand::rngs::OsRng, Language, Mnemonic};
2021
use ldk_node::bitcoin::hashes::{sha256, Hash};
22+
use ldk_node::entropy::NodeEntropy;
2123
use ldk_node::lightning::ln::msgs::SocketAddress;
2224
use ldk_node::lightning::offers::offer::Offer;
2325
use ldk_node::lightning_invoice::Bolt11Invoice;
@@ -1244,8 +1246,9 @@ async fn test_forwarded_payment_event() {
12441246
let b_addr = SocketAddress::from_str(&format!("127.0.0.1:{}", server_b.p2p_port)).unwrap();
12451247
builder_c.add_liquidity_source(b_node_id, b_addr, None, true);
12461248

1247-
let mnemonic_c = ldk_node::entropy::generate_entropy_mnemonic(None);
1248-
let node_entropy_c = ldk_node::entropy::NodeEntropy::from_bip39_mnemonic(mnemonic_c, None);
1249+
let mut rng = OsRng;
1250+
let mnemonic_c = Mnemonic::generate_in_with(&mut rng, Language::English, 24).unwrap();
1251+
let node_entropy_c = NodeEntropy::from_bip39_mnemonic(mnemonic_c, None);
12491252
let node_c = builder_c.build(node_entropy_c).unwrap();
12501253

12511254
node_c.start().unwrap();

ldk-server/src/util/entropy.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ use std::str::FromStr;
1212
use std::{fs, io};
1313

1414
use ldk_node::bip39::Mnemonic;
15-
use ldk_node::entropy::{generate_entropy_mnemonic, NodeEntropy};
15+
use ldk_node::entropy::NodeEntropy;
1616
use log::info;
1717

1818
use crate::util::write_new;
1919

2020
const DEFAULT_MNEMONIC_FILE: &str = "keys_mnemonic";
21+
const DEFAULT_MNEMONIC_ENTROPY_BYTES: usize = 32;
2122

2223
pub(crate) fn load_or_generate_node_entropy(storage_dir: &Path) -> io::Result<NodeEntropy> {
2324
let mnemonic_path = storage_dir.join(DEFAULT_MNEMONIC_FILE);
@@ -34,7 +35,7 @@ pub(crate) fn load_or_generate_node_entropy(storage_dir: &Path) -> io::Result<No
3435
if let Some(parent) = mnemonic_path.parent() {
3536
fs::create_dir_all(parent)?;
3637
}
37-
let mnemonic = generate_entropy_mnemonic(None);
38+
let mnemonic = generate_entropy_mnemonic()?;
3839
write_new(&mnemonic_path, format!("{}\n", mnemonic).as_bytes(), 0o600)?;
3940
info!(
4041
"Generated new BIP39 mnemonic at {}. Back up this file securely — it is required to recover on-chain funds.",
@@ -46,6 +47,12 @@ pub(crate) fn load_or_generate_node_entropy(storage_dir: &Path) -> io::Result<No
4647
Ok(NodeEntropy::from_bip39_mnemonic(mnemonic, None))
4748
}
4849

50+
fn generate_entropy_mnemonic() -> io::Result<Mnemonic> {
51+
let mut entropy = [0u8; DEFAULT_MNEMONIC_ENTROPY_BYTES];
52+
getrandom::getrandom(&mut entropy).map_err(io::Error::other)?;
53+
Ok(Mnemonic::from_entropy(&entropy).expect("32-byte entropy must produce a valid mnemonic"))
54+
}
55+
4956
#[cfg(test)]
5057
mod tests {
5158
use std::os::unix::fs::{MetadataExt, PermissionsExt};

0 commit comments

Comments
 (0)