Architectural Overview
Core Layers
Language Bindings
LibXMTP provides native bindings for multiple platforms:- Mobile (
bindings/mobile): Uses UniFFI to generate Kotlin (Android) and Swift (iOS) bindings - WebAssembly (
bindings/wasm): Browser and Web Worker support - Node.js (
bindings/node): Uses NAPI for native Node.js modules
Core Client Layer (xmtp_mls)
Thexmtp_mls crate contains the main application logic:
- Group creation and management
- Message sending and receiving
- Synchronization with the network
- Intent processing and publishing
- Member management
Foundation Crates
xmtp_api
Defines theXmtpApi trait for network communication. Implementations include:
xmtp_api_grpc: gRPC-based API client for centralized infrastructurexmtp_api_d14n: Decentralized API implementation
xmtp_db
Provides encrypted SQLite storage using Diesel ORM:- Group metadata and membership
- Message storage with delivery status
- Identity updates and associations
- Key packages and cryptographic material
xmtp_id
Implements the identity system based on XIP-46:- Inbox ID management
- Wallet and installation associations
- Signature verification (EOA, smart contracts, legacy V2)
- Association state tracking
xmtp_proto
Protocol Buffer definitions for:- API messages (group operations, welcomes, key packages)
- Identity updates and associations
- Content types and encoded messages
xmtp_cryptography
Cryptographic primitives and operations:- Key generation and management
- Signature creation and verification
- ECDSA recovery for wallet signatures
Key Design Patterns
Generic Client with Context
TheClient<Context> type is parameterized by context, enabling different configurations:
- Testing with mock implementations
- Different storage backends
- Alternative API clients
- Platform-specific optimizations
Shared Context (XmtpMlsLocalContext)
TheXmtpMlsLocalContext centralizes dependencies:
Builder Pattern for Client Construction
TheClientBuilder provides fluent API for configuration:
Trait Abstractions
Core traits enable pluggable implementations:XmtpApi: Network communicationXmtpDb: Database operationsXmtpMlsStorageProvider: MLS key storageSmartContractSignatureVerifier: Smart contract wallet verificationInboxOwner: Identity operations (for testing)
Platform-Specific Code
Macros handle platform differences:Background Workers
TheWorkerRunner manages background tasks:
- SyncWorker: Polls network for new messages and welcomes
- KeyPackagesCleanerWorker: Rotates and cleans up key packages
- DisappearingMessagesWorker: Handles ephemeral message lifecycle
- PendingSelfRemoveWorker: Processes pending member removals
- CommitLogWorker: Publishes commit logs (when enabled)
- TaskWorker: Executes queued background tasks
Storage Architecture
LibXMTP uses a dual-storage approach:Application Database (xmtp_db)
- Group metadata and conversation list
- Decrypted message content
- Identity updates and associations
- Consent records and preferences
MLS Storage (SqlKeyStore)
- OpenMLS group state and epochs
- Cryptographic key material
- Key packages (HPKE init keys)
- Ratchet tree state
Error Handling
LibXMTP uses structured error types with error codes:RetryableError to indicate transient failures that can be retried.
Testing Infrastructure
The architecture supports comprehensive testing:- Mock implementations:
MockApi,MockScwVerifierfor unit tests - Test utilities:
tester!macro creates test clients with wallets - Cross-platform tests:
#[xmtp_common::test]runs on native and WASM - Wallet utilities:
WalletTestExttrait for test wallet operations
All tests use the
#[xmtp_common::test(unwrap_try = true)] attribute instead of #[test] to ensure cross-platform compatibility.Related Concepts
- MLS Protocol - Detailed MLS implementation
- Identity System - Inbox and association management
- Client Lifecycle - Initialization and registration
- Groups and Conversations - Group operations
