diff options
| author | tv <tv@krebsco.de> | 2026-11-01 05:32:36 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-01 05:32:36 +0100 |
| commit | d264f2df01c59bf4627b496290a553aa376369ef (patch) | |
| tree | b0830b6a5b730223f4ef92ad8e4df863e60bc87d /src/transport/libp2p | |
| parent | 9f8feeab71ccb864606c336097e20dbb8e1d8c86 (diff) | |
allow creating all key files
Diffstat (limited to 'src/transport/libp2p')
| -rw-r--r-- | src/transport/libp2p/config.rs | 31 |
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", |
