1use std::{collections::HashMap, ops::Range};
2
3use http_body_util::Full;
4use hyper::body::Bytes;
5use serde::{Deserialize, Serialize};
6use serde_json::Value as JsonValue;
7use tsify_next::Tsify;
8use wasm_bindgen::prelude::*;
9
10#[derive(Debug, Tsify, Deserialize)]
11#[tsify(from_wasm_abi)]
12#[serde(untagged)]
13#[non_exhaustive]
14pub enum Body {
15 Json(JsonValue),
16}
17
18#[derive(Debug, Tsify, Deserialize)]
19#[tsify(from_wasm_abi)]
20pub enum Method {
21 GET,
22 POST,
23 PUT,
24 DELETE,
25}
26
27impl From<Method> for hyper::Method {
28 fn from(value: Method) -> Self {
29 match value {
30 Method::GET => hyper::Method::GET,
31 Method::POST => hyper::Method::POST,
32 Method::PUT => hyper::Method::PUT,
33 Method::DELETE => hyper::Method::DELETE,
34 }
35 }
36}
37
38#[derive(Debug, Tsify, Deserialize)]
39#[tsify(from_wasm_abi)]
40pub struct HttpRequest {
41 pub uri: String,
42 pub method: Method,
43 pub headers: HashMap<String, Vec<u8>>,
44 pub body: Option<Body>,
45}
46
47impl TryFrom<HttpRequest> for hyper::Request<Full<Bytes>> {
48 type Error = JsError;
49
50 fn try_from(value: HttpRequest) -> Result<Self, Self::Error> {
51 let mut builder = hyper::Request::builder();
52 builder = builder.uri(value.uri).method(value.method);
53 for (name, value) in value.headers {
54 builder = builder.header(name, value);
55 }
56
57 if let Some(body) = value.body {
58 let body = match body {
59 Body::Json(value) => Full::new(Bytes::from(serde_json::to_vec(&value).unwrap())),
60 };
61 builder.body(body).map_err(Into::into)
62 } else {
63 builder.body(Full::new(Bytes::new())).map_err(Into::into)
64 }
65 }
66}
67
68#[derive(Debug, Tsify, Serialize)]
69#[tsify(into_wasm_abi)]
70pub struct HttpResponse {
71 pub status: u16,
72 pub headers: Vec<(String, Vec<u8>)>,
73}
74
75#[derive(Debug, Tsify, Serialize)]
76#[tsify(into_wasm_abi)]
77pub enum TlsVersion {
78 V1_2,
79 V1_3,
80}
81
82impl From<tlsn::connection::TlsVersion> for TlsVersion {
83 fn from(value: tlsn::connection::TlsVersion) -> Self {
84 match value {
85 tlsn::connection::TlsVersion::V1_2 => Self::V1_2,
86 tlsn::connection::TlsVersion::V1_3 => Self::V1_3,
87 }
88 }
89}
90
91#[derive(Debug, Tsify, Serialize)]
92#[tsify(into_wasm_abi)]
93pub struct TranscriptLength {
94 pub sent: usize,
95 pub recv: usize,
96}
97
98impl From<tlsn::connection::TranscriptLength> for TranscriptLength {
99 fn from(value: tlsn::connection::TranscriptLength) -> Self {
100 Self {
101 sent: value.sent as usize,
102 recv: value.received as usize,
103 }
104 }
105}
106
107#[derive(Debug, Tsify, Serialize)]
108#[tsify(into_wasm_abi)]
109pub struct ConnectionInfo {
110 time: u64,
111 version: TlsVersion,
112 transcript_length: TranscriptLength,
113}
114
115impl From<tlsn::connection::ConnectionInfo> for ConnectionInfo {
116 fn from(value: tlsn::connection::ConnectionInfo) -> Self {
117 Self {
118 time: value.time,
119 version: value.version.into(),
120 transcript_length: value.transcript_length.into(),
121 }
122 }
123}
124
125#[derive(Debug, Tsify, Serialize)]
126#[tsify(into_wasm_abi)]
127pub struct Transcript {
128 pub sent: Vec<u8>,
129 pub recv: Vec<u8>,
130}
131
132impl From<&tlsn::transcript::Transcript> for Transcript {
133 fn from(value: &tlsn::transcript::Transcript) -> Self {
134 Self {
135 sent: value.sent().to_vec(),
136 recv: value.received().to_vec(),
137 }
138 }
139}
140
141#[derive(Debug, Tsify, Serialize)]
142#[tsify(into_wasm_abi)]
143pub struct PartialTranscript {
144 pub sent: Vec<u8>,
145 pub sent_authed: Vec<Range<usize>>,
146 pub recv: Vec<u8>,
147 pub recv_authed: Vec<Range<usize>>,
148}
149
150impl From<tlsn::transcript::PartialTranscript> for PartialTranscript {
151 fn from(value: tlsn::transcript::PartialTranscript) -> Self {
152 Self {
153 sent: value.sent_unsafe().to_vec(),
154 sent_authed: value.sent_authed().iter_ranges().collect(),
155 recv: value.received_unsafe().to_vec(),
156 recv_authed: value.received_authed().iter_ranges().collect(),
157 }
158 }
159}
160
161#[derive(Debug, Tsify, Deserialize)]
162#[tsify(from_wasm_abi)]
163pub struct Commit {
164 pub sent: Vec<Range<usize>>,
165 pub recv: Vec<Range<usize>>,
166}
167
168#[derive(Debug, Tsify, Deserialize)]
169#[tsify(from_wasm_abi)]
170pub struct Reveal {
171 pub sent: Vec<Range<usize>>,
172 pub recv: Vec<Range<usize>>,
173 pub server_identity: bool,
174}
175
176#[derive(Debug, Tsify, Serialize)]
177#[tsify(into_wasm_abi)]
178pub struct VerifierOutput {
179 pub server_name: Option<String>,
180 pub connection_info: ConnectionInfo,
181 pub transcript: Option<PartialTranscript>,
182}
183
184#[derive(Debug, Tsify, Deserialize)]
185#[tsify(from_wasm_abi)]
186pub enum NetworkSetting {
187 Bandwidth,
189 Latency,
191}
192
193impl From<NetworkSetting> for tlsn::config::NetworkSetting {
194 fn from(value: NetworkSetting) -> Self {
195 match value {
196 NetworkSetting::Bandwidth => Self::Bandwidth,
197 NetworkSetting::Latency => Self::Latency,
198 }
199 }
200}