WIP: feat: add self implement nostr sdk (nostrkit) #31
577
Cargo.lock
generated
577
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -24,7 +24,7 @@ nostr-connect = { git = "https://github.com/rust-nostr/nostr" }
|
|||||||
nostr-blossom = { 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-gossip-memory = { git = "https://github.com/rust-nostr/nostr" }
|
||||||
nostr-sdk = { 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
|
# Others
|
||||||
anyhow = "1.0.44"
|
anyhow = "1.0.44"
|
||||||
|
|||||||
@@ -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,14 +358,11 @@ impl ChatRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get all messages for the provided signer
|
/// Get all messages for the provided signer
|
||||||
fn get_messages<T>(&mut self, signer: T, cx: &mut Context<Self>)
|
fn get_messages(&mut self, public_key: &PublicKey, cx: &mut Context<Self>) {
|
||||||
where
|
let future = self.subscribe_msg(public_key, cx);
|
||||||
T: NostrSigner + 'static,
|
|
||||||
{
|
|
||||||
let task = self.subscribe_gift_wrap_events(signer, 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);
|
||||||
@@ -402,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()
|
||||||
@@ -412,16 +397,21 @@ 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?;
|
||||||
|
|
||||||
while let Some((_url, res)) = stream.next().await {
|
while let Some((_url, res)) = stream.next().await {
|
||||||
if let Ok(event) = res {
|
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);
|
return Ok(urls);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -431,17 +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<T>(&self, signer: T, cx: &App) -> Task<Result<(), Error>>
|
fn subscribe_msg(&self, public_key: &PublicKey, cx: &App) -> Task<Result<(), Error>> {
|
||||||
where
|
|
||||||
T: NostrSigner + 'static,
|
|
||||||
{
|
|
||||||
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()));
|
||||||
|
|
||||||
@@ -470,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
|
||||||
@@ -579,10 +570,9 @@ impl ChatRegistry {
|
|||||||
I: Into<Room> + 'static,
|
I: Into<Room> + 'static,
|
||||||
{
|
{
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let client = nostr.read(cx).client();
|
let signer = nostr.read(cx).signer();
|
||||||
|
|
||||||
cx.spawn(async move |this, cx| {
|
cx.spawn(async move |this, cx| {
|
||||||
let signer = client.signer()?;
|
|
||||||
let public_key = signer.get_public_key().await.ok()?;
|
let public_key = signer.get_public_key().await.ok()?;
|
||||||
let room: Room = room.into().organize(&public_key);
|
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>> {
|
fn get_rooms_from_database(&self, cx: &App) -> Task<Result<HashSet<Room>, Error>> {
|
||||||
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();
|
||||||
|
|
||||||
cx.background_spawn(async move {
|
cx.background_spawn(async move {
|
||||||
let signer = client.signer().context("Signer not found")?;
|
|
||||||
let public_key = signer.get_public_key().await?;
|
let public_key = signer.get_public_key().await?;
|
||||||
|
|
||||||
// Get contacts
|
// Get contacts
|
||||||
@@ -794,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);
|
||||||
@@ -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.
|
/// 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>
|
async fn try_unwrap_with(
|
||||||
where
|
gift_wrap: &Event,
|
||||||
T: NostrSigner + 'static,
|
signer: &Arc<dyn AsyncNostrSigner>,
|
||||||
{
|
) -> Result<UnwrappedGift, Error> {
|
||||||
// Get the sealed event
|
// Get the sealed event
|
||||||
let seal = signer
|
let seal = signer
|
||||||
.nip44_decrypt(&gift_wrap.pubkey, &gift_wrap.content)
|
.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));
|
tags.push(Tag::identifier(id));
|
||||||
|
|
||||||
// Add a reference to the rumor's author
|
// Add a reference to the rumor's author
|
||||||
tags.push(Tag::custom(
|
tags.push(Tag::custom("a", [author]));
|
||||||
TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::A)),
|
|
||||||
[author],
|
|
||||||
));
|
|
||||||
|
|
||||||
// Add a conversation id
|
// Add a conversation id
|
||||||
tags.push(Tag::custom(
|
tags.push(Tag::custom("c", [conversation.to_string()]));
|
||||||
TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::C)),
|
|
||||||
[conversation.to_string()],
|
|
||||||
));
|
|
||||||
|
|
||||||
// Add a reference to the rumor's id
|
// Add a reference to the rumor's id
|
||||||
tags.push(Tag::event(rumor_id));
|
tags.push(Tag::event(rumor_id));
|
||||||
|
|
||||||
// Add references to the rumor's participants
|
// Add references to the rumor's participants
|
||||||
for receiver in rumor.tags.public_keys().copied() {
|
for receiver in rumor.tags.public_keys() {
|
||||||
tags.push(Tag::custom(
|
tags.push(Tag::public_key(receiver));
|
||||||
TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::P)),
|
|
||||||
[receiver],
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert rumor to json
|
// Convert rumor to json
|
||||||
@@ -934,7 +913,7 @@ async fn set_rumor(client: &Client, id: EventId, rumor: &UnsignedEvent) -> Resul
|
|||||||
// Construct the event
|
// Construct the event
|
||||||
let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
|
let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
|
||||||
.tags(tags)
|
.tags(tags)
|
||||||
.sign(&Keys::generate())
|
.sign_async(&Keys::generate())
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Save the event to the database
|
// 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).
|
/// Get the conversation ID for a given rumor (message).
|
||||||
fn conversation_id(rumor: &UnsignedEvent) -> u64 {
|
fn conversation_id(rumor: &UnsignedEvent) -> u64 {
|
||||||
let mut hasher = DefaultHasher::new();
|
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.push(rumor.pubkey);
|
||||||
pubkeys.sort();
|
pubkeys.sort();
|
||||||
pubkeys.dedup();
|
pubkeys.dedup();
|
||||||
|
|||||||
@@ -242,13 +242,13 @@ fn extract_mentions(content: &str) -> Vec<Mention> {
|
|||||||
fn extract_reply_ids(inner: &Tags) -> Vec<EventId> {
|
fn extract_reply_ids(inner: &Tags) -> Vec<EventId> {
|
||||||
let mut replies_to = vec![];
|
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()) {
|
if let Some(id) = tag.content().and_then(|id| EventId::parse(id).ok()) {
|
||||||
replies_to.push(id);
|
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()) {
|
if let Some(id) = tag.content().and_then(|id| EventId::parse(id).ok()) {
|
||||||
replies_to.push(id);
|
replies_to.push(id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{Error, anyhow};
|
use anyhow::Error;
|
||||||
use common::EventExt;
|
use common::EventExt;
|
||||||
use gpui::{App, AppContext, Context, EventEmitter, SharedString, Task};
|
use gpui::{App, AppContext, Context, EventEmitter, SharedString, Task};
|
||||||
use itertools::Itertools;
|
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::{NostrRegistry, TIMEOUT};
|
use smol::lock::RwLock;
|
||||||
|
use state::{CoopSigner, Gossip, NostrRegistry, TIMEOUT};
|
||||||
|
|
||||||
use crate::NewMessage;
|
use crate::NewMessage;
|
||||||
|
|
||||||
@@ -171,7 +173,8 @@ impl From<&UnsignedEvent> for Room {
|
|||||||
let members = val.extract_public_keys();
|
let members = val.extract_public_keys();
|
||||||
let subject = val
|
let subject = val
|
||||||
.tags
|
.tags
|
||||||
.find(TagKind::Subject)
|
.iter()
|
||||||
|
.find(|tag| tag.kind() == "subject")
|
||||||
.and_then(|tag| tag.content().map(|s| s.to_owned().into()));
|
.and_then(|tag| tag.content().map(|s| s.to_owned().into()));
|
||||||
|
|
||||||
Room {
|
Room {
|
||||||
@@ -425,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
|
||||||
@@ -440,9 +443,7 @@ impl Room {
|
|||||||
|
|
||||||
// Add subject tag if present
|
// Add subject tag if present
|
||||||
if let Some(value) = self.subject.as_ref() {
|
if let Some(value) = self.subject.as_ref() {
|
||||||
tags.push(Tag::from_standardized_without_cell(TagStandard::Subject(
|
tags.push(Tag::custom("subject", vec![value.to_string()]));
|
||||||
value.to_string(),
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add all reply tags
|
// Add all reply tags
|
||||||
@@ -452,14 +453,13 @@ impl Room {
|
|||||||
|
|
||||||
// Add all receiver tags
|
// Add all receiver tags
|
||||||
for member in members.into_iter() {
|
for member in members.into_iter() {
|
||||||
tags.push(Tag::from_standardized_without_cell(
|
tags.push(
|
||||||
TagStandard::PublicKey {
|
Nip01Tag::PublicKey {
|
||||||
public_key: member.public_key(),
|
public_key: member.public_key(),
|
||||||
relay_url: member.messaging_relay_hint(),
|
relay_hint: member.messaging_relay_hint(),
|
||||||
alias: None,
|
}
|
||||||
uppercase: false,
|
.to_tag(),
|
||||||
},
|
);
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct a direct message rumor event
|
// Construct a direct message rumor event
|
||||||
@@ -474,16 +474,19 @@ impl Room {
|
|||||||
|
|
||||||
/// Send rumor event to all members's messaging relays
|
/// Send rumor event to all members's messaging relays
|
||||||
pub fn send(&self, rumor: UnsignedEvent, cx: &App) -> Option<Task<Vec<SendReport>>> {
|
pub fn send(&self, rumor: UnsignedEvent, cx: &App) -> Option<Task<Vec<SendReport>>> {
|
||||||
let config = self.config.clone();
|
|
||||||
let persons = PersonRegistry::global(cx);
|
let persons = PersonRegistry::global(cx);
|
||||||
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();
|
||||||
|
|
||||||
// Get all members (excluding sender)
|
// Get all members (excluding sender)
|
||||||
let members: Vec<Person> = self
|
let members: Vec<Person> = self
|
||||||
.members
|
.members
|
||||||
@@ -492,12 +495,10 @@ impl Room {
|
|||||||
.map(|member| persons.read(cx).get(member, cx))
|
.map(|member| persons.read(cx).get(member, cx))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Some(cx.background_spawn(async move {
|
Some(cx.spawn(async move |_cx| {
|
||||||
let signer_kind = config.signer_kind();
|
let signer_kind = config.signer_kind();
|
||||||
let backup = config.backup();
|
let backup = config.backup();
|
||||||
|
let has_encryption_signer = signer.has_encryption_signer().await;
|
||||||
let user_signer = signer.get().await;
|
|
||||||
let encryption_signer = signer.get_encryption_signer().await;
|
|
||||||
|
|
||||||
let mut sents = 0;
|
let mut sents = 0;
|
||||||
let mut reports = Vec::new();
|
let mut reports = Vec::new();
|
||||||
@@ -516,33 +517,21 @@ impl Room {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sender didn't set up a decoupled encryption key
|
// 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));
|
reports.push(SendReport::new(sender.public_key()).error(USER_NO_DEKEY));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine the signer to use
|
// Determine the signer to use
|
||||||
let signer = match signer_kind {
|
let use_encryption = match signer_kind {
|
||||||
SignerKind::Auto => {
|
SignerKind::Auto => announcement.is_some() && has_encryption_signer,
|
||||||
if announcement.is_some()
|
SignerKind::Encryption => true,
|
||||||
&& let Some(encryption_signer) = encryption_signer.clone()
|
SignerKind::User => false,
|
||||||
{
|
|
||||||
// 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(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 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, signer_kind).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;
|
||||||
@@ -559,25 +548,13 @@ impl Room {
|
|||||||
let public_key = sender.public_key();
|
let public_key = sender.public_key();
|
||||||
|
|
||||||
// Determine the signer to use
|
// Determine the signer to use
|
||||||
let signer = match signer_kind {
|
let use_encryption = match signer_kind {
|
||||||
SignerKind::Auto => {
|
SignerKind::Auto => sender.announcement().is_some() && has_encryption_signer,
|
||||||
if sender.announcement().is_some()
|
SignerKind::Encryption => true,
|
||||||
&& let Some(encryption_signer) = encryption_signer.clone()
|
SignerKind::User => false,
|
||||||
{
|
|
||||||
// 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(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
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),
|
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());
|
||||||
@@ -592,22 +569,20 @@ impl Room {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to send a gift-wrapped event
|
// Helper function to send a gift-wrapped event
|
||||||
async fn send_gift_wrap<T>(
|
async fn send(
|
||||||
client: &Client,
|
client: &Client,
|
||||||
signer: &T,
|
gossip: &Arc<RwLock<Gossip>>,
|
||||||
|
signer: &Arc<CoopSigner>,
|
||||||
receiver: &Person,
|
receiver: &Person,
|
||||||
rumor: &UnsignedEvent,
|
rumor: &UnsignedEvent,
|
||||||
config: &SignerKind,
|
encryption: bool,
|
||||||
) -> Result<SendReport, Error>
|
) -> Result<SendReport, Error> {
|
||||||
where
|
let k_tag = Tag::custom("k", vec!["14"]);
|
||||||
T: NostrSigner + 'static,
|
|
||||||
{
|
|
||||||
let k_tag = Tag::custom(TagKind::k(), vec!["14"]);
|
|
||||||
let mut extra_tags = vec![k_tag];
|
let mut extra_tags = vec![k_tag];
|
||||||
|
|
||||||
// Determine the receiver public key based on the config
|
// Determine the receiver public key based on the config
|
||||||
let receiver = match config {
|
let receiver = match encryption {
|
||||||
SignerKind::Auto => {
|
true => {
|
||||||
if let Some(announcement) = receiver.announcement().as_ref() {
|
if let Some(announcement) = receiver.announcement().as_ref() {
|
||||||
extra_tags.push(Tag::public_key(receiver.public_key()));
|
extra_tags.push(Tag::public_key(receiver.public_key()));
|
||||||
announcement.public_key()
|
announcement.public_key()
|
||||||
@@ -615,24 +590,30 @@ where
|
|||||||
receiver.public_key()
|
receiver.public_key()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SignerKind::Encryption => {
|
false => receiver.public_key(),
|
||||||
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(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Construct the gift wrap event
|
// 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
|
// 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| {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ impl EventExt for Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn extract_public_keys(&self) -> Vec<PublicKey> {
|
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.push(self.pubkey);
|
||||||
|
|
||||||
public_keys.into_iter().unique().collect()
|
public_keys.into_iter().unique().collect()
|
||||||
@@ -46,7 +46,7 @@ impl EventExt for UnsignedEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn extract_public_keys(&self) -> Vec<PublicKey> {
|
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.push(self.pubkey);
|
||||||
public_keys.into_iter().unique().sorted().collect()
|
public_keys.into_iter().unique().sorted().collect()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use std::cell::Cell;
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{Context as AnyhowContext, Error, anyhow};
|
use anyhow::{Context as AnyhowContext, Error, anyhow};
|
||||||
@@ -11,7 +12,7 @@ use gpui::{
|
|||||||
};
|
};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use person::PersonRegistry;
|
use person::PersonRegistry;
|
||||||
use state::{Announcement, 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;
|
||||||
@@ -34,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
|
||||||
@@ -89,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);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -110,6 +111,8 @@ impl DeviceRegistry {
|
|||||||
fn handle_notifications(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
fn handle_notifications(&mut self, window: &mut Window, 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 (tx, rx) = flume::bounded::<Event>(100);
|
let (tx, rx) = flume::bounded::<Event>(100);
|
||||||
|
|
||||||
self.tasks.push(cx.background_spawn(async move {
|
self.tasks.push(cx.background_spawn(async move {
|
||||||
@@ -127,12 +130,12 @@ impl DeviceRegistry {
|
|||||||
|
|
||||||
match event.kind {
|
match event.kind {
|
||||||
Kind::Custom(4454) => {
|
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?;
|
tx.send_async(event.into_owned()).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Kind::Custom(4455) => {
|
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?;
|
tx.send_async(event.into_owned()).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,18 +184,19 @@ impl DeviceRegistry {
|
|||||||
/// Set the decoupled encryption key for the current user
|
/// Set the decoupled encryption key for the current user
|
||||||
fn set_signer<S>(&mut self, new: S, cx: &mut Context<Self>)
|
fn set_signer<S>(&mut self, new: S, cx: &mut Context<Self>)
|
||||||
where
|
where
|
||||||
S: NostrSigner + 'static,
|
S: AsyncNostrSigner,
|
||||||
{
|
{
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
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(())
|
||||||
@@ -203,9 +207,10 @@ impl DeviceRegistry {
|
|||||||
pub fn backup(&self, path: PathBuf, cx: &App) -> Task<Result<(), Error>> {
|
pub fn backup(&self, path: PathBuf, 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 signer = nostr.read(cx).signer();
|
||||||
|
|
||||||
cx.background_spawn(async move {
|
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()?;
|
let content = keys.secret_key().to_bech32()?;
|
||||||
|
|
||||||
smol::fs::write(path, &content).await?;
|
smol::fs::write(path, &content).await?;
|
||||||
@@ -215,13 +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 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 = client.signer().context("Signer not found")?;
|
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()
|
||||||
@@ -229,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 {
|
||||||
@@ -293,19 +306,24 @@ impl DeviceRegistry {
|
|||||||
fn create_encryption(&self, keys: Keys, cx: &App) -> Task<Result<Keys, Error>> {
|
fn create_encryption(&self, keys: Keys, cx: &App) -> Task<Result<Keys, Error>> {
|
||||||
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 secret = keys.secret_key().to_secret_hex();
|
let secret = keys.secret_key().to_secret_hex();
|
||||||
let n = keys.public_key();
|
let n = keys.public_key();
|
||||||
|
|
||||||
cx.background_spawn(async move {
|
cx.background_spawn(async move {
|
||||||
// Construct an announcement event
|
// Construct an announcement event
|
||||||
let builder = EventBuilder::new(Kind::Custom(10044), "").tags(vec![
|
let event = EventBuilder::new(Kind::Custom(10044), "")
|
||||||
Tag::custom(TagKind::custom("n"), vec![n]),
|
.tags(vec![
|
||||||
Tag::client(app_name()),
|
Tag::custom("n", vec![n]),
|
||||||
]);
|
Nip89Tag::Client {
|
||||||
|
name: app_name().to_string(),
|
||||||
// Sign the event with user's signer
|
address: None,
|
||||||
let event = client.sign_event_builder(builder).await?;
|
}
|
||||||
|
.to_tag(),
|
||||||
|
])
|
||||||
|
.sign_async(&signer.get().await)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Publish announcement
|
// Publish announcement
|
||||||
client
|
client
|
||||||
@@ -315,7 +333,7 @@ impl DeviceRegistry {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Save device keys to the database
|
// Save device keys to the database
|
||||||
set_keys(&client, &secret).await?;
|
set_keys(&client, &signer, &secret).await?;
|
||||||
|
|
||||||
Ok(keys)
|
Ok(keys)
|
||||||
})
|
})
|
||||||
@@ -325,13 +343,14 @@ impl DeviceRegistry {
|
|||||||
fn set_encryption(&mut self, event: &Event, cx: &mut Context<Self>) {
|
fn set_encryption(&mut self, event: &Event, 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 announcement = Announcement::from(event);
|
let announcement = Announcement::from(event);
|
||||||
let device_pubkey = announcement.public_key();
|
let device_pubkey = announcement.public_key();
|
||||||
|
|
||||||
// Get encryption key from the database and compare with the announcement
|
// Get encryption key from the database and compare with the announcement
|
||||||
let task: Task<Result<Keys, Error>> = cx.background_spawn(async move {
|
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
|
// Compare the public key from the announcement with the one from the database
|
||||||
if keys.public_key() != device_pubkey {
|
if keys.public_key() != device_pubkey {
|
||||||
@@ -362,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()
|
||||||
@@ -372,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(())
|
||||||
}));
|
}));
|
||||||
@@ -385,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))
|
||||||
@@ -403,17 +431,24 @@ 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 => {
|
||||||
// Construct an event for device key request
|
let write_relays = write_relays.await;
|
||||||
let builder = EventBuilder::new(Kind::Custom(4454), "").tags(vec![
|
let signer = signer.get().await;
|
||||||
Tag::custom(TagKind::custom("P"), vec![app_pubkey]),
|
|
||||||
Tag::client(app_name()),
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Sign the event with user's signer
|
// Construct an event for device key request
|
||||||
let event = client.sign_event_builder(builder).await?;
|
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
|
// 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)
|
||||||
}
|
}
|
||||||
@@ -451,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()
|
||||||
@@ -460,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(())
|
||||||
}));
|
}));
|
||||||
@@ -471,17 +514,19 @@ impl DeviceRegistry {
|
|||||||
fn extract_encryption(&mut self, event: Event, cx: &mut Context<Self>) {
|
fn extract_encryption(&mut self, event: Event, cx: &mut Context<Self>) {
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let app_keys = nostr.read(cx).keys();
|
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 task: Task<Result<Keys, Error>> = cx.background_spawn(async move {
|
||||||
let master = event
|
let master = event
|
||||||
.tags
|
.tags
|
||||||
.find(TagKind::custom("P"))
|
.iter()
|
||||||
|
.find(|tag| tag.kind() == "P")
|
||||||
.and_then(|tag| tag.content())
|
.and_then(|tag| tag.content())
|
||||||
.and_then(|content| PublicKey::parse(content).ok())
|
.and_then(|content| PublicKey::parse(content).ok())
|
||||||
.context("Invalid event's tags")?;
|
.context("Invalid event's tags")?;
|
||||||
|
|
||||||
let payload = event.content.as_str();
|
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 secret = SecretKey::from_hex(&decrypted)?;
|
||||||
let keys = Keys::new(secret);
|
let keys = Keys::new(secret);
|
||||||
@@ -510,6 +555,10 @@ impl DeviceRegistry {
|
|||||||
fn approve(&mut self, event: &Event, window: &mut Window, cx: &mut Context<Self>) {
|
fn approve(&mut self, event: &Event, window: &mut Window, 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_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();
|
||||||
@@ -517,34 +566,40 @@ impl DeviceRegistry {
|
|||||||
|
|
||||||
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
||||||
// Get device keys
|
// 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 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
|
// Extract the target public key from the event tags
|
||||||
let target = event
|
let target = event
|
||||||
.tags
|
.tags
|
||||||
.find(TagKind::custom("P"))
|
.iter()
|
||||||
|
.find(|tag| tag.kind() == "P")
|
||||||
.and_then(|tag| tag.content())
|
.and_then(|tag| tag.content())
|
||||||
.and_then(|content| PublicKey::parse(content).ok())
|
.and_then(|content| PublicKey::parse(content).ok())
|
||||||
.context("Target is not a valid public key")?;
|
.context("Target is not a valid public key")?;
|
||||||
|
|
||||||
// Encrypt the device keys with the user's signer
|
// 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
|
// Construct the response event
|
||||||
//
|
//
|
||||||
// P tag: the current device's public key
|
// P tag: the current device's public key
|
||||||
// p tag: the requester's public key
|
// p tag: the requester's public key
|
||||||
let builder = EventBuilder::new(Kind::Custom(4455), payload).tags(vec![
|
let event = EventBuilder::new(Kind::Custom(4455), payload)
|
||||||
Tag::custom(TagKind::custom("P"), vec![keys.public_key().to_hex()]),
|
.tags(vec![
|
||||||
|
Tag::custom("P", vec![public_key.to_hex()]),
|
||||||
Tag::public_key(target),
|
Tag::public_key(target),
|
||||||
]);
|
])
|
||||||
|
.sign_async(&signer)
|
||||||
// Sign the builder
|
.await?;
|
||||||
let event = client.sign_event_builder(builder).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(())
|
||||||
});
|
});
|
||||||
@@ -689,18 +744,15 @@ impl DeviceRegistry {
|
|||||||
struct DeviceNotification;
|
struct DeviceNotification;
|
||||||
|
|
||||||
/// Verify the author of an event
|
/// Verify the author of an event
|
||||||
async fn verify_author(client: &Client, event: &Event) -> bool {
|
async fn verify_author(signer: &Arc<CoopSigner>, event: &Event) -> bool {
|
||||||
if let Some(signer) = client.signer()
|
if let Ok(public_key) = signer.get_public_key().await {
|
||||||
&& let Ok(public_key) = signer.get_public_key().await
|
|
||||||
{
|
|
||||||
return public_key == event.pubkey;
|
return public_key == event.pubkey;
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encrypt and store device keys in the local database.
|
/// Encrypt and store device keys in the local database.
|
||||||
async fn set_keys(client: &Client, secret: &str) -> Result<(), Error> {
|
async fn set_keys(client: &Client, signer: &Arc<CoopSigner>, secret: &str) -> Result<(), Error> {
|
||||||
let signer = client.signer().context("Signer not found")?;
|
|
||||||
let public_key = signer.get_public_key().await?;
|
let public_key = signer.get_public_key().await?;
|
||||||
|
|
||||||
// Encrypt the value
|
// Encrypt the value
|
||||||
@@ -710,7 +762,7 @@ async fn set_keys(client: &Client, secret: &str) -> Result<(), Error> {
|
|||||||
let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
|
let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
|
||||||
.tag(Tag::identifier(IDENTIFIER))
|
.tag(Tag::identifier(IDENTIFIER))
|
||||||
.build(public_key)
|
.build(public_key)
|
||||||
.sign(&Keys::generate())
|
.sign_async(&Keys::generate())
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Save the event to the database
|
// 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.
|
/// Get device keys from the local database.
|
||||||
async fn get_keys(client: &Client) -> Result<Keys, Error> {
|
async fn get_keys(client: &Client, signer: &Arc<CoopSigner>) -> Result<Keys, Error> {
|
||||||
let signer = client.signer().context("Signer not found")?;
|
|
||||||
let public_key = signer.get_public_key().await?;
|
let public_key = signer.get_public_key().await?;
|
||||||
|
|
||||||
let filter = Filter::new()
|
let filter = Filter::new()
|
||||||
|
|||||||
@@ -242,7 +242,7 @@ impl PersonRegistry {
|
|||||||
|
|
||||||
/// Set messaging relays for a person
|
/// Set messaging relays for a person
|
||||||
fn set_messaging_relays(&mut self, event: &Event, cx: &mut App) {
|
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) {
|
if let Some(person) = self.persons.get(&event.pubkey) {
|
||||||
person.update(cx, |person, cx| {
|
person.update(cx, |person, cx| {
|
||||||
|
|||||||
@@ -193,15 +193,19 @@ impl RelayAuth {
|
|||||||
fn auth(&self, req: &Arc<AuthRequest>, cx: &App) -> Task<Result<(), Error>> {
|
fn auth(&self, req: &Arc<AuthRequest>, 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 signer = nostr.read(cx).signer();
|
||||||
let req = req.clone();
|
let req = req.clone();
|
||||||
|
|
||||||
// Get all pending events for the relay
|
// Get all pending events for the relay
|
||||||
let pending_events = self.get_pending_events(req.url(), cx);
|
let pending_events = self.get_pending_events(req.url(), cx);
|
||||||
|
|
||||||
cx.background_spawn(async move {
|
cx.background_spawn(async move {
|
||||||
|
let signer = signer.get().await;
|
||||||
|
|
||||||
// Construct event
|
// Construct event
|
||||||
let builder = EventBuilder::auth(req.challenge(), req.url().clone());
|
let event = EventBuilder::auth(req.challenge(), req.url().clone())
|
||||||
let event = client.sign_event_builder(builder).await?;
|
.sign_async(&signer)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Get the event ID
|
// Get the event ID
|
||||||
let id = event.id;
|
let id = event.id;
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -16,14 +16,15 @@ impl From<&Event> for Announcement {
|
|||||||
let public_key = val
|
let public_key = val
|
||||||
.tags
|
.tags
|
||||||
.iter()
|
.iter()
|
||||||
.find(|tag| tag.kind().as_str() == "n")
|
.find(|tag| tag.kind() == "n")
|
||||||
.and_then(|tag| tag.content())
|
.and_then(|tag| tag.content())
|
||||||
.and_then(|c| PublicKey::parse(c).ok())
|
.and_then(|c| PublicKey::parse(c).ok())
|
||||||
.unwrap_or(val.pubkey);
|
.unwrap_or(val.pubkey);
|
||||||
|
|
||||||
let client_name = val
|
let client_name = val
|
||||||
.tags
|
.tags
|
||||||
.find(TagKind::Client)
|
.iter()
|
||||||
|
.find(|tag| tag.kind() == "client")
|
||||||
.and_then(|tag| tag.content())
|
.and_then(|tag| tag.content())
|
||||||
.map(|c| c.to_string());
|
.map(|c| c.to_string());
|
||||||
|
|
||||||
|
|||||||
73
crates/state/src/gossip.rs
Normal file
73
crates/state/src/gossip.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,25 +1,27 @@
|
|||||||
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;
|
||||||
|
|
||||||
use anyhow::{Context as AnyhowContext, Error, anyhow};
|
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
|
||||||
@@ -130,11 +135,9 @@ impl NostrRegistry {
|
|||||||
|
|
||||||
// Construct the nostr client
|
// Construct the nostr client
|
||||||
let client = ClientBuilder::default()
|
let client = ClientBuilder::default()
|
||||||
.signer(signer.clone())
|
|
||||||
.database(lmdb)
|
.database(lmdb)
|
||||||
.gossip(NostrGossipMemory::unbounded())
|
|
||||||
.automatic_authentication(false)
|
|
||||||
.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),
|
||||||
})
|
})
|
||||||
@@ -143,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);
|
||||||
@@ -154,6 +159,7 @@ impl NostrRegistry {
|
|||||||
|
|
||||||
Self {
|
Self {
|
||||||
client,
|
client,
|
||||||
|
gossip,
|
||||||
signer,
|
signer,
|
||||||
npubs,
|
npubs,
|
||||||
key_dir,
|
key_dir,
|
||||||
@@ -167,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()
|
||||||
@@ -209,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
|
||||||
@@ -236,96 +246,139 @@ impl NostrRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connect to all added relays
|
// Connect to all added relays
|
||||||
client
|
client.connect().await;
|
||||||
.connect()
|
|
||||||
.and_wait(Duration::from_secs(TIMEOUT))
|
|
||||||
.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(())
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the secret for a given npub.
|
/// Switch to a different account by public key
|
||||||
pub fn get_secret(
|
pub fn switch_account(&self, public_key: PublicKey, cx: &App) -> Result<(), Error> {
|
||||||
&self,
|
let client = self.client();
|
||||||
public_key: PublicKey,
|
let signer = self.signer();
|
||||||
cx: &App,
|
|
||||||
) -> Task<Result<Arc<dyn NostrSigner>, 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 = self.app_keys.clone();
|
|
||||||
|
|
||||||
if let Ok(payload) = std::fs::read_to_string(key_path) {
|
let app_keys = self.app_keys.clone();
|
||||||
if !payload.is_empty() {
|
let app_signer: Arc<dyn NostrSigner> = Arc::new(app_keys.clone());
|
||||||
cx.background_spawn(async move {
|
|
||||||
let decrypted = app_keys.nip44_decrypt(&public_key, &payload).await?;
|
if let Ok(payload) = std::fs::read_to_string(key_path)
|
||||||
let secret = SecretKey::parse(&decrypted)?;
|
&& !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 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 {
|
.detach();
|
||||||
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"))?;
|
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
} else if let Ok(uri) = NostrConnectUri::parse(decrypted) {
|
||||||
let timeout = Duration::from_secs(NOSTR_CONNECT_TIMEOUT);
|
let timeout = Duration::from_secs(NOSTR_CONNECT_TIMEOUT);
|
||||||
let mut nip46 = NostrConnect::new(uri, app_keys, timeout, None)?;
|
let mut nip46 = NostrConnect::new(uri, app_keys, timeout, None)?;
|
||||||
|
|
||||||
// Set the auth URL handler
|
|
||||||
nip46.auth_url_handler(CoopAuthUrlHandler);
|
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
|
/// Add a new npub to the keys directory
|
||||||
@@ -337,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 = 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,16 +439,37 @@ impl NostrRegistry {
|
|||||||
|
|
||||||
// Run async tasks in background
|
// Run async tasks in background
|
||||||
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
||||||
let signer = async_keys.into_nostr_signer();
|
|
||||||
|
|
||||||
// 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).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
|
// 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?;
|
||||||
|
|
||||||
@@ -403,32 +477,42 @@ impl NostrRegistry {
|
|||||||
let name = petname::petname(2, "-").unwrap_or("Cooper".to_string());
|
let name = petname::petname(2, "-").unwrap_or("Cooper".to_string());
|
||||||
let avatar = Url::parse(&format!("https://avatar.vercel.sh/{name}")).unwrap();
|
let avatar = Url::parse(&format!("https://avatar.vercel.sh/{name}")).unwrap();
|
||||||
let metadata = Metadata::new().display_name(&name).picture(avatar);
|
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
|
// 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?;
|
||||||
|
|
||||||
// Construct the default contact list
|
// Construct the default contact list
|
||||||
let contacts = vec![Contact::new(PublicKey::parse(COOP_PUBKEY).unwrap())];
|
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
|
// 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?;
|
||||||
|
|
||||||
// Construct the default messaging relay list
|
// Construct the default messaging relay list
|
||||||
let relays = default_messaging_relays();
|
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
|
// 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?;
|
||||||
@@ -440,56 +524,9 @@ impl NostrRegistry {
|
|||||||
match task.await {
|
match task.await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
this.update(cx, |this, cx| {
|
this.update(cx, |this, cx| {
|
||||||
this.set_signer(keys, cx);
|
if let Err(e) = this.switch_account(keys.public_key(), cx) {
|
||||||
})?;
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
this.update(cx, |_this, cx| {
|
|
||||||
cx.emit(StateEvent::error(e.to_string()));
|
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) => {
|
Err(e) => {
|
||||||
@@ -513,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.set_signer(keys, cx);
|
if let Err(e) = this.switch_account(keys.public_key(), cx) {
|
||||||
|
cx.emit(StateEvent::error(e.to_string()));
|
||||||
|
}
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -552,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.set_signer(nip46, cx);
|
if let Err(e) = this.switch_account(public_key, cx) {
|
||||||
|
cx.emit(StateEvent::error(e.to_string()));
|
||||||
|
}
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -666,64 +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 public_key = public_key.to_owned();
|
||||||
|
|
||||||
cx.background_spawn(async move {
|
cx.background_spawn(async move {
|
||||||
// Construct a vertex request event
|
let urls = gossip.read().await.write_relays(&public_key);
|
||||||
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?;
|
|
||||||
|
|
||||||
// Send the event to vertex relays
|
// Ensure the client is connected to each relay
|
||||||
let output = client.send_event(&event).to(WOT_RELAYS).await?;
|
for url in urls.iter() {
|
||||||
|
client
|
||||||
// Construct a filter to get the response or error from vertex
|
.add_relay(url)
|
||||||
let filter = Filter::new()
|
.capabilities(RelayCapabilities::WRITE)
|
||||||
.kinds(vec![Kind::Custom(6315), Kind::Custom(7000)])
|
.and_connect()
|
||||||
.event(output.id().to_owned());
|
.await
|
||||||
|
.ok();
|
||||||
// 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
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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>)> {
|
fn default_relay_list() -> Vec<(RelayUrl, Option<RelayMetadata>)> {
|
||||||
vec![
|
vec![
|
||||||
(
|
(
|
||||||
|
|||||||
@@ -7,62 +7,69 @@ use smol::lock::RwLock;
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct CoopSigner {
|
pub struct CoopSigner {
|
||||||
/// User's signer
|
signer: Arc<RwLock<Arc<dyn AsyncNostrSigner>>>,
|
||||||
signer: RwLock<Arc<dyn NostrSigner>>,
|
|
||||||
|
|
||||||
/// User's signer public key
|
/// User's signer public key
|
||||||
signer_pkey: RwLock<Option<PublicKey>>,
|
public_key: RwLock<Option<PublicKey>>,
|
||||||
|
|
||||||
/// Specific signer for encryption purposes
|
/// Specific signer for encryption purposes
|
||||||
encryption_signer: RwLock<Option<Arc<dyn NostrSigner>>>,
|
encryption_signer: Arc<RwLock<Option<Arc<dyn AsyncNostrSigner>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CoopSigner {
|
impl CoopSigner {
|
||||||
pub fn new<T>(signer: T) -> Self
|
pub fn new<T>(signer: T) -> Self
|
||||||
where
|
where
|
||||||
T: IntoNostrSigner,
|
T: AsyncNostrSigner,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
signer: RwLock::new(signer.into_nostr_signer()),
|
signer: Arc::new(RwLock::new(Arc::new(signer))),
|
||||||
signer_pkey: RwLock::new(None),
|
public_key: RwLock::new(None),
|
||||||
encryption_signer: RwLock::new(None),
|
encryption_signer: Arc::new(RwLock::new(None)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the current signer.
|
/// 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()
|
self.signer.read().await.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the encryption signer.
|
/// 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()
|
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
|
/// Get public key
|
||||||
///
|
///
|
||||||
/// 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.
|
||||||
pub async fn switch<T>(&self, new: T)
|
pub async fn switch<T>(&self, new: T)
|
||||||
where
|
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 = 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;
|
||||||
|
|
||||||
// Switch to the new signer
|
// Verify the public key
|
||||||
*signer = new_signer;
|
*public_key = new.get_public_key().await.ok();
|
||||||
|
|
||||||
// Update the public key
|
// Switch to the new signer
|
||||||
*signer_pkey = public_key;
|
*signer = Arc::new(new);
|
||||||
|
|
||||||
// Reset the encryption signer
|
// Reset the encryption signer
|
||||||
*encryption_signer = None;
|
*encryption_signer = None;
|
||||||
@@ -71,35 +78,33 @@ impl CoopSigner {
|
|||||||
/// Set the encryption signer.
|
/// Set the encryption signer.
|
||||||
pub async fn set_encryption_signer<T>(&self, new: T)
|
pub async fn set_encryption_signer<T>(&self, new: T)
|
||||||
where
|
where
|
||||||
T: IntoNostrSigner,
|
T: AsyncNostrSigner,
|
||||||
{
|
{
|
||||||
let mut encryption_signer = self.encryption_signer.write().await;
|
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 {
|
impl AsyncGetPublicKey for CoopSigner {
|
||||||
#[allow(mismatched_lifetime_syntaxes)]
|
fn get_public_key(&self) -> BoxedFuture<'_, Result<PublicKey, SignerError>> {
|
||||||
fn backend(&self) -> SignerBackend {
|
|
||||||
SignerBackend::Custom(Cow::Borrowed("custom"))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_public_key<'a>(&'a self) -> BoxedFuture<'a, Result<PublicKey, SignerError>> {
|
|
||||||
Box::pin(async move { self.get().await.get_public_key().await })
|
Box::pin(async move { self.get().await.get_public_key().await })
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn sign_event<'a>(
|
impl AsyncSignEvent for CoopSigner {
|
||||||
&'a self,
|
fn sign_event(&self, unsigned: UnsignedEvent) -> BoxedFuture<'_, Result<Event, SignerError>> {
|
||||||
unsigned: UnsignedEvent,
|
|
||||||
) -> BoxedFuture<'a, Result<Event, SignerError>> {
|
|
||||||
Box::pin(async move { self.get().await.sign_event(unsigned).await })
|
Box::pin(async move { self.get().await.sign_event(unsigned).await })
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsyncNip04 for CoopSigner {
|
||||||
|
type Error = SignerError;
|
||||||
|
|
||||||
fn nip04_encrypt<'a>(
|
fn nip04_encrypt<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
public_key: &'a PublicKey,
|
public_key: &'a PublicKey,
|
||||||
content: &'a str,
|
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 })
|
Box::pin(async move { self.get().await.nip04_encrypt(public_key, content).await })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +112,7 @@ impl NostrSigner for CoopSigner {
|
|||||||
&'a self,
|
&'a self,
|
||||||
public_key: &'a PublicKey,
|
public_key: &'a PublicKey,
|
||||||
encrypted_content: &'a str,
|
encrypted_content: &'a str,
|
||||||
) -> BoxedFuture<'a, Result<String, SignerError>> {
|
) -> BoxedFuture<'a, Result<String, Self::Error>> {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
self.get()
|
self.get()
|
||||||
.await
|
.await
|
||||||
@@ -115,12 +120,16 @@ impl NostrSigner for CoopSigner {
|
|||||||
.await
|
.await
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsyncNip44 for CoopSigner {
|
||||||
|
type Error = SignerError;
|
||||||
|
|
||||||
fn nip44_encrypt<'a>(
|
fn nip44_encrypt<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
public_key: &'a PublicKey,
|
public_key: &'a PublicKey,
|
||||||
content: &'a str,
|
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 })
|
Box::pin(async move { self.get().await.nip44_encrypt(public_key, content).await })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,7 +137,13 @@ impl NostrSigner for CoopSigner {
|
|||||||
&'a self,
|
&'a self,
|
||||||
public_key: &'a PublicKey,
|
public_key: &'a PublicKey,
|
||||||
payload: &'a str,
|
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 })
|
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"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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,28 +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);
|
|
||||||
let task = nostr.read(cx).get_secret(public_key, 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);
|
||||||
|
|
||||||
self.tasks.push(cx.spawn_in(window, async move |this, cx| {
|
let nostr = NostrRegistry::global(cx);
|
||||||
match task.await {
|
if let Err(e) = nostr.read(cx).switch_account(public_key, cx) {
|
||||||
Ok(signer) => {
|
self.set_error(e.to_string(), cx);
|
||||||
nostr.update(cx, |this, cx| {
|
|
||||||
this.set_signer(signer, 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>) {
|
fn remove(&mut self, public_key: PublicKey, cx: &mut Context<Self>) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{Context as AnyhowContext, Error};
|
use anyhow::Error;
|
||||||
use common::TimestampExt;
|
use common::TimestampExt;
|
||||||
use gpui::prelude::FluentBuilder;
|
use gpui::prelude::FluentBuilder;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
@@ -76,10 +76,10 @@ impl Screening {
|
|||||||
fn check_contact(&mut self, cx: &mut Context<Self>) {
|
fn check_contact(&mut self, 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 public_key = self.public_key;
|
let public_key = self.public_key;
|
||||||
|
|
||||||
let task: Task<Result<bool, Error>> = cx.background_spawn(async move {
|
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?;
|
let signer_pubkey = signer.get_public_key().await?;
|
||||||
|
|
||||||
// Check if user is in contact list
|
// Check if user is in contact list
|
||||||
@@ -103,10 +103,10 @@ impl Screening {
|
|||||||
fn check_wot(&mut self, cx: &mut Context<Self>) {
|
fn check_wot(&mut self, 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 public_key = self.public_key;
|
let public_key = self.public_key;
|
||||||
|
|
||||||
let task: Task<Result<Vec<PublicKey>, Error>> = cx.background_spawn(async move {
|
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?;
|
let signer_pubkey = signer.get_public_key().await?;
|
||||||
|
|
||||||
// Check mutual contacts
|
// Check mutual contacts
|
||||||
@@ -222,12 +222,21 @@ impl Screening {
|
|||||||
fn report(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
fn report(&mut self, window: &mut Window, 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 public_key = self.public_key;
|
let public_key = self.public_key;
|
||||||
|
|
||||||
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
||||||
let tag = Tag::public_key_report(public_key, Report::Impersonation);
|
let signer = signer.get().await;
|
||||||
let builder = EventBuilder::report(vec![tag], "");
|
|
||||||
let event = client.sign_event_builder(builder).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
|
// Send the report to the public relays
|
||||||
client.send_event(&event).to(BOOTSTRAP_RELAYS).await?;
|
client.send_event(&event).to(BOOTSTRAP_RELAYS).await?;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{Context as AnyhowContext, Error};
|
use anyhow::Error;
|
||||||
use gpui::prelude::FluentBuilder;
|
use gpui::prelude::FluentBuilder;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
|
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>) {
|
fn load(&mut self, window: &mut Window, 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 task: Task<Result<HashSet<PublicKey>, Error>> = cx.background_spawn(async move {
|
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 public_key = signer.get_public_key().await?;
|
||||||
let contact_list = client.database().contacts_public_keys(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 nostr = NostrRegistry::global(cx);
|
||||||
let client = nostr.read(cx).client();
|
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
|
// Get contacts
|
||||||
let contacts: Vec<Contact> = self
|
let contacts: Vec<Contact> = self
|
||||||
@@ -168,12 +172,16 @@ 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;
|
||||||
|
|
||||||
// Construct contact list event builder
|
// Construct contact list event builder
|
||||||
let builder = EventBuilder::contact_list(contacts);
|
let event = EventBuilder::contact_list(contacts)
|
||||||
let event = client.sign_event_builder(builder).await?;
|
.sign_async(&signer)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Set contact list
|
// Set contact list
|
||||||
client.send_event(&event).to_nip65().await?;
|
client.send_event(&event).to(write_relays).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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(
|
||||||
@@ -47,7 +47,6 @@ impl GreeterPanel {
|
|||||||
.detach();
|
.detach();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Panel for GreeterPanel {
|
impl Panel for GreeterPanel {
|
||||||
fn panel_id(&self) -> SharedString {
|
fn panel_id(&self) -> SharedString {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{Context as AnyhowContext, Error, anyhow};
|
use anyhow::{Error, anyhow};
|
||||||
use gpui::prelude::FluentBuilder;
|
use gpui::prelude::FluentBuilder;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
|
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>) {
|
fn load(&mut self, window: &mut Window, 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 task: Task<Result<Vec<RelayUrl>, Error>> = cx.background_spawn(async move {
|
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 public_key = signer.get_public_key().await?;
|
||||||
|
|
||||||
let filter = Filter::new()
|
let filter = Filter::new()
|
||||||
@@ -93,7 +93,7 @@ impl MessagingRelayPanel {
|
|||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
if let Some(event) = client.database().query(filter).await?.first_owned() {
|
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 {
|
} else {
|
||||||
Err(anyhow!("Not found."))
|
Err(anyhow!("Not found."))
|
||||||
}
|
}
|
||||||
@@ -170,24 +170,33 @@ impl MessagingRelayPanel {
|
|||||||
|
|
||||||
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 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
|
||||||
.iter()
|
.iter()
|
||||||
.map(|relay| Tag::relay(relay.clone()))
|
.map(|relay| Tag::custom("relay", vec![relay.to_string()]))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// Set updating state
|
// Set updating state
|
||||||
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;
|
||||||
|
|
||||||
// Construct nip17 event builder
|
// Construct nip17 event builder
|
||||||
let builder = EventBuilder::new(Kind::InboxRelays, "").tags(tags);
|
let event = EventBuilder::new(Kind::InboxRelays, "")
|
||||||
let event = client.sign_event_builder(builder).await?;
|
.tags(tags)
|
||||||
|
.sign_async(&signer)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Set messaging relays
|
// Set messaging relays
|
||||||
client.send_event(&event).to_nip65().await?;
|
client.send_event(&event).to(write_relays).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -207,15 +207,24 @@ impl ProfilePanel {
|
|||||||
fn publish(&self, metadata: &Metadata, cx: &App) -> Task<Result<(), Error>> {
|
fn publish(&self, metadata: &Metadata, 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 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;
|
||||||
|
|
||||||
// Build and sign the metadata event
|
// Build and sign the metadata event
|
||||||
let builder = EventBuilder::metadata(&metadata);
|
let event = EventBuilder::metadata(&metadata)
|
||||||
let event = client.sign_event_builder(builder).await?;
|
.sign_async(&signer)
|
||||||
|
.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(())
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{Context as AnyhowContext, Error, anyhow};
|
use anyhow::{Error, anyhow};
|
||||||
use gpui::prelude::FluentBuilder;
|
use gpui::prelude::FluentBuilder;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
Action, AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
|
Action, AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
|
||||||
@@ -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};
|
||||||
@@ -99,10 +99,10 @@ impl RelayListPanel {
|
|||||||
fn load(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
fn load(&mut self, window: &mut Window, 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 task: Task<Result<Vec<(RelayUrl, Option<RelayMetadata>)>, Error>> = cx
|
let task: Task<Result<Vec<(RelayUrl, Option<RelayMetadata>)>, Error>> = cx
|
||||||
.background_spawn(async move {
|
.background_spawn(async move {
|
||||||
let signer = client.signer().context("Signer not found")?;
|
|
||||||
let public_key = signer.get_public_key().await?;
|
let public_key = signer.get_public_key().await?;
|
||||||
|
|
||||||
let filter = Filter::new()
|
let filter = Filter::new()
|
||||||
@@ -111,7 +111,7 @@ impl RelayListPanel {
|
|||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
if let Some(event) = client.database().query(filter).await?.first_owned() {
|
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 {
|
} else {
|
||||||
Err(anyhow!("Not found."))
|
Err(anyhow!("Not found."))
|
||||||
}
|
}
|
||||||
@@ -206,6 +206,7 @@ impl RelayListPanel {
|
|||||||
|
|
||||||
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();
|
||||||
|
|
||||||
// Get all relays
|
// Get all relays
|
||||||
let relays = self.relays.clone();
|
let relays = self.relays.clone();
|
||||||
@@ -214,11 +215,11 @@ impl RelayListPanel {
|
|||||||
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 builder = EventBuilder::relay_list(relays);
|
let signer = signer.get().await;
|
||||||
let event = client.sign_event_builder(builder).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(())
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use std::collections::HashSet;
|
|||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{Context as AnyhowContext, Error};
|
use anyhow::Error;
|
||||||
use chat::{ChatEvent, ChatRegistry, Room, RoomKind};
|
use chat::{ChatEvent, ChatRegistry, Room, RoomKind};
|
||||||
use common::{DebouncedDelay, TimestampExt, coop_cache};
|
use common::{DebouncedDelay, TimestampExt, coop_cache};
|
||||||
use entry::RoomEntry;
|
use entry::RoomEntry;
|
||||||
@@ -158,9 +158,9 @@ impl Sidebar {
|
|||||||
fn get_contact_list(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
fn get_contact_list(&mut self, window: &mut Window, 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 task: Task<Result<HashSet<PublicKey>, Error>> = cx.background_spawn(async move {
|
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 public_key = signer.get_public_key().await?;
|
||||||
let contacts = client.database().contacts_public_keys(public_key).await?;
|
let contacts = client.database().contacts_public_keys(public_key).await?;
|
||||||
|
|
||||||
|
|||||||
@@ -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(¤t_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();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user