lib

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

gift_wrap.rs (1810B)


      1 use crate::nip01::{EventFields, WireError, signed_json_len};
      2 use crate::nip59::{MAX_TIME, SIGNED_MAX_BYTES, nip44_v2_payload_len};
      3 use alloc::{string::String, vec};
      4 use radroots_domain::PublicKey;
      5 
      6 /// Complete kind-1059 budget for an already obtained seal's exact UTF-8 bytes.
      7 /// A canonical reserialization cannot stand in for the original representation.
      8 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
      9 pub struct GiftWrapBudget {
     10     ciphertext_bytes: usize,
     11     signed_max_bytes: usize,
     12 }
     13 
     14 impl GiftWrapBudget {
     15     /// Counts padding, base64 and the complete one-recipient signed envelope.
     16     /// This validates size only, without authenticating the seal or recipient.
     17     pub fn for_seal(seal_bytes: usize, recipient: PublicKey) -> Result<Self, WireError> {
     18         if seal_bytes >= SIGNED_MAX_BYTES {
     19             return Err(WireError::LimitExceeded);
     20         }
     21         let ciphertext_bytes = nip44_v2_payload_len(seal_bytes)?;
     22         let tags = [vec![String::from("p"), recipient.to_hex()]];
     23         let fields = EventFields {
     24             author: recipient,
     25             created_at: MAX_TIME,
     26             kind: 1059,
     27             tags: &tags,
     28             content: "",
     29         };
     30         let signed_max_bytes = signed_json_len(&fields, SIGNED_MAX_BYTES)? + ciphertext_bytes;
     31         if signed_max_bytes > SIGNED_MAX_BYTES {
     32             return Err(WireError::LimitExceeded);
     33         }
     34         Ok(Self {
     35             ciphertext_bytes,
     36             signed_max_bytes,
     37         })
     38     }
     39 
     40     /// Exact encoded ciphertext size for this seal representation.
     41     pub const fn ciphertext_bytes(self) -> usize {
     42         self.ciphertext_bytes
     43     }
     44 
     45     /// Complete signed JSON upper bound using the largest supported timestamp.
     46     pub const fn signed_max_bytes(self) -> usize {
     47         self.signed_max_bytes
     48     }
     49 }