Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove try_array_to_vec and downcast #82

Merged
merged 14 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@

**BREAKING CHANGES**

- Rename `OlmMachine.init_from_store` introduced in v3.6.0 to `OlmMachine.initFromStore`.
- Rename `OlmMachine.init_from_store` introduced in v3.6.0 to
`OlmMachine.initFromStore`.
([#84](https://github.com/matrix-org/matrix-rust-sdk-crypto-wasm/pull/84))

**BREAKING CHANGES**

richvdh marked this conversation as resolved.
Show resolved Hide resolved
- Functions/methods that take a JavaScript `Array` as argument now invalidate the
items within that array so that they cannot be re-used as soon as
they are received by the functions/methods. See the patch for affected methods.
([#82](https://github.com/matrix-org/matrix-rust-sdk-crypto-wasm/pull/82/))

- Update `wasm-bindgen` to 0.2.89. It allows to remove the `downcast` method.
It fixes [#51](https://github.com/matrix-org/matrix-rust-sdk-crypto-wasm/pull/51),
thus the resulting JavaScript code of `matrix-rust-sdk-crypto-wasm` can
be minified with no issue now.
([#82](https://github.com/matrix-org/matrix-rust-sdk-crypto-wasm/pull/82/))
richvdh marked this conversation as resolved.
Show resolved Hide resolved

# matrix-sdk-crypto-wasm v3.6.0

Expand Down
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ serde_json = "1.0.91"
serde-wasm-bindgen = "0.5.0"
tracing = { version = "0.1.36", default-features = false, features = ["std"] }
tracing-subscriber = { version = "0.3.14", default-features = false, features = ["registry", "std", "ansi"] }
# 0.2.88 causes breakage, per https://github.com/rustwasm/wasm-bindgen/issues/3685
wasm-bindgen = "=0.2.87"
wasm-bindgen = "0.2.89"
wasm-bindgen-futures = "0.4.33"
zeroize = "1.6.0"

Expand Down
14 changes: 8 additions & 6 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use crate::{
encryption::EncryptionAlgorithm,
future::future_to_promise,
identifiers::{self, DeviceId, UserId},
impl_from_to_inner,
js::try_array_to_vec,
requests, types, verification, vodozemac,
impl_from_to_inner, requests, types, verification, vodozemac,
};

/// A device represents a E2EE capable client of an user.
Expand All @@ -25,13 +23,17 @@ impl_from_to_inner!(matrix_sdk_crypto::Device => Device);
impl Device {
/// Request an interactive verification with this device.
///
/// Items inside `methods` will be invalidated by this method.
///
/// Returns a Promise for a 2-element array `[VerificationRequest,
/// ToDeviceRequest]`.
Hywan marked this conversation as resolved.
Show resolved Hide resolved
#[wasm_bindgen(js_name = "requestVerification")]
pub fn request_verification(&self, methods: Option<Array>) -> Result<Promise, JsError> {
let methods =
methods.map(try_array_to_vec::<verification::VerificationMethod, _>).transpose()?;
pub fn request_verification(
&self,
methods: Option<Vec<verification::VerificationMethod>>,
) -> Result<Promise, JsError> {
let me = self.inner.clone();
let methods = methods.map(|methods| methods.iter().map(Into::into).collect());

Ok(future_to_promise(async move {
let tuple = Array::new();
Expand Down
11 changes: 0 additions & 11 deletions src/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,6 @@ pub enum EncryptionAlgorithm {
MegolmV1AesSha2,
}

impl From<EncryptionAlgorithm> for JsValue {
fn from(value: EncryptionAlgorithm) -> Self {
use EncryptionAlgorithm::*;

match value {
OlmV1Curve25519AesSha2 => JsValue::from(0),
MegolmV1AesSha2 => JsValue::from(1),
}
}
}

impl From<EncryptionAlgorithm> for matrix_sdk_crypto::types::EventEncryptionAlgorithm {
fn from(value: EncryptionAlgorithm) -> Self {
use EncryptionAlgorithm::*;
Expand Down
9 changes: 9 additions & 0 deletions src/identifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ impl UserId {
pub fn to_string(&self) -> String {
self.inner.as_str().to_owned()
}

/// Create a clone of this `UserId`.
///
/// This can be useful when passing a `UserId` instance to methods such as
/// {@link OlmMachine.updateTrackedUsers} which destroy the instance.
#[wasm_bindgen(js_name = "clone")]
pub fn clone_me(&self) -> Self {
self.clone()
}
}

/// A Matrix key ID.
Expand Down
32 changes: 19 additions & 13 deletions src/identities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
use js_sys::{Array, Promise};
use wasm_bindgen::prelude::*;

use crate::{
future::future_to_promise, identifiers, impl_from_to_inner, js::try_array_to_vec, requests,
verification,
};
use crate::{future::future_to_promise, identifiers, impl_from_to_inner, requests, verification};

pub(crate) struct UserIdentities {
inner: matrix_sdk_crypto::UserIdentities,
Expand Down Expand Up @@ -59,10 +56,14 @@ impl OwnUserIdentity {
}

/// Send a verification request to our other devices.
Hywan marked this conversation as resolved.
Show resolved Hide resolved
///
/// Items inside `methods` will be invalidated by this method.
#[wasm_bindgen(js_name = "requestVerification")]
pub fn request_verification(&self, methods: Option<Array>) -> Result<Promise, JsError> {
let methods =
methods.map(try_array_to_vec::<verification::VerificationMethod, _>).transpose()?;
pub fn request_verification(
&self,
methods: Option<Vec<verification::VerificationMethod>>,
) -> Result<Promise, JsError> {
let methods = methods.map(|methods| methods.iter().map(Into::into).collect());
let me = self.inner.clone();

Ok(future_to_promise(async move {
Expand Down Expand Up @@ -159,18 +160,19 @@ impl UserIdentity {

/// Create a `VerificationRequest` object after the verification
/// request content has been sent out.
///
/// Items inside `methods` will be invalidated by this method.
#[wasm_bindgen(js_name = "requestVerification")]
pub fn request_verification(
&self,
room_id: &identifiers::RoomId,
request_event_id: &identifiers::EventId,
methods: Option<Array>,
methods: Option<Vec<verification::VerificationMethod>>,
Hywan marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<Promise, JsError> {
let me = self.inner.clone();
let room_id = room_id.inner.clone();
let request_event_id = request_event_id.inner.clone();
let methods =
methods.map(try_array_to_vec::<verification::VerificationMethod, _>).transpose()?;
let methods = methods.map(|methods| methods.iter().map(Into::into).collect());

Ok(future_to_promise::<_, verification::VerificationRequest>(async move {
Ok(me
Expand All @@ -187,11 +189,15 @@ impl UserIdentity {
///
/// After the content has been sent out a VerificationRequest can be started
/// with the `request_verification` method.
///
/// Items inside `methods` will be invalidated by this method.
#[wasm_bindgen(js_name = "verificationRequestContent")]
pub fn verification_request_content(&self, methods: Option<Array>) -> Result<Promise, JsError> {
pub fn verification_request_content(
&self,
methods: Option<Vec<verification::VerificationMethod>>,
Hywan marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<Promise, JsError> {
let me = self.inner.clone();
let methods =
methods.map(try_array_to_vec::<verification::VerificationMethod, _>).transpose()?;
let methods = methods.map(|methods| methods.iter().map(Into::into).collect());

Ok(future_to_promise(async move {
Ok(serde_json::to_string(&me.verification_request_content(methods).await)?)
Expand Down
40 changes: 0 additions & 40 deletions src/js.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub mod events;
mod future;
pub mod identifiers;
pub mod identities;
mod js;
pub mod libolm_migration;
pub mod machine;
mod macros;
Expand Down
27 changes: 13 additions & 14 deletions src/libolm_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use std::{iter, time::Duration};

use anyhow::Context;
use js_sys::{Array, Date, Uint8Array};
use js_sys::{Date, Uint8Array};
use matrix_sdk_common::ruma::{
DeviceKeyAlgorithm, MilliSecondsSinceUnixEpoch, SecondsSinceUnixEpoch, UInt,
};
Expand All @@ -33,7 +33,6 @@ use wasm_bindgen::prelude::*;

use crate::{
identifiers::{DeviceId, RoomId, UserId},
js::downcast,
store::StoreHandle,
};

Expand Down Expand Up @@ -223,22 +222,23 @@ impl Migration {
///
/// # Arguments
///
/// * `sessions` - An `Array` of {@link PickledSession}s to import.
/// * `sessions` - An `Array` of {@link PickledSession}s to import. Items
/// inside `sessions` are going to be invalidated by this method.
/// * `pickle_key` - The libolm pickle key that was used to pickle the olm
/// session objects.
/// * `store_handle` - A connection to the CryptoStore which will be used to
/// store the vodozemac data.
#[wasm_bindgen(js_name = "migrateOlmSessions")]
pub async fn migrate_olm_sessions(
sessions: Array,
sessions: Vec<PickledSession>,
pickle_key: Uint8Array,
store_handle: &StoreHandle,
) -> Result<JsValue, JsError> {
let pickle_key = pickle_key.to_vec();

let rust_sessions = sessions
.into_iter()
.map(|s| libolm_pickled_session_to_rust_pickled_session(s, &pickle_key))
.map(|session| libolm_pickled_session_to_rust_pickled_session(session, &pickle_key))
.collect::<Result<_>>()?;

import_olm_sessions_to_store(rust_sessions, store_handle.store.as_ref())
Expand All @@ -261,10 +261,9 @@ impl Default for PickledSession {
}

fn libolm_pickled_session_to_rust_pickled_session(
libolm_session: JsValue,
libolm_session: PickledSession,
pickle_key: &[u8],
) -> Result<matrix_sdk_crypto::olm::PickledSession> {
let libolm_session = downcast::<PickledSession>(&libolm_session, "PickledSession")?;
let session = vodozemac::olm::Session::from_libolm_pickle(&libolm_session.pickle, &pickle_key)?;

let creation_time = date_to_seconds_since_epoch(&libolm_session.creation_time)
Expand Down Expand Up @@ -369,22 +368,25 @@ impl Migration {
/// # Arguments
///
/// * `sessions` - An `Array` of {@link PickledInboundGroupSession}s to
/// import.
/// import. Items inside `sessions` are going to be invalidated by this
/// method.
/// * `pickle_key` - The libolm pickle key that was used to pickle the
/// megolm session objects.
/// * `store_handle` - A connection to the CryptoStore which will be used to
/// store the vodozemac data.
#[wasm_bindgen(js_name = "migrateMegolmSessions")]
pub async fn migrate_megolm_sessions(
sessions: Array,
sessions: Vec<PickledInboundGroupSession>,
pickle_key: Uint8Array,
store_handle: &StoreHandle,
) -> Result<JsValue, JsError> {
let pickle_key = pickle_key.to_vec();

let rust_sessions = sessions
.into_iter()
.map(|s| libolm_pickled_megolm_session_to_rust_pickled_session(s, &pickle_key))
.map(|session| {
libolm_pickled_megolm_session_to_rust_pickled_session(session, &pickle_key)
})
.collect::<Result<_>>()?;

import_megolm_sessions_to_store(rust_sessions, store_handle.store.as_ref())
Expand All @@ -395,12 +397,9 @@ impl Migration {
}

fn libolm_pickled_megolm_session_to_rust_pickled_session(
libolm_session: JsValue,
libolm_session: PickledInboundGroupSession,
pickle_key: &[u8],
) -> Result<matrix_sdk_crypto::olm::PickledInboundGroupSession> {
let libolm_session =
downcast::<PickledInboundGroupSession>(&libolm_session, "PickledInboundGroupSession")?;

let pickle = vodozemac::megolm::InboundGroupSession::from_libolm_pickle(
&libolm_session.pickle,
pickle_key,
Expand Down
Loading