tlsn_wasm/
lib.rs

1//! TLSNotary WASM bindings.
2
3#![cfg(target_arch = "wasm32")]
4#![deny(unreachable_pub, unused_must_use, clippy::all)]
5#![allow(non_snake_case)]
6
7pub(crate) mod io;
8mod log;
9pub mod prover;
10#[cfg(feature = "test")]
11pub mod tests;
12pub mod types;
13pub mod verifier;
14
15pub use log::{LoggingConfig, LoggingLevel};
16
17use tlsn_core::{transcript::Direction, CryptoProvider};
18use wasm_bindgen::prelude::*;
19use wasm_bindgen_futures::JsFuture;
20
21use crate::types::{Attestation, Presentation, Reveal, Secrets};
22
23#[cfg(feature = "test")]
24pub use tests::*;
25
26/// Initializes the module.
27#[wasm_bindgen]
28pub async fn initialize(
29    logging_config: Option<LoggingConfig>,
30    thread_count: usize,
31) -> Result<(), JsValue> {
32    log::init_logging(logging_config);
33
34    JsFuture::from(web_spawn::start_spawner()).await?;
35
36    // Initialize rayon global thread pool.
37    rayon::ThreadPoolBuilder::new()
38        .num_threads(thread_count)
39        .spawn_handler(|thread| {
40            // Drop join handle.
41            let _ = web_spawn::spawn(move || thread.run());
42            Ok(())
43        })
44        .build_global()
45        .unwrap_throw();
46
47    Ok(())
48}
49
50/// Builds a presentation.
51#[wasm_bindgen]
52pub fn build_presentation(
53    attestation: &Attestation,
54    secrets: &Secrets,
55    reveal: Reveal,
56) -> Result<Presentation, JsError> {
57    let provider = CryptoProvider::default();
58
59    let mut builder = attestation.0.presentation_builder(&provider);
60
61    builder.identity_proof(secrets.0.identity_proof());
62
63    let mut proof_builder = secrets.0.transcript_proof_builder();
64
65    for range in reveal.sent.iter() {
66        proof_builder.reveal(range, Direction::Sent)?;
67    }
68
69    for range in reveal.recv.iter() {
70        proof_builder.reveal(range, Direction::Received)?;
71    }
72
73    builder.transcript_proof(proof_builder.build()?);
74
75    builder
76        .build()
77        .map(Presentation::from)
78        .map_err(JsError::from)
79}