.
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m40s
Rust / build (ubuntu-latest, stable) (pull_request) Failing after 1m50s

This commit is contained in:
2026-02-06 13:25:34 +07:00
parent fce4c1bbcd
commit 253d04f988
17 changed files with 1081 additions and 872 deletions

View File

@@ -1,101 +0,0 @@
use nostr_sdk::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RelayState {
#[default]
Initial,
NotSet,
Set,
}
impl RelayState {
pub fn is_initial(&self) -> bool {
matches!(self, RelayState::Initial)
}
}
/// Identity
#[derive(Debug, Default)]
pub struct Identity {
/// Signer's public key
pub public_key: Option<PublicKey>,
/// Whether the identity is owned by the user
pub owned: bool,
/// Status of the current user NIP-65 relays
relay_list: RelayState,
/// Status of the current user NIP-17 relays
messaging_relays: RelayState,
}
impl AsRef<Identity> for Identity {
fn as_ref(&self) -> &Identity {
self
}
}
impl Identity {
pub fn new() -> Self {
Self {
public_key: None,
owned: true,
relay_list: RelayState::default(),
messaging_relays: RelayState::default(),
}
}
/// Resets the relay states to their default values.
pub fn reset_relay_state(&mut self) {
self.relay_list = RelayState::default();
self.messaging_relays = RelayState::default();
}
/// Sets the state of the NIP-65 relays.
pub fn set_relay_list_state(&mut self, state: RelayState) {
self.relay_list = state;
}
/// Returns the state of the NIP-65 relays.
pub fn relay_list_state(&self) -> RelayState {
self.relay_list
}
/// Sets the state of the NIP-17 relays.
pub fn set_messaging_relays_state(&mut self, state: RelayState) {
self.messaging_relays = state;
}
/// Returns the state of the NIP-17 relays.
pub fn messaging_relays_state(&self) -> RelayState {
self.messaging_relays
}
/// Force getting the public key of the identity.
///
/// Panics if the public key is not set.
pub fn public_key(&self) -> PublicKey {
self.public_key.unwrap()
}
/// Returns true if the identity has a public key.
pub fn has_public_key(&self) -> bool {
self.public_key.is_some()
}
/// Sets the public key of the identity.
pub fn set_public_key(&mut self, public_key: PublicKey) {
self.public_key = Some(public_key);
}
/// Unsets the public key of the identity.
pub fn unset_public_key(&mut self) {
self.public_key = None;
}
/// Sets whether the identity is owned by the user.
pub fn set_owned(&mut self, owned: bool) {
self.owned = owned;
}
}

View File

@@ -1,31 +1,29 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::os::unix::fs::PermissionsExt;
use std::sync::Arc;
use std::time::Duration;
use anyhow::{anyhow, Error};
use anyhow::{anyhow, Context as AnyhowContext, Error};
use common::{config_dir, CLIENT_NAME};
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task};
use gpui_tokio::Tokio;
use nostr_connect::prelude::*;
use nostr_gossip_sqlite::prelude::*;
use nostr_lmdb::NostrLmdb;
use nostr_sdk::prelude::*;
mod device;
mod event;
mod gossip;
mod identity;
mod nip05;
mod signer;
pub use device::*;
pub use event::*;
pub use gossip::*;
pub use identity::*;
pub use nip05::*;
pub use signer::*;
use crate::identity::Identity;
/// Default timeout for subscription
pub const TIMEOUT: u64 = 3;
/// Default delay for searching
@@ -55,9 +53,19 @@ pub const BOOTSTRAP_RELAYS: [&str; 4] = [
];
pub fn init(cx: &mut App) {
// rustls uses the `aws_lc_rs` provider by default
// This only errors if the default provider has already
// been installed. We can ignore this `Result`.
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.ok();
// Initialize the tokio runtime
gpui_tokio::init(cx);
// Initialize the event tracker
let _tracker = tracker();
NostrRegistry::set_global(cx.new(NostrRegistry::new), cx);
}
@@ -74,23 +82,26 @@ pub struct NostrRegistry {
/// Nostr signer
signer: Arc<CoopSigner>,
/// By default, Coop generates a new signer for new users.
///
/// This flag indicates whether the signer is user-owned or Coop-generated.
owned_signer: bool,
/// NIP-65 relay state
nip65: Entity<RelayState>,
/// NIP-17 relay state
nip17: Entity<RelayState>,
/// App keys
///
/// Used for Nostr Connect and NIP-4e operations
app_keys: Keys,
/// Current identity (user's public key)
///
/// Set by the current Nostr signer
identity: Entity<Identity>,
/// Gossip implementation
gossip: Entity<Gossip>,
/// Tasks for asynchronous operations
tasks: Vec<Task<Result<(), Error>>>,
/// Subscriptions
/// Event subscriptions
_subscriptions: Vec<Subscription>,
}
@@ -107,27 +118,39 @@ impl NostrRegistry {
/// Create a new nostr instance
fn new(cx: &mut Context<Self>) -> Self {
// rustls uses the `aws_lc_rs` provider by default
// This only errors if the default provider has already
// been installed. We can ignore this `Result`.
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.ok();
// Construct the lmdb
// Construct the nostr lmdb instance
let lmdb = cx.foreground_executor().block_on(async move {
NostrLmdb::open(config_dir().join("nostr"))
.await
.expect("Failed to initialize database")
});
// Use tokio to spawn a task to build the gossip instance
let build_gossip_sqlite = Tokio::spawn(cx, async move {
NostrGossipSqlite::open(config_dir().join("gossip"))
.await
.expect("Failed to initialize gossip")
});
// Initialize the nostr gossip instance
let gossip = cx.foreground_executor().block_on(async move {
build_gossip_sqlite
.await
.expect("Failed to initialize gossip")
});
// Construct the nostr signer
let keys = Keys::generate();
let signer = Arc::new(CoopSigner::new(keys));
let app_keys = Self::create_or_init_app_keys().unwrap_or(Keys::generate());
let signer = Arc::new(CoopSigner::new(app_keys.clone()));
// Construct the relay states entity
let nip65 = cx.new(|_| RelayState::default());
let nip17 = cx.new(|_| RelayState::default());
// Construct the nostr client
let client = ClientBuilder::default()
.signer(signer.clone())
.gossip(gossip)
.database(lmdb)
.automatic_authentication(false)
.verify_subscriptions(false)
@@ -136,83 +159,23 @@ impl NostrRegistry {
})
.build();
// Construct the event tracker
let _tracker = tracker();
// Get the app keys
let app_keys = Self::create_or_init_app_keys().unwrap();
// Construct the gossip entity
let gossip = cx.new(|_| Gossip::default());
let async_gossip = gossip.downgrade();
// Construct the identity entity
let identity = cx.new(|_| Identity::new());
// Channel for communication between nostr and gpui
let (tx, rx) = flume::bounded::<Event>(2048);
let mut subscriptions = vec![];
let mut tasks = vec![];
subscriptions.push(
// Observe the identity entity
cx.observe(&identity, |this, state, cx| {
if state.read(cx).has_public_key() {
match state.read(cx).relay_list_state() {
RelayState::Initial => {
this.get_relay_list(cx);
}
RelayState::Set => {
if state.read(cx).messaging_relays_state() == RelayState::Initial {
this.get_profile(cx);
this.get_messaging_relays(cx);
};
}
_ => {}
}
// Observe the NIP-65 state
cx.observe(&nip65, |this, state, cx| {
if state.read(cx).configured() {
this.get_profile(cx);
this.get_messaging_relays(cx);
}
}),
);
tasks.push(
// Handle nostr notifications
cx.background_spawn({
let client = client.clone();
async move { Self::handle_notifications(&client, &tx).await }
}),
);
tasks.push(
// Update GPUI states
cx.spawn(async move |_this, cx| {
while let Ok(event) = rx.recv_async().await {
match event.kind {
Kind::RelayList => {
async_gossip.update(cx, |this, cx| {
this.insert_relays(&event);
cx.notify();
})?;
}
Kind::InboxRelays => {
async_gossip.update(cx, |this, cx| {
this.insert_messaging_relays(&event);
cx.notify();
})?;
}
_ => {}
}
}
Ok(())
}),
);
cx.defer(|cx| {
let nostr = NostrRegistry::global(cx);
nostr.update(cx, |this, cx| {
this.connect(cx);
this.get_identity(cx);
});
});
@@ -220,89 +183,188 @@ impl NostrRegistry {
Self {
client,
signer,
owned_signer: false,
nip65,
nip17,
app_keys,
identity,
gossip,
tasks: vec![],
_subscriptions: subscriptions,
tasks,
}
}
/// Handle nostr notifications
async fn handle_notifications(client: &Client, tx: &flume::Sender<Event>) -> Result<(), Error> {
// Add bootstrap relay to the relay pool
for url in BOOTSTRAP_RELAYS.into_iter() {
client.add_relay(url).await?;
}
/// Connect to all bootstrap relays
fn connect(&mut self, cx: &mut Context<Self>) {
let client = self.client();
// Add search relay to the relay pool
for url in SEARCH_RELAYS.into_iter() {
client.add_relay(url).await?;
}
self.tasks.push(cx.background_spawn(async move {
// Add bootstrap relay to the relay pool
for url in BOOTSTRAP_RELAYS.into_iter() {
client.add_relay(url).await?;
}
// Add wot relay to the relay pool
for url in WOT_RELAYS.into_iter() {
client.add_relay(url).await?;
}
// Add search relay to the relay pool
for url in SEARCH_RELAYS.into_iter() {
client.add_relay(url).await?;
}
// Connect to all added relays
client.connect().await;
// Add wot relay to the relay pool
for url in WOT_RELAYS.into_iter() {
client.add_relay(url).await?;
}
// Handle nostr notifications
let mut notifications = client.notifications();
let mut processed_events = HashSet::new();
// Connect to all added relays
client.connect().await;
while let Some(notification) = notifications.next().await {
if let ClientNotification::Message { message, relay_url } = notification {
match message {
RelayMessage::Event {
event,
subscription_id,
} => {
if !processed_events.insert(event.id) {
// Skip if the event has already been processed
continue;
}
Ok(())
}));
}
match event.kind {
Kind::RelayList => {
// Automatically get messaging relays for each member when the user opens a room
if subscription_id.as_str().starts_with("room-") {
Self::get_adv_events_by(client, event.as_ref()).await?;
}
/// Get the nostr client
pub fn client(&self) -> Client {
self.client.clone()
}
tx.send_async(event.into_owned()).await?;
}
Kind::InboxRelays => {
tx.send_async(event.into_owned()).await?;
}
_ => {}
}
}
RelayMessage::Ok {
event_id, message, ..
} => {
let msg = MachineReadablePrefix::parse(&message);
let mut tracker = tracker().write().await;
/// Get the app keys
pub fn app_keys(&self) -> &Keys {
&self.app_keys
}
// Handle authentication messages
if let Some(MachineReadablePrefix::AuthRequired) = msg {
// Keep track of events that need to be resent after authentication
tracker.add_to_pending(event_id, relay_url);
} else {
// Keep track of events sent by Coop
tracker.sent(event_id)
}
}
_ => {}
/// Returns whether the current signer is owned by user
pub fn owned_signer(&self) -> bool {
self.owned_signer
}
/// Set whether the current signer is owned by user
pub fn set_owned_signer(&mut self, owned: bool, cx: &mut Context<Self>) {
self.owned_signer = owned;
cx.notify();
}
/// Get the NIP-65 state
pub fn nip65_state(&self) -> Entity<RelayState> {
self.nip65.clone()
}
/// Get the NIP-17 state
pub fn nip17_state(&self) -> Entity<RelayState> {
self.nip17.clone()
}
/// Get current signer's public key
pub fn signer_pkey(&self, cx: &App) -> Task<Result<PublicKey, Error>> {
let client = self.client();
cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
Ok(public_key)
})
}
/// Get a relay hint (messaging relay) for a given public key
///
/// Used for building chat messages
pub fn relay_hint(&self, public_key: &PublicKey, cx: &App) -> Task<Option<RelayUrl>> {
let client = self.client();
let public_key = public_key.to_owned();
cx.background_spawn(async move {
let filter = Filter::new()
.author(public_key)
.kind(Kind::InboxRelays)
.limit(1);
if let Ok(events) = client.database().query(filter).await {
if let Some(event) = events.first_owned() {
let relays: Vec<RelayUrl> = nip17::extract_owned_relay_list(event).collect();
return relays.first().cloned();
}
}
}
Ok(())
None
})
}
/// Automatically get messaging relays and encryption announcement from a received relay list
/// Get a list of messaging relays with current signer's public key
pub fn messaging_relays(&self, cx: &App) -> Task<Vec<RelayUrl>> {
let client = self.client();
cx.background_spawn(async move {
let Ok(signer) = client.signer().context("Signer not found") else {
return vec![];
};
let Ok(public_key) = signer.get_public_key().await else {
return vec![];
};
let filter = Filter::new()
.kind(Kind::InboxRelays)
.author(public_key)
.limit(1);
client
.database()
.query(filter)
.await
.ok()
.and_then(|events| events.first_owned())
.map(|event| nip17::extract_owned_relay_list(event).collect())
.unwrap_or_default()
})
}
/// Reset all relay states
pub fn reset_relay_states(&mut self, cx: &mut Context<Self>) {
self.nip65.update(cx, |this, cx| {
*this = RelayState::default();
cx.notify();
});
self.nip17.update(cx, |this, cx| {
*this = RelayState::default();
cx.notify();
});
}
/// Set the signer for the nostr client and verify the public key
pub fn set_signer<T>(&mut self, new: T, owned: bool, cx: &mut Context<Self>)
where
T: NostrSigner + 'static,
{
let client = self.client();
let signer = self.signer.clone();
// Create a task to update the signer and verify the public key
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
// Update signer
signer.switch(new).await;
// Verify signer
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(())
});
self.tasks.push(cx.spawn(async move |this, cx| {
// set signer
task.await?;
// Update states
this.update(cx, |this, cx| {
this.reset_relay_states(cx);
this.get_relay_list(cx);
this.set_owned_signer(owned, cx);
})?;
Ok(())
}));
}
/*
async fn get_adv_events_by(client: &Client, event: &Event) -> Result<(), Error> {
// Subscription options
let opts = SubscribeAutoCloseOptions::default()
@@ -348,6 +410,7 @@ impl NostrRegistry {
Ok(())
}
*/
/// Get or create a new app keys
fn create_or_init_app_keys() -> Result<Keys, Error> {
@@ -377,124 +440,15 @@ impl NostrRegistry {
Ok(keys)
}
/// Get the nostr client
pub fn client(&self) -> Client {
self.client.clone()
}
/// Get the app keys
pub fn app_keys(&self) -> &Keys {
&self.app_keys
}
/// Get current identity
pub fn identity(&self) -> Entity<Identity> {
self.identity.clone()
}
/// Get a relay hint (messaging relay) for a given public key
pub fn relay_hint(&self, public_key: &PublicKey, cx: &App) -> Option<RelayUrl> {
self.gossip
.read(cx)
.messaging_relays(public_key)
.first()
.cloned()
}
/// Get a list of write relays for a given public key
pub fn write_relays(&self, public_key: &PublicKey, cx: &App) -> Task<Vec<RelayUrl>> {
let client = self.client();
let relays = self.gossip.read(cx).write_relays(public_key);
cx.background_spawn(async move {
// Ensure relay connections
for url in relays.iter() {
client.add_relay(url).await.ok();
client.connect_relay(url).await.ok();
}
relays
})
}
/// Get a list of read relays for a given public key
pub fn read_relays(&self, public_key: &PublicKey, cx: &App) -> Task<Vec<RelayUrl>> {
let client = self.client();
let relays = self.gossip.read(cx).read_relays(public_key);
cx.background_spawn(async move {
// Ensure relay connections
for url in relays.iter() {
client.add_relay(url).await.ok();
client.connect_relay(url).await.ok();
}
relays
})
}
/// Get a list of messaging relays for a given public key
pub fn messaging_relays(&self, public_key: &PublicKey, cx: &App) -> Task<Vec<RelayUrl>> {
let client = self.client();
let relays = self.gossip.read(cx).messaging_relays(public_key);
cx.background_spawn(async move {
// Ensure relay connections
for url in relays.iter() {
client.add_relay(url).await.ok();
client.connect_relay(url).await.ok();
}
relays
})
}
/// Set the signer for the nostr client and verify the public key
pub fn set_signer<T>(&mut self, new: T, owned: bool, cx: &mut Context<Self>)
where
T: NostrSigner + 'static,
{
let identity = self.identity.downgrade();
let signer = self.signer.clone();
// 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
signer.switch(new).await;
// Verify signer
let public_key = signer.get_public_key().await?;
log::info!("test: {public_key:?}");
Ok(public_key)
});
self.tasks.push(cx.spawn(async move |_this, cx| {
match task.await {
Ok(public_key) => {
identity.update(cx, |this, cx| {
this.set_public_key(public_key);
this.reset_relay_state();
this.set_owned(owned);
cx.notify();
})?;
}
Err(e) => {
log::error!("Failed to set signer: {e}");
}
};
Ok(())
}));
}
// Get relay list for current user
fn get_relay_list(&mut self, cx: &mut Context<Self>) {
let client = self.client();
let async_identity = self.identity.downgrade();
let public_key = self.identity().read(cx).public_key();
let nip65 = self.nip65.downgrade();
let task: Task<Result<RelayState, Error>> = cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
let filter = Filter::new()
.kind(Kind::RelayList)
.author(public_key)
@@ -531,7 +485,7 @@ impl NostrRegistry {
// Subscribe to the relay list events
client.subscribe(target).await?;
return Ok(RelayState::Set);
return Ok(RelayState::Configured);
}
Err(e) => {
log::error!("Failed to receive relay list event: {e}");
@@ -539,15 +493,15 @@ impl NostrRegistry {
}
}
Ok(RelayState::NotSet)
Ok(RelayState::NotConfigured)
});
self.tasks.push(cx.spawn(async move |_this, cx| {
match task.await {
Ok(state) => {
async_identity
Ok(new_state) => {
nip65
.update(cx, |this, cx| {
this.set_relay_list_state(state);
*this = new_state;
cx.notify();
})
.ok();
@@ -561,19 +515,78 @@ impl NostrRegistry {
}));
}
/// Get messaging relays for current user
fn get_messaging_relays(&mut self, cx: &mut Context<Self>) {
let client = self.client();
let nip17 = self.nip17.downgrade();
let task: Task<Result<RelayState, Error>> = cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
// Construct the filter for inbox relays
let filter = Filter::new()
.kind(Kind::InboxRelays)
.author(public_key)
.limit(1);
// Stream events from the write relays
let mut stream = client
.stream_events(filter)
.timeout(Duration::from_secs(TIMEOUT))
.await?;
while let Some((_url, res)) = stream.next().await {
match res {
Ok(event) => {
log::info!("Received messaging relays event: {event:?}");
// Construct a filter to continuously receive relay list events
let filter = Filter::new()
.kind(Kind::InboxRelays)
.author(public_key)
.since(Timestamp::now());
// Subscribe to the relay list events
client.subscribe(filter).await?;
return Ok(RelayState::Configured);
}
Err(e) => {
log::error!("Failed to get messaging relays: {e}");
}
}
}
Ok(RelayState::NotConfigured)
});
self.tasks.push(cx.spawn(async move |_this, cx| {
match task.await {
Ok(new_state) => {
nip17
.update(cx, |this, cx| {
*this = new_state;
cx.notify();
})
.ok();
}
Err(e) => {
log::error!("Failed to get messaging relays: {e}");
}
}
Ok(())
}));
}
/// Get profile and contact list for current user
fn get_profile(&mut self, cx: &mut Context<Self>) {
let client = self.client();
let public_key = self.identity().read(cx).public_key();
let write_relays = self.write_relays(&public_key, cx);
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let mut urls = write_relays.await;
urls.extend(
BOOTSTRAP_RELAYS
.iter()
.filter_map(|url| RelayUrl::parse(url).ok()),
);
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
// Construct subscription options
let opts = SubscribeAutoCloseOptions::default()
@@ -592,13 +605,10 @@ impl NostrRegistry {
.limit(1)
.author(public_key);
// Construct targets for subscription
let target = urls
.into_iter()
.map(|relay| (relay, vec![metadata.clone(), contact_list.clone()]))
.collect::<HashMap<_, _>>();
client.subscribe(target).close_on(opts).await?;
client
.subscribe(vec![metadata, contact_list])
.close_on(opts)
.await?;
Ok(())
});
@@ -606,90 +616,14 @@ impl NostrRegistry {
task.detach();
}
/// Get messaging relays for current user
fn get_messaging_relays(&mut self, cx: &mut Context<Self>) {
let client = self.client();
let async_identity = self.identity.downgrade();
let public_key = self.identity().read(cx).public_key();
let write_relays = self.write_relays(&public_key, cx);
let task: Task<Result<RelayState, Error>> = cx.background_spawn(async move {
let urls = write_relays.await;
// Construct the filter for inbox relays
let filter = Filter::new()
.kind(Kind::InboxRelays)
.author(public_key)
.limit(1);
// Construct targets for subscription
let target = urls
.iter()
.map(|relay| (relay, vec![filter.clone()]))
.collect::<HashMap<_, _>>();
// Stream events from the write relays
let mut stream = client
.stream_events(target)
.timeout(Duration::from_secs(TIMEOUT))
.await?;
while let Some((_url, res)) = stream.next().await {
match res {
Ok(event) => {
log::info!("Received messaging relays event: {event:?}");
// Construct a filter to continuously receive relay list events
let filter = Filter::new()
.kind(Kind::InboxRelays)
.author(public_key)
.since(Timestamp::now());
// Construct targets for subscription
let target = urls
.iter()
.map(|relay| (relay, vec![filter.clone()]))
.collect::<HashMap<_, _>>();
// Subscribe to the relay list events
client.subscribe(target).await?;
return Ok(RelayState::Set);
}
Err(e) => {
log::error!("Failed to get messaging relays: {e}");
}
}
}
Ok(RelayState::NotSet)
});
self.tasks.push(cx.spawn(async move |_this, cx| {
match task.await {
Ok(state) => {
async_identity
.update(cx, |this, cx| {
this.set_messaging_relays_state(state);
cx.notify();
})
.ok();
}
Err(e) => {
log::error!("Failed to get messaging relays: {e}");
}
}
Ok(())
}));
}
/// Get contact list for the current user
pub fn get_contact_list(&self, cx: &App) -> Task<Result<Vec<PublicKey>, Error>> {
let client = self.client();
let public_key = self.identity().read(cx).public_key();
cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
let contacts = client.database().contacts_public_keys(public_key).await?;
let results = contacts.into_iter().collect();
@@ -700,19 +634,15 @@ impl NostrRegistry {
/// Set the metadata for the current user
pub fn set_metadata(&self, metadata: &Metadata, cx: &App) -> Task<Result<(), Error>> {
let client = self.client();
let public_key = self.identity().read(cx).public_key();
let write_relays = self.write_relays(&public_key, cx);
let metadata = metadata.clone();
cx.background_spawn(async move {
let urls = write_relays.await;
// Build and sign the metadata event
let builder = EventBuilder::metadata(&metadata);
let event = client.sign_event_builder(builder).await?;
// Send event to user's write relayss
client.send_event(&event).to(urls).await?;
client.send_event(&event).to_nip65().await?;
Ok(())
})
@@ -1007,6 +937,29 @@ impl NostrRegistry {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RelayState {
#[default]
Idle,
Checking,
NotConfigured,
Configured,
}
impl RelayState {
pub fn idle(&self) -> bool {
matches!(self, RelayState::Idle)
}
pub fn not_configured(&self) -> bool {
matches!(self, RelayState::NotConfigured)
}
pub fn configured(&self) -> bool {
matches!(self, RelayState::Configured)
}
}
#[derive(Debug, Clone)]
pub struct CoopAuthUrlHandler;

View File

@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::result::Result;
use std::sync::Arc;
use nostr_sdk::prelude::*;
@@ -19,10 +20,12 @@ impl CoopSigner {
}
}
async fn get(&self) -> Arc<dyn NostrSigner> {
/// Get the current signer.
pub async fn get(&self) -> Arc<dyn NostrSigner> {
self.signer.read().await.clone()
}
/// Switch the current signer to a new signer.
pub async fn switch<T>(&self, new: T)
where
T: IntoNostrSigner,
@@ -33,40 +36,40 @@ impl CoopSigner {
}
impl NostrSigner for CoopSigner {
#[allow(mismatched_lifetime_syntaxes)]
fn backend(&self) -> SignerBackend {
SignerBackend::Custom(Cow::Borrowed("custom"))
}
fn get_public_key(&self) -> BoxedFuture<Result<PublicKey, SignerError>> {
Box::pin(async move { Ok(self.get().await.get_public_key().await?) })
fn get_public_key<'a>(&'a self) -> BoxedFuture<'a, Result<PublicKey, SignerError>> {
Box::pin(async move { self.get().await.get_public_key().await })
}
fn sign_event(
&self,
fn sign_event<'a>(
&'a self,
unsigned: UnsignedEvent,
) -> BoxedFuture<std::result::Result<Event, SignerError>> {
Box::pin(async move { Ok(self.get().await.sign_event(unsigned).await?) })
) -> BoxedFuture<'a, Result<Event, SignerError>> {
Box::pin(async move { self.get().await.sign_event(unsigned).await })
}
fn nip04_encrypt<'a>(
&'a self,
public_key: &'a PublicKey,
content: &'a str,
) -> BoxedFuture<'a, std::result::Result<String, SignerError>> {
Box::pin(async move { Ok(self.get().await.nip04_encrypt(public_key, content).await?) })
) -> BoxedFuture<'a, Result<String, SignerError>> {
Box::pin(async move { self.get().await.nip04_encrypt(public_key, content).await })
}
fn nip04_decrypt<'a>(
&'a self,
public_key: &'a PublicKey,
encrypted_content: &'a str,
) -> BoxedFuture<'a, std::result::Result<String, SignerError>> {
) -> BoxedFuture<'a, Result<String, SignerError>> {
Box::pin(async move {
Ok(self
.get()
self.get()
.await
.nip04_decrypt(public_key, encrypted_content)
.await?)
.await
})
}
@@ -74,15 +77,15 @@ impl NostrSigner for CoopSigner {
&'a self,
public_key: &'a PublicKey,
content: &'a str,
) -> BoxedFuture<'a, std::result::Result<String, SignerError>> {
Box::pin(async move { Ok(self.get().await.nip44_encrypt(public_key, content).await?) })
) -> BoxedFuture<'a, Result<String, SignerError>> {
Box::pin(async move { self.get().await.nip44_encrypt(public_key, content).await })
}
fn nip44_decrypt<'a>(
&'a self,
public_key: &'a PublicKey,
payload: &'a str,
) -> BoxedFuture<'a, std::result::Result<String, SignerError>> {
Box::pin(async move { Ok(self.get().await.nip44_decrypt(public_key, payload).await?) })
) -> BoxedFuture<'a, Result<String, SignerError>> {
Box::pin(async move { self.get().await.nip44_decrypt(public_key, payload).await })
}
}