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

Closed
reya wants to merge 2 commits from feat/nostrkit into master
23 changed files with 1092 additions and 883 deletions

577
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -24,7 +24,7 @@ nostr-connect = { git = "https://github.com/rust-nostr/nostr" }
nostr-blossom = { git = "https://github.com/rust-nostr/nostr" }
nostr-gossip-memory = { git = "https://github.com/rust-nostr/nostr" }
nostr-sdk = { git = "https://github.com/rust-nostr/nostr" }
nostr = { git = "https://github.com/rust-nostr/nostr", features = [ "nip96", "nip59", "nip49", "nip44" ] }
nostr = { git = "https://github.com/rust-nostr/nostr", features = [ "nip59", "nip49", "nip44" ] }
# Others
anyhow = "1.0.44"

View File

@@ -134,41 +134,21 @@ impl ChatRegistry {
subscriptions.push(
// Subscribe to the signer event
cx.subscribe_in(&nostr, window, |this, state, event, window, cx| {
if event == &StateEvent::SignerSet {
cx.subscribe(&nostr, |this, _state, event, cx| {
if let StateEvent::SignerSet(public_key) = event {
this.reset(cx);
this.get_contact_list(cx);
this.get_contact_list(public_key, cx);
this.get_rooms(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();
this.get_messages(public_key, cx);
};
}),
);
subscriptions.push(
// Subscribe to the device event
cx.subscribe_in(&device, window, |_this, _s, event, window, cx| {
if event == &DeviceEvent::Set {
let nostr = NostrRegistry::global(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();
cx.subscribe(&device, |this, _device, event, cx| {
if let DeviceEvent::Set(public_key) = event {
this.get_messages(public_key, cx);
};
}),
);
@@ -342,17 +322,17 @@ impl ChatRegistry {
}
/// 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 client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let Some(public_key) = signer.public_key() else {
return;
};
let public_key = public_key.to_owned();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let urls = write_relays.await;
let id = SubscriptionId::new("contact-list");
let opts = SubscribeAutoCloseOptions::default()
.exit_policy(ReqExitPolicy::ExitOnEOSE)
.timeout(Some(Duration::from_secs(TIMEOUT)));
@@ -363,8 +343,13 @@ impl ChatRegistry {
.author(public_key)
.limit(1);
let target = urls
.into_iter()
.map(|relay| (relay, vec![filter.clone()]))
.collect::<Vec<_>>();
// Subscribe
client.subscribe(filter).close_on(opts).with_id(id).await?;
client.subscribe(target).close_on(opts).with_id(id).await?;
Ok(())
});
@@ -373,14 +358,11 @@ impl ChatRegistry {
}
/// Get all messages for the provided signer
fn get_messages<T>(&mut self, signer: T, cx: &mut Context<Self>)
where
T: NostrSigner + 'static,
{
let task = self.subscribe_gift_wrap_events(signer, cx);
fn get_messages(&mut self, public_key: &PublicKey, cx: &mut Context<Self>) {
let future = self.subscribe_msg(public_key, cx);
self.tasks.push(cx.spawn(async move |this, cx| {
match task.await {
match future.await {
Ok(_) => {
this.update(cx, |this, cx| {
this.set_initializing(false, cx);
@@ -402,9 +384,12 @@ impl ChatRegistry {
let client = nostr.read(cx).client();
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 {
let public_key = signer.get_public_key().await?;
let id = SubscriptionId::new("inbox-relay");
let urls = write_relays.await;
// Construct filter for inbox relays
let filter = Filter::new()
@@ -412,16 +397,21 @@ impl ChatRegistry {
.author(public_key)
.limit(1);
let target = urls
.into_iter()
.map(|relay| (relay, vec![filter.clone()]))
.collect::<Vec<_>>();
// Stream events from user's write relays
let mut stream = client
.stream_events(filter)
.stream_events(target)
.with_id(id)
.timeout(Duration::from_secs(TIMEOUT))
.await?;
while let Some((_url, res)) = stream.next().await {
if let Ok(event) = res {
let urls: Vec<RelayUrl> = nip17::extract_owned_relay_list(event).collect();
let urls: Vec<RelayUrl> = nip17::extract_relay_list(&event).collect();
return Ok(urls);
}
}
@@ -431,17 +421,14 @@ impl ChatRegistry {
}
/// Continuously get gift wrap events for the signer
fn subscribe_gift_wrap_events<T>(&self, signer: T, cx: &App) -> Task<Result<(), Error>>
where
T: NostrSigner + 'static,
{
fn subscribe_msg(&self, public_key: &PublicKey, cx: &App) -> Task<Result<(), Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let urls = self.get_messaging_relays(cx);
let public_key = public_key.to_owned();
cx.background_spawn(async move {
let urls = urls.await?;
let public_key = signer.get_public_key().await?;
let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
let id = SubscriptionId::new(format!("{}-msg", public_key.to_hex()));
@@ -470,26 +457,30 @@ impl ChatRegistry {
/// Refresh the chat registry, fetching messages and contact list from relays.
pub fn refresh(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.reset(cx);
self.get_contact_list(cx);
self.get_rooms(cx);
let nostr = NostrRegistry::global(cx);
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 device_signer = signer.get_encryption_signer().await;
let user_pubkey = user_signer.get_public_key().await?;
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 {
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();
})
.detach();
Ok(())
}));
}
/// Set the initializing status of the chat registry
@@ -579,10 +570,9 @@ impl ChatRegistry {
I: Into<Room> + 'static,
{
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
cx.spawn(async move |this, cx| {
let signer = client.signer()?;
let public_key = signer.get_public_key().await.ok()?;
let room: Room = room.into().organize(&public_key);
@@ -712,9 +702,9 @@ impl ChatRegistry {
fn get_rooms_from_database(&self, cx: &App) -> Task<Result<HashSet<Room>, Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
// Get contacts
@@ -794,14 +784,12 @@ impl ChatRegistry {
pub fn new_message(&mut self, message: NewMessage, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let signer = nostr.read(cx).signer();
let public_key = signer.public_key();
match self.rooms.iter().find(|e| e.read(cx).id == message.room) {
Some(room) => {
room.update(cx, |this, cx| {
if this.kind == RoomKind::Request
&& let Some(public_key) = signer.public_key()
&& message.rumor.pubkey == public_key
{
if this.kind == RoomKind::Request && message.rumor.pubkey == public_key {
this.set_ongoing(cx);
}
this.push_message(message, cx);
@@ -871,10 +859,10 @@ async fn try_unwrap(signer: &Arc<CoopSigner>, gift_wrap: &Event) -> Result<Unwra
}
/// Attempts to unwrap a gift wrap event with a given signer.
async fn try_unwrap_with<T>(gift_wrap: &Event, signer: &T) -> Result<UnwrappedGift, Error>
where
T: NostrSigner + 'static,
{
async fn try_unwrap_with(
gift_wrap: &Event,
signer: &Arc<dyn AsyncNostrSigner>,
) -> Result<UnwrappedGift, Error> {
// Get the sealed event
let seal = signer
.nip44_decrypt(&gift_wrap.pubkey, &gift_wrap.content)
@@ -906,26 +894,17 @@ async fn set_rumor(client: &Client, id: EventId, rumor: &UnsignedEvent) -> Resul
tags.push(Tag::identifier(id));
// Add a reference to the rumor's author
tags.push(Tag::custom(
TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::A)),
[author],
));
tags.push(Tag::custom("a", [author]));
// Add a conversation id
tags.push(Tag::custom(
TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::C)),
[conversation.to_string()],
));
tags.push(Tag::custom("c", [conversation.to_string()]));
// Add a reference to the rumor's id
tags.push(Tag::event(rumor_id));
// Add references to the rumor's participants
for receiver in rumor.tags.public_keys().copied() {
tags.push(Tag::custom(
TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::P)),
[receiver],
));
for receiver in rumor.tags.public_keys() {
tags.push(Tag::public_key(receiver));
}
// Convert rumor to json
@@ -934,7 +913,7 @@ async fn set_rumor(client: &Client, id: EventId, rumor: &UnsignedEvent) -> Resul
// Construct the event
let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
.tags(tags)
.sign(&Keys::generate())
.sign_async(&Keys::generate())
.await?;
// Save the event to the database
@@ -960,7 +939,7 @@ async fn get_rumor(client: &Client, gift_wrap: EventId) -> Result<UnsignedEvent,
/// Get the conversation ID for a given rumor (message).
fn conversation_id(rumor: &UnsignedEvent) -> u64 {
let mut hasher = DefaultHasher::new();
let mut pubkeys: Vec<PublicKey> = rumor.tags.public_keys().copied().collect();
let mut pubkeys: Vec<PublicKey> = rumor.tags.public_keys().collect();
pubkeys.push(rumor.pubkey);
pubkeys.sort();
pubkeys.dedup();

View File

@@ -242,13 +242,13 @@ fn extract_mentions(content: &str) -> Vec<Mention> {
fn extract_reply_ids(inner: &Tags) -> Vec<EventId> {
let mut replies_to = vec![];
for tag in inner.filter(TagKind::e()) {
for tag in inner.iter().filter(|tag| tag.kind() == "e") {
if let Some(id) = tag.content().and_then(|id| EventId::parse(id).ok()) {
replies_to.push(id);
}
}
for tag in inner.filter(TagKind::q()) {
for tag in inner.iter().filter(|tag| tag.kind() == "q") {
if let Some(id) = tag.content().and_then(|id| EventId::parse(id).ok()) {
replies_to.push(id);
}

View File

@@ -1,15 +1,17 @@
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::time::Duration;
use anyhow::{Error, anyhow};
use anyhow::Error;
use common::EventExt;
use gpui::{App, AppContext, Context, EventEmitter, SharedString, Task};
use itertools::Itertools;
use nostr_sdk::prelude::*;
use person::{Person, PersonRegistry};
use settings::{RoomConfig, SignerKind};
use state::{NostrRegistry, TIMEOUT};
use smol::lock::RwLock;
use state::{CoopSigner, Gossip, NostrRegistry, TIMEOUT};
use crate::NewMessage;
@@ -171,7 +173,8 @@ impl From<&UnsignedEvent> for Room {
let members = val.extract_public_keys();
let subject = val
.tags
.find(TagKind::Subject)
.iter()
.find(|tag| tag.kind() == "subject")
.and_then(|tag| tag.content().map(|s| s.to_owned().into()));
Room {
@@ -425,7 +428,7 @@ impl Room {
let nostr = NostrRegistry::global(cx);
// 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
let members: Vec<Person> = self
@@ -440,9 +443,7 @@ impl Room {
// Add subject tag if present
if let Some(value) = self.subject.as_ref() {
tags.push(Tag::from_standardized_without_cell(TagStandard::Subject(
value.to_string(),
)));
tags.push(Tag::custom("subject", vec![value.to_string()]));
}
// Add all reply tags
@@ -452,14 +453,13 @@ impl Room {
// Add all receiver tags
for member in members.into_iter() {
tags.push(Tag::from_standardized_without_cell(
TagStandard::PublicKey {
tags.push(
Nip01Tag::PublicKey {
public_key: member.public_key(),
relay_url: member.messaging_relay_hint(),
alias: None,
uppercase: false,
},
));
relay_hint: member.messaging_relay_hint(),
}
.to_tag(),
);
}
// Construct a direct message rumor event
@@ -474,16 +474,19 @@ impl Room {
/// Send rumor event to all members's messaging relays
pub fn send(&self, rumor: UnsignedEvent, cx: &App) -> Option<Task<Vec<SendReport>>> {
let config = self.config.clone();
let persons = PersonRegistry::global(cx);
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let gossip = nostr.read(cx).gossip();
let signer = nostr.read(cx).signer();
// 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 config = self.config.clone();
// Get all members (excluding sender)
let members: Vec<Person> = self
.members
@@ -492,12 +495,10 @@ impl Room {
.map(|member| persons.read(cx).get(member, cx))
.collect();
Some(cx.background_spawn(async move {
Some(cx.spawn(async move |_cx| {
let signer_kind = config.signer_kind();
let backup = config.backup();
let user_signer = signer.get().await;
let encryption_signer = signer.get_encryption_signer().await;
let has_encryption_signer = signer.has_encryption_signer().await;
let mut sents = 0;
let mut reports = Vec::new();
@@ -516,33 +517,21 @@ impl Room {
}
// Sender didn't set up a decoupled encryption key
if encryption_signer.is_none() {
if !has_encryption_signer {
reports.push(SendReport::new(sender.public_key()).error(USER_NO_DEKEY));
continue;
}
}
// Determine the signer to use
let signer = match signer_kind {
SignerKind::Auto => {
if announcement.is_some()
&& let Some(encryption_signer) = encryption_signer.clone()
{
// Safe to unwrap due to earlier checks
encryption_signer
} else {
user_signer.clone()
}
}
SignerKind::Encryption => {
// Safe to unwrap due to earlier checks
encryption_signer.as_ref().unwrap().clone()
}
SignerKind::User => user_signer.clone(),
let use_encryption = match signer_kind {
SignerKind::Auto => announcement.is_some() && has_encryption_signer,
SignerKind::Encryption => true,
SignerKind::User => false,
};
// Send the gift wrap event and collect the report
match send_gift_wrap(&client, &signer, &member, &rumor, signer_kind).await {
match send(&client, &gossip, &signer, &member, &rumor, use_encryption).await {
Ok(report) => {
reports.push(report);
sents += 1;
@@ -559,25 +548,13 @@ impl Room {
let public_key = sender.public_key();
// Determine the signer to use
let signer = match signer_kind {
SignerKind::Auto => {
if sender.announcement().is_some()
&& let Some(encryption_signer) = encryption_signer.clone()
{
// Safe to unwrap due to earlier checks
encryption_signer
} else {
user_signer.clone()
}
}
SignerKind::Encryption => {
// Safe to unwrap due to earlier checks
encryption_signer.as_ref().unwrap().clone()
}
SignerKind::User => user_signer.clone(),
let use_encryption = match signer_kind {
SignerKind::Auto => sender.announcement().is_some() && has_encryption_signer,
SignerKind::Encryption => true,
SignerKind::User => false,
};
match send_gift_wrap(&client, &signer, &sender, &rumor, signer_kind).await {
match send(&client, &gossip, &signer, &sender, &rumor, use_encryption).await {
Ok(report) => reports.push(report),
Err(error) => {
let report = SendReport::new(public_key).error(error.to_string());
@@ -592,22 +569,20 @@ impl Room {
}
// Helper function to send a gift-wrapped event
async fn send_gift_wrap<T>(
async fn send(
client: &Client,
signer: &T,
gossip: &Arc<RwLock<Gossip>>,
signer: &Arc<CoopSigner>,
receiver: &Person,
rumor: &UnsignedEvent,
config: &SignerKind,
) -> Result<SendReport, Error>
where
T: NostrSigner + 'static,
{
let k_tag = Tag::custom(TagKind::k(), vec!["14"]);
encryption: bool,
) -> Result<SendReport, Error> {
let k_tag = Tag::custom("k", vec!["14"]);
let mut extra_tags = vec![k_tag];
// Determine the receiver public key based on the config
let receiver = match config {
SignerKind::Auto => {
let receiver = match encryption {
true => {
if let Some(announcement) = receiver.announcement().as_ref() {
extra_tags.push(Tag::public_key(receiver.public_key()));
announcement.public_key()
@@ -615,24 +590,30 @@ where
receiver.public_key()
}
}
SignerKind::Encryption => {
if let Some(announcement) = receiver.announcement().as_ref() {
extra_tags.push(Tag::public_key(receiver.public_key()));
announcement.public_key()
} else {
return Err(anyhow!("User has no encryption announcement"));
}
}
SignerKind::User => receiver.public_key(),
false => receiver.public_key(),
};
// Construct the gift wrap event
let event = EventBuilder::gift_wrap(signer, &receiver, rumor.clone(), extra_tags).await?;
let event = match encryption {
true => {
let signer = signer.get_encryption_signer().await.unwrap();
EventBuilder::gift_wrap_async(&signer, &receiver, rumor.clone(), extra_tags).await?
}
false => {
let signer = signer.get().await;
EventBuilder::gift_wrap_async(&signer, &receiver, rumor.clone(), extra_tags).await?
}
};
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
let report = client
.send_event(&event)
.to_nip17()
.to(relays)
.ack_policy(AckPolicy::none())
.await
.map(|output| {

View File

@@ -18,7 +18,7 @@ impl EventExt for Event {
}
fn extract_public_keys(&self) -> Vec<PublicKey> {
let mut public_keys: Vec<PublicKey> = self.tags.public_keys().copied().collect();
let mut public_keys: Vec<PublicKey> = self.tags.public_keys().collect();
public_keys.push(self.pubkey);
public_keys.into_iter().unique().collect()
@@ -46,7 +46,7 @@ impl EventExt for UnsignedEvent {
}
fn extract_public_keys(&self) -> Vec<PublicKey> {
let mut public_keys: Vec<PublicKey> = self.tags.public_keys().copied().collect();
let mut public_keys: Vec<PublicKey> = self.tags.public_keys().collect();
public_keys.push(self.pubkey);
public_keys.into_iter().unique().sorted().collect()
}

View File

@@ -2,6 +2,7 @@ use std::cell::Cell;
use std::collections::HashSet;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;
use anyhow::{Context as AnyhowContext, Error, anyhow};
@@ -11,7 +12,7 @@ use gpui::{
};
use nostr_sdk::prelude::*;
use person::PersonRegistry;
use state::{Announcement, NostrRegistry, StateEvent, TIMEOUT, app_name};
use state::{Announcement, CoopSigner, NostrRegistry, StateEvent, app_name};
use theme::ActiveTheme;
use ui::avatar::Avatar;
use ui::button::Button;
@@ -34,7 +35,7 @@ impl Global for GlobalDeviceRegistry {}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum DeviceEvent {
/// A new encryption signer has been set
Set,
Set(PublicKey),
/// The device is requesting an encryption key
Requesting,
/// The device is creating a new encryption key
@@ -89,9 +90,9 @@ impl DeviceRegistry {
// Subscribe to nostr state events
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.get_announcement(cx);
this.get_announcement(public_key, cx);
};
});
@@ -110,6 +111,8 @@ impl DeviceRegistry {
fn handle_notifications(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let (tx, rx) = flume::bounded::<Event>(100);
self.tasks.push(cx.background_spawn(async move {
@@ -127,12 +130,12 @@ impl DeviceRegistry {
match event.kind {
Kind::Custom(4454) => {
if verify_author(&client, event.as_ref()).await {
if verify_author(&signer, event.as_ref()).await {
tx.send_async(event.into_owned()).await?;
}
}
Kind::Custom(4455) => {
if verify_author(&client, event.as_ref()).await {
if verify_author(&signer, event.as_ref()).await {
tx.send_async(event.into_owned()).await?;
}
}
@@ -181,18 +184,19 @@ impl DeviceRegistry {
/// Set the decoupled encryption key for the current user
fn set_signer<S>(&mut self, new: S, cx: &mut Context<Self>)
where
S: NostrSigner + 'static,
S: AsyncNostrSigner,
{
let nostr = NostrRegistry::global(cx);
let signer = nostr.read(cx).signer();
self.tasks.push(cx.spawn(async move |this, cx| {
let public_key = new.get_public_key().await?;
signer.set_encryption_signer(new).await;
// Update state
this.update(cx, |this, cx| {
this.set_initializing(false, cx);
cx.emit(DeviceEvent::Set);
cx.emit(DeviceEvent::Set(public_key));
})?;
Ok(())
@@ -203,9 +207,10 @@ impl DeviceRegistry {
pub fn backup(&self, path: PathBuf, cx: &App) -> Task<Result<(), Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
cx.background_spawn(async move {
let keys = get_keys(&client).await?;
let keys = get_keys(&client, &signer).await?;
let content = keys.secret_key().to_bech32()?;
smol::fs::write(path, &content).await?;
@@ -215,13 +220,16 @@ impl DeviceRegistry {
}
/// 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 client = nostr.read(cx).client();
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 signer = client.signer().context("Signer not found")?;
let signer = signer.get().await;
let public_key = signer.get_public_key().await?;
let urls = write_relays.await;
// Construct the filter for the device announcement event
let filter = Filter::new()
@@ -229,10 +237,15 @@ impl DeviceRegistry {
.author(public_key)
.limit(1);
let target = urls
.into_iter()
.map(|relay| (relay, vec![filter.clone()]))
.collect::<Vec<_>>();
// Stream events from user's write relays
let mut stream = client
.stream_events(filter)
.timeout(Duration::from_secs(TIMEOUT))
.stream_events(target)
.timeout(Duration::from_secs(10))
.await?;
while let Some((_url, res)) = stream.next().await {
@@ -293,19 +306,24 @@ impl DeviceRegistry {
fn create_encryption(&self, keys: Keys, cx: &App) -> Task<Result<Keys, Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let secret = keys.secret_key().to_secret_hex();
let n = keys.public_key();
cx.background_spawn(async move {
// Construct an announcement event
let builder = EventBuilder::new(Kind::Custom(10044), "").tags(vec![
Tag::custom(TagKind::custom("n"), vec![n]),
Tag::client(app_name()),
]);
// Sign the event with user's signer
let event = client.sign_event_builder(builder).await?;
let event = EventBuilder::new(Kind::Custom(10044), "")
.tags(vec![
Tag::custom("n", vec![n]),
Nip89Tag::Client {
name: app_name().to_string(),
address: None,
}
.to_tag(),
])
.sign_async(&signer.get().await)
.await?;
// Publish announcement
client
@@ -315,7 +333,7 @@ impl DeviceRegistry {
.await?;
// Save device keys to the database
set_keys(&client, &secret).await?;
set_keys(&client, &signer, &secret).await?;
Ok(keys)
})
@@ -325,13 +343,14 @@ impl DeviceRegistry {
fn set_encryption(&mut self, event: &Event, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let announcement = Announcement::from(event);
let device_pubkey = announcement.public_key();
// Get encryption key from the database and compare with the announcement
let task: Task<Result<Keys, Error>> = cx.background_spawn(async move {
let keys = get_keys(&client).await?;
let keys = get_keys(&client, &signer).await?;
// Compare the public key from the announcement with the one from the database
if keys.public_key() != device_pubkey {
@@ -362,9 +381,12 @@ impl DeviceRegistry {
let client = nostr.read(cx).client();
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 {
let public_key = signer.get_public_key().await?;
let id = SubscriptionId::new("dekey-requests");
let urls = write_relays.await;
// Construct a filter for encryption key requests
let filter = Filter::new()
@@ -372,8 +394,13 @@ impl DeviceRegistry {
.author(public_key)
.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
client.subscribe(vec![filter]).with_id(id).await?;
client.subscribe(target).with_id(id).await?;
Ok(())
}));
@@ -385,12 +412,13 @@ impl DeviceRegistry {
let client = nostr.read(cx).client();
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_pubkey = app_keys.public_key();
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
let filter = Filter::new()
.kind(Kind::Custom(4455))
@@ -403,17 +431,24 @@ impl DeviceRegistry {
Some(event) => Ok(Some(event)),
// No approval event found, construct a request event
None => {
// Construct an event for device key request
let builder = EventBuilder::new(Kind::Custom(4454), "").tags(vec![
Tag::custom(TagKind::custom("P"), vec![app_pubkey]),
Tag::client(app_name()),
]);
let write_relays = write_relays.await;
let signer = signer.get().await;
// Sign the event with user's signer
let event = client.sign_event_builder(builder).await?;
// Construct an event for device key request
let event = EventBuilder::new(Kind::Custom(4454), "")
.tags(vec![
Tag::custom("P", vec![app_pubkey]),
Nip89Tag::Client {
name: app_name().to_string(),
address: None,
}
.to_tag(),
])
.sign_async(&signer)
.await?;
// Send the event to write relays
client.send_event(&event).to_nip65().await?;
client.send_event(&event).to(write_relays).await?;
Ok(None)
}
@@ -451,8 +486,11 @@ impl DeviceRegistry {
let client = nostr.read(cx).client();
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 {
let public_key = signer.get_public_key().await?;
let urls = write_relays.await;
// Construct a filter for device key requests
let filter = Filter::new()
@@ -460,8 +498,13 @@ impl DeviceRegistry {
.author(public_key)
.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
client.subscribe(filter).await?;
client.subscribe(target).await?;
Ok(())
}));
@@ -471,17 +514,19 @@ impl DeviceRegistry {
fn extract_encryption(&mut self, event: Event, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let app_keys = nostr.read(cx).keys();
let app_signer: Arc<dyn AsyncNostrSigner> = Arc::new(app_keys);
let task: Task<Result<Keys, Error>> = cx.background_spawn(async move {
let master = event
.tags
.find(TagKind::custom("P"))
.iter()
.find(|tag| tag.kind() == "P")
.and_then(|tag| tag.content())
.and_then(|content| PublicKey::parse(content).ok())
.context("Invalid event's tags")?;
let payload = event.content.as_str();
let decrypted = app_keys.nip44_decrypt(&master, payload).await?;
let decrypted = app_signer.nip44_decrypt(&master, payload).await?;
let secret = SecretKey::from_hex(&decrypted)?;
let keys = Keys::new(secret);
@@ -510,6 +555,10 @@ impl DeviceRegistry {
fn approve(&mut self, event: &Event, window: &mut Window, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
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
let event = event.clone();
@@ -517,34 +566,40 @@ impl DeviceRegistry {
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
// Get device keys
let keys = get_keys(&client).await?;
let keys = get_keys(&client, &signer).await?;
let public_key = keys.public_key();
let secret = keys.secret_key().to_secret_hex();
let device_signer: Arc<dyn AsyncNostrSigner> = Arc::new(keys);
let write_relays = write_relays.await;
let signer = signer.get().await;
// Extract the target public key from the event tags
let target = event
.tags
.find(TagKind::custom("P"))
.iter()
.find(|tag| tag.kind() == "P")
.and_then(|tag| tag.content())
.and_then(|content| PublicKey::parse(content).ok())
.context("Target is not a valid public key")?;
// Encrypt the device keys with the user's signer
let payload = keys.nip44_encrypt(&target, &secret).await?;
let payload = device_signer.nip44_encrypt(&target, &secret).await?;
// Construct the response event
//
// P tag: the current device's public key
// p tag: the requester's public key
let builder = EventBuilder::new(Kind::Custom(4455), payload).tags(vec![
Tag::custom(TagKind::custom("P"), vec![keys.public_key().to_hex()]),
let event = EventBuilder::new(Kind::Custom(4455), payload)
.tags(vec![
Tag::custom("P", vec![public_key.to_hex()]),
Tag::public_key(target),
]);
// Sign the builder
let event = client.sign_event_builder(builder).await?;
])
.sign_async(&signer)
.await?;
// 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(())
});
@@ -689,18 +744,15 @@ impl DeviceRegistry {
struct DeviceNotification;
/// Verify the author of an event
async fn verify_author(client: &Client, event: &Event) -> bool {
if let Some(signer) = client.signer()
&& let Ok(public_key) = signer.get_public_key().await
{
async fn verify_author(signer: &Arc<CoopSigner>, event: &Event) -> bool {
if let Ok(public_key) = signer.get_public_key().await {
return public_key == event.pubkey;
}
false
}
/// Encrypt and store device keys in the local database.
async fn set_keys(client: &Client, secret: &str) -> Result<(), Error> {
let signer = client.signer().context("Signer not found")?;
async fn set_keys(client: &Client, signer: &Arc<CoopSigner>, secret: &str) -> Result<(), Error> {
let public_key = signer.get_public_key().await?;
// Encrypt the value
@@ -710,7 +762,7 @@ async fn set_keys(client: &Client, secret: &str) -> Result<(), Error> {
let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
.tag(Tag::identifier(IDENTIFIER))
.build(public_key)
.sign(&Keys::generate())
.sign_async(&Keys::generate())
.await?;
// Save the event to the database
@@ -720,8 +772,7 @@ async fn set_keys(client: &Client, secret: &str) -> Result<(), Error> {
}
/// Get device keys from the local database.
async fn get_keys(client: &Client) -> Result<Keys, Error> {
let signer = client.signer().context("Signer not found")?;
async fn get_keys(client: &Client, signer: &Arc<CoopSigner>) -> Result<Keys, Error> {
let public_key = signer.get_public_key().await?;
let filter = Filter::new()

View File

@@ -242,7 +242,7 @@ impl PersonRegistry {
/// Set messaging relays for a person
fn set_messaging_relays(&mut self, event: &Event, cx: &mut App) {
let urls: Vec<RelayUrl> = nip17::extract_relay_list(event).cloned().collect();
let urls: Vec<RelayUrl> = nip17::extract_relay_list(event).collect();
if let Some(person) = self.persons.get(&event.pubkey) {
person.update(cx, |person, cx| {

View File

@@ -193,15 +193,19 @@ impl RelayAuth {
fn auth(&self, req: &Arc<AuthRequest>, cx: &App) -> Task<Result<(), Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let req = req.clone();
// Get all pending events for the relay
let pending_events = self.get_pending_events(req.url(), cx);
cx.background_spawn(async move {
let signer = signer.get().await;
// Construct event
let builder = EventBuilder::auth(req.challenge(), req.url().clone());
let event = client.sign_event_builder(builder).await?;
let event = EventBuilder::auth(req.challenge(), req.url().clone())
.sign_async(&signer)
.await?;
// Get the event ID
let id = event.id;

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

@@ -16,14 +16,15 @@ impl From<&Event> for Announcement {
let public_key = val
.tags
.iter()
.find(|tag| tag.kind().as_str() == "n")
.find(|tag| tag.kind() == "n")
.and_then(|tag| tag.content())
.and_then(|c| PublicKey::parse(c).ok())
.unwrap_or(val.pubkey);
let client_name = val
.tags
.find(TagKind::Client)
.iter()
.find(|tag| tag.kind() == "client")
.and_then(|tag| tag.content())
.map(|c| c.to_string());

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,25 +1,27 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use anyhow::{Context as AnyhowContext, Error, anyhow};
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
@@ -130,11 +135,9 @@ impl NostrRegistry {
// Construct the nostr client
let client = ClientBuilder::default()
.signer(signer.clone())
.database(lmdb)
.gossip(NostrGossipMemory::unbounded())
.automatic_authentication(false)
.connect_timeout(Duration::from_secs(10))
.max_avg_latency(Duration::from_millis(800))
.sleep_when_idle(SleepWhenIdle::Enabled {
timeout: Duration::from_secs(600),
})
@@ -143,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);
@@ -154,6 +159,7 @@ impl NostrRegistry {
Self {
client,
gossip,
signer,
npubs,
key_dir,
@@ -167,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()
@@ -209,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
@@ -236,96 +246,139 @@ impl NostrRegistry {
}
// Connect to all added relays
client
.connect()
.and_wait(Duration::from_secs(TIMEOUT))
.await;
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(())
}));
}
/// Get the secret for a given npub.
pub fn get_secret(
&self,
public_key: PublicKey,
cx: &App,
) -> Task<Result<Arc<dyn NostrSigner>, Error>> {
/// Switch to a different account by public key
pub fn switch_account(&self, public_key: PublicKey, cx: &App) -> Result<(), Error> {
let client = self.client();
let signer = self.signer();
let npub = public_key.to_bech32().unwrap();
let key_path = self.key_dir.join(format!("{}.npub", npub));
let app_keys = self.app_keys.clone();
if let Ok(payload) = std::fs::read_to_string(key_path) {
if !payload.is_empty() {
cx.background_spawn(async move {
let decrypted = app_keys.nip44_decrypt(&public_key, &payload).await?;
let secret = SecretKey::parse(&decrypted)?;
let app_keys = self.app_keys.clone();
let app_signer: Arc<dyn NostrSigner> = Arc::new(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);
let public_key = keys.public_key();
Ok(keys.into_nostr_signer())
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));
});
});
})
} else {
self.get_secret_keyring(&npub, cx)
}
} else {
self.get_secret_keyring(&npub, cx)
}
}
/// Get the secret for a given npub in the OS credentials store.
#[deprecated = "Use get_secret instead"]
fn get_secret_keyring(
&self,
user: &str,
cx: &App,
) -> Task<Result<Arc<dyn NostrSigner>, Error>> {
let read = cx.read_credentials(user);
let app_keys = self.app_keys.clone();
cx.background_spawn(async move {
let (_, secret) = read
.await
.map_err(|_| anyhow!("Failed to get signer. Please re-import the secret key"))?
.ok_or_else(|| anyhow!("Failed to get signer. Please re-import the secret key"))?;
// Try to parse as a direct secret key first
if let Ok(secret_key) = SecretKey::from_slice(&secret) {
return Ok(Keys::new(secret_key).into_nostr_signer());
}
// Convert the secret into string
let sec = String::from_utf8(secret)
.map_err(|_| anyhow!("Failed to parse secret as UTF-8"))?;
// Try to parse as a NIP-46 URI
let uri =
NostrConnectUri::parse(&sec).map_err(|_| anyhow!("Failed to parse NIP-46 URI"))?;
.detach();
return Ok(());
} 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)?;
// Set the auth URL handler
nip46.auth_url_handler(CoopAuthUrlHandler);
Ok(nip46.into_nostr_signer())
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();
return Ok(());
}
}
Err(anyhow!("Secret not found"))
}
/// Add a new npub to the keys directory
@@ -337,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 = 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,16 +439,37 @@ impl NostrRegistry {
// Run async tasks in background
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let signer = async_keys.into_nostr_signer();
// Construct relay list event
let relay_list = default_relay_list();
let event = EventBuilder::relay_list(relay_list).sign(&signer).await?;
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?;
@@ -403,32 +477,42 @@ impl NostrRegistry {
let name = petname::petname(2, "-").unwrap_or("Cooper".to_string());
let avatar = Url::parse(&format!("https://avatar.vercel.sh/{name}")).unwrap();
let metadata = Metadata::new().display_name(&name).picture(avatar);
let event = EventBuilder::metadata(&metadata).sign(&signer).await?;
let event = EventBuilder::metadata(&metadata)
.sign_async(&async_keys)
.await?;
// Publish metadata event
client
.send_event(&event)
.to_nip65()
.to(&write_relays)
.ack_policy(AckPolicy::none())
.await?;
// Construct the default contact list
let contacts = vec![Contact::new(PublicKey::parse(COOP_PUBKEY).unwrap())];
let event = EventBuilder::contact_list(contacts).sign(&signer).await?;
let event = EventBuilder::contact_list(contacts)
.sign_async(&async_keys)
.await?;
// Publish contact list event
client
.send_event(&event)
.to_nip65()
.to(&write_relays)
.ack_policy(AckPolicy::none())
.await?;
// Construct the default messaging relay list
let relays = default_messaging_relays();
let event = EventBuilder::nip17_relay_list(relays).sign(&signer).await?;
let event = EventBuilder::nip17_relay_list(relays)
.sign_async(&async_keys)
.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?;
@@ -440,56 +524,9 @@ impl NostrRegistry {
match task.await {
Ok(_) => {
this.update(cx, |this, cx| {
this.set_signer(keys, cx);
})?;
}
Err(e) => {
this.update(cx, |_this, cx| {
if let Err(e) = this.switch_account(keys.public_key(), cx) {
cx.emit(StateEvent::error(e.to_string()));
})?;
}
};
Ok(())
}));
}
/// Set the signer for the nostr client and verify the public key
pub fn set_signer<T>(&mut self, new: T, cx: &mut Context<Self>)
where
T: NostrSigner + 'static,
{
let client = self.client();
let signer = self.signer();
// Create a task to update the signer and verify the public key
let task: Task<Result<PublicKey, Error>> = cx.background_spawn(async move {
// Update signer and unsubscribe
signer.switch(new).await;
client.unsubscribe_all().await?;
// Verify and get public key
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
log::info!("Signer's public key: {}", public_key);
Ok(public_key)
});
self.tasks.push(cx.spawn(async move |this, cx| {
match task.await {
Ok(public_key) => {
this.update(cx, |this, cx| {
// Add public key to npubs if not already present
this.npubs.update(cx, |this, cx| {
if !this.contains(&public_key) {
this.push(public_key);
cx.notify();
}
});
// Emit signer changed event
cx.emit(StateEvent::SignerSet);
})?;
}
Err(e) => {
@@ -513,7 +550,9 @@ impl NostrRegistry {
match write_secret.await {
Ok(_) => {
this.update(cx, |this, cx| {
this.set_signer(keys, cx);
if let Err(e) = this.switch_account(keys.public_key(), cx) {
cx.emit(StateEvent::error(e.to_string()));
}
})?;
}
Err(e) => {
@@ -552,7 +591,9 @@ impl NostrRegistry {
match write_secret.await {
Ok(_) => {
this.update(cx, |this, cx| {
this.set_signer(nip46, cx);
if let Err(e) = this.switch_account(public_key, cx) {
cx.emit(StateEvent::error(e.to_string()));
}
})?;
}
Err(e) => {
@@ -666,64 +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 gossip = self.gossip.clone();
let public_key = public_key.to_owned();
cx.background_spawn(async move {
// Construct a vertex request event
let builder = EventBuilder::new(Kind::Custom(5315), "").tags(vec![
Tag::custom(TagKind::custom("param"), vec!["search", &query]),
Tag::custom(TagKind::custom("param"), vec!["limit", "10"]),
]);
let event = client.sign_event_builder(builder).await?;
let urls = gossip.read().await.write_relays(&public_key);
// 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
})
}
}
@@ -757,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

@@ -7,62 +7,69 @@ use smol::lock::RwLock;
#[derive(Debug)]
pub struct CoopSigner {
/// User's signer
signer: RwLock<Arc<dyn NostrSigner>>,
signer: Arc<RwLock<Arc<dyn AsyncNostrSigner>>>,
/// User's signer public key
signer_pkey: RwLock<Option<PublicKey>>,
public_key: RwLock<Option<PublicKey>>,
/// Specific signer for encryption purposes
encryption_signer: RwLock<Option<Arc<dyn NostrSigner>>>,
encryption_signer: Arc<RwLock<Option<Arc<dyn AsyncNostrSigner>>>>,
}
impl CoopSigner {
pub fn new<T>(signer: T) -> Self
where
T: IntoNostrSigner,
T: AsyncNostrSigner,
{
Self {
signer: RwLock::new(signer.into_nostr_signer()),
signer_pkey: RwLock::new(None),
encryption_signer: RwLock::new(None),
signer: Arc::new(RwLock::new(Arc::new(signer))),
public_key: RwLock::new(None),
encryption_signer: Arc::new(RwLock::new(None)),
}
}
/// Get the current signer.
pub async fn get(&self) -> Arc<dyn NostrSigner> {
pub async fn get(&self) -> Arc<dyn AsyncNostrSigner> {
self.signer.read().await.clone()
}
/// Get the encryption signer.
pub async fn get_encryption_signer(&self) -> Option<Arc<dyn NostrSigner>> {
pub async fn get_encryption_signer(&self) -> Option<Arc<dyn AsyncNostrSigner>> {
self.encryption_signer.read().await.clone()
}
/// Check if the encryption signer is available.
pub async fn has_encryption_signer(&self) -> bool {
self.encryption_signer.read().await.is_some()
}
/// Get public key
///
/// 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.
pub async fn switch<T>(&self, new: T)
where
T: IntoNostrSigner,
T: AsyncNostrSigner,
{
let new_signer = new.into_nostr_signer();
let public_key = new_signer.get_public_key().await.ok();
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;
// Switch to the new signer
*signer = new_signer;
// Verify the public key
*public_key = new.get_public_key().await.ok();
// Update the public key
*signer_pkey = public_key;
// Switch to the new signer
*signer = Arc::new(new);
// Reset the encryption signer
*encryption_signer = None;
@@ -71,35 +78,33 @@ impl CoopSigner {
/// Set the encryption signer.
pub async fn set_encryption_signer<T>(&self, new: T)
where
T: IntoNostrSigner,
T: AsyncNostrSigner,
{
let mut encryption_signer = self.encryption_signer.write().await;
*encryption_signer = Some(new.into_nostr_signer());
*encryption_signer = Some(Arc::new(new));
}
}
impl NostrSigner for CoopSigner {
#[allow(mismatched_lifetime_syntaxes)]
fn backend(&self) -> SignerBackend {
SignerBackend::Custom(Cow::Borrowed("custom"))
}
fn get_public_key<'a>(&'a self) -> BoxedFuture<'a, Result<PublicKey, SignerError>> {
impl AsyncGetPublicKey for CoopSigner {
fn get_public_key(&self) -> BoxedFuture<'_, Result<PublicKey, SignerError>> {
Box::pin(async move { self.get().await.get_public_key().await })
}
}
fn sign_event<'a>(
&'a self,
unsigned: UnsignedEvent,
) -> BoxedFuture<'a, Result<Event, SignerError>> {
impl AsyncSignEvent for CoopSigner {
fn sign_event(&self, unsigned: UnsignedEvent) -> BoxedFuture<'_, Result<Event, SignerError>> {
Box::pin(async move { self.get().await.sign_event(unsigned).await })
}
}
impl AsyncNip04 for CoopSigner {
type Error = SignerError;
fn nip04_encrypt<'a>(
&'a self,
public_key: &'a PublicKey,
content: &'a str,
) -> BoxedFuture<'a, Result<String, SignerError>> {
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move { self.get().await.nip04_encrypt(public_key, content).await })
}
@@ -107,7 +112,7 @@ impl NostrSigner for CoopSigner {
&'a self,
public_key: &'a PublicKey,
encrypted_content: &'a str,
) -> BoxedFuture<'a, Result<String, SignerError>> {
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move {
self.get()
.await
@@ -115,12 +120,16 @@ impl NostrSigner for CoopSigner {
.await
})
}
}
impl AsyncNip44 for CoopSigner {
type Error = SignerError;
fn nip44_encrypt<'a>(
&'a self,
public_key: &'a PublicKey,
content: &'a str,
) -> BoxedFuture<'a, Result<String, SignerError>> {
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move { self.get().await.nip44_encrypt(public_key, content).await })
}
@@ -128,7 +137,13 @@ impl NostrSigner for CoopSigner {
&'a self,
public_key: &'a PublicKey,
payload: &'a str,
) -> BoxedFuture<'a, Result<String, SignerError>> {
) -> BoxedFuture<'a, Result<String, Self::Error>> {
Box::pin(async move { self.get().await.nip44_decrypt(public_key, payload).await })
}
}
impl AsyncNostrSigner for CoopSigner {
fn backend(&self) -> SignerBackend<'_> {
SignerBackend::Custom(Cow::Borrowed("custom"))
}
}

View File

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

View File

@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::time::Duration;
use anyhow::{Context as AnyhowContext, Error};
use anyhow::Error;
use common::TimestampExt;
use gpui::prelude::FluentBuilder;
use gpui::{
@@ -76,10 +76,10 @@ impl Screening {
fn check_contact(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let public_key = self.public_key;
let task: Task<Result<bool, Error>> = cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let signer_pubkey = signer.get_public_key().await?;
// Check if user is in contact list
@@ -103,10 +103,10 @@ impl Screening {
fn check_wot(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let public_key = self.public_key;
let task: Task<Result<Vec<PublicKey>, Error>> = cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let signer_pubkey = signer.get_public_key().await?;
// Check mutual contacts
@@ -222,12 +222,21 @@ impl Screening {
fn report(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let public_key = self.public_key;
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let tag = Tag::public_key_report(public_key, Report::Impersonation);
let builder = EventBuilder::report(vec![tag], "");
let event = client.sign_event_builder(builder).await?;
let signer = signer.get().await;
let tag = Nip56Tag::PublicKey {
public_key,
report: Report::Impersonation,
}
.to_tag();
let event = EventBuilder::report(vec![tag], "")
.sign_async(&signer)
.await?;
// Send the report to the public relays
client.send_event(&event).to(BOOTSTRAP_RELAYS).await?;

View File

@@ -1,7 +1,7 @@
use std::collections::HashSet;
use std::time::Duration;
use anyhow::{Context as AnyhowContext, Error};
use anyhow::Error;
use gpui::prelude::FluentBuilder;
use gpui::{
AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
@@ -81,9 +81,9 @@ impl ContactListPanel {
fn load(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let task: Task<Result<HashSet<PublicKey>, Error>> = cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
let contact_list = client.database().contacts_public_keys(public_key).await?;
@@ -156,6 +156,10 @@ impl ContactListPanel {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
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
let contacts: Vec<Contact> = self
@@ -168,12 +172,16 @@ impl ContactListPanel {
self.set_updating(true, cx);
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let write_relays = write_relays.await;
let signer = signer.get().await;
// Construct contact list event builder
let builder = EventBuilder::contact_list(contacts);
let event = client.sign_event_builder(builder).await?;
let event = EventBuilder::contact_list(contacts)
.sign_async(&signer)
.await?;
// Set contact list
client.send_event(&event).to_nip65().await?;
client.send_event(&event).to(write_relays).await?;
Ok(())
});

View File

@@ -31,8 +31,8 @@ impl GreeterPanel {
fn add_profile_panel(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
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.update(|window, cx| {
Workspace::add_panel(
@@ -47,7 +47,6 @@ impl GreeterPanel {
.detach();
}
}
}
impl Panel for GreeterPanel {
fn panel_id(&self) -> SharedString {

View File

@@ -1,7 +1,7 @@
use std::collections::HashSet;
use std::time::Duration;
use anyhow::{Context as AnyhowContext, Error, anyhow};
use anyhow::{Error, anyhow};
use gpui::prelude::FluentBuilder;
use gpui::{
AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
@@ -82,9 +82,9 @@ impl MessagingRelayPanel {
fn load(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let task: Task<Result<Vec<RelayUrl>, Error>> = cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
let filter = Filter::new()
@@ -93,7 +93,7 @@ impl MessagingRelayPanel {
.limit(1);
if let Some(event) = client.database().query(filter).await?.first_owned() {
Ok(nip17::extract_owned_relay_list(event).collect())
Ok(nip17::extract_relay_list(&event).collect())
} else {
Err(anyhow!("Not found."))
}
@@ -170,24 +170,33 @@ impl MessagingRelayPanel {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
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
let tags: Vec<Tag> = self
.relays
.iter()
.map(|relay| Tag::relay(relay.clone()))
.map(|relay| Tag::custom("relay", vec![relay.to_string()]))
.collect();
// Set updating state
self.set_updating(true, cx);
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let write_relays = write_relays.await;
let signer = signer.get().await;
// Construct nip17 event builder
let builder = EventBuilder::new(Kind::InboxRelays, "").tags(tags);
let event = client.sign_event_builder(builder).await?;
let event = EventBuilder::new(Kind::InboxRelays, "")
.tags(tags)
.sign_async(&signer)
.await?;
// Set messaging relays
client.send_event(&event).to_nip65().await?;
client.send_event(&event).to(write_relays).await?;
Ok(())
});

View File

@@ -207,15 +207,24 @@ impl ProfilePanel {
fn publish(&self, metadata: &Metadata, cx: &App) -> Task<Result<(), Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
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();
cx.background_spawn(async move {
let write_relays = write_relays.await;
let signer = signer.get().await;
// Build and sign the metadata event
let builder = EventBuilder::metadata(&metadata);
let event = client.sign_event_builder(builder).await?;
let event = EventBuilder::metadata(&metadata)
.sign_async(&signer)
.await?;
// Send event to user's relays
client.send_event(&event).await?;
client.send_event(&event).to(write_relays).await?;
Ok(())
})

View File

@@ -1,7 +1,7 @@
use std::collections::HashSet;
use std::time::Duration;
use anyhow::{Context as AnyhowContext, Error, anyhow};
use anyhow::{Error, anyhow};
use gpui::prelude::FluentBuilder;
use gpui::{
Action, AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
@@ -11,7 +11,7 @@ use gpui::{
use nostr_sdk::prelude::*;
use serde::Deserialize;
use smallvec::{SmallVec, smallvec};
use state::NostrRegistry;
use state::{BOOTSTRAP_RELAYS, NostrRegistry};
use theme::ActiveTheme;
use ui::button::{Button, ButtonVariants};
use ui::dock::{Panel, PanelEvent};
@@ -99,10 +99,10 @@ impl RelayListPanel {
fn load(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let task: Task<Result<Vec<(RelayUrl, Option<RelayMetadata>)>, Error>> = cx
.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
let filter = Filter::new()
@@ -111,7 +111,7 @@ impl RelayListPanel {
.limit(1);
if let Some(event) = client.database().query(filter).await?.first_owned() {
Ok(nip65::extract_owned_relay_list(event).collect())
Ok(nip65::extract_relay_list(&event).collect())
} else {
Err(anyhow!("Not found."))
}
@@ -206,6 +206,7 @@ impl RelayListPanel {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
// Get all relays
let relays = self.relays.clone();
@@ -214,11 +215,11 @@ impl RelayListPanel {
self.set_updating(true, cx);
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let builder = EventBuilder::relay_list(relays);
let event = client.sign_event_builder(builder).await?;
let signer = signer.get().await;
let event = EventBuilder::relay_list(relays).sign_async(&signer).await?;
// Set relay list for current user
client.send_event(&event).await?;
client.send_event(&event).to(BOOTSTRAP_RELAYS).await?;
Ok(())
});

View File

@@ -2,7 +2,7 @@ use std::collections::HashSet;
use std::ops::Range;
use std::time::Duration;
use anyhow::{Context as AnyhowContext, Error};
use anyhow::Error;
use chat::{ChatEvent, ChatRegistry, Room, RoomKind};
use common::{DebouncedDelay, TimestampExt, coop_cache};
use entry::RoomEntry;
@@ -158,9 +158,9 @@ impl Sidebar {
fn get_contact_list(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
let task: Task<Result<HashSet<PublicKey>, Error>> = cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
let contacts = client.database().contacts_public_keys(public_key).await?;

View File

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