NostrO2
Simple yet powerful Rust tools for interacting with the Nostr ecosystem.
Built on top of serde and tokio for easy integration with other Rust libraries.
Supports WASM compilation for use in web applications.
Features
The library provides redy to use types for the main data structures of the Nostr protocol, as well as a simple interface for interacting with a relay, or a more complex interface for creating a relay pool.
NostrNotes
The main data structures of Nostr, as defined by NIP-01.
NostrNotes can be created using default helpers like this:
let note = NostrNote ;
NostrKeypair
Can be created from a private key str or a mnemonic phrase and will allow you to sign Nostr Notes.
let new_user = "<64-bit hex string>"..expect;
let mut note = NostrNote ;
user_key_pair.sign_nostr_event; // -> Modifies the note in place, adds pubkey, id and sig.
Subscriptions
Create a new NostrSubscription using the default constructor and then add filters to it.
Filters correspond to the object described by NIP-01.
All fields are represented as options and skipped in serialization if None.
let subscription =
NostrFilter ;
let relay_event = pool.send?;
println!;
NostrRelay
Ready-to-go connection to a relay. The NostrRelay has two main parts, a reader and a writer.
The writer is reference counted and can be cloned to send events to the relay across multiple threads.
The reader is a single stream that can be used to receive events from the relay.
NostrRelay also holds its url and connection state internally.
let mut relay = new.await?;
let filter = NostrSubscription
.relay_subscription;
let id = relay.writer.subscribe.await?;
_debug;
let mut finished = Stringnew;
let mut ws_stream = relay.relay_event_stream?;
while let Some = ws_stream.next.await
Relay Pool
The NostrRelayPool is a more complex interface for managing multiple relays. It's created with a list of relay urls
and will connect to all relays concurrently. The pool will then manage the connections and distribute events
to the relays in a round-robin fashion.
Unique NostrNotes are kep in a library and duplicate notes are filtered out.
The pool holds a RelayTable that keeps the connection state of each relay.
let mut pool = new
.await
.expect;
let filter = NostrSubscription
.relay_subscription;
pool.subscribe
.await
.expect;
let mut events = vec!;
while let Some = pool.listener.recv.await
Nostr Authentication
The NostrNotes objects also provide a verification method for both content and signatures.
assert_eq!;
Installation
Run cargo add nostro2 to get the latest version.
You can also add nostro2 to your Cargo.toml dependencies:
[]
= "0.2.0"