lib

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

encode.rs (1789B)


      1 use radroots_events::{job_feedback::RadrootsJobFeedback, kinds::KIND_JOB_FEEDBACK};
      2 
      3 use crate::job::encode::{JobEncodeError, WireEventParts, canonicalize_tags};
      4 use crate::job::util::{feedback_status_tag, push_amount_tag_msat};
      5 
      6 #[cfg(not(feature = "std"))]
      7 use alloc::{
      8     string::{String, ToString},
      9     vec,
     10     vec::Vec,
     11 };
     12 
     13 pub fn job_feedback_build_tags(fb: &RadrootsJobFeedback) -> Vec<Vec<String>> {
     14     let mut tags: Vec<Vec<String>> = Vec::with_capacity(
     15         2 + usize::from(fb.customer_pubkey.is_some())
     16             + usize::from(fb.payment.is_some())
     17             + usize::from(fb.encrypted),
     18     );
     19 
     20     let mut st = Vec::with_capacity(3);
     21     st.push("status".to_string());
     22     st.push(feedback_status_tag(fb.status).to_string());
     23     if let Some(info) = &fb.extra_info {
     24         st.push(info.clone());
     25     }
     26     tags.push(st);
     27 
     28     if let Some(pay) = &fb.payment {
     29         push_amount_tag_msat(&mut tags, pay.amount_sat, pay.bolt11.clone());
     30     }
     31 
     32     let mut e = Vec::with_capacity(3);
     33     e.push("e".to_string());
     34     e.push(fb.request_event.id.clone());
     35     if let Some(r) = &fb.request_event.relays {
     36         e.push(r.clone());
     37     }
     38     tags.push(e);
     39 
     40     if let Some(p) = &fb.customer_pubkey {
     41         tags.push(vec!["p".into(), p.clone()]);
     42     }
     43 
     44     if fb.encrypted {
     45         tags.push(vec!["encrypted".into()]);
     46     }
     47 
     48     tags
     49 }
     50 
     51 pub fn to_wire_parts(
     52     fb: &RadrootsJobFeedback,
     53     content: &str,
     54 ) -> Result<WireEventParts, JobEncodeError> {
     55     let kind = fb.kind as u32;
     56     if kind != KIND_JOB_FEEDBACK {
     57         return Err(JobEncodeError::InvalidKind(kind));
     58     }
     59 
     60     let mut tags = job_feedback_build_tags(fb);
     61     canonicalize_tags(&mut tags);
     62 
     63     Ok(WireEventParts {
     64         kind,
     65         content: content.to_string(),
     66         tags,
     67     })
     68 }