refactor signer

This commit is contained in:
2026-07-23 11:14:56 +07:00
parent ab665cd661
commit bf6ae65b05
22 changed files with 291 additions and 567 deletions

View File

@@ -19,7 +19,7 @@ use nostr_sdk::prelude::*;
use smallvec::{SmallVec, smallvec};
#[cfg(not(target_arch = "wasm32"))]
use smol::lock::RwLock;
use state::{DEVICE_GIFTWRAP, NostrRegistry, USER_GIFTWRAP};
use state::{DEVICE_GIFTWRAP, NostrRegistry, USER_GIFTWRAP, UniversalSigner};
mod message;
mod room;
@@ -140,17 +140,15 @@ impl ChatRegistry {
/// Create a new chat registry instance
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let nostr = NostrRegistry::global(cx);
let user_signer = nostr.read(cx).signer.clone();
let (tx, rx) = flume::unbounded::<Signal>();
let mut subscriptions = smallvec![];
subscriptions.push(
// Subscribe to the signer event
cx.observe(&user_signer, |this, signer, cx| {
if let Some(keys) = signer.read(cx).clone() {
cx.subscribe(&nostr, |this, _nostr, event, cx| {
if event.signer_changed() {
this.reset(cx);
this.handle_notifications(keys, cx);
this.handle_notifications(cx);
this.get_metadata(cx);
this.get_rooms(cx);
};
@@ -178,9 +176,10 @@ impl ChatRegistry {
}
/// Handle nostr notifications
fn handle_notifications(&mut self, signer: Keys, cx: &mut Context<Self>) {
fn handle_notifications(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let tracking = self.tracking.clone();
let msg_relays_existed = self.msg_relays_existed.clone();
@@ -340,7 +339,7 @@ impl ChatRegistry {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let Some(public_key) = nostr.read(cx).signer_pubkey(cx) else {
let Some(public_key) = nostr.read(cx).current_user() else {
return;
};
@@ -392,11 +391,9 @@ impl ChatRegistry {
fn get_messages(&mut self, msg_relays: &Event, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let urls: Vec<RelayUrl> = nip17::extract_relay_list(msg_relays).collect();
let signer = nostr.read(cx).signer();
let Some(signer) = nostr.read(cx).signer(cx) else {
return;
};
let urls: Vec<RelayUrl> = nip17::extract_relay_list(msg_relays).collect();
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let public_key = signer.get_public_key_async().await?;
@@ -514,7 +511,7 @@ impl ChatRegistry {
{
let nostr = NostrRegistry::global(cx);
let Some(public_key) = nostr.read(cx).signer_pubkey(cx) else {
let Some(public_key) = nostr.read(cx).current_user() else {
return;
};
@@ -625,7 +622,7 @@ impl ChatRegistry {
pub fn get_rooms(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let Some(public_key) = nostr.read(cx).signer_pubkey(cx) else {
let Some(public_key) = nostr.read(cx).current_user() else {
return;
};
@@ -735,7 +732,7 @@ impl ChatRegistry {
pub fn new_message(&mut self, message: NewMessage, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let Some(public_key) = nostr.read(cx).signer_pubkey(cx) else {
let Some(public_key) = nostr.read(cx).current_user() else {
return;
};
@@ -771,7 +768,7 @@ impl ChatRegistry {
/// Unwraps a gift-wrapped event and processes its contents.
async fn extract_rumor(
client: &Client,
signer: &Keys,
signer: &UniversalSigner,
gift_wrap: &Event,
) -> Result<UnsignedEvent, Error> {
// Try to get cached rumor first
@@ -795,7 +792,7 @@ async fn extract_rumor(
}
/// Helper method to try unwrapping with different signers
async fn try_unwrap(signer: &Keys, gift_wrap: &Event) -> Result<UnwrappedGift, Error> {
async fn try_unwrap(signer: &UniversalSigner, gift_wrap: &Event) -> Result<UnwrappedGift, Error> {
/*
* // Try with the device signer first
if let Some(signer) = signer.get_encryption_signer().await {
@@ -808,13 +805,16 @@ async fn try_unwrap(signer: &Keys, gift_wrap: &Event) -> Result<UnwrappedGift, E
// Fallback to the user's signer
let user_signer = signer.get().await;
*/
let unwrapped = try_unwrap_with(gift_wrap, signer).await?;
let unwrapped = try_unwrap_with(signer, gift_wrap).await?;
Ok(unwrapped)
}
/// Attempts to unwrap a gift wrap event with a given signer.
async fn try_unwrap_with(gift_wrap: &Event, signer: &Keys) -> Result<UnwrappedGift, Error> {
async fn try_unwrap_with(
signer: &UniversalSigner,
gift_wrap: &Event,
) -> Result<UnwrappedGift, Error> {
// Get the sealed event
let seal = signer
.nip44_decrypt_async(&gift_wrap.pubkey, &gift_wrap.content)

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};
use state::{NostrRegistry, TIMEOUT, UniversalSigner};
use crate::NewMessage;
@@ -427,7 +427,7 @@ impl Room {
let nostr = NostrRegistry::global(cx);
// Get current user's public key
let sender = nostr.read(cx).signer_pubkey(cx)?;
let sender = nostr.read(cx).current_user()?;
// Get all members, excluding the sender
let members: Vec<Person> = self
@@ -482,13 +482,12 @@ impl Room {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let user_signer = nostr.read(cx).signer();
let current_user = nostr.read(cx).current_user()?;
// Get current user's public key
let user_signer = nostr.read(cx).signer(cx)?;
let public_key = nostr.read(cx).signer_pubkey(cx)?;
// Get sender's profile
let persons = PersonRegistry::global(cx);
let sender = persons.read(cx).get(&public_key, cx);
let sender = persons.read(cx).get(&current_user, cx);
// Get all members (excluding sender)
let members: Vec<Person> = self
@@ -597,7 +596,7 @@ impl Room {
// Helper function to send a gift-wrapped event
async fn send_gift_wrap(
client: &Client,
signer: &Keys,
signer: &UniversalSigner,
receiver: &Person,
rumor: &UnsignedEvent,
config: &SignerKind,