fix decrypt message
This commit is contained in:
@@ -16,7 +16,7 @@ use gpui::{
|
|||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use smallvec::{SmallVec, smallvec};
|
use smallvec::{SmallVec, smallvec};
|
||||||
use smol::lock::RwLock;
|
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 message;
|
||||||
mod room;
|
mod room;
|
||||||
@@ -154,7 +154,6 @@ impl ChatRegistry {
|
|||||||
let rx = self.signal_rx.clone();
|
let rx = self.signal_rx.clone();
|
||||||
|
|
||||||
self.tasks.push(cx.background_spawn(async move {
|
self.tasks.push(cx.background_spawn(async move {
|
||||||
let device_signer = signer.get_encryption_signer().await;
|
|
||||||
let mut notifications = client.notifications();
|
let mut notifications = client.notifications();
|
||||||
let mut processed_events = HashSet::new();
|
let mut processed_events = HashSet::new();
|
||||||
|
|
||||||
@@ -183,7 +182,7 @@ impl ChatRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Extract the rumor from the gift wrap event
|
// 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) => {
|
Ok(rumor) => {
|
||||||
if rumor.tags.is_empty() {
|
if rumor.tags.is_empty() {
|
||||||
let error: SharedString = "No room for message".into();
|
let error: SharedString = "No room for message".into();
|
||||||
@@ -682,7 +681,7 @@ impl ChatRegistry {
|
|||||||
/// Unwraps a gift-wrapped event and processes its contents.
|
/// Unwraps a gift-wrapped event and processes its contents.
|
||||||
async fn extract_rumor(
|
async fn extract_rumor(
|
||||||
client: &Client,
|
client: &Client,
|
||||||
device_signer: &Option<Arc<dyn NostrSigner>>,
|
signer: &Arc<CoopSigner>,
|
||||||
gift_wrap: &Event,
|
gift_wrap: &Event,
|
||||||
) -> Result<UnsignedEvent, Error> {
|
) -> Result<UnsignedEvent, Error> {
|
||||||
// Try to get cached rumor first
|
// Try to get cached rumor first
|
||||||
@@ -691,7 +690,7 @@ async fn extract_rumor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try to unwrap with the available signer
|
// 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;
|
let mut rumor = unwrapped.rumor;
|
||||||
|
|
||||||
// Generate event id for the rumor if it doesn't have one
|
// 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
|
/// Helper method to try unwrapping with different signers
|
||||||
async fn try_unwrap(
|
async fn try_unwrap(signer: &Arc<CoopSigner>, gift_wrap: &Event) -> Result<UnwrappedGift, Error> {
|
||||||
client: &Client,
|
|
||||||
device_signer: &Option<Arc<dyn NostrSigner>>,
|
|
||||||
gift_wrap: &Event,
|
|
||||||
) -> Result<UnwrappedGift, Error> {
|
|
||||||
// Try with the device signer first
|
// Try with the device signer first
|
||||||
if let Some(signer) = device_signer
|
if let Some(signer) = signer.get_encryption_signer().await {
|
||||||
&& let Ok(unwrapped) = try_unwrap_with(gift_wrap, signer).await
|
log::info!("trying with device signer");
|
||||||
{
|
return try_unwrap_with(gift_wrap, &signer).await;
|
||||||
return Ok(unwrapped);
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// Try with the user's signer
|
// Fallback to the user's signer
|
||||||
let user_signer = client.signer().context("Signer not found")?;
|
let user_signer = signer.get().await;
|
||||||
let unwrapped = try_unwrap_with(gift_wrap, user_signer).await?;
|
let unwrapped = try_unwrap_with(gift_wrap, &user_signer).await?;
|
||||||
|
|
||||||
Ok(unwrapped)
|
Ok(unwrapped)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempts to unwrap a gift wrap event with a given signer.
|
/// Attempts to unwrap a gift wrap event with a given signer.
|
||||||
async fn try_unwrap_with(
|
async fn try_unwrap_with<T>(gift_wrap: &Event, signer: &T) -> Result<UnwrappedGift, Error>
|
||||||
gift_wrap: &Event,
|
where
|
||||||
signer: &Arc<dyn NostrSigner>,
|
T: NostrSigner + 'static,
|
||||||
) -> Result<UnwrappedGift, Error> {
|
{
|
||||||
// Get the sealed event
|
// Get the sealed event
|
||||||
let seal = signer
|
let seal = signer
|
||||||
.nip44_decrypt(&gift_wrap.pubkey, &gift_wrap.content)
|
.nip44_decrypt(&gift_wrap.pubkey, &gift_wrap.content)
|
||||||
|
|||||||
Reference in New Issue
Block a user