aboutsummaryrefslogtreecommitdiffstats
path: root/src/transport/libp2p/config.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
commitdf74bf7026569514210982a2c7c3a4c36f469a32 (patch)
tree79b6b0d1fde6acfca37187de0dd459cea7c23335 /src/transport/libp2p/config.rs
parent6bab0c4af15988e3c0d4e38de3ee9c1f8c4a8be2 (diff)
allow creating all key files
Diffstat (limited to 'src/transport/libp2p/config.rs')
-rw-r--r--src/transport/libp2p/config.rs31
1 files changed, 23 insertions, 8 deletions
diff --git a/src/transport/libp2p/config.rs b/src/transport/libp2p/config.rs
index 6d26224..348a170 100644
--- a/src/transport/libp2p/config.rs
+++ b/src/transport/libp2p/config.rs
@@ -70,6 +70,9 @@ pub(crate) fn check(settings: &crate::config::Settings) -> Result<()> {
&settings.libp2p_ignore,
&settings.libp2p_prefer,
)?;
+ if settings.libp2p_key_create && settings.libp2p_key_file.is_none() {
+ bail!("libp2p-key-create creates the key at libp2p-key-file; set that too");
+ }
if settings.relay_only {
if settings.libp2p_listen.is_empty() {
bail!("relay-only needs libp2p-listen: where nodes reach the relay");
@@ -119,7 +122,10 @@ impl Libp2pConfig {
) -> Result<Self> {
let parse = parse_addresses;
Ok(Self {
- key: Self::load_key(settings.libp2p_key_file.as_deref())?,
+ key: Self::load_key(
+ settings.libp2p_key_file.as_deref(),
+ settings.libp2p_key_create,
+ )?,
listen: parse("libp2p-listen", &settings.libp2p_listen)?,
announce: parse("libp2p-announce", &settings.libp2p_announce)?,
relays: parse("libp2p-relays", &settings.libp2p_relays)?,
@@ -135,16 +141,25 @@ impl Libp2pConfig {
})
}
- /// The key in `path` (32 raw bytes, an Ed25519 secret key), or a new one each start
- /// without a file: the peer ID only says where a node is, so a new one costs nothing
- /// but the WHO that finds it again (`transport::identity`).
- pub(crate) fn load_key(path: Option<&Path>) -> Result<Keypair> {
+ /// The key in `path` (32 raw bytes, an Ed25519 secret key), created there with
+ /// `create` if there is no file; or a new one each start without a path: the peer
+ /// ID only says where a node is, so a new one costs nothing but the WHO that finds
+ /// it again (`transport::identity`).
+ pub(crate) fn load_key(path: Option<&Path>, create: bool) -> Result<Keypair> {
let Some(path) = path else {
return Ok(Keypair::generate_ed25519());
};
- let mut bytes = std::fs::read(path)
- .with_context(|| format!("Reading the libp2p key from {}", path.display()))?;
- crate::files::warn_if_readable_by_others(path, "libp2p key");
+ let (mut bytes, created) = crate::files::key_file(
+ path,
+ "libp2p key",
+ create,
+ "libp2p-key-create",
+ "a new peer ID",
+ 32,
+ )?;
+ if created {
+ info!(path = %path.display(), "Created a new libp2p key");
+ }
if bytes.len() != 32 {
bail!(
"The libp2p key in {} has {} bytes; it has 32",