From f45abb58c792f754c585538a7f66d186dba0b752 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Wed, 18 Mar 2026 10:16:04 +0700 Subject: [PATCH] fix decrypt message --- crates/chat/src/lib.rs | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/crates/chat/src/lib.rs b/crates/chat/src/lib.rs index 361dd96..bbf8a96 100644 --- a/crates/chat/src/lib.rs +++ b/crates/chat/src/lib.rs @@ -16,7 +16,7 @@ use gpui::{ use nostr_sdk::prelude::*; use smallvec::{SmallVec, smallvec}; use smol::lock::RwLock; -use state::{DEVICE_GIFTWRAP, NostrRegistry, StateEvent, TIMEOUT, USER_GIFTWRAP}; +use state::{CoopSigner, DEVICE_GIFTWRAP, NostrRegistry, StateEvent, TIMEOUT, USER_GIFTWRAP}; mod message; mod room; @@ -154,7 +154,6 @@ impl ChatRegistry { let rx = self.signal_rx.clone(); self.tasks.push(cx.background_spawn(async move { - let device_signer = signer.get_encryption_signer().await; let mut notifications = client.notifications(); let mut processed_events = HashSet::new(); @@ -183,7 +182,7 @@ impl ChatRegistry { } // Extract the rumor from the gift wrap event - match extract_rumor(&client, &device_signer, event.as_ref()).await { + match extract_rumor(&client, &signer, event.as_ref()).await { Ok(rumor) => { if rumor.tags.is_empty() { let error: SharedString = "No room for message".into(); @@ -682,7 +681,7 @@ impl ChatRegistry { /// Unwraps a gift-wrapped event and processes its contents. async fn extract_rumor( client: &Client, - device_signer: &Option>, + signer: &Arc, gift_wrap: &Event, ) -> Result { // Try to get cached rumor first @@ -691,7 +690,7 @@ async fn extract_rumor( } // Try to unwrap with the available signer - let unwrapped = try_unwrap(client, device_signer, gift_wrap).await?; + let unwrapped = try_unwrap(signer, gift_wrap).await?; let mut rumor = unwrapped.rumor; // Generate event id for the rumor if it doesn't have one @@ -706,30 +705,25 @@ async fn extract_rumor( } /// Helper method to try unwrapping with different signers -async fn try_unwrap( - client: &Client, - device_signer: &Option>, - gift_wrap: &Event, -) -> Result { +async fn try_unwrap(signer: &Arc, gift_wrap: &Event) -> Result { // Try with the device signer first - if let Some(signer) = device_signer - && let Ok(unwrapped) = try_unwrap_with(gift_wrap, signer).await - { - return Ok(unwrapped); - }; + if let Some(signer) = signer.get_encryption_signer().await { + log::info!("trying with device signer"); + return try_unwrap_with(gift_wrap, &signer).await; + } - // Try with the user's signer - let user_signer = client.signer().context("Signer not found")?; - let unwrapped = try_unwrap_with(gift_wrap, user_signer).await?; + // Fallback to the user's signer + let user_signer = signer.get().await; + let unwrapped = try_unwrap_with(gift_wrap, &user_signer).await?; Ok(unwrapped) } /// Attempts to unwrap a gift wrap event with a given signer. -async fn try_unwrap_with( - gift_wrap: &Event, - signer: &Arc, -) -> Result { +async fn try_unwrap_with(gift_wrap: &Event, signer: &T) -> Result +where + T: NostrSigner + 'static, +{ // Get the sealed event let seal = signer .nip44_decrypt(&gift_wrap.pubkey, &gift_wrap.content)