From 218aad30b42f01b132d5c513d414efeb7b7f47d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BCdiger=20Klaehn?= Date: Fri, 13 Dec 2024 18:45:00 +0200 Subject: [PATCH] refactor!: Make PUBLIC_KEY_LENGTH a const that is on PublicKey (#3043) ## Description Make PUBLIC_KEY_LENGTH a const that is on PublicKey that way it is not so in your face ## Breaking Changes - PUBLIC_KEY_LENGTH is moved from a top level constant to PublicKey::LENGTH --- iroh-base/src/key.rs | 8 +++++--- iroh-base/src/lib.rs | 4 +--- iroh-relay/src/protos/relay.rs | 34 +++++++++++++++---------------- iroh/src/disco.rs | 4 ++-- iroh/src/magicsock/relay_actor.rs | 4 ++-- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/iroh-base/src/key.rs b/iroh-base/src/key.rs index a3a1714ffa..eeb13ce8ef 100644 --- a/iroh-base/src/key.rs +++ b/iroh-base/src/key.rs @@ -10,7 +10,7 @@ use std::{ time::Duration, }; -pub use ed25519_dalek::{Signature, PUBLIC_KEY_LENGTH}; +pub use ed25519_dalek::Signature; use ed25519_dalek::{SignatureError, SigningKey, VerifyingKey}; use once_cell::sync::OnceCell; use rand_core::CryptoRngCore; @@ -180,6 +180,8 @@ impl PublicKey { pub fn fmt_short(&self) -> String { data_encoding::HEXLOWER.encode(&self.as_bytes()[..5]) } + + pub const LENGTH: usize = ed25519_dalek::PUBLIC_KEY_LENGTH; } impl TryFrom<&[u8]> for PublicKey { @@ -422,7 +424,7 @@ impl TryFrom<&[u8]> for SecretKey { fn decode_base32_hex(s: &str) -> Result<[u8; 32], KeyParsingError> { let mut bytes = [0u8; 32]; - let res = if s.len() == PUBLIC_KEY_LENGTH * 2 { + let res = if s.len() == PublicKey::LENGTH * 2 { // hex data_encoding::HEXLOWER.decode_mut(s.as_bytes(), &mut bytes) } else { @@ -430,7 +432,7 @@ fn decode_base32_hex(s: &str) -> Result<[u8; 32], KeyParsingError> { }; match res { Ok(len) => { - if len != PUBLIC_KEY_LENGTH { + if len != PublicKey::LENGTH { return Err(KeyParsingError::DecodeInvalidLength); } } diff --git a/iroh-base/src/lib.rs b/iroh-base/src/lib.rs index ac20731a37..df78ddbebc 100644 --- a/iroh-base/src/lib.rs +++ b/iroh-base/src/lib.rs @@ -13,9 +13,7 @@ mod node_addr; mod relay_url; #[cfg(feature = "key")] -pub use self::key::{ - KeyParsingError, NodeId, PublicKey, SecretKey, SharedSecret, Signature, PUBLIC_KEY_LENGTH, -}; +pub use self::key::{KeyParsingError, NodeId, PublicKey, SecretKey, SharedSecret, Signature}; #[cfg(feature = "key")] pub use self::node_addr::NodeAddr; #[cfg(feature = "relay")] diff --git a/iroh-relay/src/protos/relay.rs b/iroh-relay/src/protos/relay.rs index bab86791dd..5f74acd151 100644 --- a/iroh-relay/src/protos/relay.rs +++ b/iroh-relay/src/protos/relay.rs @@ -20,7 +20,7 @@ use bytes::{Buf, BufMut, Bytes, BytesMut}; use futures_lite::{Stream, StreamExt}; use futures_sink::Sink; use futures_util::SinkExt; -use iroh_base::{PublicKey, SecretKey, Signature, PUBLIC_KEY_LENGTH}; +use iroh_base::{PublicKey, SecretKey, Signature}; use postcard::experimental::max_size::MaxSize; use serde::{Deserialize, Serialize}; use tokio_util::codec::{Decoder, Encoder}; @@ -268,15 +268,15 @@ impl Frame { client_public_key: _, message, signature: _, - } => MAGIC.len() + PUBLIC_KEY_LENGTH + message.len() + Signature::BYTE_SIZE, - Frame::SendPacket { dst_key: _, packet } => PUBLIC_KEY_LENGTH + packet.len(), + } => MAGIC.len() + PublicKey::LENGTH + message.len() + Signature::BYTE_SIZE, + Frame::SendPacket { dst_key: _, packet } => PublicKey::LENGTH + packet.len(), Frame::RecvPacket { src_key: _, content, - } => PUBLIC_KEY_LENGTH + content.len(), + } => PublicKey::LENGTH + content.len(), Frame::KeepAlive => 0, Frame::NotePreferred { .. } => 1, - Frame::NodeGone { .. } => PUBLIC_KEY_LENGTH, + Frame::NodeGone { .. } => PublicKey::LENGTH, Frame::Ping { .. } => 8, Frame::Pong { .. } => 8, Frame::Health { problem } => problem.len(), @@ -368,7 +368,7 @@ impl Frame { let res = match frame_type { FrameType::ClientInfo => { ensure!( - content.len() >= PUBLIC_KEY_LENGTH + Signature::BYTE_SIZE + MAGIC.len(), + content.len() >= PublicKey::LENGTH + Signature::BYTE_SIZE + MAGIC.len(), "invalid client info frame length: {}", content.len() ); @@ -379,8 +379,8 @@ impl Frame { let start = MAGIC.len(); let client_public_key = - PublicKey::try_from(&content[start..start + PUBLIC_KEY_LENGTH])?; - let start = start + PUBLIC_KEY_LENGTH; + PublicKey::try_from(&content[start..start + PublicKey::LENGTH])?; + let start = start + PublicKey::LENGTH; let signature = Signature::from_slice(&content[start..start + Signature::BYTE_SIZE])?; let start = start + Signature::BYTE_SIZE; @@ -393,32 +393,32 @@ impl Frame { } FrameType::SendPacket => { ensure!( - content.len() >= PUBLIC_KEY_LENGTH, + content.len() >= PublicKey::LENGTH, "invalid send packet frame length: {}", content.len() ); - let packet_len = content.len() - PUBLIC_KEY_LENGTH; + let packet_len = content.len() - PublicKey::LENGTH; ensure!( packet_len <= MAX_PACKET_SIZE, "data packet longer ({packet_len}) than max of {MAX_PACKET_SIZE}" ); - let dst_key = PublicKey::try_from(&content[..PUBLIC_KEY_LENGTH])?; - let packet = content.slice(PUBLIC_KEY_LENGTH..); + let dst_key = PublicKey::try_from(&content[..PublicKey::LENGTH])?; + let packet = content.slice(PublicKey::LENGTH..); Self::SendPacket { dst_key, packet } } FrameType::RecvPacket => { ensure!( - content.len() >= PUBLIC_KEY_LENGTH, + content.len() >= PublicKey::LENGTH, "invalid recv packet frame length: {}", content.len() ); - let packet_len = content.len() - PUBLIC_KEY_LENGTH; + let packet_len = content.len() - PublicKey::LENGTH; ensure!( packet_len <= MAX_PACKET_SIZE, "data packet longer ({packet_len}) than max of {MAX_PACKET_SIZE}" ); - let src_key = PublicKey::try_from(&content[..PUBLIC_KEY_LENGTH])?; - let content = content.slice(PUBLIC_KEY_LENGTH..); + let src_key = PublicKey::try_from(&content[..PublicKey::LENGTH])?; + let content = content.slice(PublicKey::LENGTH..); Self::RecvPacket { src_key, content } } FrameType::KeepAlive => { @@ -436,7 +436,7 @@ impl Frame { } FrameType::PeerGone => { anyhow::ensure!( - content.len() == PUBLIC_KEY_LENGTH, + content.len() == PublicKey::LENGTH, "invalid peer gone frame length" ); let peer = PublicKey::try_from(&content[..32])?; diff --git a/iroh/src/disco.rs b/iroh/src/disco.rs index 5d06f73c42..8e67ce70d0 100644 --- a/iroh/src/disco.rs +++ b/iroh/src/disco.rs @@ -44,7 +44,7 @@ const TX_LEN: usize = 12; /// Header: Type | Version const HEADER_LEN: usize = 2; -const PING_LEN: usize = TX_LEN + iroh_base::PUBLIC_KEY_LENGTH; +const PING_LEN: usize = TX_LEN + iroh_base::PublicKey::LENGTH; const EP_LENGTH: usize = 16 + 2; // 16 byte IP address + 2 byte port #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -206,7 +206,7 @@ impl Ping { // Deliberately lax on longer-than-expected messages, for future compatibility. ensure!(p.len() >= PING_LEN, "message too short"); let tx_id: [u8; TX_LEN] = p[..TX_LEN].try_into().expect("length checked"); - let raw_key = &p[TX_LEN..TX_LEN + iroh_base::PUBLIC_KEY_LENGTH]; + let raw_key = &p[TX_LEN..TX_LEN + iroh_base::PublicKey::LENGTH]; let node_key = PublicKey::try_from(raw_key)?; let tx_id = stun_rs::TransactionId::from(tx_id); diff --git a/iroh/src/magicsock/relay_actor.rs b/iroh/src/magicsock/relay_actor.rs index 4ea796a6b3..48cb02b808 100644 --- a/iroh/src/magicsock/relay_actor.rs +++ b/iroh/src/magicsock/relay_actor.rs @@ -13,7 +13,7 @@ use std::{ use anyhow::Context; use backoff::backoff::Backoff; use bytes::{Bytes, BytesMut}; -use iroh_base::{NodeId, RelayUrl, PUBLIC_KEY_LENGTH}; +use iroh_base::{NodeId, PublicKey, RelayUrl}; use iroh_metrics::{inc, inc_by}; use iroh_relay::{self as relay, client::ClientError, ReceivedMessage, MAX_PACKET_SIZE}; use tokio::{ @@ -422,7 +422,7 @@ impl RelayActor { } async fn send_relay(&mut self, url: &RelayUrl, contents: RelayContents, remote_node: NodeId) { - const PAYLOAD_SIZE: usize = MAX_PACKET_SIZE - PUBLIC_KEY_LENGTH; + const PAYLOAD_SIZE: usize = MAX_PACKET_SIZE - PublicKey::LENGTH; let total_bytes = contents.iter().map(|c| c.len() as u64).sum::(); trace!( %url,