Skip to main content
LibXMTP uses SQLite for local storage and supports optional encryption via SQLCipher. This guide covers enabling encryption, managing keys, and best practices for securing user data.

Overview

Database encryption protects:
  • Message history and content
  • Group metadata and membership
  • User identity and installation keys
  • Consent preferences
  • All local XMTP data
Important: Database encryption is strongly recommended for production applications. However, key derivation, storage, and secure management are the responsibility of the application developer.

Enabling Encryption

Basic Encryption Setup

Provide a 32-byte encryption key when creating the database:

Without Encryption

For testing or non-sensitive environments:
If you create a database without encryption, you cannot add encryption later without recreating the database.

Key Generation

Secure Random Key

Generate a cryptographically secure key:

Derive from Password

Derive a key from a user password using a KDF:
Always use a proper Key Derivation Function (KDF) like Argon2, PBKDF2, or scrypt. Never use plain password hashing algorithms like SHA-256 for key derivation.

Platform-Specific Storage

Store encryption keys securely using platform keychains:

SQLCipher Configuration

Verify SQLCipher Support

Check if SQLCipher is available:

Custom Cipher Settings

SQLCipher uses secure defaults, but you can customize if needed:
The default SQLCipher settings provide strong security. Only modify cipher settings if you have specific security requirements and understand the implications.

TypeScript (Node.js) Examples

Key Rotation

SQLCipher does not support online key rotation. To change encryption keys, you must create a new database and migrate data.

Migrate to New Key

Best Practices

Platform Considerations

Native (iOS/Android)

SQLCipher is fully supported:

WebAssembly

Encryption is not supported in WASM:
In WASM environments, the database is stored in IndexedDB without encryption. Consider this when handling sensitive data in web applications.

Troubleshooting

Database Won’t Open

Performance Impact

Encryption adds minimal overhead:

Key Size Errors

Next Steps

Creating Clients

Learn how to create clients with encrypted storage

Consent Management

Manage user preferences and privacy