Skip to main content

relay_crypto/
record.rs

1use serde::{Deserialize, Serialize};
2use time::OffsetDateTime;
3
4/// A record containing both Ed25519 and X25519 public keys along with metadata.
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct KeyRecord {
7    pub id: String,
8    pub version: u16,
9    #[serde(with = "hex::serde")]
10    pub ed25519: [u8; 32],
11    #[serde(with = "hex::serde")]
12    pub x25519: [u8; 32],
13    #[serde(with = "time::serde::rfc3339")]
14    pub created_at: OffsetDateTime,
15    #[serde(with = "time::serde::rfc3339::option")]
16    pub expires_at: Option<OffsetDateTime>,
17}
18
19impl KeyRecord {
20    /// Gets the Ed25519 verifying key.
21    pub fn signing_key(
22        &self,
23    ) -> Result<ed25519_dalek::VerifyingKey, ed25519_dalek::ed25519::Error> {
24        ed25519_dalek::VerifyingKey::from_bytes(&self.ed25519)
25    }
26
27    /// Gets the X25519 public key.
28    pub fn public_key(&self) -> x25519_dalek::PublicKey {
29        x25519_dalek::PublicKey::from(self.x25519)
30    }
31
32    /// Checks if the key record is expired based on the current time.
33    pub fn is_expired(&self) -> bool {
34        let now = OffsetDateTime::now_utc();
35        if let Some(expires_at) = self.expires_at {
36            now >= expires_at
37        } else {
38            false
39        }
40    }
41}