Skip to main content

core_crypto/proteus/
conversation_session.rs

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