pub struct Client { /* private fields */ }
Implementations
sourceimpl Client
impl Client
sourcepub fn new(keys: &Keys) -> Self
pub fn new(keys: &Keys) -> Self
Create a new Client
Example
use nostr_sdk::Client;
let my_keys = Client::generate_keys();
let mut client = Client::new(&my_keys);
sourcepub fn generate_keys() -> Keys
pub fn generate_keys() -> Keys
Generate new random keys using entorpy from OS
sourcepub fn notifications(&self) -> Receiver<RelayPoolNotifications>
pub fn notifications(&self) -> Receiver<RelayPoolNotifications>
Get new notification listener
sourcepub fn add_relay(&mut self, url: &str, proxy: Option<SocketAddr>) -> Result<()>
pub fn add_relay(&mut self, url: &str, proxy: Option<SocketAddr>) -> Result<()>
Add new relay
Example
client.add_relay("wss://relay.nostr.info", None)?;
client.add_relay("wss://relay.damus.io", None)?;
sourcepub async fn remove_relay(&mut self, url: &str) -> Result<()>
pub async fn remove_relay(&mut self, url: &str) -> Result<()>
sourcepub async fn disconnect_relay(&mut self, url: &str) -> Result<()>
pub async fn disconnect_relay(&mut self, url: &str) -> Result<()>
sourcepub async fn connect(&mut self) -> Result<()>
pub async fn connect(&mut self) -> Result<()>
Connect to all added relays without waiting for connection and keep connection alive
Example
client.connect().await?;
sourcepub async fn connect_and_wait(&mut self) -> Result<()>
pub async fn connect_and_wait(&mut self) -> Result<()>
Connect to all added relays waiting for initial connection and keep connection alive
Example
client.connect_and_wait().await?;
sourcepub async fn disconnect(&mut self) -> Result<()>
pub async fn disconnect(&mut self) -> Result<()>
sourcepub async fn subscribe(&mut self, filters: Vec<SubscriptionFilter>) -> Result<()>
pub async fn subscribe(&mut self, filters: Vec<SubscriptionFilter>) -> Result<()>
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();
sourcepub async fn send_event(&self, event: Event) -> Result<()>
pub async fn send_event(&self, event: Event) -> Result<()>
Send event
sourcepub async fn update_profile(&self, metadata: Metadata) -> Result<()>
pub async fn update_profile(&self, metadata: Metadata) -> Result<()>
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();
sourcepub async fn publish_text_note(&self, content: &str, tags: &[Tag]) -> Result<()>
pub async fn publish_text_note(&self, content: &str, tags: &[Tag]) -> Result<()>
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();
sourcepub async fn publish_pow_text_note(
&self,
content: &str,
tags: &[Tag],
difficulty: u8
) -> Result<()>
pub async fn publish_pow_text_note(
&self,
content: &str,
tags: &[Tag],
difficulty: u8
) -> Result<()>
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();
sourcepub async fn add_recommended_relay(&self, url: &str) -> Result<()>
pub async fn add_recommended_relay(&self, url: &str) -> Result<()>
Add recommended relay
https://github.com/nostr-protocol/nips/blob/master/01.md
Example
client.add_recommended_relay("wss://relay.damus.io").await.unwrap();
sourcepub async fn set_contact_list(&self, list: Vec<Contact>) -> Result<()>
pub async fn set_contact_list(&self, list: Vec<Contact>) -> Result<()>
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();
sourcepub async fn get_contact_list(&mut self) -> Result<Vec<Contact>>
pub async fn get_contact_list(&mut self) -> Result<Vec<Contact>>
Get contact list
https://github.com/nostr-protocol/nips/blob/master/02.md
Example
let list = client.get_contact_list().await.unwrap();
sourcepub async fn send_direct_msg(&self, recipient: &Keys, msg: &str) -> Result<()>
pub async fn send_direct_msg(&self, recipient: &Keys, msg: &str) -> Result<()>
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();
sourcepub async fn delete_event(&self, event_id: &str) -> Result<()>
pub async fn delete_event(&self, event_id: &str) -> Result<()>
sourcepub async fn like(&self, event: &Event) -> Result<()>
pub async fn like(&self, event: &Event) -> Result<()>
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();
sourcepub async fn dislike(&self, event: &Event) -> Result<()>
pub async fn dislike(&self, event: &Event) -> Result<()>
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();
sourcepub async fn new_channel(
&self,
name: &str,
about: Option<&str>,
picture: Option<&str>
) -> Result<()>
pub async fn new_channel(
&self,
name: &str,
about: Option<&str>,
picture: Option<&str>
) -> Result<()>
Create new channel
sourcepub async fn update_channel(
&self,
channel_id: Hash,
relay_url: Url,
name: Option<&str>,
about: Option<&str>,
picture: Option<&str>
) -> Result<()>
pub async fn update_channel(
&self,
channel_id: Hash,
relay_url: Url,
name: Option<&str>,
about: Option<&str>,
picture: Option<&str>
) -> Result<()>
Update channel metadata
sourcepub async fn send_channel_msg(
&self,
channel_id: Hash,
relay_url: Url,
msg: &str
) -> Result<()>
pub async fn send_channel_msg(
&self,
channel_id: Hash,
relay_url: Url,
msg: &str
) -> Result<()>
Send message to channel
sourcepub async fn hide_channel_msg(&self, message_id: Hash, reason: &str) -> Result<()>
pub async fn hide_channel_msg(&self, message_id: Hash, reason: &str) -> Result<()>
Hide channel message
sourcepub async fn mute_channel_user(
&self,
pubkey: XOnlyPublicKey,
reason: &str
) -> Result<()>
pub async fn mute_channel_user(
&self,
pubkey: XOnlyPublicKey,
reason: &str
) -> Result<()>
Mute channel user