//! Read and write path shared by every synced sidebar document (kind:30078, //! NIP-44 encrypted to yourself): one verified head read, one signed publish. use std::time::SystemTime; use buzz_core::kind::KIND_READ_STATE; use nostr::{EventBuilder, Kind, Tag, Timestamp}; use serde_json::{json, Value}; use super::parse_write_response; use super::sidebar_doc::{self, Codec, Edit, Stamp, Tree}; use crate::client::BuzzClient; use crate::error::CliError; /// Existing byte budget for the other sidebar documents. const MAX_CIPHERTEXT_BYTES: usize = 65_535; /// A 500-entry channel flag record can exceed the old ciphertext budget /// after millisecond timestamps, NIP-44 padding, and base64. The relay /// accepts the full short-form NIP-44 envelope for these records. const FLAG_MAX_CIPHERTEXT_BYTES: usize = 87_472; /// One synced document: its `d` tag and the name used in messages. #[derive(Clone, Copy)] pub(crate) struct Lane { pub d_tag: &'static str, pub label: &'static str, } /// The decoded head. `doc` is `None` when the relay holds no event yet. pub(crate) struct Head { pub created_at: u64, pub doc: Option, } pub(crate) fn now() -> std::time::Duration { SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .unwrap_or_default() } /// Channel ids in the lowercase hyphenated form desktop keys entries by. pub(crate) fn channel_ids(channels: &[String]) -> Result, CliError> { channels .iter() .map(|channel| { uuid::Uuid::parse_str(channel.trim()) .map(|id| id.hyphenated().to_string()) .map_err(|_| CliError::Usage(format!("invalid channel UUID: {channel}"))) }) .collect() } /// The relay's current verified `lane` event for this identity, read from the /// writer. `None` when the relay holds none; an error when it returns only /// events that are not yours or fail verification. async fn head_event(client: &BuzzClient, lane: Lane) -> Result, CliError> { let me = client.public_key(); let filter = json!({ "kinds": [KIND_READ_STATE], "authors": [me.to_hex()], "#d": [lane.d_tag], "limit": 1, "consistency": "strong", }); let raw = client.query(&filter).await?; let events: Vec = serde_json::from_str(&raw) .map_err(|e| CliError::Other(format!("failed to parse {} query: {e}", lane.label)))?; let returned = events.len(); let head = events .into_iter() .filter_map(|event| serde_json::from_value::(event).ok()) .filter(|event| { event.kind.as_u16() == KIND_READ_STATE as u16 && event.pubkey == me && event.tags.identifier() == Some(lane.d_tag) && event.verify().is_ok() }) .min_by(|a, b| { b.created_at .cmp(&a.created_at) .then_with(|| a.id.to_hex().cmp(&b.id.to_hex())) }); if head.is_none() && returned > 0 { return Err(CliError::Other(format!( "the relay returned a {} event that is not yours or fails verification; refusing to overwrite it", lane.label ))); } Ok(head) } /// The relay's current event for `lane` and this identity, decrypted and /// passed to `decode` with the event's `created_at`. An absent head is /// `doc: None`; an unreadable one is an error, never an empty document. The /// read is pinned to the writer (`consistency: strong`): a write derives a /// full snapshot from it, and a lagging replica would drop the caller's own /// just-accepted edits. pub(crate) async fn fetch_head( client: &BuzzClient, lane: Lane, decode: impl FnOnce(&Value, u64) -> Option, ) -> Result, CliError> { let Some(event) = head_event(client, lane).await? else { return Ok(Head { created_at: 0, doc: None, }); }; let me = client.public_key(); let unreadable = |detail: String| { CliError::Other(format!( "your {} event {} is unreadable ({detail}); refusing to read or overwrite it", lane.label, event.id.to_hex() )) }; let plaintext = client .credentials() .nip44_decrypt(&me, &event.content) .await? .ok_or_else(|| unreadable("not valid ciphertext for this key".into()))?; let json: Value = serde_json::from_str(&plaintext).map_err(|e| unreadable(e.to_string()))?; let created_at = event.created_at.as_secs(); let doc = decode(&json, created_at).ok_or_else(|| { unreadable("unsupported format; a newer client may have written it".into()) })?; Ok(Head { created_at, doc: Some(doc), }) } /// Encrypts `plaintext` to yourself and publishes it as the new head, one /// second after `head_created_at` at the earliest. A relay `duplicate:` reply /// is a conflict unless the head is this event: `submit_event` retries an /// ambiguous attempt with the same signed event, which the relay then reports /// as a duplicate of the copy it already stored. pub(crate) async fn publish( client: &BuzzClient, lane: Lane, plaintext: String, head_created_at: u64, ) -> Result { let me = client.public_key(); let too_large = || { CliError::Usage(format!( "{} would exceed the relay's event size limit", lane.label )) }; // NIP-44 rejects longer plaintexts outright. if plaintext.len() > u16::MAX as usize { return Err(too_large()); } let content = client .credentials() .nip44_encrypt(&me, &plaintext) .await .map_err(|e| CliError::Other(format!("failed to encrypt {}: {e}", lane.label)))?; let ciphertext_limit = match lane.d_tag { "channel-stars" | "channel-mutes" => FLAG_MAX_CIPHERTEXT_BYTES, _ => MAX_CIPHERTEXT_BYTES, }; if content.len() > ciphertext_limit { return Err(too_large()); } let created_at = now().as_secs().max(head_created_at + 1); let builder = EventBuilder::new(Kind::Custom(KIND_READ_STATE as u16), content) .tags([Tag::identifier(lane.d_tag), Tag::hashtag(lane.d_tag)]) .custom_created_at(Timestamp::from(created_at)); let event = client.sign_event(builder).await?; let event_id = event.id; let raw = client.submit_event(event).await?; let normalized = match parse_write_response( &raw, &format!("a newer {} update won; run the command again", lane.label), ) { Err(CliError::Conflict(conflict)) => match head_event(client, lane).await? { Some(head) if head.id == event_id => { json!({"event_id": event_id.to_hex(), "accepted": true, "message": ""}).to_string() } _ => return Err(CliError::Conflict(conflict)), }, other => other?, }; serde_json::from_str(&normalized) .map_err(|e| CliError::Other(format!("relay response is not JSON: {e}"))) } /// The current register-tree document of `lane` and the `created_at` of its /// head; an absent head is an empty tree at `created_at` 0. pub(crate) async fn fetch_tree( client: &BuzzClient, lane: Lane, codec: &Codec, ) -> Result<(u64, Tree), CliError> { let head = fetch_head(client, lane, |json, created_at| { let legacy_v = i64::try_from(created_at.saturating_mul(1_000)).unwrap_or(i64::MAX); sidebar_doc::decode(codec, json, legacy_v) }) .await?; Ok((head.created_at, head.doc.unwrap_or_default())) } /// Reads the head tree, applies `apply` (which sees the unedited head), and /// publishes the result once. `check` rejects an edited tree over the /// document's limits. Returns the write response, or an `unchanged` response /// without publishing. pub(crate) async fn edit_tree( client: &BuzzClient, lane: Lane, codec: &Codec, check: impl FnOnce(&Tree) -> Result<(), CliError>, apply: impl FnOnce(&mut Edit, &Tree) -> Result<(), CliError>, ) -> Result { let (head_created_at, head) = fetch_tree(client, lane, codec).await?; let mut tree = head.clone(); let stamp = Stamp { now_ms: i64::try_from(now().as_millis()).unwrap_or(i64::MAX), dev: format!("{:016x}", rand::random::()), }; apply(&mut Edit::new(&mut tree, &stamp), &head)?; if tree == head { return Ok(unchanged()); } check(&tree)?; let plaintext = serde_json::to_string(&sidebar_doc::encode(codec, &tree)) .map_err(|e| CliError::Other(format!("failed to encode {}: {e}", lane.label)))?; publish(client, lane, plaintext, head_created_at).await } /// The response of a command that changed nothing and published nothing. pub(crate) fn unchanged() -> Value { json!({"event_id": null, "accepted": true, "message": "unchanged"}) } /// Shared fake relay and signing helpers for the sidebar command tests. #[cfg(test)] pub(crate) mod test_support { use super::*; use crate::credentials::Credentials; use axum::{body::Bytes, extract::State, routing::post, Json, Router}; use nostr::nips::nip44; use nostr::Keys; use std::sync::{Arc, Mutex}; use tokio::net::TcpListener; #[derive(Default)] pub struct Relay { pub events: Vec, pub duplicate: bool, /// Store the event but answer `duplicate:`, as the relay answers a /// retry of an attempt it already stored. pub stored_duplicate: bool, /// Answer every query with all stored events, whoever authored them. pub leaky: bool, } pub type Shared = Arc>; /// Fake relay: `/query` checks the head filter and returns every stored /// event matching its author and requested d-tag (more than the relay's /// `limit`, so head selection is exercised); `/events` stores one. async fn fake_relay(relay: Shared) -> String { let app = Router::new() .route( "/query", post(|State(relay): State, body: Bytes| async move { let filters: Vec = serde_json::from_slice(&body).unwrap(); let [filter] = filters.as_slice() else { panic!("one filter expected: {filters:?}") }; assert_eq!(filter["kinds"], json!([KIND_READ_STATE])); assert_eq!(filter["limit"], json!(1)); assert_eq!(filter["consistency"], json!("strong")); let author = &filter["authors"][0]; let d_tag = &filter["#d"][0]; let relay = relay.lock().unwrap(); let matching: Vec<_> = relay .events .iter() .filter(|e| { relay.leaky || (&e["pubkey"] == author && &e["tags"][0][1] == d_tag) }) .cloned() .collect(); Json(json!(matching)) }), ) .route( "/events", post(|State(relay): State, body: Bytes| async move { let event: Value = serde_json::from_slice(&body).unwrap(); let mut relay = relay.lock().unwrap(); let message = if relay.duplicate || relay.stored_duplicate { "duplicate:" } else { "" }; if !relay.duplicate { relay.events.push(event.clone()); } Json(json!({"event_id": event["id"], "accepted": true, "message": message})) }), ) .with_state(relay); let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); let addr = listener.local_addr().unwrap(); tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); format!("http://{addr}") } pub async fn setup() -> (BuzzClient, Shared) { let relay = Shared::default(); let url = fake_relay(relay.clone()).await; let client = BuzzClient::new(url, Credentials::from_keys(Keys::generate())).unwrap(); (client, relay) } pub fn seed_at( client: &BuzzClient, relay: &Shared, lane: Lane, plaintext: &str, created_at: u64, ) -> Value { let keys = client.credentials().test_keys(); let content = nip44::encrypt( keys.secret_key(), &keys.public_key(), plaintext, nip44::Version::V2, ) .unwrap(); let event = EventBuilder::new(Kind::Custom(KIND_READ_STATE as u16), content) .tags([Tag::identifier(lane.d_tag)]) .custom_created_at(Timestamp::from(created_at)) .sign_with_keys(&keys) .unwrap(); let event = serde_json::to_value(event).unwrap(); relay.lock().unwrap().events.push(event.clone()); event } /// The decrypted document of the newest published event. pub fn published(client: &BuzzClient, relay: &Shared) -> (nostr::Event, Value) { let raw = relay.lock().unwrap().events.last().cloned().unwrap(); let event: nostr::Event = serde_json::from_value(raw).unwrap(); let keys = client.credentials().test_keys(); let plaintext = nip44::decrypt(keys.secret_key(), &keys.public_key(), &event.content).unwrap(); (event, serde_json::from_str(&plaintext).unwrap()) } pub fn published_count(relay: &Shared) -> usize { relay.lock().unwrap().events.len() } }