use signer trait

This commit is contained in:
2026-09-30 04:51:23 +07:00
parent 7dbf55d48e
commit 44307d39f4
5 changed files with 67 additions and 47 deletions
+11 -8
View File
@@ -14,7 +14,7 @@ use gpui::{
use instant::Duration;
use nostr_sdk::prelude::*;
use smallvec::{SmallVec, smallvec};
use state::{DEVICE_GIFTWRAP, NostrRegistry, USER_GIFTWRAP, UniversalSigner};
use state::{DEVICE_GIFTWRAP, NostrRegistry, USER_GIFTWRAP};
mod message;
mod room;
@@ -740,11 +740,14 @@ impl ChatRegistry {
}
/// Unwraps a gift-wrapped event and processes its contents.
async fn extract_rumor(
async fn extract_rumor<S>(
client: &Client,
signer: &UniversalSigner,
signer: &S,
gift_wrap: &Event,
) -> Result<UnsignedEvent, Error> {
) -> Result<UnsignedEvent, Error>
where
S: AsyncNip44 + ?Sized,
{
// Try to get cached rumor first
if let Ok(rumor) = get_rumor(client, gift_wrap.id).await {
return Ok(rumor);
@@ -771,10 +774,10 @@ async fn extract_rumor(
}
/// Attempts to unwrap a gift wrap event with a given signer.
async fn try_unwrap_with(
signer: &UniversalSigner,
gift_wrap: &Event,
) -> Result<UnwrappedGift, Error> {
async fn try_unwrap_with<S>(signer: &S, gift_wrap: &Event) -> Result<UnwrappedGift, Error>
where
S: AsyncNip44 + ?Sized,
{
// Get the sealed event
let seal = signer
.nip44_decrypt_async(&gift_wrap.pubkey, &gift_wrap.content)
+24 -21
View File
@@ -10,7 +10,7 @@ use itertools::Itertools;
use nostr_sdk::prelude::*;
use person::{Person, PersonRegistry};
use settings::{RoomConfig, SignerKind};
use state::{NostrRegistry, TIMEOUT, UniversalSigner};
use state::{NostrRegistry, TIMEOUT};
use crate::{FileAttachment, KIND_FILE_MESSAGE, NewMessage};
@@ -473,31 +473,28 @@ impl Room {
}
/// Select the appropriate signer based on signer kind and available keys.
fn select_signer(
fn select_signer<'a, S>(
signer_kind: &SignerKind,
has_announcement: bool,
encryption_signer: &Option<UniversalSigner>,
user_signer: &UniversalSigner,
) -> UniversalSigner {
encryption_signer: &'a Option<S>,
user_signer: &'a S,
) -> &'a S {
match signer_kind {
SignerKind::Auto => {
if has_announcement {
encryption_signer
.clone()
.unwrap_or_else(|| user_signer.clone())
encryption_signer.as_ref().unwrap_or(user_signer)
} else {
user_signer.clone()
user_signer
}
}
SignerKind::Encryption => encryption_signer
.clone()
.as_ref()
.expect("encryption signer must be set"),
SignerKind::User => user_signer.clone(),
SignerKind::User => user_signer,
}
}
/// Send the rumor to every member's messaging relays
/// and to the sender as a backup, when enabled.
/// Send the rumor to every member's messaging relays.
pub fn send(&self, rumor: UnsignedEvent, cx: &App) -> Option<Task<Vec<SendReport>>> {
let config = self.config.clone();
@@ -558,14 +555,17 @@ impl Room {
}
/// Deliver a rumor to a single receiver.
async fn deliver(
async fn deliver<S>(
client: &Client,
signer_kind: &SignerKind,
receiver: Person,
rumor: &UnsignedEvent,
encryption_signer: &Option<UniversalSigner>,
user_signer: &UniversalSigner,
) -> SendReport {
encryption_signer: &Option<S>,
user_signer: &S,
) -> SendReport
where
S: AsyncGetPublicKey + AsyncSignEvent + AsyncNip44,
{
let public_key = receiver.public_key();
let announcement = receiver.announcement();
@@ -595,7 +595,7 @@ async fn deliver(
user_signer,
);
match send_gift_wrap(client, &signer, &receiver, rumor, signer_kind).await {
match send_gift_wrap(client, signer, &receiver, rumor, signer_kind).await {
Ok(output) => SendReport {
receiver: public_key,
result: Ok(output),
@@ -608,13 +608,16 @@ async fn deliver(
}
/// Build the gift-wrapped event for a rumor and send it.
async fn send_gift_wrap(
async fn send_gift_wrap<S>(
client: &Client,
signer: &UniversalSigner,
signer: &S,
receiver: &Person,
rumor: &UnsignedEvent,
config: &SignerKind,
) -> Result<Output<EventId, EventSendStatus>, Error> {
) -> Result<Output<EventId, EventSendStatus>, Error>
where
S: AsyncGetPublicKey + AsyncSignEvent + AsyncNip44,
{
let mut extra_tags = vec![Tag::custom("k", [rumor.kind.to_string()])];
// Determine the receiver public key based on the config