crypto: Simplify the OlmMachine -> Device conversion.

master
Damir Jelić 2020-07-22 09:27:43 +02:00
parent 2481fbbd27
commit 9ef784d665
2 changed files with 7 additions and 36 deletions

View File

@ -184,40 +184,11 @@ impl Device {
pub(crate) fn mark_as_deleted(&self) { pub(crate) fn mark_as_deleted(&self) {
self.deleted.store(true, Ordering::Relaxed); self.deleted.store(true, Ordering::Relaxed);
} }
}
#[cfg(test)] #[cfg(test)]
impl From<&OlmMachine> for Device { pub async fn from_machine(machine: &OlmMachine) -> Device {
fn from(machine: &OlmMachine) -> Self { let device_keys = machine.account.device_keys().await;
let signatures = BTreeMap::new(); Device::try_from(&device_keys).unwrap()
Device {
user_id: Arc::new(machine.user_id().clone()),
device_id: Arc::new(machine.device_id().into()),
algorithms: Arc::new(vec![
Algorithm::MegolmV1AesSha2,
Algorithm::OlmV1Curve25519AesSha2,
]),
keys: Arc::new(
machine
.identity_keys()
.iter()
.map(|(key, value)| {
(
AlgorithmAndDeviceId(
KeyAlgorithm::try_from(key.as_ref()).unwrap(),
machine.device_id().into(),
),
value.to_owned(),
)
})
.collect(),
),
display_name: Arc::new(None),
deleted: Arc::new(AtomicBool::new(false)),
signatures: Arc::new(signatures),
trust_state: Arc::new(Atomic::new(TrustState::Unset)),
}
} }
} }

View File

@ -62,7 +62,7 @@ pub struct OlmMachine {
/// The unique device id of the device that holds this account. /// The unique device id of the device that holds this account.
device_id: Box<DeviceId>, device_id: Box<DeviceId>,
/// Our underlying Olm Account holding our identity keys. /// Our underlying Olm Account holding our identity keys.
account: Account, pub(crate) account: Account,
/// Store for the encryption keys. /// Store for the encryption keys.
/// Persists all the encryption keys so a client can resume the session /// Persists all the encryption keys so a client can resume the session
/// without the need to create new keys. /// without the need to create new keys.
@ -1278,8 +1278,8 @@ mod test {
let alice_device = alice_device_id(); let alice_device = alice_device_id();
let alice = OlmMachine::new(&alice_id, &alice_device); let alice = OlmMachine::new(&alice_id, &alice_device);
let alice_deivce = Device::from(&alice); let alice_deivce = Device::from_machine(&alice).await;
let bob_device = Device::from(&bob); let bob_device = Device::from_machine(&bob).await;
alice.store.save_devices(&[bob_device]).await.unwrap(); alice.store.save_devices(&[bob_device]).await.unwrap();
bob.store.save_devices(&[alice_deivce]).await.unwrap(); bob.store.save_devices(&[alice_deivce]).await.unwrap();