// namespacing use crate::{Packet, Result}; use chrono::prelude::*; use serde::{Deserialize, Serialize}; use uuid::Uuid; /// packet to be used when server/client are coming to an agreement on /// encryption key material #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Agreement { pub timestamp: i64, pub message_id: u128, pub public_key: Vec, } impl Agreement { /// creates a new agreement packet from a public key pub fn new(public_key: Vec) -> Agreement { let timestamp = Utc::now().timestamp(); let message_id = Uuid::new_v4().as_u128(); Agreement { timestamp, message_id, public_key, } } } impl crate::Sendable for Agreement { fn to_packet(&self, encrypt_flag: crate::EncryptFlag) -> Result { let contents: Vec = serde_json::to_string(&self)?.into_bytes(); let kind = 0xff; Ok(Packet::new(kind, contents, encrypt_flag)) } fn from_packet(packet: Packet) -> Result { let contents = &String::from_utf8(packet.contents)?; let agreement: Agreement = serde_json::from_str(contents)?; Ok(agreement) } fn packet_kind(&self) -> u8 { 0xff } }