wacore/store/
traits.rs

1use crate::appstate::hash::HashState;
2use crate::store::error::Result;
3use async_trait::async_trait;
4
5use crate::libsignal::protocol::Direction;
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, Default, Serialize, Deserialize)]
9pub struct AppStateSyncKey {
10    pub key_data: Vec<u8>,
11    pub fingerprint: Vec<u8>,
12    pub timestamp: i64,
13}
14
15#[async_trait]
16pub trait IdentityStore: Send + Sync {
17    async fn put_identity(&self, address: &str, key: [u8; 32]) -> Result<()>;
18    async fn delete_identity(&self, address: &str) -> Result<()>;
19    async fn is_trusted_identity(
20        &self,
21        address: &str,
22        key: &[u8; 32],
23        direction: Direction,
24    ) -> Result<bool>;
25    async fn load_identity(&self, address: &str) -> Result<Option<Vec<u8>>>;
26}
27
28#[async_trait]
29pub trait SenderKeyStoreHelper: Send + Sync {
30    async fn put_sender_key(&self, address: &str, record: &[u8]) -> Result<()>;
31    async fn get_sender_key(&self, address: &str) -> Result<Option<Vec<u8>>>;
32    async fn delete_sender_key(&self, address: &str) -> Result<()>;
33}
34
35#[async_trait]
36pub trait SessionStore: Send + Sync {
37    async fn get_session(&self, address: &str) -> Result<Option<Vec<u8>>>;
38    async fn put_session(&self, address: &str, session: &[u8]) -> Result<()>;
39    async fn delete_session(&self, address: &str) -> Result<()>;
40    async fn has_session(&self, address: &str) -> Result<bool>;
41}
42
43#[async_trait]
44pub trait AppStateKeyStore: Send + Sync {
45    async fn get_app_state_sync_key(&self, key_id: &[u8]) -> Result<Option<AppStateSyncKey>>;
46    async fn set_app_state_sync_key(&self, key_id: &[u8], key: AppStateSyncKey) -> Result<()>;
47}
48
49#[async_trait]
50pub trait AppStateStore: Send + Sync {
51    async fn get_app_state_version(&self, name: &str) -> Result<HashState>;
52    async fn set_app_state_version(&self, name: &str, state: HashState) -> Result<()>;
53    async fn put_app_state_mutation_macs(
54        &self,
55        name: &str,
56        version: u64,
57        mutations: &[AppStateMutationMAC],
58    ) -> Result<()>;
59    async fn delete_app_state_mutation_macs(
60        &self,
61        name: &str,
62        index_macs: &[Vec<u8>],
63    ) -> Result<()>;
64    async fn get_app_state_mutation_mac(
65        &self,
66        name: &str,
67        index_mac: &[u8],
68    ) -> Result<Option<Vec<u8>>>;
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct AppStateMutationMAC {
73    pub index_mac: Vec<u8>,
74    pub value_mac: Vec<u8>,
75}
76
77/// Trait for device data persistence operations
78#[async_trait]
79pub trait DevicePersistence: Send + Sync {
80    /// Save device data (single device mode)
81    async fn save_device_data(&self, device_data: &crate::store::Device) -> Result<()>;
82
83    /// Save device data for a specific device ID (multi-account mode)
84    async fn save_device_data_for_device(
85        &self,
86        device_id: i32,
87        device_data: &crate::store::Device,
88    ) -> Result<()>;
89
90    /// Load device data (single device mode)
91    async fn load_device_data(&self) -> Result<Option<crate::store::Device>>;
92
93    /// Load device data for a specific device ID (multi-account mode)
94    async fn load_device_data_for_device(
95        &self,
96        device_id: i32,
97    ) -> Result<Option<crate::store::Device>>;
98
99    /// Check if a device row exists for the given `device_id`.
100    async fn device_exists(&self, device_id: i32) -> Result<bool>;
101
102    /// Create a new device row and return its generated `device_id`.
103    async fn create_new_device(&self) -> Result<i32>;
104}
105
106pub trait Backend:
107    IdentityStore
108    + SessionStore
109    + AppStateKeyStore
110    + AppStateStore
111    + crate::libsignal::store::PreKeyStore
112    + crate::libsignal::store::SignedPreKeyStore
113    + SenderKeyStoreHelper
114    + DevicePersistence
115    + Send
116    + Sync
117{
118}
119
120impl<T> Backend for T where
121    T: IdentityStore
122        + SessionStore
123        + AppStateKeyStore
124        + AppStateStore
125        + crate::libsignal::store::PreKeyStore
126        + crate::libsignal::store::SignedPreKeyStore
127        + SenderKeyStoreHelper
128        + DevicePersistence
129        + Send
130        + Sync
131{
132}