Expand description
Event lifecycle management for Matrix room events.
This crate provides a type-safe implementation of the complete Matrix room event lifecycle, from initial draft to verified persistence. The implementation follows the ADR “ESIG-RDAC” and uses the type system to enforce correct state transitions.
§Architecture
Events progress through four distinct states, each represented by a different type:
RoomEvent<Draft>: Raw user intent (content + type only)RoomEvent<Contextualized>: Contextualized in the DAG with auth/prev eventsRoomEvent<Signed>: Signed with event ID and signaturesRoomEvent<Verified>: Verified signatures and authorization
State transitions are enforced at compile-time, preventing common errors like attempting to verify an event which has not been signed yet or storing an unverified event.
§Lifecycle Flow
The typical lifecycle flows are:
Local Event Creation (Client → Database):
- Client Request →
Draft(vianew()) Draft→Contextualized(viacontextualize())Contextualized→Verified(viasign()- Signatures are trivially valid, auth rules are checked)Verified→ Database
Federation Event Reception (Federation → Database):
- Federation Input →
Signed Signed→Verified(viaverify()- explicitly verified)Verified→ Database
Event Distribution (Database → Clients/Federation):
- Database →
Verified Verified→ PDU (viato_pdu()for federation)Verified→ Client Event (viato_client_event()for client API)
§Room Version Support
This crate supports multiple Matrix room versions (V1, V3-V11, V12+) with version-specific handling of:
- Event ID format (included in PDU vs. computed from reference hash)
- Event reference format (hashed vs. plain)
- Authorization rules
- Field requirements (e.g.,
room_idinm.room.createevents)
Room version rules are configurable via
[ruma::room_version_rules::RoomVersionRules].
§Verifying an event from federation
use std::collections::HashMap;
use arminius_lib_events::{RoomEvent, Signed, Verified, VerifiedFields as _};
use arminius_lib_events::errors::ValidationError;
use ruma::{events::StateEventType, signatures::PublicKeyMap};
// Verify signatures and authorization against room state
let verified = signed.verify(
&previous_state,
&auth_events,
¤t_state,
&required_verify_keys,
)?;
// Check if the event was soft-failed
if verified.soft_failed() {
// Event passed auth against its own auth_events and previous_state
// but failed against current room state. Persist it, but don't add
// to leaf events or relay to clients.
}
§Error Handling
The crate provides specific error types for different failure modes:
errors::SigningError: Canonicalization, signing, or event ID generation failureserrors::ValidationError: Signature verification, authorization, or key fetch failureserrors::EventConversionError: PDU/client event serialization failures
Modules§
- conversions 🔒
- Helper functions for converting events between different formats.
- errors
- Error types for event validation, signing, and conversion operations.
- event_
id 🔒 - Event ID derivation from federation PDUs.
- fields 🔒
- Utility traits for RoomEvents, which allows accessing fields that would otherwise be deep inside the structs
- pdu 🔒
- PDU (Persistent Data Unit) representations for different room version formats.
- redaction 🔒
- Event redaction functionality.
- states 🔒
- Event lifecycle state type definitions.
- traits 🔒
- Public traits defining shared behavior.
- transitions 🔒
- Core
RoomEvent<S>type and state transition logic. - types 🔒
- Foundational type definitions and aliases used throughout the crate.
Structs§
- Client
Context - Client context used when converting to client events.
- Contextualized
- State representing an event contextualized in the room DAG.
- Draft
- State representing a draft event containing only user intent.
- Event
Format Options - Event format options derived from room version rules.
- Event
Hash - Content hashes for a Matrix event.
- Hashed
Event Ref - An event reference with content hash (room versions 1-2).
- PduCommon
- Common PDU fields independent of room version format.
- Room
Event - A Matrix room event in a specific lifecycle state.
- Signed
- State representing a signed event with event ID and signatures.
- V1Pdu
- PDU representation for room version formats V1/V2.
- V3Pdu
- PDU representation for room version formats V3-V5.
- V12Pdu
- PDU representation for room version format V12.
- Verification
Metadata - Metadata recorded when attempting to verify the signature of an event.
- Verified
- State representing a fully verified event ready for persistence or distribution.
Enums§
- Event
Refs - Event references in a room-version-specific format.
- Known
Room Version Id - An alternative to [
ruma::RoomVersionId], including only types supported byarminius-lib-rooms - Parts
Verified - Describes how much of a ruma event was actually verified during a signature verification
- Pdu
- Supported PDU representations for room versions.
Constants§
- SUPPORTED_
ROOM_ VERSIONS - The room versions which are completely supported in arminius-lib-events
Traits§
- Contextualized
Fields - A trait which accesses fields of a contextualized event
- Draft
Fields - A trait which accesses fields of a draft event
- Redactable
- Trait for events that can be redacted in-place.
- Signed
Fields - A trait which accesses fields of a signed event
- Verified
Fields - A trait which accesses fields of a verified event
Functions§
- event_
id_ from_ pdu - Resolves the event ID for a federation PDU.
Type Aliases§
- Client
Event - Client event representation.