From 4c60db94a25ddc96fecfefc7c959d3719df99ef2 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Thu, 25 Nov 2021 15:52:03 +0100 Subject: [PATCH] Implement helper for wasm32 MilliSecondsSinceUnixEpoch --- crates/matrix-sdk-common/src/lib.rs | 1 + crates/matrix-sdk-common/src/util.rs | 14 ++++++++++++++ crates/matrix-sdk-crypto/src/machine.rs | 4 +++- .../src/olm/group_sessions/mod.rs | 4 +++- .../matrix-sdk-crypto/src/store/indexeddb.rs | 4 ++-- .../src/verification/machine.rs | 18 ++++-------------- .../src/verification/requests.rs | 6 +++--- .../src/verification/sas/inner_sas.rs | 2 +- .../src/verification/sas/mod.rs | 2 +- .../src/verification/sas/sas_state.rs | 6 ++---- 10 files changed, 34 insertions(+), 27 deletions(-) create mode 100644 crates/matrix-sdk-common/src/util.rs diff --git a/crates/matrix-sdk-common/src/lib.rs b/crates/matrix-sdk-common/src/lib.rs index 9f904663b..0e4b58db3 100644 --- a/crates/matrix-sdk-common/src/lib.rs +++ b/crates/matrix-sdk-common/src/lib.rs @@ -15,6 +15,7 @@ pub use uuid; pub mod deserialized_responses; pub mod executor; pub mod locks; +pub mod util; /// Super trait that is used for our store traits, this trait will differ if /// it's used on WASM. WASM targets will not require `Send` and `Sync` to have diff --git a/crates/matrix-sdk-common/src/util.rs b/crates/matrix-sdk-common/src/util.rs new file mode 100644 index 000000000..8a644639c --- /dev/null +++ b/crates/matrix-sdk-common/src/util.rs @@ -0,0 +1,14 @@ +use ruma::MilliSecondsSinceUnixEpoch; +use instant::SystemTime; + +/// Platform agnostic helper function to create MilliSecondsSinceUnixEpoch +pub fn milli_seconds_since_unix_epoch() -> MilliSecondsSinceUnixEpoch { + let duration = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .expect("now is always higher"); + let millis = duration. + as_millis() + .try_into() + .expect("can't convert miliseconds since UNIXEPOCH"); + MilliSecondsSinceUnixEpoch(millis) +} \ No newline at end of file diff --git a/crates/matrix-sdk-crypto/src/machine.rs b/crates/matrix-sdk-crypto/src/machine.rs index d532fb6cb..0b44ff4bc 100644 --- a/crates/matrix-sdk-crypto/src/machine.rs +++ b/crates/matrix-sdk-crypto/src/machine.rs @@ -25,6 +25,7 @@ use matrix_sdk_common::{ deserialized_responses::{AlgorithmInfo, EncryptionInfo, SyncRoomEvent, VerificationState}, locks::Mutex, uuid::Uuid, + util::milli_seconds_since_unix_epoch, }; use ruma::{ api::client::r0::{ @@ -1569,6 +1570,7 @@ pub(crate) mod test { verification::test::{outgoing_request_to_event, request_to_event}, EncryptionSettings, ReadOnlyDevice, ToDeviceRequest, }; + use matrix_sdk_common::util::milli_seconds_since_unix_epoch; /// These keys need to be periodically uploaded to the server. type OneTimeKeys = BTreeMap; @@ -2020,7 +2022,7 @@ pub(crate) mod test { let event = SyncMessageEvent { event_id: event_id!("$xxxxx:example.org"), - origin_server_ts: MilliSecondsSinceUnixEpoch::now(), + origin_server_ts: milli_seconds_since_unix_epoch(), sender: alice.user_id().clone(), content: encrypted_content, unsigned: Unsigned::default(), diff --git a/crates/matrix-sdk-crypto/src/olm/group_sessions/mod.rs b/crates/matrix-sdk-crypto/src/olm/group_sessions/mod.rs index 400bc8160..fd2e5a315 100644 --- a/crates/matrix-sdk-crypto/src/olm/group_sessions/mod.rs +++ b/crates/matrix-sdk-crypto/src/olm/group_sessions/mod.rs @@ -164,9 +164,11 @@ mod test { use matrix_sdk_test::async_test; use std::{ sync::Arc, - time::{Duration, Instant}, + time::Duration, }; + use matrix_sdk_common::instant::Instant; + use ruma::{events::room::message::RoomMessageEventContent, room_id, user_id}; use super::EncryptionSettings; diff --git a/crates/matrix-sdk-crypto/src/store/indexeddb.rs b/crates/matrix-sdk-crypto/src/store/indexeddb.rs index eb97a2b77..6097c64c1 100644 --- a/crates/matrix-sdk-crypto/src/store/indexeddb.rs +++ b/crates/matrix-sdk-crypto/src/store/indexeddb.rs @@ -32,8 +32,8 @@ use tracing::trace; use uuid::Uuid; use super::{ - caches::SessionStore, Changes, CryptoStore, CryptoStoreError, InboundGroupSession, PickleKey, - ReadOnlyAccount, Result, Session, EncryptedPickleKey, + caches::SessionStore, BackupKeys, Changes, CryptoStore, CryptoStoreError, InboundGroupSession, + PickleKey, EncryptedPickleKey, ReadOnlyAccount, Result, RoomKeyCounts, Session, }; use crate::{ gossiping::{GossipRequest, SecretInfo}, diff --git a/crates/matrix-sdk-crypto/src/verification/machine.rs b/crates/matrix-sdk-crypto/src/verification/machine.rs index 14ecd73e8..fb24581c7 100644 --- a/crates/matrix-sdk-crypto/src/verification/machine.rs +++ b/crates/matrix-sdk-crypto/src/verification/machine.rs @@ -28,6 +28,7 @@ use ruma::{ DeviceId, DeviceIdBox, EventId, MilliSecondsSinceUnixEpoch, RoomId, UserId, }; use tracing::{info, trace, warn}; +use matrix_sdk_common::util::milli_seconds_since_unix_epoch; use super::{ cache::VerificationCache, @@ -190,7 +191,6 @@ impl VerificationMachine { self.verifications.get_sas(user_id, flow_id) } - #[cfg(not(target_arch = "wasm32"))] fn is_timestamp_valid(timestamp: &MilliSecondsSinceUnixEpoch) -> bool { use ruma::{uint, UInt}; @@ -201,21 +201,12 @@ impl VerificationMachine { let timestamp_threshold: UInt = uint!(300); let timestamp = timestamp.as_secs(); - let now = MilliSecondsSinceUnixEpoch::now().as_secs(); + let now = milli_seconds_since_unix_epoch().as_secs(); !(now.saturating_sub(timestamp) > old_timestamp_threshold || timestamp.saturating_sub(now) > timestamp_threshold) } - #[cfg(target_arch = "wasm32")] - fn is_timestamp_valid(timestamp: &MilliSecondsSinceUnixEpoch) -> bool { - // TODO the non-wasm method with the same name uses - // `MilliSecondsSinceUnixEpoch::now()` which internally uses - // `SystemTime::now()` this panics under WASM, thus we're returning here - // true for now. - true - } - fn queue_up_content( &self, recipient: &UserId, @@ -519,10 +510,9 @@ mod test { use std::{ convert::TryFrom, sync::Arc, - time::{Duration, Instant}, + time::Duration, }; - - use matrix_sdk_common::locks::Mutex; + use matrix_sdk_common::{instant::Instant, locks::Mutex}; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::wasm_bindgen_test; diff --git a/crates/matrix-sdk-crypto/src/verification/requests.rs b/crates/matrix-sdk-crypto/src/verification/requests.rs index 7bb5b6d8f..6910c73dd 100644 --- a/crates/matrix-sdk-crypto/src/verification/requests.rs +++ b/crates/matrix-sdk-crypto/src/verification/requests.rs @@ -19,7 +19,7 @@ use std::{ #[cfg(feature = "qrcode")] use matrix_qrcode::QrVerificationData; -use matrix_sdk_common::{instant::Instant, uuid::Uuid}; +use matrix_sdk_common::{instant::Instant, uuid::Uuid, util::milli_seconds_since_unix_epoch}; #[cfg(feature = "qrcode")] use ruma::DeviceKeyAlgorithm; use ruma::{ @@ -35,7 +35,7 @@ use ruma::{ AnyMessageEventContent, AnyToDeviceEventContent, }, to_device::DeviceIdOrAllDevices, - DeviceId, DeviceIdBox, MilliSecondsSinceUnixEpoch, RoomId, UserId, + DeviceId, DeviceIdBox, RoomId, UserId, }; use tracing::{info, trace, warn}; @@ -161,7 +161,7 @@ impl VerificationRequest { self.account.device_id().into(), self.flow_id().as_str().to_string(), methods, - MilliSecondsSinceUnixEpoch::now(), + milli_seconds_since_unix_epoch(), ); ToDeviceRequest::new_for_recipients( diff --git a/crates/matrix-sdk-crypto/src/verification/sas/inner_sas.rs b/crates/matrix-sdk-crypto/src/verification/sas/inner_sas.rs index af0c806f2..bac86ffc9 100644 --- a/crates/matrix-sdk-crypto/src/verification/sas/inner_sas.rs +++ b/crates/matrix-sdk-crypto/src/verification/sas/inner_sas.rs @@ -14,7 +14,7 @@ use std::sync::Arc; #[cfg(test)] -use std::time::Instant; +use matrix_sdk_common::instant::Instant; use ruma::{ events::key::verification::{cancel::CancelCode, ShortAuthenticationString}, diff --git a/crates/matrix-sdk-crypto/src/verification/sas/mod.rs b/crates/matrix-sdk-crypto/src/verification/sas/mod.rs index 073571945..05f450d83 100644 --- a/crates/matrix-sdk-crypto/src/verification/sas/mod.rs +++ b/crates/matrix-sdk-crypto/src/verification/sas/mod.rs @@ -18,7 +18,7 @@ mod sas_state; use std::sync::{Arc, Mutex}; #[cfg(test)] -use std::time::Instant; +use matrix_sdk_common::instant::Instant; use inner_sas::InnerSas; use matrix_sdk_common::uuid::Uuid; diff --git a/crates/matrix-sdk-crypto/src/verification/sas/sas_state.rs b/crates/matrix-sdk-crypto/src/verification/sas/sas_state.rs index 9902bc603..551059da8 100644 --- a/crates/matrix-sdk-crypto/src/verification/sas/sas_state.rs +++ b/crates/matrix-sdk-crypto/src/verification/sas/sas_state.rs @@ -16,8 +16,9 @@ use std::{ convert::{TryFrom, TryInto}, matches, sync::{Arc, Mutex}, - time::{Duration, Instant}, + time::{Duration}, }; +use matrix_sdk_common::instant::Instant; use matrix_sdk_common::uuid::Uuid; use olm_rs::sas::OlmSas; @@ -1136,9 +1137,6 @@ impl SasState { #[cfg(test)] mod test { use std::convert::TryFrom; - - #[cfg(target_arch = "wasm32")] - use wasm_bindgen_test::wasm_bindgen_test; use matrix_sdk_test::async_test; use ruma::{