|
| 1 | +//! Types for a `Device`. |
| 2 | +
|
| 3 | +use js_sys::{Array, Map, Promise}; |
| 4 | +use wasm_bindgen::prelude::*; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + future::future_to_promise, |
| 8 | + identifiers::{self, DeviceId, UserId}, |
| 9 | + types, verification, vodozemac, |
| 10 | +}; |
| 11 | + |
| 12 | +/// A device represents a E2EE capable client of an user. |
| 13 | +#[wasm_bindgen] |
| 14 | +#[derive(Debug)] |
| 15 | +pub struct Device { |
| 16 | + pub(crate) inner: matrix_sdk_crypto::Device, |
| 17 | +} |
| 18 | + |
| 19 | +impl From<matrix_sdk_crypto::Device> for Device { |
| 20 | + fn from(inner: matrix_sdk_crypto::Device) -> Self { |
| 21 | + Self { inner } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +#[wasm_bindgen] |
| 26 | +impl Device { |
| 27 | + /// Request an interactive verification with this device. |
| 28 | + #[wasm_bindgen(js_name = "requestVerification")] |
| 29 | + pub fn request_verification(&self, methods: Option<Array>) -> Result<Promise, JsError> { |
| 30 | + let methods = methods |
| 31 | + .map(|array| { |
| 32 | + array |
| 33 | + .iter() |
| 34 | + .map(|method| { |
| 35 | + verification::VerificationMethod::try_from(method).map(Into::into) |
| 36 | + }) |
| 37 | + .collect::<Result<_, _>>() |
| 38 | + }) |
| 39 | + .transpose()?; |
| 40 | + let me = self.inner.clone(); |
| 41 | + |
| 42 | + Ok(future_to_promise(async move { |
| 43 | + let tuple = Array::new(); |
| 44 | + let (verification_request, outgoing_verification_request) = match methods { |
| 45 | + Some(methods) => me.request_verification_with_methods(methods).await, |
| 46 | + None => me.request_verification().await, |
| 47 | + }; |
| 48 | + |
| 49 | + tuple.set(0, verification::VerificationRequest::from(verification_request).into()); |
| 50 | + tuple.set( |
| 51 | + 1, |
| 52 | + verification::OutgoingVerificationRequest::from(outgoing_verification_request) |
| 53 | + .try_into()?, |
| 54 | + ); |
| 55 | + |
| 56 | + Ok(tuple) |
| 57 | + })) |
| 58 | + } |
| 59 | + |
| 60 | + /// Is this device considered to be verified. |
| 61 | + /// |
| 62 | + /// This method returns true if either the `is_locally_trusted` |
| 63 | + /// method returns `true` or if the `is_cross_signing_trusted` |
| 64 | + /// method returns `true`. |
| 65 | + #[wasm_bindgen(js_name = "isVerified")] |
| 66 | + pub fn is_verified(&self) -> bool { |
| 67 | + self.inner.is_verified() |
| 68 | + } |
| 69 | + |
| 70 | + /// Is this device considered to be verified using cross signing. |
| 71 | + #[wasm_bindgen(js_name = "isCrossSigningTrusted")] |
| 72 | + pub fn is_cross_signing_trusted(&self) -> bool { |
| 73 | + self.inner.is_cross_signing_trusted() |
| 74 | + } |
| 75 | + |
| 76 | + /// Set the local trust state of the device to the given state. |
| 77 | + /// |
| 78 | + /// This won’t affect any cross signing trust state, this only |
| 79 | + /// sets a flag marking to have the given trust state. |
| 80 | + /// |
| 81 | + /// `trust_state` represents the new trust state that should be |
| 82 | + /// set for the device. |
| 83 | + #[wasm_bindgen(js_name = "setLocalTrust")] |
| 84 | + pub fn set_local_trust(&self, local_state: LocalTrust) -> Promise { |
| 85 | + let me = self.inner.clone(); |
| 86 | + |
| 87 | + future_to_promise(async move { |
| 88 | + me.set_local_trust(local_state.into()).await?; |
| 89 | + |
| 90 | + Ok(JsValue::NULL) |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + /// The user ID of the device owner. |
| 95 | + #[wasm_bindgen(getter, js_name = "userId")] |
| 96 | + pub fn user_id(&self) -> UserId { |
| 97 | + self.inner.user_id().to_owned().into() |
| 98 | + } |
| 99 | + |
| 100 | + /// The unique ID of the device. |
| 101 | + #[wasm_bindgen(getter, js_name = "deviceId")] |
| 102 | + pub fn device_id(&self) -> DeviceId { |
| 103 | + self.inner.device_id().to_owned().into() |
| 104 | + } |
| 105 | + |
| 106 | + /// Get the human readable name of the device. |
| 107 | + #[wasm_bindgen(getter, js_name = "displayName")] |
| 108 | + pub fn display_name(&self) -> Option<String> { |
| 109 | + self.inner.display_name().map(ToOwned::to_owned) |
| 110 | + } |
| 111 | + |
| 112 | + /// Get the key of the given key algorithm belonging to this device. |
| 113 | + #[wasm_bindgen(js_name = "getKey")] |
| 114 | + pub fn get_key( |
| 115 | + &self, |
| 116 | + algorithm: identifiers::DeviceKeyAlgorithmName, |
| 117 | + ) -> Result<Option<vodozemac::DeviceKey>, JsError> { |
| 118 | + Ok(self.inner.get_key(algorithm.try_into()?).cloned().map(Into::into)) |
| 119 | + } |
| 120 | + |
| 121 | + /// Get the Curve25519 key of the given device. |
| 122 | + #[wasm_bindgen(getter, js_name = "curve25519Key")] |
| 123 | + pub fn curve25519_key(&self) -> Option<vodozemac::Curve25519PublicKey> { |
| 124 | + self.inner.curve25519_key().map(Into::into) |
| 125 | + } |
| 126 | + |
| 127 | + /// Get the Ed25519 key of the given device. |
| 128 | + #[wasm_bindgen(getter, js_name = "ed25519Key")] |
| 129 | + pub fn ed25519_key(&self) -> Option<vodozemac::Ed25519PublicKey> { |
| 130 | + self.inner.ed25519_key().map(Into::into) |
| 131 | + } |
| 132 | + |
| 133 | + /// Get a map containing all the device keys. |
| 134 | + #[wasm_bindgen(getter)] |
| 135 | + pub fn keys(&self) -> Map { |
| 136 | + let map = Map::new(); |
| 137 | + |
| 138 | + for (device_key_id, device_key) in self.inner.keys() { |
| 139 | + map.set( |
| 140 | + &identifiers::DeviceKeyId::from(device_key_id.clone()).into(), |
| 141 | + &vodozemac::DeviceKey::from(device_key.clone()).into(), |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + map |
| 146 | + } |
| 147 | + |
| 148 | + /// Get a map containing all the device signatures. |
| 149 | + #[wasm_bindgen(getter)] |
| 150 | + pub fn signatures(&self) -> types::Signatures { |
| 151 | + self.inner.signatures().clone().into() |
| 152 | + } |
| 153 | + |
| 154 | + /// Get the trust state of the device. |
| 155 | + #[wasm_bindgen(getter, js_name = "localTrustState")] |
| 156 | + pub fn local_trust_state(&self) -> LocalTrust { |
| 157 | + self.inner.local_trust_state().into() |
| 158 | + } |
| 159 | + |
| 160 | + /// Is the device locally marked as trusted? |
| 161 | + #[wasm_bindgen(js_name = "isLocallyTrusted")] |
| 162 | + pub fn is_locally_trusted(&self) -> bool { |
| 163 | + self.inner.is_locally_trusted() |
| 164 | + } |
| 165 | + |
| 166 | + /// Is the device locally marked as blacklisted? |
| 167 | + /// |
| 168 | + /// Blacklisted devices won’t receive any group sessions. |
| 169 | + #[wasm_bindgen(js_name = "isBlacklisted")] |
| 170 | + pub fn is_blacklisted(&self) -> bool { |
| 171 | + self.inner.is_blacklisted() |
| 172 | + } |
| 173 | + |
| 174 | + /// Is the device deleted? |
| 175 | + #[wasm_bindgen(js_name = "isDeleted")] |
| 176 | + pub fn is_deleted(&self) -> bool { |
| 177 | + self.inner.is_deleted() |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +/// The local trust state of a device. |
| 182 | +#[wasm_bindgen] |
| 183 | +#[derive(Debug)] |
| 184 | +pub enum LocalTrust { |
| 185 | + /// The device has been verified and is trusted. |
| 186 | + Verified, |
| 187 | + |
| 188 | + /// The device been blacklisted from communicating. |
| 189 | + BlackListed, |
| 190 | + |
| 191 | + /// The trust state of the device is being ignored. |
| 192 | + Ignored, |
| 193 | + |
| 194 | + /// The trust state is unset. |
| 195 | + Unset, |
| 196 | +} |
| 197 | + |
| 198 | +impl From<matrix_sdk_crypto::LocalTrust> for LocalTrust { |
| 199 | + fn from(value: matrix_sdk_crypto::LocalTrust) -> Self { |
| 200 | + use matrix_sdk_crypto::LocalTrust::*; |
| 201 | + |
| 202 | + match value { |
| 203 | + Verified => Self::Verified, |
| 204 | + BlackListed => Self::BlackListed, |
| 205 | + Ignored => Self::Ignored, |
| 206 | + Unset => Self::Unset, |
| 207 | + } |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +impl From<LocalTrust> for matrix_sdk_crypto::LocalTrust { |
| 212 | + fn from(value: LocalTrust) -> Self { |
| 213 | + use LocalTrust::*; |
| 214 | + |
| 215 | + match value { |
| 216 | + Verified => Self::Verified, |
| 217 | + BlackListed => Self::BlackListed, |
| 218 | + Ignored => Self::Ignored, |
| 219 | + Unset => Self::Unset, |
| 220 | + } |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +/// A read only view over all devices belonging to a user. |
| 225 | +#[wasm_bindgen] |
| 226 | +#[derive(Debug)] |
| 227 | +pub struct UserDevices { |
| 228 | + pub(crate) inner: matrix_sdk_crypto::UserDevices, |
| 229 | +} |
| 230 | + |
| 231 | +impl From<matrix_sdk_crypto::UserDevices> for UserDevices { |
| 232 | + fn from(inner: matrix_sdk_crypto::UserDevices) -> Self { |
| 233 | + Self { inner } |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +#[wasm_bindgen] |
| 238 | +impl UserDevices { |
| 239 | + /// Get the specific device with the given device ID. |
| 240 | + pub fn get(&self, device_id: &DeviceId) -> Option<Device> { |
| 241 | + self.inner.get(&device_id.inner).map(Into::into) |
| 242 | + } |
| 243 | + |
| 244 | + /// Returns true if there is at least one devices of this user |
| 245 | + /// that is considered to be verified, false otherwise. |
| 246 | + /// |
| 247 | + /// This won't consider your own device as verified, as your own |
| 248 | + /// device is always implicitly verified. |
| 249 | + #[wasm_bindgen(js_name = "isAnyVerified")] |
| 250 | + pub fn is_any_verified(&self) -> bool { |
| 251 | + self.inner.is_any_verified() |
| 252 | + } |
| 253 | + |
| 254 | + /// Array over all the device IDs of the user devices. |
| 255 | + pub fn keys(&self) -> Array { |
| 256 | + self.inner.keys().map(ToOwned::to_owned).map(DeviceId::from).map(JsValue::from).collect() |
| 257 | + } |
| 258 | + |
| 259 | + /// Iterator over all the devices of the user devices. |
| 260 | + pub fn devices(&self) -> Array { |
| 261 | + self.inner.devices().map(Device::from).map(JsValue::from).collect() |
| 262 | + } |
| 263 | +} |
0 commit comments