Skip to content

Commit

Permalink
update server
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jan 9, 2025
1 parent 92d6068 commit cdedfed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
16 changes: 10 additions & 6 deletions pkarr/src/signed_packet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Signed DNS packet
use crate::{Error, Keypair, PublicKey, Result};
use bytes::Bytes;
use ed25519_dalek::Signature;
use hickory_proto::{
op::Message,
Expand Down Expand Up @@ -49,7 +48,12 @@ impl SignedPacket {
/// - Returns [crate::Error::InvalidEd25519PublicKey] if the first 32 bytes are invalid `ed25519` public key
/// - Returns [crate::Error::InvalidEd25519Signature] if the following 64 bytes are invalid `ed25519` signature
/// - Returns [crate::Error::DnsError] if it failed to parse the DNS Packet after the first 104 bytes
pub fn from_bytes(bytes: &Bytes) -> Result<SignedPacket> {
pub fn from_bytes(bytes: &[u8]) -> Result<SignedPacket> {
Self::from_bytes_with_last_seen(bytes, system_time())
}

/// Document ME
pub fn from_bytes_with_last_seen(bytes: &[u8], last_seen: u64) -> Result<SignedPacket> {
if bytes.len() < OFFSET {
return Err(Error::InvalidSignedPacketBytesLength(bytes.len()));
}
Expand All @@ -60,7 +64,7 @@ impl SignedPacket {
let signature = Signature::from_bytes(bytes[32..96].try_into().unwrap());
let timestamp = u64::from_be_bytes(bytes[96..104].try_into().unwrap());

let raw_message = &bytes.slice(104..);
let raw_message = &bytes[OFFSET..];
public_key.verify(&signable(timestamp, raw_message), &signature)?;
let message = Message::from_vec(raw_message)?;

Expand All @@ -69,7 +73,7 @@ impl SignedPacket {
signature,
timestamp,
message,
last_seen: system_time(),
last_seen,
})
}

Expand All @@ -86,7 +90,7 @@ impl SignedPacket {
bytes.extend_from_slice(public_key.as_bytes());
bytes.extend_from_slice(payload);

SignedPacket::from_bytes(&bytes.into())
SignedPacket::from_bytes(&bytes)
}

/// Creates a new [SignedPacket] from a [Keypair] and a DNS [Packet].
Expand Down Expand Up @@ -471,7 +475,7 @@ mod tests {
fn from_too_large_bytes() {
let keypair = Keypair::random();

let bytes = Bytes::from(vec![0; 1073]);
let bytes = vec![0; 1073];
let error = SignedPacket::from_relay_payload(&keypair.public_key(), &bytes);

assert!(error.is_err());
Expand Down
9 changes: 3 additions & 6 deletions server/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ impl<'a> BytesEncode<'a> for SignedPacketCodec {
type EItem = SignedPacket;

fn bytes_encode(signed_packet: &Self::EItem) -> Result<Cow<[u8]>, BoxedError> {
let bytes = signed_packet.as_bytes();

let bytes = signed_packet.to_vec();
let mut vec = Vec::with_capacity(bytes.len() + 8);

vec.extend(<U64<LittleEndian>>::bytes_encode(signed_packet.last_seen())?.as_ref());
Expand All @@ -57,10 +56,8 @@ impl<'a> BytesDecode<'a> for SignedPacketCodec {
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
let last_seen = <U64<LittleEndian>>::bytes_decode(bytes)?;

Ok(SignedPacket::from_bytes_unchecked(
&bytes[8..].to_vec().into(),
last_seen,
))
let packet = SignedPacket::from_bytes_with_last_seen(&bytes[8..], last_seen)?;
Ok(packet)
}
}

Expand Down

0 comments on commit cdedfed

Please sign in to comment.