Skip to content

Commit 218aad3

Browse files
authored
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
1 parent 542f56d commit 218aad3

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

iroh-base/src/key.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
time::Duration,
1111
};
1212

13-
pub use ed25519_dalek::{Signature, PUBLIC_KEY_LENGTH};
13+
pub use ed25519_dalek::Signature;
1414
use ed25519_dalek::{SignatureError, SigningKey, VerifyingKey};
1515
use once_cell::sync::OnceCell;
1616
use rand_core::CryptoRngCore;
@@ -180,6 +180,8 @@ impl PublicKey {
180180
pub fn fmt_short(&self) -> String {
181181
data_encoding::HEXLOWER.encode(&self.as_bytes()[..5])
182182
}
183+
184+
pub const LENGTH: usize = ed25519_dalek::PUBLIC_KEY_LENGTH;
183185
}
184186

185187
impl TryFrom<&[u8]> for PublicKey {
@@ -422,15 +424,15 @@ impl TryFrom<&[u8]> for SecretKey {
422424
fn decode_base32_hex(s: &str) -> Result<[u8; 32], KeyParsingError> {
423425
let mut bytes = [0u8; 32];
424426

425-
let res = if s.len() == PUBLIC_KEY_LENGTH * 2 {
427+
let res = if s.len() == PublicKey::LENGTH * 2 {
426428
// hex
427429
data_encoding::HEXLOWER.decode_mut(s.as_bytes(), &mut bytes)
428430
} else {
429431
data_encoding::BASE32_NOPAD.decode_mut(s.to_ascii_uppercase().as_bytes(), &mut bytes)
430432
};
431433
match res {
432434
Ok(len) => {
433-
if len != PUBLIC_KEY_LENGTH {
435+
if len != PublicKey::LENGTH {
434436
return Err(KeyParsingError::DecodeInvalidLength);
435437
}
436438
}

iroh-base/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ mod node_addr;
1313
mod relay_url;
1414

1515
#[cfg(feature = "key")]
16-
pub use self::key::{
17-
KeyParsingError, NodeId, PublicKey, SecretKey, SharedSecret, Signature, PUBLIC_KEY_LENGTH,
18-
};
16+
pub use self::key::{KeyParsingError, NodeId, PublicKey, SecretKey, SharedSecret, Signature};
1917
#[cfg(feature = "key")]
2018
pub use self::node_addr::NodeAddr;
2119
#[cfg(feature = "relay")]

iroh-relay/src/protos/relay.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use bytes::{Buf, BufMut, Bytes, BytesMut};
2020
use futures_lite::{Stream, StreamExt};
2121
use futures_sink::Sink;
2222
use futures_util::SinkExt;
23-
use iroh_base::{PublicKey, SecretKey, Signature, PUBLIC_KEY_LENGTH};
23+
use iroh_base::{PublicKey, SecretKey, Signature};
2424
use postcard::experimental::max_size::MaxSize;
2525
use serde::{Deserialize, Serialize};
2626
use tokio_util::codec::{Decoder, Encoder};
@@ -268,15 +268,15 @@ impl Frame {
268268
client_public_key: _,
269269
message,
270270
signature: _,
271-
} => MAGIC.len() + PUBLIC_KEY_LENGTH + message.len() + Signature::BYTE_SIZE,
272-
Frame::SendPacket { dst_key: _, packet } => PUBLIC_KEY_LENGTH + packet.len(),
271+
} => MAGIC.len() + PublicKey::LENGTH + message.len() + Signature::BYTE_SIZE,
272+
Frame::SendPacket { dst_key: _, packet } => PublicKey::LENGTH + packet.len(),
273273
Frame::RecvPacket {
274274
src_key: _,
275275
content,
276-
} => PUBLIC_KEY_LENGTH + content.len(),
276+
} => PublicKey::LENGTH + content.len(),
277277
Frame::KeepAlive => 0,
278278
Frame::NotePreferred { .. } => 1,
279-
Frame::NodeGone { .. } => PUBLIC_KEY_LENGTH,
279+
Frame::NodeGone { .. } => PublicKey::LENGTH,
280280
Frame::Ping { .. } => 8,
281281
Frame::Pong { .. } => 8,
282282
Frame::Health { problem } => problem.len(),
@@ -368,7 +368,7 @@ impl Frame {
368368
let res = match frame_type {
369369
FrameType::ClientInfo => {
370370
ensure!(
371-
content.len() >= PUBLIC_KEY_LENGTH + Signature::BYTE_SIZE + MAGIC.len(),
371+
content.len() >= PublicKey::LENGTH + Signature::BYTE_SIZE + MAGIC.len(),
372372
"invalid client info frame length: {}",
373373
content.len()
374374
);
@@ -379,8 +379,8 @@ impl Frame {
379379

380380
let start = MAGIC.len();
381381
let client_public_key =
382-
PublicKey::try_from(&content[start..start + PUBLIC_KEY_LENGTH])?;
383-
let start = start + PUBLIC_KEY_LENGTH;
382+
PublicKey::try_from(&content[start..start + PublicKey::LENGTH])?;
383+
let start = start + PublicKey::LENGTH;
384384
let signature =
385385
Signature::from_slice(&content[start..start + Signature::BYTE_SIZE])?;
386386
let start = start + Signature::BYTE_SIZE;
@@ -393,32 +393,32 @@ impl Frame {
393393
}
394394
FrameType::SendPacket => {
395395
ensure!(
396-
content.len() >= PUBLIC_KEY_LENGTH,
396+
content.len() >= PublicKey::LENGTH,
397397
"invalid send packet frame length: {}",
398398
content.len()
399399
);
400-
let packet_len = content.len() - PUBLIC_KEY_LENGTH;
400+
let packet_len = content.len() - PublicKey::LENGTH;
401401
ensure!(
402402
packet_len <= MAX_PACKET_SIZE,
403403
"data packet longer ({packet_len}) than max of {MAX_PACKET_SIZE}"
404404
);
405-
let dst_key = PublicKey::try_from(&content[..PUBLIC_KEY_LENGTH])?;
406-
let packet = content.slice(PUBLIC_KEY_LENGTH..);
405+
let dst_key = PublicKey::try_from(&content[..PublicKey::LENGTH])?;
406+
let packet = content.slice(PublicKey::LENGTH..);
407407
Self::SendPacket { dst_key, packet }
408408
}
409409
FrameType::RecvPacket => {
410410
ensure!(
411-
content.len() >= PUBLIC_KEY_LENGTH,
411+
content.len() >= PublicKey::LENGTH,
412412
"invalid recv packet frame length: {}",
413413
content.len()
414414
);
415-
let packet_len = content.len() - PUBLIC_KEY_LENGTH;
415+
let packet_len = content.len() - PublicKey::LENGTH;
416416
ensure!(
417417
packet_len <= MAX_PACKET_SIZE,
418418
"data packet longer ({packet_len}) than max of {MAX_PACKET_SIZE}"
419419
);
420-
let src_key = PublicKey::try_from(&content[..PUBLIC_KEY_LENGTH])?;
421-
let content = content.slice(PUBLIC_KEY_LENGTH..);
420+
let src_key = PublicKey::try_from(&content[..PublicKey::LENGTH])?;
421+
let content = content.slice(PublicKey::LENGTH..);
422422
Self::RecvPacket { src_key, content }
423423
}
424424
FrameType::KeepAlive => {
@@ -436,7 +436,7 @@ impl Frame {
436436
}
437437
FrameType::PeerGone => {
438438
anyhow::ensure!(
439-
content.len() == PUBLIC_KEY_LENGTH,
439+
content.len() == PublicKey::LENGTH,
440440
"invalid peer gone frame length"
441441
);
442442
let peer = PublicKey::try_from(&content[..32])?;

iroh/src/disco.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const TX_LEN: usize = 12;
4444
/// Header: Type | Version
4545
const HEADER_LEN: usize = 2;
4646

47-
const PING_LEN: usize = TX_LEN + iroh_base::PUBLIC_KEY_LENGTH;
47+
const PING_LEN: usize = TX_LEN + iroh_base::PublicKey::LENGTH;
4848
const EP_LENGTH: usize = 16 + 2; // 16 byte IP address + 2 byte port
4949

5050
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -206,7 +206,7 @@ impl Ping {
206206
// Deliberately lax on longer-than-expected messages, for future compatibility.
207207
ensure!(p.len() >= PING_LEN, "message too short");
208208
let tx_id: [u8; TX_LEN] = p[..TX_LEN].try_into().expect("length checked");
209-
let raw_key = &p[TX_LEN..TX_LEN + iroh_base::PUBLIC_KEY_LENGTH];
209+
let raw_key = &p[TX_LEN..TX_LEN + iroh_base::PublicKey::LENGTH];
210210
let node_key = PublicKey::try_from(raw_key)?;
211211
let tx_id = stun_rs::TransactionId::from(tx_id);
212212

iroh/src/magicsock/relay_actor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{
1313
use anyhow::Context;
1414
use backoff::backoff::Backoff;
1515
use bytes::{Bytes, BytesMut};
16-
use iroh_base::{NodeId, RelayUrl, PUBLIC_KEY_LENGTH};
16+
use iroh_base::{NodeId, PublicKey, RelayUrl};
1717
use iroh_metrics::{inc, inc_by};
1818
use iroh_relay::{self as relay, client::ClientError, ReceivedMessage, MAX_PACKET_SIZE};
1919
use tokio::{
@@ -422,7 +422,7 @@ impl RelayActor {
422422
}
423423

424424
async fn send_relay(&mut self, url: &RelayUrl, contents: RelayContents, remote_node: NodeId) {
425-
const PAYLOAD_SIZE: usize = MAX_PACKET_SIZE - PUBLIC_KEY_LENGTH;
425+
const PAYLOAD_SIZE: usize = MAX_PACKET_SIZE - PublicKey::LENGTH;
426426
let total_bytes = contents.iter().map(|c| c.len() as u64).sum::<u64>();
427427
trace!(
428428
%url,

0 commit comments

Comments
 (0)