lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

commit eb1c8bdf69d3aa131888a6715f90ccdd6b2c281d
parent 9a2675ea0b7180301b757f99b3d38f76e226ddbf
Author: triesap <tyson@radroots.org>
Date:   Mon, 19 Oct 2026 22:30:12 +0000

storage: Retain exact browser delivery attempts

- Verify original seal containment and captured relay routes
- Preserve exact frames, immutable policy and selected endpoint budgets
- Fence original request indexes and deadlines at actual commit
- Prove browser rollback, uncertainty, cancellation and restart behavior

Diffstat:
MCargo.lock | 3+++
Mcontracts/api_baselines/radroots_storage_indexeddb.wasm32_workspace.txt | 3+++
Mcontracts/architecture/decisions/browser_workspace.v2.json | 37+++++++++++++++++++++++++++++++------
Mcrates/storage_indexeddb/Cargo.toml | 5++++-
Mcrates/storage_indexeddb/README.md | 30++++++++++++++++++++++++++++--
Mcrates/storage_indexeddb/src/workspace/address.rs | 40++++++++++++++++++++++++++++++++++++----
Acrates/storage_indexeddb/src/workspace/delivery/codec.rs | 190+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_indexeddb/src/workspace/delivery/graph.rs | 224+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_indexeddb/src/workspace/delivery/json.rs | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_indexeddb/src/workspace/delivery/mod.rs | 140+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_indexeddb/src/workspace/delivery/operations.rs | 332+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/storage_indexeddb/src/workspace/installation.rs | 78++++++++++++++++++++++++++++++++++++++++++++----------------------------------
Mcrates/storage_indexeddb/src/workspace/issuance.rs | 12+++++++++++-
Mcrates/storage_indexeddb/src/workspace/journal.rs | 11+++++++++++
Mcrates/storage_indexeddb/src/workspace/mod.rs | 1+
Mcrates/storage_indexeddb/src/workspace/plans.rs | 2++
Mcrates/storage_indexeddb/src/workspace/retained.rs | 28+++++++++++++++-------------
Mcrates/storage_indexeddb/tests/browser/Cargo.lock | 6++++++
Mcrates/storage_indexeddb/tests/browser/Cargo.toml | 1+
Acrates/storage_indexeddb/tests/browser/delivery.mjs | 119+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/storage_indexeddb/tests/browser/run.mjs | 10+++++++++-
Mcrates/storage_indexeddb/tests/browser/src/lib.rs | 2+-
Mcrates/storage_indexeddb/tests/browser/src/workspace.rs | 2++
Acrates/storage_indexeddb/tests/browser/src/workspace/delivery.rs | 274+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/storage_indexeddb/tests/browser/src/workspace/plans.rs | 42+++++++++++++++++++++++++++++++++++++-----
25 files changed, 1629 insertions(+), 68 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -3659,10 +3659,13 @@ version = "0.1.0-alpha" dependencies = [ "futures", "js-sys", + "nostr 0.44.7", "radroots_domain", "radroots_secrets", "radroots_storage", "radroots_transport", + "serde", + "serde_json", "sha2", "wasm-bindgen", "wasm-bindgen-futures", diff --git a/contracts/api_baselines/radroots_storage_indexeddb.wasm32_workspace.txt b/contracts/api_baselines/radroots_storage_indexeddb.wasm32_workspace.txt @@ -20,6 +20,9 @@ pub async fn radroots_storage_indexeddb::workspace::BrowserWorkspace::open_exist pub async fn radroots_storage_indexeddb::workspace::BrowserWorkspace::open_existing_for_capture(radroots_storage::workspace::AttachmentInput, radroots_storage::workspace::AttachmentProof, alloc::rc::Rc<dyn radroots_secrets::provider::SecretProvider>, radroots_secrets::id::SecretRef, radroots_secrets::provider::AccessPolicy, alloc::rc::Rc<dyn radroots_storage::workspace::WorkspaceClock>) -> core::result::Result<Self, radroots_storage::workspace::WorkspaceError> pub async fn radroots_storage_indexeddb::workspace::BrowserWorkspace::revoke(&self, radroots_storage::workspace::AttachmentInput, radroots_storage::workspace::AttachmentProof) -> core::result::Result<(), radroots_storage::workspace::WorkspaceError> impl radroots_storage_indexeddb::workspace::BrowserWorkspace +pub async fn radroots_storage_indexeddb::workspace::BrowserWorkspace::delivery_attempt(&self, radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, radroots_domain::identifier::CommandDigest, radroots_identity::key::PublicKey, radroots_transport::sink::DeliveryRequestId, radroots_storage::workspace::AttachmentProof) -> core::result::Result<core::option::Option<radroots_storage::command::delivery::DeliveryAttempt>, radroots_storage::command::OperationError> +pub async fn radroots_storage_indexeddb::workspace::BrowserWorkspace::prepare_delivery_attempt(&self, radroots_storage::command::delivery::DeliveryAttemptRequest, radroots_storage::workspace::AttachmentProof, core::option::Option<&radroots_storage::command::delivery::EnvelopeCandidate>) -> core::result::Result<radroots_storage::command::delivery::DeliveryAttemptOutcome, radroots_storage::command::OperationError> +impl radroots_storage_indexeddb::workspace::BrowserWorkspace pub async fn radroots_storage_indexeddb::workspace::BrowserWorkspace::install_transition_and_outbox(&self, radroots_storage::command::installation::InstallationRequest, radroots_storage::workspace::AttachmentProof) -> core::result::Result<radroots_storage::command::installation::InstallationOutcome, radroots_storage::command::OperationError> pub async fn radroots_storage_indexeddb::workspace::BrowserWorkspace::operation_snapshot(&self, radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, core::option::Option<radroots_domain::identifier::CommandDigest>, radroots_storage::workspace::AttachmentProof) -> core::result::Result<core::option::Option<radroots_storage::command::snapshot::OperationSnapshot>, radroots_storage::command::OperationError> impl core::fmt::Debug for radroots_storage_indexeddb::workspace::BrowserWorkspace diff --git a/contracts/architecture/decisions/browser_workspace.v2.json b/contracts/architecture/decisions/browser_workspace.v2.json @@ -1,7 +1,7 @@ { "schema_version": 1, "contract_id": "radroots.storage.browser_workspace.v2", - "state": "implemented_verified_installation_component", + "state": "implemented_verified_delivery_frame_component", "owner": "radroots_storage_indexeddb", "feature": "workspace", "target": "wasm32-unknown-unknown", @@ -152,9 +152,7 @@ "delivery_request_index" ], "reserved_not_yet_implemented_families": [ - "delivery_frame", - "delivery_observation", - "delivery_request_index" + "delivery_observation" ] }, "signing_claim": { @@ -202,6 +200,33 @@ "capture_evidence", "obtained_evidence", "install_transition_and_outbox", - "operation_snapshot" - ] + "operation_snapshot", + "prepare_delivery_attempt", + "delivery_attempt" + ], + "delivery_frame": { + "phase": "D_attempt", + "complete_bytes_max": 393216, + "clear_part_bytes_max": 16384, + "parts_max": 24, + "manifest_bytes": 200, + "request_index_bytes": 100, + "new_records_max": 26, + "grant_t0_comparisons": 6, + "atomic_records_max": 32, + "per_recipient_slots_max": 192, + "total_slots_max": 384, + "per_endpoint_invocations_max": 12, + "protected_request_index_and_manifest_must_agree": true, + "one_original_publication_policy": true, + "retained_or_unknown_authorizes_redispatch": false, + "actual_original_seal_containment_and_temporary_key_verified": true, + "current_access_lease_clock_and_original_delivery_deadline_at_commit": true, + "signing_window_applied_to_delivery": false, + "caller_candidate_borrowed": true, + "accepted_work_has_bounded_owned_copy": true, + "actual_complete_prefix_fence": true, + "provider_signing_or_network_inside_transaction": false, + "delivery_observations_or_full_client_claim": false + } } diff --git a/crates/storage_indexeddb/Cargo.toml b/crates/storage_indexeddb/Cargo.toml @@ -13,7 +13,7 @@ readme = "README.md" [features] default = [] -workspace = ["radroots_storage/protected_outbox", "dep:radroots_domain", "dep:radroots_secrets", "radroots_secrets/std", "dep:radroots_transport", "dep:sha2", "dep:zeroize"] +workspace = ["radroots_storage/protected_outbox", "dep:radroots_domain", "dep:radroots_secrets", "radroots_secrets/std", "dep:radroots_transport", "dep:sha2", "dep:zeroize", "dep:nostr", "dep:serde", "dep:serde_json"] [dependencies] radroots_storage = { workspace = true } @@ -24,6 +24,9 @@ sha2 = { workspace = true, optional = true } zeroize = { workspace = true, optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies] +nostr = { workspace = true, features = ["alloc", "nip44"], optional = true } +serde = { workspace = true, features = ["alloc", "derive"], optional = true } +serde_json = { workspace = true, features = ["alloc", "raw_value"], optional = true } futures = { workspace = true } js-sys = { workspace = true } wasm-bindgen = { workspace = true } diff --git a/crates/storage_indexeddb/README.md b/crates/storage_indexeddb/README.md @@ -63,10 +63,33 @@ obtained bytes requires current grant/proof, lease and clock. Shared checked installation/publication models validate relationships; host-private v2 metadata is a closed 468-byte record under independent protection contexts. Bodies stay in the original command. `operation_snapshot` verifies the complete retained -T0/T1/S/T2 graph and reconstructs its original message through the shared model. +T0/T1/S/T2/D graph and reconstructs its original message through the shared model. All facts are witnessed and compared in the actual transaction before results are returned. Missing predecessors with retained descendants refuse false -absence and new issuance. Delivery and a complete client remain separate phases. +absence and new issuance. + +`prepare_delivery_attempt` retains exact obtained outer bytes and selected target +intent before a potential transport invocation. It independently verifies the +temporary wrapping key, distinct author, signature, original contained seal, +actual publication work and captured signed relay routes. The complete original +target set and publication policy remain fixed. Selected endpoints spend at most +twelve contiguous invocation ordinals each. Retained attempts authorize no +redispatch; a new explicit attempt can reuse original exact bytes without another +participant signature. Replay preserves the original transaction and deadline. +Fresh writes recheck current grant/proof, lease, monotonic time and the original +delivery deadline after crypto, inside the actual transaction callback. + +A private frame contains at most 393,216 bytes in at most twenty-four 16 KiB +parts, a 200-byte manifest and an independent 100-byte request index. Both rows +must agree under separately expected contexts. At most twenty-six new records +plus the existing six grant/preparation comparisons fit the 32-record atomic +limit. The complete witnessed operation prefix fences every write. A coherent +snapshot and `delivery_attempt` read original retained data without dispatch. +Partial or missing predecessors, manifest, index and parts refuse replacement +and false absence. Actual uncertain completion preserves the original operation, +request and optional transaction anchor. Borrowed candidates remain caller-owned; +accepted work retains its bounded copy through observer loss and cancelled close. +Delivery observations and a complete journal/client remain separate phases. An owner-bound read snapshot witnesses at most 16,384 ordered opaque keys, lengths and SHA-256 digests, using a conservative 2 MiB scan reservation. Every @@ -112,6 +135,9 @@ fixture's lockfile, using an external output directory and the output name `fixture`. Run `tests/browser/run.mjs` with an installed Playwright package directory, that output directory, `chromium`, `firefox`, or `webkit`, and the preserved v1 fixture package directory for actual cross-version refusal checks. +The optional final argument selects `delivery`, `delivery_limits`, +`delivery_corruption`, or `delivery_faults`; omission runs the existing base +regressions. Additional profiles preserve that base's bounded deadline. Both the page and dedicated worker import those generated bytes. The runner uses isolated loopback origins and bounds each expanded run to 180 seconds. This harness ceiling is not a product deadline or performance acceptance. Route diff --git a/crates/storage_indexeddb/src/workspace/address.rs b/crates/storage_indexeddb/src/workspace/address.rs @@ -11,6 +11,8 @@ pub(super) const CLAIM: u8 = 3; pub(super) const EVIDENCE: u8 = 4; pub(super) const INSTALLATION: u8 = 5; pub(super) const PUBLICATION: u8 = 6; +pub(super) const DELIVERY: u8 = 7; +pub(super) const REQUEST_INDEX: u8 = 9; fn prefix_bytes(operation: OperationId) -> [u8; 16] { let digest = Sha256::new() @@ -37,10 +39,14 @@ pub(super) fn fields(key: RecordKey) -> Result<(u8, u8, u8), OperationError> { let kind = bytes[16]; let recipient = bytes[17]; let part = bytes[20]; - if bytes[18..20] - .iter() - .chain(bytes[21..].iter()) - .any(|b| *b != 0) + let delivery = matches!(kind, DELIVERY | REQUEST_INDEX); + let ordinal = u16::from_be_bytes([bytes[18], bytes[19]]); + if bytes[21..].iter().any(|b| *b != 0) + || if delivery { + !(1..=192).contains(&ordinal) + } else { + ordinal != 0 + } || match kind { PREPARED => recipient != 0 || part > 4, PLAN => recipient != 0 || part > 24, @@ -48,6 +54,8 @@ pub(super) fn fields(key: RecordKey) -> Result<(u8, u8, u8), OperationError> { EVIDENCE => recipient > 1 || part > 4, INSTALLATION => recipient != 0 || part != 0, PUBLICATION => recipient > 1 || part != 0, + DELIVERY => recipient > 1 || part > 24, + REQUEST_INDEX => recipient > 1 || part != 0, // Later reserved families remain unsupported until their actual // implementations and proving cohort land. Never ignore them. _ => true, @@ -57,6 +65,30 @@ pub(super) fn fields(key: RecordKey) -> Result<(u8, u8, u8), OperationError> { } Ok((kind, recipient, part)) } +pub(super) fn delivery_key( + operation: OperationId, + kind: u8, + recipient: u8, + ordinal: u16, + part: u8, +) -> RecordKey { + let mut bytes = *key(operation, kind, recipient, part).as_bytes(); + bytes[18..20].copy_from_slice(&ordinal.to_be_bytes()); + RecordKey::new(bytes) +} +pub(super) fn delivery_position(key: RecordKey) -> Result<(u8, u8, u16, u8), OperationError> { + let (kind, recipient, part) = fields(key)?; + if !matches!(kind, DELIVERY | REQUEST_INDEX) { + return Err(OperationError::RecoveryRequired); + } + let bytes = key.as_bytes(); + Ok(( + kind, + recipient, + u16::from_be_bytes([bytes[18], bytes[19]]), + part, + )) +} impl State { pub(super) async fn read_view( &self, diff --git a/crates/storage_indexeddb/src/workspace/delivery/codec.rs b/crates/storage_indexeddb/src/workspace/delivery/codec.rs @@ -0,0 +1,190 @@ +//! Closed private v2 frame, manifest and independent request-index metadata. +use core::num::NonZeroU64; +use radroots_domain::{EnvelopeEventId, TransactionId, UnixSeconds}; +use radroots_storage::command::{DeliveryAttempt, DeliveryIntent, OperationError, PublicationWork}; +use radroots_transport::{TargetSet, policy::SatisfactionPolicy, target::TargetFingerprint}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use zeroize::Zeroizing; + +pub(super) const MAX_BYTES: usize = 393216; +pub(super) const PART_BYTES: usize = 16384; +pub(super) const PARTS: u8 = 24; +const _: () = assert!(MAX_BYTES == PART_BYTES * PARTS as usize); +fn bad<E>(_: E) -> OperationError { + OperationError::RecoveryRequired +} +pub(super) fn valid_request(raw: &str) -> bool { + raw.len() == 64 + && raw + .bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) +} +#[derive(Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct Header<'a> { + version: u8, + work: [u8; 32], + #[serde(borrow)] + request: &'a str, + event: EnvelopeEventId, + intent: [u8; 32], + #[serde(deserialize_with = "super::json::targets")] + targets: TargetSet, + #[serde(deserialize_with = "super::json::small_vec")] + selection: Vec<TargetFingerprint>, + #[serde(deserialize_with = "super::json::policy")] + policy: SatisfactionPolicy, + deadline: u64, + transaction: TransactionId, + retained_at: UnixSeconds, + #[serde(deserialize_with = "super::json::small_vec")] + ordinals: Vec<u8>, +} +pub(super) fn encode( + value: &DeliveryAttempt, + work: [u8; 32], +) -> Result<Zeroizing<Vec<u8>>, OperationError> { + for target in value.intent().targets().targets() { + if serde_json::to_vec(target).map_err(bad)?.len() > PART_BYTES { + return Err(OperationError::InvalidCommand); + } + } + let header = Header { + version: 2, + work, + request: value.intent().request_id().as_str(), + event: value.intent().event_id(), + intent: *value.intent().digest(), + targets: value.intent().targets().clone(), + selection: value.intent().selection().to_vec(), + policy: value.intent().policy().clone(), + deadline: value.intent().deadline_unix_ms().get(), + transaction: value.transaction_id(), + retained_at: value.retained_at(), + ordinals: value.target_ordinals().to_vec(), + }; + let header = Zeroizing::new(serde_json::to_vec(&header).map_err(bad)?); + let length = 4usize + .checked_add(header.len()) + .and_then(|n| n.checked_add(value.raw_json().len())) + .filter(|n| *n <= MAX_BYTES) + .ok_or(OperationError::InvalidCommand)?; + let mut raw = Zeroizing::new(Vec::with_capacity(length)); + raw.extend_from_slice(&(header.len() as u32).to_be_bytes()); + raw.extend_from_slice(&header); + raw.extend_from_slice(value.raw_json().as_bytes()); + Ok(raw) +} +pub(super) fn decode( + raw: &[u8], + work: PublicationWork, + commitment: [u8; 32], +) -> Result<DeliveryAttempt, OperationError> { + if raw.len() < 5 || raw.len() > MAX_BYTES { + return Err(OperationError::RecoveryRequired); + } + let boundary = 4usize + .checked_add(u32::from_be_bytes(raw[..4].try_into().map_err(bad)?) as usize) + .filter(|n| *n < raw.len()) + .ok_or(OperationError::RecoveryRequired)?; + if !super::json::bounded(&raw[4..boundary], 32) { + return Err(OperationError::RecoveryRequired); + } + let header: Header<'_> = serde_json::from_slice(&raw[4..boundary]).map_err(bad)?; + if header.version != 2 || header.work != commitment { + return Err(OperationError::RecoveryRequired); + } + let intent = DeliveryIntent::new( + header.request, + header.event, + &header.targets, + &header.policy, + NonZeroU64::new(header.deadline).ok_or(OperationError::RecoveryRequired)?, + ) + .map_err(bad)?; + let selected = TargetSet::new( + intent + .targets() + .targets() + .iter() + .filter(|target| header.selection.contains(target.fingerprint())) + .cloned() + .collect(), + ) + .map_err(bad)?; + if selected + .targets() + .iter() + .map(|target| target.fingerprint()) + .ne(header.selection.iter()) + { + return Err(OperationError::RecoveryRequired); + } + let intent = intent.with_selection(&selected).map_err(bad)?; + if intent.digest() != &header.intent { + return Err(OperationError::RecoveryRequired); + } + DeliveryAttempt::from_store_report( + work, + intent, + core::str::from_utf8(&raw[boundary..]).map_err(bad)?, + header.transaction, + header.retained_at, + header.ordinals, + ) + .map_err(bad) +} +pub(super) struct Manifest { + pub request: String, + pub event: EnvelopeEventId, + pub transaction: TransactionId, + pub bytes: usize, + pub digest: [u8; 32], +} +impl Manifest { + pub(super) fn encode( + value: &DeliveryAttempt, + commitment: [u8; 32], + frame: &[u8], + ) -> Zeroizing<Vec<u8>> { + let mut raw = Zeroizing::new(Vec::with_capacity(200)); + raw.extend_from_slice(b"RDD2"); + raw.extend_from_slice(&commitment); + raw.extend_from_slice(value.intent().request_id().as_str().as_bytes()); + raw.extend_from_slice(value.intent().event_id().as_bytes()); + raw.extend_from_slice(value.transaction_id().as_bytes()); + raw.extend_from_slice(&(frame.len() as u32).to_be_bytes()); + raw.extend_from_slice(&Sha256::digest(frame)); + raw + } + pub(super) fn decode(raw: &[u8], commitment: [u8; 32]) -> Result<Self, OperationError> { + if raw.len() != 200 || &raw[..4] != b"RDD2" || raw[4..36] != commitment { + return Err(OperationError::RecoveryRequired); + } + let request = core::str::from_utf8(&raw[36..100]).map_err(bad)?; + let bytes = u32::from_be_bytes(raw[164..168].try_into().map_err(bad)?) as usize; + if !valid_request(request) || !(5..=MAX_BYTES).contains(&bytes) { + return Err(OperationError::RecoveryRequired); + } + Ok(Self { + request: request.into(), + event: EnvelopeEventId::from_bytes(raw[100..132].try_into().map_err(bad)?), + transaction: TransactionId::from_bytes(raw[132..164].try_into().map_err(bad)?), + bytes, + digest: raw[168..200].try_into().map_err(bad)?, + }) + } + pub(super) fn matches(&self, value: &DeliveryAttempt) -> bool { + self.request == value.intent().request_id().as_str() + && self.event == value.intent().event_id() + && self.transaction == value.transaction_id() + } +} +pub(super) fn index(manifest: &[u8], request: &str) -> Zeroizing<Vec<u8>> { + let mut raw = Zeroizing::new(Vec::with_capacity(100)); + raw.extend_from_slice(b"RDI2"); + raw.extend_from_slice(&Sha256::digest(manifest)); + raw.extend_from_slice(request.as_bytes()); + raw +} diff --git a/crates/storage_indexeddb/src/workspace/delivery/graph.rs b/crates/storage_indexeddb/src/workspace/delivery/graph.rs @@ -0,0 +1,224 @@ +use super::{ + Selection, Slot, + codec::{self, Manifest}, + context, routes, +}; +use crate::workspace::{ + State, address, crypto, + installation::{Records, work_commitment}, + retained, +}; +use crate::{Mutation, ReadSnapshot}; +use radroots_domain::OperationId; +use radroots_storage::command::{DeliveryAttempt, DeliveryAttemptSummary, OperationError}; +use sha2::{Digest, Sha256}; +use std::collections::{BTreeMap, BTreeSet}; +use zeroize::Zeroizing; + +pub(in crate::workspace) struct Graph { + pub summaries: Vec<DeliveryAttemptSummary>, + pub selected: Option<DeliveryAttempt>, + pub slots: [u16; 2], +} +impl Graph { + pub(in crate::workspace) async fn read( + state: &State, + view: &ReadSnapshot, + original: &retained::Graph, + installed: &Records, + operation: OperationId, + selected: Option<Selection<'_>>, + ) -> Result<Self, OperationError> { + let mut positions = BTreeMap::<(u8, u16), BTreeSet<(u8, u8)>>::new(); + for key in view.keys() { + let (kind, _, _) = address::fields(key)?; + if !matches!(kind, address::DELIVERY | address::REQUEST_INDEX) { + continue; + } + let (kind, recipient, ordinal, part) = address::delivery_position(key)?; + positions + .entry((recipient, ordinal)) + .or_default() + .insert((kind, part)); + } + if positions.len() > 384 { + return Err(OperationError::RecoveryRequired); + } + let mut result = Self { + summaries: Vec::with_capacity(positions.len()), + selected: None, + slots: [0; 2], + }; + let mut representations = BTreeMap::new(); + for ((recipient, ordinal), positions) in positions { + if ordinal != result.slots[recipient as usize] + 1 { + return Err(OperationError::RecoveryRequired); + } + result.slots[recipient as usize] = ordinal; + let bound = original.bound().ok_or(OperationError::RecoveryRequired)?; + let participant = bound + .plan() + .seals() + .get(recipient as usize) + .ok_or(OperationError::RecoveryRequired)? + .recipient(); + let work = installed + .work + .iter() + .find(|work| work.evidence().recipient() == participant) + .copied() + .ok_or(OperationError::RecoveryRequired)?; + let commitment = work_commitment( + installed.message.ok_or(OperationError::RecoveryRequired)?, + work, + ); + let slot = Slot { + operation, + recipient, + ordinal, + commitment, + }; + let manifest = read(state, view, slot, 0, false).await?; + let parsed = Manifest::decode(&manifest, commitment)?; + let count = parsed.bytes.div_ceil(codec::PART_BYTES) as u8; + if positions.len() != count as usize + 2 + || !positions.contains(&(address::REQUEST_INDEX, 0)) + || (0..=count).any(|part| !positions.contains(&(address::DELIVERY, part))) + { + return Err(OperationError::RecoveryRequired); + } + let index = read(state, view, slot, 0, true).await?; + if index.as_slice() != codec::index(&manifest, &parsed.request).as_slice() { + return Err(OperationError::RecoveryRequired); + } + let mut frame = Zeroizing::new(Vec::with_capacity(parsed.bytes)); + for part in 1..=count { + let clear = read(state, view, slot, part, false).await?; + if clear.len() != (parsed.bytes - frame.len()).min(codec::PART_BYTES) { + return Err(OperationError::RecoveryRequired); + } + frame.extend_from_slice(&clear); + } + if Sha256::digest(frame.as_slice()).as_slice() != parsed.digest { + return Err(OperationError::RecoveryRequired); + } + let report = codec::decode(&frame, work, commitment)?; + if !parsed.matches(&report) { + return Err(OperationError::RecoveryRequired); + } + routes(bound, participant, report.intent()) + .map_err(|_| OperationError::RecoveryRequired)?; + if representations + .insert(report.intent().event_id(), *report.envelope_digest()) + .is_some_and(|digest| digest != *report.envelope_digest()) + { + return Err(OperationError::RecoveryRequired); + } + result + .summaries + .push(DeliveryAttemptSummary::from_attempt(&report)); + if let Some(selected) = selected.filter(|selected| selected.recipient == participant) { + let exact = selected.request == report.intent().request_id(); + if exact + || (result.selected.is_none() + && selected.event == Some(report.intent().event_id())) + { + result.selected = Some(report); + } + } + } + Ok(result) + } +} +async fn read( + state: &State, + view: &ReadSnapshot, + slot: Slot, + part: u8, + index: bool, +) -> Result<Zeroizing<Vec<u8>>, OperationError> { + let kind = if index { + address::REQUEST_INDEX + } else { + address::DELIVERY + }; + let raw = state + .read_view_key( + view, + address::delivery_key(slot.operation, kind, slot.recipient, slot.ordinal, part), + ) + .await? + .ok_or(OperationError::RecoveryRequired)?; + let clear = crypto::open_bound( + state.provider.as_ref(), + &state.reference, + context(state, slot, part, index)?, + &raw, + ) + .await?; + Ok(clear.expose_secret(|raw| Zeroizing::new(raw.to_vec()))) +} +pub(super) async fn protect( + state: &State, + operation: OperationId, + recipient: u8, + ordinal: u16, + commitment: [u8; 32], + report: &DeliveryAttempt, +) -> Result<Vec<Mutation>, OperationError> { + let frame = codec::encode(report, commitment)?; + let manifest = Manifest::encode(report, commitment, &frame); + let slot = Slot { + operation, + recipient, + ordinal, + commitment, + }; + let mut changes = Vec::with_capacity(frame.len().div_ceil(codec::PART_BYTES) + 2); + changes.push(seal(state, slot, 0, false, &manifest).await?); + changes.push( + seal( + state, + slot, + 0, + true, + &codec::index(&manifest, report.intent().request_id().as_str()), + ) + .await?, + ); + for (part, raw) in frame.chunks(codec::PART_BYTES).enumerate() { + changes.push(seal(state, slot, part as u8 + 1, false, raw).await?); + } + Ok(changes) +} +async fn seal( + state: &State, + slot: Slot, + part: u8, + index: bool, + raw: &[u8], +) -> Result<Mutation, OperationError> { + let protected = crypto::seal_bound( + state.provider.as_ref(), + &state.reference, + context(state, slot, part, index)?, + raw, + ) + .await?; + Mutation::new( + address::delivery_key( + slot.operation, + if index { + address::REQUEST_INDEX + } else { + address::DELIVERY + }, + slot.recipient, + slot.ordinal, + part, + ), + None, + Some(protected), + ) + .map_err(super::super::journal::storage_error) +} diff --git a/crates/storage_indexeddb/src/workspace/delivery/json.rs b/crates/storage_indexeddb/src/workspace/delivery/json.rs @@ -0,0 +1,105 @@ +//! Bounded host-private metadata parsing, followed by checked shared models. +use core::{fmt, marker::PhantomData}; +use serde::{ + Deserialize, Deserializer, + de::{self, SeqAccess, Visitor}, +}; +use serde_json::value::RawValue; + +pub(super) fn bounded(raw: &[u8], array_limit: usize) -> bool { + let mut stack = [(0u8, false, 0usize); 16]; + let (mut depth, mut quoted, mut escaped) = (0, false, false); + for byte in raw.iter().copied() { + if quoted { + if escaped { + escaped = false; + } else if byte == b'\\' { + escaped = true; + } else if byte == b'"' { + quoted = false; + } + continue; + } + if byte.is_ascii_whitespace() || byte == b':' { + continue; + } + if byte == b',' { + if depth > 0 && stack[depth - 1].0 == b'[' { + stack[depth - 1].1 = true; + } + continue; + } + if matches!(byte, b'}' | b']') { + if depth == 0 { + return false; + } + depth -= 1; + if stack[depth].0 != if byte == b'}' { b'{' } else { b'[' } { + return false; + } + continue; + } + if depth > 0 && stack[depth - 1].0 == b'[' && stack[depth - 1].1 { + stack[depth - 1].2 += 1; + stack[depth - 1].1 = false; + if stack[depth - 1].2 > array_limit { + return false; + } + } + if matches!(byte, b'{' | b'[') { + if depth == stack.len() { + return false; + } + stack[depth] = (byte, byte == b'[', 0); + depth += 1; + } else if byte == b'"' { + quoted = true; + } + } + !quoted && !escaped && depth == 0 +} +pub(super) fn small_vec<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error> +where + D: Deserializer<'de>, + T: Deserialize<'de>, +{ + struct Items<T>(PhantomData<T>); + impl<'de, T: Deserialize<'de>> Visitor<'de> for Items<T> { + type Value = Vec<T>; + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("at most sixteen bounded entries") + } + fn visit_seq<A: SeqAccess<'de>>(self, mut sequence: A) -> Result<Self::Value, A::Error> { + let mut result = Vec::with_capacity(sequence.size_hint().unwrap_or(0).min(16)); + while let Some(raw) = sequence.next_element::<&'de RawValue>()? { + if result.len() == 16 || raw.get().len() > 16384 { + return Err(de::Error::custom("private delivery entry limit")); + } + result.push(serde_json::from_str(raw.get()).map_err(de::Error::custom)?); + } + Ok(result) + } + } + deserializer.deserialize_seq(Items(PhantomData)) +} +pub(super) fn targets<'de, D: Deserializer<'de>>( + deserializer: D, +) -> Result<radroots_transport::TargetSet, D::Error> { + #[derive(Deserialize)] + #[serde(deny_unknown_fields)] + struct Wire { + #[serde(deserialize_with = "small_vec")] + targets: Vec<radroots_transport::Target>, + } + let wire = Wire::deserialize(deserializer)?; + radroots_transport::TargetSet::new(wire.targets).map_err(de::Error::custom) +} +pub(super) fn policy<'de, D: Deserializer<'de>>( + deserializer: D, +) -> Result<radroots_transport::policy::SatisfactionPolicy, D::Error> { + let raw = <&RawValue>::deserialize(deserializer)?; + if raw.get().len() > 16384 || !bounded(raw.get().as_bytes(), 16) { + return Err(de::Error::custom("private delivery policy limit")); + } + serde_json::from_str(raw.get()).map_err(de::Error::custom) +} diff --git a/crates/storage_indexeddb/src/workspace/delivery/mod.rs b/crates/storage_indexeddb/src/workspace/delivery/mod.rs @@ -0,0 +1,140 @@ +//! Protected exact transport intent; persistence never performs delivery. +mod codec; +mod graph; +mod json; +mod operations; + +use super::State; +use radroots_domain::PublicKey; +use radroots_domain::{CommandDigest, EnvelopeEventId, OperationId, UnixSeconds}; +use radroots_secrets::context::{ + EnvelopeContext, EnvelopePurpose, EnvelopeSubject, PayloadSchemaId, +}; +use radroots_storage::command::{ + BoundSigningPlan, DeliveryAttempt, DeliveryIntent, OperationError, OperationSnapshot, + SealEvidence, SignedDmRelaySource, +}; +use sha2::{Digest, Sha256}; + +#[derive(Clone, Copy)] +struct Slot { + operation: OperationId, + recipient: u8, + ordinal: u16, + commitment: [u8; 32], +} +fn context( + state: &State, + slot: Slot, + part: u8, + index: bool, +) -> Result<EnvelopeContext, OperationError> { + let subject = Sha256::new() + .chain_update(b"radroots.browser.local_outbox.delivery.context.v2\0") + .chain_update(state.selected.workspace_id().as_bytes()) + .chain_update(slot.operation.as_bytes()) + .chain_update([slot.recipient]) + .chain_update(slot.ordinal.to_be_bytes()) + .chain_update([part, u8::from(index)]) + .chain_update(slot.commitment) + .finalize() + .iter() + .map(|b| format!("{b:02x}")) + .collect::<String>(); + Ok(EnvelopeContext::new( + EnvelopePurpose::parse("radroots.browser.workspace.delivery") + .map_err(|_| OperationError::InvalidCommand)?, + EnvelopeSubject::parse("workspace_delivery", subject) + .map_err(|_| OperationError::InvalidCommand)?, + PayloadSchemaId::parse(if index { + "radroots.browser.workspace.delivery_index.v2" + } else { + "radroots.browser.workspace.delivery.v2" + }) + .map_err(|_| OperationError::InvalidCommand)?, + )) +} +fn routes( + bound: &BoundSigningPlan, + recipient: PublicKey, + intent: &DeliveryIntent, +) -> Result<(), OperationError> { + let mut selected = None; + for route in bound + .plan() + .routes() + .iter() + .filter(|route| route.participant() == recipient) + { + let source = SignedDmRelaySource::verify(route.raw_json())?; + source.validate_capture(route)?; + if selected.replace(route).is_some() || route.selected_relays().is_empty() { + return Err(OperationError::UnverifiedApproval); + } + } + let route = selected.ok_or(OperationError::UnverifiedApproval)?; + if intent.targets().targets().iter().any(|target| { + !route + .selected_relays() + .iter() + .any(|raw| matches!(target.matches_nostr_relay(raw.as_ref()), Ok(true))) + }) { + return Err(OperationError::UnverifiedApproval); + } + Ok(()) +} +pub(super) use graph::Graph; + +#[derive(Clone, Copy)] +pub(super) struct Selection<'a> { + pub recipient: PublicKey, + pub request: &'a radroots_transport::sink::DeliveryRequestId, + pub event: Option<EnvelopeEventId>, + pub retain_seal: bool, +} +pub(super) struct Facts { + pub snapshot: Option<OperationSnapshot>, + pub selected: Option<DeliveryAttempt>, + pub original: Option<SealEvidence>, + pub slots: [u16; 2], +} +impl State { + pub(super) async fn delivery_facts( + &self, + view: &crate::ReadSnapshot, + operation: OperationId, + digest: Option<CommandDigest>, + t0: &super::journal::Records, + observed: UnixSeconds, + selected: Option<Selection<'_>>, + ) -> Result<Facts, OperationError> { + let mut original = super::retained::Graph::read_for_output( + self, + view, + operation, + digest, + t0, + selected.filter(|s| s.retain_seal).map(|s| s.recipient), + ) + .await?; + let installed = + super::installation::Records::read(self, view, &original, operation).await?; + let deliveries = + Graph::read(self, view, &original, &installed, operation, selected).await?; + let output = original.take_output(); + let snapshot = original + .into_snapshot(observed)? + .map(|snapshot| { + snapshot + .with_installation(installed.message, installed.work)? + .with_delivery(deliveries.summaries, Vec::new()) + }) + .transpose()?; + Ok(Facts { + snapshot, + selected: deliveries.selected, + original: output, + slots: deliveries.slots, + }) + } +} diff --git a/crates/storage_indexeddb/src/workspace/delivery/operations.rs b/crates/storage_indexeddb/src/workspace/delivery/operations.rs @@ -0,0 +1,332 @@ +use super::{Selection, codec, graph, routes}; +use crate::workspace::{ + BrowserWorkspace, WorkspaceError, crypto, installation::work_commitment, + journal::OperationChange, owned, +}; +use nostr::JsonUtil; +use radroots_domain::{CommandDigest, OperationId, PublicKey, TransactionId}; +use radroots_storage::{ + command::{ + BoundSigningPlan, CommitAttempt, DeliveryAttempt, DeliveryAttemptOutcome, + DeliveryAttemptRequest, DeliveryAttemptSummary, EnvelopeCandidate, OperationError, + PublicationWork, SealEvidence, + }, + workspace::{AttachmentProof, WorkspaceBinding}, +}; +use radroots_transport::sink::DeliveryRequestId; +use zeroize::Zeroizing; + +fn contains_original( + candidate: &EnvelopeCandidate, + work: PublicationWork, + bound: &BoundSigningPlan, + source: &SealEvidence, +) -> Result<(), OperationError> { + let actor = bound + .prepared() + .request() + .command() + .context() + .actor() + .participant(); + if candidate.work() != work + || candidate.author() == actor + || candidate.author() == bound.prepared().request().command().peer() + || candidate.author() == work.evidence().recipient() + || source.digest() != work.evidence().digest() + { + return Err(OperationError::InvalidEvidence); + } + let event = nostr::Event::from_json(candidate.raw_json()) + .map_err(|_| OperationError::InvalidEvidence)?; + let mut secp = nostr::secp256k1::Secp256k1::new(); + secp.seeded_randomize(&*crypto::random::<32>()?); + event + .verify_with_ctx(&secp) + .map_err(|_| OperationError::InvalidEvidence)?; + let recipient = nostr::PublicKey::from_slice(work.evidence().recipient().as_bytes()) + .map_err(|_| OperationError::InvalidEvidence)?; + let opened = candidate.with_wrapping_key(|key| { + let secret = + nostr::SecretKey::from_slice(key).map_err(|_| OperationError::InvalidEvidence)?; + let keys = nostr::Keys::new_with_ctx(&secp, secret); + if keys.public_key().to_bytes() != *candidate.author().as_bytes() { + return Err(OperationError::InvalidEvidence); + } + nostr::nips::nip44::decrypt(keys.secret_key(), &recipient, &event.content) + .map(Zeroizing::new) + .map_err(|_| OperationError::InvalidEvidence) + })?; + if opened.as_bytes() != source.raw_json().as_bytes() { + return Err(OperationError::InvalidEvidence); + } + Ok(()) +} +fn unknown(operation: CommitAttempt, attempt: DeliveryRequestId) -> DeliveryAttemptOutcome { + DeliveryAttemptOutcome::Unknown { operation, attempt } +} +impl BrowserWorkspace { + /// Retains actual original outer bytes and selected target intent before a + /// potential invocation. Only a fresh actual commit yields `Claimed`. + pub async fn prepare_delivery_attempt( + &self, + request: DeliveryAttemptRequest, + proof: AttachmentProof, + candidate: Option<&EnvelopeCandidate>, + ) -> Result<DeliveryAttemptOutcome, OperationError> { + let fallback = request.identity(); + let request_id = request.intent().request_id().clone(); + let work = self.state.admit()?; + let candidate = candidate.map(EnvelopeCandidate::retained_copy); + let state = self.state.clone(); + let result = owned(work, async move { + let access = state.operation_access(request.binding(), proof).await?; + let actual = request.work(); + let evidence = actual.evidence(); + let operation = evidence.operation_id(); + let view = state.read_view(operation).await?; + let t0 = state.operation_records(&view, operation).await?; + let facts = state + .delivery_facts( + &view, + operation, + Some(evidence.command_digest()), + &t0, + access.binding.observed_at(), + Some(Selection { + recipient: evidence.recipient(), + request: request.intent().request_id(), + event: Some(request.intent().event_id()), + retain_seal: true, + }), + ) + .await?; + let snapshot = facts.snapshot.ok_or(OperationError::RecoveryRequired)?; + radroots_storage::command::protected::same_binding( + snapshot.prepared().request().binding(), + request.binding(), + )?; + if !snapshot.publications().contains(&actual) { + return Err(OperationError::InvalidEvidence); + } + let bound = snapshot + .bound_plan() + .ok_or(OperationError::RecoveryRequired)?; + let recipient = bound + .plan() + .seals() + .iter() + .position(|seal| seal.recipient() == evidence.recipient()) + .ok_or(OperationError::InvalidEvidence)? as u8; + let retained = facts + .selected + .as_ref() + .filter(|value| value.intent().request_id() == request.intent().request_id()); + if let Some(retained) = retained + && (retained.work() != actual + || retained.intent().digest() != request.intent().digest() + || candidate.as_ref().is_some_and(|value| { + value.work() != actual || value.raw_json() != retained.raw_json() + })) + { + return Err(OperationError::IdempotencyConflict); + } + let fresh = retained.is_none(); + let (report, extra) = if fresh { + routes(bound, evidence.recipient(), request.intent())?; + if snapshot.delivery_attempts().iter().any(|value| { + value.work() == actual + && value.intent().publication_policy_digest() + != request.intent().publication_policy_digest() + }) { + return Err(OperationError::IdempotencyConflict); + } + let now = access.binding.observed_at(); + if now < actual.queued_at() + || snapshot + .delivery_attempts() + .iter() + .any(|value| value.retained_at() > now) + { + return Err(WorkspaceError::Unavailable.into()); + } + if now + .get() + .checked_mul(1000) + .is_none_or(|time| time >= request.intent().deadline_unix_ms().get()) + { + return Err(OperationError::DeadlineExceeded); + } + let mut ordinals = Vec::with_capacity(request.intent().selection().len()); + for target in request.intent().selected_targets() { + let count = snapshot + .delivery_attempts() + .iter() + .filter(|value| value.work() == actual) + .flat_map(|value| value.intent().selected_targets()) + .filter(|old| old.uri() == target.uri()) + .count(); + if count >= 12 { + return Err(OperationError::DeliveryAttemptLimit); + } + ordinals.push(count as u8 + 1); + } + let raw = if let Some(candidate) = &candidate { + if candidate.event_id() != request.intent().event_id() { + return Err(OperationError::InvalidEvidence); + } + contains_original( + candidate, + actual, + bound, + facts + .original + .as_ref() + .ok_or(OperationError::RecoveryRequired)?, + )?; + Zeroizing::new(candidate.raw_json().to_owned()) + } else { + let original = facts + .selected + .as_ref() + .filter(|value| { + value.work() == actual + && value.intent().event_id() == request.intent().event_id() + }) + .ok_or(OperationError::InvalidEvidence)?; + Zeroizing::new(original.raw_json().to_owned()) + }; + if facts.selected.as_ref().is_some_and(|value| { + value.intent().event_id() == request.intent().event_id() + && value.raw_json() != raw.as_str() + }) { + return Err(OperationError::IdempotencyConflict); + } + if facts.slots[recipient as usize] >= 192 { + return Err(OperationError::DeliveryAttemptLimit); + } + let report = DeliveryAttempt::from_store_report( + actual, + request.intent().clone(), + &raw, + TransactionId::from_bytes(*crypto::random::<32>()?), + now, + ordinals, + )?; + let commitment = work_commitment( + snapshot + .installation() + .ok_or(OperationError::RecoveryRequired)?, + actual, + ); + let mut summaries = snapshot.delivery_attempts().to_vec(); + summaries.push(DeliveryAttemptSummary::from_attempt(&report)); + snapshot.with_delivery(summaries, Vec::new())?; + let extra = graph::protect( + &state, + operation, + recipient, + facts.slots[recipient as usize] + 1, + commitment, + &report, + ) + .await?; + (report, extra) + } else { + ( + facts.selected.ok_or(OperationError::RecoveryRequired)?, + Vec::new(), + ) + }; + let anchor = CommitAttempt::new( + evidence.workspace_id(), + operation, + evidence.command_digest(), + Some(report.transaction_id()), + ); + let identity = report.intent().request_id().clone(); + let deadline = fresh.then_some(report.intent().deadline_unix_ms()); + match state + .check_operation( + access, + operation, + OperationChange { + before: t0, + after: None, + window: None, + delivery_deadline: deadline, + extra, + }, + view, + ) + .await + { + Ok(()) => Ok(if fresh { + DeliveryAttemptOutcome::Claimed(Box::new(report)) + } else { + DeliveryAttemptOutcome::Retained(Box::new(report)) + }), + Err(OperationError::Workspace(WorkspaceError::CommitUnknown)) => { + state.stop(); + Ok(unknown(anchor, identity)) + } + Err(error) => Err(error), + } + }) + .await; + match result { + Err(OperationError::Workspace(WorkspaceError::CommitUnknown)) => { + self.state.stop(); + Ok(unknown(fallback, request_id)) + } + other => other, + } + } + /// Reads the exact original attempt without dispatch or safe-reissue rights. + pub async fn delivery_attempt( + &self, + binding: WorkspaceBinding, + operation: OperationId, + digest: CommandDigest, + recipient: PublicKey, + request: DeliveryRequestId, + proof: AttachmentProof, + ) -> Result<Option<DeliveryAttempt>, OperationError> { + if !codec::valid_request(request.as_str()) { + return Err(OperationError::InvalidCommand); + } + let work = self.state.admit()?; + let state = self.state.clone(); + owned(work, async move { + let access = state.operation_access(&binding, proof).await?; + let view = state.read_view(operation).await?; + let t0 = state.operation_records(&view, operation).await?; + let facts = state + .delivery_facts( + &view, + operation, + Some(digest), + &t0, + access.binding.observed_at(), + Some(Selection { + recipient, + request: &request, + event: None, + retain_seal: false, + }), + ) + .await?; + if let Some(snapshot) = &facts.snapshot { + radroots_storage::command::protected::same_binding( + snapshot.prepared().request().binding(), + &binding, + )?; + } + state + .check_operation(access, operation, OperationChange::observe(t0), view) + .await?; + Ok(facts.selected) + }) + .await + } +} diff --git a/crates/storage_indexeddb/src/workspace/installation.rs b/crates/storage_indexeddb/src/workspace/installation.rs @@ -5,7 +5,7 @@ use super::{ BrowserWorkspace, Mutation, State, WorkspaceError, address, crypto, owned, retained::Graph, }; use crate::ReadSnapshot; -use radroots_domain::{CommandDigest, OperationId, TransactionId}; +use radroots_domain::{CommandDigest, OperationId, TransactionId, UnixSeconds}; use radroots_secrets::context::{ EnvelopeContext, EnvelopePurpose, EnvelopeSubject, PayloadSchemaId, }; @@ -55,11 +55,11 @@ fn context( )) } pub(super) struct Records { - message: Option<ChatInstallation>, - work: Vec<PublicationWork>, + pub message: Option<ChatInstallation>, + pub work: Vec<PublicationWork>, } impl Records { - async fn read( + pub(super) async fn read( state: &State, view: &ReadSnapshot, graph: &Graph, @@ -113,6 +113,9 @@ impl Records { Ok(Self { message, work }) } } +pub(super) fn work_commitment(message: ChatInstallation, work: PublicationWork) -> [u8; 32] { + Sha256::digest(codec::work(message, work)).into() +} impl State { /// A missing claim must not conceal installation/publication descendants. /// The complete witnessed prefix still fences all facts at actual commit. @@ -122,9 +125,10 @@ impl State { operation: OperationId, digest: CommandDigest, t0: &PreparationRecords, + observed: UnixSeconds, ) -> Result<(), OperationError> { - let graph = Graph::read(self, view, operation, Some(digest), t0).await?; - Records::read(self, view, &graph, operation).await?; + self.delivery_facts(view, operation, Some(digest), t0, observed, None) + .await?; Ok(()) } } @@ -144,15 +148,21 @@ impl BrowserWorkspace { let operation = request.operation_id(); let view = state.read_view(operation).await?; let t0 = state.operation_records(&view, operation).await?; - let graph = Graph::read( - &state, - &view, - operation, - Some(request.command_digest()), - &t0, - ) - .await?; - let bound = graph.bound().ok_or(OperationError::RecoveryRequired)?; + let snapshot = state + .delivery_facts( + &view, + operation, + Some(request.command_digest()), + &t0, + access.binding.observed_at(), + None, + ) + .await? + .snapshot + .ok_or(OperationError::RecoveryRequired)?; + let bound = snapshot + .bound_plan() + .ok_or(OperationError::RecoveryRequired)?; radroots_storage::command::protected::same_binding( bound.prepared().request().binding(), request.binding(), @@ -160,29 +170,30 @@ impl BrowserWorkspace { if bound.plan().digest() != request.plan_digest() { return Err(OperationError::IdempotencyConflict); } - let records = Records::read(&state, &view, &graph, operation).await?; - let missing = graph - .obtained() + let original = snapshot.installation(); + let missing = snapshot + .recipients() + .iter() + .filter_map(|value| value.obtained()) .filter(|evidence| { - !records - .work + !snapshot + .publications() .iter() .any(|value| value.evidence().recipient() == evidence.recipient()) }) .collect::<Vec<_>>(); - if records.message.is_none() && missing.is_empty() { + if original.is_none() && missing.is_empty() { return Err(OperationError::InvalidEvidence); } - let fresh = records.message.is_none() || !missing.is_empty(); + let fresh = original.is_none() || !missing.is_empty(); let transaction = if fresh { TransactionId::from_bytes(*crypto::random::<32>()?) } else { - records - .message + original .ok_or(OperationError::RecoveryRequired)? .transaction_id() }; - let installed = if let Some(value) = records.message { + let installed = if let Some(value) = original { value } else { ChatInstallation::from_committed_store( @@ -193,7 +204,7 @@ impl BrowserWorkspace { )? }; let mut extra = Vec::with_capacity(3); - if records.message.is_none() { + if original.is_none() { let raw = codec::message(bound, installed); let protected = crypto::seal_bound( state.provider.as_ref(), @@ -256,6 +267,7 @@ impl BrowserWorkspace { before: t0, after: None, window: None, + delivery_deadline: None, extra, }, view, @@ -295,18 +307,16 @@ impl BrowserWorkspace { let observed = access.binding.observed_at(); let view = state.read_view(operation).await?; let t0 = state.operation_records(&view, operation).await?; - let graph = Graph::read(&state, &view, operation, digest, &t0).await?; - if let Some(prepared) = graph.prepared() { + let snapshot = state + .delivery_facts(&view, operation, digest, &t0, observed, None) + .await? + .snapshot; + if let Some(snapshot) = &snapshot { radroots_storage::command::protected::same_binding( - prepared.request().binding(), + snapshot.prepared().request().binding(), &binding, )?; } - let records = Records::read(&state, &view, &graph, operation).await?; - let snapshot = graph - .into_snapshot(observed)? - .map(|snapshot| snapshot.with_installation(records.message, records.work)) - .transpose()?; state .check_operation(access, operation, OperationChange::observe(t0), view) .await?; diff --git a/crates/storage_indexeddb/src/workspace/issuance.rs b/crates/storage_indexeddb/src/workspace/issuance.rs @@ -278,7 +278,13 @@ impl BrowserWorkspace { Claim::Present(attempt) => (attempt, false, Vec::new()), Claim::Absent(bound, index) => { state - .check_claim_absence(&view, operation, request.command_digest(), &t0) + .check_claim_absence( + &view, + operation, + request.command_digest(), + &t0, + access.binding.observed_at(), + ) .await?; let now = access.binding.observed_at(); if now < bound.bound_at() { @@ -318,6 +324,7 @@ impl BrowserWorkspace { before: t0, after: None, window: fresh.then(|| attempt.bound_plan().prepared().issuance_window()), + delivery_deadline: None, extra, }; match state.check_operation(access, operation, change, view).await { @@ -376,6 +383,7 @@ impl BrowserWorkspace { operation, request.command_digest(), &t0, + access.binding.observed_at(), ) .await?; None @@ -534,6 +542,7 @@ impl BrowserWorkspace { operation, request.command_digest(), &t0, + access.binding.observed_at(), ) .await?; } @@ -554,6 +563,7 @@ impl BrowserWorkspace { operation, request.command_digest(), &t0, + access.binding.observed_at(), ) .await?; None diff --git a/crates/storage_indexeddb/src/workspace/journal.rs b/crates/storage_indexeddb/src/workspace/journal.rs @@ -28,6 +28,7 @@ pub(super) struct OperationChange { pub before: Records, pub after: Option<Records>, pub window: Option<IssuanceWindow>, + pub delivery_deadline: Option<core::num::NonZeroU64>, pub extra: Vec<Mutation>, } impl OperationChange { @@ -36,6 +37,7 @@ impl OperationChange { before, after: None, window: None, + delivery_deadline: None, extra: Vec::new(), } } @@ -261,6 +263,7 @@ impl State { before, after, window, + delivery_deadline, extra, } = change; let batch = mutations(access.protected, operation, before, after, extra)?; @@ -286,6 +289,13 @@ impl State { return Err(OperationError::DeadlineExceeded); } } + if delivery_deadline.is_some_and(|deadline| { + now.get() + .checked_mul(1000) + .is_none_or(|time| time >= deadline.get()) + }) { + return Err(OperationError::DeadlineExceeded); + } Ok(()) }) .await @@ -392,6 +402,7 @@ impl BrowserWorkspace { before, after, window: fresh.then(|| prepared.issuance_window()), + delivery_deadline: None, extra, }, view, diff --git a/crates/storage_indexeddb/src/workspace/mod.rs b/crates/storage_indexeddb/src/workspace/mod.rs @@ -3,6 +3,7 @@ //! requires its independently retained selection and protected proof. mod address; mod crypto; +mod delivery; mod installation; mod issuance; mod journal; diff --git a/crates/storage_indexeddb/src/workspace/plans.rs b/crates/storage_indexeddb/src/workspace/plans.rs @@ -266,6 +266,7 @@ impl BrowserWorkspace { before: t0, after: None, window: fresh.then(|| prepared.issuance_window()), + delivery_deadline: None, extra: comparisons(operation, before, after)?, }; match state.check_operation(access, operation, change, view).await { @@ -322,6 +323,7 @@ impl BrowserWorkspace { before: t0, after: None, window: None, + delivery_deadline: None, extra: comparisons(operation, records, None)?, }; state diff --git a/crates/storage_indexeddb/src/workspace/retained.rs b/crates/storage_indexeddb/src/workspace/retained.rs @@ -4,7 +4,7 @@ use crate::ReadSnapshot; use radroots_domain::{CommandDigest, EnvelopeEventId, OperationId, PublicKey, UnixSeconds}; use radroots_storage::command::{ BoundSigningPlan, ClaimSnapshot, EvidenceReceipt, OperationError, OperationSnapshot, - RecipientSnapshot, RetainedOperation, protected, + RecipientSnapshot, RetainedOperation, SealEvidence, protected, }; pub(super) async fn header( @@ -50,17 +50,20 @@ pub(super) async fn header( pub(super) struct Graph { retained: Option<RetainedOperation>, recipients: Vec<RecipientSnapshot>, + output: Option<SealEvidence>, } impl Graph { - pub(super) async fn read( + pub(super) async fn read_for_output( state: &State, view: &ReadSnapshot, operation: OperationId, digest: Option<CommandDigest>, t0: &Records, + selected: Option<PublicKey>, ) -> Result<Self, OperationError> { let retained = header(state, view, operation, digest, t0).await?; let mut recipients = Vec::with_capacity(2); + let mut output = None; let retained = match retained { Some(RetainedOperation::Planned(bound)) => { let mut bound = *bound; @@ -87,15 +90,16 @@ impl Graph { let claim = ClaimSnapshot::from_store_report(&attempt); // Verify one bounded exact seal at a time; retaining a // receipt does not duplicate private plan/raw bodies. - let obtained = state - .evidence(view, &attempt, index as u8) - .await? - .map(|(_, receipt)| receipt); + let obtained = state.evidence(view, &attempt, index as u8).await?; + let receipt = obtained.as_ref().map(|(_, receipt)| *receipt); + if Some(recipient) == selected { + output = obtained.map(|(value, _)| value); + } recipients.push(RecipientSnapshot::from_store_report( recipient, Some(expected), Some(claim), - obtained, + receipt, )); bound = attempt.into_bound_plan(); } @@ -120,16 +124,17 @@ impl Graph { Ok(Self { retained, recipients, + output, }) } + pub(super) fn take_output(&mut self) -> Option<SealEvidence> { + self.output.take() + } pub(super) fn bound(&self) -> Option<&BoundSigningPlan> { self.retained .as_ref() .and_then(RetainedOperation::bound_plan) } - pub(super) fn prepared(&self) -> Option<&radroots_storage::command::PreparedOperation> { - self.retained.as_ref().map(RetainedOperation::prepared) - } pub(super) fn receipt(&self, recipient: PublicKey) -> Result<EvidenceReceipt, OperationError> { self.recipients .iter() @@ -137,9 +142,6 @@ impl Graph { .and_then(|value| value.obtained()) .ok_or(OperationError::RecoveryRequired) } - pub(super) fn obtained(&self) -> impl Iterator<Item = EvidenceReceipt> + '_ { - self.recipients.iter().filter_map(|value| value.obtained()) - } pub(super) fn into_snapshot( self, observed: UnixSeconds, diff --git a/crates/storage_indexeddb/tests/browser/Cargo.lock b/crates/storage_indexeddb/tests/browser/Cargo.lock @@ -672,6 +672,7 @@ name = "radroots_blossom" version = "0.1.0-alpha" dependencies = [ "mediatype", + "serde", "sha2", "unicode-general-category", "url", @@ -749,6 +750,7 @@ dependencies = [ "radroots_secrets", "radroots_storage", "radroots_storage_indexeddb", + "radroots_transport", "serde_json", "sha2", "wasm-bindgen", @@ -807,10 +809,13 @@ version = "0.1.0-alpha" dependencies = [ "futures", "js-sys", + "nostr", "radroots_domain", "radroots_secrets", "radroots_storage", "radroots_transport", + "serde", + "serde_json", "sha2", "wasm-bindgen", "wasm-bindgen-futures", @@ -1101,6 +1106,7 @@ dependencies = [ "form_urlencoded", "idna 1.1.0", "percent-encoding", + "serde", ] [[package]] diff --git a/crates/storage_indexeddb/tests/browser/Cargo.toml b/crates/storage_indexeddb/tests/browser/Cargo.toml @@ -15,6 +15,7 @@ radroots_storage_indexeddb = { path = "../..", features = ["workspace"] } radroots_storage = { path = "../../../storage" } radroots_domain = { path = "../../../domain", features = ["serde", "std"] } radroots_protocol = { path = "../../../protocol", features = ["chat"] } +radroots_transport = { path = "../../../transport", features = ["serde"] } nostr = { version = "=0.44.7", default-features = false, features = ["alloc", "nip44"] } serde_json = "=1.0.149" sha2 = "=0.10.9" diff --git a/crates/storage_indexeddb/tests/browser/delivery.mjs b/crates/storage_indexeddb/tests/browser/delivery.mjs @@ -0,0 +1,119 @@ +import assert from 'node:assert/strict'; + +export async function runDelivery(page, profile) { + const result=await page.evaluate(async profile=>{ + const {default:init,WorkspaceFixture}=await import('./fixture.js');await init(); + const tests=[],passed=name=>{tests.push(name);console.log('PASS '+name);}; + const same=(a,b)=>{if(JSON.stringify(a)!==JSON.stringify(b))throw Error('assertion mismatch: '+JSON.stringify(a)+' / '+JSON.stringify(b));}; + const rejects=async(fn,code)=>{try{await fn();}catch(error){if(String(error)!==code)throw Error('expected '+code+', received '+String(error));return;}throw Error('expected refusal: '+code);}; + const kb=key=>Array.from(new Uint8Array(key)); + const rawOpen=r=>new Promise((resolve,reject)=>{const req=indexedDB.open(r.namespace,1);req.onupgradeneeded=()=>req.transaction.abort();req.onsuccess=()=>resolve(req.result);req.onerror=reject;}); + const rows=async r=>{const db=await rawOpen(r);try{return await new Promise((resolve,reject)=>{const tx=db.transaction('records','readonly'),store=tx.objectStore('records');let keys,values;store.getAllKeys().onsuccess=e=>keys=e.target.result;store.getAll().onsuccess=e=>values=e.target.result;tx.oncomplete=()=>resolve({keys,values});tx.onabort=reject;});}finally{db.close();}}; + const change=async(r,fn)=>{const db=await rawOpen(r);try{await new Promise((resolve,reject)=>{const tx=db.transaction('records','readwrite');fn(tx.objectStore('records'));tx.oncomplete=resolve;tx.onabort=reject;});}finally{db.close();}}; + const sameRows=(a,b)=>{same(a.keys.map(kb),b.keys.map(kb));same(a.values.map(kb),b.values.map(kb));}; + const restore=async(r,before)=>change(r,store=>{store.clear();before.keys.forEach((key,i)=>store.put(before.values[i],key));}); + const open=r=>WorkspaceFixture.open(r.n,new Uint8Array(r.fingerprint),new Uint8Array(r.selection),new Uint8Array(r.proof)); + const receipt=(owner,n)=>({n,fingerprint:Array.from(owner.fingerprint()),selection:Array.from(owner.selection()),proof:Array.from(owner.proof()),namespace:owner.data_namespace()}); + const setup=async(n,index=0,large=false)=>{const owner=await WorkspaceFixture.create(n+1);await owner.admit(n,false);const wire=await(large?owner.new_large_delivery_plan(n):owner.new_delivery_plan(n));await owner.bind_plan(n,wire);const seal=await owner.obtain(n,index,false);await owner.capture(seal);await owner.install(n);const envelope=await owner.envelope(n,index,0);return{owner,envelope,receipt:receipt(owner,n+1)};}; + const query=async(owner,n,index,id)=>JSON.parse(await owner.query_envelope(n,index,id)); + const store=async(s,n,id,selection=1,mode=0,envelope=s.envelope)=>JSON.parse(await s.owner.store_envelope(n,id,selection,mode,envelope)); + const key=async(n,kind,recipient=0,ordinal=0,part=0)=>{const digest=new Uint8Array(await crypto.subtle.digest('SHA-256',new Uint8Array([...new TextEncoder().encode('radroots.browser.local_outbox.operation.prefix.v2\0'),...new Uint8Array(32).fill(n)])));const raw=new Uint8Array(32);raw.set(digest.subarray(0,16));raw[16]=kind;raw[17]=recipient;new DataView(raw.buffer).setUint16(18,ordinal);raw[20]=part;return raw;}; + const put=IDBObjectStore.prototype.put,add=IDBObjectStore.prototype.add,abort=IDBTransaction.prototype.abort; + const inject=(namespace,target,redirect)=>{ + IDBObjectStore.prototype.put=function(bytes,at){if(this.transaction.db.name===namespace&&JSON.stringify(kb(at))===JSON.stringify(kb(target))){const r=add.call(this,bytes,redirect??at);r.addEventListener('error',event=>event.preventDefault());return r;}return put.call(this,bytes,at);}; + IDBTransaction.prototype.abort=function(){if(this.db.name===namespace)return;return abort.call(this);}; + }; + const reset=()=>{IDBObjectStore.prototype.put=put;IDBTransaction.prototype.abort=abort;}; + try { + if(profile==='delivery') { + const n=210,s=await setup(n),before=await rows(s.receipt); + const originalClaim=Array.from(await s.owner.attempt(n,0));same(await query(s.owner,n,0,1),null);await rejects(()=>store(s,n,1,1,16),'invalid_evidence');sameRows(await rows(s.receipt),before); + passed('actual D absence is read without a transport invocation or replacement'); + const first=await store(s,n,1);same(first.kind,'claimed');same(first.report.raw,s.envelope.raw());same(first.report.event,s.envelope.event());same(first.report.targets.length,2);same(first.report.selection.length,1);same(first.report.ordinals,[1]);same(await query(s.owner,n,0,1),first.report); + passed('actual D commit retains exact signed outer, original work, full policy and selected target before invocation'); + s.owner.set_time(1850);const random=Crypto.prototype.getRandomValues; + try{Crypto.prototype.getRandomValues=()=>{throw Error('retained D acquired new entropy');};const replay=await store(s,n,1);same(replay.kind,'retained');same(replay.report,first.report);} + finally{Crypto.prototype.getRandomValues=random;} + passed('exact retained D replay after original delivery expiry preserves identity and never authorizes redispatch'); + s.owner.set_time(1600);const second=await store(s,n,2,2,16);same(second.kind,'claimed');same(second.report.raw,first.report.raw);same(second.report.policy,first.report.policy);same(second.report.ordinals,[1]);same(second.report.at,1600);if(second.report.transaction===first.report.transaction)throw Error('new D reused original transaction'); + passed('new selected-endpoint retry retains the original exact outer and policy after signing expiry with an independent ordinal'); + const stable=await rows(s.receipt);await rejects(()=>store(s,n,2,1,16),'idempotency_conflict');await rejects(()=>store(s,n,3,1,5),'idempotency_conflict');sameRows(await rows(s.receipt),stable); + passed('retained request conflicts and changed original publication criterion refuse without writes'); + const another=await s.owner.envelope(n,0,0),third=await store(s,n,3,1,0,another);same(third.kind,'claimed');same(third.report.ordinals,[2]);if(third.report.event===first.report.event)throw Error('independent wrap reused its original event'); + const snapshot=JSON.parse(await s.owner.snapshot(n));same(snapshot.deliveries,3);same(snapshot.observations,0);same(snapshot.publications.length,1);same(snapshot.message.text,'\u0001'.repeat(3000));same(Array.from(await s.owner.attempt(n,0)),originalClaim);await s.owner.close(); + passed('fresh wrapping of the original obtained seal advances only selected transport attempts while retaining one logical message'); + const peerN=212,peer=await setup(peerN,1);await store(peer,peerN,1);const self=await peer.owner.obtain(peerN,0,false);await peer.owner.capture(self);await peer.owner.install(peerN);const progress=JSON.parse(await peer.owner.snapshot(peerN));same(progress.publications.length,2);same(progress.deliveries,1);await peer.owner.close(); + passed('actual first self claim remains legal after peer delivery retention without replacing original peer work'); + return{tests,receipt:s.receipt,operation:n,index:0,id:1,golden:first.report}; + } + if(profile==='delivery_limits') { + const n=214,s=await setup(n);let first; + for(let id=1;id<=12;id++){const value=await store(s,n,id,1,id===1?0:16);same(value.kind,'claimed');same(value.report.ordinals,[id]);first??=value.report;same(value.report.raw,first.raw);} + const exhausted=await rows(s.receipt);await rejects(()=>store(s,n,13,1,16),'delivery_attempt_limit');sameRows(await rows(s.receipt),exhausted); + passed('all twelve actual selected-endpoint invocations retain contiguous ordinals and the thirteenth is refused'); + const other=await store(s,n,14,2,16);same(other.report.ordinals,[1]);same(other.report.raw,first.raw);same(other.report.policy,first.policy);same((await store(s,n,1)).kind,'retained');await s.owner.close(); + passed('an exhausted endpoint does not consume another endpoint budget or change the original completion policy'); + const largeN=216,large=await setup(largeN,0,true),value=await store(large,largeN,1,32768,6);same(value.kind,'claimed');same(value.report.targets.length,16);same(value.report.selection.length,1);same(value.report.ordinals,[1]); + const actual=await rows(large.receipt),parts=actual.keys.map(kb).filter(key=>key[16]===7&&key[20]>0);if(parts.length<2||parts.length>24)throw Error('actual valid long-target frame did not exercise bounded multipart storage');same(actual.keys.map(kb).filter(key=>key[16]===9).length,1);same(await query(large.owner,largeN,0,1),value.report);await large.owner.close(); + passed('actual sixteen-target policy and last-target selection span protected private parts with one matching request index'); + return{tests}; + } + if(profile==='delivery_corruption') { + const n=218,s=await setup(n);await store(s,n,1);await store(s,n,2,2,16);await s.owner.close();const stable=await rows(s.receipt); + const at=(kind,ordinal,part)=>stable.keys.find(k=>{const bytes=kb(k);return bytes[16]===kind&&bytes[18]===0&&bytes[19]===ordinal&&bytes[20]===part;}); + for(const mode of ['manifest','part','index','tampered','foreign_index','t2_loss','all_predecessors']) { + await change(s.receipt,store=>{ + if(mode==='manifest')store.delete(at(7,1,0));if(mode==='part')store.delete(at(7,1,1));if(mode==='index')store.delete(at(9,1,0)); + if(mode==='tampered'){const selected=at(7,1,1),i=stable.keys.indexOf(selected),raw=new Uint8Array(stable.values[i]);raw[raw.length-1]^=1;store.put(raw,selected);} + if(mode==='foreign_index')store.put(stable.values[stable.keys.indexOf(at(9,2,0))],at(9,1,0)); + if(mode==='t2_loss'||mode==='all_predecessors')stable.keys.filter(k=>(mode==='all_predecessors'?[3,4,5,6]:[5,6]).includes(kb(k)[16])).forEach(k=>store.delete(k)); + }); + const damaged=await rows(s.receipt);let current=await open(s.receipt);await rejects(()=>query(current,n,0,1),'recovery_required');await current.close(); + current=await open(s.receipt);await rejects(()=>current.install(n),'recovery_required');await current.close(); + if(mode==='all_predecessors'){current=await open(s.receipt);await rejects(()=>current.claim(n,0),'recovery_required');await current.close();} + sameRows(await rows(s.receipt),damaged);await restore(s.receipt,stable); + passed('actual '+mode+' D damage refuses original query and predecessor replacement with exact damaged bytes preserved'); + } + return{tests}; + } + if(profile==='delivery_faults') { + const n=220,s=await setup(n),initial=await rows(s.receipt); + for(const mode of [1,2,3]){const invalid=await s.owner.envelope(n,0,mode);await rejects(()=>store(s,n,1,1,0,invalid),'invalid_evidence');} + await rejects(()=>store(s,n,1,1,4),'unverified_approval');sameRows(await rows(s.receipt),initial); + passed('actual valid outer signatures with wrong key, wrong contained seal, participant author or unapproved relay cannot create D facts'); + for(const [mode,code] of [[1,'workspace_unavailable'],[2,'grant_expired'],[3,'deadline_exceeded']]){await rejects(()=>store(s,n,1,1,mode),code);await s.owner.close();sameRows(await rows(s.receipt),initial);s.owner=await open(s.receipt);} + s.owner.set_time(1000); + passed('actual D callback rechecks clock, grant and original delivery deadline after crypto and before mutations'); + let written=false;const manifest=await key(n,7,0,1,0); + try{IDBObjectStore.prototype.put=function(bytes,at){const request=put.call(this,bytes,at);if(this.transaction.db.name===s.receipt.namespace&&JSON.stringify(kb(at))===JSON.stringify(kb(manifest)))request.addEventListener('success',()=>{written=true;this.transaction.abort();},{once:true});return request;};await rejects(()=>store(s,n,1),'storage_failure');} + finally{reset();} + same(written,true);sameRows(await rows(s.receipt),initial);same((await store(s,n,1)).kind,'claimed');await s.owner.close(); + passed('actual frame-write success followed by abort retains no D fact and a known-rollback retry uses the caller original'); + const unknownN=222,unknown=await setup(unknownN),unchanged=await key(unknownN,1,0,0,1);let anchor; + try{anchor=JSON.parse(await unknown.owner.store_envelope_unknown(unknownN,1,1,unknown.envelope,()=>inject(unknown.receipt.namespace,unchanged)));} + finally{reset();} + same(anchor.kind,'unknown');same(anchor.request,'01'.repeat(32));if(!anchor.transaction)throw Error('uncertain D lost its original transaction');await unknown.owner.close();let reopened=await open(unknown.receipt);const retained=await query(reopened,unknownN,0,1);same(retained.transaction,anchor.transaction);same(retained.raw,unknown.envelope.raw());await reopened.close(); + passed('actual unexpected completion preserves original D uncertainty and reopens the exact committed frame and transaction'); + const partialN=224,partial=await setup(partialN),failedIndex=await key(partialN,9,0,1),oldT0=await key(partialN,1,0,0,1); + try{anchor=JSON.parse(await partial.owner.store_envelope_unknown(partialN,1,1,partial.envelope,()=>inject(partial.receipt.namespace,failedIndex,oldT0)));} + finally{reset();} + same(anchor.kind,'unknown');if(!anchor.transaction)throw Error('partial D lost original transaction');await partial.owner.close();const damaged=await rows(partial.receipt);reopened=await open(partial.receipt);await rejects(()=>query(reopened,partialN,0,1),'recovery_required');await reopened.close();sameRows(await rows(partial.receipt),damaged); + passed('actual partial D completion with a missing request index is retained as unknown and cannot become absence or replacement'); + const cancelN=226,cancel=await setup(cancelN);await cancel.owner.abandon_envelope(cancelN,1,1,cancel.envelope);cancel.envelope.free();cancel.owner.abandon_close();await cancel.owner.close();reopened=await open(cancel.receipt);const completed=await query(reopened,cancelN,0,1);if(!completed)throw Error('lost observer discarded accepted original D input');await reopened.close(); + passed('accepted D owns its bounded candidate through observer loss, original disposal and cancelled close'); + return{tests}; + } + throw Error('unknown delivery profile'); + } finally {reset();} + },profile); + if(result.receipt) { + const message={kind:'delivery_query',...result.receipt,operation:result.operation,index:result.index,id:result.id}; + const worker=await page.evaluate(message=>new Promise((resolve,reject)=>{const worker=new Worker('./worker.js',{type:'module'});worker.onmessage=e=>{if(e.data.ready){worker.postMessage(message);return;}worker.terminate();resolve(e.data);};worker.onerror=error=>{worker.terminate();reject(Error(error.message));};}),message); + assert.deepEqual(worker,{report:result.golden});result.tests.push('actual worker termination and fresh WASM preserve the exact original D report'); + await page.reload(); + const report=await page.evaluate(async r=>{const{default:init,WorkspaceFixture}=await import('./fixture.js');await init();const owner=await WorkspaceFixture.open(r.receipt.n,new Uint8Array(r.receipt.fingerprint),new Uint8Array(r.receipt.selection),new Uint8Array(r.receipt.proof));const report=JSON.parse(await owner.query_envelope(r.operation,r.index,r.id));await owner.close();return report;},result); + assert.deepEqual(report,result.golden);result.tests.push('actual page and WASM restart preserve original D bytes, policy, selection and transaction'); + delete result.receipt;delete result.golden; + } + return result; +} diff --git a/crates/storage_indexeddb/tests/browser/run.mjs b/crates/storage_indexeddb/tests/browser/run.mjs @@ -3,13 +3,15 @@ import { createRequire } from 'node:module'; import { createServer } from 'node:http'; import { readFile } from 'node:fs/promises'; import { resolve, sep } from 'node:path'; +import { runDelivery } from './delivery.mjs'; // Caller supplies its installed Playwright package and an external generated // WASM directory. No dependency installation or generated bytes live here. -const [moduleRoot, packageDir, engine = 'chromium', legacyPackageDir] = process.argv.slice(2); +const [moduleRoot, packageDir, engine = 'chromium', legacyPackageDir, profile = 'base'] = process.argv.slice(2); assert(moduleRoot && packageDir && legacyPackageDir, 'usage: node run.mjs PLAYWRIGHT_PACKAGE WASM_DIRECTORY ENGINE V1_WASM_DIRECTORY'); const playwright = createRequire(resolve(moduleRoot, 'package.json'))('playwright'); assert(['chromium','firefox','webkit'].includes(engine)); +assert(['base','delivery','delivery_limits','delivery_faults','delivery_corruption'].includes(profile)); const root = resolve(packageDir); const legacyRoot = resolve(legacyPackageDir); const server = createServer(async (req,res) => { @@ -32,6 +34,7 @@ const server = createServer(async (req,res) => { if(event.data?.kind==='plan_query'){const m=event.data;const owner=await WorkspaceFixture.open(m.n,new Uint8Array(m.fingerprint),new Uint8Array(m.selection),new Uint8Array(m.proof));const plan=Array.from(await owner.plan_query(180));await owner.close();self.postMessage({plan});return;} if(event.data?.kind==='issuance_query'){const m=event.data;const owner=await WorkspaceFixture.open(m.n,new Uint8Array(m.fingerprint),new Uint8Array(m.selection),new Uint8Array(m.proof));const claim=Array.from(await owner.attempt(190,0)),seal=Array.from(await owner.captured(190,0));await owner.close();self.postMessage({claim,seal});return;} if(event.data?.kind==='installation_query'){const m=event.data;const owner=await WorkspaceFixture.open(m.n,new Uint8Array(m.fingerprint),new Uint8Array(m.selection),new Uint8Array(m.proof));const state=JSON.parse(await owner.snapshot(200));await owner.close();self.postMessage({state});return;} + if(event.data?.kind==='delivery_query'){const m=event.data;const owner=await WorkspaceFixture.open(m.n,new Uint8Array(m.fingerprint),new Uint8Array(m.selection),new Uint8Array(m.proof));const report=JSON.parse(await owner.query_envelope(m.operation,m.index,m.id));await owner.close();self.postMessage({report});return;} if(event.data?.kind==='persistent_create'){const provider=await Persistent.create(90);const fingerprint=provider.fingerprint();const encoded=await provider.protect(new Uint8Array([90,91,92]));await provider.close();self.postMessage({fingerprint:Array.from(fingerprint),encoded:Array.from(encoded)});return;} if(event.data?.kind==='persistent_open'){const provider=await Persistent.open(90,new Uint8Array(event.data.fingerprint));const plain=await provider.reveal(new Uint8Array(event.data.encoded));await provider.close();self.postMessage({plain:Array.from(plain)});return;} await protected_round_trip(79);const db=await Fixture.open(77,true);await db.write(1,undefined,new Uint8Array([77]));await db.close();const again=await Fixture.open(77,false);const value=await again.read(1);await again.close();self.postMessage({value:Array.from(value)});}catch(error){self.postMessage({error:String(error)});}}; @@ -51,6 +54,7 @@ try { page.on('pageerror',error=>console.error('PAGE_ERROR',error.message)); page.on('console',message=>{if(message.type()==='log')console.log(message.text());}); await page.goto(`http://127.0.0.1:${server.address().port}/index.html`); + if(profile==='base') { const result=await page.evaluate(async()=>{ const {default:init,Fixture,Persistent,WorkspaceFixture,protected_round_trip}=await import('./fixture.js'); await init(); console.log('STEP wasm_initialized'); @@ -709,6 +713,10 @@ try { const installationRestart=await page.evaluate(async r=>{const {default:init,WorkspaceFixture}=await import('./fixture.js');await init();const owner=await WorkspaceFixture.open(r.n,new Uint8Array(r.fingerprint),new Uint8Array(r.selection),new Uint8Array(r.proof));const state=JSON.parse(await owner.snapshot(200));await owner.close();return state;},result.installation);assert.deepEqual(installationRestart,result.installation.installedState);result.tests.push('original installation and per-recipient work survive actual page and WASM restart');delete result.installation; const issuanceRestart=await page.evaluate(async r=>{const {default:init,WorkspaceFixture}=await import('./fixture.js');await init();const owner=await WorkspaceFixture.open(r.n,new Uint8Array(r.fingerprint),new Uint8Array(r.selection),new Uint8Array(r.proof));const claim=Array.from(await owner.attempt(190,0)),seal=Array.from(await owner.captured(190,0));await owner.close();return {claim,seal};},result.issuance);assert.deepEqual(issuanceRestart,{claim:result.issuance.originalClaim,seal:result.issuance.originalSeal});result.tests.push('original claim and captured signature survive actual page and WASM restart');delete result.issuance; console.log(JSON.stringify({engine,version:browser.version(),...result})); + } else { + const result=await runDelivery(page,profile); + console.log(JSON.stringify({engine,version:browser.version(),profile,...result})); + } } finally { clearTimeout(deadline); await browser?.close();await new Promise(resolve=>server.close(resolve)); diff --git a/crates/storage_indexeddb/tests/browser/src/lib.rs b/crates/storage_indexeddb/tests/browser/src/lib.rs @@ -10,7 +10,7 @@ pub use protected::protected_round_trip; mod persistent; pub use persistent::Persistent; mod workspace; -pub use workspace::{ObtainedSeal, WorkspaceFixture}; +pub use workspace::{ObtainedEnvelope, ObtainedSeal, WorkspaceFixture}; mod snapshot; pub use snapshot::Frozen; diff --git a/crates/storage_indexeddb/tests/browser/src/workspace.rs b/crates/storage_indexeddb/tests/browser/src/workspace.rs @@ -1,7 +1,9 @@ //! Synthetic isolated host-effect fixture, never a production custody API. +mod delivery; mod installation; mod issuance; mod plans; +pub use delivery::ObtainedEnvelope; use futures::FutureExt; pub use issuance::ObtainedSeal; use radroots_domain::{ diff --git a/crates/storage_indexeddb/tests/browser/src/workspace/delivery.rs b/crates/storage_indexeddb/tests/browser/src/workspace/delivery.rs @@ -0,0 +1,274 @@ +//! Actual protected D ports using independently signed synthetic carriers. +use super::{WorkspaceFixture, copied, plans::BrowserRng}; +use core::num::NonZeroU64; +use futures::FutureExt; +use nostr::{JsonUtil, nips::nip44}; +use radroots_domain::{OperationId, PublicKey}; +use radroots_storage::command::{ + ClaimAttemptRequest, DeliveryAttempt, DeliveryAttemptOutcome, DeliveryAttemptRequest, + DeliveryIntent, EnvelopeCandidate, +}; +use radroots_transport::{ + Target, TargetSet, + policy::{SatisfactionClass, SatisfactionPolicy, TargetPolicy}, + sink::DeliveryRequestId, +}; +use serde_json::{Value, json}; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub struct ObtainedEnvelope { + candidate: EnvelopeCandidate, +} +#[wasm_bindgen] +impl ObtainedEnvelope { + pub fn event(&self) -> String { + self.candidate.event_id().to_string() + } + pub fn raw(&self) -> String { + self.candidate.raw_json().into() + } +} +fn summary(report: &DeliveryAttempt) -> Value { + json!({"request":report.intent().request_id().as_str(),"event":report.intent().event_id().to_string(),"transaction":report.transaction_id().to_string(),"at":report.retained_at().get(),"raw":report.raw_json(),"digest":hex::encode(report.envelope_digest()),"policy":hex::encode(report.intent().publication_policy_digest()),"targets":report.intent().targets().targets().iter().map(|target|target.uri().as_str()).collect::<Vec<_>>(),"selection":report.intent().selected_targets().map(|target|target.uri().as_str()).collect::<Vec<_>>(),"ordinals":report.target_ordinals()}) +} +fn outcome(value: DeliveryAttemptOutcome) -> Value { + match value { + DeliveryAttemptOutcome::Claimed(report) => { + json!({"kind":"claimed","report":summary(&report)}) + } + DeliveryAttemptOutcome::Retained(report) => { + json!({"kind":"retained","report":summary(&report)}) + } + DeliveryAttemptOutcome::Unknown { operation, attempt } => { + json!({"kind":"unknown","operation":operation.operation_id().to_string(),"command":operation.command_digest().to_string(),"transaction":operation.transaction_id().map(|tx|tx.to_string()),"request":attempt.as_str()}) + } + } +} +impl WorkspaceFixture { + async fn delivery_request( + &self, + n: u8, + id: u8, + selection: u16, + mode: u8, + envelope: &ObtainedEnvelope, + ) -> Result<DeliveryAttemptRequest, JsValue> { + let request = self.request(n, false).await?; + let mut relays = super::plans::delivery_relays(mode & 15 == 6); + if mode & 15 == 4 { + relays[1] = "wss://unapproved.fixture.example/".into(); + } + let targets = TargetSet::new( + relays + .iter() + .map(|relay| Target::nostr_relay(relay).unwrap()) + .collect(), + ) + .unwrap(); + let policy = SatisfactionPolicy::new( + SatisfactionClass::Accepted, + if mode & 15 == 5 { + TargetPolicy::any() + } else { + TargetPolicy::all() + }, + ); + let selected = TargetSet::new( + targets + .targets() + .iter() + .enumerate() + .filter(|(index, _)| selection & (1 << index) != 0) + .map(|(_, target)| target.clone()) + .collect(), + ) + .unwrap(); + let intent = DeliveryIntent::new( + &hex::encode([id; 32]), + envelope.candidate.event_id(), + &targets, + &policy, + NonZeroU64::new(1_800_000).unwrap(), + ) + .unwrap() + .with_selection(&selected) + .unwrap(); + DeliveryAttemptRequest::new(request.binding().clone(), envelope.candidate.work(), intent) + .map_err(|error| JsValue::from_str(error.code())) + } +} +#[wasm_bindgen] +impl WorkspaceFixture { + pub async fn envelope(&self, n: u8, index: u8, mode: u8) -> Result<ObtainedEnvelope, JsValue> { + let request = self.request(n, false).await?; + let snapshot = self + .owner + .operation_snapshot( + request.binding().clone(), + OperationId::from_bytes([n; 32]), + Some(request.command().digest()), + copied(&self.proof), + ) + .await + .map_err(|error| JsValue::from_str(error.code()))? + .ok_or(JsValue::from_str("missing_operation"))?; + let recipient = snapshot + .bound_plan() + .unwrap() + .plan() + .seals() + .get(index as usize) + .ok_or(JsValue::from_str("invalid_recipient"))? + .recipient(); + let work = snapshot + .publications() + .iter() + .find(|work| work.evidence().recipient() == recipient) + .copied() + .ok_or(JsValue::from_str("missing_work"))?; + let retained = self + .owner + .obtained_evidence( + ClaimAttemptRequest::new( + request.binding().clone(), + OperationId::from_bytes([n; 32]), + request.command().digest(), + *snapshot.bound_plan().unwrap().plan().digest(), + recipient, + ), + copied(&self.proof), + ) + .await + .map_err(|error| JsValue::from_str(error.code()))? + .ok_or(JsValue::from_str("missing_output"))?; + let context = nostr::secp256k1::Secp256k1::new(); + let mut scalar = [0; 32]; + scalar[31] = if mode == 3 { 1 } else { 3 }; + let keys = + nostr::Keys::new_with_ctx(&context, nostr::SecretKey::from_slice(&scalar).unwrap()); + let recipient = nostr::PublicKey::from_slice(recipient.as_bytes()).unwrap(); + let plain = if mode == 2 { + "different original seal" + } else { + retained.evidence().raw_json() + }; + let encrypted = nip44::encrypt_with_rng( + &mut BrowserRng, + keys.secret_key(), + &recipient, + plain, + nip44::Version::V2, + ) + .unwrap(); + let event = nostr::UnsignedEvent { + id: None, + pubkey: keys.public_key(), + created_at: nostr::Timestamp::from_secs(900), + kind: nostr::Kind::GiftWrap, + tags: nostr::Tags::from_list(vec![nostr::Tag::public_key(recipient)]), + content: encrypted, + } + .sign_with_ctx(&context, &mut BrowserRng, &keys) + .unwrap(); + event.verify_with_ctx(&context).unwrap(); + if mode == 1 { + scalar[31] = 4; + } + let candidate = EnvelopeCandidate::from_obtained(work, &event.as_json(), &scalar) + .map_err(|error| JsValue::from_str(error.code()))?; + Ok(ObtainedEnvelope { candidate }) + } + pub async fn store_envelope( + &self, + n: u8, + id: u8, + selection: u16, + mode: u8, + envelope: &ObtainedEnvelope, + ) -> Result<String, JsValue> { + let request = self + .delivery_request(n, id, selection, mode, envelope) + .await?; + self.clock.after.set(match mode & 15 { + 1 => Some(900), + 2 => Some(2000), + 3 => Some(1800), + _ => None, + }); + self.owner + .prepare_delivery_attempt( + request, + copied(&self.proof), + if mode & 16 != 0 { + None + } else { + Some(&envelope.candidate) + }, + ) + .await + .map(|value| outcome(value).to_string()) + .map_err(|error| JsValue::from_str(error.code())) + } + pub async fn store_envelope_unknown( + &self, + n: u8, + id: u8, + selection: u16, + envelope: &ObtainedEnvelope, + hook: js_sys::Function, + ) -> Result<String, JsValue> { + let request = self.delivery_request(n, id, selection, 0, envelope).await?; + hook.call0(&JsValue::NULL)?; + self.owner + .prepare_delivery_attempt(request, copied(&self.proof), Some(&envelope.candidate)) + .await + .map(|value| outcome(value).to_string()) + .map_err(|error| JsValue::from_str(error.code())) + } + pub async fn abandon_envelope( + &self, + n: u8, + id: u8, + selection: u16, + envelope: &ObtainedEnvelope, + ) -> Result<(), JsValue> { + let request = self.delivery_request(n, id, selection, 0, envelope).await?; + if self + .owner + .prepare_delivery_attempt(request, copied(&self.proof), Some(&envelope.candidate)) + .now_or_never() + .is_some() + { + return Err(JsValue::from_str("unexpected_immediate_delivery_retention")); + } + Ok(()) + } + pub async fn query_envelope(&self, n: u8, index: u8, id: u8) -> Result<String, JsValue> { + let request = self.request(n, false).await?; + let mut recipients = vec![self.input.participant(), request.command().peer()]; + recipients.sort_unstable(); + recipients.dedup(); + let recipient: PublicKey = *recipients + .get(index as usize) + .ok_or(JsValue::from_str("invalid_recipient"))?; + self.owner + .delivery_attempt( + request.binding().clone(), + OperationId::from_bytes([n; 32]), + request.command().digest(), + recipient, + DeliveryRequestId::parse(hex::encode([id; 32])).unwrap(), + copied(&self.proof), + ) + .await + .map(|value| { + value + .as_ref() + .map(summary) + .unwrap_or(Value::Null) + .to_string() + }) + .map_err(|error| JsValue::from_str(error.code())) + } +} diff --git a/crates/storage_indexeddb/tests/browser/src/workspace/plans.rs b/crates/storage_indexeddb/tests/browser/src/workspace/plans.rs @@ -45,6 +45,18 @@ impl RngCore for BrowserRng { } } impl CryptoRng for BrowserRng {} +pub(super) fn delivery_relays(large: bool) -> Vec<String> { + if large { + (0..16) + .map(|index| format!("wss://plan.fixture.example/{index}/{}", "x".repeat(1900))) + .collect() + } else { + vec![ + "wss://plan.fixture.example/a".into(), + "wss://plan.fixture.example/b".into(), + ] + } +} fn summary(bound: BoundSigningPlan) -> Vec<u8> { let mut bytes = bound.transaction_id().as_bytes().to_vec(); bytes.extend_from_slice(bound.plan().digest()); @@ -53,9 +65,8 @@ fn summary(bound: BoundSigningPlan) -> Vec<u8> { bytes.extend_from_slice(bound.prepared().transaction_id().as_bytes()); bytes } -#[wasm_bindgen] impl WorkspaceFixture { - pub async fn new_plan(&self, n: u8) -> Result<Vec<u8>, JsValue> { + async fn plan_with_relays(&self, n: u8, profile: u8) -> Result<Vec<u8>, JsValue> { std::panic::set_hook(Box::new(|info| fixture_panic(&format!("{info}")))); let request = self.request(n, false).await?; let command = request.command(); @@ -100,13 +111,22 @@ impl WorkspaceFixture { ) .unwrap(), ); - let relay = "wss://plan.fixture.example/"; + let relays = if profile != 0 { + delivery_relays(profile == 2) + } else { + vec![String::from("wss://plan.fixture.example/")] + }; let source = nostr::UnsignedEvent { id: None, pubkey: public, created_at: nostr::Timestamp::from_secs(999), kind: nostr::Kind::Custom(10050), - tags: nostr::Tags::from_list(vec![nostr::Tag::parse(["relay", relay]).unwrap()]), + tags: nostr::Tags::from_list( + relays + .iter() + .map(|relay| nostr::Tag::parse(["relay", relay.as_str()]).unwrap()) + .collect(), + ), content: "synthetic public route metadata ".repeat(1000), } .sign_with_ctx(&context, &mut rng, &receiver) @@ -117,7 +137,7 @@ impl WorkspaceFixture { *recipient, EnvelopeEventId::from_bytes(*source.id.as_bytes()), &source.as_json(), - &[relay.into()], + &relays, ) .unwrap(), ); @@ -129,6 +149,18 @@ impl WorkspaceFixture { .to_vec(), ) } +} +#[wasm_bindgen] +impl WorkspaceFixture { + pub async fn new_plan(&self, n: u8) -> Result<Vec<u8>, JsValue> { + self.plan_with_relays(n, 0).await + } + pub async fn new_delivery_plan(&self, n: u8) -> Result<Vec<u8>, JsValue> { + self.plan_with_relays(n, 1).await + } + pub async fn new_large_delivery_plan(&self, n: u8) -> Result<Vec<u8>, JsValue> { + self.plan_with_relays(n, 2).await + } pub async fn bind_plan(&self, n: u8, wire: Vec<u8>) -> Result<Vec<u8>, JsValue> { self.bind_plan_mode(n, wire, 0).await }