From 077050efb48118c9903737c11fca68f42f7fd334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 22 Jan 2021 18:40:08 +0100 Subject: [PATCH] crypto: Add a hack so e2ee support works under WASM again --- matrix_sdk_crypto/src/identities/device.rs | 6 ++++++ matrix_sdk_crypto/src/identities/mod.rs | 24 ++++++++++++++++++++++ matrix_sdk_crypto/src/identities/user.rs | 6 ++++++ 3 files changed, 36 insertions(+) diff --git a/matrix_sdk_crypto/src/identities/device.rs b/matrix_sdk_crypto/src/identities/device.rs index a26a33be..5348f497 100644 --- a/matrix_sdk_crypto/src/identities/device.rs +++ b/matrix_sdk_crypto/src/identities/device.rs @@ -56,6 +56,8 @@ use crate::{ Sas, ToDeviceRequest, }; +use super::{atomic_bool_deserializer, atomic_bool_serializer}; + /// A read-only version of a `Device`. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ReadOnlyDevice { @@ -65,6 +67,10 @@ pub struct ReadOnlyDevice { keys: Arc>, pub(crate) signatures: Arc>>, display_name: Arc>, + #[serde( + serialize_with = "atomic_bool_serializer", + deserialize_with = "atomic_bool_deserializer" + )] deleted: Arc, #[serde( serialize_with = "local_trust_serializer", diff --git a/matrix_sdk_crypto/src/identities/mod.rs b/matrix_sdk_crypto/src/identities/mod.rs index c1392eba..e9161a0d 100644 --- a/matrix_sdk_crypto/src/identities/mod.rs +++ b/matrix_sdk_crypto/src/identities/mod.rs @@ -50,3 +50,27 @@ pub use user::{ MasterPubkey, OwnUserIdentity, SelfSigningPubkey, UserIdentities, UserIdentity, UserSigningPubkey, }; + +use serde::{Deserialize, Deserializer, Serializer}; +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, +}; + +// These methods are only here because Serialize and Deserialize don't seem to +// be implemented for WASM. +fn atomic_bool_serializer(x: &AtomicBool, s: S) -> Result +where + S: Serializer, +{ + let value = x.load(Ordering::SeqCst); + s.serialize_some(&value) +} + +fn atomic_bool_deserializer<'de, D>(deserializer: D) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + let value = bool::deserialize(deserializer)?; + Ok(Arc::new(AtomicBool::new(value))) +} diff --git a/matrix_sdk_crypto/src/identities/user.rs b/matrix_sdk_crypto/src/identities/user.rs index dc87243b..fa5e7b2e 100644 --- a/matrix_sdk_crypto/src/identities/user.rs +++ b/matrix_sdk_crypto/src/identities/user.rs @@ -33,6 +33,8 @@ use matrix_sdk_common::{ use crate::olm::PrivateCrossSigningIdentity; use crate::{error::SignatureError, olm::Utility, ReadOnlyDevice}; +use super::{atomic_bool_deserializer, atomic_bool_serializer}; + /// Wrapper for a cross signing key marking it as the master key. /// /// Master keys are used to sign other cross signing keys, the self signing and @@ -573,6 +575,10 @@ pub struct OwnUserIdentity { master_key: MasterPubkey, self_signing_key: SelfSigningPubkey, user_signing_key: UserSigningPubkey, + #[serde( + serialize_with = "atomic_bool_serializer", + deserialize_with = "atomic_bool_deserializer" + )] verified: Arc, }