Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:
- nostr-blossom
- nostr-http-file-storage
- nostr-database
- nostr-gossip
- nostr-gossip-memory
- nostr-lmdb
- nostr-indexeddb --target wasm32-unknown-unknown
- nostr-ndb
Expand Down
61 changes: 49 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ members = [
"database/nostr-lmdb",
"database/nostr-ndb",

# Gossip
"gossip/nostr-gossip",
"gossip/nostr-gossip-memory",

# Remote File Storage implementations
"rfs/nostr-blossom",
"rfs/nostr-http-file-storage",
Expand Down Expand Up @@ -39,6 +43,8 @@ negentropy = { version = "0.5", default-features = false }
nostr = { version = "0.43", path = "./crates/nostr", default-features = false }
nostr-connect = { version = "0.43", path = "./signer/nostr-connect", default-features = false }
nostr-database = { version = "0.43", path = "./database/nostr-database", default-features = false }
nostr-gossip = { version = "0.43", path = "./gossip/nostr-gossip", default-features = false }
nostr-gossip-memory = { version = "0.43", path = "./gossip/nostr-gossip-memory", default-features = false }
nostr-lmdb = { version = "0.43", path = "./database/nostr-lmdb", default-features = false }
nostr-ndb = { version = "0.43", path = "./database/nostr-ndb", default-features = false }
nostr-relay-builder = { version = "0.43", path = "./crates/nostr-relay-builder", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ The project is split up into several crates:
- [**nostr-lmdb**](./database/nostr-lmdb): LMDB storage backend
- [**nostr-ndb**](./database/nostr-ndb): [nostrdb](https://github.com/damus-io/nostrdb) storage backend
- [**nostr-indexeddb**](./database/nostr-indexeddb): IndexedDB storage backend
- [**nostr-gossip**](./gossip/nostr-gossip): Gossip traits
- [**nostr-gossip-memory**](./gossip/nostr-gossip-memory): In-memory gossip database
- Remote File Storage implementations:
- [**nostr-blossom**](./rfs/nostr-blossom): A library for interacting with the Blossom protocol
- [**nostr-http-file-storage**](./rfs/nostr-http-file-storage): HTTP File Storage client (NIP-96)
Expand Down
2 changes: 2 additions & 0 deletions contrib/scripts/check-crates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ buildargs=(
"-p nostr-blossom"
"-p nostr-http-file-storage"
"-p nostr-database"
"-p nostr-gossip"
"-p nostr-gossip-memory"
"-p nostr-lmdb"
"-p nostr-indexeddb --target wasm32-unknown-unknown"
"-p nostr-ndb"
Expand Down
2 changes: 2 additions & 0 deletions crates/nostr-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ nip98 = ["nostr/nip98"]
async-utility.workspace = true
nostr = { workspace = true, features = ["std"] }
nostr-database.workspace = true
nostr-gossip.workspace = true
nostr-relay-pool.workspace = true
tokio = { workspace = true, features = ["sync"] }
tracing = { workspace = true, features = ["std"] }
Expand All @@ -43,6 +44,7 @@ tracing = { workspace = true, features = ["std"] }
nostr-connect.workspace = true
nostr-lmdb.workspace = true
nostr-ndb.workspace = true
nostr-gossip-memory.workspace = true
tokio = { workspace = true, features = ["macros"] }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

Expand Down
4 changes: 3 additions & 1 deletion crates/nostr-sdk/examples/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
// Copyright (c) 2023-2025 Rust Nostr Developers
// Distributed under the MIT software license

use nostr_gossip_memory::prelude::*;
use nostr_sdk::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();

let keys = Keys::parse("nsec12kcgs78l06p30jz7z7h3n2x2cy99nw2z6zspjdp7qc206887mwvs95lnkx")?;
let gossip = NostrGossipMemory::unbounded();
let client = Client::builder()
.signer(keys.clone())
.opts(ClientOptions::new().gossip(true))
.gossip(gossip)
.build();

println!("Bot public key: {}", keys.public_key().to_bech32()?);
Expand Down
5 changes: 3 additions & 2 deletions crates/nostr-sdk/examples/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

use std::time::Duration;

use nostr_gossip_memory::prelude::*;
use nostr_sdk::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();

let keys = Keys::parse("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85")?;
let opts = ClientOptions::new().gossip(true);
let client = Client::builder().signer(keys).opts(opts).build();
let gossip = NostrGossipMemory::unbounded();
let client = Client::builder().signer(keys).gossip(gossip).build();

client.add_discovery_relay("wss://relay.damus.io").await?;
client.add_discovery_relay("wss://purplepag.es").await?;
Expand Down
14 changes: 14 additions & 0 deletions crates/nostr-sdk/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::sync::Arc;
use nostr::signer::{IntoNostrSigner, NostrSigner};
use nostr_database::memory::MemoryDatabase;
use nostr_database::{IntoNostrDatabase, NostrDatabase};
use nostr_gossip::NostrGossip;
use nostr_relay_pool::monitor::Monitor;
use nostr_relay_pool::policy::AdmitPolicy;
use nostr_relay_pool::transport::websocket::{
Expand All @@ -29,6 +30,8 @@ pub struct ClientBuilder {
pub admit_policy: Option<Arc<dyn AdmitPolicy>>,
/// Database
pub database: Arc<dyn NostrDatabase>,
/// Gossip
pub gossip: Option<Arc<dyn NostrGossip>>,
/// Relay monitor
pub monitor: Option<Monitor>,
/// Client options
Expand All @@ -42,6 +45,7 @@ impl Default for ClientBuilder {
websocket_transport: Arc::new(DefaultWebsocketTransport),
admit_policy: None,
database: Arc::new(MemoryDatabase::default()),
gossip: None,
monitor: None,
opts: ClientOptions::default(),
}
Expand Down Expand Up @@ -106,6 +110,16 @@ impl ClientBuilder {
self
}

/// Set a gossip database
#[inline]
pub fn gossip<T>(mut self, gossip: T) -> Self
where
T: NostrGossip + 'static,
{
self.gossip = Some(Arc::new(gossip));
self
}

/// Set monitor
#[inline]
pub fn monitor(mut self, monitor: Monitor) -> Self {
Expand Down
10 changes: 10 additions & 0 deletions crates/nostr-sdk/src/client/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::fmt;
use nostr::prelude::*;
use nostr::serde_json;
use nostr_database::prelude::*;
use nostr_gossip::error::GossipError;
use nostr_relay_pool::__private::SharedStateError;
use nostr_relay_pool::prelude::*;

Expand All @@ -21,6 +22,8 @@ pub enum Error {
Database(DatabaseError),
/// Signer error
Signer(SignerError),
/// Gossip error
Gossip(GossipError),
/// [`EventBuilder`] error
EventBuilder(event::builder::Error),
/// Json error
Expand All @@ -45,6 +48,7 @@ impl fmt::Display for Error {
Self::RelayPool(e) => e.fmt(f),
Self::Database(e) => e.fmt(f),
Self::Signer(e) => e.fmt(f),
Self::Gossip(e) => e.fmt(f),
Self::EventBuilder(e) => e.fmt(f),
Self::Json(e) => e.fmt(f),
Self::SharedState(e) => e.fmt(f),
Expand Down Expand Up @@ -82,6 +86,12 @@ impl From<SignerError> for Error {
}
}

impl From<GossipError> for Error {
fn from(e: GossipError) -> Self {
Self::Gossip(e)
}
}

impl From<event::builder::Error> for Error {
fn from(e: event::builder::Error) -> Self {
Self::EventBuilder(e)
Expand Down
10 changes: 6 additions & 4 deletions crates/nostr-sdk/src/client/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use std::sync::Arc;

use nostr::util::BoxedFuture;
use nostr::{Event, RelayUrl, SubscriptionId};
use nostr_gossip::NostrGossip;
use nostr_relay_pool::policy::{AdmitPolicy, AdmitStatus, PolicyError};

use crate::gossip::Gossip;

#[derive(Debug)]
pub(crate) struct AdmissionPolicyMiddleware {
pub(crate) gossip: Option<Gossip>,
pub(crate) gossip: Option<Arc<dyn NostrGossip>>,
pub(crate) external_policy: Option<Arc<dyn AdmitPolicy>>,
}

Expand All @@ -34,7 +33,10 @@ impl AdmitPolicy for AdmissionPolicyMiddleware {
Box::pin(async move {
// Process event in gossip
if let Some(gossip) = &self.gossip {
gossip.process_event(event).await;
gossip
.process(event, Some(relay_url))
.await
.map_err(PolicyError::backend)?;
}

// Check if event is allowed by external policy
Expand Down
Loading
Loading