Skip to main content

core_crypto_keystore/
proteus.rs

1use crate::{
2    CryptoKeystoreError, CryptoKeystoreResult, Database, entities::ProteusPrekey, traits::FetchFromDatabase as _,
3};
4
5impl Database {
6    pub async fn proteus_store_prekey(&self, id: u16, prekey: &[u8]) -> CryptoKeystoreResult<()> {
7        self.with_transaction(async |tx| tx.save(ProteusPrekey::from_raw(id, prekey.to_vec())).await)
8            .await
9    }
10}
11
12#[cfg_attr(target_os = "unknown", async_trait::async_trait(?Send))]
13#[cfg_attr(not(target_os = "unknown"), async_trait::async_trait)]
14impl proteus_traits::PreKeyStore for Database {
15    type Error = CryptoKeystoreError;
16
17    async fn prekey(&self, id: proteus_traits::RawPreKeyId) -> Result<Option<proteus_traits::RawPreKey>, Self::Error> {
18        self.get::<ProteusPrekey>(&id)
19            .await
20            .map(|maybe_prekey| maybe_prekey.map(|mut db_prekey| std::mem::take(&mut db_prekey.prekey)))
21    }
22
23    async fn remove(&self, id: proteus_traits::RawPreKeyId) -> Result<(), Self::Error> {
24        self.with_transaction(async |tx| tx.remove::<ProteusPrekey>(&id).await)
25            .await
26            .map(|_| ())
27    }
28}