Skip to main content

core_crypto/transaction_context/
decrypt.rs

1use std::borrow::Cow;
2
3use super::{Result, TransactionContext};
4use crate::{DecryptionOutcome, RecursiveError, mls::conversation::ConversationIdRef};
5
6impl TransactionContext {
7    /// Decrypt and process a MLS or TNT message.
8    ///
9    /// Effect on local state depends on what the message turns out to be:
10    ///
11    /// - a commit from another member supersedes any local pending commit, and consumes all pending proposals;
12    /// - our own commit, delivered back to us, is merged rather than discarded;
13    /// - proposals and application messages leave local pending state untouched.
14    ///
15    /// The message bytes must be TLS-serialized.
16    ///
17    /// Returns a vector. The first item in this vector is always the message this function was called with;
18    /// subsequent items are buffered messages which have been unblocked by that first message. If the initial
19    /// message was deferred, this vector will be empty.
20    ///
21    /// Errors occurred when decrypting the message with which this function was called propagate as normal.
22    /// Errors occurred when decrypting buffered messages unblocked by the initial message are propagated in
23    /// case they are infrastructure errors. If they are not, then they are logged but not propagated, and the
24    /// related message is dropped.
25    ///
26    /// Logically decryption is an operation on a conversation, and in fact that is how it is implemented internally.
27    /// But there's a trap: normally, we only want to load and handle non-pending conversations, so the type system
28    /// can ensure we don't do illegal operations on a conversation which hasn't been realized yet. But for decryption
29    /// specifically we have to route it through a pending conversation, because that's how the pending conversation
30    /// can learn that it's time to merge itself.
31    pub async fn decrypt_message(
32        &self,
33        id: &ConversationIdRef,
34        bytes: impl Into<Cow<'_, [u8]>>,
35    ) -> Result<Vec<DecryptionOutcome>> {
36        let mut conversation = self.conversation_regardless_of_pending_status(id).await?;
37        conversation
38            .decrypt_message(bytes)
39            .await
40            .map_err(RecursiveError::context(
41                "dispatching message decryption to conversation",
42            ))
43            .map_err(Into::into)
44    }
45}