Expand description
Rust implementation of the Nostr protocol.
Nostr
Description
Rust implementation of Nostr protocol.
If you’re writing a typical Nostr client or bot, you may be interested in nostr-sdk.
Getting started
[dependencies]
nostr = "=0.23.0-bitcoin-v0.29"
use nostr::prelude::*;
fn main() -> Result<()> {
// Generate new random keys
let my_keys = Keys::generate();
// or use your already existing
//
// From HEX or Bech32
// let my_keys = Keys::from_sk_str("hex-or-bech32-secret-key")?;
// Show bech32 public key
let bech32_pubkey: String = my_keys.public_key().to_bech32()?;
println!("Bech32 PubKey: {}", bech32_pubkey);
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("yuki@getalby.com")
.custom_field("custom_field", Value::String("my value".into()));
let event: Event = EventBuilder::set_metadata(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)?;
// Convert client nessage to JSON
let json = ClientMessage::new_event(event).as_json();
println!("{json}");
Ok(())
}More examples can be found in the examples/ directory.
WASM
This crate supports the wasm32 targets.
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:
| Feature | Default | Description |
|---|---|---|
blocking | No | Needed to use NIP-05 and NIP-11 features in not async/await context |
vanity | No | Enable vanity public key mining module |
all-nips | Yes | Enable all NIPs |
nip03 | No | Enable NIP-03: OpenTimestamps Attestations for Events |
nip04 | Yes | Enable NIP-04: Encrypted Direct Message |
nip05 | Yes | Enable NIP-05: Mapping Nostr keys to DNS-based internet identifiers |
nip06 | Yes | Enable NIP-06: Basic key derivation from mnemonic seed phrase |
nip11 | Yes | Enable NIP-11: Relay Information Document |
nip19 | Yes | Enable NIP-19: bech32-encoded entities |
nip21 | Yes | Enable NIP-21: nostr URI scheme |
nip46 | Yes | Enable NIP-46: Nostr Connect |
nip47 | Yes | Enable NIP-47: Nostr Wallet Connect |
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
Donations
⚡ Tips: https://getalby.com/p/yuki
⚡ Lightning Address: yuki@getalby.com
Re-exports
pub use self::event::tag::ExternalIdentity;pub use self::event::tag::HttpMethod;pub use self::event::tag::Identity;pub use self::event::tag::ImageDimensions;pub use self::event::tag::Marker;pub use self::event::tag::RelayMetadata;pub use self::event::tag::Report;pub use self::event::tag::Tag;pub use self::event::tag::TagKind;pub use self::event::Event;pub use self::event::EventBuilder;pub use self::event::EventId;pub use self::event::Kind;pub use self::event::UnsignedEvent;pub use self::key::Keys;pub use self::message::Alphabet;pub use self::message::ClientMessage;pub use self::message::Filter;pub use self::message::RelayMessage;pub use self::message::SubscriptionId;pub use self::types::ChannelId;pub use self::types::Contact;pub use self::types::Entity;pub use self::types::Metadata;pub use self::types::Profile;pub use self::types::Timestamp;pub use self::types::UncheckedUrl;pub use bech32;pub use bip39;pub use bitcoin;pub use bitcoin_hashes as hashes;pub use secp256k1;pub use serde_json;pub use url;
Modules
- Event
- Keys
- Messages
- NIPs
- Prelude
- Types
Structs
- A parsed URL record.
Statics
- A global static context to avoid repeatedly creating contexts.
Type Definitions
- Result