commit 4bf3178cd52c2200e3bbbfc471e0d268b5627c8c parent cd171a17c5266c34af010527337f9352786edbd2 Author: triesap <tyson@radroots.org> Date: Tue, 14 Jul 2026 08:31:08 +0000 event-codec: migrate wire parts and drafts - move unsigned wire parts to the event foundation - remove codec frozen-draft construction - make event drafts typed private state - refresh codec validation fixtures Diffstat:
66 files changed, 711 insertions(+), 492 deletions(-)
diff --git a/crates/event/src/draft.rs b/crates/event/src/draft.rs @@ -17,7 +17,10 @@ use crate::wire::{ RadrootsCanonicalEventIdError, RadrootsEventWireError, RadrootsNip01EventWire, canonical_nip01_event_id_preimage, compute_canonical_nip01_event_id, }; -use crate::{RadrootsEventEnvelope, RadrootsEventEnvelopeError}; +use crate::{ + RadrootsEventEnvelope, RadrootsEventEnvelopeError, RadrootsEventKind, RadrootsEventTags, + RadrootsEventTimestamp, +}; use core::fmt; #[derive(Clone, Debug, PartialEq, Eq)] @@ -41,7 +44,7 @@ pub enum RadrootsDraftError { actual_event_id: String, }, SignedEventCreatedAtMismatch { - expected_created_at: u32, + expected_created_at: u64, actual_created_at: u64, }, SignedEventKindMismatch { @@ -62,6 +65,7 @@ pub enum RadrootsDraftError { }, IdParse(RadrootsIdParseError), CanonicalEventId(RadrootsCanonicalEventIdError), + Envelope(RadrootsEventEnvelopeError), SignedEvent(RadrootsSignedEventError), } @@ -135,6 +139,7 @@ impl fmt::Display for RadrootsDraftError { ), Self::IdParse(error) => write!(f, "{error}"), Self::CanonicalEventId(error) => write!(f, "{error}"), + Self::Envelope(error) => write!(f, "{error}"), Self::SignedEvent(error) => write!(f, "{error}"), } } @@ -158,6 +163,12 @@ impl From<RadrootsCanonicalEventIdError> for RadrootsDraftError { } } +impl From<RadrootsEventEnvelopeError> for RadrootsDraftError { + fn from(value: RadrootsEventEnvelopeError) -> Self { + Self::Envelope(value) + } +} + impl From<RadrootsSignedEventError> for RadrootsDraftError { fn from(value: RadrootsSignedEventError) -> Self { Self::SignedEvent(value) @@ -170,21 +181,21 @@ impl From<RadrootsSignedEventError> for RadrootsDraftError { )] #[derive(Clone, Debug, PartialEq, Eq)] pub struct RadrootsEventDraft { - pub contract_id: String, - pub contract_registry_version: u32, - pub kind: u32, - pub created_at: u32, - pub tags: Vec<Vec<String>>, - pub content: String, - pub expected_pubkey: String, - pub expected_event_id: String, + contract_id: String, + contract_registry_version: u32, + kind: RadrootsEventKind, + created_at: RadrootsEventTimestamp, + tags: RadrootsEventTags, + content: String, + expected_pubkey: RadrootsPublicKey, + expected_event_id: RadrootsEventId, } impl RadrootsEventDraft { pub fn new( contract_id: impl Into<String>, kind: u32, - created_at: u32, + created_at: u64, tags: Vec<Vec<String>>, content: impl Into<String>, expected_pubkey: impl AsRef<str>, @@ -201,7 +212,7 @@ impl RadrootsEventDraft { actual_kind: kind, }); } - let expected_pubkey = RadrootsPublicKey::parse(expected_pubkey.as_ref())?.into_string(); + let expected_pubkey = RadrootsPublicKey::parse(expected_pubkey.as_ref())?; let content = content.into(); validate_event_contract_parts(kind, &tags, content.as_str(), contract.id).map_err( |error| RadrootsDraftError::ContractShape { @@ -209,15 +220,20 @@ impl RadrootsEventDraft { error, }, )?; - let expected_event_id = - compute_nip01_event_id(expected_pubkey.as_str(), created_at, kind, &tags, &content)? - .into_string(); + let typed_tags = RadrootsEventTags::new(tags)?; + let expected_event_id = compute_nip01_event_id( + expected_pubkey.as_str(), + created_at, + kind, + &typed_tags.to_vec(), + &content, + )?; Ok(Self { contract_id: contract.id.to_owned(), contract_registry_version: RADROOTS_EVENT_CONTRACT_REGISTRY_VERSION, - kind, - created_at, - tags, + kind: RadrootsEventKind::new(kind), + created_at: RadrootsEventTimestamp::new(created_at), + tags: typed_tags, content, expected_pubkey, expected_event_id, @@ -227,12 +243,76 @@ impl RadrootsEventDraft { pub fn nip01_preimage(&self) -> Result<String, RadrootsDraftError> { nip01_event_id_preimage( self.expected_pubkey.as_str(), - self.created_at, - self.kind, - &self.tags, + self.created_at.as_u64(), + self.kind.as_u32(), + &self.tags.to_vec(), self.content.as_str(), ) } + + #[inline] + pub fn contract_id(&self) -> &str { + self.contract_id.as_str() + } + + #[inline] + pub fn contract_registry_version(&self) -> u32 { + self.contract_registry_version + } + + #[inline] + pub fn kind(&self) -> RadrootsEventKind { + self.kind + } + + #[inline] + pub fn kind_u32(&self) -> u32 { + self.kind.as_u32() + } + + #[inline] + pub fn created_at(&self) -> RadrootsEventTimestamp { + self.created_at + } + + #[inline] + pub fn created_at_u64(&self) -> u64 { + self.created_at.as_u64() + } + + #[inline] + pub fn tags(&self) -> &RadrootsEventTags { + &self.tags + } + + pub fn tags_as_vec(&self) -> Vec<Vec<String>> { + self.tags.to_vec() + } + + #[inline] + pub fn content(&self) -> &str { + self.content.as_str() + } + + #[inline] + pub fn expected_pubkey(&self) -> &RadrootsPublicKey { + &self.expected_pubkey + } + + #[inline] + pub fn expected_pubkey_str(&self) -> &str { + self.expected_pubkey.as_str() + } + + #[inline] + pub fn expected_event_id(&self) -> &RadrootsEventId { + &self.expected_event_id + } + + #[inline] + pub fn expected_event_id_str(&self) -> &str { + self.expected_event_id.as_str() + } } #[cfg_attr( @@ -514,46 +594,47 @@ pub fn validate_signed_nostr_event_matches_draft( signed_event: &RadrootsSignedEvent, draft: &RadrootsEventDraft, ) -> Result<(), RadrootsDraftError> { - if signed_event.pubkey_str() != draft.expected_pubkey.as_str() { + if signed_event.pubkey_str() != draft.expected_pubkey_str() { return Err(RadrootsDraftError::SignedEventPubkeyMismatch { - expected_pubkey: draft.expected_pubkey.clone(), + expected_pubkey: draft.expected_pubkey_str().to_owned(), actual_pubkey: signed_event.pubkey_str().to_owned(), }); } - if signed_event.id_str() != draft.expected_event_id.as_str() { + if signed_event.id_str() != draft.expected_event_id_str() { return Err(RadrootsDraftError::SignedEventIdMismatch { - expected_event_id: draft.expected_event_id.clone(), + expected_event_id: draft.expected_event_id_str().to_owned(), actual_event_id: signed_event.id_str().to_owned(), }); } - if signed_event.created_at() != u64::from(draft.created_at) { + if signed_event.created_at() != draft.created_at_u64() { return Err(RadrootsDraftError::SignedEventCreatedAtMismatch { - expected_created_at: draft.created_at, + expected_created_at: draft.created_at_u64(), actual_created_at: signed_event.created_at(), }); } - if signed_event.kind() != draft.kind { + if signed_event.kind() != draft.kind_u32() { return Err(RadrootsDraftError::SignedEventKindMismatch { - expected_kind: draft.kind, + expected_kind: draft.kind_u32(), actual_kind: signed_event.kind(), }); } let signed_tags = signed_event.tags_as_vec(); - if signed_tags != draft.tags { + let draft_tags = draft.tags_as_vec(); + if signed_tags != draft_tags { return Err(RadrootsDraftError::SignedEventTagsMismatch { - expected_len: draft.tags.len(), + expected_len: draft_tags.len(), actual_len: signed_tags.len(), }); } - if signed_event.content() != draft.content { + if signed_event.content() != draft.content() { return Err(RadrootsDraftError::SignedEventContentMismatch { - expected_len: draft.content.len(), + expected_len: draft.content().len(), actual_len: signed_event.content().len(), }); } let computed_event_id = compute_nip01_event_id( signed_event.pubkey_str(), - draft.created_at, + draft.created_at_u64(), signed_event.kind(), &signed_tags, signed_event.content(), @@ -595,34 +676,26 @@ fn verify_bip340_signature( pub fn compute_nip01_event_id( pubkey: &str, - created_at: u32, + created_at: u64, kind: u32, tags: &[Vec<String>], content: &str, ) -> Result<RadrootsEventId, RadrootsDraftError> { RadrootsPublicKey::parse(pubkey)?; Ok(compute_canonical_nip01_event_id( - pubkey, - u64::from(created_at), - kind, - tags, - content, + pubkey, created_at, kind, tags, content, )?) } pub fn nip01_event_id_preimage( pubkey: &str, - created_at: u32, + created_at: u64, kind: u32, tags: &[Vec<String>], content: &str, ) -> Result<String, RadrootsDraftError> { Ok(canonical_nip01_event_id_preimage( - pubkey, - u64::from(created_at), - kind, - tags, - content, + pubkey, created_at, kind, tags, content, )?) } @@ -704,11 +777,11 @@ mod tests { fn signed_event_for_draft(draft: &RadrootsEventDraft) -> RadrootsSignedEvent { let wire = verified_wire( - draft.expected_pubkey.clone(), - u64::from(draft.created_at), - draft.kind, - draft.tags.clone(), - draft.content.clone(), + draft.expected_pubkey_str().to_string(), + draft.created_at_u64(), + draft.kind_u32(), + draft.tags_as_vec(), + draft.content().to_string(), hex_128('b'), ); let raw_json = raw_json_for_wire(&wire); @@ -732,7 +805,7 @@ mod tests { } #[test] - fn frozen_draft_computes_expected_event_id() { + fn draft_computes_expected_event_id() { let draft = RadrootsEventDraft::new( "radroots.social.post.v1", KIND_POST, @@ -751,7 +824,7 @@ mod tests { "[0,\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",1700000000,1,[[\"t\",\"soil\"],[\"p\",\"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"]],\"hello\"]" ); assert_eq!( - draft.expected_event_id, + draft.expected_event_id_str(), "59d2486ef5557e0e317127de55005f2863361ad4041277ae523a869f2294cf9c" ); } @@ -1056,12 +1129,12 @@ mod tests { let signed = RadrootsSignedEvent::from_wire_unchecked( unchecked_wire( - draft.expected_event_id.clone(), + draft.expected_event_id_str().to_string(), hex_64('c'), - u64::from(draft.created_at), - draft.kind, - draft.tags.clone(), - draft.content.clone(), + draft.created_at_u64(), + draft.kind_u32(), + draft.tags_as_vec(), + draft.content().to_string(), hex_128('b'), ), "{}", @@ -1077,11 +1150,11 @@ mod tests { let signed = RadrootsSignedEvent::from_wire_unchecked( unchecked_wire( hex_64('d'), - draft.expected_pubkey.clone(), - u64::from(draft.created_at), - draft.kind, - draft.tags.clone(), - draft.content.clone(), + draft.expected_pubkey_str().to_string(), + draft.created_at_u64(), + draft.kind_u32(), + draft.tags_as_vec(), + draft.content().to_string(), hex_128('b'), ), "{}", @@ -1096,12 +1169,12 @@ mod tests { let signed = RadrootsSignedEvent::from_wire_unchecked( unchecked_wire( - draft.expected_event_id.clone(), - draft.expected_pubkey.clone(), - u64::from(draft.created_at) + 1, - draft.kind, - draft.tags.clone(), - draft.content.clone(), + draft.expected_event_id_str().to_string(), + draft.expected_pubkey_str().to_string(), + draft.created_at_u64() + 1, + draft.kind_u32(), + draft.tags_as_vec(), + draft.content().to_string(), hex_128('b'), ), "{}", @@ -1116,12 +1189,12 @@ mod tests { let signed = RadrootsSignedEvent::from_wire_unchecked( unchecked_wire( - draft.expected_event_id.clone(), - draft.expected_pubkey.clone(), - u64::from(draft.created_at), + draft.expected_event_id_str().to_string(), + draft.expected_pubkey_str().to_string(), + draft.created_at_u64(), KIND_PROFILE, - draft.tags.clone(), - draft.content.clone(), + draft.tags_as_vec(), + draft.content().to_string(), hex_128('b'), ), "{}", @@ -1134,16 +1207,16 @@ mod tests { RadrootsDraftError::SignedEventKindMismatch { .. } )); - let mut tags = draft.tags.clone(); + let mut tags = draft.tags_as_vec(); tags.push(vec!["p".to_owned(), hex_64('e')]); let signed = RadrootsSignedEvent::from_wire_unchecked( unchecked_wire( - draft.expected_event_id.clone(), - draft.expected_pubkey.clone(), - u64::from(draft.created_at), - draft.kind, + draft.expected_event_id_str().to_string(), + draft.expected_pubkey_str().to_string(), + draft.created_at_u64(), + draft.kind_u32(), tags, - draft.content.clone(), + draft.content().to_string(), hex_128('b'), ), "{}", @@ -1158,11 +1231,11 @@ mod tests { let signed = RadrootsSignedEvent::from_wire_unchecked( unchecked_wire( - draft.expected_event_id.clone(), - draft.expected_pubkey.clone(), - u64::from(draft.created_at), - draft.kind, - draft.tags.clone(), + draft.expected_event_id_str().to_string(), + draft.expected_pubkey_str().to_string(), + draft.created_at_u64(), + draft.kind_u32(), + draft.tags_as_vec(), "changed".to_owned(), hex_128('b'), ), @@ -1176,16 +1249,18 @@ mod tests { RadrootsDraftError::SignedEventContentMismatch { .. } )); - let mut draft = post_draft(); - draft.expected_event_id = hex_64('f'); + let mut draft_value = serde_json::to_value(post_draft()).expect("draft json"); + draft_value["expected_event_id"] = serde_json::Value::String(hex_64('f')); + let draft: RadrootsEventDraft = + serde_json::from_value(draft_value).expect("tampered draft"); let signed = RadrootsSignedEvent::from_wire_unchecked( unchecked_wire( - draft.expected_event_id.clone(), - draft.expected_pubkey.clone(), - u64::from(draft.created_at), - draft.kind, - draft.tags.clone(), - draft.content.clone(), + draft.expected_event_id_str().to_string(), + draft.expected_pubkey_str().to_string(), + draft.created_at_u64(), + draft.kind_u32(), + draft.tags_as_vec(), + draft.content().to_string(), hex_128('b'), ), "{}", diff --git a/crates/event/src/wire.rs b/crates/event/src/wire.rs @@ -203,6 +203,17 @@ impl From<RadrootsCanonicalEventIdError> for RadrootsEventWireError { derive(serde::Serialize, serde::Deserialize) )] #[derive(Clone, Debug, PartialEq, Eq)] +pub struct RadrootsNip01EventWireParts { + pub kind: u32, + pub content: String, + pub tags: Vec<Vec<String>>, +} + +#[cfg_attr( + any(feature = "serde", test), + derive(serde::Serialize, serde::Deserialize) +)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct RadrootsNip01EventWire { pub id: String, pub pubkey: String, diff --git a/crates/event_codec/src/app_data/encode.rs b/crates/event_codec/src/app_data/encode.rs @@ -9,7 +9,7 @@ use radroots_event::app_data::{KIND_APP_DATA, RadrootsAppData}; use radroots_event::tags::TAG_D; use crate::error::EventEncodeError; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; pub fn app_data_build_tags( app_data: &RadrootsAppData, @@ -20,19 +20,21 @@ pub fn app_data_build_tags( Ok(vec![vec![TAG_D.to_string(), app_data.d_tag.clone()]]) } -pub fn to_wire_parts(app_data: &RadrootsAppData) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + app_data: &RadrootsAppData, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(app_data, KIND_APP_DATA) } pub fn to_wire_parts_with_kind( app_data: &RadrootsAppData, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_APP_DATA { return Err(EventEncodeError::InvalidKind(kind)); } let tags = app_data_build_tags(app_data)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: app_data.content.clone(), tags, diff --git a/crates/event_codec/src/article/encode.rs b/crates/event_codec/src/article/encode.rs @@ -14,7 +14,7 @@ use crate::d_tag::validate_d_tag; use crate::error::EventEncodeError; use crate::field_helpers::{push_optional_tag, push_tag, validate_non_empty_field}; use crate::social_helpers::push_location_tags; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const DEFAULT_KIND: u32 = KIND_ARTICLE; @@ -42,18 +42,20 @@ pub fn article_build_tags(article: &RadrootsArticle) -> Result<Vec<Vec<String>>, Ok(tags) } -pub fn to_wire_parts(article: &RadrootsArticle) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + article: &RadrootsArticle, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(article, DEFAULT_KIND) } pub fn to_wire_parts_with_kind( article: &RadrootsArticle, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != DEFAULT_KIND { return Err(EventEncodeError::InvalidKind(kind)); } - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: article.content.clone(), tags: article_build_tags(article)?, diff --git a/crates/event_codec/src/calendar/encode.rs b/crates/event_codec/src/calendar/encode.rs @@ -33,7 +33,7 @@ use crate::social_helpers::{ push_location_tags, push_participants, validate_date, validate_date_end_after_start, validate_end_after_start, }; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; pub fn calendar_date_event_build_tags( event: &RadrootsCalendarDateEvent, @@ -129,36 +129,36 @@ pub fn rsvp_build_tags( pub fn date_to_wire_parts( event: &RadrootsCalendarDateEvent, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { date_to_wire_parts_with_kind(event, KIND_CALENDAR_DATE_EVENT) } pub fn time_to_wire_parts( event: &RadrootsCalendarTimeEvent, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { time_to_wire_parts_with_kind(event, KIND_CALENDAR_TIME_EVENT) } pub fn calendar_to_wire_parts( calendar: &RadrootsCalendar, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { calendar_to_wire_parts_with_kind(calendar, KIND_CALENDAR) } pub fn rsvp_to_wire_parts( rsvp: &RadrootsCalendarEventRsvp, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { rsvp_to_wire_parts_with_kind(rsvp, KIND_CALENDAR_EVENT_RSVP) } pub fn date_to_wire_parts_with_kind( event: &RadrootsCalendarDateEvent, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_CALENDAR_DATE_EVENT { return Err(EventEncodeError::InvalidKind(kind)); } - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: event.description.clone().unwrap_or_default(), tags: calendar_date_event_build_tags(event)?, @@ -168,11 +168,11 @@ pub fn date_to_wire_parts_with_kind( pub fn time_to_wire_parts_with_kind( event: &RadrootsCalendarTimeEvent, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_CALENDAR_TIME_EVENT { return Err(EventEncodeError::InvalidKind(kind)); } - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: event.description.clone().unwrap_or_default(), tags: calendar_time_event_build_tags(event)?, @@ -182,11 +182,11 @@ pub fn time_to_wire_parts_with_kind( pub fn calendar_to_wire_parts_with_kind( calendar: &RadrootsCalendar, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_CALENDAR { return Err(EventEncodeError::InvalidKind(kind)); } - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: calendar.description.clone().unwrap_or_default(), tags: calendar_collection_build_tags(calendar)?, @@ -196,11 +196,11 @@ pub fn calendar_to_wire_parts_with_kind( pub fn rsvp_to_wire_parts_with_kind( rsvp: &RadrootsCalendarEventRsvp, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_CALENDAR_EVENT_RSVP { return Err(EventEncodeError::InvalidKind(kind)); } - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: rsvp.note.clone().unwrap_or_default(), tags: rsvp_build_tags(rsvp)?, diff --git a/crates/event_codec/src/comment/encode.rs b/crates/event_codec/src/comment/encode.rs @@ -16,7 +16,7 @@ use crate::error::EventEncodeError; use crate::field_helpers::{ parse_address_tag, validate_lowercase_hex_64, validate_non_empty_field, }; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const DEFAULT_KIND: u32 = KIND_COMMENT; @@ -27,14 +27,16 @@ pub fn comment_build_tags(comment: &RadrootsComment) -> Result<Vec<Vec<String>>, Ok(tags) } -pub fn to_wire_parts(comment: &RadrootsComment) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + comment: &RadrootsComment, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(comment, DEFAULT_KIND) } pub fn to_wire_parts_with_kind( comment: &RadrootsComment, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != DEFAULT_KIND { return Err(EventEncodeError::InvalidKind(kind)); } @@ -42,7 +44,7 @@ pub fn to_wire_parts_with_kind( return Err(EventEncodeError::EmptyRequiredField("content")); } let tags = comment_build_tags(comment)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: comment.content.clone(), tags, diff --git a/crates/event_codec/src/coop/encode.rs b/crates/event_codec/src/coop/encode.rs @@ -17,7 +17,7 @@ use crate::d_tag::validate_d_tag; use crate::error::EventEncodeError; #[cfg(feature = "serde_json")] -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const TAG_T: &str = "t"; const TAG_G: &str = "g"; @@ -72,7 +72,7 @@ pub fn coop_ref_tags(coop: &RadrootsCoopRef) -> Result<Vec<Vec<String>>, EventEn } #[cfg(feature = "serde_json")] -pub fn to_wire_parts(coop: &RadrootsCoop) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts(coop: &RadrootsCoop) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(coop, KIND_COOP) } @@ -80,13 +80,13 @@ pub fn to_wire_parts(coop: &RadrootsCoop) -> Result<WireEventParts, EventEncodeE pub fn to_wire_parts_with_kind( coop: &RadrootsCoop, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_COOP { return Err(EventEncodeError::InvalidKind(kind)); } let tags = coop_build_tags(coop)?; let content = serde_json::to_string(coop).map_err(|_| EventEncodeError::Json)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content, tags, diff --git a/crates/event_codec/src/document/encode.rs b/crates/event_codec/src/document/encode.rs @@ -16,7 +16,7 @@ use crate::error::EventEncodeError; use radroots_event::kinds::KIND_DOCUMENT; #[cfg(feature = "serde_json")] -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const TAG_T: &str = "t"; const TAG_P: &str = "p"; @@ -64,7 +64,9 @@ pub fn document_build_tags( } #[cfg(feature = "serde_json")] -pub fn to_wire_parts(document: &RadrootsDocument) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + document: &RadrootsDocument, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(document, KIND_DOCUMENT) } @@ -72,13 +74,13 @@ pub fn to_wire_parts(document: &RadrootsDocument) -> Result<WireEventParts, Even pub fn to_wire_parts_with_kind( document: &RadrootsDocument, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_DOCUMENT { return Err(EventEncodeError::InvalidKind(kind)); } let tags = document_build_tags(document)?; let content = serde_json::to_string(document).map_err(|_| EventEncodeError::Json)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content, tags, diff --git a/crates/event_codec/src/farm/encode.rs b/crates/event_codec/src/farm/encode.rs @@ -16,7 +16,7 @@ use crate::d_tag::validate_d_tag; use crate::error::EventEncodeError; #[cfg(feature = "serde_json")] -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const TAG_T: &str = "t"; const TAG_G: &str = "g"; @@ -82,7 +82,7 @@ pub fn farm_ref_tags(farm: &RadrootsFarmRef) -> Result<Vec<Vec<String>>, EventEn } #[cfg(feature = "serde_json")] -pub fn to_wire_parts(farm: &RadrootsFarm) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts(farm: &RadrootsFarm) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(farm, KIND_FARM) } @@ -90,13 +90,13 @@ pub fn to_wire_parts(farm: &RadrootsFarm) -> Result<WireEventParts, EventEncodeE pub fn to_wire_parts_with_kind( farm: &RadrootsFarm, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_FARM { return Err(EventEncodeError::InvalidKind(kind)); } let tags = farm_build_tags(farm)?; let content = serde_json::to_string(farm).map_err(|_| EventEncodeError::Json)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content, tags, diff --git a/crates/event_codec/src/farm/mod.rs b/crates/event_codec/src/farm/mod.rs @@ -23,6 +23,13 @@ mod tests { use radroots_event::listing::{RadrootsListing, RadrootsListingBin, RadrootsListingProduct}; use radroots_event::plot::RadrootsPlot; + const EVENT_ID: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + const AUTHOR: &str = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; + const EVENT_SIG: &str = concat!( + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" + ); + fn d_tag(raw: &str) -> RadrootsDTag { raw.parse().unwrap() } @@ -282,21 +289,20 @@ mod tests { }; let content = serde_json::to_string(&farm).expect("farm content"); let tags = vec![ - Vec::new(), vec!["d".to_string(), "AAAAAAAAAAAAAAAAAAAAAA".to_string()], vec!["g".to_string(), "9q8yy".to_string()], ]; let parsed = parsed_from_event( - "event-id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 42, KIND_FARM, content.clone(), tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .expect("parsed farm"); - assert_eq!(parsed.event.sig_str(), "sig"); + assert_eq!(parsed.event.sig_str(), EVENT_SIG); assert_eq!(parsed.data.data.name, "Test Farm"); for (tag, expected) in [ diff --git a/crates/event_codec/src/farm_crdt/encode.rs b/crates/event_codec/src/farm_crdt/encode.rs @@ -16,7 +16,7 @@ use crate::field_helpers::{ validate_non_empty_field, }; #[cfg(feature = "serde_json")] -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; pub fn farm_crdt_change_build_tags( change: &RadrootsFarmCrdtChange, @@ -48,7 +48,9 @@ pub fn farm_crdt_change_build_tags_with_author( } #[cfg(feature = "serde_json")] -pub fn to_wire_parts(change: &RadrootsFarmCrdtChange) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + change: &RadrootsFarmCrdtChange, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(change, KIND_FARM_CRDT_CHANGE) } @@ -56,7 +58,7 @@ pub fn to_wire_parts(change: &RadrootsFarmCrdtChange) -> Result<WireEventParts, pub fn to_wire_parts_with_author( change: &RadrootsFarmCrdtChange, author_pubkey: &str, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind_and_author(change, KIND_FARM_CRDT_CHANGE, Some(author_pubkey)) } @@ -64,7 +66,7 @@ pub fn to_wire_parts_with_author( pub fn to_wire_parts_with_kind( change: &RadrootsFarmCrdtChange, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind_and_author(change, kind, None) } @@ -73,13 +75,13 @@ pub fn to_wire_parts_with_kind_and_author( change: &RadrootsFarmCrdtChange, kind: u32, author_pubkey: Option<&str>, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_FARM_CRDT_CHANGE { return Err(EventEncodeError::InvalidKind(kind)); } let tags = farm_crdt_change_build_tags_with_author(change, author_pubkey)?; let content = serde_json::to_string(change).map_err(|_| EventEncodeError::Json)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content, tags, diff --git a/crates/event_codec/src/farm_crdt/mod.rs b/crates/event_codec/src/farm_crdt/mod.rs @@ -28,7 +28,12 @@ mod tests { const WORKSPACE_D_TAG: &str = "AAAAAAAAAAAAAAAAAAAAAA"; const DOCUMENT_ID: &str = "AAAAAAAAAAAAAAAAAAAAAg"; const GROUP_ID: &str = "field-group"; - const AUTHOR: &str = "author_pubkey"; + const EVENT_ID: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + const AUTHOR: &str = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; + const EVENT_SIG: &str = concat!( + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" + ); #[test] fn farm_crdt_change_encodes_and_decodes_task_change() { @@ -257,7 +262,7 @@ mod tests { let parts = to_wire_parts_with_author(&change, AUTHOR).expect("crdt wire parts"); let data = data_from_event( - "event-id".to_string(), + EVENT_ID.to_string(), AUTHOR.to_string(), 99, parts.kind, @@ -265,23 +270,23 @@ mod tests { parts.tags.clone(), ) .expect("parsed data"); - assert_eq!(data.id, "event-id"); + assert_eq!(data.id, EVENT_ID); assert_eq!(data.author, AUTHOR); assert_eq!(data.published_at, 99); assert_eq!(data.kind, KIND_FARM_CRDT_CHANGE); assert_eq!(data.data, change); let parsed = parsed_from_event( - "event-id".to_string(), + EVENT_ID.to_string(), AUTHOR.to_string(), 99, parts.kind, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .expect("parsed event"); - assert_eq!(parsed.event.sig_str(), "sig"); + assert_eq!(parsed.event.sig_str(), EVENT_SIG); assert_eq!(parsed.data.data, change); let no_author_parts = to_wire_parts(&change).expect("crdt wire parts"); diff --git a/crates/event_codec/src/farm_file/encode.rs b/crates/event_codec/src/farm_file/encode.rs @@ -22,7 +22,7 @@ use crate::field_helpers::{ address_string, push_optional_tag, push_tag, push_tag_values, validate_lowercase_hex_64, validate_non_empty_field, }; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const TAG_ALT: &str = "alt"; const TAG_BLURHASH: &str = "blurhash"; @@ -82,19 +82,19 @@ pub fn farm_file_metadata_build_tags( pub fn to_wire_parts( metadata: &RadrootsFarmFileMetadata, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(metadata, KIND_FARM_FILE_METADATA) } pub fn to_wire_parts_with_kind( metadata: &RadrootsFarmFileMetadata, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_FARM_FILE_METADATA { return Err(EventEncodeError::InvalidKind(kind)); } let tags = farm_file_metadata_build_tags(metadata)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: metadata.caption.clone().unwrap_or_default(), tags, diff --git a/crates/event_codec/src/farm_file/mod.rs b/crates/event_codec/src/farm_file/mod.rs @@ -25,6 +25,12 @@ mod tests { const WORKSPACE_D_TAG: &str = "AAAAAAAAAAAAAAAAAAAAAA"; const OWNER_DOCUMENT_ID: &str = "AAAAAAAAAAAAAAAAAAAAAg"; const GROUP_ID: &str = "field-group"; + const EVENT_ID: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + const AUTHOR: &str = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; + const EVENT_SIG: &str = concat!( + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" + ); const SHA256: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; #[test] @@ -157,28 +163,28 @@ mod tests { ])); let data = data_from_event( - "event-id".to_string(), - "author-pubkey".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 42, parts.kind, parts.content.clone(), parts.tags.clone(), ) .expect("parsed data"); - assert_eq!(data.id, "event-id"); - assert_eq!(data.author, "author-pubkey"); + assert_eq!(data.id, EVENT_ID); + assert_eq!(data.author, AUTHOR); assert_eq!(data.published_at, 42); assert_eq!(data.kind, KIND_FARM_FILE_METADATA); assert_eq!(data.data, metadata); let err = parsed_from_event( - "event-id".to_string(), - "author-pubkey".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 42, KIND_POST, parts.content.clone(), parts.tags.clone(), - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap_err(); assert!(matches!( @@ -190,16 +196,16 @@ mod tests { )); let parsed = parsed_from_event( - "event-id".to_string(), - "author-pubkey".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 42, parts.kind, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .expect("parsed event"); - assert_eq!(parsed.event.sig_str(), "sig"); + assert_eq!(parsed.event.sig_str(), EVENT_SIG); assert_eq!(parsed.data.data, metadata); } diff --git a/crates/event_codec/src/farm_workspace/encode.rs b/crates/event_codec/src/farm_workspace/encode.rs @@ -14,7 +14,7 @@ use crate::d_tag::validate_d_tag; use crate::error::EventEncodeError; use crate::field_helpers::{address_string, push_tag, validate_non_empty_field}; #[cfg(feature = "serde_json")] -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; pub fn farm_workspace_build_tags( manifest: &RadrootsFarmWorkspaceManifest, @@ -35,7 +35,7 @@ pub fn farm_workspace_build_tags( #[cfg(feature = "serde_json")] pub fn to_wire_parts( manifest: &RadrootsFarmWorkspaceManifest, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(manifest, KIND_FARM_WORKSPACE_MANIFEST) } @@ -43,13 +43,13 @@ pub fn to_wire_parts( pub fn to_wire_parts_with_kind( manifest: &RadrootsFarmWorkspaceManifest, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_FARM_WORKSPACE_MANIFEST { return Err(EventEncodeError::InvalidKind(kind)); } let tags = farm_workspace_build_tags(manifest)?; let content = serde_json::to_string(manifest).map_err(|_| EventEncodeError::Json)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content, tags, diff --git a/crates/event_codec/src/farm_workspace/mod.rs b/crates/event_codec/src/farm_workspace/mod.rs @@ -28,6 +28,12 @@ mod tests { const D_TAG: &str = "AAAAAAAAAAAAAAAAAAAAAA"; const FARM_D_TAG: &str = "AAAAAAAAAAAAAAAAAAAAAQ"; const GROUP_ID: &str = "field-group"; + const EVENT_ID: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + const AUTHOR: &str = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; + const EVENT_SIG: &str = concat!( + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" + ); #[test] fn farm_workspace_manifest_encodes_canonical_tags_and_decodes() { @@ -174,31 +180,31 @@ mod tests { ); let data = data_from_event( - "event-id".to_string(), - "author-pubkey".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 99, parts.kind, parts.content.clone(), parts.tags.clone(), ) .expect("parsed data"); - assert_eq!(data.id, "event-id"); - assert_eq!(data.author, "author-pubkey"); + assert_eq!(data.id, EVENT_ID); + assert_eq!(data.author, AUTHOR); assert_eq!(data.published_at, 99); assert_eq!(data.kind, KIND_FARM_WORKSPACE_MANIFEST); assert_same_manifest(&data.data, &manifest); let parsed = parsed_from_event( - "event-id".to_string(), - "author-pubkey".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 99, parts.kind, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .expect("parsed event"); - assert_eq!(parsed.event.sig_str(), "sig"); + assert_eq!(parsed.event.sig_str(), EVENT_SIG); assert_same_manifest(&parsed.data.data, &manifest); } diff --git a/crates/event_codec/src/file_metadata/encode.rs b/crates/event_codec/src/file_metadata/encode.rs @@ -18,7 +18,7 @@ use crate::field_helpers::{ push_optional_tag, push_tag, validate_lowercase_hex_64, validate_non_empty_field, }; use crate::social_helpers::{dimensions_tag, push_thumbnail, validate_http_url}; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const DEFAULT_KIND: u32 = KIND_PUBLIC_FILE_METADATA; @@ -64,18 +64,20 @@ pub fn file_metadata_build_tags( Ok(tags) } -pub fn to_wire_parts(metadata: &RadrootsFileMetadata) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + metadata: &RadrootsFileMetadata, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(metadata, DEFAULT_KIND) } pub fn to_wire_parts_with_kind( metadata: &RadrootsFileMetadata, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != DEFAULT_KIND { return Err(EventEncodeError::InvalidKind(kind)); } - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: metadata.content.clone().unwrap_or_default(), tags: file_metadata_build_tags(metadata)?, diff --git a/crates/event_codec/src/follow/encode.rs b/crates/event_codec/src/follow/encode.rs @@ -7,8 +7,8 @@ use alloc::{ use radroots_event::follow::{RadrootsFollow, RadrootsFollowProfile}; use crate::error::EventEncodeError; -use crate::wire::WireEventParts; use radroots_event::kinds::KIND_FOLLOW; +use radroots_event::wire::RadrootsNip01EventWireParts; const DEFAULT_KIND: u32 = KIND_FOLLOW; @@ -58,16 +58,18 @@ pub enum FollowMutation { }, } -pub fn to_wire_parts(follow: &RadrootsFollow) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + follow: &RadrootsFollow, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(follow, DEFAULT_KIND) } pub fn to_wire_parts_with_kind( follow: &RadrootsFollow, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { let tags = follow_build_tags(follow)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: String::new(), tags, @@ -122,7 +124,7 @@ pub fn follow_apply( pub fn follow_to_wire_parts_after( follow: &RadrootsFollow, mutation: FollowMutation, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { let updated = follow_apply(follow, mutation)?; to_wire_parts(&updated) } diff --git a/crates/event_codec/src/geochat/encode.rs b/crates/event_codec/src/geochat/encode.rs @@ -10,7 +10,7 @@ use radroots_event::geochat::RadrootsGeoChat; use radroots_event::kinds::KIND_GEOCHAT; use crate::error::EventEncodeError; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const DEFAULT_KIND: u32 = KIND_GEOCHAT; const TAG_G: &str = "g"; @@ -48,12 +48,14 @@ pub fn geochat_build_tags(geochat: &RadrootsGeoChat) -> Result<Vec<Vec<String>>, Ok(tags) } -pub fn to_wire_parts(geochat: &RadrootsGeoChat) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + geochat: &RadrootsGeoChat, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if geochat.content.trim().is_empty() { return Err(EventEncodeError::EmptyRequiredField("content")); } let tags = geochat_build_tags(geochat)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind: DEFAULT_KIND, content: geochat.content.clone(), tags, diff --git a/crates/event_codec/src/gift_wrap/encode.rs b/crates/event_codec/src/gift_wrap/encode.rs @@ -10,7 +10,7 @@ use radroots_event::gift_wrap::{RadrootsGiftWrap, RadrootsGiftWrapRecipient}; use radroots_event::kinds::KIND_GIFT_WRAP; use crate::error::EventEncodeError; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const DEFAULT_KIND: u32 = KIND_GIFT_WRAP; @@ -43,12 +43,14 @@ pub fn gift_wrap_build_tags( Ok(tags) } -pub fn to_wire_parts(gift_wrap: &RadrootsGiftWrap) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + gift_wrap: &RadrootsGiftWrap, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if gift_wrap.content.trim().is_empty() { return Err(EventEncodeError::EmptyRequiredField("content")); } let tags = gift_wrap_build_tags(gift_wrap)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind: DEFAULT_KIND, content: gift_wrap.content.clone(), tags, @@ -58,7 +60,7 @@ pub fn to_wire_parts(gift_wrap: &RadrootsGiftWrap) -> Result<WireEventParts, Eve pub fn to_wire_parts_with_kind( gift_wrap: &RadrootsGiftWrap, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != DEFAULT_KIND { return Err(EventEncodeError::InvalidKind(kind)); } diff --git a/crates/event_codec/src/group/encode.rs b/crates/event_codec/src/group/encode.rs @@ -24,7 +24,7 @@ use crate::error::EventEncodeError; use crate::field_helpers::{ push_optional_tag, push_tag, push_tag_values, validate_non_empty_field, }; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const TAG_ABOUT: &str = "about"; const TAG_CLOSED: &str = "closed"; @@ -151,7 +151,7 @@ pub fn group_roles_build_tags( pub fn group_put_user_to_wire_parts( event: &RadrootsGroupPutUser, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { message_wire( KIND_GROUP_PUT_USER, group_put_user_build_tags(event)?, @@ -161,7 +161,7 @@ pub fn group_put_user_to_wire_parts( pub fn group_remove_user_to_wire_parts( event: &RadrootsGroupRemoveUser, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { message_wire( KIND_GROUP_REMOVE_USER, group_remove_user_build_tags(event)?, @@ -171,7 +171,7 @@ pub fn group_remove_user_to_wire_parts( pub fn group_create_group_to_wire_parts( event: &RadrootsGroupCreateGroup, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { message_wire( KIND_GROUP_CREATE_GROUP, group_create_group_build_tags(event)?, @@ -181,7 +181,7 @@ pub fn group_create_group_to_wire_parts( pub fn group_edit_metadata_to_wire_parts( event: &RadrootsGroupEditMetadata, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { message_wire( KIND_GROUP_EDIT_METADATA, group_edit_metadata_build_tags(event)?, @@ -191,7 +191,7 @@ pub fn group_edit_metadata_to_wire_parts( pub fn group_delete_group_to_wire_parts( event: &RadrootsGroupDeleteGroup, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { message_wire( KIND_GROUP_DELETE_GROUP, group_delete_group_build_tags(event)?, @@ -201,7 +201,7 @@ pub fn group_delete_group_to_wire_parts( pub fn group_delete_event_to_wire_parts( event: &RadrootsGroupDeleteEvent, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { message_wire( KIND_GROUP_DELETE_EVENT, group_delete_event_build_tags(event)?, @@ -211,7 +211,7 @@ pub fn group_delete_event_to_wire_parts( pub fn group_create_invite_to_wire_parts( event: &RadrootsGroupCreateInvite, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { message_wire( KIND_GROUP_CREATE_INVITE, group_create_invite_build_tags(event)?, @@ -221,7 +221,7 @@ pub fn group_create_invite_to_wire_parts( pub fn group_join_request_to_wire_parts( event: &RadrootsGroupJoinRequest, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { message_wire( KIND_GROUP_JOIN_REQUEST, group_join_request_build_tags(event)?, @@ -231,7 +231,7 @@ pub fn group_join_request_to_wire_parts( pub fn group_leave_request_to_wire_parts( event: &RadrootsGroupLeaveRequest, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { message_wire( KIND_GROUP_LEAVE_REQUEST, group_leave_request_build_tags(event)?, @@ -241,13 +241,13 @@ pub fn group_leave_request_to_wire_parts( pub fn group_metadata_to_wire_parts( event: &RadrootsGroupMetadata, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { empty_wire(KIND_GROUP_METADATA, group_metadata_build_tags(event)?) } pub fn group_admins_to_wire_parts( event: &RadrootsGroupAdmins, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { message_wire( KIND_GROUP_ADMINS, group_admins_build_tags(event)?, @@ -257,7 +257,7 @@ pub fn group_admins_to_wire_parts( pub fn group_members_to_wire_parts( event: &RadrootsGroupMembers, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { message_wire( KIND_GROUP_MEMBERS, group_members_build_tags(event)?, @@ -267,7 +267,7 @@ pub fn group_members_to_wire_parts( pub fn group_roles_to_wire_parts( event: &RadrootsGroupRoles, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { message_wire( KIND_GROUP_ROLES, group_roles_build_tags(event)?, @@ -366,8 +366,11 @@ fn validate_optional(value: Option<&str>, field: &'static str) -> Result<(), Eve Ok(()) } -fn empty_wire(kind: u32, tags: Vec<Vec<String>>) -> Result<WireEventParts, EventEncodeError> { - Ok(WireEventParts { +fn empty_wire( + kind: u32, + tags: Vec<Vec<String>>, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { + Ok(RadrootsNip01EventWireParts { kind, content: String::new(), tags, @@ -378,9 +381,9 @@ fn message_wire( kind: u32, tags: Vec<Vec<String>>, message: Option<&str>, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { validate_optional(message, "message")?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: message.unwrap_or_default().to_string(), tags, diff --git a/crates/event_codec/src/http_auth/encode.rs b/crates/event_codec/src/http_auth/encode.rs @@ -10,7 +10,7 @@ use crate::error::EventEncodeError; use crate::field_helpers::{ push_optional_tag, push_tag, validate_lowercase_hex_64, validate_non_empty_field, }; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; pub fn http_auth_build_tags(auth: &RadrootsHttpAuth) -> Result<Vec<Vec<String>>, EventEncodeError> { validate_non_empty_field(&auth.url, "url")?; @@ -25,19 +25,21 @@ pub fn http_auth_build_tags(auth: &RadrootsHttpAuth) -> Result<Vec<Vec<String>>, Ok(tags) } -pub fn to_wire_parts(auth: &RadrootsHttpAuth) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + auth: &RadrootsHttpAuth, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(auth, KIND_HTTP_AUTH) } pub fn to_wire_parts_with_kind( auth: &RadrootsHttpAuth, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_HTTP_AUTH { return Err(EventEncodeError::InvalidKind(kind)); } let tags = http_auth_build_tags(auth)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: String::new(), tags, diff --git a/crates/event_codec/src/http_auth/mod.rs b/crates/event_codec/src/http_auth/mod.rs @@ -9,6 +9,12 @@ mod tests { use crate::http_auth::decode::{data_from_event, http_auth_from_event, parsed_from_event}; use crate::http_auth::encode::{http_auth_build_tags, to_wire_parts, to_wire_parts_with_kind}; + const EVENT_ID: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + const AUTHOR: &str = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; + const EVENT_SIG: &str = concat!( + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" + ); const PAYLOAD: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; #[test] @@ -172,30 +178,30 @@ mod tests { let parts = to_wire_parts(&auth).expect("http auth wire parts"); let data = data_from_event( - "event-id".to_string(), - "author-pubkey".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 99, parts.kind, parts.content.clone(), parts.tags.clone(), ) .expect("parsed data"); - assert_eq!(data.id, "event-id"); - assert_eq!(data.author, "author-pubkey"); + assert_eq!(data.id, EVENT_ID); + assert_eq!(data.author, AUTHOR); assert_eq!(data.published_at, 99); assert_eq!(data.data, auth); let parsed = parsed_from_event( - "event-id".to_string(), - "author-pubkey".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 99, parts.kind, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .expect("parsed event"); - assert_eq!(parsed.event.sig_str(), "sig"); + assert_eq!(parsed.event.sig_str(), EVENT_SIG); assert_eq!(parsed.data.data, auth); } diff --git a/crates/event_codec/src/job/encode.rs b/crates/event_codec/src/job/encode.rs @@ -3,8 +3,7 @@ use core::fmt; #[cfg(not(feature = "std"))] use alloc::{string::String, vec, vec::Vec}; -pub use crate::wire::{WireEventParts, canonicalize_tags, empty_content, to_frozen_draft}; -pub use radroots_event::draft::RadrootsEventDraft; +pub use crate::wire::{canonicalize_tags, empty_content}; #[derive(Debug)] pub enum JobEncodeError { diff --git a/crates/event_codec/src/job/feedback/encode.rs b/crates/event_codec/src/job/feedback/encode.rs @@ -1,7 +1,8 @@ use radroots_event::{job_feedback::RadrootsJobFeedback, kinds::KIND_JOB_FEEDBACK}; -use crate::job::encode::{JobEncodeError, WireEventParts, canonicalize_tags}; +use crate::job::encode::{JobEncodeError, canonicalize_tags}; use crate::job::util::{feedback_status_tag, push_amount_tag_msat}; +use radroots_event::wire::RadrootsNip01EventWireParts; #[cfg(not(feature = "std"))] use alloc::{ @@ -51,7 +52,7 @@ pub fn job_feedback_build_tags(fb: &RadrootsJobFeedback) -> Vec<Vec<String>> { pub fn to_wire_parts( fb: &RadrootsJobFeedback, content: &str, -) -> Result<WireEventParts, JobEncodeError> { +) -> Result<RadrootsNip01EventWireParts, JobEncodeError> { let kind = fb.kind as u32; if kind != KIND_JOB_FEEDBACK { return Err(JobEncodeError::InvalidKind(kind)); @@ -60,7 +61,7 @@ pub fn to_wire_parts( let mut tags = job_feedback_build_tags(fb); canonicalize_tags(&mut tags); - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: content.to_string(), tags, diff --git a/crates/event_codec/src/job/request/encode.rs b/crates/event_codec/src/job/request/encode.rs @@ -1,7 +1,8 @@ use radroots_event::{job_request::RadrootsJobRequest, kinds::is_request_kind}; -use crate::job::encode::{JobEncodeError, WireEventParts, canonicalize_tags}; +use crate::job::encode::{JobEncodeError, canonicalize_tags}; use crate::job::util::{job_input_type_tag, push_bid_tag_sat}; +use radroots_event::wire::RadrootsNip01EventWireParts; #[cfg(not(feature = "std"))] use alloc::{ @@ -70,7 +71,7 @@ pub fn job_request_build_tags(req: &RadrootsJobRequest) -> Vec<Vec<String>> { pub fn to_wire_parts( req: &RadrootsJobRequest, content: &str, -) -> Result<WireEventParts, JobEncodeError> { +) -> Result<RadrootsNip01EventWireParts, JobEncodeError> { let kind = req.kind as u32; if !is_request_kind(kind) { return Err(JobEncodeError::InvalidKind(kind)); @@ -82,7 +83,7 @@ pub fn to_wire_parts( let mut tags = job_request_build_tags(req); canonicalize_tags(&mut tags); - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: content.to_string(), tags, diff --git a/crates/event_codec/src/job/result/encode.rs b/crates/event_codec/src/job/result/encode.rs @@ -1,7 +1,8 @@ use radroots_event::{job_result::RadrootsJobResult, kinds::is_result_kind}; -use crate::job::encode::{JobEncodeError, WireEventParts, canonicalize_tags}; +use crate::job::encode::{JobEncodeError, canonicalize_tags}; use crate::job::util::{job_input_type_tag, push_amount_tag_msat}; +use radroots_event::wire::RadrootsNip01EventWireParts; #[cfg(not(feature = "std"))] use alloc::{ @@ -64,7 +65,7 @@ pub fn job_result_build_tags(res: &RadrootsJobResult) -> Vec<Vec<String>> { pub fn to_wire_parts( res: &RadrootsJobResult, content: &str, -) -> Result<WireEventParts, JobEncodeError> { +) -> Result<RadrootsNip01EventWireParts, JobEncodeError> { let kind = res.kind as u32; if !is_result_kind(kind) { return Err(JobEncodeError::InvalidKind(kind)); @@ -77,7 +78,7 @@ pub fn to_wire_parts( canonicalize_tags(&mut tags); - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: content.to_string(), tags, diff --git a/crates/event_codec/src/knowledge/encode.rs b/crates/event_codec/src/knowledge/encode.rs @@ -33,7 +33,8 @@ use serde::Serialize; use crate::error::EventEncodeError; use crate::event_ref::build_event_ref_tag; -use crate::wire::{WireEventParts, empty_content}; +use crate::wire::empty_content; +use radroots_event::wire::RadrootsNip01EventWireParts; const TAG_TITLE: &str = "title"; const TAG_SOURCE: &str = "source"; @@ -175,8 +176,8 @@ pub fn wiki_article_build_tags( pub fn wiki_article_to_wire_parts( article: &RadrootsWikiArticle, -) -> Result<WireEventParts, EventEncodeError> { - Ok(WireEventParts { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { + Ok(RadrootsNip01EventWireParts { kind: KIND_WIKI_ARTICLE, content: article.content_djot.clone(), tags: wiki_article_build_tags(article)?, @@ -196,8 +197,8 @@ pub fn wiki_redirect_build_tags( pub fn wiki_redirect_to_wire_parts( redirect: &RadrootsWikiRedirect, -) -> Result<WireEventParts, EventEncodeError> { - Ok(WireEventParts { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { + Ok(RadrootsNip01EventWireParts { kind: KIND_WIKI_REDIRECT, content: empty_content(), tags: wiki_redirect_build_tags(redirect)?, @@ -229,8 +230,8 @@ pub fn wiki_merge_request_build_tags( pub fn wiki_merge_request_to_wire_parts( request: &RadrootsWikiMergeRequest, -) -> Result<WireEventParts, EventEncodeError> { - Ok(WireEventParts { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { + Ok(RadrootsNip01EventWireParts { kind: KIND_WIKI_MERGE_REQUEST, content: request.explanation.clone().unwrap_or_default(), tags: wiki_merge_request_build_tags(request)?, @@ -250,8 +251,8 @@ pub fn knowledge_source_build_tags( pub fn knowledge_source_to_wire_parts( source: &RadrootsKnowledgeSource, -) -> Result<WireEventParts, EventEncodeError> { - Ok(WireEventParts { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { + Ok(RadrootsNip01EventWireParts { kind: KIND_KNOWLEDGE_SOURCE, content: json_content(source)?, tags: knowledge_source_build_tags(source)?, @@ -271,8 +272,8 @@ pub fn evidence_bounty_build_tags( pub fn evidence_bounty_to_wire_parts( bounty: &RadrootsEvidenceBounty, -) -> Result<WireEventParts, EventEncodeError> { - Ok(WireEventParts { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { + Ok(RadrootsNip01EventWireParts { kind: KIND_EVIDENCE_BOUNTY, content: json_content(bounty)?, tags: evidence_bounty_build_tags(bounty)?, @@ -296,8 +297,8 @@ pub fn knowledge_claim_build_tags( pub fn knowledge_claim_to_wire_parts( claim: &RadrootsKnowledgeClaim, -) -> Result<WireEventParts, EventEncodeError> { - Ok(WireEventParts { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { + Ok(RadrootsNip01EventWireParts { kind: KIND_KNOWLEDGE_CLAIM, content: json_content(claim)?, tags: knowledge_claim_build_tags(claim)?, @@ -315,8 +316,8 @@ pub fn knowledge_relation_build_tags( pub fn knowledge_relation_to_wire_parts( relation: &RadrootsKnowledgeRelation, -) -> Result<WireEventParts, EventEncodeError> { - Ok(WireEventParts { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { + Ok(RadrootsNip01EventWireParts { kind: KIND_KNOWLEDGE_RELATION, content: json_content(relation)?, tags: knowledge_relation_build_tags(relation)?, @@ -338,8 +339,8 @@ pub fn knowledge_review_build_tags( pub fn knowledge_review_to_wire_parts( review: &RadrootsKnowledgeReview, -) -> Result<WireEventParts, EventEncodeError> { - Ok(WireEventParts { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { + Ok(RadrootsNip01EventWireParts { kind: KIND_KNOWLEDGE_REVIEW, content: json_content(review)?, tags: knowledge_review_build_tags(review)?, @@ -364,8 +365,8 @@ pub fn knowledge_field_report_build_tags( pub fn knowledge_field_report_to_wire_parts( report: &RadrootsKnowledgeFieldReport, -) -> Result<WireEventParts, EventEncodeError> { - Ok(WireEventParts { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { + Ok(RadrootsNip01EventWireParts { kind: KIND_KNOWLEDGE_FIELD_REPORT, content: json_content(report)?, tags: knowledge_field_report_build_tags(report)?, @@ -383,8 +384,8 @@ pub fn knowledge_change_proposal_build_tags( pub fn knowledge_change_proposal_to_wire_parts( proposal: &RadrootsKnowledgeChangeProposal, -) -> Result<WireEventParts, EventEncodeError> { - Ok(WireEventParts { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { + Ok(RadrootsNip01EventWireParts { kind: KIND_KNOWLEDGE_CHANGE_PROPOSAL, content: json_content(proposal)?, tags: knowledge_change_proposal_build_tags(proposal)?, @@ -402,8 +403,8 @@ pub fn contribution_attestation_build_tags( pub fn contribution_attestation_to_wire_parts( attestation: &RadrootsContributionAttestation, -) -> Result<WireEventParts, EventEncodeError> { - Ok(WireEventParts { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { + Ok(RadrootsNip01EventWireParts { kind: KIND_CONTRIBUTION_ATTESTATION, content: json_content(attestation)?, tags: contribution_attestation_build_tags(attestation)?, diff --git a/crates/event_codec/src/list/encode.rs b/crates/event_codec/src/list/encode.rs @@ -8,7 +8,7 @@ use radroots_event::{ }; use crate::error::EventEncodeError; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; fn entry_tag(entry: &RadrootsListEntry) -> Result<Vec<String>, EventEncodeError> { if entry.tag.trim().is_empty() { @@ -44,7 +44,7 @@ pub fn list_build_tags(list: &RadrootsList) -> Result<Vec<Vec<String>>, EventEnc pub fn to_wire_parts_with_kind( list: &RadrootsList, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if !is_supported_list_kind(kind) { return Err(EventEncodeError::InvalidKind(kind)); } @@ -52,7 +52,7 @@ pub fn to_wire_parts_with_kind( validate_relay_entries(&list.entries)?; } let tags = list_build_tags(list)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: list.content.clone(), tags, diff --git a/crates/event_codec/src/list_set/encode.rs b/crates/event_codec/src/list_set/encode.rs @@ -10,7 +10,7 @@ use radroots_event::{kinds::is_nip51_list_set_kind, list_set::RadrootsListSet}; use crate::error::EventEncodeError; #[cfg(feature = "serde_json")] use crate::list::encode::list_entries_to_tags; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const TAG_D: &str = "d"; const TAG_TITLE: &str = "title"; @@ -61,12 +61,12 @@ pub fn list_set_build_tags(list: &RadrootsListSet) -> Result<Vec<Vec<String>>, E pub fn to_wire_parts_with_kind( list: &RadrootsListSet, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if !is_nip51_list_set_kind(kind) { return Err(EventEncodeError::InvalidKind(kind)); } let tags = list_set_build_tags(list)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: list.content.clone(), tags, diff --git a/crates/event_codec/src/listing/encode.rs b/crates/event_codec/src/listing/encode.rs @@ -14,7 +14,7 @@ use crate::listing::tags::listing_tags; #[cfg(feature = "serde_json")] use crate::listing::tags::listing_tags_full; #[cfg(feature = "serde_json")] -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; #[cfg(feature = "serde_json")] const DEFAULT_KIND: u32 = KIND_LISTING; @@ -24,7 +24,9 @@ pub fn listing_build_tags(listing: &RadrootsListing) -> Result<Vec<Vec<String>>, } #[cfg(feature = "serde_json")] -pub fn to_wire_parts(listing: &RadrootsListing) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + listing: &RadrootsListing, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(listing, DEFAULT_KIND) } @@ -32,13 +34,13 @@ pub fn to_wire_parts(listing: &RadrootsListing) -> Result<WireEventParts, EventE pub fn to_wire_parts_with_kind( listing: &RadrootsListing, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if !is_listing_kind(kind) { return Err(EventEncodeError::InvalidKind(kind)); } let tags = listing_tags_full(listing)?; let content = listing_markdown_content(listing); - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content, tags, @@ -49,13 +51,13 @@ pub fn to_wire_parts_with_kind( pub fn to_json_wire_parts_with_kind( listing: &RadrootsListing, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if !is_listing_kind(kind) { return Err(EventEncodeError::InvalidKind(kind)); } let tags = listing_tags_full(listing)?; let content = serde_json::to_string(listing).map_err(|_| EventEncodeError::Json)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content, tags, diff --git a/crates/event_codec/src/message/encode.rs b/crates/event_codec/src/message/encode.rs @@ -6,7 +6,7 @@ use radroots_event::message::RadrootsMessage; use crate::error::EventEncodeError; use crate::message::tags::{build_recipient_tags, build_reply_tag, build_subject_tag}; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const DEFAULT_KIND: u32 = KIND_MESSAGE; @@ -21,12 +21,14 @@ pub fn message_build_tags(message: &RadrootsMessage) -> Result<Vec<Vec<String>>, Ok(tags) } -pub fn to_wire_parts(message: &RadrootsMessage) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + message: &RadrootsMessage, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if message.content.trim().is_empty() { return Err(EventEncodeError::EmptyRequiredField("content")); } let tags = message_build_tags(message)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind: DEFAULT_KIND, content: message.content.clone(), tags, diff --git a/crates/event_codec/src/message_file/encode.rs b/crates/event_codec/src/message_file/encode.rs @@ -12,7 +12,7 @@ use radroots_event::message_file::{RadrootsMessageFile, RadrootsMessageFileDimen use crate::error::EventEncodeError; use crate::message::tags::{build_recipient_tags, build_reply_tag, build_subject_tag}; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const DEFAULT_KIND: u32 = KIND_MESSAGE_FILE; @@ -99,10 +99,12 @@ pub fn message_file_build_tags( Ok(tags) } -pub fn to_wire_parts(message: &RadrootsMessageFile) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + message: &RadrootsMessageFile, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { validate_required(&message.file_url, "file_url")?; let tags = message_file_build_tags(message)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind: DEFAULT_KIND, content: message.file_url.clone(), tags, @@ -112,7 +114,7 @@ pub fn to_wire_parts(message: &RadrootsMessageFile) -> Result<WireEventParts, Ev pub fn to_wire_parts_with_kind( message: &RadrootsMessageFile, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != DEFAULT_KIND { return Err(EventEncodeError::InvalidKind(kind)); } diff --git a/crates/event_codec/src/order/encode.rs b/crates/event_codec/src/order/encode.rs @@ -10,7 +10,9 @@ use radroots_event::{ }; #[cfg(feature = "serde_json")] -use crate::{error::EventEncodeError, order::tags::order_envelope_tags, wire::WireEventParts}; +use crate::{error::EventEncodeError, order::tags::order_envelope_tags}; +#[cfg(feature = "serde_json")] +use radroots_event::wire::RadrootsNip01EventWireParts; #[cfg(feature = "serde_json")] fn map_order_envelope_error(error: RadrootsOrderEnvelopeError) -> EventEncodeError { @@ -86,7 +88,7 @@ struct OrderEnvelopeEventBuildParts<'a, T> { #[cfg(feature = "serde_json")] fn order_envelope_event_build<T: serde::Serialize>( parts: OrderEnvelopeEventBuildParts<'_, T>, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if parts.message_type.requires_listing_snapshot() && parts.listing_event.is_none() { return Err(EventEncodeError::EmptyRequiredField("listing_event.id")); } @@ -115,7 +117,7 @@ fn order_envelope_event_build<T: serde::Serialize>( parts.root_event_id.map(RadrootsEventId::as_str), parts.prev_event_id.map(RadrootsEventId::as_str), )?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind: parts.message_type.kind(), content, tags, @@ -126,7 +128,7 @@ fn order_envelope_event_build<T: serde::Serialize>( pub fn order_request_event_build( listing_event: &RadrootsEventPtr, payload: &RadrootsOrderRequest, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { payload.validate().map_err(map_order_payload_error)?; order_envelope_event_build(OrderEnvelopeEventBuildParts { recipient_pubkey: &payload.seller_pubkey, @@ -145,7 +147,7 @@ pub fn order_decision_event_build( root_event_id: &RadrootsEventId, prev_event_id: &RadrootsEventId, payload: &RadrootsOrderDecision, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { payload.validate().map_err(map_order_payload_error)?; order_envelope_event_build(OrderEnvelopeEventBuildParts { recipient_pubkey: &payload.buyer_pubkey, @@ -164,7 +166,7 @@ pub fn order_revision_proposal_event_build( root_event_id: &RadrootsEventId, prev_event_id: &RadrootsEventId, payload: &RadrootsOrderRevisionProposal, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { payload.validate().map_err(map_order_payload_error)?; if payload.root_event_id.as_str() != root_event_id.as_str() { return Err(EventEncodeError::InvalidField("root_event_id")); @@ -189,7 +191,7 @@ pub fn order_revision_decision_event_build( root_event_id: &RadrootsEventId, prev_event_id: &RadrootsEventId, payload: &RadrootsOrderRevisionDecision, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { payload.validate().map_err(map_order_payload_error)?; if payload.root_event_id.as_str() != root_event_id.as_str() { return Err(EventEncodeError::InvalidField("root_event_id")); @@ -214,7 +216,7 @@ pub fn order_cancellation_event_build( root_event_id: &RadrootsEventId, prev_event_id: &RadrootsEventId, payload: &RadrootsOrderCancellation, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { payload.validate().map_err(map_order_payload_error)?; order_envelope_event_build(OrderEnvelopeEventBuildParts { recipient_pubkey: &payload.seller_pubkey, diff --git a/crates/event_codec/src/plot/encode.rs b/crates/event_codec/src/plot/encode.rs @@ -16,7 +16,7 @@ use crate::d_tag::validate_d_tag; use crate::error::EventEncodeError; #[cfg(feature = "serde_json")] -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const TAG_T: &str = "t"; const TAG_G: &str = "g"; @@ -91,7 +91,7 @@ pub fn plot_build_tags(plot: &RadrootsPlot) -> Result<Vec<Vec<String>>, EventEnc } #[cfg(feature = "serde_json")] -pub fn to_wire_parts(plot: &RadrootsPlot) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts(plot: &RadrootsPlot) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(plot, KIND_PLOT) } @@ -99,13 +99,13 @@ pub fn to_wire_parts(plot: &RadrootsPlot) -> Result<WireEventParts, EventEncodeE pub fn to_wire_parts_with_kind( plot: &RadrootsPlot, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_PLOT { return Err(EventEncodeError::InvalidKind(kind)); } let tags = plot_build_tags(plot)?; let content = serde_json::to_string(plot).map_err(|_| EventEncodeError::Json)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content, tags, diff --git a/crates/event_codec/src/post/encode.rs b/crates/event_codec/src/post/encode.rs @@ -16,7 +16,7 @@ use radroots_event::{ use crate::error::EventEncodeError; use crate::field_helpers::{parse_address_tag, validate_lowercase_hex_64}; use crate::social_helpers::{dimensions_tag, push_location_tags}; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const DEFAULT_KIND: u32 = KIND_POST; @@ -53,14 +53,14 @@ pub fn post_build_tags(post: &RadrootsPost) -> Result<Vec<Vec<String>>, EventEnc Ok(tags) } -pub fn to_wire_parts(post: &RadrootsPost) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts(post: &RadrootsPost) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(post, DEFAULT_KIND) } pub fn to_wire_parts_with_kind( post: &RadrootsPost, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != DEFAULT_KIND { return Err(EventEncodeError::InvalidKind(kind)); } @@ -68,7 +68,7 @@ pub fn to_wire_parts_with_kind( return Err(EventEncodeError::EmptyRequiredField("content")); } let tags = post_build_tags(post)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: post.content.clone(), tags, diff --git a/crates/event_codec/src/profile/encode.rs b/crates/event_codec/src/profile/encode.rs @@ -13,7 +13,7 @@ use nostr::prelude::Url; use alloc::{string::String, vec::Vec}; #[cfg(feature = "serde_json")] -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; fn push_tag(tags: &mut Vec<Vec<String>>, key: &str, value: &str) { tags.push(vec![key.to_string(), value.to_string()]); @@ -71,7 +71,9 @@ pub fn to_metadata(p: &RadrootsProfile) -> Result<Metadata, ProfileEncodeError> } #[cfg(feature = "serde_json")] -pub fn to_wire_parts(p: &RadrootsProfile) -> Result<WireEventParts, ProfileEncodeError> { +pub fn to_wire_parts( + p: &RadrootsProfile, +) -> Result<RadrootsNip01EventWireParts, ProfileEncodeError> { to_wire_parts_with_profile_type(p, None) } @@ -79,11 +81,11 @@ pub fn to_wire_parts(p: &RadrootsProfile) -> Result<WireEventParts, ProfileEncod pub fn to_wire_parts_with_profile_type( p: &RadrootsProfile, profile_type: Option<RadrootsProfileType>, -) -> Result<WireEventParts, ProfileEncodeError> { +) -> Result<RadrootsNip01EventWireParts, ProfileEncodeError> { let md = to_metadata(p)?; let content = serde_json::to_string(&md).map_err(|_| ProfileEncodeError::Json)?; let tags = profile_build_tags(profile_type); - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind: KIND_PROFILE, content, tags, diff --git a/crates/event_codec/src/reaction/encode.rs b/crates/event_codec/src/reaction/encode.rs @@ -14,7 +14,7 @@ use crate::error::EventEncodeError; use crate::field_helpers::{ parse_address_tag, validate_lowercase_hex_64, validate_non_empty_field, }; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const DEFAULT_KIND: u32 = KIND_REACTION; @@ -26,19 +26,21 @@ pub fn reaction_build_tags( Ok(tags) } -pub fn to_wire_parts(reaction: &RadrootsReaction) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + reaction: &RadrootsReaction, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(reaction, DEFAULT_KIND) } pub fn to_wire_parts_with_kind( reaction: &RadrootsReaction, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != DEFAULT_KIND { return Err(EventEncodeError::InvalidKind(kind)); } let tags = reaction_build_tags(reaction)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: reaction.content.clone(), tags, diff --git a/crates/event_codec/src/relay_auth/encode.rs b/crates/event_codec/src/relay_auth/encode.rs @@ -8,7 +8,7 @@ use radroots_event::{ use crate::error::EventEncodeError; use crate::field_helpers::{push_tag, validate_non_empty_field}; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; pub fn relay_auth_build_tags( auth: &RadrootsRelayAuth, @@ -21,19 +21,21 @@ pub fn relay_auth_build_tags( Ok(tags) } -pub fn to_wire_parts(auth: &RadrootsRelayAuth) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + auth: &RadrootsRelayAuth, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(auth, KIND_RELAY_AUTH) } pub fn to_wire_parts_with_kind( auth: &RadrootsRelayAuth, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_RELAY_AUTH { return Err(EventEncodeError::InvalidKind(kind)); } let tags = relay_auth_build_tags(auth)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: String::new(), tags, diff --git a/crates/event_codec/src/relay_auth/mod.rs b/crates/event_codec/src/relay_auth/mod.rs @@ -11,6 +11,13 @@ mod tests { relay_auth_build_tags, to_wire_parts, to_wire_parts_with_kind, }; + const EVENT_ID: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + const AUTHOR: &str = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; + const EVENT_SIG: &str = concat!( + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" + ); + #[test] fn relay_auth_encodes_and_decodes_nip42_event() { let auth = sample_auth(); @@ -112,30 +119,30 @@ mod tests { let parts = to_wire_parts(&auth).expect("relay auth wire parts"); let data = data_from_event( - "event-id".to_string(), - "author-pubkey".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 99, parts.kind, parts.content.clone(), parts.tags.clone(), ) .expect("parsed data"); - assert_eq!(data.id, "event-id"); - assert_eq!(data.author, "author-pubkey"); + assert_eq!(data.id, EVENT_ID); + assert_eq!(data.author, AUTHOR); assert_eq!(data.published_at, 99); assert_eq!(data.data, auth); let parsed = parsed_from_event( - "event-id".to_string(), - "author-pubkey".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 99, parts.kind, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .expect("parsed event"); - assert_eq!(parsed.event.sig_str(), "sig"); + assert_eq!(parsed.event.sig_str(), EVENT_SIG); assert_eq!(parsed.data.data, auth); } diff --git a/crates/event_codec/src/report/encode.rs b/crates/event_codec/src/report/encode.rs @@ -18,7 +18,7 @@ use crate::field_helpers::{ parse_address_tag, push_tag, validate_lowercase_hex_64, validate_non_empty_field, }; use crate::social_helpers::validate_http_url; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; pub fn report_build_tags(report: &RadrootsReport) -> Result<Vec<Vec<String>>, EventEncodeError> { validate_report(report)?; @@ -38,18 +38,20 @@ pub fn report_build_tags(report: &RadrootsReport) -> Result<Vec<Vec<String>>, Ev Ok(tags) } -pub fn to_wire_parts(report: &RadrootsReport) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + report: &RadrootsReport, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(report, KIND_REPORT) } pub fn to_wire_parts_with_kind( report: &RadrootsReport, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_REPORT { return Err(EventEncodeError::InvalidKind(kind)); } - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: report.content.clone().unwrap_or_default(), tags: report_build_tags(report)?, diff --git a/crates/event_codec/src/repost/encode.rs b/crates/event_codec/src/repost/encode.rs @@ -17,7 +17,7 @@ use crate::error::EventEncodeError; use crate::field_helpers::{ parse_address_tag, push_tag, validate_lowercase_hex_64, validate_non_empty_field, }; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; pub fn repost_build_tags(repost: &RadrootsRepost) -> Result<Vec<Vec<String>>, EventEncodeError> { let mut tags = Vec::new(); @@ -35,24 +35,26 @@ pub fn generic_repost_build_tags( Ok(tags) } -pub fn repost_to_wire_parts(repost: &RadrootsRepost) -> Result<WireEventParts, EventEncodeError> { +pub fn repost_to_wire_parts( + repost: &RadrootsRepost, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { repost_to_wire_parts_with_kind(repost, KIND_REPOST) } pub fn generic_repost_to_wire_parts( repost: &RadrootsGenericRepost, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { generic_repost_to_wire_parts_with_kind(repost, KIND_GENERIC_REPOST) } pub fn repost_to_wire_parts_with_kind( repost: &RadrootsRepost, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_REPOST { return Err(EventEncodeError::InvalidKind(kind)); } - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: repost.content.clone().unwrap_or_default(), tags: repost_build_tags(repost)?, @@ -62,11 +64,11 @@ pub fn repost_to_wire_parts_with_kind( pub fn generic_repost_to_wire_parts_with_kind( repost: &RadrootsGenericRepost, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_GENERIC_REPOST { return Err(EventEncodeError::InvalidKind(kind)); } - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content: repost.content.clone().unwrap_or_default(), tags: generic_repost_build_tags(repost)?, diff --git a/crates/event_codec/src/resource_area/encode.rs b/crates/event_codec/src/resource_area/encode.rs @@ -15,7 +15,7 @@ use crate::d_tag::validate_d_tag; use crate::error::EventEncodeError; #[cfg(feature = "serde_json")] -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const TAG_T: &str = "t"; const TAG_G: &str = "g"; @@ -79,7 +79,9 @@ pub fn resource_area_ref_tags( } #[cfg(feature = "serde_json")] -pub fn to_wire_parts(area: &RadrootsResourceArea) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + area: &RadrootsResourceArea, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(area, KIND_RESOURCE_AREA) } @@ -87,13 +89,13 @@ pub fn to_wire_parts(area: &RadrootsResourceArea) -> Result<WireEventParts, Even pub fn to_wire_parts_with_kind( area: &RadrootsResourceArea, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_RESOURCE_AREA { return Err(EventEncodeError::InvalidKind(kind)); } let tags = resource_area_build_tags(area)?; let content = serde_json::to_string(area).map_err(|_| EventEncodeError::Json)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content, tags, diff --git a/crates/event_codec/src/resource_cap/encode.rs b/crates/event_codec/src/resource_cap/encode.rs @@ -13,9 +13,9 @@ use crate::d_tag::validate_d_tag; use crate::error::EventEncodeError; #[cfg(feature = "serde_json")] -use crate::wire::WireEventParts; -#[cfg(feature = "serde_json")] use radroots_event::kinds::KIND_RESOURCE_HARVEST_CAP; +#[cfg(feature = "serde_json")] +use radroots_event::wire::RadrootsNip01EventWireParts; const TAG_A: &str = "a"; const TAG_P: &str = "p"; @@ -79,7 +79,9 @@ pub fn resource_harvest_cap_build_tags( } #[cfg(feature = "serde_json")] -pub fn to_wire_parts(cap: &RadrootsResourceHarvestCap) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts( + cap: &RadrootsResourceHarvestCap, +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { to_wire_parts_with_kind(cap, KIND_RESOURCE_HARVEST_CAP) } @@ -87,13 +89,13 @@ pub fn to_wire_parts(cap: &RadrootsResourceHarvestCap) -> Result<WireEventParts, pub fn to_wire_parts_with_kind( cap: &RadrootsResourceHarvestCap, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != KIND_RESOURCE_HARVEST_CAP { return Err(EventEncodeError::InvalidKind(kind)); } let tags = resource_harvest_cap_build_tags(cap)?; let content = serde_json::to_string(cap).map_err(|_| EventEncodeError::Json)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind, content, tags, diff --git a/crates/event_codec/src/seal/encode.rs b/crates/event_codec/src/seal/encode.rs @@ -5,7 +5,7 @@ use radroots_event::kinds::KIND_SEAL; use radroots_event::seal::RadrootsSeal; use crate::error::EventEncodeError; -use crate::wire::WireEventParts; +use radroots_event::wire::RadrootsNip01EventWireParts; const DEFAULT_KIND: u32 = KIND_SEAL; @@ -16,9 +16,9 @@ pub fn seal_build_tags(_seal: &RadrootsSeal) -> Result<Vec<Vec<String>>, EventEn Ok(Vec::new()) } -pub fn to_wire_parts(seal: &RadrootsSeal) -> Result<WireEventParts, EventEncodeError> { +pub fn to_wire_parts(seal: &RadrootsSeal) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { let tags = seal_build_tags(seal)?; - Ok(WireEventParts { + Ok(RadrootsNip01EventWireParts { kind: DEFAULT_KIND, content: seal.content.clone(), tags, @@ -28,7 +28,7 @@ pub fn to_wire_parts(seal: &RadrootsSeal) -> Result<WireEventParts, EventEncodeE pub fn to_wire_parts_with_kind( seal: &RadrootsSeal, kind: u32, -) -> Result<WireEventParts, EventEncodeError> { +) -> Result<RadrootsNip01EventWireParts, EventEncodeError> { if kind != DEFAULT_KIND { return Err(EventEncodeError::InvalidKind(kind)); } diff --git a/crates/event_codec/src/wire.rs b/crates/event_codec/src/wire.rs @@ -4,31 +4,6 @@ use alloc::{ vec::Vec, }; -use radroots_event::draft::{RadrootsDraftError, RadrootsEventDraft}; - -#[derive(Debug, Clone)] -pub struct WireEventParts { - pub kind: u32, - pub content: String, - pub tags: Vec<Vec<String>>, -} - -pub fn to_frozen_draft( - parts: WireEventParts, - contract_id: impl Into<String>, - expected_pubkey: impl AsRef<str>, - created_at: u32, -) -> Result<RadrootsEventDraft, RadrootsDraftError> { - RadrootsEventDraft::new( - contract_id, - parts.kind, - created_at, - parts.tags, - parts.content, - expected_pubkey, - ) -} - pub fn canonicalize_tags(tags: &mut Vec<Vec<String>>) { tags.retain(|t| t.first().map(|s| !s.trim().is_empty()).unwrap_or(false)); for t in tags.iter_mut() { diff --git a/crates/event_codec/tests/app_data.rs b/crates/event_codec/tests/app_data.rs @@ -1,3 +1,6 @@ +mod common; + +use common::{AUTHOR, EVENT_ID, EVENT_SIG}; use radroots_event::{ app_data::{KIND_APP_DATA, RadrootsAppData}, kinds::KIND_POST, @@ -119,21 +122,21 @@ fn app_data_metadata_and_index_from_event_roundtrip() { assert_eq!(metadata.data.content, "payload"); let index = parsed_from_event( - "id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 42, KIND_APP_DATA, "payload".to_string(), tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); - assert_eq!(index.event.id_str(), "id"); - assert_eq!(index.event.author_str(), "author"); + assert_eq!(index.event.id_str(), EVENT_ID); + assert_eq!(index.event.author_str(), AUTHOR); assert_eq!(index.event.created_at_u64(), 42); assert_eq!(index.event.kind_u32(), KIND_APP_DATA); assert_eq!(index.event.content(), "payload"); - assert_eq!(index.event.sig_str(), "sig"); + assert_eq!(index.event.sig_str(), EVENT_SIG); assert_eq!(index.data.data.d_tag, "radroots.app"); } diff --git a/crates/event_codec/tests/article.rs b/crates/event_codec/tests/article.rs @@ -1,5 +1,8 @@ #![cfg(feature = "serde_json")] +mod common; + +use common::{AUTHOR, EVENT_ID, EVENT_SIG}; use radroots_event::{ article::RadrootsArticle, farm::RadrootsFarmRef, @@ -204,16 +207,18 @@ fn article_wrappers_preserve_event_metadata() { assert_eq!(data.data.title, "Spring soil notes"); let parsed = parsed_from_event( - "event_id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 42, parts.kind, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); - assert_eq!(parsed.event.sig_str(), "sig"); + assert_eq!(parsed.event.id_str(), EVENT_ID); + assert_eq!(parsed.event.author_str(), AUTHOR); + assert_eq!(parsed.event.sig_str(), EVENT_SIG); assert_eq!(parsed.data.data.d_tag, VALID_D_TAG); } diff --git a/crates/event_codec/tests/calendar.rs b/crates/event_codec/tests/calendar.rs @@ -40,6 +40,10 @@ use radroots_event_codec::{ const VALID_D_TAG: &str = "CCCCCCCCCCCCCCCCCCCCCA"; const EVENT_ID: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; const EVENT_AUTHOR: &str = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; +const EVENT_SIG: &str = concat!( + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" +); const EVENT_D_TAG: &str = "EEEEEEEEEEEEEEEEEEEEEA"; fn sample_date_event() -> RadrootsCalendarDateEvent { @@ -735,16 +739,16 @@ fn calendar_wrappers_preserve_event_metadata() { )); let date_parsed = date_parsed_from_event( - "date_id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + EVENT_AUTHOR.to_string(), 7, date_parts.kind, date_parts.content, date_parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); - assert_eq!(date_parsed.event.sig_str(), "sig"); + assert_eq!(date_parsed.event.sig_str(), EVENT_SIG); let time = sample_time_event(); let time_parts = time_to_wire_parts(&time).unwrap(); @@ -779,13 +783,13 @@ fn calendar_wrappers_preserve_event_metadata() { )); let time_parsed = time_parsed_from_event( - "time_id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + EVENT_AUTHOR.to_string(), 8, time_parts.kind, time_parts.content, time_parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); assert_eq!(time_parsed.event.created_at_u64(), 8); @@ -823,16 +827,16 @@ fn calendar_wrappers_preserve_event_metadata() { )); let calendar_parsed = calendar_parsed_from_event( - "calendar_id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + EVENT_AUTHOR.to_string(), 9, calendar_parts.kind, calendar_parts.content, calendar_parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); - assert_eq!(calendar_parsed.event.sig_str(), "sig"); + assert_eq!(calendar_parsed.event.sig_str(), EVENT_SIG); let rsvp = sample_rsvp(); let rsvp_parts = rsvp_to_wire_parts(&rsvp).unwrap(); @@ -867,13 +871,13 @@ fn calendar_wrappers_preserve_event_metadata() { )); let rsvp_parsed = rsvp_parsed_from_event( - "rsvp_id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + EVENT_AUTHOR.to_string(), 10, rsvp_parts.kind, rsvp_parts.content, rsvp_parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); assert_eq!(rsvp_parsed.event.created_at_u64(), 10); diff --git a/crates/event_codec/tests/comment.rs b/crates/event_codec/tests/comment.rs @@ -1,3 +1,6 @@ +mod common; + +use common::{AUTHOR as EVENT_AUTHOR, EVENT_ID, EVENT_SIG}; use radroots_event::{ comment::RadrootsComment, kinds::{KIND_ARTICLE, KIND_COMMENT, KIND_POST}, @@ -533,16 +536,16 @@ fn comment_metadata_and_index_from_event_roundtrip() { )); let index = parsed_from_event( - "id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + EVENT_AUTHOR.to_string(), 77, KIND_COMMENT, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); assert_eq!(index.event.created_at_u64(), 77); - assert_eq!(index.event.sig_str(), "sig"); + assert_eq!(index.event.sig_str(), EVENT_SIG); assert_address_target(&index.data.data.parent, PARENT_AUTHOR, KIND_ARTICLE, D_TAG); } diff --git a/crates/event_codec/tests/file_metadata.rs b/crates/event_codec/tests/file_metadata.rs @@ -1,5 +1,8 @@ #![cfg(feature = "serde_json")] +mod common; + +use common::{AUTHOR, EVENT_ID, EVENT_SIG}; use radroots_event::{ farm_crdt::RadrootsFarmCrdtDocumentKind, farm_file::{RadrootsFarmFileDimensions, RadrootsFarmFileMetadata, RadrootsFarmFileSource}, @@ -370,17 +373,17 @@ fn file_metadata_wrappers_preserve_event_metadata() { assert_eq!(data.data.url, "https://media.example.test/field.jpg"); let parsed = parsed_from_event( - "file_id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 90, parts.kind, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); assert_eq!(parsed.event.created_at_u64(), 90); - assert_eq!(parsed.event.sig_str(), "sig"); + assert_eq!(parsed.event.sig_str(), EVENT_SIG); assert_eq!(parsed.data.data.sha256, VALID_HASH); } diff --git a/crates/event_codec/tests/follow.rs b/crates/event_codec/tests/follow.rs @@ -1,6 +1,8 @@ +mod common; #[path = "../src/test_fixtures.rs"] mod test_fixtures; +use common::{AUTHOR, EVENT_ID, EVENT_SIG}; use radroots_event::{ follow::{RadrootsFollow, RadrootsFollowProfile}, kinds::{KIND_FOLLOW, KIND_POST}, @@ -178,17 +180,17 @@ fn follow_metadata_and_index_from_event_roundtrip() { assert_eq!(metadata.data.list[0].contact_name.as_deref(), Some("alice")); let index = parsed_from_event( - "id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 50, KIND_FOLLOW, "".to_string(), tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); assert_eq!(index.event.kind_u32(), KIND_FOLLOW); - assert_eq!(index.event.sig_str(), "sig"); + assert_eq!(index.event.sig_str(), EVENT_SIG); assert_eq!(index.data.data.list.len(), 1); } diff --git a/crates/event_codec/tests/geochat.rs b/crates/event_codec/tests/geochat.rs @@ -1,3 +1,6 @@ +mod common; + +use common::{AUTHOR, EVENT_ID, EVENT_SIG}; use radroots_event::{ geochat::RadrootsGeoChat, kinds::{KIND_GEOCHAT, KIND_POST}, @@ -234,17 +237,17 @@ fn geochat_metadata_and_index_from_event_roundtrip() { assert!(metadata.data.teleported); let index = parsed_from_event( - "id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 77, KIND_GEOCHAT, "hello".to_string(), tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); assert_eq!(index.event.kind_u32(), KIND_GEOCHAT); - assert_eq!(index.event.sig_str(), "sig"); + assert_eq!(index.event.sig_str(), EVENT_SIG); assert_eq!(index.data.data.geohash, "dr5rsj7"); } diff --git a/crates/event_codec/tests/gift_wrap.rs b/crates/event_codec/tests/gift_wrap.rs @@ -1,6 +1,8 @@ +mod common; #[path = "../src/test_fixtures.rs"] mod test_fixtures; +use common::{AUTHOR, EVENT_ID, EVENT_SIG}; use radroots_event::gift_wrap::{RadrootsGiftWrap, RadrootsGiftWrapRecipient}; use radroots_event::kinds::{KIND_GIFT_WRAP, KIND_MESSAGE}; @@ -153,17 +155,17 @@ fn gift_wrap_metadata_and_index_from_event_roundtrip() { assert_eq!(metadata.data.expiration, gift_wrap.expiration); let index = parsed_from_event( - "id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 11, parts.kind, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); assert_eq!(index.event.kind_u32(), KIND_GIFT_WRAP); - assert_eq!(index.event.sig_str(), "sig"); + assert_eq!(index.event.sig_str(), EVENT_SIG); assert_eq!(index.data.data.recipient.public_key, "pubkey"); } diff --git a/crates/event_codec/tests/knowledge.rs b/crates/event_codec/tests/knowledge.rs @@ -18,6 +18,7 @@ use radroots_event::knowledge::{ RadrootsKnowledgeReviewTarget, RadrootsKnowledgeSource, RadrootsWikiArticle, RadrootsWikiArticleVersionRef, RadrootsWikiMergeRequest, RadrootsWikiRedirect, }; +use radroots_event::wire::RadrootsNip01EventWireParts; use radroots_event::{RadrootsEventEnvelope, RadrootsEventEnvelopeParts}; use radroots_event_codec::error::{EventEncodeError, EventParseError}; use radroots_event_codec::knowledge::{ @@ -35,7 +36,6 @@ use radroots_event_codec::verification::{ RadrootsDecodeError, RadrootsDecodedEvent, RadrootsNip01VerificationError, verify_and_decode_radroots_event, }; -use radroots_event_codec::wire::WireEventParts; fn hex_64(character: char) -> String { core::iter::repeat_n(character, 64).collect() @@ -73,7 +73,7 @@ fn article_version_ref_for(event_id_character: char, d_tag: &str) -> RadrootsWik } } -fn event_from_parts(parts: WireEventParts) -> RadrootsEventEnvelope { +fn event_from_parts(parts: RadrootsNip01EventWireParts) -> RadrootsEventEnvelope { RadrootsEventEnvelope::new(RadrootsEventEnvelopeParts { id: hex_64('0'), author: hex_64('a'), @@ -174,7 +174,7 @@ fn invalid_relay() -> String { "http://relay.radroots.example".to_string() } -fn sign_parts(parts: WireEventParts) -> RadrootsEventEnvelope { +fn sign_parts(parts: RadrootsNip01EventWireParts) -> RadrootsEventEnvelope { let tags = parts .tags .into_iter() @@ -711,7 +711,7 @@ fn malformed_knowledge_events_return_stable_decode_codes() { mutate_tags(&mut missing_contract, |tags| { tags.retain(|tag| tag.first().map(|value| value.as_str()) != Some("contract")); }); - let signed = sign_parts(WireEventParts { + let signed = sign_parts(RadrootsNip01EventWireParts { kind: missing_contract.kind_u32(), content: missing_contract.content().to_string(), tags: missing_contract.tags_as_vec(), diff --git a/crates/event_codec/tests/knowledge_fixtures.rs b/crates/event_codec/tests/knowledge_fixtures.rs @@ -13,6 +13,7 @@ use radroots_event::kinds::{ use radroots_event::knowledge::{ RADROOTS_KNOWLEDGE_CLAIM_SCHEMA, RADROOTS_KNOWLEDGE_FIELD_REPORT_SCHEMA, RadrootsWikiArticle, }; +use radroots_event::wire::RadrootsNip01EventWireParts; use radroots_event::{RadrootsEventEnvelope, RadrootsEventEnvelopeParts}; use radroots_event_codec::error::{EventEncodeError, EventParseError}; use radroots_event_codec::knowledge::{ @@ -27,7 +28,6 @@ use radroots_event_codec::verification::{ RadrootsDecodeError, RadrootsDecodedEvent, RadrootsNip01VerificationError, verify_and_decode_radroots_event, }; -use radroots_event_codec::wire::WireEventParts; use radroots_test_fixtures::RELAY_PRIMARY_WSS; use radroots_test_fixtures::knowledge::{ RADROOTS_KNOWLEDGE_ADVERSARIAL_FIXTURES, RADROOTS_KNOWLEDGE_VALID_CONTRACT_IDS, @@ -36,7 +36,7 @@ use radroots_test_fixtures::knowledge::{ wiki_redirect, }; -fn event_from_parts(parts: WireEventParts) -> RadrootsEventEnvelope { +fn event_from_parts(parts: RadrootsNip01EventWireParts) -> RadrootsEventEnvelope { RadrootsEventEnvelope::new(RadrootsEventEnvelopeParts { id: hex_64('0'), author: hex_64('a'), @@ -49,7 +49,7 @@ fn event_from_parts(parts: WireEventParts) -> RadrootsEventEnvelope { .unwrap() } -fn sign_parts(parts: WireEventParts) -> RadrootsEventEnvelope { +fn sign_parts(parts: RadrootsNip01EventWireParts) -> RadrootsEventEnvelope { let tags = parts .tags .into_iter() @@ -123,7 +123,7 @@ fn replace_sig(event: &mut RadrootsEventEnvelope, sig: String) { *event = event_with_parts(event, event.tags_as_vec(), event.content().to_string(), sig); } -fn parts_for_fixture(fixture: &RadrootsKnowledgeFixture) -> WireEventParts { +fn parts_for_fixture(fixture: &RadrootsKnowledgeFixture) -> RadrootsNip01EventWireParts { match fixture { RadrootsKnowledgeFixture::WikiArticle(value) => wiki_article_to_wire_parts(value).unwrap(), RadrootsKnowledgeFixture::WikiRedirect(value) => { @@ -278,7 +278,7 @@ fn adversarial_knowledge_fixtures_reject_at_expected_stages() { mutate_tags(&mut missing_contract_event, |tags| { tags.retain(|tag| tag.first().map(|value| value.as_str()) != Some("contract")); }); - let signed = sign_parts(WireEventParts { + let signed = sign_parts(RadrootsNip01EventWireParts { kind: missing_contract_event.kind_u32(), content: missing_contract_event.content().to_string(), tags: missing_contract_event.tags_as_vec(), @@ -300,7 +300,7 @@ fn adversarial_knowledge_fixtures_reject_at_expected_stages() { &mut private_event, serde_json::to_string(&private_value).unwrap(), ); - let signed = sign_parts(WireEventParts { + let signed = sign_parts(RadrootsNip01EventWireParts { kind: private_event.kind_u32(), content: private_event.content().to_string(), tags: private_event.tags_as_vec(), @@ -327,7 +327,7 @@ fn adversarial_knowledge_fixtures_reject_at_expected_stages() { } } }); - let signed = sign_parts(WireEventParts { + let signed = sign_parts(RadrootsNip01EventWireParts { kind: unsupported_event.kind_u32(), content: unsupported_event.content().to_string(), tags: unsupported_event.tags_as_vec(), diff --git a/crates/event_codec/tests/list.rs b/crates/event_codec/tests/list.rs @@ -1,3 +1,6 @@ +mod common; + +use common::{AUTHOR, EVENT_ID, EVENT_SIG}; use radroots_event::{ kinds::{KIND_LIST_MUTE, KIND_LIST_READ_WRITE_RELAYS, KIND_LIST_SET_FOLLOW, KIND_POST}, list::{RadrootsList, RadrootsListEntry}, @@ -150,17 +153,17 @@ fn list_metadata_and_index_from_event_roundtrip() { assert_eq!(metadata.data.entries[0].values, list.entries[0].values); let index = parsed_from_event( - "id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 44, KIND_LIST_MUTE, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); assert_eq!(index.event.kind_u32(), KIND_LIST_MUTE); - assert_eq!(index.event.sig_str(), "sig"); + assert_eq!(index.event.sig_str(), EVENT_SIG); assert_eq!(index.data.data.entries.len(), 2); } diff --git a/crates/event_codec/tests/list_set.rs b/crates/event_codec/tests/list_set.rs @@ -1,6 +1,8 @@ +mod common; #[path = "../src/test_fixtures.rs"] mod test_fixtures; +use common::{AUTHOR, EVENT_ID, EVENT_SIG}; use radroots_event::{ kinds::{KIND_LIST_SET_FOLLOW, KIND_POST}, list::RadrootsListEntry, @@ -182,17 +184,17 @@ fn list_set_metadata_and_index_from_event_roundtrip() { assert_eq!(metadata.data.d_tag, "members.owners"); let index = parsed_from_event( - "id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 44, KIND_LIST_SET_FOLLOW, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); assert_eq!(index.event.kind_u32(), KIND_LIST_SET_FOLLOW); - assert_eq!(index.event.sig_str(), "sig"); + assert_eq!(index.event.sig_str(), EVENT_SIG); assert_eq!(index.data.data.entries.len(), 2); } diff --git a/crates/event_codec/tests/message_file.rs b/crates/event_codec/tests/message_file.rs @@ -1,6 +1,8 @@ +mod common; #[path = "../src/test_fixtures.rs"] mod test_fixtures; +use common::{AUTHOR, EVENT_ID, EVENT_SIG}; use radroots_event::RadrootsEventPtr; use radroots_event::kinds::{KIND_MESSAGE, KIND_MESSAGE_FILE}; use radroots_event::message::RadrootsMessageRecipient; @@ -404,17 +406,17 @@ fn message_file_metadata_and_index_from_event_roundtrip() { assert_eq!(metadata.data.recipients.len(), 2); let index = parsed_from_event( - "id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 77, parts.kind, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); assert_eq!(index.event.kind_u32(), KIND_MESSAGE_FILE); - assert_eq!(index.event.sig_str(), "sig"); + assert_eq!(index.event.sig_str(), EVENT_SIG); assert_eq!(index.data.data.file_type, "image/jpeg"); } diff --git a/crates/event_codec/tests/post.rs b/crates/event_codec/tests/post.rs @@ -1,3 +1,6 @@ +mod common; + +use common::{AUTHOR, EVENT_ID, EVENT_SIG}; use radroots_event::{ farm::RadrootsFarmRef, kinds::{KIND_ARTICLE, KIND_COMMENT, KIND_FARM, KIND_POST}, @@ -702,21 +705,21 @@ fn post_metadata_and_index_from_event_roundtrip() { assert_eq!(metadata.data.content, "hello"); let index = parsed_from_event( - "id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 77, KIND_POST, "hello".to_string(), Vec::new(), - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); - assert_eq!(index.event.id_str(), "id"); - assert_eq!(index.event.author_str(), "author"); + assert_eq!(index.event.id_str(), EVENT_ID); + assert_eq!(index.event.author_str(), AUTHOR); assert_eq!(index.event.created_at_u64(), 77); assert_eq!(index.event.kind_u32(), KIND_POST); assert_eq!(index.event.content(), "hello"); - assert_eq!(index.event.sig_str(), "sig"); + assert_eq!(index.event.sig_str(), EVENT_SIG); assert_eq!(index.data.data.content, "hello"); } diff --git a/crates/event_codec/tests/reaction.rs b/crates/event_codec/tests/reaction.rs @@ -1,3 +1,6 @@ +mod common; + +use common::{AUTHOR as ENVELOPE_AUTHOR, EVENT_ID as ENVELOPE_ID, EVENT_SIG}; use radroots_event::{ kinds::{KIND_ARTICLE, KIND_COMMENT, KIND_POST, KIND_REACTION}, reaction::RadrootsReaction, @@ -423,16 +426,16 @@ fn reaction_metadata_and_index_from_event_roundtrip() { assert_eq!(metadata.data.content, ""); let index = parsed_from_event( - "id".to_string(), - "author".to_string(), + ENVELOPE_ID.to_string(), + ENVELOPE_AUTHOR.to_string(), 99, KIND_REACTION, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); assert_eq!(index.event.kind_u32(), KIND_REACTION); - assert_eq!(index.event.sig_str(), "sig"); + assert_eq!(index.event.sig_str(), EVENT_SIG); assert_event_target(&index.data.data.target); } diff --git a/crates/event_codec/tests/report.rs b/crates/event_codec/tests/report.rs @@ -1,5 +1,8 @@ #![cfg(feature = "serde_json")] +mod common; + +use common::{AUTHOR as ENVELOPE_AUTHOR, EVENT_ID as ENVELOPE_ID, EVENT_SIG}; use radroots_event::{ kinds::{KIND_ARTICLE, KIND_POST, KIND_REPORT}, report::RadrootsReport, @@ -439,15 +442,15 @@ fn report_wrappers_preserve_event_metadata() { assert_eq!(data.data.report_type, RadrootsReportType::Illegal); let parsed = parsed_from_event( - "report_id".to_string(), - "author".to_string(), + ENVELOPE_ID.to_string(), + ENVELOPE_AUTHOR.to_string(), 12, parts.kind, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); - assert_eq!(parsed.event.sig_str(), "sig"); + assert_eq!(parsed.event.sig_str(), EVENT_SIG); assert_eq!(parsed.data.data.reported_pubkey, REPORTED); } diff --git a/crates/event_codec/tests/repost.rs b/crates/event_codec/tests/repost.rs @@ -24,6 +24,10 @@ use radroots_event_codec::{ const EVENT_ID: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; const AUTHOR: &str = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; +const EVENT_SIG: &str = concat!( + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" +); const ARTICLE_D_TAG: &str = "DDDDDDDDDDDDDDDDDDDDDA"; fn note_repost() -> RadrootsRepost { @@ -419,16 +423,16 @@ fn repost_wrappers_preserve_event_metadata() { assert_eq!(data.published_at, 10); let parsed = repost_parsed_from_event( - "repost_id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 10, parts.kind, parts.content, parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); - assert_eq!(parsed.event.sig_str(), "sig"); + assert_eq!(parsed.event.sig_str(), EVENT_SIG); let generic_parts = generic_repost_to_wire_parts(&generic_article_repost()).unwrap(); let generic_data = generic_repost_data_from_event( @@ -443,13 +447,13 @@ fn repost_wrappers_preserve_event_metadata() { assert_eq!(generic_data.data.target_kind, KIND_ARTICLE); let generic_parsed = generic_repost_parsed_from_event( - "generic_id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 11, generic_parts.kind, generic_parts.content, generic_parts.tags, - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); assert_eq!(generic_parsed.event.created_at_u64(), 11); diff --git a/crates/event_codec/tests/seal.rs b/crates/event_codec/tests/seal.rs @@ -1,6 +1,9 @@ use radroots_event::kinds::{KIND_MESSAGE, KIND_SEAL}; use radroots_event::seal::RadrootsSeal; +mod common; + +use common::{AUTHOR, EVENT_ID, EVENT_SIG}; use radroots_event_codec::error::{EventEncodeError, EventParseError}; use radroots_event_codec::seal::decode::{data_from_event, parsed_from_event, seal_from_parts}; use radroots_event_codec::seal::encode::{seal_build_tags, to_wire_parts, to_wire_parts_with_kind}; @@ -62,32 +65,32 @@ fn seal_from_parts_requires_content() { #[test] fn seal_metadata_and_index_from_event_roundtrip() { let metadata = data_from_event( - "id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 14, KIND_SEAL, "payload".to_string(), Vec::new(), ) .unwrap(); - assert_eq!(metadata.id, "id"); - assert_eq!(metadata.author, "author"); + assert_eq!(metadata.id, EVENT_ID); + assert_eq!(metadata.author, AUTHOR); assert_eq!(metadata.published_at, 14); assert_eq!(metadata.kind, KIND_SEAL); assert_eq!(metadata.data.content, "payload"); let index = parsed_from_event( - "id".to_string(), - "author".to_string(), + EVENT_ID.to_string(), + AUTHOR.to_string(), 14, KIND_SEAL, "payload".to_string(), Vec::new(), - "sig".to_string(), + EVENT_SIG.to_string(), ) .unwrap(); assert_eq!(index.event.kind_u32(), KIND_SEAL); - assert_eq!(index.event.sig_str(), "sig"); + assert_eq!(index.event.sig_str(), EVENT_SIG); assert_eq!(index.data.data.content, "payload"); } diff --git a/crates/event_codec/tests/structured_decode.rs b/crates/event_codec/tests/structured_decode.rs @@ -3,6 +3,9 @@ #[path = "../src/test_fixtures.rs"] mod test_fixtures; +mod common; + +use common::{EVENT_ID, EVENT_SIG}; use radroots_core::{RadrootsCoreDecimal, RadrootsCoreQuantity, RadrootsCoreUnit}; use radroots_event::coop::RadrootsCoop; use radroots_event::document::{RadrootsDocument, RadrootsDocumentSubject}; @@ -271,16 +274,16 @@ fn farm_metadata_and_index_decode_roundtrip() { assert_eq!(metadata.data.d_tag, d_tag); let index: RadrootsParsedEvent<RadrootsFarm> = farm_index_from_event( - "id1".to_string(), + EVENT_ID.to_string(), TEST_PUBKEY_HEX.to_string(), 55, KIND_FARM, content, tags, - "sig1".to_string(), + EVENT_SIG.to_string(), ) .expect("farm index"); - assert_eq!(index.event.id_str(), "id1"); + assert_eq!(index.event.id_str(), EVENT_ID); assert_eq!(index.data.data.d_tag, d_tag); } @@ -333,13 +336,13 @@ fn coop_metadata_and_index_decode_roundtrip() { assert_eq!(metadata.data.d_tag, d_tag); let index: RadrootsParsedEvent<RadrootsCoop> = coop_index_from_event( - "id2".to_string(), + EVENT_ID.to_string(), TEST_PUBKEY_HEX.to_string(), 56, KIND_COOP, content, tags, - "sig2".to_string(), + EVENT_SIG.to_string(), ) .expect("coop index"); assert_eq!(index.event.kind_u32(), KIND_COOP); @@ -565,13 +568,13 @@ fn plot_metadata_and_index_decode_roundtrip() { assert_eq!(metadata.data.d_tag, d_tag); let index: RadrootsParsedEvent<RadrootsPlot> = plot_index_from_event( - "id3".to_string(), + EVENT_ID.to_string(), TEST_PUBKEY_HEX.to_string(), 57, KIND_PLOT, content, tags, - "sig3".to_string(), + EVENT_SIG.to_string(), ) .expect("plot index"); assert_eq!(index.event.author_str(), TEST_PUBKEY_HEX); @@ -745,13 +748,13 @@ fn document_metadata_and_index_decode_roundtrip() { assert_eq!(metadata.data.d_tag, d_tag); let index: RadrootsParsedEvent<RadrootsDocument> = document_index_from_event( - "id4".to_string(), + EVENT_ID.to_string(), TEST_PUBKEY_HEX.to_string(), 58, KIND_DOCUMENT, content, tags, - "sig4".to_string(), + EVENT_SIG.to_string(), ) .expect("document index"); assert_eq!(index.event.kind_u32(), KIND_DOCUMENT); @@ -824,16 +827,16 @@ fn resource_area_metadata_and_index_decode_roundtrip() { assert_eq!(metadata.data.d_tag, d_tag); let index: RadrootsParsedEvent<RadrootsResourceArea> = resource_area_index_from_event( - "id5".to_string(), + EVENT_ID.to_string(), TEST_PUBKEY_HEX.to_string(), 59, KIND_RESOURCE_AREA, content, tags, - "sig5".to_string(), + EVENT_SIG.to_string(), ) .expect("area index"); - assert_eq!(index.event.id_str(), "id5"); + assert_eq!(index.event.id_str(), EVENT_ID); assert_eq!(index.data.data.d_tag, d_tag); } @@ -907,15 +910,15 @@ fn resource_cap_metadata_and_index_decode_roundtrip() { assert_eq!(metadata.data.d_tag, d_tag); let index: RadrootsParsedEvent<RadrootsResourceHarvestCap> = resource_cap_index_from_event( - "id6".to_string(), + EVENT_ID.to_string(), TEST_PUBKEY_HEX.to_string(), 60, KIND_RESOURCE_HARVEST_CAP, content, tags, - "sig6".to_string(), + EVENT_SIG.to_string(), ) .expect("cap index"); - assert_eq!(index.event.sig_str(), "sig6"); + assert_eq!(index.event.sig_str(), EVENT_SIG); assert_eq!(index.data.data.d_tag, d_tag); } diff --git a/crates/event_codec/tests/wire.rs b/crates/event_codec/tests/wire.rs @@ -1,9 +1,8 @@ use radroots_event::contract::RadrootsContractValidationError; -use radroots_event::draft::RadrootsDraftError; +use radroots_event::draft::{RadrootsDraftError, RadrootsEventDraft}; use radroots_event::kinds::{KIND_KNOWLEDGE_CLAIM, KIND_KNOWLEDGE_SOURCE, KIND_POST}; -use radroots_event_codec::wire::{ - WireEventParts, canonicalize_tags, empty_content, to_frozen_draft, -}; +use radroots_event::wire::RadrootsNip01EventWireParts; +use radroots_event_codec::wire::{canonicalize_tags, empty_content}; #[test] fn wire_canonicalize_tags_trims_sorts_and_dedups() { @@ -27,36 +26,58 @@ fn wire_canonicalize_tags_trims_sorts_and_dedups() { } #[test] -fn wire_to_frozen_draft_copies_fields_and_computes_expected_id() { - let parts = WireEventParts { +fn wire_parts_are_canonical_event_owned_payload_parts() { + let parts = RadrootsNip01EventWireParts { kind: KIND_POST, content: "hello".to_string(), tags: vec![vec!["t".to_string(), "a".to_string()]], }; + let json = serde_json::to_string(&parts).expect("json"); + let decoded: RadrootsNip01EventWireParts = serde_json::from_str(&json).expect("decoded"); - let draft = - to_frozen_draft(parts, "radroots.social.post.v1", "a".repeat(64), 99).expect("draft"); + assert_eq!(decoded, parts); +} + +#[test] +fn draft_validation_accepts_wire_parts_without_signed_envelope() { + let parts = RadrootsNip01EventWireParts { + kind: KIND_POST, + content: "hello".to_string(), + tags: vec![vec!["t".to_string(), "a".to_string()]], + }; - assert_eq!(draft.kind, KIND_POST); - assert_eq!(draft.created_at, 99); - assert_eq!(draft.expected_pubkey, "a".repeat(64)); - assert_eq!(draft.content, "hello"); - assert_eq!(draft.tags.len(), 1); - assert_eq!(draft.expected_event_id.len(), 64); + let draft = RadrootsEventDraft::new( + "radroots.social.post.v1", + parts.kind, + 99, + parts.tags, + parts.content, + "a".repeat(64), + ) + .expect("draft"); + + assert_eq!(draft.kind_u32(), KIND_POST); + assert_eq!(draft.created_at_u64(), 99); + assert_eq!(draft.expected_pubkey_str(), "a".repeat(64)); + assert_eq!(draft.content(), "hello"); + assert_eq!(draft.tags().len(), 1); + assert_eq!(draft.expected_event_id_str().len(), 64); } #[test] -fn wire_to_frozen_draft_rejects_contract_shape_errors() { - let missing_contract_tag = WireEventParts { +fn draft_validation_rejects_contract_shape_errors() { + let missing_contract_tag = RadrootsNip01EventWireParts { kind: KIND_KNOWLEDGE_CLAIM, content: r#"{"schema":"radroots.knowledge.claim.v1","schema_version":1}"#.to_string(), tags: Vec::new(), }; - let error = to_frozen_draft( - missing_contract_tag, + let error = RadrootsEventDraft::new( "radroots.knowledge.claim.v1", - "a".repeat(64), + missing_contract_tag.kind, 99, + missing_contract_tag.tags, + missing_contract_tag.content, + "a".repeat(64), ) .expect_err("missing contract tag"); assert!(matches!( @@ -70,7 +91,7 @@ fn wire_to_frozen_draft_rejects_contract_shape_errors() { } )); - let invalid_relay = WireEventParts { + let invalid_relay = RadrootsNip01EventWireParts { kind: KIND_KNOWLEDGE_CLAIM, content: r#"{"schema":"radroots.knowledge.claim.v1","schema_version":1}"#.to_string(), tags: vec![ @@ -88,11 +109,13 @@ fn wire_to_frozen_draft_rejects_contract_shape_errors() { ], ], }; - let error = to_frozen_draft( - invalid_relay, + let error = RadrootsEventDraft::new( "radroots.knowledge.claim.v1", - "a".repeat(64), + invalid_relay.kind, 99, + invalid_relay.tags, + invalid_relay.content, + "a".repeat(64), ) .expect_err("invalid relay"); assert!(matches!(