xmtp 0.8.2

Safe, ergonomic Rust client SDK for the XMTP messaging protocol
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! SDK types: enumerations, option structs, data structs, and signer trait.

/// XMTP network environment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Env {
    /// Local development node.
    Local,
    /// Shared development environment.
    #[default]
    Dev,
    /// Production environment.
    Production,
}

impl Env {
    /// gRPC API URL for this environment.
    #[must_use]
    pub const fn url(self) -> &'static str {
        match self {
            Self::Local => "http://localhost:5556",
            Self::Dev => "https://grpc.dev.xmtp.network:443",
            Self::Production => "https://grpc.production.xmtp.network:443",
        }
    }

    /// Whether this environment uses TLS.
    #[must_use]
    pub const fn is_secure(self) -> bool {
        !matches!(self, Self::Local)
    }
}

macro_rules! ffi_enum {
    ($(#[$meta:meta])* $vis:vis enum $name:ident {
        $($(#[$vm:meta])* $variant:ident = $val:expr),* $(,)?
    }) => {
        $(#[$meta])*
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        #[repr(i32)]
        $vis enum $name { $($(#[$vm])* $variant = $val),* }

        impl $name {
            /// Convert from FFI `i32`. Returns `None` for unknown values.
            #[must_use]
            pub const fn from_ffi(v: i32) -> Option<Self> {
                match v { $($val => Some(Self::$variant),)* _ => None }
            }
        }
    };
}

ffi_enum! {
    /// Identifier kind.
    pub enum IdentifierKind {
        /// Externally-owned Ethereum account.
        Ethereum = 0,
        /// Passkey.
        Passkey = 1,
    }
}

ffi_enum! {
    /// Conversation type.
    pub enum ConversationType {
        /// Direct message.
        Dm = 0,
        /// Group conversation.
        Group = 1,
        /// Internal sync group.
        Sync = 2,
        /// One-shot conversation.
        Oneshot = 3,
    }
}

ffi_enum! {
    /// Consent state.
    pub enum ConsentState {
        /// Not yet determined.
        Unknown = 0,
        /// Explicitly allowed.
        Allowed = 1,
        /// Explicitly denied.
        Denied = 2,
    }
}

ffi_enum! {
    /// Consent entity type.
    pub enum ConsentEntityType {
        /// Group ID.
        GroupId = 0,
        /// Inbox ID.
        InboxId = 1,
    }
}

ffi_enum! {
    /// Message kind.
    pub enum MessageKind {
        /// Application-level content.
        Application = 0,
        /// MLS membership change.
        MembershipChange = 1,
    }
}

ffi_enum! {
    /// Message delivery status.
    pub enum DeliveryStatus {
        /// Not yet published.
        Unpublished = 0,
        /// Published to the network.
        Published = 1,
        /// Failed to publish.
        Failed = 2,
    }
}

ffi_enum! {
    /// Group member permission level.
    pub enum PermissionLevel {
        /// Regular member.
        Member = 0,
        /// Administrator.
        Admin = 1,
        /// Super administrator.
        SuperAdmin = 2,
    }
}

ffi_enum! {
    /// Group permissions preset.
    pub enum GroupPermissionsPreset {
        /// All members have equal permissions.
        AllMembers = 0,
        /// Only admins can modify the group.
        AdminOnly = 1,
        /// Custom permission set.
        Custom = 2,
    }
}

ffi_enum! {
    /// Group membership state.
    pub enum MembershipState {
        /// Allowed (active member).
        Allowed = 0,
        /// Rejected.
        Rejected = 1,
        /// Pending approval.
        Pending = 2,
        /// Restored after removal.
        Restored = 3,
        /// Pending removal.
        PendingRemove = 4,
    }
}

ffi_enum! {
    /// Sort direction for message listing.
    pub enum SortDirection {
        /// Ascending (oldest first).
        Ascending = 0,
        /// Descending (newest first).
        Descending = 1,
    }
}

ffi_enum! {
    /// Permission policy values (0-based, matching C read struct).
    ///
    /// **Note:** The FFI *write* function uses 1-based values.
    /// Use [`PermissionPolicy::to_write_i32`] when calling write APIs.
    pub enum PermissionPolicy {
        /// Allow all.
        Allow = 0,
        /// Deny all.
        Deny = 1,
        /// Admin only.
        AdminOnly = 2,
        /// Super admin only.
        SuperAdminOnly = 3,
        /// Policy does not exist (read-only, set by protocol).
        DoesNotExist = 4,
        /// Other / unknown policy (read-only, set by protocol).
        Other = 5,
    }
}

impl PermissionPolicy {
    /// Convert to the 1-based i32 value expected by FFI write functions.
    #[must_use]
    pub const fn to_write_i32(self) -> i32 {
        self as i32 + 1
    }
}

ffi_enum! {
    /// Permission update type.
    pub enum PermissionUpdateType {
        /// Add member.
        AddMember = 1,
        /// Remove member.
        RemoveMember = 2,
        /// Add admin.
        AddAdmin = 3,
        /// Remove admin.
        RemoveAdmin = 4,
        /// Update metadata.
        UpdateMetadata = 5,
    }
}

/// Metadata field names for [`PermissionUpdateType::UpdateMetadata`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MetadataField {
    /// Group name.
    GroupName,
    /// Group description.
    Description,
    /// Group image URL.
    ImageUrl,
    /// Group pinned frame URL.
    PinnedFrameUrl,
    /// Custom app data.
    AppData,
    /// Message disappearing settings.
    MessageDisappearing,
}

impl MetadataField {
    /// Returns the FFI string key for this metadata field.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::GroupName => "group_name",
            Self::Description => "description",
            Self::ImageUrl => "group_image_url_square",
            Self::PinnedFrameUrl => "group_pinned_frame_url",
            Self::AppData => "app_data",
            Self::MessageDisappearing => "message_disappearing",
        }
    }
}

/// An account identifier (address + kind).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AccountIdentifier {
    /// The account address or public key.
    pub address: String,
    /// The kind of identifier.
    pub kind: IdentifierKind,
}

/// Options for creating a group conversation.
#[derive(Debug, Clone, Default)]
pub struct CreateGroupOptions {
    /// Permission preset.
    pub permissions: Option<GroupPermissionsPreset>,
    /// Group name.
    pub name: Option<String>,
    /// Group description.
    pub description: Option<String>,
    /// Group image URL.
    pub image_url: Option<String>,
    /// Custom app data (max 8192 bytes).
    pub app_data: Option<String>,
    /// Disappearing message settings. `None` = disabled.
    pub disappearing: Option<DisappearingSettings>,
}

/// Options for creating a DM conversation.
#[derive(Debug, Clone, Copy, Default)]
pub struct CreateDmOptions {
    /// Disappearing message settings. `None` = disabled.
    pub disappearing: Option<DisappearingSettings>,
}

/// Options for listing messages.
#[derive(Debug, Clone, Copy, Default)]
pub struct ListMessagesOptions {
    /// Only messages sent after this timestamp (ns).
    pub sent_after_ns: i64,
    /// Only messages sent before this timestamp (ns).
    pub sent_before_ns: i64,
    /// Maximum number of messages to return.
    pub limit: i64,
    /// Sort direction.
    pub direction: Option<SortDirection>,
    /// Filter by delivery status. `None` = all.
    pub delivery_status: Option<DeliveryStatus>,
    /// Filter by message kind. `None` = all.
    pub kind: Option<MessageKind>,
}

/// Sort order for listing conversations.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ConversationOrderBy {
    /// Order by creation timestamp (default).
    #[default]
    CreatedAt = 0,
    /// Order by last activity timestamp.
    LastActivity = 1,
}

/// Options for listing conversations.
#[derive(Debug, Clone, Default)]
pub struct ListConversationsOptions {
    /// Filter by conversation type. `None` = all.
    pub conversation_type: Option<ConversationType>,
    /// Maximum number of conversations.
    pub limit: i64,
    /// Only conversations created after this timestamp (ns).
    pub created_after_ns: i64,
    /// Only conversations created before this timestamp (ns).
    pub created_before_ns: i64,
    /// Only conversations with last activity after this timestamp (ns).
    pub last_activity_after_ns: i64,
    /// Only conversations with last activity before this timestamp (ns).
    pub last_activity_before_ns: i64,
    /// Filter by consent states. Empty = all.
    pub consent_states: Vec<ConsentState>,
    /// Sort order for results.
    pub order_by: ConversationOrderBy,
    /// Whether to include duplicate DMs.
    pub include_duplicate_dms: bool,
}

/// Options for sending a message.
#[derive(Debug, Clone, Copy)]
pub struct SendOptions {
    /// Whether to include in push notifications (default: `true`).
    pub should_push: bool,
}

impl Default for SendOptions {
    fn default() -> Self {
        Self { should_push: true }
    }
}

/// Disappearing message settings.
#[derive(Debug, Clone, Copy, Default)]
pub struct DisappearingSettings {
    /// Start timestamp (ns).
    pub from_ns: i64,
    /// Duration (ns).
    pub in_ns: i64,
}

/// Full permission policy set for a conversation.
#[derive(Debug, Clone, Copy)]
pub struct PermissionPolicySet {
    /// Policy for adding members.
    pub add_member: PermissionPolicy,
    /// Policy for removing members.
    pub remove_member: PermissionPolicy,
    /// Policy for adding admins.
    pub add_admin: PermissionPolicy,
    /// Policy for removing admins.
    pub remove_admin: PermissionPolicy,
    /// Policy for updating the group name.
    pub update_group_name: PermissionPolicy,
    /// Policy for updating the group description.
    pub update_group_description: PermissionPolicy,
    /// Policy for updating the group image URL.
    pub update_group_image_url: PermissionPolicy,
    /// Policy for updating disappearing message settings.
    pub update_message_disappearing: PermissionPolicy,
    /// Policy for updating app data.
    pub update_app_data: PermissionPolicy,
}

/// Group permissions (preset + full policy set).
#[derive(Debug, Clone, Copy)]
pub struct Permissions {
    /// The permissions preset used when creating the group.
    pub preset: GroupPermissionsPreset,
    /// The full set of per-action policies.
    pub policies: PermissionPolicySet,
}

/// Conversation metadata (creator + type).
#[derive(Debug, Clone)]
pub struct ConversationMetadata {
    /// The inbox ID of the conversation creator.
    pub creator_inbox_id: String,
    /// The type of conversation.
    pub conversation_type: ConversationType,
}

/// A single cursor entry for debug info.
#[derive(Debug, Clone, Copy)]
pub struct Cursor {
    /// Originator node ID.
    pub originator_id: u32,
    /// Sequence number within the originator.
    pub sequence_id: u64,
}

/// Conversation debug information.
#[derive(Debug, Clone)]
pub struct ConversationDebugInfo {
    /// Current MLS epoch.
    pub epoch: u64,
    /// Whether a fork has been detected.
    pub maybe_forked: bool,
    /// Human-readable fork details.
    pub fork_details: Option<String>,
    /// Whether the commit log is forked. `None` = unknown.
    pub is_commit_log_forked: Option<bool>,
    /// Local commit log summary.
    pub local_commit_log: Option<String>,
    /// Remote commit log summary.
    pub remote_commit_log: Option<String>,
    /// Cursor entries for each originator.
    pub cursors: Vec<Cursor>,
}

/// A single HMAC key entry.
#[derive(Debug, Clone)]
pub struct HmacKey {
    /// The raw key bytes.
    pub key: Vec<u8>,
    /// The epoch this key belongs to.
    pub epoch: i64,
}

/// HMAC keys for a conversation group.
#[derive(Debug, Clone)]
pub struct HmacKeyEntry {
    /// Hex-encoded group ID.
    pub group_id: String,
    /// HMAC keys for each epoch.
    pub keys: Vec<HmacKey>,
}

/// Per-inbox last-read timestamp.
#[derive(Debug, Clone)]
pub struct LastReadTime {
    /// The inbox ID.
    pub inbox_id: String,
    /// Last-read timestamp in nanoseconds.
    pub timestamp_ns: i64,
}

/// MLS API call statistics.
#[derive(Debug, Clone, Copy, Default)]
pub struct ApiStats {
    /// Number of `upload_key_package` calls.
    pub upload_key_package: i64,
    /// Number of `fetch_key_package` calls.
    pub fetch_key_package: i64,
    /// Number of `send_group_messages` calls.
    pub send_group_messages: i64,
    /// Number of `send_welcome_messages` calls.
    pub send_welcome_messages: i64,
    /// Number of `query_group_messages` calls.
    pub query_group_messages: i64,
    /// Number of `query_welcome_messages` calls.
    pub query_welcome_messages: i64,
    /// Number of `subscribe_messages` calls.
    pub subscribe_messages: i64,
    /// Number of `subscribe_welcomes` calls.
    pub subscribe_welcomes: i64,
    /// Number of `publish_commit_log` calls.
    pub publish_commit_log: i64,
    /// Number of `query_commit_log` calls.
    pub query_commit_log: i64,
    /// Number of `get_newest_group_message` calls.
    pub get_newest_group_message: i64,
}

/// Identity API call statistics.
#[derive(Debug, Clone, Copy, Default)]
pub struct IdentityStats {
    /// Number of `publish_identity_update` calls.
    pub publish_identity_update: i64,
    /// Number of `get_identity_updates_v2` calls.
    pub get_identity_updates_v2: i64,
    /// Number of `get_inbox_ids` calls.
    pub get_inbox_ids: i64,
    /// Number of `verify_smart_contract_wallet_signature` calls.
    pub verify_smart_contract_wallet_signature: i64,
}

/// Key package status for an installation.
#[derive(Debug, Clone)]
pub struct KeyPackageStatus {
    /// Hex-encoded installation ID.
    pub installation_id: String,
    /// Whether the key package is valid.
    pub valid: bool,
    /// `not_before` timestamp (0 if unavailable).
    pub not_before: u64,
    /// `not_after` timestamp (0 if unavailable).
    pub not_after: u64,
    /// Validation error message, if any.
    pub validation_error: Option<String>,
}

/// Result of a sync operation.
#[derive(Debug, Clone, Copy)]
pub struct SyncResult {
    /// Number of conversations successfully synced.
    pub synced: u32,
    /// Number of conversations eligible for sync.
    pub eligible: u32,
}

/// Snapshot of an inbox's identity state.
#[derive(Debug, Clone)]
pub struct InboxState {
    /// The inbox ID.
    pub inbox_id: String,
    /// Recovery identifier.
    pub recovery_identifier: String,
    /// Associated account identifiers.
    pub identifiers: Vec<String>,
    /// Installation IDs (hex).
    pub installation_ids: Vec<String>,
}

/// Trait for signing messages during XMTP identity operations.
pub trait Signer: Send + Sync {
    /// The account identifier for this signer.
    fn identifier(&self) -> AccountIdentifier;

    /// Sign the given text and return raw signature bytes.
    ///
    /// # Errors
    ///
    /// Returns an error if signing fails (e.g. key unavailable or hardware error).
    fn sign(&self, text: &str) -> crate::error::Result<Vec<u8>>;

    /// Whether this is a smart contract wallet (ERC-1271). Default: `false`.
    fn is_smart_wallet(&self) -> bool {
        false
    }

    /// EVM chain ID for SCW verification.
    fn chain_id(&self) -> u64 {
        1
    }

    /// Block number for SCW verification. 0 = latest.
    fn block_number(&self) -> u64 {
        0
    }
}