From 2da9d9185e10f0de0bb704fc0b4d39c3dd9ccdc1 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 26 Mar 2024 12:40:41 +0000 Subject: [PATCH] crypto: Add `OlmMachine::device_creation_time` Turns out this is useful for https://github.com/element-hq/element-meta/issues/2313. --- crates/matrix-sdk-crypto/CHANGELOG.md | 3 +++ crates/matrix-sdk-crypto/src/machine.rs | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/matrix-sdk-crypto/CHANGELOG.md b/crates/matrix-sdk-crypto/CHANGELOG.md index c51e85b2100..14ff187ecd8 100644 --- a/crates/matrix-sdk-crypto/CHANGELOG.md +++ b/crates/matrix-sdk-crypto/CHANGELOG.md @@ -19,6 +19,9 @@ Breaking changes: Additions: +- Expose new method `OlmMachine::device_creation_time`. + ([#3275](https://github.com/matrix-org/matrix-rust-sdk/pull/3275)) + - Log more details about the Olm session after encryption and decryption. ([#3242](https://github.com/matrix-org/matrix-rust-sdk/pull/3242)) diff --git a/crates/matrix-sdk-crypto/src/machine.rs b/crates/matrix-sdk-crypto/src/machine.rs index 7ea6d4e296d..bede64636fe 100644 --- a/crates/matrix-sdk-crypto/src/machine.rs +++ b/crates/matrix-sdk-crypto/src/machine.rs @@ -40,8 +40,8 @@ use ruma::{ AnyToDeviceEvent, MessageLikeEventContent, }, serde::Raw, - DeviceId, DeviceKeyAlgorithm, OwnedDeviceId, OwnedDeviceKeyId, OwnedTransactionId, OwnedUserId, - RoomId, TransactionId, UInt, UserId, + DeviceId, DeviceKeyAlgorithm, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedDeviceKeyId, + OwnedTransactionId, OwnedUserId, RoomId, TransactionId, UInt, UserId, }; use serde_json::value::to_raw_value; use tokio::sync::Mutex; @@ -346,6 +346,16 @@ impl OlmMachine { &self.inner.device_id } + /// The time at which the `Account` backing this `OlmMachine` was created. + /// + /// An [`Account`] is created when an `OlmMachine` is first instantiated + /// against a given [`Store`], at which point it creates identity keys etc. + /// This method returns the timestamp, according to the local clock, at + /// which that happened. + pub fn device_creation_time(&self) -> MilliSecondsSinceUnixEpoch { + self.inner.store.static_account().creation_local_time() + } + /// Get the public parts of our Olm identity keys. pub fn identity_keys(&self) -> IdentityKeys { let account = self.inner.store.static_account(); @@ -2410,8 +2420,13 @@ pub(crate) mod tests { #[async_test] async fn test_create_olm_machine() { + let test_start_ts = MilliSecondsSinceUnixEpoch::now(); let machine = OlmMachine::new(user_id(), alice_device_id()).await; + let device_creation_time = machine.device_creation_time(); + assert!(device_creation_time <= MilliSecondsSinceUnixEpoch::now()); + assert!(device_creation_time >= test_start_ts); + let cache = machine.store().cache().await.unwrap(); let account = cache.account().await.unwrap(); assert!(!account.shared());