tlsn_wasm/types.rs
1//! WASM type definitions with TypeScript bindings.
2
3use std::{collections::HashMap, ops::Range};
4
5use serde::{Deserialize, Serialize};
6use serde_json::Value as JsonValue;
7use tsify::Tsify;
8use wasm_bindgen::prelude::*;
9
10/// HTTP request body.
11#[derive(Debug, Tsify, Deserialize, Serialize)]
12#[serde(untagged)]
13#[non_exhaustive]
14pub enum Body {
15 /// JSON body.
16 Json(JsonValue),
17}
18
19/// TypeScript shape of the JSON value used by [`Body`].
20///
21/// tsify emits `Body = JsonValue` for the `Json` variant but does not declare
22/// the alias, so the generated `.d.ts` refers to an undefined name that
23/// `skipLibCheck` would otherwise hide. Supply it here, mirroring the
24/// `IoChannel` declaration in `io.rs`.
25#[wasm_bindgen(typescript_custom_section)]
26const JSON_VALUE: &'static str = r#"
27export type JsonValue = null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue };
28"#;
29
30/// HTTP method.
31#[derive(Debug, Tsify, Deserialize, Serialize)]
32pub enum Method {
33 /// HTTP GET method.
34 GET,
35 /// HTTP POST method.
36 POST,
37 /// HTTP PUT method.
38 PUT,
39 /// HTTP DELETE method.
40 DELETE,
41}
42
43/// HTTP request.
44#[derive(Debug, Tsify, Deserialize, Serialize)]
45pub struct HttpRequest {
46 /// Request URI.
47 pub uri: String,
48 /// HTTP method.
49 pub method: Method,
50 /// Request headers.
51 pub headers: HashMap<String, Vec<u8>>,
52 /// Optional request body.
53 pub body: Option<Body>,
54}
55
56/// HTTP response.
57#[derive(Debug, Tsify, Serialize, Deserialize)]
58pub struct HttpResponse {
59 /// HTTP status code.
60 pub status: u16,
61 /// Response headers.
62 pub headers: Vec<(String, Vec<u8>)>,
63}
64
65/// TLS version.
66#[derive(Debug, Tsify, Serialize)]
67pub enum TlsVersion {
68 /// TLS 1.2.
69 V1_2,
70 /// TLS 1.3.
71 V1_3,
72}
73
74/// Transcript length information.
75#[derive(Debug, Tsify, Serialize)]
76pub struct TranscriptLength {
77 /// Bytes sent.
78 pub sent: usize,
79 /// Bytes received.
80 pub recv: usize,
81}
82
83/// Connection information.
84#[derive(Debug, Tsify, Serialize)]
85pub struct ConnectionInfo {
86 /// Unix timestamp of the connection.
87 pub time: u64,
88 /// TLS version used.
89 pub version: TlsVersion,
90 /// Transcript length information.
91 pub transcript_length: TranscriptLength,
92}
93
94/// Full transcript of sent and received data.
95#[derive(Debug, Tsify, Serialize, Deserialize)]
96pub struct Transcript {
97 /// Data sent to the server.
98 pub sent: Vec<u8>,
99 /// Data received from the server.
100 pub recv: Vec<u8>,
101}
102
103/// Partial transcript with authenticated ranges.
104#[derive(Debug, Tsify, Serialize)]
105pub struct PartialTranscript {
106 /// Data sent to the server.
107 pub sent: Vec<u8>,
108 /// Authenticated ranges of sent data.
109 pub sent_authed: Vec<Range<usize>>,
110 /// Data received from the server.
111 pub recv: Vec<u8>,
112 /// Authenticated ranges of received data.
113 pub recv_authed: Vec<Range<usize>>,
114}
115
116/// Hash algorithm for hash-commitment actions.
117#[derive(Debug, Clone, Copy, Tsify, Deserialize)]
118pub enum HashAlgorithm {
119 /// BLAKE3 hash algorithm.
120 BLAKE3,
121 /// SHA-256 hash algorithm.
122 SHA256,
123 /// Keccak-256 hash algorithm.
124 KECCAK256,
125}
126
127/// A byte range paired with a hash algorithm for commitment.
128///
129/// Uses explicit `start`/`end` fields (rather than `Range<usize>`) for
130/// clean JS/TS interop via tsify. Converted to the sdk-core `CommitRange`
131/// (which uses `Range<usize>`) in [`super::prover::convert_commit_range`].
132#[derive(Debug, Tsify, Deserialize)]
133pub struct CommitRange {
134 /// Start of the byte range (inclusive).
135 pub start: usize,
136 /// End of the byte range (exclusive).
137 pub end: usize,
138 /// Hash algorithm to use for this range.
139 pub algorithm: HashAlgorithm,
140}
141
142/// Ranges of data to hash-commit.
143#[derive(Debug, Tsify, Deserialize)]
144pub struct Commit {
145 /// Ranges of sent data to commit, each with its own algorithm.
146 pub sent: Vec<CommitRange>,
147 /// Ranges of received data to commit, each with its own algorithm.
148 pub recv: Vec<CommitRange>,
149}
150
151/// Ranges of data to reveal.
152#[derive(Debug, Tsify, Deserialize, Serialize)]
153pub struct Reveal {
154 /// Ranges of sent data to reveal.
155 pub sent: Vec<Range<usize>>,
156 /// Ranges of received data to reveal.
157 pub recv: Vec<Range<usize>>,
158 /// Whether to reveal the server identity.
159 pub server_identity: bool,
160}
161
162/// Opening for a single hash-committed range.
163///
164/// Pairs the commitment hash with the blinder used to produce it, so the
165/// caller can later prove `H(plaintext || blinder) == hash` without rerunning
166/// MPC-TLS. Range and algorithm are not repeated — they live on the input
167/// `CommitRange` at the same index.
168#[derive(Debug, Tsify, Serialize)]
169pub struct HashOpening {
170 /// The commitment hash digest.
171 pub hash: Vec<u8>,
172 /// The blinder (16 bytes) used to compute the commitment.
173 pub blinder: Vec<u8>,
174}
175
176/// Output of `Prover.reveal()`.
177///
178/// Mirrors the input `Commit`: `sent[i]` opens `commit.sent[i]`, likewise for
179/// `recv`. Both arrays are empty when no commit was supplied.
180#[derive(Debug, Tsify, Serialize)]
181pub struct RevealOutput {
182 /// Openings for `commit.sent`, in input order.
183 pub sent: Vec<HashOpening>,
184 /// Openings for `commit.recv`, in input order.
185 pub recv: Vec<HashOpening>,
186}
187
188/// Output from the verifier.
189#[derive(Debug, Tsify, Serialize)]
190pub struct VerifierOutput {
191 /// Server name (if revealed).
192 pub server_name: Option<String>,
193 /// Connection information.
194 pub connection_info: ConnectionInfo,
195 /// Partial transcript (if revealed).
196 pub transcript: Option<PartialTranscript>,
197}
198
199/// Network setting for protocol optimization.
200#[derive(Debug, Clone, Copy, Tsify, Deserialize)]
201pub enum NetworkSetting {
202 /// Prefers a bandwidth-heavy protocol.
203 Bandwidth,
204 /// Prefers a latency-heavy protocol.
205 Latency,
206}