Skip to content

Commit

Permalink
refactor!: Make PUBLIC_KEY_LENGTH a const that is on PublicKey (#3043)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
rklaehn authored Dec 13, 2024
1 parent 542f56d commit 218aad3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
8 changes: 5 additions & 3 deletions iroh-base/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -422,15 +424,15 @@ 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 {
data_encoding::BASE32_NOPAD.decode_mut(s.to_ascii_uppercase().as_bytes(), &mut bytes)
};
match res {
Ok(len) => {
if len != PUBLIC_KEY_LENGTH {
if len != PublicKey::LENGTH {
return Err(KeyParsingError::DecodeInvalidLength);
}
}
Expand Down
4 changes: 1 addition & 3 deletions iroh-base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
34 changes: 17 additions & 17 deletions iroh-relay/src/protos/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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()
);
Expand All @@ -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;
Expand All @@ -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 => {
Expand All @@ -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])?;
Expand Down
4 changes: 2 additions & 2 deletions iroh/src/disco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions iroh/src/magicsock/relay_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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::<u64>();
trace!(
%url,
Expand Down

0 comments on commit 218aad3

Please sign in to comment.