Isabelle's Lazy Message Protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 rivejä
1.2 KiB

  1. // namespacing
  2. use crate::{Packet, Result};
  3. use chrono::prelude::*;
  4. use serde::{Deserialize, Serialize};
  5. use uuid::Uuid;
  6. /// packet to be used when server/client are coming to an agreement on
  7. /// encryption key material
  8. #[derive(Debug, Clone, Serialize, Deserialize)]
  9. pub struct Agreement {
  10. pub timestamp: i64,
  11. pub message_id: u128,
  12. pub public_key: Vec<u8>,
  13. }
  14. impl Agreement {
  15. /// creates a new agreement packet from a public key
  16. pub fn new(public_key: Vec<u8>) -> Agreement {
  17. let timestamp = Utc::now().timestamp();
  18. let message_id = Uuid::new_v4().as_u128();
  19. Agreement {
  20. timestamp,
  21. message_id,
  22. public_key,
  23. }
  24. }
  25. }
  26. impl crate::Sendable for Agreement {
  27. fn to_packet(&self, encrypt_flag: crate::EncryptFlag) -> Result<Packet> {
  28. let contents: Vec<u8> = serde_json::to_string(&self)?.into_bytes();
  29. let kind = 0xff;
  30. Ok(Packet::new(kind, contents, encrypt_flag))
  31. }
  32. fn from_packet(packet: Packet) -> Result<Self> {
  33. let contents = &String::from_utf8(packet.contents)?;
  34. let agreement: Agreement = serde_json::from_str(contents)?;
  35. Ok(agreement)
  36. }
  37. fn packet_kind(&self) -> u8 {
  38. 0xff
  39. }
  40. }