use events::{Event, EventPrepare};
use secp256k1::{PublicKey, SecretKey};
use std::str::FromStr;
use utils::get_timestamp;
pub mod bech32;
pub mod events;
pub mod keys;
pub mod nips;
pub mod nostr_client;
pub mod req;
pub mod utils;
pub mod websocket;
pub const DEFAULT_HASHTAG: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
pub type Message = tungstenite::Message;
pub struct Identity {
pub secret_key: SecretKey,
pub public_key: PublicKey,
pub public_key_str: String,
pub address: String,
}
impl Identity {
pub fn make_event(
&self,
kind: u16,
content: &str,
tags: &[Vec<String>],
difficulty_target: u16,
) -> Event {
EventPrepare {
pub_key: self.public_key_str.clone(),
created_at: get_timestamp(),
kind,
tags: tags.to_vec(),
content: content.to_string(),
}
.to_event(self, difficulty_target)
}
}
impl FromStr for Identity {
type Err = String;
fn from_str(secret_key: &str) -> Result<Self, Self::Err> {
let secret_key = keys::secret_key_from_str(&{
if secret_key.starts_with("nsec") {
crate::bech32::from_hb_to_hex(crate::bech32::ToBech32Kind::SecretKey, secret_key)
.unwrap()
} else {
secret_key.to_string()
}
})?;
let public_key = keys::get_public_key_from_secret(&secret_key);
let address = keys::get_str_keys_from_secret(&secret_key).1;
Ok(Self {
secret_key,
public_key,
public_key_str: address.to_string(),
address,
})
}
}