From 3f70dfc79f020586c957c087f31b3fb680350c37 Mon Sep 17 00:00:00 2001 From: Sosthene Date: Fri, 19 Jul 2024 22:48:57 +0200 Subject: [PATCH] Add TrustedChannel --- src/network.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/network.rs b/src/network.rs index 3ae93af..09a4e18 100644 --- a/src/network.rs +++ b/src/network.rs @@ -237,3 +237,52 @@ impl CachedMessage { aes_decrypt.decrypt_with_key() } } + +#[derive(Debug, Serialize, Deserialize, Tsify, Default)] +#[tsify(into_wasm_abi, from_wasm_abi)] +#[allow(non_camel_case_types)] +pub struct TrustedChannel { + id: u32, // Just take the id of the cached message? + revokation_outpoint: OutPoint, // revokation output is locked by the shared_secret + shared_secret: [u8; 32], + revoked_in_block: [u8; 32], + with: String, // Silent payment address +} + +impl TrustedChannel { + pub fn new(with: String) -> Result { + // check that with is valid silent payment address + SilentPaymentAddress::try_from(with.as_str())?; + + // Generating random id + let mut new = Self::default(); + let mut buf = [0u8; 4]; + thread_rng().fill_bytes(&mut buf); + new.id = u32::from_be_bytes(buf); + new.with = with; + + Ok(new) + } + + pub fn set_revokation( + &mut self, + revokation_outpoint: String, + shared_secret: String, + ) -> Result<()> { + let mut buf = [0u8; 32]; + buf.copy_from_slice(&Vec::from_hex(&shared_secret)?); + self.revokation_outpoint = OutPoint::from_str(&revokation_outpoint)?; + self.shared_secret.copy_from_slice(&buf); + + Ok(()) + } + + pub fn revoke(&mut self, revoked_in_block: String) -> Result<()> { + let block_hash = BlockHash::from_str(&revoked_in_block)?; + let mut buf = [0u8; 32]; + buf.copy_from_slice(&serialize::(&block_hash)); + self.revoked_in_block = buf; + + Ok(()) + } +}