Add WASM validation library
[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 #[global_allocator]
9 static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
10
11 #[wasm_bindgen]
12 /// Verifies an RFC 9102-formatted proof and returns the [`VerifiedRRStream`] in JSON form.
13 pub fn verify_byte_stream(stream: Vec<u8>) -> String {
14         match do_verify_byte_stream(stream) {
15                 Ok(r) => r,
16                 Err(e) => format!("{{\"error\":\"{:?}\"}}", e),
17         }
18 }
19
20 fn do_verify_byte_stream(stream: Vec<u8>) -> Result<String, ValidationError> {
21         let rrs = parse_rr_stream(&stream).map_err(|()| ValidationError::Invalid)?;
22         let verified_rrs = verify_rr_stream(&rrs)?;
23         let mut resp = String::new();
24         resp += &format!("{{\"valid_from\": {}, \"expires\": {}, \"max_cache_ttl\": {}, \"verified_rrs\": [",
25                 verified_rrs.valid_from, verified_rrs.expires, verified_rrs.max_cache_ttl);
26         for (idx, rr) in verified_rrs.verified_rrs.iter().enumerate() {
27                 resp += &format!("{}\"{:?}\"", if idx != 0 { ", " } else { "" }, rr);
28         }
29         resp += "]}";
30         Ok(resp)
31 }