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