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

crypto: Add OlmMachine::device_creation_time #3275

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions crates/matrix-sdk-crypto/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
19 changes: 17 additions & 2 deletions crates/matrix-sdk-crypto/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Comment on lines +2427 to +2428
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(no action needed) SystemTime (used by MilliSecondsSinceUnixEpoch) isn't monotonic, let's hope it's monotonic enough for these checks to not fail intermittently :)


let cache = machine.store().cache().await.unwrap();
let account = cache.account().await.unwrap();
assert!(!account.shared());
Expand Down
Loading