chore: refactor backend around domain types (#26)
Rust / build (macos-latest, stable) (push) Canceled after 0s
Rust / build (ubuntu-latest, stable) (push) Canceled after 0s
Rust / build (windows-latest, stable) (push) Canceled after 0s

Reviewed-on: #26
This commit was merged in pull request #26.
This commit is contained in:
2026-10-03 10:35:48 +00:00
parent 6c4cc9f2ca
commit c76e909f7f
63 changed files with 4400 additions and 6235 deletions
+1 -6
View File
@@ -8,12 +8,7 @@ publish.workspace = true
nostr-sdk.workspace = true
nostr-connect.workspace = true
nostr-gossip-memory.workspace = true
nostr-lmdb.workspace = true
anyhow.workspace = true
webbrowser.workspace = true
[target.'cfg(target_arch = "wasm32")'.dependencies]
nostr-memory.workspace = true
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
nostr-lmdb.workspace = true
+31 -35
View File
@@ -1,51 +1,47 @@
#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;
use std::time::Duration;
use anyhow::{Context, Result};
use nostr_gossip_memory::prelude::*;
#[cfg(not(target_arch = "wasm32"))]
use nostr_lmdb::prelude::*;
#[cfg(target_arch = "wasm32")]
use nostr_memory::prelude::*;
use nostr_sdk::prelude::*;
use crate::signer::UniversalSigner;
#[cfg(not(target_arch = "wasm32"))]
pub async fn new_backend(db_path: impl AsRef<Path>) -> Result<(Client, UniversalSigner)> {
let signer = UniversalSigner::new(Keys::generate());
let database = NostrLmdb::open(db_path)
.await
.context("failed to open nostr database")?;
Ok(with_database(signer, database))
pub struct NostrBackend {
pub client: Client,
pub signer: UniversalSigner,
}
/// In-memory database on wasm, LMDB is unavailable there.
#[cfg(target_arch = "wasm32")]
pub fn new_backend() -> Result<(Client, UniversalSigner)> {
let signer = UniversalSigner::new(Keys::generate());
Ok(with_database(signer, MemoryDatabase::unbounded()))
}
impl NostrBackend {
pub async fn open(db_path: impl AsRef<Path>) -> Result<Self> {
let db_path = db_path.as_ref();
let signer = UniversalSigner::new(Keys::generate());
let database = NostrLmdb::open(db_path)
.await
.context("failed to open nostr database")?;
Ok(Self::with_database(signer, database))
}
fn with_database<D>(signer: UniversalSigner, database: D) -> (Client, UniversalSigner)
where
D: IntoNostrDatabase,
{
let authenticator = SignerAuthenticator::new(signer.clone());
fn with_database<D>(signer: UniversalSigner, database: D) -> Self
where
D: IntoNostrDatabase,
{
let authenticator = SignerAuthenticator::new(signer.clone());
let client = ClientBuilder::default()
.database(database)
.authenticator(authenticator)
.gossip(NostrGossipMemory::unbounded())
.gossip_config(GossipConfig::default().no_background_refresh())
.connect_timeout(Duration::from_secs(10))
.verify_subscriptions(true)
.ban_relay_on_mismatch(true)
.sleep_when_idle(SleepWhenIdle::Enabled {
timeout: Duration::from_secs(600),
})
.build();
let client = ClientBuilder::default()
.database(database)
.authenticator(authenticator)
.gossip(NostrGossipMemory::unbounded())
.gossip_config(GossipConfig::default().no_background_refresh())
.connect_timeout(Duration::from_secs(10))
.verify_subscriptions(true)
.ban_relay_on_mismatch(true)
.sleep_when_idle(SleepWhenIdle::Enabled {
timeout: Duration::from_secs(600),
})
.build();
(client, signer)
Self { client, signer }
}
}
+1 -1
View File
@@ -2,6 +2,6 @@ mod backend;
mod signer;
mod update;
pub use backend::new_backend;
pub use backend::NostrBackend;
pub use signer::{SignedAuthUrlHandler, UniversalSigner};
pub use update::Update;
-3
View File
@@ -8,7 +8,6 @@ use nostr_connect::client::AuthUrlHandler;
use nostr_sdk::error::Error as SignerError;
use nostr_sdk::prelude::*;
/// A type-erased signer whose inner signer can be swapped in-place.
#[derive(Clone, Debug)]
pub struct UniversalSigner {
inner: Arc<RwLock<Arc<dyn InnerSigner>>>,
@@ -27,7 +26,6 @@ impl UniversalSigner {
}
}
/// Swap the inner signer in-place. All clones see the new signer.
pub fn swap_inner<T>(&self, new_signer: T)
where
T: AsyncGetPublicKey + AsyncSignEvent + AsyncNip44 + 'static,
@@ -160,7 +158,6 @@ impl AsyncNip44 for UniversalSigner {
}
}
/// Opens the NIP-46 auth URL in the default browser.
#[derive(Debug, Clone)]
pub struct SignedAuthUrlHandler;
-2
View File
@@ -1,10 +1,8 @@
use nostr_sdk::prelude::*;
/// A lightweight change notification for the UI.
#[derive(Debug, Clone)]
pub struct Update {
pub kind: Kind,
/// First `a` tag value of the event, if any, for example the repository coordinate.
pub coordinate: Option<Coordinate>,
pub author: PublicKey,
}