Skip to content

Commit 7931c4a

Browse files
committed
chore(crypto-js): Clean up code and make CI happy.
1 parent 4db1ad3 commit 7931c4a

File tree

14 files changed

+89
-500
lines changed

14 files changed

+89
-500
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
uses: actions-rs/cargo@v1
3939
with:
4040
command: tarpaulin
41-
args: --ignore-config --exclude-files "crates/matrix-sdk/examples/*,crates/matrix-sdk-common,crates/matrix-sdk-test,crates/matrix-sdk-crypto-js,crates/matrix-sdk-crypto-nodejs" --out Xml
41+
args: --workspace --ignore-config --exclude-files "crates/matrix-sdk/examples/*,crates/matrix-sdk-common,crates/matrix-sdk-test" --exclude matrix-sdk-crypto-js --exclude matrix-sdk-crypto-nodejs --out Xml
4242

4343
- name: Upload to codecov.io
4444
uses: codecov/codecov-action@v3

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
RUSTDOCFLAGS: "--enable-index-page -Zunstable-options --cfg docsrs -Dwarnings"
3333
with:
3434
command: doc
35-
args: --no-deps --workspace --features docsrs -Zrustdoc-map
35+
args: --no-deps --workspace --exclude matrix-sdk-crypto-js --exclude matrix-sdk-crypto-nodejs --features docsrs -Zrustdoc-map
3636

3737
- name: Deploy docs
3838
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[build]
2-
target = "wasm32-unknown-unknown"
2+
target = "wasm32-unknown-unknown"

crates/matrix-sdk-crypto-js/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ wasm-bindgen-futures = "0.4.30"
3535
js-sys = "0.3.49"
3636
serde_json = "1.0.79"
3737
http = "0.2.6"
38-
anyhow = "1.0"
38+
anyhow = "1.0"

crates/matrix-sdk-crypto-js/src/identifiers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl UserId {
2323
/// Parse/validate and create a new `UserId`.
2424
#[wasm_bindgen(constructor)]
2525
pub fn new(id: &str) -> Result<UserId, JsError> {
26-
Ok(Self { inner: ruma::UserId::parse(id)? })
26+
Ok(Self::new_with(ruma::UserId::parse(id)?))
2727
}
2828

2929
/// Returns the user's localpart.
@@ -76,7 +76,7 @@ impl DeviceId {
7676
/// Create a new `DeviceId`.
7777
#[wasm_bindgen(constructor)]
7878
pub fn new(id: &str) -> DeviceId {
79-
Self { inner: id.into() }
79+
Self::new_with(id.into())
8080
}
8181

8282
/// Return the device ID as a string.
@@ -107,7 +107,7 @@ impl RoomId {
107107
/// Parse/validate and create a new `RoomId`.
108108
#[wasm_bindgen(constructor)]
109109
pub fn new(id: &str) -> Result<RoomId, JsError> {
110-
Ok(Self { inner: ruma::RoomId::parse(id)? })
110+
Ok(Self::new_with(ruma::RoomId::parse(id)?))
111111
}
112112

113113
/// Returns the user's localpart.

crates/matrix-sdk-crypto-js/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 The Matrix.org Foundation C.I.C.
1+
// Copyright 2022 The Matrix.org Foundation C.I.C.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -16,17 +16,13 @@
1616
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
1717
#![warn(missing_docs, missing_debug_implementations)]
1818

19-
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
20-
compile_error!("This crate is designed to only be compiled to `wasm32-unknown-unknown`.");
21-
2219
pub mod events;
2320
mod future;
2421
pub mod identifiers;
2522
pub mod machine;
2623
pub mod requests;
2724
pub mod responses;
2825
pub mod sync_events;
29-
pub mod verifications;
3026

3127
use js_sys::{Object, Reflect};
3228
use wasm_bindgen::{convert::RefFromWasmAbi, prelude::*};

crates/matrix-sdk-crypto-js/src/machine.rs

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
//! The crypto specific Olm objects.
22
3-
use std::{collections::BTreeMap, sync::Arc, time::Duration};
3+
use std::{collections::BTreeMap, time::Duration};
44

55
use js_sys::{Array, Map, Promise, Set};
6-
use ruma::{
7-
events::{AnyMessageLikeEventContent, EventContent},
8-
DeviceKeyAlgorithm, OwnedTransactionId, UInt,
9-
};
10-
use serde_json::value::RawValue as RawJsonValue;
6+
use ruma::{DeviceKeyAlgorithm, OwnedTransactionId, UInt};
7+
use serde_json::Value as JsonValue;
118
use wasm_bindgen::prelude::*;
129

1310
use crate::{
@@ -16,15 +13,15 @@ use crate::{
1613
identifiers, requests,
1714
requests::OutgoingRequest,
1815
responses::{self, response_from_string},
19-
sync_events, verifications,
16+
sync_events,
2017
};
2118

2219
/// State machine implementation of the Olm/Megolm encryption protocol
2320
/// used for Matrix end to end encryption.
2421
#[wasm_bindgen]
25-
#[derive(Debug)]
22+
#[derive(Debug, Clone)]
2623
pub struct OlmMachine {
27-
inner: Arc<matrix_sdk_crypto::OlmMachine>,
24+
inner: matrix_sdk_crypto::OlmMachine,
2825
}
2926

3027
#[wasm_bindgen]
@@ -45,9 +42,8 @@ impl OlmMachine {
4542

4643
future_to_promise(async move {
4744
Ok(OlmMachine {
48-
inner: Arc::new(
49-
matrix_sdk_crypto::OlmMachine::new(user_id.as_ref(), device_id.as_ref()).await,
50-
),
45+
inner: matrix_sdk_crypto::OlmMachine::new(user_id.as_ref(), device_id.as_ref())
46+
.await,
5147
})
5248
})
5349
}
@@ -119,7 +115,8 @@ impl OlmMachine {
119115
}))
120116
}
121117

122-
/// Handle a to-device and one-time key counts from a sync response.
118+
/// Handle to-device events and one-time key counts from a sync
119+
/// response.
123120
///
124121
/// This will decrypt and handle to-device events returning the
125122
/// decrypted versions of them.
@@ -206,12 +203,14 @@ impl OlmMachine {
206203
/// Mark the request with the given request ID as sent (see
207204
/// `outgoing_requests`).
208205
///
209-
/// `request_id` represents the unique ID of the request that was
210-
/// sent out. This is needed to couple the response with the now
211-
/// sent out request. `response_type` represents the type of the
212-
/// request that was sent out. `response` represents the response
213-
/// that was received from the server after the outgoing request
214-
/// was sent out. `
206+
/// Arguments are:
207+
///
208+
/// * `request_id` represents the unique ID of the request that was sent
209+
/// out. This is needed to couple the response with the now sent out
210+
/// request.
211+
/// * `response_type` represents the type of the request that was sent out.
212+
/// * `response` represents the response that was received from the server
213+
/// after the outgoing request was sent out.
215214
#[wasm_bindgen(js_name = "markRequestAsSent")]
216215
pub fn mark_request_as_sent(
217216
&self,
@@ -235,34 +234,30 @@ impl OlmMachine {
235234
/// Beware that a group session needs to be shared before this
236235
/// method can be called using the `share_group_session` method.
237236
///
238-
/// Since group sessions can expire or become invalid if the room
239-
/// membership changes, client authors should check with the
240-
/// `should_share_group_session` method if a new group session
241-
/// needs to be shared.
242-
///
243237
/// `room_id` is the ID of the room for which the message should
244238
/// be encrypted. `event_type` is the type of the event. `content`
245239
/// is the plaintext content of the message that should be
246240
/// encrypted.
247241
///
248242
/// # Panics
249243
///
250-
/// Panics if a group session for the given room wasn't shared beforehand.
244+
/// Panics if a group session for the given room wasn't shared
245+
/// beforehand.
251246
#[wasm_bindgen(js_name = "encryptRoomEvent")]
252247
pub fn encrypt_room_event(
253248
&self,
254249
room_id: &identifiers::RoomId,
255-
event_type: &str,
250+
event_type: String,
256251
content: &str,
257252
) -> Result<Promise, JsError> {
258253
let room_id = room_id.inner.clone();
259-
let content: Box<RawJsonValue> = serde_json::from_str(content)?;
260-
let content = AnyMessageLikeEventContent::from_parts(event_type, &content)?;
261-
254+
let content: JsonValue = serde_json::from_str(content)?;
262255
let me = self.inner.clone();
263256

264257
Ok(future_to_promise(async move {
265-
Ok(serde_json::to_string(&me.encrypt_room_event(&room_id, content).await?)?)
258+
Ok(serde_json::to_string(
259+
&me.encrypt_room_event_raw(&room_id, content, event_type.as_ref()).await?,
260+
)?)
266261
}))
267262
}
268263

@@ -362,31 +357,6 @@ impl OlmMachine {
362357
}
363358
}))
364359
}
365-
366-
/// Get a verification object for the given user ID with the given flow ID.
367-
///
368-
/// Returns a list of `JsValue` to represent either (depending on
369-
/// how the Wasm module has been compiled):
370-
/// * `Sas` (enabled),
371-
/// * `Qr`
372-
#[cfg_attr(feature = "qrcode", doc = "(enabled).")]
373-
#[cfg_attr(not(feature = "qrcode"), doc = "(disabled).")]
374-
///
375-
/// If a verification mode is missing, please try to compile the
376-
/// Wasm module with different features.
377-
#[wasm_bindgen(js_name = "getVerification")]
378-
pub fn get_verification(
379-
&self,
380-
user_id: &identifiers::UserId,
381-
flow_id: &str,
382-
) -> Result<JsValue, JsError> {
383-
self.inner
384-
.get_verification(user_id.inner.as_ref(), flow_id)
385-
.map(verifications::Verification)
386-
.map(JsValue::try_from)
387-
.transpose()
388-
.map(|r| r.unwrap_or(JsValue::UNDEFINED))
389-
}
390360
}
391361

392362
/// An Ed25519 public key, used to verify digital signatures.

crates/matrix-sdk-crypto-js/src/requests.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ use ruma::api::client::keys::{
1515
};
1616
use wasm_bindgen::prelude::*;
1717

18-
/// Data for a request to the `upload_keys` API endpoint.
18+
/// Data for a request to the `/keys/upload` API endpoint
19+
/// ([specification]).
1920
///
2021
/// Publishes end-to-end encryption keys for the device.
22+
///
23+
/// [specification]: https://spec.matrix.org/unstable/client-server-api/#post_matrixclientv3keysupload
2124
#[derive(Debug)]
2225
#[wasm_bindgen(getter_with_clone)]
2326
pub struct KeysUploadRequest {
@@ -34,9 +37,12 @@ pub struct KeysUploadRequest {
3437
pub body: JsString,
3538
}
3639

37-
/// Data for a request to the `get_keys` API endpoint.
40+
/// Data for a request to the `/keys/query` API endpoint
41+
/// ([specification]).
3842
///
3943
/// Returns the current devices and identity keys for the given users.
44+
///
45+
/// [specification]: https://spec.matrix.org/unstable/client-server-api/#post_matrixclientv3keysquery
4046
#[derive(Debug)]
4147
#[wasm_bindgen(getter_with_clone)]
4248
pub struct KeysQueryRequest {
@@ -53,9 +59,13 @@ pub struct KeysQueryRequest {
5359
pub body: JsString,
5460
}
5561

56-
/// Data for a request to the `claim_keys` API endpoint.
62+
/// Data for a request to the `/keys/claim` API endpoint
63+
/// ([specification]).
5764
///
58-
/// Claims one-time keys for use in pre-key messages.
65+
/// Claims one-time keys that can be used to establish 1-to-1 E2EE
66+
/// sessions.
67+
///
68+
/// [specification]: https://spec.matrix.org/unstable/client-server-api/#post_matrixclientv3keysclaim
5969
#[derive(Debug)]
6070
#[wasm_bindgen(getter_with_clone)]
6171
pub struct KeysClaimRequest {
@@ -72,9 +82,12 @@ pub struct KeysClaimRequest {
7282
pub body: JsString,
7383
}
7484

75-
/// Data for a request to the `send_event_to_device` API endpoint.
85+
/// Data for a request to the `/sendToDevice` API endpoint
86+
/// ([specification]).
87+
///
88+
/// Send an event to a single device or to a group of devices.
7689
///
77-
/// Send an event to a device or devices.
90+
/// [specification]: https://spec.matrix.org/unstable/client-server-api/#put_matrixclientv3sendtodeviceeventtypetxnid
7891
#[derive(Debug)]
7992
#[wasm_bindgen(getter_with_clone)]
8093
pub struct ToDeviceRequest {
@@ -91,9 +104,12 @@ pub struct ToDeviceRequest {
91104
pub body: JsString,
92105
}
93106

94-
/// Data for a request to the `upload_signatures` API endpoint.
107+
/// Data for a request to the `/keys/signatures/upload` API endpoint
108+
/// ([specification]).
95109
///
96110
/// Publishes cross-signing signatures for the user.
111+
///
112+
/// [specification]: https://spec.matrix.org/unstable/client-server-api/#post_matrixclientv3keyssignaturesupload
97113
#[derive(Debug)]
98114
#[wasm_bindgen(getter_with_clone)]
99115
pub struct SignatureUploadRequest {
@@ -110,7 +126,10 @@ pub struct SignatureUploadRequest {
110126
pub body: JsString,
111127
}
112128

113-
/// A customized owned request type for sending out room messages.
129+
/// A customized owned request type for sending out room messages
130+
/// ([specification]).
131+
///
132+
/// [specification]: https://spec.matrix.org/unstable/client-server-api/#put_matrixclientv3roomsroomidsendeventtypetxnid
114133
#[derive(Debug)]
115134
#[wasm_bindgen(getter_with_clone)]
116135
pub struct RoomMessageRequest {
@@ -127,7 +146,10 @@ pub struct RoomMessageRequest {
127146
pub body: JsString,
128147
}
129148

130-
/// A request that will back up a batch of room keys to the server.
149+
/// A request that will back up a batch of room keys to the server
150+
/// ([specification]).
151+
///
152+
/// [specification]: https://spec.matrix.org/unstable/client-server-api/#put_matrixclientv3room_keyskeys
131153
#[derive(Debug)]
132154
#[wasm_bindgen(getter_with_clone)]
133155
pub struct KeysBackupRequest {
@@ -138,7 +160,7 @@ pub struct KeysBackupRequest {
138160
/// A JSON-encoded object of form:
139161
///
140162
/// ```
141-
/// {"version": …, "rooms": …}
163+
/// {"rooms": …}
142164
/// ```
143165
#[wasm_bindgen(readonly)]
144166
pub body: JsString,
@@ -173,7 +195,7 @@ request!(KeysClaimRequest from RumaKeysClaimRequest maps fields timeout, one_tim
173195
request!(ToDeviceRequest from RumaToDeviceRequest maps fields event_type, txn_id, messages);
174196
request!(SignatureUploadRequest from RumaSignatureUploadRequest maps fields signed_keys);
175197
request!(RoomMessageRequest from RumaRoomMessageRequest maps fields room_id, txn_id, content);
176-
request!(KeysBackupRequest from RumaKeysBackupRequest maps fields version, rooms);
198+
request!(KeysBackupRequest from RumaKeysBackupRequest maps fields rooms);
177199

178200
// JavaScript has no complex enums like Rust. To return structs of
179201
// different types, we have no choice that hiding everything behind a

0 commit comments

Comments
 (0)