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

Skip to main content

#
tlsn_wasm/verifier/
mod.rs

    1//! WASM Verifier bindings.
    2
    3mod config;
    4
    5pub use config::VerifierConfig;
    6
    7use tlsn_sdk_core::{SdkVerifier, VerifierConfig as CoreVerifierConfig};
    8use wasm_bindgen::prelude::*;
    9
    10use crate::{
    11    io::{JsIo, JsIoAdapter},
    12    types::VerifierOutput,
    13};
    14
    15type Result<T> = std::result::Result<T, JsError>;
    16
    17/// Verifier for the TLSNotary protocol.
    18///
    19/// The verifier participates in the MPC-TLS protocol with the prover,
    20/// verifying the authenticity of the TLS session without seeing the
    21/// full plaintext.
    22#[wasm_bindgen(js_name = Verifier)]
    23pub struct JsVerifier {
    24    inner: SdkVerifier,
    25}
    26
    27#[wasm_bindgen(js_class = Verifier)]
    28impl JsVerifier {
    29    /// Creates a new Verifier with the given configuration.
    30    #[wasm_bindgen(constructor)]
    31    pub fn new(config: VerifierConfig) -> JsVerifier {
    32        let core_config = convert_verifier_config(config);
    33        let inner = SdkVerifier::new(core_config);
    34        JsVerifier { inner }
    35    }
    36
    37    /// Connects to the prover.
    38    ///
    39    /// # Arguments
    40    ///
    41    /// * `prover_io` - A JavaScript object implementing the IoChannel
    42    ///   interface, connected to the prover.
    43    pub async fn connect(&mut self, prover_io: JsIo) -> Result<()> {
    44        let adapter = JsIoAdapter::new(prover_io);
    45        self.inner
    46            .connect(adapter)
    47            .await
    48            .map_err(|e| JsError::new(&e.to_string()))
    49    }
    50
    51    /// Verifies the connection and finalizes the protocol.
    52    pub async fn verify(&mut self) -> Result<VerifierOutput> {
    53        let core_output = self
    54            .inner
    55            .verify()
    56            .await
    57            .map_err(|e| JsError::new(&e.to_string()))?;
    58        Ok(convert_verifier_output(core_output))
    59    }
    60}
    61
    62// Conversion functions between WASM types and sdk-core types.
    63
    64fn convert_verifier_config(config: VerifierConfig) -> CoreVerifierConfig {
    65    let mut builder = CoreVerifierConfig::builder()
    66        .max_sent_data(config.max_sent_data)
    67        .max_recv_data(config.max_recv_data);
    68
    69    if let Some(value) = config.max_sent_records {
    70        builder = builder.max_sent_records(value);
    71    }
    72
    73    if let Some(value) = config.max_recv_records_online {
    74        builder = builder.max_recv_records_online(value);
    75    }
    76
    77    builder.build()
    78}
    79
    80fn convert_verifier_output(output: tlsn_sdk_core::VerifierOutput) -> VerifierOutput {
    81    VerifierOutput {
    82        server_name: output.server_name,
    83        connection_info: crate::types::ConnectionInfo {
    84            time: output.connection_info.time,
    85            version: match output.connection_info.version {
    86                tlsn_sdk_core::TlsVersion::V1_2 => crate::types::TlsVersion::V1_2,
    87                tlsn_sdk_core::TlsVersion::V1_3 => crate::types::TlsVersion::V1_3,
    88            },
    89            transcript_length: crate::types::TranscriptLength {
    90                sent: output.connection_info.transcript_length.sent,
    91                recv: output.connection_info.transcript_length.recv,
    92            },
    93        },
    94        transcript: output.transcript.map(|t| crate::types::PartialTranscript {
    95            sent: t.sent,
    96            sent_authed: t.sent_authed,
    97            recv: t.recv,
    98            recv_authed: t.recv_authed,
    99        }),
    100    }
    101}