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