Skip to content

Commit c8b94d6

Browse files
author
tnull
committed
f - Reuse private file creation for node entropy
Node entropy mnemonic creation should share the existing private-file write path instead of keeping a separate implementation, while the legacy seed filename should stay scoped to the tests that need it. Co-Authored-By: HAL 9000
1 parent 8231b84 commit c8b94d6

1 file changed

Lines changed: 5 additions & 15 deletions

File tree

ldk-server/src/util/entropy.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@
88
// licenses.
99

1010
use std::fs;
11-
use std::io::{self, Write};
12-
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
11+
use std::io;
1312
use std::path::{Path, PathBuf};
1413
use std::str::FromStr;
1514

1615
use ldk_node::bip39::Mnemonic;
1716
use ldk_node::entropy::{generate_entropy_mnemonic, NodeEntropy};
1817
use log::info;
1918

20-
use crate::util::config::EntropyConfig;
19+
use crate::util::{config::EntropyConfig, write_new};
2120

2221
const DEFAULT_MNEMONIC_FILE: &str = "keys_mnemonic";
23-
#[cfg(test)]
24-
const LEGACY_SEED_FILE: &str = "keys_seed";
2522

2623
pub(crate) fn load_or_generate_node_entropy(
2724
storage_dir: &Path, entropy_config: &EntropyConfig,
@@ -49,7 +46,7 @@ pub(crate) fn load_or_generate_node_entropy(
4946
fs::create_dir_all(parent)?;
5047
}
5148
let mnemonic = generate_entropy_mnemonic(None);
52-
write_mnemonic_file(&mnemonic_path, &mnemonic)?;
49+
write_new(&mnemonic_path, format!("{}\n", mnemonic).as_bytes(), 0o600)?;
5350
info!(
5451
"Generated new BIP39 mnemonic at {}. Back up this file securely — it is required to recover on-chain funds.",
5552
mnemonic_path.display()
@@ -60,19 +57,12 @@ pub(crate) fn load_or_generate_node_entropy(
6057
Ok(NodeEntropy::from_bip39_mnemonic(mnemonic, None))
6158
}
6259

63-
fn write_mnemonic_file(path: &Path, mnemonic: &Mnemonic) -> io::Result<()> {
64-
let mut f = fs::OpenOptions::new().create_new(true).write(true).mode(0o600).open(path)?;
65-
f.set_permissions(fs::Permissions::from_mode(0o600))?;
66-
writeln!(f, "{}", mnemonic)?;
67-
f.sync_all()?;
68-
Ok(())
69-
}
70-
7160
#[cfg(test)]
7261
mod tests {
7362
use super::*;
74-
use std::os::unix::fs::MetadataExt;
63+
use std::os::unix::fs::{MetadataExt, PermissionsExt};
7564

65+
const LEGACY_SEED_FILE: &str = "keys_seed";
7666
const KNOWN_MNEMONIC: &str = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art";
7767

7868
fn tempdir(tag: &str) -> PathBuf {

0 commit comments

Comments
 (0)