lib

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

encode.rs (2335B)


      1 use radroots_event::{envelope::kind::is_result_kind, social::job_result::JobResult};
      2 
      3 use crate::job::encode::{JobEncodeError, canonicalize_tags};
      4 use crate::job::util::{job_input_type_tag, push_amount_tag_msat};
      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_result_build_tags(res: &JobResult) -> Vec<Vec<String>> {
     15     let mut tags: Vec<Vec<String>> = Vec::with_capacity(
     16         2 + res.inputs.len()
     17             + usize::from(res.customer_pubkey.is_some())
     18             + usize::from(res.payment.is_some())
     19             + usize::from(res.encrypted),
     20     );
     21 
     22     let mut e = Vec::with_capacity(3);
     23     e.push("e".to_string());
     24     e.push(res.request_event.id.clone());
     25     if let Some(r) = &res.request_event.relays {
     26         e.push(r.clone());
     27     }
     28     tags.push(e);
     29 
     30     if let Some(j) = &res.request_json {
     31         tags.push(vec!["request".into(), j.clone()]);
     32     }
     33 
     34     if !res.encrypted {
     35         for i in &res.inputs {
     36             let mut t = Vec::with_capacity(5);
     37             t.push("i".to_string());
     38             t.push(i.data.clone());
     39             t.push(job_input_type_tag(i.input_type).to_string());
     40             if let Some(relay) = &i.relay {
     41                 t.push(relay.clone());
     42             }
     43             if let Some(marker) = &i.marker {
     44                 t.push(marker.clone());
     45             }
     46             tags.push(t);
     47         }
     48     }
     49 
     50     if let Some(p) = &res.customer_pubkey {
     51         tags.push(vec!["p".into(), p.clone()]);
     52     }
     53 
     54     if let Some(pay) = &res.payment {
     55         push_amount_tag_msat(&mut tags, pay.amount_sat, pay.bolt11.clone());
     56     }
     57 
     58     if res.encrypted {
     59         tags.push(vec!["encrypted".into()]);
     60     }
     61 
     62     tags
     63 }
     64 
     65 pub fn to_wire_parts(
     66     res: &JobResult,
     67     content: &str,
     68 ) -> Result<Nip01EventWireParts, JobEncodeError> {
     69     let kind = res.kind as u32;
     70     if !is_result_kind(kind) {
     71         return Err(JobEncodeError::InvalidKind(kind));
     72     }
     73     if res.encrypted && !res.inputs.is_empty() {
     74         return Err(JobEncodeError::EmptyRequiredField("inputs-when-encrypted"));
     75     }
     76 
     77     let mut tags = job_result_build_tags(res);
     78 
     79     canonicalize_tags(&mut tags);
     80 
     81     Ok(Nip01EventWireParts {
     82         kind,
     83         content: content.to_string(),
     84         tags,
     85     })
     86 }