From 4f9775526edba68bbca307b8f67bef7adcb13e1f Mon Sep 17 00:00:00 2001 From: NicolasCantu Date: Tue, 7 Jan 2025 09:24:56 +0100 Subject: [PATCH] Rm CachedMessage --- src/network.rs | 94 -------------------------------------------------- 1 file changed, 94 deletions(-) diff --git a/src/network.rs b/src/network.rs index 7c65c21..a141d15 100644 --- a/src/network.rs +++ b/src/network.rs @@ -194,97 +194,3 @@ impl Envelope { serde_json::to_string(self).unwrap() } } - -#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Tsify, Clone)] -pub enum CachedMessageStatus { - #[default] - NoStatus, - CipherWaitingTx, - TxWaitingPrd, -} - -/// Unique struct for both 4nk messages and notification/key exchange, both rust and ts -#[derive(Debug, Default, Serialize, Deserialize, Tsify, Clone, PartialEq)] -#[tsify(into_wasm_abi, from_wasm_abi)] -#[allow(non_camel_case_types)] -pub struct CachedMessage { - pub id: u32, - pub status: CachedMessageStatus, - pub transaction: Option, - pub commitment: Option, // content of the op_return - pub sender: Option, // Never None when message sent - pub shared_secrets: Vec, // Max 2 secrets in case we send to both address of the recipient - pub cipher: Vec, // Max 2 ciphers in case we send to both address of the recipient - pub timestamp: u64, -} - -impl CachedMessage { - pub fn new() -> Self { - let mut new = Self::default(); - let mut buf = [0u8; 4]; - thread_rng().fill_bytes(&mut buf); - new.id = u32::from_be_bytes(buf); - new.timestamp = Date::now().floor() as u64; - new - } - - pub fn from_string(json: &str) -> Result { - let res = serde_json::from_str(json)?; - Ok(res) - } - - pub fn to_string(&self) -> String { - serde_json::to_string(self).unwrap() - } - - pub fn try_decrypt_message(&self, cipher: &[u8]) -> Result> { - if self.shared_secrets.is_empty() { - return Err(Error::msg( - "Can't try decrypt this message, there's no shared secret", - )); - } - for shared_secret in &self.shared_secrets { - let mut key = [0u8; 32]; - let mut nonce = [0u8; 12]; - key.copy_from_slice(&Vec::from_hex(shared_secret)?); - nonce.copy_from_slice(&cipher[..12]); - - let engine = Aes256Gcm::new(&key.into()); - let payload = Payload { - msg: &cipher[12..], - aad: AAD, - }; - match engine.decrypt(&nonce.into(), payload) { - Ok(plain) => return Ok(plain), - Err(_) => continue, - } - } - - Err(Error::msg("Failed to decrypt message")) - } - - pub fn try_decrypt_with_shared_secret(&self, shared_secret: [u8; 32]) -> Result> { - if self.cipher.is_empty() || !self.shared_secrets.is_empty() { - return Err(Error::msg( - "Can't try decrypt this message, ciphertext is none or shared_secret already found", - )); - } - for prd_cipher in &self.cipher { - let cipher = Vec::from_hex(prd_cipher)?; - let mut nonce = [0u8; 12]; - nonce.copy_from_slice(&cipher[..12]); - - let engine = Aes256Gcm::new(&shared_secret.into()); - let payload = Payload { - msg: &cipher[12..], - aad: AAD, - }; - match engine.decrypt(&nonce.into(), payload) { - Ok(plain) => return Ok(plain), - Err(_) => continue, - } - } - - Err(Error::msg("Failed to decrypt message")) - } -}