lib

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

encode.rs (2377B)


      1 use radroots_event::{envelope::kind::is_request_kind, social::job_request::JobRequest};
      2 
      3 use crate::job::encode::{JobEncodeError, canonicalize_tags};
      4 use crate::job::util::{job_input_type_tag, push_bid_tag_sat};
      5 use radroots_event::wire::Nip01EventWireParts;
      6 
      7 #[cfg(not(feature = "std"))]
      8 use alloc::{
      9     string::{String, ToString},
     10     vec,
     11     vec::Vec,
     12 };
     13 
     14 pub fn job_request_build_tags(req: &JobRequest) -> Vec<Vec<String>> {
     15     let mut tags: Vec<Vec<String>> = Vec::with_capacity(
     16         req.inputs.len()
     17             + req.params.len()
     18             + req.relays.len()
     19             + req.providers.len()
     20             + req.topics.len()
     21             + usize::from(req.output.is_some())
     22             + usize::from(req.bid_sat.is_some())
     23             + usize::from(req.encrypted),
     24     );
     25 
     26     for i in &req.inputs {
     27         let mut t = Vec::with_capacity(5);
     28         t.push("i".to_string());
     29         t.push(i.data.clone());
     30         t.push(job_input_type_tag(i.input_type).to_string());
     31         if let Some(relay) = &i.relay {
     32             t.push(relay.clone());
     33         }
     34         if let Some(marker) = &i.marker {
     35             t.push(marker.clone());
     36         }
     37         tags.push(t);
     38     }
     39 
     40     if let Some(out) = &req.output {
     41         tags.push(vec!["output".into(), out.clone()]);
     42     }
     43 
     44     for p in &req.params {
     45         tags.push(vec!["param".into(), p.key.clone(), p.value.clone()]);
     46     }
     47 
     48     if let Some(bid_sat) = req.bid_sat {
     49         push_bid_tag_sat(&mut tags, bid_sat);
     50     }
     51 
     52     for r in &req.relays {
     53         tags.push(vec!["relays".into(), r.clone()]);
     54     }
     55 
     56     for p in &req.providers {
     57         tags.push(vec!["p".into(), p.clone()]);
     58     }
     59 
     60     for t in &req.topics {
     61         tags.push(vec!["t".into(), t.clone()]);
     62     }
     63 
     64     if req.encrypted {
     65         tags.push(vec!["encrypted".into()]);
     66     }
     67 
     68     tags
     69 }
     70 
     71 pub fn to_wire_parts(
     72     req: &JobRequest,
     73     content: &str,
     74 ) -> Result<Nip01EventWireParts, JobEncodeError> {
     75     let kind = req.kind as u32;
     76     if !is_request_kind(kind) {
     77         return Err(JobEncodeError::InvalidKind(kind));
     78     }
     79     if req.encrypted && req.providers.is_empty() {
     80         return Err(JobEncodeError::MissingProvidersForEncrypted);
     81     }
     82 
     83     let mut tags = job_request_build_tags(req);
     84     canonicalize_tags(&mut tags);
     85 
     86     Ok(Nip01EventWireParts {
     87         kind,
     88         content: content.to_string(),
     89         tags,
     90     })
     91 }