This commit is contained in:
2026-05-18 08:07:59 +07:00
parent d2a17e54c4
commit eb5d64a3af
15 changed files with 524 additions and 333 deletions

View File

@@ -10,7 +10,6 @@ common = { path = "../common" }
nostr.workspace = true
nostr-sdk.workspace = true
nostr-lmdb.workspace = true
nostr-gossip-memory.workspace = true
nostr-connect.workspace = true
nostr-blossom.workspace = true

View File

@@ -0,0 +1,73 @@
use std::collections::{HashMap, HashSet};
use nostr_sdk::prelude::*;
#[derive(Debug, Clone, Default)]
pub struct Gossip {
pub nip17: HashMap<PublicKey, HashSet<RelayUrl>>,
pub nip65: HashMap<PublicKey, HashSet<(RelayUrl, Option<RelayMetadata>)>>,
}
impl Gossip {
/// Parse and insert NIP-65 or NIP-17 relays into the gossip state.
pub fn insert(&mut self, event: &Event) {
match event.kind {
Kind::InboxRelays => {
let urls: Vec<RelayUrl> = nip17::extract_relay_list(event).take(3).collect();
if !urls.is_empty() {
self.nip17.entry(event.pubkey).or_default().extend(urls);
}
}
Kind::RelayList => {
let urls: Vec<(RelayUrl, Option<RelayMetadata>)> =
nip65::extract_relay_list(event).collect();
if !urls.is_empty() {
self.nip65.entry(event.pubkey).or_default().extend(urls);
}
}
_ => {}
}
}
/// Get all write relays for a given public key
pub fn write_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> {
self.nip65
.get(public_key)
.map(|relays| {
relays
.iter()
.filter(|(_, metadata)| metadata.as_ref() == Some(&RelayMetadata::Write))
.map(|(url, _)| url)
.take(3)
.cloned()
.collect()
})
.unwrap_or_default()
}
/// Get all read relays for a given public key
pub fn read_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> {
self.nip65
.get(public_key)
.map(|relays| {
relays
.iter()
.filter(|(_, metadata)| metadata.as_ref() == Some(&RelayMetadata::Read))
.map(|(url, _)| url)
.take(3)
.cloned()
.collect()
})
.unwrap_or_default()
}
/// Get all messaging relays for a given public key
pub fn messaging_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> {
self.nip17
.get(public_key)
.map(|relays| relays.iter().cloned().collect())
.unwrap_or_default()
}
}

View File

@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
@@ -7,19 +7,21 @@ use anyhow::{Error, anyhow};
use common::config_dir;
use gpui::{App, AppContext, Context, Entity, EventEmitter, Global, SharedString, Task, Window};
use nostr_connect::prelude::*;
use nostr_gossip_memory::prelude::*;
use nostr_lmdb::prelude::*;
use nostr_sdk::prelude::*;
use smol::lock::RwLock;
mod blossom;
mod constants;
mod device;
mod gossip;
mod nip05;
mod signer;
pub use blossom::*;
pub use constants::*;
pub use device::*;
pub use gossip::*;
pub use nip05::*;
pub use signer::*;
@@ -53,7 +55,7 @@ pub enum StateEvent {
/// Show the identity dialog
Show,
/// A new signer has been set
SignerSet,
SignerSet(PublicKey),
/// An error occurred
Error(SharedString),
}
@@ -73,6 +75,9 @@ pub struct NostrRegistry {
/// Nostr client
client: Client,
/// Nostr gossip implementation
gossip: Arc<RwLock<Gossip>>,
/// Nostr signer
signer: Arc<CoopSigner>,
@@ -109,7 +114,7 @@ impl NostrRegistry {
let key_dir = config_dir().join("keys");
let app_keys = get_or_init_app_keys(cx).unwrap_or(Keys::generate());
// Construct the nostr signer
let gossip = Arc::new(RwLock::new(Gossip::default()));
let signer = Arc::new(CoopSigner::new(app_keys.clone()));
// Get all local stored npubs
@@ -131,8 +136,8 @@ impl NostrRegistry {
// Construct the nostr client
let client = ClientBuilder::default()
.database(lmdb)
.gossip(NostrGossipMemory::unbounded())
.connect_timeout(Duration::from_secs(10))
.max_avg_latency(Duration::from_millis(800))
.sleep_when_idle(SleepWhenIdle::Enabled {
timeout: Duration::from_secs(600),
})
@@ -141,6 +146,8 @@ impl NostrRegistry {
// Run at the end of current cycle
cx.defer_in(window, |this, _window, cx| {
this.connect(cx);
this.handle_notifications(cx);
// Create an identity if none exists
if this.npubs.read(cx).is_empty() {
this.create_identity(cx);
@@ -152,6 +159,7 @@ impl NostrRegistry {
Self {
client,
gossip,
signer,
npubs,
key_dir,
@@ -165,6 +173,11 @@ impl NostrRegistry {
self.client.clone()
}
/// Get the gossip instance
pub fn gossip(&self) -> Arc<RwLock<Gossip>> {
self.gossip.clone()
}
/// Get the nostr signer
pub fn signer(&self) -> Arc<CoopSigner> {
self.signer.clone()
@@ -207,11 +220,10 @@ impl NostrRegistry {
Ok(npubs)
}
/// Connect to the bootstrapping relays
fn connect(&mut self, cx: &mut Context<Self>) {
fn background_connect(&self, cx: &App) -> Task<Result<(), Error>> {
let client = self.client();
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
cx.background_spawn(async move {
// Add search relay to the relay pool
for url in SEARCH_RELAYS.into_iter() {
client
@@ -237,20 +249,60 @@ impl NostrRegistry {
client.connect().await;
Ok(())
});
})
}
/// Connect to the bootstrapping relays
fn connect(&mut self, cx: &mut Context<Self>) {
let task = self.background_connect(cx);
// Emit connecting event
cx.emit(StateEvent::Connecting);
self.tasks.push(cx.spawn(async move |this, cx| {
// Spawn a task to handle the connection result
cx.spawn(async move |this, cx| {
if let Err(e) = task.await {
this.update(cx, |_this, cx| {
cx.emit(StateEvent::error(e.to_string()));
})?;
})
.ok();
} else {
this.update(cx, |_this, cx| {
cx.emit(StateEvent::Connected);
})?;
})
.ok();
}
})
.detach();
}
fn handle_notifications(&mut self, cx: &App) {
let client = self.client();
let gossip = self.gossip.clone();
self.tasks.push(cx.background_spawn(async move {
let mut notifications = client.notifications();
let mut processed_events = HashSet::new();
while let Some(notification) = notifications.next().await {
if let ClientNotification::Message { message, .. } = notification
&& let RelayMessage::Event { event, .. } = *message
{
if !processed_events.insert(event.id) {
// Skip if the event has already been processed
continue;
}
match event.kind {
Kind::RelayList => {
gossip.write().await.insert(&event);
}
Kind::InboxRelays => {
gossip.write().await.insert(&event);
}
_ => {}
}
}
}
Ok(())
@@ -264,6 +316,7 @@ impl NostrRegistry {
let npub = public_key.to_bech32().unwrap();
let key_path = self.key_dir.join(format!("{}.npub", npub));
let app_keys = self.app_keys.clone();
let app_signer: Arc<dyn NostrSigner> = Arc::new(app_keys.clone());
@@ -274,9 +327,18 @@ impl NostrRegistry {
if let Ok(secret) = SecretKey::parse(&decrypted) {
let keys = Keys::new(secret);
cx.spawn(async move |_cx| {
let public_key = keys.public_key();
cx.spawn(async move |cx| {
signer.switch(keys).await;
client.unsubscribe_all().await.ok();
get_gossip_relays(&client, &public_key).await.ok();
cx.update_global::<GlobalNostrRegistry, _>(|this, cx| {
this.0.update(cx, |_, cx| {
cx.emit(StateEvent::SignerSet(public_key));
});
});
})
.detach();
@@ -286,9 +348,29 @@ impl NostrRegistry {
let mut nip46 = NostrConnect::new(uri, app_keys, timeout, None)?;
nip46.auth_url_handler(CoopAuthUrlHandler);
cx.spawn(async move |_cx| {
cx.spawn(async move |cx| {
signer.switch(nip46).await;
client.unsubscribe_all().await.ok();
// Verify the public key
match signer.get_public_key().await {
Ok(public_key) => {
get_gossip_relays(&client, &public_key).await.ok();
cx.update_global::<GlobalNostrRegistry, _>(|this, cx| {
this.0.update(cx, |_, cx| {
cx.emit(StateEvent::SignerSet(public_key));
});
});
}
Err(e) => {
cx.update_global::<GlobalNostrRegistry, _>(|this, cx| {
this.0.update(cx, |_, cx| {
cx.emit(StateEvent::error(e.to_string()));
});
});
}
}
})
.detach();
@@ -299,33 +381,6 @@ impl NostrRegistry {
Err(anyhow!("Secret not found"))
}
/// Get the secret for a given npub.
pub fn get_secret(&self, public_key: PublicKey) -> Result<Arc<dyn AsyncNostrSigner>, Error> {
let npub = public_key.to_bech32().unwrap();
let key_path = self.key_dir.join(format!("{}.npub", npub));
let app_keys = self.app_keys.clone();
let app_signer: Arc<dyn NostrSigner> = Arc::new(self.app_keys.clone());
if let Ok(payload) = std::fs::read_to_string(key_path)
&& !payload.is_empty()
{
let decrypted = app_signer.nip44_decrypt(&public_key, &payload)?;
if let Ok(secret) = SecretKey::parse(&decrypted) {
let keys = Keys::new(secret);
return Ok(Arc::new(keys));
} else if let Ok(uri) = NostrConnectUri::parse(decrypted) {
let timeout = Duration::from_secs(NOSTR_CONNECT_TIMEOUT);
let mut nip46 = NostrConnect::new(uri, app_keys, timeout, None)?;
nip46.auth_url_handler(CoopAuthUrlHandler);
return Ok(Arc::new(nip46));
}
}
Err(anyhow!("Secret not found"))
}
/// Add a new npub to the keys directory
fn write_secret(
&self,
@@ -335,14 +390,14 @@ impl NostrRegistry {
) -> Task<Result<(), Error>> {
let npub = public_key.to_bech32().unwrap();
let key_path = self.key_dir.join(format!("{}.npub", npub));
let app_keys: Arc<dyn AsyncNostrSigner> = Arc::new(self.app_keys.clone());
let app_signer: Arc<dyn AsyncNostrSigner> = Arc::new(self.app_keys.clone());
cx.background_spawn(async move {
// If the secret starts with "bunker://" (nostr connect), use it directly; otherwise, encrypt it
let content = if secret.starts_with("bunker://") {
secret
} else {
app_keys.nip44_encrypt(&public_key, &secret).await?
app_signer.nip44_encrypt(&public_key, &secret).await?
};
// Write the encrypted secret to the keys directory
@@ -386,14 +441,35 @@ impl NostrRegistry {
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
// Construct relay list event
let relay_list = default_relay_list();
let event = EventBuilder::relay_list(relay_list)
let event = EventBuilder::relay_list(relay_list.clone())
.sign_async(&async_keys)
.await?;
// Get write relays
let write_relays: Vec<RelayUrl> = relay_list
.iter()
.filter_map(|(url, metadata)| {
if metadata.is_none() || metadata == &Some(RelayMetadata::Write) {
Some(url.to_owned())
} else {
None
}
})
.collect();
// Ensure the client is connected to each relay
for url in write_relays.iter() {
client
.add_relay(url)
.capabilities(RelayCapabilities::WRITE)
.and_connect()
.await?;
}
// Publish relay list
client
.send_event(&event)
.to(BOOTSTRAP_RELAYS)
.to(&write_relays)
.ack_policy(AckPolicy::none())
.await?;
@@ -408,7 +484,7 @@ impl NostrRegistry {
// Publish metadata event
client
.send_event(&event)
.to_nip65()
.to(&write_relays)
.ack_policy(AckPolicy::none())
.await?;
@@ -421,7 +497,7 @@ impl NostrRegistry {
// Publish contact list event
client
.send_event(&event)
.to_nip65()
.to(&write_relays)
.ack_policy(AckPolicy::none())
.await?;
@@ -432,7 +508,11 @@ impl NostrRegistry {
.await?;
// Publish messaging relay list event
client.send_event(&event).to_nip65().await?;
client
.send_event(&event)
.to(&write_relays)
.ack_policy(AckPolicy::none())
.await?;
// Write user's credentials to the system keyring
write_secret.await?;
@@ -444,7 +524,9 @@ impl NostrRegistry {
match task.await {
Ok(_) => {
this.update(cx, |this, cx| {
this.switch_account(keys.public_key(), cx);
if let Err(e) = this.switch_account(keys.public_key(), cx) {
cx.emit(StateEvent::error(e.to_string()));
}
})?;
}
Err(e) => {
@@ -468,7 +550,9 @@ impl NostrRegistry {
match write_secret.await {
Ok(_) => {
this.update(cx, |this, cx| {
this.switch_account(keys.public_key(), cx);
if let Err(e) = this.switch_account(keys.public_key(), cx) {
cx.emit(StateEvent::error(e.to_string()));
}
})?;
}
Err(e) => {
@@ -507,7 +591,9 @@ impl NostrRegistry {
match write_secret.await {
Ok(_) => {
this.update(cx, |this, cx| {
this.switch_account(public_key, cx);
if let Err(e) = this.switch_account(public_key, cx) {
cx.emit(StateEvent::error(e.to_string()));
}
})?;
}
Err(e) => {
@@ -621,69 +707,64 @@ impl NostrRegistry {
})
}
/// Perform a WoT (via Vertex) search for a given query.
pub fn wot_search(&self, query: &str, cx: &App) -> Task<Result<Vec<PublicKey>, Error>> {
pub fn write_relays(&self, public_key: &PublicKey, cx: &App) -> Task<Vec<RelayUrl>> {
let client = self.client();
let query = query.to_string();
let signer = self.signer();
let gossip = self.gossip.clone();
let public_key = public_key.to_owned();
cx.background_spawn(async move {
let signer = signer.get().await;
let urls = gossip.read().await.write_relays(&public_key);
// Construct a vertex request event
let event = EventBuilder::new(Kind::Custom(5315), "")
.tags(vec![
Tag::custom("param", vec!["search", &query]),
Tag::custom("param", vec!["limit", "10"]),
])
.sign_async(&signer)
.await?;
// Send the event to vertex relays
let output = client.send_event(&event).to(WOT_RELAYS).await?;
// Construct a filter to get the response or error from vertex
let filter = Filter::new()
.kinds(vec![Kind::Custom(6315), Kind::Custom(7000)])
.event(output.id().to_owned());
// Construct target for subscription
let target: HashMap<&str, Vec<Filter>> = WOT_RELAYS
.into_iter()
.map(|relay| (relay, vec![filter.clone()]))
.collect();
// Stream events from the wot relays
let mut stream = client
.stream_events(target)
.timeout(Duration::from_secs(TIMEOUT))
.await?;
while let Some((_url, res)) = stream.next().await {
if let Ok(event) = res {
match event.kind {
Kind::Custom(6315) => {
let content: serde_json::Value = serde_json::from_str(&event.content)?;
let pubkeys: Vec<PublicKey> = content
.as_array()
.into_iter()
.flatten()
.filter_map(|item| item.as_object())
.filter_map(|obj| obj.get("pubkey").and_then(|v| v.as_str()))
.filter_map(|pubkey_str| PublicKey::parse(pubkey_str).ok())
.collect();
return Ok(pubkeys);
}
Kind::Custom(7000) => {
return Err(anyhow!("Search error"));
}
_ => {}
}
}
// Ensure the client is connected to each relay
for url in urls.iter() {
client
.add_relay(url)
.capabilities(RelayCapabilities::WRITE)
.and_connect()
.await
.ok();
}
Err(anyhow!("No results for query: {query}"))
urls
})
}
pub fn read_relays(&self, public_key: &PublicKey, cx: &App) -> Task<Vec<RelayUrl>> {
let client = self.client();
let gossip = self.gossip.clone();
let public_key = public_key.to_owned();
cx.background_spawn(async move {
let urls = gossip.read().await.read_relays(&public_key);
// Ensure the client is connected to each relay
for url in urls.iter() {
client
.add_relay(url)
.capabilities(RelayCapabilities::READ)
.and_connect()
.await
.ok();
}
urls
})
}
pub fn msg_relays(&self, public_key: &PublicKey, cx: &App) -> Task<Vec<RelayUrl>> {
let client = self.client();
let gossip = self.gossip.clone();
let public_key = public_key.to_owned();
cx.background_spawn(async move {
let urls = gossip.read().await.messaging_relays(&public_key);
// Ensure the client is connected to each relay
for url in urls.iter() {
client.add_relay(url).and_connect().await.ok();
}
urls
})
}
}
@@ -717,6 +798,20 @@ fn get_or_init_app_keys(cx: &App) -> Result<Keys, Error> {
}
}
async fn get_gossip_relays(client: &Client, public_key: &PublicKey) -> Result<(), Error> {
let id = SubscriptionId::new("gossip");
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
let filter = Filter::new()
.author(*public_key)
.kind(Kind::RelayList)
.limit(1);
client.subscribe(filter).close_on(opts).with_id(id).await?;
Ok(())
}
fn default_relay_list() -> Vec<(RelayUrl, Option<RelayMetadata>)> {
vec![
(

View File

@@ -10,7 +10,7 @@ pub struct CoopSigner {
signer: Arc<RwLock<Arc<dyn AsyncNostrSigner>>>,
/// User's signer public key
signer_pkey: Arc<RwLock<Option<PublicKey>>>,
public_key: RwLock<Option<PublicKey>>,
/// Specific signer for encryption purposes
encryption_signer: Arc<RwLock<Option<Arc<dyn AsyncNostrSigner>>>>,
@@ -23,7 +23,7 @@ impl CoopSigner {
{
Self {
signer: Arc::new(RwLock::new(Arc::new(signer))),
signer_pkey: Arc::new(RwLock::new(None)),
public_key: RwLock::new(None),
encryption_signer: Arc::new(RwLock::new(None)),
}
}
@@ -47,8 +47,13 @@ impl CoopSigner {
///
/// Ensure to call this method after the signer has been initialized.
/// Otherwise, it will panic.
pub fn public_key(&self) -> Option<PublicKey> {
*self.signer_pkey.read_blocking()
pub fn public_key(&self) -> PublicKey {
self.public_key.read_blocking().unwrap()
}
/// Check if the public key is present, indicating the signer is logged in.
pub fn is_logged_in(&self) -> bool {
self.public_key.read_blocking().is_some()
}
/// Switch the current signer to a new signer.
@@ -57,11 +62,11 @@ impl CoopSigner {
T: AsyncNostrSigner,
{
let mut signer = self.signer.write().await;
let mut signer_pkey = self.signer_pkey.write().await;
let mut public_key = self.public_key.write().await;
let mut encryption_signer = self.encryption_signer.write().await;
// Update the public key
*signer_pkey = new.get_public_key().await.ok();
// Verify the public key
*public_key = new.get_public_key().await.ok();
// Switch to the new signer
*signer = Arc::new(new);