core_crypto_keystore/
proteus.rs1use crate::{
2 CryptoKeystoreError, CryptoKeystoreResult,
3 connection::{Connection, FetchFromDatabase},
4 entities::ProteusPrekey,
5};
6
7#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
8#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
9pub trait CryptoKeystoreProteus {
10 async fn proteus_store_prekey(&self, id: u16, prekey: &[u8]) -> CryptoKeystoreResult<()>;
11}
12
13#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
14#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
15impl CryptoKeystoreProteus for Connection {
16 async fn proteus_store_prekey(&self, id: u16, prekey: &[u8]) -> CryptoKeystoreResult<()> {
17 let entity = ProteusPrekey::from_raw(id, prekey.to_vec());
18 self.save(entity).await?;
19 Ok(())
20 }
21}
22
23#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
24#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
25impl proteus_traits::PreKeyStore for Connection {
26 type Error = CryptoKeystoreError;
27
28 async fn prekey(
29 &mut self,
30 id: proteus_traits::RawPreKeyId,
31 ) -> Result<Option<proteus_traits::RawPreKey>, Self::Error> {
32 Ok(self
33 .find::<ProteusPrekey>(&id.to_le_bytes())
34 .await?
35 .map(|db_prekey| db_prekey.prekey.clone()))
36 }
37
38 async fn remove(&mut self, id: proteus_traits::RawPreKeyId) -> Result<(), Self::Error> {
39 Connection::remove::<ProteusPrekey, _>(self, id.to_le_bytes()).await?;
40
41 Ok(())
42 }
43}