Files
XHChat/crypto/src/wasm.rs
T

62 lines
1.6 KiB
Rust

use crate::engine::Engine;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct DeviceMls(Engine);
#[wasm_bindgen]
impl DeviceMls {
#[wasm_bindgen(constructor)]
pub fn new(device: &str) -> Result<DeviceMls, JsError> {
Ok(Self(Engine::new(device).map_err(JsError::new)?))
}
pub fn restore(state: &[u8]) -> Result<DeviceMls, JsError> {
Ok(Self(Engine::restore(state).map_err(JsError::new)?))
}
pub fn snapshot(&self) -> Result<Vec<u8>, JsError> {
self.0.snapshot().map_err(JsError::new)
}
pub fn device(&self) -> String {
self.0.device().to_owned()
}
pub fn public_key(&self) -> Vec<u8> {
self.0.public_key().to_vec()
}
pub fn key_package(&self) -> Result<Vec<u8>, JsError> {
self.0.key_package().map_err(JsError::new)
}
pub fn start(
&self,
id: &[u8],
peer: &str,
key: &[u8],
wire: &[u8],
) -> Result<Vec<u8>, JsError> {
self.0.start(id, peer, key, wire).map_err(JsError::new)
}
pub fn join(&self, peer: &str, key: &[u8], wire: &[u8]) -> Result<Vec<u8>, JsError> {
self.0.join(peer, key, wire).map_err(JsError::new)
}
pub fn encrypt(&self, id: &[u8], aad: &[u8], plaintext: &[u8]) -> Result<Vec<u8>, JsError> {
self.0.encrypt(id, aad, plaintext).map_err(JsError::new)
}
pub fn decrypt(
&self,
peer: &str,
key: &[u8],
aad: &[u8],
wire: &[u8],
) -> Result<Vec<u8>, JsError> {
self.0.decrypt(peer, key, aad, wire).map_err(JsError::new)
}
}