Overview
Thexmtp_db crate provides a durable, encrypted SQLite-based storage layer for XMTP. It handles persistence of groups, messages, identity information, and MLS protocol state using Diesel ORM with encrypted storage via SQLCipher.
Installation
Key Exports
The default platform-specific encrypted message store implementation.
Default database connection type for the store.
Default MLS key store implementation backed by SQL.
Main encrypted storage implementation parameterized by database type.
Wrapper around database connections with query execution methods.
Core Modules
encrypted_store
Main encrypted storage implementation:- Database initialization
- Migration management
- Encrypted operations
- Transaction handling
sql_key_store
OpenMLS key store implementation:- MLS group state storage
- Key package storage
- Cryptographic key management
- Credential storage
group
Group conversation storage:- Group metadata
- Membership information
- Group state tracking
- Conversation types (group/DM)
group_message
Message persistence:- Message storage and retrieval
- Delivery status tracking
- Message sequencing
- Reactions and replies
identity
Identity information storage:- Installation identity
- Inbox associations
- Key rotation state
consent_record
Consent state management:- Per-contact consent
- Per-group consent
- Allow/deny lists
Main Types and Traits
Core trait for XMTP database operations.Associated Types:
DbQuery- Query type for database operations
raw_query_read()- Execute read-only queriesraw_query_write()- Execute write queriestransaction()- Execute transactional operations
Encrypted storage implementation.Type Parameters:
D: Database- Underlying database type
new()- Create new store with encryption keynew_unencrypted()- Create unencrypted store (testing only)conn()- Get database connection
Represents a stored group conversation.Fields:
id: Vec<u8>- Group IDcreated_at_ns: i64- Creation timestampmembership_state: GroupMembershipState- Current membershipconversation_type: ConversationType- Group or DMadded_by_inbox_id: String- Who added this installation
Represents a stored message.Fields:
id: Vec<u8>- Message IDgroup_id: Vec<u8>- Parent group IDdecrypted_message_bytes: Vec<u8>- Message contentsent_at_ns: i64- Send timestampsender_inbox_id: String- Sender’s inbox IDkind: GroupMessageKind- Message typedelivery_status: DeliveryStatus- Delivery statecontent_type: ContentType- Content encoding
Stored installation identity.Fields:
inbox_id: String- Associated inbox IDinstallation_keys: Vec<u8>- Installation key bytescredential_bytes: Vec<u8>- MLS credentialnext_key_package_rotation_ns: i64- Next rotation time
Consent state for a contact or group.Fields:
entity_type: ConsentType- Address, Group, or Inboxstate: ConsentState- Allowed, Denied, or Unknownentity: String- Entity identifier
Database Traits
Trait for storing entities in the database.
Trait for fetching entities from the database.
Trait for deleting entities.
Usage Examples
Creating an Encrypted Store
Storing and Fetching Groups
Storing Messages
Querying Messages
Managing Consent
Transactions
Using the Prelude
Storage Modules
Each storage module provides query traits and types:Query operations for groups.Methods:
query_groups()- List groups with filtersfind_groups()- Find specific groups
Query operations for messages.Methods:
query_group_messages()- List messagesget_latest_message()- Get most recent message
Query operations for identity.Methods:
fetch_identity()- Get installation identitystore_identity()- Store identity
Query operations for consent.Methods:
is_allowed()- Check if entity is allowedget_consent_record()- Get consent state
Migrations
Migrations are located incrates/xmtp_db/migrations/ and run automatically:
Error Types
Features
Testing utilities including in-memory databases and test helpers.
Tools for updating database schema definitions.
Platform Support
Native
- Uses SQLCipher with bundled vendored OpenSSL
- Connection pooling via r2d2
- Full encryption support
WASM
- Uses sqlite-wasm-rs
- Single-threaded connections
- Limited encryption (browser security model)
Testing Utilities
Withtest-utils feature:
Performance Considerations
Indexing
The schema includes indexes on commonly queried fields:- Group ID and creation time
- Message sent_at and sender
- Consent entity lookups
