Overview
whatsapp-rust uses a trait-based storage system to persist device state, cryptographic keys, and protocol metadata. The storage layer is split into four domain-specific traits:- SignalStore - Signal protocol cryptographic operations (identity keys, sessions, pre-keys, sender keys)
- AppSyncStore - WhatsApp app state synchronization (sync keys, versions, mutation MACs)
- ProtocolStore - WhatsApp protocol alignment (SKDM tracking, LID-PN mapping, device registry)
- DeviceStore - Device persistence operations
Backend trait for convenience.
The Backend Trait
Any type implementing all four domain traits automatically implementsBackend:
SignalStore Trait
Handles Signal protocol cryptographic storage for end-to-end encryption.Identity Operations
Session Operations
PreKey Operations
Signed PreKey Operations
Sender Key Operations
For group messaging encryption:AppSyncStore Trait
Handles WhatsApp app state synchronization storage.Sync Key Operations
Version Tracking
Mutation MAC Operations
ProtocolStore Trait
Handles WhatsApp protocol alignment and tracking.SKDM Tracking
Tracks which devices have received Sender Key Distribution Messages in groups:LID-PN Mapping
Manages mappings between LID (Locally Indexed Device) and phone numbers:Base Key Collision Detection
Device Registry
Sender Key Status
Lazy deletion tracking for sender keys:TcToken Storage
Trusted contact privacy tokens:DeviceStore Trait
Handles device data persistence:SqliteStore Implementation
The default storage implementation using SQLite with Diesel ORM.Creating a Store
Features
- Connection pooling - Uses Diesel r2d2 with pool size of 2
- WAL mode - Write-Ahead Logging for better concurrency
- Automatic migrations - Runs embedded migrations on startup
- Semaphore-based locking - Prevents concurrent writes
- Retry logic - Automatic retry with exponential backoff for locked database
- Multi-device support - Single database can store multiple device sessions
Database Configuration
SqliteStore automatically configures connections with:Usage Example
Implementing Custom Storage
To implement a custom storage backend:- Implement all four domain traits
- The
Backendtrait is automatically implemented - All methods must be
asyncand thread-safe (Send + Sync)
Example: Redis Store
Best Practices
- Thread Safety - Use
Arcfor shared state,Mutexfor mutable state - Error Handling - Convert backend errors to
StoreErrorvariants - Transactions - Use database transactions for atomic operations
- Retries - Implement retry logic for transient failures
- Connection Pooling - Reuse connections when possible
- Blocking Operations - Wrap blocking I/O in
tokio::task::spawn_blocking
Data Structures
AppStateSyncKey
LidPnMappingEntry
TcTokenEntry
DeviceListRecord
Error Handling
All storage operations returnResult<T> from wacore::store::error:
See Also
- Transport Trait - Network transport abstraction
- HTTP Client Trait - HTTP client abstraction
- Client API - Main client interface