lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

private.rs (3448B)


      1 //! One obtained private outer, independent of a business-work descriptor.
      2 use super::{OperationError, outer};
      3 use core::fmt;
      4 use radroots_domain::{EnvelopeEventId, PublicKey, UnixSeconds};
      5 use sha2::{Digest, Sha256};
      6 use std::string::String;
      7 use zeroize::Zeroizing;
      8 
      9 /// Bounded signed NIP-59 outer and temporary wrapping material. A verified
     10 /// carrier proves no inner containment, installation, grant or transport right.
     11 /// Every concrete owner must verify these against its actual retained work.
     12 pub struct PrivateEnvelopeCandidate {
     13     recipient: PublicKey,
     14     event: EnvelopeEventId,
     15     author: PublicKey,
     16     created_at: UnixSeconds,
     17     digest: [u8; 32],
     18     raw: Zeroizing<String>,
     19     key: Zeroizing<[u8; 32]>,
     20 }
     21 impl PrivateEnvelopeCandidate {
     22     /// Bounds before copying and verifies the exact one-recipient outer.
     23     /// The reported key must still be derived and used to verify actual inner
     24     /// containment by the qualified store; this constructor grants no authority.
     25     pub fn from_obtained(
     26         recipient: PublicKey,
     27         raw: &str,
     28         wrapping_key: &[u8; 32],
     29     ) -> Result<Self, OperationError> {
     30         let carrier = outer(raw, recipient)?;
     31         Ok(Self {
     32             recipient,
     33             event: EnvelopeEventId::from_bytes(*carrier.envelope().id().as_bytes()),
     34             author: *carrier.envelope().author(),
     35             created_at: UnixSeconds::new(carrier.envelope().created_at_u64())
     36                 .map_err(|_| OperationError::InvalidEvidence)?,
     37             digest: Sha256::digest(raw.as_bytes()).into(),
     38             raw: Zeroizing::new(String::from(raw)),
     39             key: Zeroizing::new(*wrapping_key),
     40         })
     41     }
     42     /// Exact declared recipient authenticated by the outer's single p tag.
     43     pub const fn recipient(&self) -> PublicKey {
     44         self.recipient
     45     }
     46     /// Actual signed outer event identity, separate from the original seal.
     47     pub const fn event_id(&self) -> EnvelopeEventId {
     48         self.event
     49     }
     50     /// Actual outer author; the store must prove the temporary key matches.
     51     pub const fn author(&self) -> PublicKey {
     52         self.author
     53     }
     54     /// Signed randomized time, without a grant or trusted observation.
     55     pub const fn created_at(&self) -> UnixSeconds {
     56         self.created_at
     57     }
     58     /// SHA-256 of the exact original outer representation.
     59     pub const fn digest(&self) -> &[u8; 32] {
     60         &self.digest
     61     }
     62     /// Exact obtained bytes for protected handling, never diagnostic output.
     63     pub fn raw_json(&self) -> &str {
     64         &self.raw
     65     }
     66     /// Scoped borrow solely for qualified containment verification.
     67     pub fn with_wrapping_key<T>(&self, verify: impl FnOnce(&[u8; 32]) -> T) -> T {
     68         verify(&self.key)
     69     }
     70     /// Explicit bounded owner copy; retain the original until actual storage
     71     /// completion is reconciled. This creates no persistence or dispatch fact.
     72     pub fn retained_copy(&self) -> Self {
     73         Self {
     74             recipient: self.recipient,
     75             event: self.event,
     76             author: self.author,
     77             created_at: self.created_at,
     78             digest: self.digest,
     79             raw: Zeroizing::new(String::from(self.raw_json())),
     80             key: Zeroizing::new(*self.key),
     81         }
     82     }
     83 }
     84 impl fmt::Debug for PrivateEnvelopeCandidate {
     85     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     86         f.write_str("PrivateEnvelopeCandidate(<redacted>)")
     87     }
     88 }