]> git.bitcoin.ninja Git - dnssec-prover/commitdiff
Add WASM validation library
authorMatt Corallo <git@bluematt.me>
Thu, 8 Feb 2024 06:18:38 +0000 (06:18 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 8 Feb 2024 06:18:59 +0000 (06:18 +0000)
wasmpack/Cargo.toml [new file with mode: 0644]
wasmpack/src/lib.rs [new file with mode: 0644]

diff --git a/wasmpack/Cargo.toml b/wasmpack/Cargo.toml
new file mode 100644 (file)
index 0000000..780a4dc
--- /dev/null
@@ -0,0 +1,17 @@
+[package]
+name = "dnssec-prover-wasm"
+version = "0.1.0"
+authors = ["Matt Corallo"]
+license = "MIT OR Apache-2.0"
+repository = "https://git.bitcoin.ninja/index.cgi?p=dnssec-prover"
+description = "A simple crate which allows for the creation and validation of transferrable proofs of entries in the DNS."
+edition = "2021"
+
+[dependencies]
+dnssec-prover = { path = "../", default-features = false, features = ["validation"] }
+wasm-bindgen = { version = "0.2", default-features = false }
+getrandom = { version = "0.2", default-features = false, features = ["js"] }
+wee_alloc = { version = "0.4", default-features = false }
+
+[lib]
+crate-type = ["cdylib", "rlib"]
diff --git a/wasmpack/src/lib.rs b/wasmpack/src/lib.rs
new file mode 100644 (file)
index 0000000..976df50
--- /dev/null
@@ -0,0 +1,31 @@
+//! WASM-compatible verification wrappers
+
+use dnssec_prover::ser::parse_rr_stream;
+use dnssec_prover::validation::{verify_rr_stream, ValidationError};
+
+use wasm_bindgen::prelude::wasm_bindgen;
+
+#[global_allocator]
+static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
+
+#[wasm_bindgen]
+/// Verifies an RFC 9102-formatted proof and returns the [`VerifiedRRStream`] in JSON form.
+pub fn verify_byte_stream(stream: Vec<u8>) -> String {
+       match do_verify_byte_stream(stream) {
+               Ok(r) => r,
+               Err(e) => format!("{{\"error\":\"{:?}\"}}", e),
+       }
+}
+
+fn do_verify_byte_stream(stream: Vec<u8>) -> Result<String, ValidationError> {
+       let rrs = parse_rr_stream(&stream).map_err(|()| ValidationError::Invalid)?;
+       let verified_rrs = verify_rr_stream(&rrs)?;
+       let mut resp = String::new();
+       resp += &format!("{{\"valid_from\": {}, \"expires\": {}, \"max_cache_ttl\": {}, \"verified_rrs\": [",
+               verified_rrs.valid_from, verified_rrs.expires, verified_rrs.max_cache_ttl);
+       for (idx, rr) in verified_rrs.verified_rrs.iter().enumerate() {
+               resp += &format!("{}\"{:?}\"", if idx != 0 { ", " } else { "" }, rr);
+       }
+       resp += "]}";
+       Ok(resp)
+}