Crate nostr_sdk

Source
Expand description

High level Nostr client library.

§Nostr SDK

crates.io crates.io - Downloads Documentation CI MIT

§Description

A high-level, Nostr client library written in Rust.

If you’re writing a typical Nostr client or bot, this is likely the crate you need.

However, the crate is designed in a modular way and depends on several other lower-level crates. If you’re attempting something more custom, you might be interested in these.

§Getting started

use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};

use nostr_sdk::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    // Generate new random keys
    let keys = Keys::generate();

    // Or use your already existing (from hex or bech32)
    let keys = Keys::parse("hex-or-bech32-secret-key")?;

    // Show bech32 public key
    let bech32_pubkey: String = keys.public_key().to_bech32()?;
    println!("Bech32 PubKey: {}", bech32_pubkey);

    // Configure client to use proxy for `.onion` relays
    let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 9050));
    let connection: Connection = Connection::new()
        .proxy(addr) // Use `.embedded_tor()` instead to enable the embedded tor client (require `tor` feature)
        .target(ConnectionTarget::Onion);
    let opts = ClientOptions::new().connection(connection);

    // Create new client with custom options
    let client = Client::builder().signer(keys.clone()).opts(opts).build();

    // Add relays
    client.add_relay("wss://relay.damus.io").await?;
    client.add_relay("ws://jgqaglhautb4k6e6i2g34jakxiemqp6z4wynlirltuukgkft2xuglmqd.onion").await?;
    
    // Add read relay
    client.add_read_relay("wss://relay.nostr.info").await?;

    // Connect to relays
    client.connect().await;

    let metadata = Metadata::new()
        .name("username")
        .display_name("My Username")
        .about("Description")
        .picture(Url::parse("https://example.com/avatar.png")?)
        .banner(Url::parse("https://example.com/banner.png")?)
        .nip05("username@example.com")
        .lud16("pay@yukikishimoto.com")
        .custom_field("custom_field", "my value");

    // Update metadata
    client.set_metadata(&metadata).await?;

    // Publish a text note
    let builder = EventBuilder::text_note("My first text note from rust-nostr!");
    client.send_event_builder(builder).await?;

    // Create a POW text note
    let builder = EventBuilder::text_note("POW text note from nostr-sdk").pow(20);
    client.send_event_builder(builder).await?; // Send to all relays
    // client.send_event_builder_to(["wss://relay.damus.io"], builder).await?; // Send to specific relay

    Ok(())
}

More examples can be found in the examples/ directory.

§WASM

This crate supports the wasm32 targets.

An example can be found at nostr-sdk-wasm-example repo.

On macOS, you need to install llvm:

brew install llvm
LLVM_PATH=$(brew --prefix llvm)
AR="${LLVM_PATH}/bin/llvm-ar" CC="${LLVM_PATH}/bin/clang" cargo build --target wasm32-unknown-unknown

NOTE: Currently nip03 feature not support WASM.

§Crate Feature Flags

The following crate feature flags are available:

FeatureDefaultDescription
torNoEnable support for embedded tor client
lmdbNoEnable LMDB storage backend
ndbNoEnable nostrdb storage backend
indexeddbNoEnable Web’s IndexedDb storage backend
all-nipsNoEnable all NIPs
nip03NoEnable NIP-03: OpenTimestamps Attestations for Events
nip04NoEnable NIP-04: Encrypted Direct Message
nip06NoEnable NIP-06: Basic key derivation from mnemonic seed phrase
nip44NoEnable NIP-44: Encrypted Payloads (Versioned)
nip47NoEnable NIP-47: Nostr Wallet Connect
nip49NoEnable NIP-49: Private Key Encryption
nip57NoEnable NIP-57: Zaps
nip59NoEnable NIP-59: Gift Wrap

§Changelog

All notable changes to this library are documented in the CHANGELOG.md.

§State

This library is in an ALPHA state, things that are implemented generally work but the API will change in breaking ways.

§Donations

rust-nostr is free and open-source. This means we do not earn any revenue by selling it. Instead, we rely on your financial support. If you actively use any of the rust-nostr libs/software/services, then please donate.

§License

This project is distributed under the MIT software license - see the LICENSE file for details

Re-exports§

pub use self::client::Client;
pub use self::client::ClientBuilder;
pub use self::client::ClientOptions;

Modules§

client
Client