Skip to main content

relay_crypto/
record.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
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    pub created_at: DateTime<Utc>,
14    pub expires_at: Option<DateTime<Utc>>,
15}
16
17impl KeyRecord {
18    /// Gets the Ed25519 verifying key.
19    pub fn signing_key(
20        &self,
21    ) -> Result<ed25519_dalek::VerifyingKey, ed25519_dalek::ed25519::Error> {
22        ed25519_dalek::VerifyingKey::from_bytes(&self.ed25519)
23    }
24
25    /// Gets the X25519 public key.
26    pub fn public_key(&self) -> x25519_dalek::PublicKey {
27        x25519_dalek::PublicKey::from(self.x25519)
28    }
29
30    /// Checks if the key record is expired based on the current time.
31    pub fn is_expired(&self) -> bool {
32        if let Some(expires_at) = self.expires_at {
33            Utc::now() > expires_at
34        } else {
35            false
36        }
37    }
38}