crypto: Add OlmMachine::device_creation_time (#3275)

Turns out this is useful for https://github.com/element-hq/element-meta/issues/2313.
This commit is contained in:
Richard van der Hoff
2024-03-26 15:13:55 +00:00
committed by GitHub
parent ce7143b833
commit ab9e4f73b1
2 changed files with 20 additions and 2 deletions

View File

@@ -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))

View File

@@ -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());