aboutsummaryrefslogtreecommitdiffstats
path: root/src/identity.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-01 05:32:36 +0100
committertv <tv@krebsco.de>2026-11-01 05:32:36 +0100
commitd264f2df01c59bf4627b496290a553aa376369ef (patch)
treeb0830b6a5b730223f4ef92ad8e4df863e60bc87d /src/identity.rs
parent9f8feeab71ccb864606c336097e20dbb8e1d8c86 (diff)
allow creating all key files
Diffstat (limited to 'src/identity.rs')
-rw-r--r--src/identity.rs58
1 files changed, 15 insertions, 43 deletions
diff --git a/src/identity.rs b/src/identity.rs
index 8efe30f..04b7e6f 100644
--- a/src/identity.rs
+++ b/src/identity.rs
@@ -3,13 +3,12 @@
//!
//! The node ID names the node whatever transport reaches it: a transport address only
//! says where it is (`docs/peers.md`, node IDs and addresses). A new key is a new
-//! node, so the key is created only when asked to (`--create-node-key`), never because
+//! node, so the key is created only when asked to (`--node-key-create`), never because
//! a path was mistyped.
-use std::io::Write;
use std::path::Path;
-use anyhow::{Context, Result, bail};
+use anyhow::{Result, bail};
use ed25519_dalek::SigningKey;
use tracing::info;
@@ -30,25 +29,14 @@ impl NodeKey {
/// Reads the key from `path`; with `create`, makes a new one there if the file
/// doesn't exist.
pub(crate) fn load(path: &Path, create: bool) -> Result<Self> {
- let bytes = match std::fs::read(path) {
- Ok(bytes) => bytes,
- Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
- if !create {
- bail!(
- "The node key file {} doesn't exist; add --create-node-key to create a new node key there (a new key is a new node)",
- path.display()
- );
- }
- let key = Self::create(path)?;
- info!(path = %path.display(), node_id = %key.id(), "Created a new node key: this is a new node");
- return Ok(key);
- }
- Err(error) => {
- return Err(error)
- .with_context(|| format!("Reading the node key from {}", path.display()));
- }
- };
- crate::files::warn_if_readable_by_others(path, "node key");
+ let (bytes, created) = crate::files::key_file(
+ path,
+ "node key",
+ create,
+ "node-key-create",
+ "a new key is a new node",
+ KEY_BYTES,
+ )?;
let Ok(seed) = <[u8; KEY_BYTES]>::try_from(bytes.as_slice()) else {
bail!(
"The node key in {} has {} bytes; a node key has {KEY_BYTES}",
@@ -56,7 +44,11 @@ impl NodeKey {
bytes.len()
);
};
- Ok(Self::from_seed(seed))
+ let key = Self::from_seed(seed);
+ if created {
+ info!(path = %path.display(), node_id = %key.id(), "Created a new node key: this is a new node");
+ }
+ Ok(key)
}
/// A key from its 32 secret bytes: for simulated nodes, which have no key files.
@@ -66,26 +58,6 @@ impl NodeKey {
}
}
- /// A new key in a new file at `path` (only its owner may read it).
- fn create(path: &Path) -> Result<Self> {
- let mut seed = [0u8; KEY_BYTES];
- getrandom::fill(&mut seed).map_err(|error| anyhow::anyhow!("No randomness: {error}"))?;
- let mut options = std::fs::OpenOptions::new();
- options.write(true).create_new(true);
- #[cfg(unix)]
- {
- use std::os::unix::fs::OpenOptionsExt;
- options.mode(0o600);
- }
- let written = (|| {
- let mut file = options.open(path)?;
- file.write_all(&seed)?;
- file.sync_all()
- })();
- written.with_context(|| format!("Creating the node key file {}", path.display()))?;
- Ok(Self::from_seed(seed))
- }
-
/// This node's ID.
pub(crate) fn id(&self) -> String {
node_id_of(&self.public())