Skip to main content

Crate nostro2

Crate nostro2 

Source
Expand description

§NostrO2

Simple yet powerful Rust library for the Nostr protocol.

nostro2 provides the core data structures and types for working with Nostr, as defined in NIP-01. It focuses on type safety, ergonomics, and performance with full serde support and zero-copy operations where possible.

§Quick Start

§Creating Notes

use nostro2::NostrNote;

// Simple text note
let note = NostrNote::text_note("Hello, Nostr!");

// Using the builder
let note = NostrNote::builder()
    .content("Hello, Nostr!")
    .kind(1)
    .tag_pubkey("abc123...")
    .build();

// Metadata note
let metadata = r#"{"name":"Alice","about":"Nostr user"}"#;
let note = NostrNote::metadata(metadata);

§Creating Subscriptions

use nostro2::NostrSubscription;

// Filter for text notes from specific authors
let filter = NostrSubscription::new()
    .kind(1)
    .author("pubkey1...")
    .author("pubkey2...")
    .limit(10);

§Working with Tags

use nostro2::NostrTags;

let tags = NostrTags::new()
    .with_pubkey("abc123...", None)
    .with_event("event123...")
    .with_tag("t", "nostr");

// Tags behave like Vec
assert_eq!(tags.len(), 3);

§Validation

use nostro2::validation;

if validation::is_valid_pubkey("abc123...") {
    // Valid hex-encoded public key
}

§Features

  • NIP-01 Data Structures: Complete implementation of core Nostr types
  • Serde Support: Full serialization/deserialization with serde
  • Builder Patterns: Ergonomic APIs for constructing notes and filters
  • Type Safety: Strong typing with comprehensive error handling
  • WASM Compatible: Works in browser environments
  • Zero-Copy Operations: Non-allocating variants for performance

§Error Handling

The crate uses a Result type alias for convenience:

use nostro2::{NostrNote, Result};

fn create_note() -> Result<NostrNote> {
    let mut note = NostrNote::text_note("Hello");
    note.serialize_id()?;
    Ok(note)
}

Modules§

errors
Error types for the nostro2 crate
validation
Validation helpers for Nostr data

Structs§

NostrNote
A Nostr note (event) as defined by NIP-01
NostrNoteBuilder
Builder for constructing NostrNote instances
NostrSubscription
Subscription filter for querying Nostr events
NostrTags
Collection of tags attached to a Nostr note

Enums§

NostrClientEvent
NostrRelayEvent
NostrTag
Tag type identifiers for Nostr protocol
RelayEventTag

Traits§

NostrSigner

Type Aliases§

Result
Convenience type alias for Results with NostrErrors