chore: rewrite the backend (not tested) (#203)
* wip: refactor * refactor * clean up * . * rename * add relay auth * . * . * optimize * . * clean up * add encryption crate * . * . * . * . * . * add encryption crate * . * refactor nip4e * . * fix endless loop * fix metadata fetching
This commit is contained in:
23
crates/state/Cargo.toml
Normal file
23
crates/state/Cargo.toml
Normal file
@@ -0,0 +1,23 @@
|
||||
[package]
|
||||
name = "state"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
publish.workspace = true
|
||||
|
||||
[dependencies]
|
||||
common = { path = "../common" }
|
||||
|
||||
nostr-sdk.workspace = true
|
||||
nostr-lmdb.workspace = true
|
||||
nostr-gossip-memory.workspace = true
|
||||
|
||||
gpui.workspace = true
|
||||
smol.workspace = true
|
||||
smallvec.workspace = true
|
||||
log.workspace = true
|
||||
anyhow.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
||||
rustls = "0.23.23"
|
||||
event-listener = "5.4.1"
|
||||
312
crates/state/src/lib.rs
Normal file
312
crates/state/src/lib.rs
Normal file
@@ -0,0 +1,312 @@
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{anyhow, Error};
|
||||
use common::{config_dir, BOOTSTRAP_RELAYS, SEARCH_RELAYS};
|
||||
use gpui::{App, AppContext, Context, Entity, Global, Task};
|
||||
use nostr_gossip_memory::prelude::*;
|
||||
use nostr_lmdb::NostrLMDB;
|
||||
use nostr_sdk::prelude::*;
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
use smol::lock::RwLock;
|
||||
pub use storage::*;
|
||||
pub use tracker::*;
|
||||
|
||||
mod storage;
|
||||
mod tracker;
|
||||
|
||||
pub const GIFTWRAP_SUBSCRIPTION: &str = "default-inbox";
|
||||
pub const ENCRYPTION_GIFTWARP_SUBSCRIPTION: &str = "encryption-inbox";
|
||||
|
||||
pub fn init(cx: &mut App) {
|
||||
NostrRegistry::set_global(cx.new(NostrRegistry::new), cx);
|
||||
}
|
||||
|
||||
struct GlobalNostrRegistry(Entity<NostrRegistry>);
|
||||
|
||||
impl Global for GlobalNostrRegistry {}
|
||||
|
||||
/// Nostr Registry
|
||||
#[derive(Debug)]
|
||||
pub struct NostrRegistry {
|
||||
/// Nostr client instance
|
||||
client: Arc<Client>,
|
||||
|
||||
/// Tracks activity related to Nostr events
|
||||
tracker: Arc<RwLock<EventTracker>>,
|
||||
|
||||
/// Manages caching of nostr events
|
||||
cache_manager: Arc<RwLock<CacheManager>>,
|
||||
|
||||
/// Tasks for asynchronous operations
|
||||
_tasks: SmallVec<[Task<()>; 1]>,
|
||||
}
|
||||
|
||||
impl NostrRegistry {
|
||||
/// Retrieve the global nostr state
|
||||
pub fn global(cx: &App) -> Entity<Self> {
|
||||
cx.global::<GlobalNostrRegistry>().0.clone()
|
||||
}
|
||||
|
||||
/// Set the global nostr instance
|
||||
fn set_global(state: Entity<Self>, cx: &mut App) {
|
||||
cx.set_global(GlobalNostrRegistry(state));
|
||||
}
|
||||
|
||||
/// 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();
|
||||
|
||||
let path = config_dir().join("nostr");
|
||||
let lmdb = NostrLMDB::open(path).expect("Failed to initialize database");
|
||||
let gossip = NostrGossipMemory::unbounded();
|
||||
|
||||
// Nostr client options
|
||||
let opts = ClientOptions::new()
|
||||
.automatic_authentication(false)
|
||||
.verify_subscriptions(false)
|
||||
.sleep_when_idle(SleepWhenIdle::Enabled {
|
||||
timeout: Duration::from_secs(600),
|
||||
});
|
||||
|
||||
// Construct the nostr client
|
||||
let client = Arc::new(
|
||||
ClientBuilder::default()
|
||||
.gossip(gossip)
|
||||
.database(lmdb)
|
||||
.opts(opts)
|
||||
.build(),
|
||||
);
|
||||
|
||||
let tracker = Arc::new(RwLock::new(EventTracker::default()));
|
||||
let cache_manager = Arc::new(RwLock::new(CacheManager::default()));
|
||||
|
||||
let mut tasks = smallvec![];
|
||||
|
||||
tasks.push(
|
||||
// Establish connection to the bootstrap relays
|
||||
//
|
||||
// And handle notifications from the nostr relay pool channel
|
||||
cx.background_spawn({
|
||||
let client = Arc::clone(&client);
|
||||
let cache_manager = Arc::clone(&cache_manager);
|
||||
let tracker = Arc::clone(&tracker);
|
||||
let _ = initialized_at();
|
||||
|
||||
async move {
|
||||
// Connect to the bootstrap relays
|
||||
Self::connect(&client).await;
|
||||
|
||||
// Handle notifications from the relay pool
|
||||
Self::handle_notifications(&client, &cache_manager, &tracker).await;
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
Self {
|
||||
client,
|
||||
tracker,
|
||||
cache_manager,
|
||||
_tasks: tasks,
|
||||
}
|
||||
}
|
||||
|
||||
/// Establish connection to the bootstrap relays
|
||||
async fn connect(client: &Client) {
|
||||
// Get all bootstrapping relays
|
||||
let mut urls = vec![];
|
||||
urls.extend(BOOTSTRAP_RELAYS);
|
||||
urls.extend(SEARCH_RELAYS);
|
||||
|
||||
// Add relay to the relay pool
|
||||
for url in urls.into_iter() {
|
||||
client.add_relay(url).await.ok();
|
||||
}
|
||||
|
||||
// Connect to all added relays
|
||||
client.connect().await;
|
||||
}
|
||||
|
||||
async fn handle_notifications(
|
||||
client: &Client,
|
||||
cache: &Arc<RwLock<CacheManager>>,
|
||||
tracker: &Arc<RwLock<EventTracker>>,
|
||||
) {
|
||||
let mut notifications = client.notifications();
|
||||
log::info!("Listening for notifications");
|
||||
|
||||
let mut processed_events = HashSet::new();
|
||||
|
||||
while let Ok(notification) = notifications.recv().await {
|
||||
let RelayPoolNotification::Message { message, relay_url } = notification else {
|
||||
// Skip if the notification is not a message
|
||||
continue;
|
||||
};
|
||||
|
||||
match message {
|
||||
RelayMessage::Event { event, .. } => {
|
||||
if !processed_events.insert(event.id) {
|
||||
// Skip if the event has already been processed
|
||||
continue;
|
||||
}
|
||||
|
||||
match event.kind {
|
||||
Kind::RelayList => {
|
||||
if Self::is_self_authored(client, &event).await {
|
||||
log::info!("Found relay list event for the current user");
|
||||
let author = event.pubkey;
|
||||
let announcement = Kind::Custom(10044);
|
||||
|
||||
// Fetch user's messaging relays event
|
||||
_ = Self::subscribe(client, author, Kind::InboxRelays).await;
|
||||
// Fetch user's encryption announcement event
|
||||
_ = Self::subscribe(client, author, announcement).await;
|
||||
// Fetch user's metadata event
|
||||
_ = Self::subscribe(client, author, Kind::Metadata).await;
|
||||
// Fetch user's contact list event
|
||||
_ = Self::subscribe(client, author, Kind::ContactList).await;
|
||||
}
|
||||
}
|
||||
Kind::InboxRelays => {
|
||||
// Extract up to 3 messaging relays
|
||||
let urls: Vec<RelayUrl> =
|
||||
nip17::extract_relay_list(&event).take(3).cloned().collect();
|
||||
|
||||
// Subscribe to gift wrap events if event is from current user
|
||||
if Self::is_self_authored(client, &event).await {
|
||||
log::info!("Found messaging list event for the current user");
|
||||
|
||||
if let Err(e) =
|
||||
Self::get_messages(client, &urls, event.pubkey).await
|
||||
{
|
||||
log::error!("Failed to subscribe to gift wrap events: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
// Cache the messaging relays
|
||||
let mut cache = cache.write().await;
|
||||
cache.insert_relay(event.pubkey, urls);
|
||||
}
|
||||
Kind::ContactList => {
|
||||
if Self::is_self_authored(client, &event).await {
|
||||
let pubkeys: Vec<_> = event.tags.public_keys().copied().collect();
|
||||
|
||||
if let Err(e) = Self::get_metadata_for_list(client, pubkeys).await {
|
||||
log::error!("Failed to get metadata for list: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
RelayMessage::Ok {
|
||||
event_id, message, ..
|
||||
} => {
|
||||
let msg = MachineReadablePrefix::parse(&message);
|
||||
let mut tracker = tracker.write().await;
|
||||
|
||||
// Message that need to be authenticated will be handled separately
|
||||
if let Some(MachineReadablePrefix::AuthRequired) = msg {
|
||||
// Keep track of events that need to be resent after authentication
|
||||
tracker.resend_queue.insert(event_id, relay_url);
|
||||
} else {
|
||||
// Keep track of events sent by Coop
|
||||
tracker.sent_ids.insert(event_id);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if event is published by current user
|
||||
async fn is_self_authored(client: &Client, event: &Event) -> bool {
|
||||
if let Ok(signer) = client.signer().await {
|
||||
if let Ok(public_key) = signer.get_public_key().await {
|
||||
return public_key == event.pubkey;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Subscribe for events that match the given kind for a given author
|
||||
async fn subscribe(client: &Client, author: PublicKey, kind: Kind) -> Result<(), Error> {
|
||||
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
|
||||
let filter = Filter::new().author(author).kind(kind).limit(1);
|
||||
|
||||
// Subscribe to filters from the user's write relays
|
||||
client.subscribe(filter, Some(opts)).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get all gift wrap events in the messaging relays for a given public key
|
||||
async fn get_messages(
|
||||
client: &Client,
|
||||
urls: &[RelayUrl],
|
||||
public_key: PublicKey,
|
||||
) -> Result<(), Error> {
|
||||
let id = SubscriptionId::new(GIFTWRAP_SUBSCRIPTION);
|
||||
let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
|
||||
|
||||
// Verify that there are relays provided
|
||||
if urls.is_empty() {
|
||||
return Err(anyhow!("No relays provided"));
|
||||
}
|
||||
|
||||
// Add and connect relays
|
||||
for url in urls {
|
||||
client.add_relay(url).await?;
|
||||
client.connect_relay(url).await?;
|
||||
}
|
||||
|
||||
// Subscribe to filters to user's messaging relays
|
||||
client.subscribe_with_id_to(urls, id, filter, None).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get metadata for a list of public keys
|
||||
async fn get_metadata_for_list(client: &Client, pubkeys: Vec<PublicKey>) -> Result<(), Error> {
|
||||
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
|
||||
let kinds = vec![Kind::Metadata, Kind::ContactList, Kind::RelayList];
|
||||
|
||||
// Return if the list is empty
|
||||
if pubkeys.is_empty() {
|
||||
return Err(anyhow!("You need at least one public key".to_string(),));
|
||||
}
|
||||
|
||||
let filter = Filter::new()
|
||||
.limit(pubkeys.len() * kinds.len() + 10)
|
||||
.authors(pubkeys)
|
||||
.kinds(kinds);
|
||||
|
||||
// Subscribe to filters to the bootstrap relays
|
||||
client
|
||||
.subscribe_to(BOOTSTRAP_RELAYS, filter, Some(opts))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns a reference to the nostr client.
|
||||
pub fn client(&self) -> Arc<Client> {
|
||||
Arc::clone(&self.client)
|
||||
}
|
||||
|
||||
/// Returns a reference to the event tracker.
|
||||
pub fn tracker(&self) -> Arc<RwLock<EventTracker>> {
|
||||
Arc::clone(&self.tracker)
|
||||
}
|
||||
|
||||
/// Returns a reference to the cache manager.
|
||||
pub fn cache_manager(&self) -> Arc<RwLock<CacheManager>> {
|
||||
Arc::clone(&self.cache_manager)
|
||||
}
|
||||
}
|
||||
87
crates/state/src/storage.rs
Normal file
87
crates/state/src/storage.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use gpui::SharedString;
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Announcement {
|
||||
id: EventId,
|
||||
client: String,
|
||||
public_key: PublicKey,
|
||||
}
|
||||
|
||||
impl Announcement {
|
||||
pub fn new(id: EventId, client_name: String, public_key: PublicKey) -> Self {
|
||||
Self {
|
||||
id,
|
||||
client: client_name,
|
||||
public_key,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn id(&self) -> EventId {
|
||||
self.id
|
||||
}
|
||||
|
||||
pub fn public_key(&self) -> PublicKey {
|
||||
self.public_key
|
||||
}
|
||||
|
||||
pub fn client(&self) -> SharedString {
|
||||
SharedString::from(self.client.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Response {
|
||||
payload: String,
|
||||
public_key: PublicKey,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
pub fn new(payload: String, public_key: PublicKey) -> Self {
|
||||
Self {
|
||||
payload,
|
||||
public_key,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn public_key(&self) -> PublicKey {
|
||||
self.public_key
|
||||
}
|
||||
|
||||
pub fn payload(&self) -> &str {
|
||||
self.payload.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct CacheManager {
|
||||
/// Cache of messaging relays for each public key
|
||||
relay: HashMap<PublicKey, HashSet<RelayUrl>>,
|
||||
|
||||
/// Cache of device announcement for each public key
|
||||
announcement: HashMap<PublicKey, Option<Announcement>>,
|
||||
}
|
||||
|
||||
impl CacheManager {
|
||||
pub fn relay(&self, public_key: &PublicKey) -> Option<&HashSet<RelayUrl>> {
|
||||
self.relay.get(public_key)
|
||||
}
|
||||
|
||||
pub fn insert_relay(&mut self, public_key: PublicKey, urls: Vec<RelayUrl>) {
|
||||
self.relay.entry(public_key).or_default().extend(urls);
|
||||
}
|
||||
|
||||
pub fn announcement(&self, public_key: &PublicKey) -> Option<&Option<Announcement>> {
|
||||
self.announcement.get(public_key)
|
||||
}
|
||||
|
||||
pub fn insert_announcement(
|
||||
&mut self,
|
||||
public_key: PublicKey,
|
||||
announcement: Option<Announcement>,
|
||||
) {
|
||||
self.announcement.insert(public_key, announcement);
|
||||
}
|
||||
}
|
||||
50
crates/state/src/tracker.rs
Normal file
50
crates/state/src/tracker.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
static INITIALIZED_AT: OnceLock<Timestamp> = OnceLock::new();
|
||||
|
||||
pub fn initialized_at() -> &'static Timestamp {
|
||||
INITIALIZED_AT.get_or_init(Timestamp::now)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct EventTracker {
|
||||
/// Tracking events that have failed to unwrap
|
||||
pub failed_unwrap_events: Vec<Event>,
|
||||
|
||||
/// Tracking events that have been resent by Coop in the current session
|
||||
pub resent_ids: Vec<Output<EventId>>,
|
||||
|
||||
/// Temporarily store events that need to be resent later
|
||||
pub resend_queue: HashMap<EventId, RelayUrl>,
|
||||
|
||||
/// Tracking events sent by Coop in the current session
|
||||
pub sent_ids: HashSet<EventId>,
|
||||
|
||||
/// Tracking events seen on which relays in the current session
|
||||
pub seen_on_relays: HashMap<EventId, HashSet<RelayUrl>>,
|
||||
}
|
||||
|
||||
impl EventTracker {
|
||||
pub fn failed_unwrap_events(&self) -> &Vec<Event> {
|
||||
&self.failed_unwrap_events
|
||||
}
|
||||
|
||||
pub fn resent_ids(&self) -> &Vec<Output<EventId>> {
|
||||
&self.resent_ids
|
||||
}
|
||||
|
||||
pub fn resend_queue(&self) -> &HashMap<EventId, RelayUrl> {
|
||||
&self.resend_queue
|
||||
}
|
||||
|
||||
pub fn sent_ids(&self) -> &HashSet<EventId> {
|
||||
&self.sent_ids
|
||||
}
|
||||
|
||||
pub fn seen_on_relays(&self) -> &HashMap<EventId, HashSet<RelayUrl>> {
|
||||
&self.seen_on_relays
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user