From f9bd0775d1bdcdf3695cce721ee267ec4cde9d04 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 8 Feb 2024 06:18:38 +0000 Subject: [PATCH] Add WASM validation library --- wasmpack/Cargo.toml | 17 +++++++++++++++++ wasmpack/src/lib.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 wasmpack/Cargo.toml create mode 100644 wasmpack/src/lib.rs diff --git a/wasmpack/Cargo.toml b/wasmpack/Cargo.toml new file mode 100644 index 0000000..780a4dc --- /dev/null +++ b/wasmpack/Cargo.toml @@ -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 index 0000000..976df50 --- /dev/null +++ b/wasmpack/src/lib.rs @@ -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) -> String { + match do_verify_byte_stream(stream) { + Ok(r) => r, + Err(e) => format!("{{\"error\":\"{:?}\"}}", e), + } +} + +fn do_verify_byte_stream(stream: Vec) -> Result { + 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) +} -- 2.30.2