1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//! Core library for DE-MLS group management.
//!
//! This module provides the reusable API for building decentralized MLS chat applications.
//! It handles MLS cryptography, consensus voting, and message routing while leaving
//! transport, UI, and state management to the application layer.
//!
//! # Architecture Overview
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │ Your Application │
//! │ ┌─────────────┐ ┌──────────────┐ ┌───────────────────┐ │
//! │ │ Transport │ │ UI/State │ │ GroupEventHandler │ │
//! │ │ (Waku, etc) │ │ Management │ │ (your impl) │ │
//! │ └──────┬──────┘ └──────┬───────┘ └─────────┬─────────┘ │
//! └─────────┼────────────────┼────────────────────┼────────────┘
//! │ │ │
//! ┌─────────┼────────────────┼────────────────────┼────────────┐
//! │ ▼ ▼ ▼ │
//! │ ┌─────────────────────────────────────────────────────┐ │
//! │ │ de_mls::core │ │
//! │ │ • Group lifecycle (create, join, leave) │ │
//! │ │ • Message encryption/decryption │ │
//! │ │ • Consensus voting integration │ │
//! │ │ • Proposal management │ │
//! │ └─────────────────────────────────────────────────────┘ │
//! │ │ │
//! │ ┌─────────────────┼─────────────────┐ │
//! │ ▼ ▼ ▼ │
//! │ ┌────────────┐ ┌───────────────┐ ┌────────────────┐ │
//! │ │ mls_crypto │ │ hashgraph-like│ │ Protobuf msgs │ │
//! │ │ (OpenMLS) │ │ consensus │ │ (de_mls.v1) │ │
//! │ └────────────┘ └───────────────┘ └────────────────┘ │
//! └────────────────────────────────────────────────────────────┘
//! ```
//!
//! # What You Implement
//!
//! To build a chat application, you need to implement:
//!
//! 1. **`GroupEventHandler`** - Required trait for handling output events:
//! - `on_outbound()` - Send encrypted packets to the network
//! - `on_app_message()` - Deliver messages to your UI
//! - `on_leave_group()` / `on_joined_group()` - Update your state
//! - `on_error()` - Handle background operation failures
//!
//! 2. **Transport Layer** - How packets travel between peers (Waku, libp2p, etc.)
//!
//! 3. **State Management** - Track which groups exist, their states, UI updates
//!
//! # What's Provided
//!
//! - **MLS Operations** - Group creation, joining, message encryption via OpenMLS
//! - **Consensus Voting** - Proposal creation, vote casting, result handling
//! - **Message Types** - Protobuf definitions for all protocol messages
//! - **Default Services** - `DefaultProvider` bundles standard implementations
//!
//! # Core Concepts
//!
//! ## GroupHandle
//!
//! `GroupHandle` is the per-group state container. It holds:
//! - MLS cryptographic state (encrypted, thread-safe via `Arc<Mutex>`)
//! - Steward status (who can commit membership changes)
//! - Pending and approved proposals for the current epoch
//!
//! ## Steward Role
//!
//! One member per group is the "steward" who:
//! - Receives key packages from users wanting to join
//! - Batches approved proposals into MLS commits
//! - Sends welcome messages to new members
//!
//! ## ProcessResult & DispatchAction
//!
//! When processing inbound messages:
//! 1. Call `process_inbound()` → returns `ProcessResult`
//! 2. Call `dispatch_result()` → returns `DispatchAction`
//! 3. Handle the action in your application layer
//!
//! # Quick Start
//!
//! ```ignore
//! use de_mls::core::{
//! create_group, build_message, process_inbound, dispatch_result,
//! GroupEventHandler, GroupHandle, ProcessResult, DispatchAction,
//! DefaultProvider,
//! };
//!
//! // 1. Implement GroupEventHandler
//! struct MyHandler { /* transport, ui channels */ }
//!
//! #[async_trait]
//! impl GroupEventHandler for MyHandler {
//! async fn on_outbound(&self, group: &str, packet: OutboundPacket) -> Result<String, CoreError> {
//! self.transport.send(packet).await
//! }
//! async fn on_app_message(&self, group: &str, msg: AppMessage) -> Result<(), CoreError> {
//! self.ui.send(msg).await
//! }
//! // ... other methods
//! }
//!
//! // 2. Create a group (as steward)
//! let handle = create_group("my-chat", &identity_service)?;
//!
//! // 3. Send a message
//! let app_msg = ConversationMessage { message: b"Hello".to_vec(), .. }.into();
//! let packet = build_message(&handle, &identity_service, &app_msg).await?;
//! handler.on_outbound("my-chat", packet).await?;
//!
//! // 4. Process inbound messages (in your receive loop)
//! let result = process_inbound(&mut handle, &payload, subtopic, &mls, &identity).await?;
//! let action = dispatch_result::<DefaultProvider>(&handle, "my-chat", result, &consensus, &handler, &identity).await?;
//!
//! match action {
//! DispatchAction::Done => { /* nothing more to do */ }
//! DispatchAction::StartVoting(request) => { /* spawn voting task */ }
//! DispatchAction::GroupUpdated => { /* refresh UI */ }
//! DispatchAction::LeaveGroup => { /* cleanup group state */ }
//! DispatchAction::JoinedGroup => { /* update state to Working */ }
//! }
//! ```
//!
//! # Module Organization
//!
//! - `api` - Core group operations (create, join, send, process)
//! - `consensus` - Voting workflow and dispatch
//! - `events` - `GroupEventHandler` trait
//! - `provider` - `DeMlsProvider` trait and `DefaultProvider`
//! - `types` - `ProcessResult`, message conversions
pub use ;
pub use ;
pub use CoreError;
pub use GroupEventHandler;
pub use GroupHandle;
pub use ;
pub use ;
pub use ;