use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fmt;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NostrConfig {
pub private_key: String,
pub relays: Vec<String>,
pub metadata: Option<NostrMetadata>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NostrMetadata {
pub name: Option<String>,
pub about: Option<String>,
pub picture: Option<String>,
}
#[derive(Debug, Clone)]
pub struct NostrUserProfile {
pub public_key: String,
pub private_key: String,
pub metadata: Option<NostrMetadata>,
}
impl NostrUserProfile {
pub async fn subscribe_with_key(
private_key: &str,
_relays: Option<Vec<String>>,
) -> Result<Self, Box<dyn Error>> {
let public_key = format!("npub{}", &private_key[4..]);
Ok(NostrUserProfile {
public_key,
private_key: private_key.to_string(),
metadata: None,
})
}
pub fn to_nsec(&self) -> Result<String, Box<dyn Error>> {
Ok(self.private_key.clone())
}
}
pub struct NostrClient {
#[allow(dead_code)] config: NostrConfig,
connected: bool,
}
impl NostrClient {
pub async fn new(config: NostrConfig) -> Result<Self, Box<dyn Error>> {
Ok(NostrClient {
config,
connected: false,
})
}
pub async fn connect(&mut self) -> Result<(), Box<dyn Error>> {
self.connected = true;
Ok(())
}
pub async fn send_message(
&self,
content: &str,
recipient: Option<&str>,
) -> Result<String, Box<dyn Error>> {
if !self.connected {
return Err("Not connected to relays".into());
}
let event_id = format!("event_{}", uuid::Uuid::new_v4());
println!(
"Sent Nostr message: {} -> {}",
content,
recipient.unwrap_or("public")
);
Ok(event_id)
}
pub async fn publish_note(&self, content: &str) -> Result<String, Box<dyn Error>> {
self.send_message(content, None).await
}
pub async fn send_dm(&self, recipient: &str, content: &str) -> Result<String, Box<dyn Error>> {
self.send_message(content, Some(recipient)).await
}
}
impl Default for NostrConfig {
fn default() -> Self {
Self {
private_key: "nsec1example".to_string(),
relays: vec![
"wss://relay.damus.io".to_string(),
"wss://relay.nostr.info".to_string(),
"wss://nostr-pub.wellorder.net".to_string(),
],
metadata: None,
}
}
}
#[derive(Debug)]
pub enum EnterpriseError {
Connection(String),
Authentication(String),
Message(String),
Relay(String),
}
impl fmt::Display for EnterpriseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EnterpriseError::Connection(msg) => write!(f, "Connection error: {msg}"),
EnterpriseError::Authentication(msg) => write!(f, "Authentication error: {msg}"),
EnterpriseError::Message(msg) => write!(f, "Message error: {msg}"),
EnterpriseError::Relay(msg) => write!(f, "Relay error: {msg}"),
}
}
}
impl Error for EnterpriseError {}