Skip to main content

core_crypto/proteus/
conversation_session.rs

1use std::sync::Arc;
2
3use proteus_wasm::{keys::IdentityKeyPair, message::Envelope, session::Session};
4
5use crate::{ProteusError, Result};
6
7/// Proteus session IDs, it seems it's basically a string
8pub type SessionIdentifier = String;
9
10/// Proteus Session wrapper, that contains the identifier and the associated proteus Session
11#[derive(Debug)]
12pub struct ProteusConversationSession {
13    pub(crate) identifier: SessionIdentifier,
14    pub(crate) session: Session<Arc<IdentityKeyPair>>,
15}
16
17impl ProteusConversationSession {
18    /// Encrypts a message for this Proteus session
19    pub fn encrypt(&mut self, plaintext: &[u8]) -> Result<Vec<u8>> {
20        self.session
21            .encrypt(plaintext)
22            .and_then(|e| e.serialise())
23            .map_err(ProteusError::wrap("encrypting message for proteus session"))
24            .map_err(Into::into)
25    }
26
27    /// Decrypts a message for this Proteus session
28    pub async fn decrypt(&mut self, store: &mut core_crypto_keystore::Database, ciphertext: &[u8]) -> Result<Vec<u8>> {
29        let envelope = Envelope::deserialise(ciphertext).map_err(ProteusError::wrap("deserializing envelope"))?;
30        self.session
31            .decrypt(store, &envelope)
32            .await
33            .map_err(ProteusError::wrap("decrypting message for proteus session"))
34            .map_err(Into::into)
35    }
36
37    /// Returns the session identifier
38    pub fn identifier(&self) -> &str {
39        &self.identifier
40    }
41
42    /// Returns the public key fingerprint of the local identity (= self identity)
43    pub fn fingerprint_local(&self) -> String {
44        self.session.local_identity().fingerprint()
45    }
46
47    /// Returns the public key fingerprint of the remote identity (= client you're communicating with)
48    pub fn fingerprint_remote(&self) -> String {
49        self.session.remote_identity().fingerprint()
50    }
51}