nostr-sdk 0.44.1

High level Nostr client library.
Documentation
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2025 Rust Nostr Developers
// Distributed under the MIT software license

//! Client builder

use std::sync::Arc;

use nostr::signer::{IntoNostrSigner, NostrSigner};
use nostr_database::memory::MemoryDatabase;
use nostr_database::{IntoNostrDatabase, NostrDatabase};
use nostr_gossip::{IntoNostrGossip, NostrGossip};
use nostr_relay_pool::monitor::Monitor;
use nostr_relay_pool::policy::AdmitPolicy;
use nostr_relay_pool::transport::websocket::{
    DefaultWebsocketTransport, IntoWebSocketTransport, WebSocketTransport,
};

use crate::client::options::ClientOptions;
use crate::client::Client;

/// Client builder
#[derive(Debug, Clone)]
pub struct ClientBuilder {
    /// Nostr Signer
    pub signer: Option<Arc<dyn NostrSigner>>,
    /// WebSocket transport
    pub websocket_transport: Arc<dyn WebSocketTransport>,
    /// Admission policy
    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
    pub opts: ClientOptions,
}

impl Default for ClientBuilder {
    fn default() -> Self {
        Self {
            signer: None,
            websocket_transport: Arc::new(DefaultWebsocketTransport),
            admit_policy: None,
            database: Arc::new(MemoryDatabase::default()),
            gossip: None,
            monitor: None,
            opts: ClientOptions::default(),
        }
    }
}

impl ClientBuilder {
    /// New default client builder
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set signer
    ///
    /// # Example
    /// ```rust,no_run
    /// use nostr_sdk::prelude::*;
    ///
    /// // Signer with private keys
    /// let keys = Keys::generate();
    /// let client = ClientBuilder::new().signer(keys).build();
    /// ```
    #[inline]
    pub fn signer<T>(mut self, signer: T) -> Self
    where
        T: IntoNostrSigner,
    {
        self.signer = Some(signer.into_nostr_signer());
        self
    }

    /// Set custom WebSocket transport
    ///
    /// By default [`DefaultWebsocketTransport`] is used.
    #[inline]
    pub fn websocket_transport<T>(mut self, transport: T) -> Self
    where
        T: IntoWebSocketTransport,
    {
        self.websocket_transport = transport.into_transport();
        self
    }

    /// Set an admission policy
    #[inline]
    pub fn admit_policy<T>(mut self, policy: T) -> Self
    where
        T: AdmitPolicy + 'static,
    {
        self.admit_policy = Some(Arc::new(policy));
        self
    }

    /// Set database
    #[inline]
    pub fn database<D>(mut self, database: D) -> Self
    where
        D: IntoNostrDatabase,
    {
        self.database = database.into_nostr_database();
        self
    }

    /// Set a gossip database
    #[inline]
    pub fn gossip<T>(mut self, gossip: T) -> Self
    where
        T: IntoNostrGossip,
    {
        self.gossip = Some(gossip.into_nostr_gossip());
        self
    }

    /// Set monitor
    #[inline]
    pub fn monitor(mut self, monitor: Monitor) -> Self {
        self.monitor = Some(monitor);
        self
    }

    /// Set opts
    #[inline]
    pub fn opts(mut self, opts: ClientOptions) -> Self {
        self.opts = opts;
        self
    }

    /// Build [`Client`]
    #[inline]
    pub fn build(self) -> Client {
        Client::from_builder(self)
    }
}