Rust implementation of Nostr protocol.
You may be interested in:
nostr-sdk
if you want to write a typical Nostr client or botnostr-database
if you need a database for your Nostr app (native or web)
[dependencies]
nostr = "0.25"
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("[email protected]")
.lud16("[email protected]")
.custom_field("custom_field", "my value");
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.
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.
This crate support no_std
environments.
Check the example in the embedded/ directory.
The following crate feature flags are available:
Feature | Default | Description |
---|---|---|
std |
Yes | Enable std library |
alloc |
No | Needed to use this library in no_std context |
blocking |
No | Needed to use NIP-05 and NIP-11 features in not async/await context |
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 |
nip44 |
Yes | Enable NIP-44: Encrypted Payloads (Versioned) |
nip46 |
Yes | Enable NIP-46: Nostr Connect |
nip47 |
Yes | Enable NIP-47: Nostr Wallet Connect |
This library is in an ALPHA state, things that are implemented generally work but the API will change in breaking ways.
This project is distributed under the MIT software license - see the LICENSE file for details
⚡ Tips: https://getalby.com/p/yuki
⚡ Lightning Address: [email protected]