Crate nostr

source ·
Expand description

Nostr

crates.io Documentation MIT

Description

Rust implementation of Nostr protocol.

Getting started

[dependencies]
anyhow = "1"
nostr = "0.5"
tungstenite = { version = "0.17", features = ["rustls-tls-webpki-roots"]}
url = "2"
use std::str::FromStr;
use nostr::{Event, EventBuilder, Metadata};
use nostr::key::{FromBech32, Keys};
use nostr::message::ClientMessage;
use tungstenite::{Message as WsMessage};
use url::Url;

fn main() -> anyhow::Result<()> {
    // Generate new random keys
    let my_new_keys = Keys::generate_from_os_random();

    // Use your already existing bec32 keys
    let my_bech32_keys = Keys::from_bech32("nsec1...")?;

    // Use your already existing keys
    let my_keys = Keys::from_str("hex-secret-key")?;

    let metadata = Metadata::new()
        .name("username")
        .display_name("My Username")
        .about("Description")
        .picture(Url::from_str("https://example.com/avatar.png")?)
        .nip05("username@example.com");

    let event: Event = EventBuilder::set_metadata(&my_keys, metadata)?.to_event(&my_keys)?;

    // New text note
    let event: Event = EventBuilder::new_text_note("Hello from Nostr SDK", &[]).to_event(&my_keys)?;

    // New POW text note
    let event: Event = EventBuilder::new_text_note("My first POW text note from Nostr SDK", &[]).to_pow_event(&my_keys, 20)?;

    // Connect to relay
    let (mut socket, _) = tungstenite::connect(Url::parse("wss://relay.damus.io")?).expect("Can't connect to relay");

    // Send msg
    let msg = ClientMessage::new_event(event).to_json();
    socket.write_message(WsMessage::Text(msg))?;

    Ok(())
}

More examples can be found in the examples directory.

Crate Feature Flags

The following crate feature flags are available:

FeatureDefaultDescription
nip06YesEnable NIP-06: Basic key derivation from mnemonic seed phrase

Supported NIPs

State

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

License

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

Re-exports

pub use self::contact::Contact;
pub use self::event::Event;
pub use self::event::EventBuilder;
pub use self::event::Kind;
pub use self::event::KindBase;
pub use self::event::Tag;
pub use self::key::Keys;
pub use self::message::ClientMessage;
pub use self::message::RelayMessage;
pub use self::message::SubscriptionFilter;
pub use self::metadata::Metadata;

Modules