mirror of
https://github.com/block/buzz.git
synced 2026-12-01 13:42:45 +00:00
feat(relay): import owner section workspace
Co-authored-by: Larry <loganj+larry@squareup.com> Signed-off-by: Logan Johnson <loganj@squareup.com>
This commit is contained in:
@@ -22,7 +22,7 @@ pub fn filters_match(filters: &[Filter], event: &StoredEvent) -> bool {
|
||||
/// a known event id) still cannot read another user's private event.
|
||||
pub fn reader_authorized_for_event(event: &nostr::Event, reader_pubkey_hex: &str) -> bool {
|
||||
let kind = crate::kind::event_kind_u32(event);
|
||||
if kind != crate::kind::KIND_DM_VISIBILITY && kind != crate::kind::KIND_AGENT_TURN_METRIC {
|
||||
if !crate::kind::RESULT_GATED_KINDS.contains(&kind) {
|
||||
return true;
|
||||
}
|
||||
let p = nostr::SingleLetterTag::lowercase(nostr::Alphabet::P);
|
||||
|
||||
@@ -73,6 +73,10 @@ pub const KIND_USER_STATUS: u32 = 30315;
|
||||
/// Stored globally (channel_id = NULL); user-owned personal data, not channel-scoped.
|
||||
/// Content is NIP-44 encrypted to the user's own keypair.
|
||||
pub const KIND_READ_STATE: u32 = 30078;
|
||||
/// NIP-SW: owner-signed revision-zero section workspace import command.
|
||||
pub const KIND_SECTION_WORKSPACE_IMPORT: u32 = 9050;
|
||||
/// NIP-SW: relay-signed, reader-specific current section workspace projection.
|
||||
pub const KIND_SECTION_WORKSPACE_PROJECTION: u32 = 30623;
|
||||
/// NIP-42 auth event — never stored (carries bearer tokens).
|
||||
pub const KIND_AUTH: u32 = 22242;
|
||||
/// BUD-01: Blossom upload auth (used in upload.rs, not stored).
|
||||
@@ -139,7 +143,12 @@ pub const AUTHOR_ONLY_KINDS: &[u32] = &[
|
||||
///
|
||||
/// Used by `filter_can_match_result_gated_kinds` to force the per-event
|
||||
/// fallback path in COUNT rather than the fast SQL `count_events()`.
|
||||
pub const RESULT_GATED_KINDS: &[u32] = &[KIND_DM_VISIBILITY, KIND_AGENT_TURN_METRIC];
|
||||
pub const RESULT_GATED_KINDS: &[u32] = &[
|
||||
KIND_DM_VISIBILITY,
|
||||
KIND_AGENT_TURN_METRIC,
|
||||
KIND_SECTION_WORKSPACE_IMPORT,
|
||||
KIND_SECTION_WORKSPACE_PROJECTION,
|
||||
];
|
||||
|
||||
/// Kinds whose stored events have `#p`-bound read access — readable only by
|
||||
/// subscribers whose pubkey appears in the event's `#p` tag.
|
||||
@@ -162,12 +171,20 @@ pub const P_GATED_KINDS: &[u32] = &[
|
||||
KIND_MEMBER_REMOVED_NOTIFICATION,
|
||||
KIND_GIFT_WRAP,
|
||||
KIND_DM_VISIBILITY,
|
||||
KIND_SECTION_WORKSPACE_IMPORT,
|
||||
KIND_SECTION_WORKSPACE_PROJECTION,
|
||||
// NIP-AM: agent turn metrics are encrypted to the owner and must not be
|
||||
// readable by any unauthenticated or non-owner party, including via `ids`
|
||||
// filters — see NIP-AM §Relay Behavior.
|
||||
KIND_AGENT_TURN_METRIC,
|
||||
];
|
||||
|
||||
/// Immutable Stage 1 section-workspace migration records.
|
||||
pub const IMMUTABLE_SECTION_WORKSPACE_KINDS: &[u32] = &[
|
||||
KIND_SECTION_WORKSPACE_IMPORT,
|
||||
KIND_SECTION_WORKSPACE_PROJECTION,
|
||||
];
|
||||
|
||||
/// NIP-AP: Agent Persona (parameterized replaceable, owner-authored).
|
||||
///
|
||||
/// Persona definition event published by the workspace owner. Addressed by
|
||||
@@ -822,6 +839,7 @@ pub const fn is_command_kind(kind: u32) -> bool {
|
||||
| KIND_WORKFLOW_TRIGGER
|
||||
| KIND_APPROVAL_GRANT
|
||||
| KIND_APPROVAL_DENY
|
||||
| KIND_SECTION_WORKSPACE_IMPORT
|
||||
)
|
||||
}
|
||||
|
||||
@@ -834,6 +852,7 @@ pub const fn is_relay_only_kind(kind: u32) -> bool {
|
||||
| KIND_CHANNEL_SUMMARY
|
||||
| KIND_PRESENCE_SNAPSHOT
|
||||
| KIND_DM_VISIBILITY
|
||||
| KIND_SECTION_WORKSPACE_PROJECTION
|
||||
| KIND_THREAD_SUMMARY
|
||||
| KIND_WINDOW_BOUNDS
|
||||
)
|
||||
|
||||
@@ -38,6 +38,7 @@ pub mod presence;
|
||||
pub mod private_managed_agent;
|
||||
/// Canonical relay runtime identities.
|
||||
pub mod relay;
|
||||
pub mod section_workspace;
|
||||
/// Tenant identity — the server-resolved community key carried on scoped paths.
|
||||
pub mod tenant;
|
||||
/// Schnorr signature and event ID verification.
|
||||
|
||||
181
crates/buzz-core/src/section_workspace.rs
Normal file
181
crates/buzz-core/src/section_workspace.rs
Normal file
@@ -0,0 +1,181 @@
|
||||
//! Stage-one owner import and reader-specific section-workspace projection.
|
||||
#![allow(missing_docs)]
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashSet;
|
||||
use thiserror::Error;
|
||||
use uuid::Uuid;
|
||||
pub const VERSION: u32 = 1;
|
||||
pub const MAX_SECTIONS: usize = 100;
|
||||
pub const MAX_ASSIGNMENTS: usize = 1_000;
|
||||
pub const MAX_ENCRYPTED_METADATA_BYTES: usize = 65_535;
|
||||
pub const MAX_KEY_ENVELOPE_BYTES: usize = 4_096;
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct ImportSection {
|
||||
pub id: Uuid,
|
||||
pub rank: u32,
|
||||
pub encrypted_label: String,
|
||||
pub encrypted_icon: Option<String>,
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct ImportAssignment {
|
||||
pub channel_id: Uuid,
|
||||
pub section_id: Uuid,
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Import {
|
||||
pub version: u32,
|
||||
pub action_id: Uuid,
|
||||
pub source_event_id: String,
|
||||
pub source_hash: String,
|
||||
pub key_epoch: u64,
|
||||
pub owner_key_envelope: String,
|
||||
pub sections: Vec<ImportSection>,
|
||||
pub assignments: Vec<ImportAssignment>,
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct MigrationMarker {
|
||||
pub source_event_id: String,
|
||||
pub source_hash: String,
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct ProjectedAssignment {
|
||||
pub channel_id: Uuid,
|
||||
pub section_id: Uuid,
|
||||
pub revision: u64,
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Projection {
|
||||
pub version: u32,
|
||||
pub owner_pubkey: String,
|
||||
pub revision: u64,
|
||||
pub layout_revision: u64,
|
||||
pub key_epoch: u64,
|
||||
pub migration: MigrationMarker,
|
||||
pub reader_key_envelope: String,
|
||||
pub sections: Vec<ImportSection>,
|
||||
pub assignments: Vec<ProjectedAssignment>,
|
||||
}
|
||||
impl Import {
|
||||
pub fn validate(&self) -> Result<(), ValidationError> {
|
||||
if self.version != VERSION {
|
||||
return Err(ValidationError::Invalid("version"));
|
||||
}
|
||||
if self.action_id.is_nil() {
|
||||
return Err(ValidationError::Invalid("action_id"));
|
||||
}
|
||||
if self.key_epoch != 1 {
|
||||
return Err(ValidationError::Invalid("key_epoch"));
|
||||
}
|
||||
hex32(&self.source_event_id, "source_event_id")?;
|
||||
hex32(&self.source_hash, "source_hash")?;
|
||||
bounded(
|
||||
&self.owner_key_envelope,
|
||||
MAX_KEY_ENVELOPE_BYTES,
|
||||
"owner_key_envelope",
|
||||
)?;
|
||||
if self.sections.len() > MAX_SECTIONS {
|
||||
return Err(ValidationError::TooMany("sections", MAX_SECTIONS));
|
||||
}
|
||||
if self.assignments.len() > MAX_ASSIGNMENTS {
|
||||
return Err(ValidationError::TooMany("assignments", MAX_ASSIGNMENTS));
|
||||
}
|
||||
let mut ids = HashSet::with_capacity(self.sections.len());
|
||||
let mut ranks = HashSet::with_capacity(self.sections.len());
|
||||
for section in &self.sections {
|
||||
if section.id.is_nil() || !ids.insert(section.id) {
|
||||
return Err(ValidationError::Invalid("section id"));
|
||||
}
|
||||
if section.rank as usize >= self.sections.len() || !ranks.insert(section.rank) {
|
||||
return Err(ValidationError::Invalid("section rank"));
|
||||
}
|
||||
bounded(
|
||||
§ion.encrypted_label,
|
||||
MAX_ENCRYPTED_METADATA_BYTES,
|
||||
"encrypted_label",
|
||||
)?;
|
||||
if let Some(icon) = §ion.encrypted_icon {
|
||||
bounded(icon, MAX_ENCRYPTED_METADATA_BYTES, "encrypted_icon")?;
|
||||
}
|
||||
}
|
||||
let mut channels = HashSet::with_capacity(self.assignments.len());
|
||||
if self.assignments.iter().any(|assignment| {
|
||||
assignment.channel_id.is_nil()
|
||||
|| !channels.insert(assignment.channel_id)
|
||||
|| !ids.contains(&assignment.section_id)
|
||||
}) {
|
||||
return Err(ValidationError::Invalid("assignment"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn owner_projection(&self, owner_pubkey: String) -> Projection {
|
||||
Projection {
|
||||
version: VERSION,
|
||||
owner_pubkey,
|
||||
revision: 1,
|
||||
layout_revision: 1,
|
||||
key_epoch: self.key_epoch,
|
||||
migration: MigrationMarker {
|
||||
source_event_id: self.source_event_id.clone(),
|
||||
source_hash: self.source_hash.clone(),
|
||||
},
|
||||
reader_key_envelope: self.owner_key_envelope.clone(),
|
||||
sections: self.sections.clone(),
|
||||
assignments: self
|
||||
.assignments
|
||||
.iter()
|
||||
.map(|assignment| ProjectedAssignment {
|
||||
channel_id: assignment.channel_id,
|
||||
section_id: assignment.section_id,
|
||||
revision: 1,
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn validate_import_tags(
|
||||
tags: &[nostr::Tag],
|
||||
owner: &str,
|
||||
action_id: Uuid,
|
||||
) -> Result<(), ValidationError> {
|
||||
let expected_action = action_id.to_string();
|
||||
let fields: Vec<Vec<String>> = tags.iter().map(|tag| tag.as_slice().to_vec()).collect();
|
||||
if fields
|
||||
!= [
|
||||
vec!["p".into(), owner.into()],
|
||||
vec!["action".into(), expected_action],
|
||||
]
|
||||
{
|
||||
return Err(ValidationError::Invalid("tags"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn hex32(value: &str, field: &'static str) -> Result<(), ValidationError> {
|
||||
if value.len() != 64
|
||||
|| !value
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
|
||||
{
|
||||
return Err(ValidationError::Invalid(field));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn bounded(value: &str, max: usize, field: &'static str) -> Result<(), ValidationError> {
|
||||
if value.is_empty() || value.len() > max {
|
||||
return Err(ValidationError::Invalid(field));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Error)]
|
||||
pub enum ValidationError {
|
||||
#[error("invalid {0}")]
|
||||
Invalid(&'static str),
|
||||
#[error("{0} exceeds maximum {1}")]
|
||||
TooMany(&'static str, usize),
|
||||
}
|
||||
Reference in New Issue
Block a user