@kumiai/mls 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/group.d.ts CHANGED
@@ -1,339 +1,7 @@
1
- import { type DIDCache, type DIDResolver, type OwnIdentity, type SigningIdentity } from '@kokuin/token';
2
- import { type ClientState, type Credential, type IncomingMessageCallback, type KeyPackage, type MlsContext, type ProposalWithSender } from 'ts-mls';
3
- import { type GroupAnchor } from './anchor.js';
4
- import { type GroupMember, type MemberCredential } from './credential.js';
5
- import { type VerifiedLedgerEntry } from './ledger.js';
6
- import { type GroupPermission, type RosterState } from './roster.js';
7
- import type { GroupOptions, Invite, KeyPackageBundle } from './types.js';
8
- export declare function makeMLSCredential(identity: OwnIdentity): Credential;
9
- /**
10
- * Thrown by GroupHandle.processMessage when the active commit policy rejects
11
- * an incoming commit. The handle is left at its pre-commit epoch.
12
- */
13
- export declare class CommitRejectedError extends Error {
14
- #private;
15
- constructor(proposals: Array<ProposalWithSender>, senderLeafIndex?: number);
16
- get proposals(): Array<ProposalWithSender>;
17
- get senderLeafIndex(): number | undefined;
18
- }
19
- /**
20
- * A control-ledger entry as a handle holds it: the signed token — the canonical
21
- * persistent and wire form, the only thing that can be handed to another party —
22
- * paired with its verified form. The verified form is a one-way derivation (the
23
- * token cannot be reconstructed from it), so keeping only it would leave a handle
24
- * unable to forward the ledger it holds.
25
- */
26
- export type HeldLedgerEntry = {
27
- token: string;
28
- verified: VerifiedLedgerEntry;
29
- };
30
- /** A held entry paired with its content id — one position in the ledger log. */
31
- export type LedgerLogEntry = HeldLedgerEntry & {
32
- entryID: string;
33
- };
34
- export type GroupHandleParams = {
35
- state: ClientState;
36
- credential: MemberCredential;
37
- context: MlsContext;
38
- /** The control-ledger log this handle starts from, in enactment order. A handle
39
- * derived from another (commitInvite/removeMember) inherits the parent's, so the
40
- * roster it folds does not revert to the anchor alone. */
41
- ledger?: ReadonlyArray<LedgerLogEntry>;
42
- cache: DIDCache;
43
- resolver?: DIDResolver;
44
- /** Default commit policy applied by processMessage. */
45
- commitPolicy?: IncomingMessageCallback;
46
- /** Fetch control-ledger entry bodies the local ledger lacks (commit pre-pass). */
47
- resolveLedgerEntries?: (ids: Array<string>) => Promise<Array<string>>;
48
- /** Surface an accepted commit's notarized non-`group.role` entries to the consumer. */
49
- onLedgerEntries?: (entries: Array<VerifiedLedgerEntry>) => void;
50
- };
51
- /**
52
- * Mutable wrapper around MLS group state + Enkaku credential.
53
- */
54
- export declare class GroupHandle {
55
- #private;
56
- constructor(params: GroupHandleParams);
57
- get groupID(): string;
58
- get epoch(): bigint;
59
- get treeHash(): Uint8Array;
60
- get credential(): MemberCredential;
61
- get state(): ClientState;
62
- get context(): MlsContext;
63
- get cache(): DIDCache;
64
- get resolver(): DIDResolver | undefined;
65
- /** The commit policy enforced by processMessage, if any. Carried onto
66
- * handles derived from this one (commitInvite/removeMember). */
67
- get commitPolicy(): IncomingMessageCallback | undefined;
68
- /** The ledger-entry resolver, carried onto handles derived from this one. */
69
- get resolveLedgerEntries(): ((ids: Array<string>) => Promise<Array<string>>) | undefined;
70
- /** The accepted-commit entry sink, carried onto handles derived from this one. */
71
- get onLedgerEntries(): ((entries: Array<VerifiedLedgerEntry>) => void) | undefined;
72
- /** The genesis anchor seeded into this handle at construction. */
73
- get anchor(): GroupAnchor;
74
- /** The control-ledger log this handle holds, in enactment order, repeats and
75
- * all. Carried onto handles derived from this one (commitInvite/removeMember). */
76
- get ledger(): ReadonlyArray<LedgerLogEntry>;
77
- /** The ordered signed tokens this handle holds, including repeats — the ledger's
78
- * canonical persistent and wire form, and the list the authenticated head folds
79
- * over. Feeds createInvite, restoreGroup, and host persistence. The verified
80
- * entries are a derived cache with no export form: the only way into a ledger is
81
- * applyLedgerEntries, which re-verifies. */
82
- get ledgerTokens(): Array<string>;
83
- /** The control roster folded from the anchor and every applied ledger entry. */
84
- get roster(): RosterState;
85
- /**
86
- * Verify signed ledger tokens, append the valid ones to the log in the order
87
- * given, and refold the roster. Tokens that fail verification or whose groupID
88
- * does not match the group are dropped (defensive — this is the low-level apply
89
- * primitive; the commit pre-pass does the strict admin-issuer enforcement).
90
- * Every token is re-verified on the way in: no entry enters a ledger unverified,
91
- * whatever the import path.
92
- *
93
- * A token the log already holds is appended again rather than skipped. The log
94
- * is an ordered record of what each commit enacted, not a set of claims, and a
95
- * repeat is the only way to express a demotion back to a previously-held role.
96
- * Nothing replays a commit into it: MLS applies each commit exactly once,
97
- * restoreGroup replays a token list once, and processWelcome folds an invite once.
98
- * Runs serialized on the handle's mutex, like the other state-mutating operations.
99
- */
100
- applyLedgerEntries(tokens: Array<string>): Promise<void>;
101
- get memberCount(): number;
102
- findMemberLeafIndex(id: string): number | undefined;
103
- /**
104
- * Enumerate the group's current members from the ratchet tree, in ascending
105
- * leaf-index order. Leaves whose credential identity fails to parse are skipped
106
- * (same tolerance as findMemberLeafIndex). Reflects the handle's current #state
107
- * — call before and after processMessage to diff a commit's membership change.
108
- */
109
- listMembers(): Array<GroupMember>;
110
- /**
111
- * Encrypt an application message for the group, returning framed wire bytes.
112
- * Encrypts at this handle's current epoch. A handle a commit has already
113
- * superseded (see {@link commitInvite}, {@link removeMember},
114
- * {@link commitLedgerEntries}) must not be reused to send — doing so silently
115
- * emits an application message at the now-stale epoch; adopt the commit's
116
- * `newGroup` and call `encrypt` on that instead.
117
- */
118
- encrypt(plaintext: Uint8Array): Promise<Uint8Array>;
119
- /**
120
- * Process a received MLS message (Commit, Proposal, or application).
121
- *
122
- * Accepts either wire-form bytes (the preferred input — framed MLSMessage
123
- * `Uint8Array`, e.g. from commitInvite/removeMember) or a pre-decoded ts-mls
124
- * message object (legacy path). The param type widens to `unknown` because
125
- * `Uint8Array | unknown` collapses to `unknown` in TypeScript; the runtime
126
- * `instanceof` check selects the decode path.
127
- */
128
- processMessage(message: Uint8Array | unknown, opts?: {
129
- commitPolicy?: IncomingMessageCallback;
130
- }): Promise<Uint8Array | null>;
131
- }
132
- export type CreateGroupResult = {
133
- group: GroupHandle;
134
- credential: MemberCredential;
135
- };
136
- /**
137
- * Create a new MLS group. The identity becomes the sole member and admin.
138
- */
139
- export declare function createGroup(identity: OwnIdentity, groupID: string, options?: GroupOptions): Promise<CreateGroupResult>;
140
- export type RestoreGroupParams = {
141
- state: ClientState;
142
- credential: MemberCredential;
143
- /** Signed ledger tokens the host persisted, replayed to rebuild the roster. */
144
- ledgerEntries?: Array<string>;
145
- options?: GroupOptions;
146
- };
147
- export declare function restoreGroup(params: RestoreGroupParams): Promise<GroupHandle>;
148
- export type CreateInviteParams = {
149
- group: GroupHandle;
150
- identity: SigningIdentity;
151
- recipientDID: string;
152
- permission: GroupPermission;
153
- };
154
- export type CreateInviteResult = {
155
- invite: Invite;
156
- };
157
- /**
158
- * Create an invite for a new member.
159
- * Does NOT add them to the group — call commitInvite with their key package to do that.
160
- *
161
- * Only an admin may invite: a role entry from a non-admin issuer is dropped by every
162
- * receiver's fold, so refusing here turns a silent downstream commit rejection into a
163
- * local error.
164
- */
165
- export declare function createInvite(params: CreateInviteParams): Promise<CreateInviteResult>;
166
- export type CommitLedgerEntriesResult = {
167
- /** Framed MLSMessage bytes. Broadcast to existing members via the DS. */
168
- commitMessage: Uint8Array;
169
- newGroup: GroupHandle;
170
- /** Post-commit epoch the group is now at (== newGroup.epoch). */
171
- epoch: bigint;
172
- };
173
- /**
174
- * The admin write path for the control ledger: a commit that carries no membership
175
- * proposal, only the entries it enacts and the head move that covers them. This is how
176
- * an admin promotes, demotes, or writes any other entry type — an entry that never
177
- * rides a commit is invisible to the head, and a joiner recomputing the head would
178
- * read the group's history as doctored.
179
- *
180
- * Enacts exactly `tokens`, at the end of the log — including one whose content the log
181
- * already carries, which is how an admin is demoted back to a role they previously held.
182
- * Rejects an empty `tokens` list: a commit that enacts nothing has no reason to be
183
- * authored here.
184
- *
185
- * Reads `group`'s state and returns a NEW derived handle (`newGroup`); it never
186
- * advances `group` itself. The caller must adopt `newGroup` — never reuse `group` —
187
- * before issuing the next commit: two commits issued from the same source handle
188
- * both frame at that handle's epoch and diverge. The per-handle mutex only
189
- * serializes concurrent calls against one handle; it does not make it safe to
190
- * produce a second commit from a handle a prior commit has already superseded.
191
- */
192
- export declare function commitLedgerEntries(group: GroupHandle, tokens: Array<string>): Promise<CommitLedgerEntriesResult>;
193
- export type CommitInviteResult = {
194
- /** Framed MLSMessage bytes. Broadcast to existing members via the DS. */
195
- commitMessage: Uint8Array;
196
- /** Framed MLSMessage(Welcome) bytes. Delivered to the new member. */
197
- welcomeMessage: Uint8Array;
198
- newGroup: GroupHandle;
199
- /** Post-commit epoch the group is now at (== newGroup.epoch). NOTE: this is
200
- * NOT the epoch carried in the commit's wire header — a commit is framed at
201
- * the sender's pre-commit epoch (== epoch - 1n), which is what receivers
202
- * compare against their own handle.epoch for ordering (see readMessageEpoch). */
203
- epoch: bigint;
204
- };
205
- /**
206
- * Commit an invite by adding the invitee's key package to the group.
207
- * Produces an MLS Commit + Welcome.
208
- *
209
- * The invite's ledger entries are enacted by this commit: their content ids ride in
210
- * the commit's control envelope and the commit advances the ledger head by exactly
211
- * those ids, so every receiver folds the invitee's role entry into its roster as it
212
- * applies the Add. The envelope carries ids, not bodies — a receiver that holds
213
- * neither the entry nor a `resolveLedgerEntries` resolver throws
214
- * MissingLedgerEntriesError on this commit.
215
- *
216
- * The invite itself carries the group's whole history — a joiner has nothing to fold
217
- * it onto — but only the entries it adds beyond that history ride the commit; see
218
- * {@link entriesAddedByInvite} and {@link commitWithEntries}.
219
- *
220
- * Reads `group`'s state and returns a NEW derived handle (`newGroup`); it never
221
- * advances `group` itself. The caller must adopt `newGroup` — never reuse `group` —
222
- * before issuing the next commit: two commits issued from the same source handle
223
- * both frame at that handle's epoch and diverge. The per-handle mutex only
224
- * serializes concurrent calls against one handle; it does not make it safe to
225
- * produce a second commit from a handle a prior commit has already superseded.
226
- */
227
- export declare function commitInvite(group: GroupHandle, keyPackage: KeyPackage, invite: Invite): Promise<CommitInviteResult>;
228
- export type ProcessWelcomeResult = {
229
- group: GroupHandle;
230
- credential: MemberCredential;
231
- };
232
- export type ProcessWelcomeParams = {
233
- identity: OwnIdentity;
234
- invite: Invite;
235
- /** Wire-form framed MLSMessage(Welcome) bytes (preferred), or a pre-decoded
236
- * ts-mls Welcome object (legacy). `Uint8Array | unknown` collapses to
237
- * `unknown` in TypeScript; the runtime `instanceof` check selects the path. */
238
- welcome: Uint8Array | unknown;
239
- keyPackageBundle: KeyPackageBundle;
240
- ratchetTree?: unknown;
241
- options?: GroupOptions;
242
- };
243
- /**
244
- * Process a Welcome message to join a group.
245
- */
246
- export declare function processWelcome(params: ProcessWelcomeParams): Promise<ProcessWelcomeResult>;
247
- export type RemoveMemberResult = {
248
- /** Framed MLSMessage bytes. Broadcast to existing members via the DS. */
249
- commitMessage: Uint8Array;
250
- newGroup: GroupHandle;
251
- /** Post-commit epoch the group is now at (== newGroup.epoch). NOTE: this is
252
- * NOT the epoch carried in the commit's wire header — a commit is framed at
253
- * the sender's pre-commit epoch (== epoch - 1n), which is what receivers
254
- * compare against their own handle.epoch for ordering (see readMessageEpoch). */
255
- epoch: bigint;
256
- };
257
- /**
258
- * Remove a member from the group. Advances the epoch and rotates keys.
259
- *
260
- * Removal must demote: a receiver rejects a Remove whose target is still `admin` in
261
- * the roster the commit's entries fold to. So removing an admin means riding the
262
- * demotion entry on the same commit — pass it as `ledgerEntries`. The entry is signed
263
- * by the caller, who holds the identity; this only carries it.
264
- *
265
- * Reads `group`'s state and returns a NEW derived handle (`newGroup`); it never
266
- * advances `group` itself. The caller must adopt `newGroup` — never reuse `group` —
267
- * before issuing the next commit: two commits issued from the same source handle
268
- * both frame at that handle's epoch and diverge. The per-handle mutex only
269
- * serializes concurrent calls against one handle; it does not make it safe to
270
- * produce a second commit from a handle a prior commit has already superseded.
271
- */
272
- export declare function removeMember(group: GroupHandle, leafIndex: number, ledgerEntries?: Array<string>): Promise<RemoveMemberResult>;
273
- /**
274
- * Read the MLS epoch from a framed handshake/application message's cleartext
275
- * header, without decrypting. Advisory only — the header epoch is unauthenticated;
276
- * use it to drop stale / buffer future messages before the authenticated
277
- * processMessage. Returns undefined for non-message or undecodable bytes.
278
- *
279
- * Total by contract: this is a safe pre-filter over bytes arriving from an
280
- * untrusted Delivery Service, so it never throws. ts-mls `decode` throws (e.g.
281
- * CodecError on input larger than its max size) rather than returning undefined
282
- * for some malformed inputs; that is caught and treated as "not a message".
283
- */
284
- export declare function readMessageEpoch(bytes: Uint8Array): bigint | undefined;
285
- export type InspectGroupInfoResult = {
286
- /** The GroupInfo's epoch, read from its groupContext. */
287
- epoch: bigint;
288
- /** The GroupInfo's ratchet-tree hash, read from its groupContext. Compare for
289
- * equality against a known post-commit state's treeHash to confirm canonical
290
- * convergence (same epoch + same treeHash ⟺ same group state). */
291
- treeHash: Uint8Array;
292
- };
293
- /**
294
- * Non-destructively inspect a framed MLSMessage(GroupInfo) — read its epoch and
295
- * ratchet-tree hash without joining or mutating any state. Used to confirm an
296
- * external-resync Commit was canonically accepted: compare the returned
297
- * (epoch, treeHash) for equality against the rejoiner's own post-commit state
298
- * (GroupHandle.epoch / GroupHandle.treeHash). Equal ⟹ this device's Commit won
299
- * the epoch; unequal ⟹ another Commit won and the rejoin must retry.
300
- *
301
- * Structural read only: it does NOT verify the GroupInfo signature. The caller
302
- * is expected to have obtained the bytes over the group's authorized channel.
303
- * Unlike readMessageEpoch (a total pre-filter over untrusted DS bytes), this
304
- * THROWS on malformed input — a malformed already-trusted GroupInfo is a
305
- * programming error, not expected traffic.
306
- */
307
- export declare function inspectGroupInfo(groupInfoBytes: Uint8Array): InspectGroupInfoResult;
308
- /**
309
- * Generate a key package for joining groups.
310
- */
311
- export declare function createKeyPackageBundle(identity: OwnIdentity, options?: GroupOptions): Promise<KeyPackageBundle>;
312
- export type ExportGroupInfoParams = {
313
- group: GroupHandle;
314
- };
315
- export type ExportGroupInfoResult = {
316
- /** Framed MLSMessage(GroupInfo) bytes. Self-describing with protocol
317
- * version + wireformat + GroupInfo (external_pub + ratchet tree embedded). */
318
- groupInfo: Uint8Array;
319
- };
320
- export declare function exportGroupInfo(params: ExportGroupInfoParams): Promise<ExportGroupInfoResult>;
321
- export type JoinGroupExternalParams = {
322
- identity: OwnIdentity;
323
- /** Framed MLSMessage(GroupInfo) bytes from exportGroupInfo. */
324
- groupInfo: Uint8Array;
325
- /** Caller's cached credential (from prior processWelcome). Reused as-is,
326
- * not re-validated. */
327
- credential: MemberCredential;
328
- /** Stale-recovery only: atomically removes prior leaf for same identity. */
329
- resync: true;
330
- options?: GroupOptions;
331
- authenticatedData?: Uint8Array;
332
- };
333
- export type JoinGroupExternalResult = {
334
- /** Framed MLSMessage(PublicMessage) bytes. Broadcast to existing members. */
335
- commitMessage: Uint8Array;
336
- /** New GroupHandle at post-commit epoch. */
337
- group: GroupHandle;
338
- };
339
- export declare function joinGroupExternal(params: JoinGroupExternalParams): Promise<JoinGroupExternalResult>;
1
+ export { type CommitInviteResult, type CommitLedgerEntriesResult, type CreateInviteParams, type CreateInviteResult, commitInvite, commitLedgerEntries, createInvite, } from './group-commit.js';
2
+ export { type CreateGroupResult, createGroup, type RestoreGroupParams, restoreGroup, } from './group-create.js';
3
+ export { createKeyPackageBundle, makeMLSCredential } from './group-credential.js';
4
+ export { CommitRejectedError, GroupHandle, type GroupHandleParams, type HeldLedgerEntry, type LedgerLogEntry, } from './group-handle.js';
5
+ export { type ExportGroupInfoParams, type ExportGroupInfoResult, exportGroupInfo, type GroupInfoBinding, type InspectGroupInfoResult, inspectGroupInfo, readGroupInfoBinding, readMessageEpoch, } from './group-info.js';
6
+ export { type RemoveMemberResult, removeMember } from './group-membership.js';
7
+ export { type JoinGroupExternalParams, type JoinGroupExternalResult, joinGroupExternal, type ProcessWelcomeOnceParams, type ProcessWelcomeParams, type ProcessWelcomeResult, processWelcome, processWelcomeOnce, } from './group-welcome.js';