core_crypto/proteus/
conversation_session.rs1use std::sync::Arc;
2
3use core_crypto_keystore::Transaction;
4use proteus_wasm::{keys::IdentityKeyPair, message::Envelope, session::Session};
5
6use crate::{ProteusError, Result};
7
8pub type SessionIdentifier = String;
10
11#[derive(Debug)]
13pub struct ProteusConversationSession {
14 pub(crate) identifier: SessionIdentifier,
15 pub(crate) session: Session<Arc<IdentityKeyPair>>,
16}
17
18impl ProteusConversationSession {
19 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 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 pub fn identifier(&self) -> &str {
40 &self.identifier
41 }
42
43 pub fn fingerprint_local(&self) -> String {
45 self.session.local_identity().fingerprint()
46 }
47
48 pub fn fingerprint_remote(&self) -> String {
50 self.session.remote_identity().fingerprint()
51 }
52}