From 333e4ea50d93aa9477c63c5a8c7c98855041dbf2 Mon Sep 17 00:00:00 2001 From: valere Date: Thu, 20 Apr 2023 10:48:22 +0200 Subject: [PATCH] quick olm bindings --- bindings/matrix-sdk-crypto-ffi/src/machine.rs | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/bindings/matrix-sdk-crypto-ffi/src/machine.rs b/bindings/matrix-sdk-crypto-ffi/src/machine.rs index bd9b476ac..0f88e8623 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/machine.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/machine.rs @@ -743,6 +743,57 @@ impl OlmMachine { Ok(serde_json::to_string(&encrypted_content)?) } + /// Encrypt the given event with the given type and content for the given + /// room using olm encryption. + /// + /// **Note**: Olm sessions should be established with the given user prior to + /// calling this method. + /// + /// The usual flow to encrypt an event using this state machine is as + /// follows: + /// + /// 1. Get the one-time key claim request to establish 1:1 Olm sessions for + /// the room members of the room we wish to participate in. This is done + /// using the [`get_missing_sessions()`](#method.get_missing_sessions) + /// method. This method call should be locked per call. + /// + /// 3. Encrypt the event using this method. + /// + /// 4. Send the encrypted event to the server. + /// + pub fn encrypt_direct( + &self, + room_id: String, + users: Vec, + event_type: String, + content: String, + ) -> Result, CryptoStoreError> { + let room_id = RoomId::parse(room_id)?; + let content: Value = serde_json::from_str(&content)?; + + let users: Vec = + users.into_iter().filter_map(|u| UserId::parse(u).ok()).collect(); + + let encrypted_content = self + .runtime + .block_on(self.inner.encrypt_room_event_direct( + users.iter().map(Deref::deref), + &room_id, + content, + &event_type, + )) + .expect("Encrypting an event produced an error"); + + let events: Vec = encrypted_content + .iter() + .map(|c| serde_json::to_string(&c)) + .filter(|r| r.is_ok()) + .map(|o| o.unwrap()) + .collect(); + + Ok(events) + } + /// Decrypt the given event that was sent in the given room. /// /// # Arguments @@ -816,6 +867,15 @@ impl OlmMachine { encryption_info.verification_state.to_shield_state_lax().into() }, } + }, + AlgorithmInfo::OlmV1Curve25519AesSha2 { curve25519_key } => { + DecryptedEvent { + clear_event: serde_json::to_string(&event_json)?, + sender_curve25519_key: curve25519_key.to_owned(), + claimed_ed25519_key: None, + forwarding_curve25519_chain: vec![], + shield_state: encryption_info.verification_state.to_shield_state_lax().into() + } } }) }