intent.rs (4533B)
1 //! Exact v1 signing commitment data, without signer or execution authority. 2 use core::num::NonZeroU64; 3 use radroots_domain::{OperationId, PublicKey}; 4 use sha2::{Digest, Sha256}; 5 6 const DOMAIN: &[u8] = b"radroots.exact_signing_intent.v1\0"; 7 const MAX_OBSERVATION_UNIX_MS: u64 = 253_402_300_799_999; 8 9 /// Purpose of an exact transport artifact, separate from business permission. 10 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 11 #[non_exhaustive] 12 pub enum ArtifactRole { 13 /// A public authored event, excluding private carrier and rumor kinds. 14 PublicEvent, 15 /// One encrypted kind-13 business seal for an exact recipient. 16 BusinessSeal, 17 /// One outer kind-1059 gift wrap for an exact recipient. 18 GiftWrap, 19 } 20 /// Invalid descriptor data; no provider diagnostic or private input is retained. 21 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 22 pub struct InvalidIntentBinding; 23 impl core::fmt::Display for InvalidIntentBinding { 24 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 25 f.write_str("invalid exact intent binding") 26 } 27 } 28 impl core::error::Error for InvalidIntentBinding {} 29 30 /// Immutable v1 commitment, with no signature, qualified replay guarantee, 31 /// command grant, dispatch permission or proof of a retained transaction. 32 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 33 pub struct IntentBinding { 34 operation_id: OperationId, 35 command_digest: [u8; 32], 36 digest: [u8; 32], 37 } 38 impl IntentBinding { 39 /// Hashes exact already-captured data. The caller independently validates 40 /// the plan, role, current access, provider and durable execution boundary. 41 /// This preserves the existing v1 byte layout and domain separator. 42 pub fn from_components( 43 operation_id: OperationId, 44 command_digest: [u8; 32], 45 plan_digest: [u8; 32], 46 role: ArtifactRole, 47 recipient: Option<PublicKey>, 48 deadline_unix_ms: NonZeroU64, 49 ) -> Result<Self, InvalidIntentBinding> { 50 if deadline_unix_ms.get() > MAX_OBSERVATION_UNIX_MS 51 || matches!(role, ArtifactRole::PublicEvent) != recipient.is_none() 52 { 53 return Err(InvalidIntentBinding); 54 } 55 let mut digest = Sha256::new(); 56 digest.update(DOMAIN); 57 digest.update(operation_id.as_bytes()); 58 digest.update(command_digest); 59 digest.update(plan_digest); 60 digest.update([match role { 61 ArtifactRole::PublicEvent => 0, 62 ArtifactRole::BusinessSeal => 1, 63 ArtifactRole::GiftWrap => 2, 64 }]); 65 match recipient { 66 Some(peer) => { 67 digest.update([1]); 68 digest.update(peer.as_bytes()); 69 } 70 None => digest.update([0]), 71 } 72 digest.update(deadline_unix_ms.get().to_be_bytes()); 73 Ok(Self { 74 operation_id, 75 command_digest, 76 digest: digest.finalize().into(), 77 }) 78 } 79 /// Returns the full original operation identity, without truncation. 80 pub const fn operation_id(self) -> OperationId { 81 self.operation_id 82 } 83 /// Borrows the original semantic command commitment. 84 pub const fn command_digest(&self) -> &[u8; 32] { 85 &self.command_digest 86 } 87 /// Borrows the exact domain-separated v1 commitment. 88 pub const fn digest(&self) -> &[u8; 32] { 89 &self.digest 90 } 91 } 92 93 /// Captured provider declaration. Execution must independently verify current 94 /// support before any reissue; this data authenticates no provider guarantee. 95 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 96 pub enum SigningReplay { 97 /// Uncertain invocation requires reconciliation, never automatic reissue. 98 NonReplayable, 99 /// The actual local provider independently qualifies safe exact reissue. 100 LocalReplaySafe, 101 /// The actual provider independently binds exact replay to request identity. 102 ExactReplayByRequestId, 103 } 104 #[cfg(any(feature = "chat", feature = "listing", feature = "commerce"))] 105 impl SigningReplay { 106 pub(crate) const fn code(self) -> u8 { 107 match self { 108 Self::NonReplayable => 0, 109 Self::LocalReplaySafe => 1, 110 Self::ExactReplayByRequestId => 2, 111 } 112 } 113 pub(crate) fn from_code(code: u8) -> Result<Self, crate::nip01::WireError> { 114 match code { 115 0 => Ok(Self::NonReplayable), 116 1 => Ok(Self::LocalReplaySafe), 117 2 => Ok(Self::ExactReplayByRequestId), 118 _ => Err(crate::nip01::WireError::InvalidInput), 119 } 120 } 121 }