Skip to main content

Overview

The xmtp_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

EncryptedMessageStore<DefaultDatabase>
The default platform-specific encrypted message store implementation.
DbConnection
Default database connection type for the store.
SqlKeyStore
Default MLS key store implementation backed by SQL.
struct
Main encrypted storage implementation parameterized by database type.
struct
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 state management:
  • Per-contact consent
  • Per-group consent
  • Allow/deny lists

Main Types and Traits

trait
Core trait for XMTP database operations.Associated Types:
  • DbQuery - Query type for database operations
Key Methods:
  • raw_query_read() - Execute read-only queries
  • raw_query_write() - Execute write queries
  • transaction() - Execute transactional operations
struct
Encrypted storage implementation.Type Parameters:
  • D: Database - Underlying database type
Methods:
  • new() - Create new store with encryption key
  • new_unencrypted() - Create unencrypted store (testing only)
  • conn() - Get database connection
struct
Represents a stored group conversation.Fields:
  • id: Vec<u8> - Group ID
  • created_at_ns: i64 - Creation timestamp
  • membership_state: GroupMembershipState - Current membership
  • conversation_type: ConversationType - Group or DM
  • added_by_inbox_id: String - Who added this installation
struct
Represents a stored message.Fields:
  • id: Vec<u8> - Message ID
  • group_id: Vec<u8> - Parent group ID
  • decrypted_message_bytes: Vec<u8> - Message content
  • sent_at_ns: i64 - Send timestamp
  • sender_inbox_id: String - Sender’s inbox ID
  • kind: GroupMessageKind - Message type
  • delivery_status: DeliveryStatus - Delivery state
  • content_type: ContentType - Content encoding
struct
Stored installation identity.Fields:
  • inbox_id: String - Associated inbox ID
  • installation_keys: Vec<u8> - Installation key bytes
  • credential_bytes: Vec<u8> - MLS credential
  • next_key_package_rotation_ns: i64 - Next rotation time
Consent state for a contact or group.Fields:
  • entity_type: ConsentType - Address, Group, or Inbox
  • state: ConsentState - Allowed, Denied, or Unknown
  • entity: String - Entity identifier

Database Traits

trait
Trait for storing entities in the database.
trait
Trait for fetching entities from the database.
trait
Trait for deleting entities.

Usage Examples

Creating an Encrypted Store

Storing and Fetching Groups

Storing Messages

Querying Messages

Transactions

Using the Prelude

Storage Modules

Each storage module provides query traits and types:
trait
Query operations for groups.Methods:
  • query_groups() - List groups with filters
  • find_groups() - Find specific groups
trait
Query operations for messages.Methods:
  • query_group_messages() - List messages
  • get_latest_message() - Get most recent message
trait
Query operations for identity.Methods:
  • fetch_identity() - Get installation identity
  • store_identity() - Store identity
Query operations for consent.Methods:
  • is_allowed() - Check if entity is allowed
  • get_consent_record() - Get consent state

Migrations

Migrations are located in crates/xmtp_db/migrations/ and run automatically:

Error Types

Features

feature
Testing utilities including in-memory databases and test helpers.
feature
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

With test-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

Connection Pooling

On native platforms, use connection pools for concurrent access:

Batch Operations

Use transactions for batch inserts: