# handler.rs - source
Source of the Rust file `crates/wasm/src/handler.rs`.

Skip to main content

#
tlsn_wasm/
handler.rs

    1//! WASM bindings for handler-based range extraction.
    2//!
    3//! Exposes [`compute_reveal`] to JavaScript, which parses HTTP transcripts
    4//! and maps plugin handlers to byte ranges for selective disclosure.
    5
    6use serde::Serialize;
    7use wasm_bindgen::prelude::*;
    8
    9/// Parses HTTP request/response transcripts and maps handlers to byte ranges.
    10///
    11/// This is the WASM wrapper around `tlsn_sdk_core::compute_reveal`.
    12///
    13/// # Arguments
    14///
    15/// * `sent` - Raw bytes of the HTTP request (sent data).
    16/// * `recv` - Raw bytes of the HTTP response (received data).
    17/// * `handlers` - Array of handler objects (deserialized from JS).
    18///
    19/// # Returns
    20///
    21/// A `ComputeRevealOutput` object containing:
    22/// - `sentRanges` / `recvRanges`: byte ranges for `Prover.reveal()`
    23/// - `sentRangesWithHandlers` / `recvRangesWithHandlers`: ranges annotated with
    24///   handlers
    25/// - `commit` (optional): ranges to hash-commit, with per-range algorithm
    26#[wasm_bindgen(js_name = compute_reveal)]
    27pub fn compute_reveal(sent: &[u8], recv: &[u8], handlers: JsValue) -> Result<JsValue, JsError> {
    28    let handlers: Vec<tlsn_sdk_core::Handler> = serde_wasm_bindgen::from_value(handlers)
    29        .map_err(|e| JsError::new(&format!("failed to deserialize handlers: {e}")))?;
    30
    31    let output = tlsn_sdk_core::compute_reveal(sent, recv, &handlers)
    32        .map_err(|e| JsError::new(&e.to_string()))?;
    33
    34    // Use the JSON-compatible serializer so Handler produces plain JS Objects
    35    // instead of `Map` instances. `JSON.stringify(Map)` returns "{}", so a
    36    // non-Object handler would silently lose all its fields when the
    37    // extension forwards the reveal_config to the verifier over WebSocket.
    38    let serializer = serde_wasm_bindgen::Serializer::json_compatible();
    39    output
    40        .serialize(&serializer)
    41        .map_err(|e| JsError::new(&e.to_string()))
    42}