lib.rs (1272B)
1 use secp256k1::{PublicKey, SecretKey}; 2 use std::str::FromStr; 3 4 pub mod events; 5 pub mod keys; 6 pub mod nips; 7 pub mod nostr_client; 8 pub mod req; 9 pub mod utils; 10 pub mod websocket; 11 12 /// Nostr Identity with secret and public keys 13 pub struct Identity { 14 pub secret_key: SecretKey, 15 pub public_key: PublicKey, 16 pub public_key_str: String, 17 pub address: String, 18 } 19 20 impl FromStr for Identity { 21 type Err = String; 22 23 /// Create an Identity from a secret key as a hex string 24 /// # Example 25 /// ``` 26 /// use nostr_rust::Identity; 27 /// use std::str::FromStr; 28 /// 29 /// // Working format 30 /// let identity = Identity::from_str(env!("SECRET_KEY")); 31 /// assert!(identity.is_ok()); 32 /// 33 /// // Invalid format 34 /// let identity = Identity::from_str("aeaeaeaeae"); 35 /// assert!(identity.is_err()); 36 /// ``` 37 fn from_str(secret_key: &str) -> Result<Self, Self::Err> { 38 let secret_key = keys::secret_key_from_str(secret_key)?; 39 let public_key = keys::get_public_key_from_secret(&secret_key); 40 let address = keys::get_str_keys_from_secret(&secret_key).1; 41 42 Ok(Self { 43 secret_key, 44 public_key, 45 public_key_str: address.to_string(), 46 address, 47 }) 48 } 49 }