Skip to content

Commit

Permalink
crypto: Add OlmMachine::device_creation_time (#3275)
Browse files Browse the repository at this point in the history
Turns out this is useful for element-hq/element-meta#2313.
  • Loading branch information
richvdh committed Mar 26, 2024
1 parent ce7143b commit ab9e4f7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
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);

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

0 comments on commit ab9e4f7

Please sign in to comment.