nip1.rs (3729B)
1 use crate::{events::EventPrepare, nostr_client::Client, utils::get_timestamp, Identity}; 2 use serde_json::json; 3 4 // Implementation of the NIP1 protocol 5 // https://github.com/nostr-protocol/nips/blob/master/01.md 6 7 impl Client { 8 /// Set the metadata of the identity 9 /// # Example 10 /// # Example 11 /// ```rust 12 /// use nostr_rust::{nostr_client::Client, Identity}; 13 /// use std::str::FromStr; 14 /// let mut client = Client::new(vec!["wss://nostr-pub.wellorder.net"]).unwrap(); 15 /// let identity = Identity::from_str(env!("SECRET_KEY")).unwrap(); 16 /// 17 /// // Here we set the metadata of the identity but not the profile picture one 18 /// client.set_metadata(&identity, Some("Rust Nostr Client"), Some("Automated account for Rust Nostr Client tests :)"), None).unwrap(); 19 /// ``` 20 pub fn set_metadata( 21 &mut self, 22 identity: &Identity, 23 name: Option<&str>, 24 about: Option<&str>, 25 picture: Option<&str>, 26 ) -> Result<(), String> { 27 let mut json_body = json!({}); 28 29 if name.is_none() && about.is_none() && picture.is_none() { 30 return Err("No metadata provided".to_string()); 31 } 32 33 if let Some(name) = name { 34 json_body["name"] = json!(name); 35 } 36 37 if let Some(about) = about { 38 json_body["about"] = json!(about); 39 } 40 41 if let Some(picture) = picture { 42 json_body["picture"] = json!(picture); 43 } 44 45 let event = EventPrepare { 46 pub_key: identity.public_key_str.clone(), 47 created_at: get_timestamp(), 48 kind: 0, 49 tags: vec![], 50 content: json_body.to_string(), 51 } 52 .to_event(identity); 53 54 self.publish_event(&event)?; 55 Ok(()) 56 } 57 58 /// Publish a text note (text_note) event 59 /// # Example 60 /// ```rust 61 /// use nostr_rust::{nostr_client::Client, Identity, utils::get_timestamp}; 62 /// use std::str::FromStr; 63 /// let mut client = Client::new(vec!["wss://nostr-pub.wellorder.net"]).unwrap(); 64 /// let identity = Identity::from_str(env!("SECRET_KEY")).unwrap(); 65 /// let message = format!("Hello Nostr! {}", get_timestamp()); 66 /// client.publish_text_note(&identity, &message, &vec![]).unwrap(); 67 /// ``` 68 pub fn publish_text_note( 69 &mut self, 70 identity: &Identity, 71 content: &str, 72 tags: &[Vec<String>], 73 ) -> Result<(), String> { 74 let event = EventPrepare { 75 pub_key: identity.public_key_str.clone(), 76 created_at: get_timestamp(), 77 kind: 1, 78 tags: tags.to_vec(), 79 content: content.to_string(), 80 } 81 .to_event(identity); 82 83 self.publish_event(&event)?; 84 Ok(()) 85 } 86 87 /// Add recommended relay server 88 /// # Example 89 /// ```rust 90 /// use nostr_rust::{nostr_client::Client, Identity}; 91 /// use std::str::FromStr; 92 /// let mut client = Client::new(vec!["wss://nostr-pub.wellorder.net"]).unwrap(); 93 /// let identity = Identity::from_str(env!("SECRET_KEY")).unwrap(); 94 /// 95 /// // Here we set the recommended relay server to the one hosted by Wellorder 96 /// client.add_recommended_relay(&identity, "wss://relay.damus.io").unwrap(); 97 /// ``` 98 pub fn add_recommended_relay( 99 &mut self, 100 identity: &Identity, 101 relay: &str, 102 ) -> Result<(), String> { 103 let event = EventPrepare { 104 pub_key: identity.public_key_str.clone(), 105 created_at: get_timestamp(), 106 kind: 2, 107 tags: vec![], 108 content: relay.to_string(), 109 } 110 .to_event(identity); 111 112 self.publish_event(&event)?; 113 Ok(()) 114 } 115 }