Skip to content

Commit 2da9d91

Browse files
committed
crypto: Add OlmMachine::device_creation_time
Turns out this is useful for element-hq/element-meta#2313.
1 parent ce7143b commit 2da9d91

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

crates/matrix-sdk-crypto/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Breaking changes:
1919

2020
Additions:
2121

22+
- Expose new method `OlmMachine::device_creation_time`.
23+
([#3275](https://github.com/matrix-org/matrix-rust-sdk/pull/3275))
24+
2225
- Log more details about the Olm session after encryption and decryption.
2326
([#3242](https://github.com/matrix-org/matrix-rust-sdk/pull/3242))
2427

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ use ruma::{
4040
AnyToDeviceEvent, MessageLikeEventContent,
4141
},
4242
serde::Raw,
43-
DeviceId, DeviceKeyAlgorithm, OwnedDeviceId, OwnedDeviceKeyId, OwnedTransactionId, OwnedUserId,
44-
RoomId, TransactionId, UInt, UserId,
43+
DeviceId, DeviceKeyAlgorithm, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedDeviceKeyId,
44+
OwnedTransactionId, OwnedUserId, RoomId, TransactionId, UInt, UserId,
4545
};
4646
use serde_json::value::to_raw_value;
4747
use tokio::sync::Mutex;
@@ -346,6 +346,16 @@ impl OlmMachine {
346346
&self.inner.device_id
347347
}
348348

349+
/// The time at which the `Account` backing this `OlmMachine` was created.
350+
///
351+
/// An [`Account`] is created when an `OlmMachine` is first instantiated
352+
/// against a given [`Store`], at which point it creates identity keys etc.
353+
/// This method returns the timestamp, according to the local clock, at
354+
/// which that happened.
355+
pub fn device_creation_time(&self) -> MilliSecondsSinceUnixEpoch {
356+
self.inner.store.static_account().creation_local_time()
357+
}
358+
349359
/// Get the public parts of our Olm identity keys.
350360
pub fn identity_keys(&self) -> IdentityKeys {
351361
let account = self.inner.store.static_account();
@@ -2410,8 +2420,13 @@ pub(crate) mod tests {
24102420

24112421
#[async_test]
24122422
async fn test_create_olm_machine() {
2423+
let test_start_ts = MilliSecondsSinceUnixEpoch::now();
24132424
let machine = OlmMachine::new(user_id(), alice_device_id()).await;
24142425

2426+
let device_creation_time = machine.device_creation_time();
2427+
assert!(device_creation_time <= MilliSecondsSinceUnixEpoch::now());
2428+
assert!(device_creation_time >= test_start_ts);
2429+
24152430
let cache = machine.store().cache().await.unwrap();
24162431
let account = cache.account().await.unwrap();
24172432
assert!(!account.shared());

0 commit comments

Comments
 (0)