From f4e0c6e243a09d3cdfc549e5cecce0850f8adc8e Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 15 Sep 2022 10:32:38 +0200 Subject: [PATCH] feat(crypto-js): Start implementing `OlmMachine.get_identity`. --- .../matrix-sdk-crypto-js/src/identifiers.rs | 2 +- .../matrix-sdk-crypto-js/src/identities.rs | 98 +++++++++++++++++++ bindings/matrix-sdk-crypto-js/src/lib.rs | 1 + bindings/matrix-sdk-crypto-js/src/machine.rs | 13 ++- 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 bindings/matrix-sdk-crypto-js/src/identities.rs diff --git a/bindings/matrix-sdk-crypto-js/src/identifiers.rs b/bindings/matrix-sdk-crypto-js/src/identifiers.rs index d52b96b00..938cf2d6b 100644 --- a/bindings/matrix-sdk-crypto-js/src/identifiers.rs +++ b/bindings/matrix-sdk-crypto-js/src/identifiers.rs @@ -288,7 +288,7 @@ impl ServerName { /// [event ID]: https://spec.matrix.org/v1.2/appendices/#room-ids-and-event-ids #[wasm_bindgen] pub struct EventId { - inner: ruma::OwnedEventId, + pub(crate) inner: ruma::OwnedEventId, } #[wasm_bindgen] diff --git a/bindings/matrix-sdk-crypto-js/src/identities.rs b/bindings/matrix-sdk-crypto-js/src/identities.rs new file mode 100644 index 000000000..38fc930dc --- /dev/null +++ b/bindings/matrix-sdk-crypto-js/src/identities.rs @@ -0,0 +1,98 @@ +//! User identities. + +use js_sys::{Array, Promise}; +use ruma::events::key::verification::VerificationMethod as RumaVerificationMethod; +use wasm_bindgen::prelude::*; + +use crate::{future::future_to_promise, identifiers, impl_from_to_inner, requests, verification}; + +pub(crate) struct UserIdentities { + inner: matrix_sdk_crypto::UserIdentities, +} + +impl_from_to_inner!(matrix_sdk_crypto::UserIdentities => UserIdentities); + +impl From for JsValue { + fn from(user_identities: UserIdentities) -> Self { + use matrix_sdk_crypto::UserIdentities::*; + + match user_identities.inner { + Own(own) => JsValue::from(OwnUserIdentity::from(own)), + Other(other) => JsValue::from(UserIdentity::from(other)), + } + } +} + +#[wasm_bindgen] +#[derive(Debug)] +pub struct OwnUserIdentity { + inner: matrix_sdk_crypto::OwnUserIdentity, +} + +impl_from_to_inner!(matrix_sdk_crypto::OwnUserIdentity => OwnUserIdentity); + +#[wasm_bindgen] +#[derive(Debug)] +pub struct UserIdentity { + inner: matrix_sdk_crypto::UserIdentity, +} + +impl_from_to_inner!(matrix_sdk_crypto::UserIdentity => UserIdentity); + +#[wasm_bindgen] +impl UserIdentity { + /// Is this user identity verified? + #[wasm_bindgen(js_name = "isVerified")] + pub fn is_verified(&self) -> bool { + self.inner.is_verified() + } + + /// Manually verify this user. + /// + /// This method will attempt to sign the user identity using our private + /// cross signing key. + /// + /// This method fails if we don't have the private part of our user-signing + /// key. + /// + /// Returns a request that needs to be sent out for the user to be marked as + /// verified. + pub fn verify(&self) -> Promise { + let me = self.inner.clone(); + + future_to_promise(async move { + Ok(requests::SignatureUploadRequest::try_from(&me.verify().await?)?) + }) + } + + /// Create a `VerificationRequest` object after the verification + /// request content has been sent out. } + #[wasm_bindgen(js_name = "requestVerification")] + pub fn request_verification( + &self, + room_id: &identifiers::RoomId, + request_event_id: &identifiers::EventId, + methods: Option, + ) -> Result { + let me = self.inner.clone(); + let room_id = room_id.inner.clone(); + let request_event_id = request_event_id.inner.clone(); + let methods: Option> = methods + .map(|array| { + array + .iter() + .map(|method| { + verification::VerificationMethod::try_from(method).map(Into::into) + }) + .collect::>() + }) + .transpose()?; + + Ok(future_to_promise::<_, verification::VerificationRequest>(async move { + Ok(me + .request_verification(room_id.as_ref(), request_event_id.as_ref(), methods) + .await + .into()) + })) + } +} diff --git a/bindings/matrix-sdk-crypto-js/src/lib.rs b/bindings/matrix-sdk-crypto-js/src/lib.rs index 2421eb801..219cf8fed 100644 --- a/bindings/matrix-sdk-crypto-js/src/lib.rs +++ b/bindings/matrix-sdk-crypto-js/src/lib.rs @@ -23,6 +23,7 @@ pub mod encryption; pub mod events; mod future; pub mod identifiers; +pub mod identities; pub mod machine; pub mod olm; mod macros; diff --git a/bindings/matrix-sdk-crypto-js/src/machine.rs b/bindings/matrix-sdk-crypto-js/src/machine.rs index fa4aeefd1..fda7459cc 100644 --- a/bindings/matrix-sdk-crypto-js/src/machine.rs +++ b/bindings/matrix-sdk-crypto-js/src/machine.rs @@ -10,7 +10,7 @@ use wasm_bindgen::prelude::*; use crate::{ device, downcast, encryption, future::future_to_promise, - identifiers, olm, requests, + identifiers, identities, olm, requests, requests::OutgoingRequest, responses::{self, response_from_string}, store, sync_events, types, verification, vodozemac, @@ -413,6 +413,17 @@ impl OlmMachine { }) } + /// Get the cross signing user identity of a user. + #[wasm_bindgen(js_name = "getIdentity")] + pub fn get_identity(&self, user_id: &identifiers::UserId) -> Promise { + let me = self.inner.clone(); + let user_id = user_id.inner.clone(); + + future_to_promise(async move { + Ok(me.get_identity(user_id.as_ref(), None).await?.map(identities::UserIdentities::from)) + }) + } + /// Sign the given message using our device key and if available /// cross-signing master key. pub fn sign(&self, message: String) -> Promise {