relay-crypto 0.2.0-alpha.0

The crypto library for the Relay Ecosystem.
Documentation
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// A record containing both Ed25519 and X25519 public keys along with metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyRecord {
    pub id: String,
    pub version: u16,
    #[serde(with = "hex::serde")]
    pub ed25519: [u8; 32],
    #[serde(with = "hex::serde")]
    pub x25519: [u8; 32],
    pub created_at: DateTime<Utc>,
    pub expires_at: Option<DateTime<Utc>>,
}

impl KeyRecord {
    /// Gets the Ed25519 verifying key.
    pub fn signing_key(
        &self,
    ) -> Result<ed25519_dalek::VerifyingKey, ed25519_dalek::ed25519::Error> {
        ed25519_dalek::VerifyingKey::from_bytes(&self.ed25519)
    }

    /// Gets the X25519 public key.
    pub fn public_key(&self) -> x25519_dalek::PublicKey {
        x25519_dalek::PublicKey::from(self.x25519)
    }

    /// Checks if the key record is expired based on the current time.
    pub fn is_expired(&self) -> bool {
        if let Some(expires_at) = self.expires_at {
            Utc::now() > expires_at
        } else {
            false
        }
    }
}