Struct nostr_sdk::client::Client

source ·
pub struct Client { /* private fields */ }

Implementations

Create a new Client

Example
use nostr_sdk::Client;

let my_keys = Client::generate_keys();
let mut client = Client::new(&my_keys);

Generate new random keys using entorpy from OS

Get new notification listener

Add new relay

Example
client.add_relay("wss://relay.nostr.info", None)?;
client.add_relay("wss://relay.damus.io", None)?;

Disconnect and remove relay

Example
client.remove_relay("wss://relay.nostr.info").await?;

Connect relay

Example
client.connect_relay("wss://relay.nostr.info", true).await?;

Disconnect relay

Example
client.disconnect_relay("wss://relay.nostr.info").await?;

Connect to all added relays without waiting for connection and keep connection alive

Example
client.connect().await?;

Connect to all added relays waiting for initial connection and keep connection alive

Example
client.connect_and_wait().await?;

Disconnect from all relays

Example
client.disconnect().await?;

Subscribe to filters

Example
use nostr::SubscriptionFilter;

let subscription = SubscriptionFilter::new()
    .pubkeys(vec![my_keys.public_key()])
    .since(Utc::now());

client.subscribe(vec![subscription]).await.unwrap();

Send event

Update profile metadata

https://github.com/nostr-protocol/nips/blob/master/01.md

Example
use nostr_sdk::nostr::Metadata;

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

client.update_profile(metadata).await.unwrap();

Publish text note

https://github.com/nostr-protocol/nips/blob/master/01.md

Example
client.publish_text_note("My first text note from Nostr SDK!", &[]).await.unwrap();

Publish POW text note

https://github.com/nostr-protocol/nips/blob/master/13.md

Example
client.publish_pow_text_note("My first POW text note from Nostr SDK!", &[], 16).await.unwrap();

Add recommended relay

https://github.com/nostr-protocol/nips/blob/master/01.md

Example
client.add_recommended_relay("wss://relay.damus.io").await.unwrap();

Set contact list

https://github.com/nostr-protocol/nips/blob/master/02.md

Example
use nostr::Contact;
use nostr::key::{Keys, FromBech32};

let list = vec![
    Contact::new(
        "my_first_contact",
        Keys::from_bech32_public_key("npub1...").unwrap().public_key(),
        "wss://relay.damus.io",
    ),
];
client.set_contact_list(list).await.unwrap();

Get contact list

https://github.com/nostr-protocol/nips/blob/master/02.md

Example
let list = client.get_contact_list().await.unwrap();

Send encrypted direct message

https://github.com/nostr-protocol/nips/blob/master/04.md

Example
use nostr::key::{Keys, FromBech32};
use nostr_sdk::Client;

let my_keys = Client::generate_keys();
let mut client = Client::new(&my_keys);

client.add_relay("wss://relay.nostr.info", None).unwrap();
client.connect().await.unwrap();

let alice_keys = Keys::from_bech32_public_key("npub1...").unwrap();
client.send_direct_msg(alice_keys, "My first DM fro Nostr SDK!").await.unwrap();

Like event

https://github.com/nostr-protocol/nips/blob/master/25.md

Example
use nostr::Event;

let event = Event::from_json(r#"{
    "pubkey":"a8e76c3ace7829f9ee44cf9293309e21a1824bf1e57631d00685a1ed0b0bd8a2",
    "content":"🔥 78,680 blocks to the next Halving 🔥",
    "id":"3aded8d2194dc2fedb1d7b70480b43b6c4deb0a22dcdc9c471d1958485abcf21",
    "created_at":1667337749,
    "sig":"96e0a125e15ecc889757a1b517fdab0223a9ceae22d2591536b5f5186599b50cb1c5f20c2d0d06cdd5cd75368529e33bac4fcd3b321db9865d47785b95b72625",
    "kind":1,
    "tags":[]
}"#).unwrap();

client.like(event).await.unwrap();

Disike event

https://github.com/nostr-protocol/nips/blob/master/25.md

Example
use nostr::Event;

let event = Event::from_json(r#"{
    "pubkey":"a8e76c3ace7829f9ee44cf9293309e21a1824bf1e57631d00685a1ed0b0bd8a2",
    "content":"🔥 78,680 blocks to the next Halving 🔥",
    "id":"3aded8d2194dc2fedb1d7b70480b43b6c4deb0a22dcdc9c471d1958485abcf21",
    "created_at":1667337749,
    "sig":"96e0a125e15ecc889757a1b517fdab0223a9ceae22d2591536b5f5186599b50cb1c5f20c2d0d06cdd5cd75368529e33bac4fcd3b321db9865d47785b95b72625",
    "kind":1,
    "tags":[]
}"#).unwrap();
  
client.dislike(event).await.unwrap();

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more