reference.rs (4472B)
1 use super::ListingId; 2 use crate::{EnvelopeEventId, PublicKey}; 3 use core::fmt; 4 5 /// Exact public listing coordinate, without an authenticated observation claim. 6 #[derive(Clone, Copy, Eq, PartialEq)] 7 pub struct ListingCoordinate { 8 author: PublicKey, 9 d: ListingId, 10 } 11 impl ListingCoordinate { 12 /// Captures validated identity parts; the kind is always NIP-99 kind30402. 13 pub const fn new(author: PublicKey, d: ListingId) -> Self { 14 Self { author, d } 15 } 16 /// Returns the fixed listing kind, never a caller-supplied alternative. 17 pub const fn kind(self) -> u16 { 18 30402 19 } 20 /// Returns the participant named by the coordinate, without author proof. 21 pub const fn author(self) -> PublicKey { 22 self.author 23 } 24 /// Returns the exact stable 128-bit discriminator. 25 pub const fn d(self) -> ListingId { 26 self.d 27 } 28 } 29 impl fmt::Debug for ListingCoordinate { 30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 31 f.write_str("ListingCoordinate(<redacted>)") 32 } 33 } 34 #[cfg(feature = "serde")] 35 impl serde::Serialize for ListingCoordinate { 36 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { 37 use serde::ser::SerializeStruct; 38 let mut out = serializer.serialize_struct("ListingCoordinate", 3)?; 39 out.serialize_field("kind", &self.kind())?; 40 out.serialize_field("author", &self.author)?; 41 out.serialize_field("d", &self.d)?; 42 out.end() 43 } 44 } 45 46 #[cfg(feature = "serde")] 47 impl<'de> serde::Deserialize<'de> for ListingCoordinate { 48 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { 49 #[derive(serde::Deserialize)] 50 #[serde(deny_unknown_fields)] 51 struct Wire { 52 kind: u16, 53 #[serde(deserialize_with = "canonical_author")] 54 author: PublicKey, 55 d: ListingId, 56 } 57 struct Visitor; 58 impl<'de> serde::de::Visitor<'de> for Visitor { 59 type Value = Wire; 60 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 61 f.write_str("a closed NIP-99 listing coordinate object") 62 } 63 fn visit_map<A: serde::de::MapAccess<'de>>(self, map: A) -> Result<Wire, A::Error> { 64 <Wire as serde::Deserialize>::deserialize( 65 serde::de::value::MapAccessDeserializer::new(map), 66 ) 67 } 68 } 69 let wire = deserializer.deserialize_map(Visitor)?; 70 if wire.kind != 30402 { 71 return Err(serde::de::Error::custom( 72 "unsupported listing coordinate kind", 73 )); 74 } 75 Ok(Self::new(wire.author, wire.d)) 76 } 77 } 78 79 #[cfg(feature = "serde")] 80 fn canonical_author<'de, D: serde::Deserializer<'de>>( 81 deserializer: D, 82 ) -> Result<PublicKey, D::Error> { 83 struct Author; 84 impl serde::de::Visitor<'_> for Author { 85 type Value = PublicKey; 86 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 87 f.write_str("a canonical lowercase public key") 88 } 89 fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<PublicKey, E> { 90 let key = PublicKey::from_hex(value).map_err(E::custom)?; 91 if key.to_hex() != value { 92 return Err(E::custom("noncanonical listing author")); 93 } 94 Ok(key) 95 } 96 } 97 deserializer.deserialize_str(Author) 98 } 99 100 /// Exact revision identity and coordinate; bytes alone authenticate no source. 101 #[derive(Clone, Copy, Eq, PartialEq)] 102 #[cfg_attr(feature = "serde", derive(serde::Serialize))] 103 pub struct ListingRevision { 104 coordinate: ListingCoordinate, 105 event_id: EnvelopeEventId, 106 } 107 impl ListingRevision { 108 /// Captures structural data; source authentication/admission remains separate. 109 pub const fn new(coordinate: ListingCoordinate, event_id: EnvelopeEventId) -> Self { 110 Self { 111 coordinate, 112 event_id, 113 } 114 } 115 /// Returns the complete stable coordinate. 116 pub const fn coordinate(self) -> ListingCoordinate { 117 self.coordinate 118 } 119 /// Returns the exact signed-artifact identity claimed as the source. 120 pub const fn event_id(self) -> EnvelopeEventId { 121 self.event_id 122 } 123 } 124 impl fmt::Debug for ListingRevision { 125 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 126 f.write_str("ListingRevision(<redacted>)") 127 } 128 }