A hands-on learning project to explore the Matrix protocol through experimentation. This repository demonstrates how to set up a local Matrix homeserver, build client applications, and test end-to-end encryption capabilities.
Goal: Learn the basics of the Matrix protocol through practical experiments, from server setup to encrypted messaging.
What's Included:
- Local Matrix homeserver configuration (Synapse)
- Client utilities for user registration and basic operations
- End-to-end encryption test programs using the matrix-nio library
- Multiple test scenarios demonstrating different aspects of the protocol
.
├── server/ # Synapse homeserver configuration
│ ├── homeserver.yaml # Main server configuration
│ ├── homeserver.db # SQLite database (persists state)
│ ├── log.yaml # Logging configuration
│ └── requirements.txt # Server dependencies
│
├── client/ # Core client utilities
│ ├── register_user.py # User registration utility
│ ├── sync_and_invite.py # Sync and room invitation logic
│ ├── show_events.py # Event display utility
│ ├── encryption.py # Encryption helper functions
│ └── test_e2e.py # Unit tests for E2EE
│
├── e2etest/ # End-to-end encryption test (Python)
│ ├── README.md # Detailed E2EE test documentation
│ ├── init.py # Register users and setup E2EE keys
│ ├── setup.py # Create encrypted room and invite users
│ ├── send.py # Send encrypted message
│ ├── recv.py # Receive and decrypt message
│ ├── userconfig.json # Generated user credentials
│ └── roomconfig.json # Generated room configuration
│
├── e2eetest-js/ # E2EE tests using JavaScript/Node.js
│ └── (JavaScript test programs)
│
├── nio_store/ # Encryption state persistence
│ └── (matrix-nio encrypted state storage)
│
└── venv/ # Python virtual environment
- Python 3.7+
- A running Synapse Matrix homeserver (configured at
localhost:8008) - matrix-nio library (
pip install matrix-nio)
The Synapse homeserver should already be configured and running. To verify it's running:
curl http://localhost:8008/_matrix/client/versionsRun the e2etest program suite to test end-to-end encryption:
cd e2etest
# Step 1: Register two users and setup encryption keys
python3 init.py
# Step 2: Create an encrypted room and invite the second user
python3 setup.py
# Step 3: In one terminal, start receiving messages
python3 recv.py
# Step 4: In another terminal, send an encrypted message
python3 send.pySee e2etest/README.md for detailed documentation on each program.
Register a new user:
python3 client/register_user.py alice "Alice Smith"Show recent events in a room:
python3 client/show_events.py '!roomid:localhost' --user-id '@alice:localhost'- Homeserver: A server that manages user accounts and room state (Synapse at
localhost:8008) - User ID: Format
@username:servername(e.g.,@alice:localhost) - Room ID: Format
!roomid:servername(e.g.,!abc123:localhost) - Events: Messages and state changes that form the immutable history of rooms
- Sync: The process of retrieving new events from the server
- Protocol: Olm/Megolm encryption
- Device Keys: Each client device has a unique identity
- One-Time Keys: Used to establish initial encrypted sessions
- Megolm Sessions: Room-wide symmetric keys for efficient group messaging
- Storage: Encryption state persisted in
nio_store/directory
This project uses matrix-nio, a modern async Python library for Matrix:
- Advantages: Modern async/await syntax, active development, comprehensive E2EE support
- Documentation: https://matrix-nio.readthedocs.io/
- Source: https://github.com/poljar/matrix-nio
The server/ directory contains a Synapse Matrix homeserver setup:
- Configured to listen on
localhost:8008 - SQLite database for persistence
- Registration enabled for testing
- Logging configuration for debugging
Core Utilities (client/):
register_user.py: Registers a new user with a generated password hashsync_and_invite.py: Handles syncing with the server and sending invitationsshow_events.py: Displays recent events in a roomencryption.py: Helper functions for E2EE operations
E2EE Test Suite (e2etest/):
init.py: Creates two test users with encryption keyssetup.py: Creates an encrypted room and manages invitationssend.py: Sends encrypted messagesrecv.py: Receives and decrypts messages
The nio_store/ directory persists encryption state between runs:
- Device identities
- Encryption keys
- Trust relationships
- Session state
Important: Do not delete this directory between send/recv runs, as it breaks the encryption trust chain.
python3 client/test_e2e.pytail -f server/logs/*.logcurl http://localhost:8008/_synapse/admin/v1/users- Start Simple: Use
register_user.pyto understand basic user registration - Explore Events: Use
show_events.pyto see the event model - Sync Operations: Study
sync_and_invite.pyto understand the sync API - E2EE Basics: Run
e2etest/init.pyand examine how encryption keys are created - Encrypted Messaging: Complete the send/recv cycle to see E2EE in action
- Inspect State: Look at
userconfig.jsonandroomconfig.jsonto see the data model
- Server Config: See
server/homeserver.yamlfor Synapse configuration - E2EE Details: See
e2etest/README.mdfor comprehensive E2EE test documentation - Encryption Logic: See
client/encryption.pyfor E2EE implementation details - CLAUDE.md: Project rules and guidelines for development
Check if Synapse is running:
ps aux | grep synapseStart it if needed:
python -m synapse.app.homeserver --config-path server/homeserver.yaml- Ensure
nio_store/directory is not deleted between send/recv runs - Check that both users have completed the
init.pysetup - Verify device trust by checking the logs in
recv.py
- Check that the server is accessible at
localhost:8008 - Ensure the username is not already registered
- Try with a unique username format:
user_XXXXX
- Each
init.pyrun creates new users with random usernames - Multiple test runs can coexist without conflicts
- Messages are fully encrypted end-to-end; the server cannot read them
- Device verification is automatic in tests; production requires explicit verification
- The
nio_store/directory must not be deleted between send/recv runs
This project is for educational purposes and learning the Matrix protocol.