WIP: feat: add self implement nostr sdk (nostrkit) #31

Closed
reya wants to merge 2 commits from feat/nostrkit into master
15 changed files with 524 additions and 333 deletions
Showing only changes of commit eb5d64a3af - Show all commits

13
Cargo.lock generated
View File

@@ -4483,18 +4483,6 @@ dependencies = [
"nostr", "nostr",
] ]
[[package]]
name = "nostr-gossip-memory"
version = "0.44.0"
source = "git+https://github.com/rust-nostr/nostr#919b2cdd1a1909b2082911a5fff23cfbff22b8fd"
dependencies = [
"indexmap",
"lru",
"nostr",
"nostr-gossip",
"tokio",
]
[[package]] [[package]]
name = "nostr-lmdb" name = "nostr-lmdb"
version = "0.44.0" version = "0.44.0"
@@ -6691,7 +6679,6 @@ dependencies = [
"nostr", "nostr",
"nostr-blossom", "nostr-blossom",
"nostr-connect", "nostr-connect",
"nostr-gossip-memory",
"nostr-lmdb", "nostr-lmdb",
"nostr-sdk", "nostr-sdk",
"petname", "petname",

View File

@@ -134,41 +134,21 @@ impl ChatRegistry {
subscriptions.push( subscriptions.push(
// Subscribe to the signer event // Subscribe to the signer event
cx.subscribe_in(&nostr, window, |this, state, event, window, cx| { cx.subscribe(&nostr, |this, _state, event, cx| {
if event == &StateEvent::SignerSet { if let StateEvent::SignerSet(public_key) = event {
this.reset(cx); this.reset(cx);
this.get_contact_list(cx); this.get_contact_list(public_key, cx);
this.get_rooms(cx); this.get_rooms(cx);
this.get_messages(public_key, cx);
let signer = state.read(cx).signer();
cx.spawn_in(window, async move |this, cx| {
let user_signer = signer.get().await;
this.update(cx, |this, cx| {
this.get_messages(user_signer, cx);
})
.ok();
})
.detach();
}; };
}), }),
); );
subscriptions.push( subscriptions.push(
// Subscribe to the device event // Subscribe to the device event
cx.subscribe_in(&device, window, |_this, _s, event, window, cx| { cx.subscribe(&device, |this, _device, event, cx| {
if event == &DeviceEvent::Set { if let DeviceEvent::Set(public_key) = event {
let nostr = NostrRegistry::global(cx); this.get_messages(public_key, cx);
let signer = nostr.read(cx).signer();
cx.spawn_in(window, async move |this, cx| {
if let Some(device_signer) = signer.get_encryption_signer().await {
this.update(cx, |this, cx| {
this.get_messages(device_signer, cx);
})
.ok();
}
})
.detach();
}; };
}), }),
); );
@@ -342,17 +322,17 @@ impl ChatRegistry {
} }
/// Get contact list from relays /// Get contact list from relays
fn get_contact_list(&mut self, cx: &mut Context<Self>) { fn get_contact_list(&mut self, public_key: &PublicKey, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let Some(public_key) = signer.public_key() else { let public_key = public_key.to_owned();
return; let write_relays = nostr.read(cx).write_relays(&public_key, cx);
};
let task: Task<Result<(), Error>> = cx.background_spawn(async move { let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let urls = write_relays.await;
let id = SubscriptionId::new("contact-list"); let id = SubscriptionId::new("contact-list");
let opts = SubscribeAutoCloseOptions::default() let opts = SubscribeAutoCloseOptions::default()
.exit_policy(ReqExitPolicy::ExitOnEOSE) .exit_policy(ReqExitPolicy::ExitOnEOSE)
.timeout(Some(Duration::from_secs(TIMEOUT))); .timeout(Some(Duration::from_secs(TIMEOUT)));
@@ -363,8 +343,13 @@ impl ChatRegistry {
.author(public_key) .author(public_key)
.limit(1); .limit(1);
let target = urls
.into_iter()
.map(|relay| (relay, vec![filter.clone()]))
.collect::<Vec<_>>();
// Subscribe // Subscribe
client.subscribe(filter).close_on(opts).with_id(id).await?; client.subscribe(target).close_on(opts).with_id(id).await?;
Ok(()) Ok(())
}); });
@@ -373,11 +358,11 @@ impl ChatRegistry {
} }
/// Get all messages for the provided signer /// Get all messages for the provided signer
fn get_messages(&mut self, signer: Arc<dyn AsyncNostrSigner>, cx: &mut Context<Self>) { fn get_messages(&mut self, public_key: &PublicKey, cx: &mut Context<Self>) {
let task = self.subscribe_gift_wrap_events(signer, cx); let future = self.subscribe_msg(public_key, cx);
self.tasks.push(cx.spawn(async move |this, cx| { self.tasks.push(cx.spawn(async move |this, cx| {
match task.await { match future.await {
Ok(_) => { Ok(_) => {
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.set_initializing(false, cx); this.set_initializing(false, cx);
@@ -399,9 +384,12 @@ impl ChatRegistry {
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let public_key = signer.public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
cx.background_spawn(async move { cx.background_spawn(async move {
let public_key = signer.get_public_key().await?;
let id = SubscriptionId::new("inbox-relay"); let id = SubscriptionId::new("inbox-relay");
let urls = write_relays.await;
// Construct filter for inbox relays // Construct filter for inbox relays
let filter = Filter::new() let filter = Filter::new()
@@ -409,9 +397,14 @@ impl ChatRegistry {
.author(public_key) .author(public_key)
.limit(1); .limit(1);
let target = urls
.into_iter()
.map(|relay| (relay, vec![filter.clone()]))
.collect::<Vec<_>>();
// Stream events from user's write relays // Stream events from user's write relays
let mut stream = client let mut stream = client
.stream_events(filter) .stream_events(target)
.with_id(id) .with_id(id)
.timeout(Duration::from_secs(TIMEOUT)) .timeout(Duration::from_secs(TIMEOUT))
.await?; .await?;
@@ -428,18 +421,14 @@ impl ChatRegistry {
} }
/// Continuously get gift wrap events for the signer /// Continuously get gift wrap events for the signer
fn subscribe_gift_wrap_events( fn subscribe_msg(&self, public_key: &PublicKey, cx: &App) -> Task<Result<(), Error>> {
&self,
signer: Arc<dyn AsyncNostrSigner>,
cx: &App,
) -> Task<Result<(), Error>> {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let urls = self.get_messaging_relays(cx); let urls = self.get_messaging_relays(cx);
let public_key = public_key.to_owned();
cx.background_spawn(async move { cx.background_spawn(async move {
let urls = urls.await?; let urls = urls.await?;
let public_key = signer.get_public_key().await?;
let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key); let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
let id = SubscriptionId::new(format!("{}-msg", public_key.to_hex())); let id = SubscriptionId::new(format!("{}-msg", public_key.to_hex()));
@@ -468,26 +457,30 @@ impl ChatRegistry {
/// Refresh the chat registry, fetching messages and contact list from relays. /// Refresh the chat registry, fetching messages and contact list from relays.
pub fn refresh(&mut self, window: &mut Window, cx: &mut Context<Self>) { pub fn refresh(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.reset(cx); self.reset(cx);
self.get_contact_list(cx);
self.get_rooms(cx); self.get_rooms(cx);
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
cx.spawn_in(window, async move |this, cx| { self.tasks.push(cx.spawn_in(window, async move |this, cx| {
let user_signer = signer.get().await; let user_signer = signer.get().await;
let device_signer = signer.get_encryption_signer().await; let user_pubkey = user_signer.get_public_key().await?;
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.get_messages(user_signer, cx); this.get_messages(&user_pubkey, cx);
})?;
let device_signer = signer.get_encryption_signer().await;
if let Some(device_signer) = device_signer { if let Some(device_signer) = device_signer {
this.get_messages(device_signer, cx); let device_pubkey = device_signer.get_public_key().await?;
this.update(cx, |this, cx| {
this.get_messages(&device_pubkey, cx);
})?;
} }
})
.ok(); Ok(())
}) }));
.detach();
} }
/// Set the initializing status of the chat registry /// Set the initializing status of the chat registry
@@ -791,14 +784,12 @@ impl ChatRegistry {
pub fn new_message(&mut self, message: NewMessage, cx: &mut Context<Self>) { pub fn new_message(&mut self, message: NewMessage, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let public_key = signer.public_key();
match self.rooms.iter().find(|e| e.read(cx).id == message.room) { match self.rooms.iter().find(|e| e.read(cx).id == message.room) {
Some(room) => { Some(room) => {
room.update(cx, |this, cx| { room.update(cx, |this, cx| {
if this.kind == RoomKind::Request if this.kind == RoomKind::Request && message.rumor.pubkey == public_key {
&& let Some(public_key) = signer.public_key()
&& message.rumor.pubkey == public_key
{
this.set_ongoing(cx); this.set_ongoing(cx);
} }
this.push_message(message, cx); this.push_message(message, cx);

View File

@@ -10,7 +10,8 @@ use itertools::Itertools;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use person::{Person, PersonRegistry}; use person::{Person, PersonRegistry};
use settings::{RoomConfig, SignerKind}; use settings::{RoomConfig, SignerKind};
use state::{CoopSigner, NostrRegistry, TIMEOUT}; use smol::lock::RwLock;
use state::{CoopSigner, Gossip, NostrRegistry, TIMEOUT};
use crate::NewMessage; use crate::NewMessage;
@@ -427,7 +428,7 @@ impl Room {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
// Get current user's public key // Get current user's public key
let sender = nostr.read(cx).signer().public_key()?; let sender = nostr.read(cx).signer().public_key();
// Get all members, excluding the sender // Get all members, excluding the sender
let members: Vec<Person> = self let members: Vec<Person> = self
@@ -477,10 +478,11 @@ impl Room {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let gossip = nostr.read(cx).gossip();
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
// Get current user's public key // Get current user's public key
let public_key = nostr.read(cx).signer().public_key()?; let public_key = nostr.read(cx).signer().public_key();
let sender = persons.read(cx).get(&public_key, cx); let sender = persons.read(cx).get(&public_key, cx);
let config = self.config.clone(); let config = self.config.clone();
@@ -529,7 +531,7 @@ impl Room {
}; };
// Send the gift wrap event and collect the report // Send the gift wrap event and collect the report
match send_gift_wrap(&client, &signer, &member, &rumor, use_encryption).await { match send(&client, &gossip, &signer, &member, &rumor, use_encryption).await {
Ok(report) => { Ok(report) => {
reports.push(report); reports.push(report);
sents += 1; sents += 1;
@@ -552,7 +554,7 @@ impl Room {
SignerKind::User => false, SignerKind::User => false,
}; };
match send_gift_wrap(&client, &signer, &sender, &rumor, use_encryption).await { match send(&client, &gossip, &signer, &sender, &rumor, use_encryption).await {
Ok(report) => reports.push(report), Ok(report) => reports.push(report),
Err(error) => { Err(error) => {
let report = SendReport::new(public_key).error(error.to_string()); let report = SendReport::new(public_key).error(error.to_string());
@@ -567,8 +569,9 @@ impl Room {
} }
// Helper function to send a gift-wrapped event // Helper function to send a gift-wrapped event
async fn send_gift_wrap( async fn send(
client: &Client, client: &Client,
gossip: &Arc<RwLock<Gossip>>,
signer: &Arc<CoopSigner>, signer: &Arc<CoopSigner>,
receiver: &Person, receiver: &Person,
rumor: &UnsignedEvent, rumor: &UnsignedEvent,
@@ -602,10 +605,15 @@ async fn send_gift_wrap(
} }
}; };
let relays = gossip.read().await.messaging_relays(&receiver);
for url in relays.iter() {
client.add_relay(url).and_connect().await?;
}
// Send the gift wrap event and collect the report // Send the gift wrap event and collect the report
let report = client let report = client
.send_event(&event) .send_event(&event)
.to_nip17() .to(relays)
.ack_policy(AckPolicy::none()) .ack_policy(AckPolicy::none())
.await .await
.map(|output| { .map(|output| {

View File

@@ -12,7 +12,7 @@ use gpui::{
}; };
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use person::PersonRegistry; use person::PersonRegistry;
use state::{Announcement, CoopSigner, NostrRegistry, StateEvent, TIMEOUT, app_name}; use state::{Announcement, CoopSigner, NostrRegistry, StateEvent, app_name};
use theme::ActiveTheme; use theme::ActiveTheme;
use ui::avatar::Avatar; use ui::avatar::Avatar;
use ui::button::Button; use ui::button::Button;
@@ -35,7 +35,7 @@ impl Global for GlobalDeviceRegistry {}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum DeviceEvent { pub enum DeviceEvent {
/// A new encryption signer has been set /// A new encryption signer has been set
Set, Set(PublicKey),
/// The device is requesting an encryption key /// The device is requesting an encryption key
Requesting, Requesting,
/// The device is creating a new encryption key /// The device is creating a new encryption key
@@ -90,9 +90,9 @@ impl DeviceRegistry {
// Subscribe to nostr state events // Subscribe to nostr state events
let subscription = cx.subscribe_in(&nostr, window, |this, _e, event, _window, cx| { let subscription = cx.subscribe_in(&nostr, window, |this, _e, event, _window, cx| {
if event == &StateEvent::SignerSet { if let StateEvent::SignerSet(public_key) = event {
this.set_initializing(true, cx); this.set_initializing(true, cx);
this.get_announcement(cx); this.get_announcement(public_key, cx);
}; };
}); });
@@ -190,12 +190,13 @@ impl DeviceRegistry {
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
self.tasks.push(cx.spawn(async move |this, cx| { self.tasks.push(cx.spawn(async move |this, cx| {
let public_key = new.get_public_key().await?;
signer.set_encryption_signer(new).await; signer.set_encryption_signer(new).await;
// Update state // Update state
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.set_initializing(false, cx); this.set_initializing(false, cx);
cx.emit(DeviceEvent::Set); cx.emit(DeviceEvent::Set(public_key));
})?; })?;
Ok(()) Ok(())
@@ -219,14 +220,16 @@ impl DeviceRegistry {
} }
/// Get device announcement for current user /// Get device announcement for current user
pub fn get_announcement(&mut self, cx: &mut Context<Self>) { pub fn get_announcement(&mut self, public_key: &PublicKey, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let write_relays = nostr.read(cx).write_relays(public_key, cx);
let task: Task<Result<Event, Error>> = cx.background_spawn(async move { let task: Task<Result<Event, Error>> = cx.background_spawn(async move {
let signer = signer.get().await; let signer = signer.get().await;
let public_key = signer.get_public_key().await?; let public_key = signer.get_public_key().await?;
let urls = write_relays.await;
// Construct the filter for the device announcement event // Construct the filter for the device announcement event
let filter = Filter::new() let filter = Filter::new()
@@ -234,10 +237,15 @@ impl DeviceRegistry {
.author(public_key) .author(public_key)
.limit(1); .limit(1);
let target = urls
.into_iter()
.map(|relay| (relay, vec![filter.clone()]))
.collect::<Vec<_>>();
// Stream events from user's write relays // Stream events from user's write relays
let mut stream = client let mut stream = client
.stream_events(filter) .stream_events(target)
.timeout(Duration::from_secs(TIMEOUT)) .timeout(Duration::from_secs(10))
.await?; .await?;
while let Some((_url, res)) = stream.next().await { while let Some((_url, res)) = stream.next().await {
@@ -373,9 +381,12 @@ impl DeviceRegistry {
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let public_key = signer.public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
self.tasks.push(cx.background_spawn(async move { self.tasks.push(cx.background_spawn(async move {
let public_key = signer.get_public_key().await?;
let id = SubscriptionId::new("dekey-requests"); let id = SubscriptionId::new("dekey-requests");
let urls = write_relays.await;
// Construct a filter for encryption key requests // Construct a filter for encryption key requests
let filter = Filter::new() let filter = Filter::new()
@@ -383,8 +394,13 @@ impl DeviceRegistry {
.author(public_key) .author(public_key)
.since(Timestamp::now()); .since(Timestamp::now());
let target = urls
.into_iter()
.map(|relay| (relay, vec![filter.clone()]))
.collect::<Vec<_>>();
// Subscribe to the device key requests on user's write relays // Subscribe to the device key requests on user's write relays
client.subscribe(vec![filter]).with_id(id).await?; client.subscribe(target).with_id(id).await?;
Ok(()) Ok(())
})); }));
@@ -396,12 +412,13 @@ impl DeviceRegistry {
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let public_key = signer.public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
let app_keys = nostr.read(cx).keys(); let app_keys = nostr.read(cx).keys();
let app_pubkey = app_keys.public_key(); let app_pubkey = app_keys.public_key();
let task: Task<Result<Option<Event>, Error>> = cx.background_spawn(async move { let task: Task<Result<Option<Event>, Error>> = cx.background_spawn(async move {
let public_key = signer.get_public_key().await?;
// Construct a filter to get the latest approval event // Construct a filter to get the latest approval event
let filter = Filter::new() let filter = Filter::new()
.kind(Kind::Custom(4455)) .kind(Kind::Custom(4455))
@@ -414,6 +431,7 @@ impl DeviceRegistry {
Some(event) => Ok(Some(event)), Some(event) => Ok(Some(event)),
// No approval event found, construct a request event // No approval event found, construct a request event
None => { None => {
let write_relays = write_relays.await;
let signer = signer.get().await; let signer = signer.get().await;
// Construct an event for device key request // Construct an event for device key request
@@ -430,7 +448,7 @@ impl DeviceRegistry {
.await?; .await?;
// Send the event to write relays // Send the event to write relays
client.send_event(&event).to_nip65().await?; client.send_event(&event).to(write_relays).await?;
Ok(None) Ok(None)
} }
@@ -468,8 +486,11 @@ impl DeviceRegistry {
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let public_key = signer.public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
self.tasks.push(cx.background_spawn(async move { self.tasks.push(cx.background_spawn(async move {
let public_key = signer.get_public_key().await?; let urls = write_relays.await;
// Construct a filter for device key requests // Construct a filter for device key requests
let filter = Filter::new() let filter = Filter::new()
@@ -477,8 +498,13 @@ impl DeviceRegistry {
.author(public_key) .author(public_key)
.since(Timestamp::now()); .since(Timestamp::now());
let target = urls
.into_iter()
.map(|relay| (relay, vec![filter.clone()]))
.collect::<Vec<_>>();
// Subscribe to the device key requests on user's write relays // Subscribe to the device key requests on user's write relays
client.subscribe(filter).await?; client.subscribe(target).await?;
Ok(()) Ok(())
})); }));
@@ -531,6 +557,9 @@ impl DeviceRegistry {
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let signer_pubkey = signer.public_key();
let write_relays = nostr.read(cx).write_relays(&signer_pubkey, cx);
// Get user's write relays // Get user's write relays
let event = event.clone(); let event = event.clone();
let id: SharedString = event.id.to_hex().into(); let id: SharedString = event.id.to_hex().into();
@@ -542,6 +571,7 @@ impl DeviceRegistry {
let secret = keys.secret_key().to_secret_hex(); let secret = keys.secret_key().to_secret_hex();
let device_signer: Arc<dyn AsyncNostrSigner> = Arc::new(keys); let device_signer: Arc<dyn AsyncNostrSigner> = Arc::new(keys);
let write_relays = write_relays.await;
let signer = signer.get().await; let signer = signer.get().await;
// Extract the target public key from the event tags // Extract the target public key from the event tags
@@ -569,7 +599,7 @@ impl DeviceRegistry {
.await?; .await?;
// Send the response event to the user's relay list // Send the response event to the user's relay list
client.send_event(&event).to_nip65().await?; client.send_event(&event).to(write_relays).await?;
Ok(()) Ok(())
}); });

View File

@@ -10,7 +10,6 @@ common = { path = "../common" }
nostr.workspace = true nostr.workspace = true
nostr-sdk.workspace = true nostr-sdk.workspace = true
nostr-lmdb.workspace = true nostr-lmdb.workspace = true
nostr-gossip-memory.workspace = true
nostr-connect.workspace = true nostr-connect.workspace = true
nostr-blossom.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::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@@ -7,19 +7,21 @@ use anyhow::{Error, anyhow};
use common::config_dir; use common::config_dir;
use gpui::{App, AppContext, Context, Entity, EventEmitter, Global, SharedString, Task, Window}; use gpui::{App, AppContext, Context, Entity, EventEmitter, Global, SharedString, Task, Window};
use nostr_connect::prelude::*; use nostr_connect::prelude::*;
use nostr_gossip_memory::prelude::*;
use nostr_lmdb::prelude::*; use nostr_lmdb::prelude::*;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use smol::lock::RwLock;
mod blossom; mod blossom;
mod constants; mod constants;
mod device; mod device;
mod gossip;
mod nip05; mod nip05;
mod signer; mod signer;
pub use blossom::*; pub use blossom::*;
pub use constants::*; pub use constants::*;
pub use device::*; pub use device::*;
pub use gossip::*;
pub use nip05::*; pub use nip05::*;
pub use signer::*; pub use signer::*;
@@ -53,7 +55,7 @@ pub enum StateEvent {
/// Show the identity dialog /// Show the identity dialog
Show, Show,
/// A new signer has been set /// A new signer has been set
SignerSet, SignerSet(PublicKey),
/// An error occurred /// An error occurred
Error(SharedString), Error(SharedString),
} }
@@ -73,6 +75,9 @@ pub struct NostrRegistry {
/// Nostr client /// Nostr client
client: Client, client: Client,
/// Nostr gossip implementation
gossip: Arc<RwLock<Gossip>>,
/// Nostr signer /// Nostr signer
signer: Arc<CoopSigner>, signer: Arc<CoopSigner>,
@@ -109,7 +114,7 @@ impl NostrRegistry {
let key_dir = config_dir().join("keys"); let key_dir = config_dir().join("keys");
let app_keys = get_or_init_app_keys(cx).unwrap_or(Keys::generate()); 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())); let signer = Arc::new(CoopSigner::new(app_keys.clone()));
// Get all local stored npubs // Get all local stored npubs
@@ -131,8 +136,8 @@ impl NostrRegistry {
// Construct the nostr client // Construct the nostr client
let client = ClientBuilder::default() let client = ClientBuilder::default()
.database(lmdb) .database(lmdb)
.gossip(NostrGossipMemory::unbounded())
.connect_timeout(Duration::from_secs(10)) .connect_timeout(Duration::from_secs(10))
.max_avg_latency(Duration::from_millis(800))
.sleep_when_idle(SleepWhenIdle::Enabled { .sleep_when_idle(SleepWhenIdle::Enabled {
timeout: Duration::from_secs(600), timeout: Duration::from_secs(600),
}) })
@@ -141,6 +146,8 @@ impl NostrRegistry {
// Run at the end of current cycle // Run at the end of current cycle
cx.defer_in(window, |this, _window, cx| { cx.defer_in(window, |this, _window, cx| {
this.connect(cx); this.connect(cx);
this.handle_notifications(cx);
// Create an identity if none exists // Create an identity if none exists
if this.npubs.read(cx).is_empty() { if this.npubs.read(cx).is_empty() {
this.create_identity(cx); this.create_identity(cx);
@@ -152,6 +159,7 @@ impl NostrRegistry {
Self { Self {
client, client,
gossip,
signer, signer,
npubs, npubs,
key_dir, key_dir,
@@ -165,6 +173,11 @@ impl NostrRegistry {
self.client.clone() self.client.clone()
} }
/// Get the gossip instance
pub fn gossip(&self) -> Arc<RwLock<Gossip>> {
self.gossip.clone()
}
/// Get the nostr signer /// Get the nostr signer
pub fn signer(&self) -> Arc<CoopSigner> { pub fn signer(&self) -> Arc<CoopSigner> {
self.signer.clone() self.signer.clone()
@@ -207,11 +220,10 @@ impl NostrRegistry {
Ok(npubs) Ok(npubs)
} }
/// Connect to the bootstrapping relays fn background_connect(&self, cx: &App) -> Task<Result<(), Error>> {
fn connect(&mut self, cx: &mut Context<Self>) {
let client = self.client(); 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 // Add search relay to the relay pool
for url in SEARCH_RELAYS.into_iter() { for url in SEARCH_RELAYS.into_iter() {
client client
@@ -237,20 +249,60 @@ impl NostrRegistry {
client.connect().await; client.connect().await;
Ok(()) Ok(())
}); })
}
/// Connect to the bootstrapping relays
fn connect(&mut self, cx: &mut Context<Self>) {
let task = self.background_connect(cx);
// Emit connecting event // Emit connecting event
cx.emit(StateEvent::Connecting); 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 { if let Err(e) = task.await {
this.update(cx, |_this, cx| { this.update(cx, |_this, cx| {
cx.emit(StateEvent::error(e.to_string())); cx.emit(StateEvent::error(e.to_string()));
})?; })
.ok();
} else { } else {
this.update(cx, |_this, cx| { this.update(cx, |_this, cx| {
cx.emit(StateEvent::Connected); 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(()) Ok(())
@@ -264,6 +316,7 @@ impl NostrRegistry {
let npub = public_key.to_bech32().unwrap(); let npub = public_key.to_bech32().unwrap();
let key_path = self.key_dir.join(format!("{}.npub", npub)); let key_path = self.key_dir.join(format!("{}.npub", npub));
let app_keys = self.app_keys.clone(); let app_keys = self.app_keys.clone();
let app_signer: Arc<dyn NostrSigner> = Arc::new(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) { if let Ok(secret) = SecretKey::parse(&decrypted) {
let keys = Keys::new(secret); 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; signer.switch(keys).await;
client.unsubscribe_all().await.ok(); 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(); .detach();
@@ -286,9 +348,29 @@ impl NostrRegistry {
let mut nip46 = NostrConnect::new(uri, app_keys, timeout, None)?; let mut nip46 = NostrConnect::new(uri, app_keys, timeout, None)?;
nip46.auth_url_handler(CoopAuthUrlHandler); nip46.auth_url_handler(CoopAuthUrlHandler);
cx.spawn(async move |_cx| { cx.spawn(async move |cx| {
signer.switch(nip46).await; signer.switch(nip46).await;
client.unsubscribe_all().await.ok(); 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(); .detach();
@@ -299,33 +381,6 @@ impl NostrRegistry {
Err(anyhow!("Secret not found")) 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 /// Add a new npub to the keys directory
fn write_secret( fn write_secret(
&self, &self,
@@ -335,14 +390,14 @@ impl NostrRegistry {
) -> Task<Result<(), Error>> { ) -> Task<Result<(), Error>> {
let npub = public_key.to_bech32().unwrap(); let npub = public_key.to_bech32().unwrap();
let key_path = self.key_dir.join(format!("{}.npub", npub)); 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 { cx.background_spawn(async move {
// If the secret starts with "bunker://" (nostr connect), use it directly; otherwise, encrypt it // If the secret starts with "bunker://" (nostr connect), use it directly; otherwise, encrypt it
let content = if secret.starts_with("bunker://") { let content = if secret.starts_with("bunker://") {
secret secret
} else { } 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 // Write the encrypted secret to the keys directory
@@ -386,14 +441,35 @@ impl NostrRegistry {
let task: Task<Result<(), Error>> = cx.background_spawn(async move { let task: Task<Result<(), Error>> = cx.background_spawn(async move {
// Construct relay list event // Construct relay list event
let relay_list = default_relay_list(); 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) .sign_async(&async_keys)
.await?; .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 // Publish relay list
client client
.send_event(&event) .send_event(&event)
.to(BOOTSTRAP_RELAYS) .to(&write_relays)
.ack_policy(AckPolicy::none()) .ack_policy(AckPolicy::none())
.await?; .await?;
@@ -408,7 +484,7 @@ impl NostrRegistry {
// Publish metadata event // Publish metadata event
client client
.send_event(&event) .send_event(&event)
.to_nip65() .to(&write_relays)
.ack_policy(AckPolicy::none()) .ack_policy(AckPolicy::none())
.await?; .await?;
@@ -421,7 +497,7 @@ impl NostrRegistry {
// Publish contact list event // Publish contact list event
client client
.send_event(&event) .send_event(&event)
.to_nip65() .to(&write_relays)
.ack_policy(AckPolicy::none()) .ack_policy(AckPolicy::none())
.await?; .await?;
@@ -432,7 +508,11 @@ impl NostrRegistry {
.await?; .await?;
// Publish messaging relay list event // 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 user's credentials to the system keyring
write_secret.await?; write_secret.await?;
@@ -444,7 +524,9 @@ impl NostrRegistry {
match task.await { match task.await {
Ok(_) => { Ok(_) => {
this.update(cx, |this, cx| { 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) => { Err(e) => {
@@ -468,7 +550,9 @@ impl NostrRegistry {
match write_secret.await { match write_secret.await {
Ok(_) => { Ok(_) => {
this.update(cx, |this, cx| { 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) => { Err(e) => {
@@ -507,7 +591,9 @@ impl NostrRegistry {
match write_secret.await { match write_secret.await {
Ok(_) => { Ok(_) => {
this.update(cx, |this, cx| { 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) => { Err(e) => {
@@ -621,69 +707,64 @@ impl NostrRegistry {
}) })
} }
/// Perform a WoT (via Vertex) search for a given query. pub fn write_relays(&self, public_key: &PublicKey, cx: &App) -> Task<Vec<RelayUrl>> {
pub fn wot_search(&self, query: &str, cx: &App) -> Task<Result<Vec<PublicKey>, Error>> {
let client = self.client(); let client = self.client();
let query = query.to_string(); let gossip = self.gossip.clone();
let signer = self.signer(); let public_key = public_key.to_owned();
cx.background_spawn(async move { cx.background_spawn(async move {
let signer = signer.get().await; let urls = gossip.read().await.write_relays(&public_key);
// Construct a vertex request event // Ensure the client is connected to each relay
let event = EventBuilder::new(Kind::Custom(5315), "") for url in urls.iter() {
.tags(vec![ client
Tag::custom("param", vec!["search", &query]), .add_relay(url)
Tag::custom("param", vec!["limit", "10"]), .capabilities(RelayCapabilities::WRITE)
]) .and_connect()
.sign_async(&signer) .await
.await?; .ok();
// 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"));
}
_ => {}
}
}
} }
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>)> { fn default_relay_list() -> Vec<(RelayUrl, Option<RelayMetadata>)> {
vec![ vec![
( (

View File

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

View File

@@ -1,8 +1,7 @@
use anyhow::Error;
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
App, AppContext, Context, Entity, InteractiveElement, IntoElement, ParentElement, Render, App, AppContext, Context, Entity, InteractiveElement, IntoElement, ParentElement, Render,
SharedString, StatefulInteractiveElement, Styled, Subscription, Task, Window, div, px, SharedString, StatefulInteractiveElement, Styled, Subscription, Window, div, px,
}; };
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use person::PersonRegistry; use person::PersonRegistry;
@@ -28,11 +27,8 @@ pub struct AccountSelector {
/// The error message displayed when an error occurs. /// The error message displayed when an error occurs.
error: Entity<Option<SharedString>>, error: Entity<Option<SharedString>>,
/// Async tasks
tasks: Vec<Task<Result<(), Error>>>,
/// Subscription to the signer events /// Subscription to the signer events
_subscription: Option<Subscription>, _event_subscription: Option<Subscription>,
} }
impl AccountSelector { impl AccountSelector {
@@ -44,7 +40,7 @@ impl AccountSelector {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let subscription = cx.subscribe_in(&nostr, window, |this, _state, event, window, cx| { let subscription = cx.subscribe_in(&nostr, window, |this, _state, event, window, cx| {
match event { match event {
StateEvent::SignerSet => { StateEvent::SignerSet(_) => {
window.close_all_modals(cx); window.close_all_modals(cx);
window.refresh(); window.refresh();
} }
@@ -58,8 +54,7 @@ impl AccountSelector {
Self { Self {
logging_in, logging_in,
error, error,
tasks: vec![], _event_subscription: Some(subscription),
_subscription: Some(subscription),
} }
} }
@@ -89,20 +84,14 @@ impl AccountSelector {
}) })
} }
fn login(&mut self, public_key: PublicKey, window: &mut Window, cx: &mut Context<Self>) { fn login(&mut self, public_key: PublicKey, _window: &mut Window, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
// Mark the public key as being logged in // Mark the public key as being logged in
self.set_logging_in(public_key, cx); self.set_logging_in(public_key, cx);
match nostr.read(cx).switch_account(public_key, cx) { let nostr = NostrRegistry::global(cx);
Ok(()) => { if let Err(e) = nostr.read(cx).switch_account(public_key, cx) {
//
}
Err(e) => {
self.set_error(e.to_string(), cx); self.set_error(e.to_string(), cx);
} }
};
} }
fn remove(&mut self, public_key: PublicKey, cx: &mut Context<Self>) { fn remove(&mut self, public_key: PublicKey, cx: &mut Context<Self>) {

View File

@@ -158,6 +158,9 @@ impl ContactListPanel {
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let public_key = signer.public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
// Get contacts // Get contacts
let contacts: Vec<Contact> = self let contacts: Vec<Contact> = self
.contacts .contacts
@@ -169,6 +172,7 @@ impl ContactListPanel {
self.set_updating(true, cx); self.set_updating(true, cx);
let task: Task<Result<(), Error>> = cx.background_spawn(async move { let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let write_relays = write_relays.await;
let signer = signer.get().await; let signer = signer.get().await;
// Construct contact list event builder // Construct contact list event builder
@@ -177,7 +181,7 @@ impl ContactListPanel {
.await?; .await?;
// Set contact list // Set contact list
client.send_event(&event).to_nip65().await?; client.send_event(&event).to(write_relays).await?;
Ok(()) Ok(())
}); });

View File

@@ -31,8 +31,8 @@ impl GreeterPanel {
fn add_profile_panel(&mut self, window: &mut Window, cx: &mut Context<Self>) { fn add_profile_panel(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let public_key = signer.public_key();
if let Some(public_key) = signer.public_key() {
cx.spawn_in(window, async move |_this, cx| { cx.spawn_in(window, async move |_this, cx| {
cx.update(|window, cx| { cx.update(|window, cx| {
Workspace::add_panel( Workspace::add_panel(
@@ -46,7 +46,6 @@ impl GreeterPanel {
}) })
.detach(); .detach();
} }
}
} }
impl Panel for GreeterPanel { impl Panel for GreeterPanel {

View File

@@ -172,6 +172,9 @@ impl MessagingRelayPanel {
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let public_key = signer.public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
// Construct event tags // Construct event tags
let tags: Vec<Tag> = self let tags: Vec<Tag> = self
.relays .relays
@@ -183,6 +186,7 @@ impl MessagingRelayPanel {
self.set_updating(true, cx); self.set_updating(true, cx);
let task: Task<Result<(), Error>> = cx.background_spawn(async move { let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let write_relays = write_relays.await;
let signer = signer.get().await; let signer = signer.get().await;
// Construct nip17 event builder // Construct nip17 event builder
@@ -192,7 +196,7 @@ impl MessagingRelayPanel {
.await?; .await?;
// Set messaging relays // Set messaging relays
client.send_event(&event).to_nip65().await?; client.send_event(&event).to(write_relays).await?;
Ok(()) Ok(())
}); });

View File

@@ -208,9 +208,14 @@ impl ProfilePanel {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let public_key = signer.public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
let metadata = metadata.clone(); let metadata = metadata.clone();
cx.background_spawn(async move { cx.background_spawn(async move {
let write_relays = write_relays.await;
let signer = signer.get().await; let signer = signer.get().await;
// Build and sign the metadata event // Build and sign the metadata event
@@ -219,7 +224,7 @@ impl ProfilePanel {
.await?; .await?;
// Send event to user's relays // Send event to user's relays
client.send_event(&event).await?; client.send_event(&event).to(write_relays).await?;
Ok(()) Ok(())
}) })

View File

@@ -11,7 +11,7 @@ use gpui::{
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use serde::Deserialize; use serde::Deserialize;
use smallvec::{SmallVec, smallvec}; use smallvec::{SmallVec, smallvec};
use state::NostrRegistry; use state::{BOOTSTRAP_RELAYS, NostrRegistry};
use theme::ActiveTheme; use theme::ActiveTheme;
use ui::button::{Button, ButtonVariants}; use ui::button::{Button, ButtonVariants};
use ui::dock::{Panel, PanelEvent}; use ui::dock::{Panel, PanelEvent};
@@ -219,7 +219,7 @@ impl RelayListPanel {
let event = EventBuilder::relay_list(relays).sign_async(&signer).await?; let event = EventBuilder::relay_list(relays).sign_async(&signer).await?;
// Set relay list for current user // Set relay list for current user
client.send_event(&event).await?; client.send_event(&event).to(BOOTSTRAP_RELAYS).await?;
Ok(()) Ok(())
}); });

View File

@@ -126,7 +126,7 @@ impl Workspace {
window.push_notification(note, cx); window.push_notification(note, cx);
} }
StateEvent::SignerSet => { StateEvent::SignerSet(_) => {
this.set_center_layout(window, cx); this.set_center_layout(window, cx);
// Clear the signer notification // Clear the signer notification
window.clear_notification::<SignerNotifcation>(cx); window.clear_notification::<SignerNotifcation>(cx);
@@ -165,7 +165,7 @@ impl Workspace {
window.push_notification(note, cx); window.push_notification(note, cx);
} }
DeviceEvent::Set => { DeviceEvent::Set(_) => {
let note = Notification::new() let note = Notification::new()
.id::<DeviceNotifcation>() .id::<DeviceNotifcation>()
.message("Encryption Key has been set") .message("Encryption Key has been set")
@@ -307,8 +307,8 @@ impl Workspace {
Command::ShowProfile => { Command::ShowProfile => {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let public_key = signer.public_key();
if let Some(public_key) = signer.public_key() {
self.dock.update(cx, |this, cx| { self.dock.update(cx, |this, cx| {
this.add_panel( this.add_panel(
Arc::new(profile::init(public_key, window, cx)), Arc::new(profile::init(public_key, window, cx)),
@@ -318,7 +318,6 @@ impl Workspace {
); );
}); });
} }
}
Command::ShowContactList => { Command::ShowContactList => {
self.dock.update(cx, |this, cx| { self.dock.update(cx, |this, cx| {
this.add_panel( this.add_panel(
@@ -368,8 +367,11 @@ impl Workspace {
} }
Command::RefreshEncryption => { Command::RefreshEncryption => {
let device = DeviceRegistry::global(cx); let device = DeviceRegistry::global(cx);
let nostr = NostrRegistry::global(cx);
let public_key = nostr.read(cx).signer().public_key();
device.update(cx, |this, cx| { device.update(cx, |this, cx| {
this.get_announcement(cx); this.get_announcement(&public_key, cx);
}); });
} }
Command::ResetEncryption => { Command::ResetEncryption => {
@@ -561,26 +563,29 @@ impl Workspace {
fn titlebar_left(&mut self, cx: &mut Context<Self>) -> impl IntoElement { fn titlebar_left(&mut self, cx: &mut Context<Self>) -> impl IntoElement {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let current_user = signer.public_key(); let is_logged_in = signer.is_logged_in();
if !is_logged_in {
return div();
}
let persons = PersonRegistry::global(cx);
let public_key = signer.public_key();
let profile = persons.read(cx).get(&public_key, cx);
let avatar = profile.avatar();
let name = profile.name();
h_flex() h_flex()
.flex_shrink_0() .flex_shrink_0()
.gap_2() .gap_2()
.when_none(&current_user, |this| { .child(
this.child(
div() div()
.text_xs() .text_xs()
.text_color(cx.theme().text_muted) .text_color(cx.theme().text_muted)
.child(SharedString::from("Choose an account to continue...")), .child(SharedString::from("Choose an account to continue...")),
) )
}) .child(
.when_some(current_user.as_ref(), |this, public_key| {
let persons = PersonRegistry::global(cx);
let profile = persons.read(cx).get(public_key, cx);
let avatar = profile.avatar();
let name = profile.name();
this.child(
Button::new("current-user") Button::new("current-user")
.child(Avatar::new(avatar.clone()).xsmall()) .child(Avatar::new(avatar.clone()).xsmall())
.small() .small()
@@ -616,11 +621,7 @@ impl Workspace {
IconName::UserKey, IconName::UserKey,
Box::new(Command::ShowBackup), Box::new(Command::ShowBackup),
) )
.menu_with_icon( .menu_with_icon("Themes", IconName::Sun, Box::new(Command::ToggleTheme))
"Themes",
IconName::Sun,
Box::new(Command::ToggleTheme),
)
.separator() .separator()
.menu_with_icon( .menu_with_icon(
"Accounts", "Accounts",
@@ -634,7 +635,6 @@ impl Workspace {
) )
}), }),
) )
})
} }
fn titlebar_right(&mut self, cx: &mut Context<Self>) -> impl IntoElement { fn titlebar_right(&mut self, cx: &mut Context<Self>) -> impl IntoElement {
@@ -647,12 +647,14 @@ impl Workspace {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let signer = nostr.read(cx).signer(); let signer = nostr.read(cx).signer();
let is_logged_in = signer.is_logged_in();
let Some(public_key) = signer.public_key() else { if !is_logged_in {
return div(); return div();
}; }
let persons = PersonRegistry::global(cx); let persons = PersonRegistry::global(cx);
let public_key = signer.public_key();
let profile = persons.read(cx).get(&public_key, cx); let profile = persons.read(cx).get(&public_key, cx);
let announcement = profile.announcement(); let announcement = profile.announcement();