core_crypto_keystore/
proteus.rs

1// Wire
2// Copyright (C) 2022 Wire Swiss GmbH
3
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see http://www.gnu.org/licenses/.
16
17use crate::{
18    connection::{Connection, FetchFromDatabase},
19    entities::ProteusPrekey,
20    CryptoKeystoreError, CryptoKeystoreResult,
21};
22
23#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
24#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
25pub trait CryptoKeystoreProteus {
26    async fn proteus_store_prekey(&self, id: u16, prekey: &[u8]) -> CryptoKeystoreResult<()>;
27}
28
29#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
30#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
31impl CryptoKeystoreProteus for Connection {
32    async fn proteus_store_prekey(&self, id: u16, prekey: &[u8]) -> CryptoKeystoreResult<()> {
33        let entity = ProteusPrekey::from_raw(id, prekey.to_vec());
34        self.save(entity).await?;
35        Ok(())
36    }
37}
38
39#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
40#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
41impl proteus_traits::PreKeyStore for Connection {
42    type Error = CryptoKeystoreError;
43
44    async fn prekey(
45        &mut self,
46        id: proteus_traits::RawPreKeyId,
47    ) -> Result<Option<proteus_traits::RawPreKey>, Self::Error> {
48        Ok(self
49            .find::<ProteusPrekey>(&id.to_le_bytes())
50            .await?
51            .map(|db_prekey| db_prekey.prekey.clone()))
52    }
53
54    async fn remove(&mut self, id: proteus_traits::RawPreKeyId) -> Result<(), Self::Error> {
55        Connection::remove::<ProteusPrekey, _>(self, id.to_le_bytes()).await?;
56
57        Ok(())
58    }
59}