lib

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

sealed.rs (1666B)


      1 //! Shared exact core for contract-specific sealed Nostr builders.
      2 
      3 #![forbid(unsafe_code)]
      4 
      5 use crate::{
      6     error::Error,
      7     types::{
      8         ExternalSigningRequest, RadrootsNostrEvent, RadrootsNostrKeys, RadrootsNostrPublicKey,
      9         RadrootsNostrTimestamp,
     10     },
     11 };
     12 use radroots_event_codec::authoring::{AuthoredEventBody, AuthoredEventPlan};
     13 
     14 pub(crate) struct SealedBuilderCore {
     15     body: AuthoredEventBody,
     16     created_at: Option<RadrootsNostrTimestamp>,
     17 }
     18 
     19 impl SealedBuilderCore {
     20     pub(crate) const fn new(body: AuthoredEventBody) -> Self {
     21         Self {
     22             body,
     23             created_at: None,
     24         }
     25     }
     26 
     27     pub(crate) const fn at(body: AuthoredEventBody, created_at: RadrootsNostrTimestamp) -> Self {
     28         Self {
     29             body,
     30             created_at: Some(created_at),
     31         }
     32     }
     33 
     34     pub(crate) const fn custom_created_at(mut self, created_at: RadrootsNostrTimestamp) -> Self {
     35         self.created_at = Some(created_at);
     36         self
     37     }
     38 
     39     pub(crate) fn sign_with_keys(
     40         self,
     41         keys: &RadrootsNostrKeys,
     42     ) -> Result<RadrootsNostrEvent, Error> {
     43         self.into_external_signing_request(keys.public_key())?
     44             .sign_with_keys(keys)
     45     }
     46 
     47     pub(crate) fn into_external_signing_request(
     48         self,
     49         public_key: RadrootsNostrPublicKey,
     50     ) -> Result<ExternalSigningRequest, Error> {
     51         let created_at = self
     52             .created_at
     53             .unwrap_or_else(RadrootsNostrTimestamp::now)
     54             .as_secs();
     55         let plan = AuthoredEventPlan::bind(self.body, created_at, public_key.to_hex())?;
     56         ExternalSigningRequest::from_authored_plan(plan)
     57     }
     58 }