952e036c191617a66bb0b34661ca487224b73983
[dnssec-prover] / wasmpack / src / lib.rs
1 //! WASM-compatible verification wrappers
2
3 use dnssec_prover::ser::parse_rr_stream;
4 use dnssec_prover::validation::{verify_rr_stream, ValidationError};
5
6 use wasm_bindgen::prelude::wasm_bindgen;
7
8 use core::fmt::Write;
9
10 #[global_allocator]
11 static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
12
13 #[wasm_bindgen]
14 /// Verifies an RFC 9102-formatted proof and returns the [`VerifiedRRStream`] in JSON form.
15 pub fn verify_byte_stream(stream: Vec<u8>) -> String {
16         match do_verify_byte_stream(stream) {
17                 Ok(r) => r,
18                 Err(e) => format!("{{\"error\":\"{:?}\"}}", e),
19         }
20 }
21
22 fn do_verify_byte_stream(stream: Vec<u8>) -> Result<String, ValidationError> {
23         let rrs = parse_rr_stream(&stream).map_err(|()| ValidationError::Invalid)?;
24         let verified_rrs = verify_rr_stream(&rrs)?;
25         let mut resp = String::new();
26         write!(&mut resp, "{}",
27                 format_args!("{{\"valid_from\": {}, \"expires\": {}, \"max_cache_ttl\": {}, \"verified_rrs\": [",
28                 verified_rrs.valid_from, verified_rrs.expires, verified_rrs.max_cache_ttl)
29         ).expect("Write to a String shouldn't fail");
30         for (idx, rr) in verified_rrs.verified_rrs.iter().enumerate() {
31                 write!(&mut resp, "{}{}", if idx != 0 { ", " } else { "" }, rr.json())
32                         .expect("Write to a String shouldn't fail");
33         }
34         resp += "]}";
35         Ok(resp)
36 }