plan.rs (8894B)
1 //! Common immutable signing-plan data, with explicit artifact families. 2 use crate::{ 3 command::CapturedCommand, 4 intent::{ArtifactRole, SigningReplay}, 5 nip01::{EventFields, WireError}, 6 }; 7 use alloc::vec::Vec; 8 use core::fmt; 9 use radroots_domain::{CommandDigest, OperationId, PublicKey, UnixSeconds}; 10 11 /// Exact T1 artifact selection. Public events have no invented private peer. 12 /// Selection is data only and establishes neither authority nor an invocation. 13 #[derive(Clone, Copy, Eq, PartialEq, Hash)] 14 pub enum SigningTarget { 15 /// The single exact public event in a public plan. 16 PublicEvent, 17 /// One exact recipient's business seal in a private plan. 18 BusinessSeal(PublicKey), 19 } 20 impl SigningTarget { 21 /// Role bound by the selected plan artifact. 22 pub const fn role(self) -> ArtifactRole { 23 match self { 24 Self::PublicEvent => ArtifactRole::PublicEvent, 25 Self::BusinessSeal(_) => ArtifactRole::BusinessSeal, 26 } 27 } 28 /// No public event is represented by a participant key. 29 pub const fn recipient(self) -> Option<PublicKey> { 30 match self { 31 Self::PublicEvent => None, 32 Self::BusinessSeal(peer) => Some(peer), 33 } 34 } 35 /// Bounded unambiguous storage selector. Its public tag is not a recipient. 36 pub fn key(self) -> Vec<u8> { 37 match self { 38 Self::PublicEvent => alloc::vec![0], 39 Self::BusinessSeal(peer) => peer.as_bytes().to_vec(), 40 } 41 } 42 /// Decodes only the exact supported tagged selector profiles. 43 pub fn from_key(key: &[u8]) -> Result<Self, WireError> { 44 match key { 45 [0] => Ok(Self::PublicEvent), 46 rest if rest.len() == 32 => { 47 PublicKey::from_bytes(rest.try_into().map_err(|_| WireError::InvalidInput)?) 48 .map(Self::BusinessSeal) 49 .map_err(|_| WireError::InvalidInput) 50 } 51 _ => Err(WireError::InvalidInput), 52 } 53 } 54 } 55 impl From<PublicKey> for SigningTarget { 56 fn from(recipient: PublicKey) -> Self { 57 Self::BusinessSeal(recipient) 58 } 59 } 60 impl fmt::Debug for SigningTarget { 61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 62 f.write_str(match self { 63 Self::PublicEvent => "SigningTarget::PublicEvent", 64 Self::BusinessSeal(_) => "SigningTarget::BusinessSeal(<redacted>)", 65 }) 66 } 67 } 68 69 /// Borrowed exact known artifact. This authenticates no signer or store fact. 70 pub enum SigningArtifact<'a> { 71 /// Exact assigned-kind public listing. 72 #[cfg(feature = "listing")] 73 Public(&'a crate::listing::ListingEvent), 74 /// Exact private business seal, with its actual recipient. 75 #[cfg(feature = "chat")] 76 Seal(&'a crate::chat::ChatSeal), 77 } 78 impl SigningArtifact<'_> { 79 /// Borrows original NIP-01 fields without rebuilding content or tags. 80 pub fn fields(&self) -> EventFields<'_> { 81 match self { 82 #[cfg(feature = "listing")] 83 Self::Public(event) => event.fields(), 84 #[cfg(feature = "chat")] 85 Self::Seal(seal) => seal.fields(), 86 } 87 } 88 /// Original exact event ID. 89 pub fn event_id(&self) -> [u8; 32] { 90 match self { 91 #[cfg(feature = "listing")] 92 Self::Public(event) => *event.event_id(), 93 #[cfg(feature = "chat")] 94 Self::Seal(seal) => seal.event_id(), 95 } 96 } 97 /// Exact role/recipient pair without a public-peer fallback. 98 pub fn target(&self) -> SigningTarget { 99 match self { 100 #[cfg(feature = "listing")] 101 Self::Public(_) => SigningTarget::PublicEvent, 102 #[cfg(feature = "chat")] 103 Self::Seal(seal) => SigningTarget::BusinessSeal(seal.recipient()), 104 } 105 } 106 } 107 impl fmt::Debug for SigningArtifact<'_> { 108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 109 f.write_str("SigningArtifact(<redacted>)") 110 } 111 } 112 113 /// Exact complete plan data. No public artifact is represented as a chat seal, 114 /// and no optional recipient is filled with a synthetic participant. 115 pub enum CapturedSigningPlan { 116 /// Original private profile, preserving its established binary bytes. 117 #[cfg(feature = "chat")] 118 Chat(crate::chat::ChatSigningPlan), 119 /// Exact known-kind public listing preimage and selected target descriptors. 120 #[cfg(feature = "listing")] 121 Listing(crate::listing::PublicSigningPlan), 122 } 123 impl CapturedSigningPlan { 124 /// Resolves only an actual retained artifact in this exact plan family. 125 pub fn artifact(&self, target: SigningTarget) -> Result<SigningArtifact<'_>, WireError> { 126 match (self, target) { 127 #[cfg(feature = "listing")] 128 (Self::Listing(plan), SigningTarget::PublicEvent) => { 129 Ok(SigningArtifact::Public(plan.event())) 130 } 131 #[cfg(feature = "chat")] 132 (Self::Chat(plan), SigningTarget::BusinessSeal(peer)) => plan 133 .seals() 134 .iter() 135 .find(|seal| seal.recipient() == peer) 136 .map(SigningArtifact::Seal) 137 .ok_or(WireError::InvalidInput), 138 _ => Err(WireError::InvalidInput), 139 } 140 } 141 /// Reconstructs only the family of the independently retained command. 142 pub fn parse(command: &CapturedCommand, wire: &[u8]) -> Result<Self, WireError> { 143 match command { 144 #[cfg(feature = "chat")] 145 CapturedCommand::Chat(command) => { 146 crate::chat::ChatSigningPlan::parse(command, wire).map(Self::Chat) 147 } 148 #[cfg(feature = "listing")] 149 CapturedCommand::Listing(command) => { 150 crate::listing::PublicSigningPlan::parse(command, wire).map(Self::Listing) 151 } 152 #[cfg(not(feature = "chat"))] 153 CapturedCommand::Chat(_) => Err(WireError::InvalidInput), 154 } 155 } 156 /// Original operation identity. 157 pub const fn operation_id(&self) -> OperationId { 158 match self { 159 #[cfg(feature = "chat")] 160 Self::Chat(p) => p.operation_id(), 161 #[cfg(feature = "listing")] 162 Self::Listing(p) => p.operation_id(), 163 } 164 } 165 /// Exact command commitment. 166 pub const fn command_digest(&self) -> CommandDigest { 167 match self { 168 #[cfg(feature = "chat")] 169 Self::Chat(p) => p.command_digest(), 170 #[cfg(feature = "listing")] 171 Self::Listing(p) => p.command_digest(), 172 } 173 } 174 /// Original business event time. 175 pub const fn created_at(&self) -> UnixSeconds { 176 match self { 177 #[cfg(feature = "chat")] 178 Self::Chat(p) => p.created_at(), 179 #[cfg(feature = "listing")] 180 Self::Listing(p) => p.created_at(), 181 } 182 } 183 /// Historical declaration, without permission to reissue. 184 pub const fn replay(&self) -> SigningReplay { 185 match self { 186 #[cfg(feature = "chat")] 187 Self::Chat(p) => p.replay(), 188 #[cfg(feature = "listing")] 189 Self::Listing(p) => p.replay(), 190 } 191 } 192 /// Complete immutable protected representation. 193 pub fn protected_wire(&self) -> &[u8] { 194 match self { 195 #[cfg(feature = "chat")] 196 Self::Chat(p) => p.protected_wire(), 197 #[cfg(feature = "listing")] 198 Self::Listing(p) => p.protected_wire(), 199 } 200 } 201 /// Commitment to every retained field. 202 pub const fn digest(&self) -> &[u8; 32] { 203 match self { 204 #[cfg(feature = "chat")] 205 Self::Chat(p) => p.digest(), 206 #[cfg(feature = "listing")] 207 Self::Listing(p) => p.digest(), 208 } 209 } 210 /// Borrows actual private preimages only. 211 #[cfg(feature = "chat")] 212 pub const fn chat(&self) -> Option<&crate::chat::ChatSigningPlan> { 213 match self { 214 Self::Chat(p) => Some(p), 215 #[cfg(feature = "listing")] 216 Self::Listing(_) => None, 217 } 218 } 219 /// Borrows actual public data only. 220 #[cfg(feature = "listing")] 221 pub const fn listing(&self) -> Option<&crate::listing::PublicSigningPlan> { 222 match self { 223 Self::Listing(p) => Some(p), 224 #[cfg(feature = "chat")] 225 Self::Chat(_) => None, 226 } 227 } 228 } 229 #[cfg(feature = "chat")] 230 impl From<crate::chat::ChatSigningPlan> for CapturedSigningPlan { 231 fn from(p: crate::chat::ChatSigningPlan) -> Self { 232 Self::Chat(p) 233 } 234 } 235 #[cfg(feature = "listing")] 236 impl From<crate::listing::PublicSigningPlan> for CapturedSigningPlan { 237 fn from(p: crate::listing::PublicSigningPlan) -> Self { 238 Self::Listing(p) 239 } 240 } 241 impl fmt::Debug for CapturedSigningPlan { 242 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 243 f.write_str("CapturedSigningPlan(<redacted>)") 244 } 245 }