xwt_web_utils/
cert.rs

1//! WebTransport Certificate utils.
2
3/// Represents the following object:
4///
5/// ```js
6/// {
7///   "algorithm": "<algorithm>",
8///   "value": ArrayBuffer(<value>)
9/// }
10/// ```
11///
12/// Use `sha-256` `algorithm` for SHA-256.
13#[derive(Debug)]
14pub struct CertHashRef<'a> {
15    pub algorithm: &'a str,
16    pub value: &'a [u8],
17}
18
19impl<'a> From<CertHashRef<'a>> for web_sys::WebTransportHash {
20    fn from(cert_hash: CertHashRef<'a>) -> Self {
21        let mut wt_hash = Self::new();
22
23        wt_hash.algorithm(cert_hash.algorithm);
24        wt_hash.value(&js_sys::Uint8Array::from(cert_hash.value));
25
26        wt_hash
27    }
28}
29
30impl<'a> From<CertHashRef<'a>> for js_sys::Object {
31    fn from(cert_hash: CertHashRef<'a>) -> Self {
32        web_sys::WebTransportHash::from(cert_hash).into()
33    }
34}
35
36impl<'a> From<CertHashRef<'a>> for wasm_bindgen::JsValue {
37    fn from(cert_hash: CertHashRef<'a>) -> Self {
38        web_sys::WebTransportHash::from(cert_hash).into()
39    }
40}
41
42/// Create an [`js_sys::Array`] with parameters suitable for passing to
43/// the [`web_sys::WebTransportOptions::server_certificate_hashes`] and
44/// assign the corresponding value at the passed `options`.
45pub fn assign<'a, Iter>(options: &mut web_sys::WebTransportOptions, iter: Iter)
46where
47    Iter: IntoIterator<Item = CertHashRef<'a>>,
48{
49    let array: js_sys::Array = iter.into_iter().map(wasm_bindgen::JsValue::from).collect();
50    options.server_certificate_hashes(&array);
51}