feat(crypto) Implement UserId, DeviceId and OlmMachine in Wasm for JS.

This commit is contained in:
Ivan Enderlin
2022-05-09 17:22:16 +02:00
parent 0fe0910fea
commit 3318789a8b
6 changed files with 89 additions and 2 deletions

View File

@@ -54,13 +54,15 @@ tracing = "0.1.34"
zeroize = { version = "1.3.0", features = ["zeroize_derive"] }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ruma = { version = "0.6.1", features = ["client-api-c", "rand", "unstable-msc2676", "unstable-msc2677"] }
ruma = { version = "0.6.2", features = ["client-api-c", "rand", "unstable-msc2676", "unstable-msc2677"] }
vodozemac = { git = "https://github.com/matrix-org/vodozemac", rev = "e09c93f2c8df9770793abeec57ed984d5e1f3834" }
[target.'cfg(target_arch = "wasm32")'.dependencies]
ruma = { version = "0.6.1", features = ["client-api-c", "js", "rand", "unstable-msc2676", "unstable-msc2677"] }
ruma = { version = "0.6.2", features = ["client-api-c", "js", "rand", "unstable-msc2676", "unstable-msc2677"] }
vodozemac = { git = "https://github.com/matrix-org/vodozemac", rev = "e09c93f2c8df9770793abeec57ed984d5e1f3834", features = ["js"] }
wasm-bindgen = "0.2.80"
wasm-bindgen-futures = "0.4.30"
js-sys = "0.3.49"
[dev-dependencies]
futures = { version = "0.3.21", default-features = false, features = ["executor"] }

View File

@@ -0,0 +1,40 @@
//! Additional API that can be useful from JavaScript.
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct UserId {
pub(crate) inner: ruma::OwnedUserId,
}
#[wasm_bindgen]
impl UserId {
#[wasm_bindgen(constructor)]
pub fn new(id: &str) -> Result<UserId, String> {
Ok(Self { inner: ruma::UserId::parse(id).map_err(|e| e.to_string())? })
}
pub fn localpart(&self) -> String {
self.inner.localpart().to_owned()
}
#[wasm_bindgen(getter, js_name = "isHistorical")]
pub fn is_historical(&self) -> bool {
self.inner.is_historical()
}
}
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct DeviceId {
pub(crate) inner: ruma::OwnedDeviceId,
}
#[wasm_bindgen]
impl DeviceId {
#[wasm_bindgen(constructor)]
pub fn new(id: &str) -> DeviceId {
Self { inner: id.into() }
}
}

View File

@@ -26,6 +26,7 @@ mod error;
mod file_encryption;
mod gossiping;
mod identities;
mod js;
mod machine;
pub mod olm;
mod requests;
@@ -82,6 +83,8 @@ pub use identities::{
Device, LocalTrust, MasterPubkey, OwnUserIdentity, ReadOnlyDevice, ReadOnlyOwnUserIdentity,
ReadOnlyUserIdentities, ReadOnlyUserIdentity, UserDevices, UserIdentities, UserIdentity,
};
#[cfg(feature = "js")]
pub use js::*;
pub use machine::OlmMachine;
#[cfg(feature = "qrcode")]
pub use matrix_qrcode;

View File

@@ -47,10 +47,14 @@ use ruma::{
};
use serde_json::Value;
use tracing::{debug, error, info, trace, warn};
#[cfg(feature = "js")]
use wasm_bindgen::prelude::*;
use zeroize::Zeroize;
#[cfg(feature = "backups_v1")]
use crate::backups::BackupMachine;
#[cfg(feature = "js")]
use crate::js;
use crate::{
error::{EventError, MegolmError, MegolmResult, OlmError, OlmResult},
gossiping::GossipMachine,
@@ -72,6 +76,7 @@ use crate::{
/// State machine implementation of the Olm/Megolm encryption protocol used for
/// Matrix end to end encryption.
#[cfg_attr(feature = "js", wasm_bindgen)]
#[derive(Clone)]
pub struct OlmMachine {
/// The unique user id that owns this account.
@@ -1514,6 +1519,27 @@ impl OlmMachine {
&self.backup_machine
}
}
#[cfg_attr(feature = "js", wasm_bindgen)]
impl OlmMachine {
#[wasm_bindgen(constructor)]
pub async fn js_new(user_id: js::UserId, device_id: js::DeviceId) -> js_sys::Promise {
js_sys::Promise::new(&mut |resolve, reject| {
let user_id = user_id.inner.clone();
let device_id = device_id.inner.clone();
wasm_bindgen_futures::spawn_local(async move {
let result = Self::new(user_id.as_ref(), device_id.as_ref()).await;
resolve
.call1(&wasm_bindgen::JsValue::UNDEFINED, &wasm_bindgen::JsValue::from(result))
.unwrap_throw();
})
})
}
//pub async fn new(user_id: &UserId, device_id: &DeviceId) -> Self {
}
#[cfg(any(feature = "testing", test))]
pub(crate) mod testing {
#![allow(dead_code)]

2
examples/js/Makefile Normal file
View File

@@ -0,0 +1,2 @@
build:
wasm-pack build --target nodejs ../../crates/matrix-sdk-crypto --features js

14
examples/js/index.js Normal file
View File

@@ -0,0 +1,14 @@
const { UserId, DeviceId, OlmMachine } = require('../../crates/matrix-sdk-crypto/pkg');
async function run_example() {
const user_id = new UserId('@alice:example.org');
console.log(user_id);
const device_id = new DeviceId("DEVICE_ID");
console.log(device_id);
const olm_machine = await new OlmMachine(user_id, device_id);
console.log(olm_machine);
}
run_example();