Skip to main content

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_next::Tsify;
8
9/// HTTP request body.
10#[derive(Debug, Tsify, Deserialize)]
11#[tsify(from_wasm_abi)]
12#[serde(untagged)]
13#[non_exhaustive]
14pub enum Body {
15    /// JSON body.
16    Json(JsonValue),
17}
18
19/// HTTP method.
20#[derive(Debug, Tsify, Deserialize)]
21#[tsify(from_wasm_abi)]
22pub enum Method {
23    /// HTTP GET method.
24    GET,
25    /// HTTP POST method.
26    POST,
27    /// HTTP PUT method.
28    PUT,
29    /// HTTP DELETE method.
30    DELETE,
31}
32
33/// HTTP request.
34#[derive(Debug, Tsify, Deserialize)]
35#[tsify(from_wasm_abi)]
36pub struct HttpRequest {
37    /// Request URI.
38    pub uri: String,
39    /// HTTP method.
40    pub method: Method,
41    /// Request headers.
42    pub headers: HashMap<String, Vec<u8>>,
43    /// Optional request body.
44    pub body: Option<Body>,
45}
46
47/// HTTP response.
48#[derive(Debug, Tsify, Serialize)]
49#[tsify(into_wasm_abi)]
50pub struct HttpResponse {
51    /// HTTP status code.
52    pub status: u16,
53    /// Response headers.
54    pub headers: Vec<(String, Vec<u8>)>,
55}
56
57/// TLS version.
58#[derive(Debug, Tsify, Serialize)]
59#[tsify(into_wasm_abi)]
60pub enum TlsVersion {
61    /// TLS 1.2.
62    V1_2,
63    /// TLS 1.3.
64    V1_3,
65}
66
67/// Transcript length information.
68#[derive(Debug, Tsify, Serialize)]
69#[tsify(into_wasm_abi)]
70pub struct TranscriptLength {
71    /// Bytes sent.
72    pub sent: usize,
73    /// Bytes received.
74    pub recv: usize,
75}
76
77/// Connection information.
78#[derive(Debug, Tsify, Serialize)]
79#[tsify(into_wasm_abi)]
80pub struct ConnectionInfo {
81    /// Unix timestamp of the connection.
82    pub time: u64,
83    /// TLS version used.
84    pub version: TlsVersion,
85    /// Transcript length information.
86    pub transcript_length: TranscriptLength,
87}
88
89/// Full transcript of sent and received data.
90#[derive(Debug, Tsify, Serialize)]
91#[tsify(into_wasm_abi)]
92pub struct Transcript {
93    /// Data sent to the server.
94    pub sent: Vec<u8>,
95    /// Data received from the server.
96    pub recv: Vec<u8>,
97}
98
99/// Partial transcript with authenticated ranges.
100#[derive(Debug, Tsify, Serialize)]
101#[tsify(into_wasm_abi)]
102pub struct PartialTranscript {
103    /// Data sent to the server.
104    pub sent: Vec<u8>,
105    /// Authenticated ranges of sent data.
106    pub sent_authed: Vec<Range<usize>>,
107    /// Data received from the server.
108    pub recv: Vec<u8>,
109    /// Authenticated ranges of received data.
110    pub recv_authed: Vec<Range<usize>>,
111}
112
113/// Hash algorithm for hash-commitment actions.
114#[derive(Debug, Clone, Copy, Tsify, Deserialize)]
115#[tsify(from_wasm_abi)]
116pub enum HashAlgorithm {
117    /// BLAKE3 hash algorithm.
118    BLAKE3,
119    /// SHA-256 hash algorithm.
120    SHA256,
121    /// Keccak-256 hash algorithm.
122    KECCAK256,
123}
124
125/// A byte range paired with a hash algorithm for commitment.
126///
127/// Uses explicit `start`/`end` fields (rather than `Range<usize>`) for
128/// clean JS/TS interop via tsify. Converted to the sdk-core `CommitRange`
129/// (which uses `Range<usize>`) in [`super::prover::convert_commit_range`].
130#[derive(Debug, Tsify, Deserialize)]
131#[tsify(from_wasm_abi)]
132pub struct CommitRange {
133    /// Start of the byte range (inclusive).
134    pub start: usize,
135    /// End of the byte range (exclusive).
136    pub end: usize,
137    /// Hash algorithm to use for this range.
138    pub algorithm: HashAlgorithm,
139}
140
141/// Ranges of data to hash-commit.
142#[derive(Debug, Tsify, Deserialize)]
143#[tsify(from_wasm_abi)]
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)]
153#[tsify(from_wasm_abi)]
154pub struct Reveal {
155    /// Ranges of sent data to reveal.
156    pub sent: Vec<Range<usize>>,
157    /// Ranges of received data to reveal.
158    pub recv: Vec<Range<usize>>,
159    /// Whether to reveal the server identity.
160    pub server_identity: bool,
161}
162
163/// Output from the verifier.
164#[derive(Debug, Tsify, Serialize)]
165#[tsify(into_wasm_abi)]
166pub struct VerifierOutput {
167    /// Server name (if revealed).
168    pub server_name: Option<String>,
169    /// Connection information.
170    pub connection_info: ConnectionInfo,
171    /// Partial transcript (if revealed).
172    pub transcript: Option<PartialTranscript>,
173}
174
175/// Network setting for protocol optimization.
176#[derive(Debug, Clone, Copy, Tsify, Deserialize)]
177#[tsify(from_wasm_abi)]
178pub enum NetworkSetting {
179    /// Prefers a bandwidth-heavy protocol.
180    Bandwidth,
181    /// Prefers a latency-heavy protocol.
182    Latency,
183}