@kumiai/mls 0.2.0 → 0.3.1

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.
@@ -0,0 +1,196 @@
1
+ import { type DIDCache, type DIDResolver } from '@kokuin/token';
2
+ import { type ClientState, type IncomingMessageCallback, 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 Mutex } from './mutex.js';
7
+ import { type CommitPolicyContext } from './policy.js';
8
+ import { type RosterState } from './roster.js';
9
+ export declare function mutexFor(handle: GroupHandle): Mutex;
10
+ /** Thrown by GroupHandle.processMessage when the commit policy rejects an incoming
11
+ * commit. The handle is left at its pre-commit epoch. */
12
+ export declare class CommitRejectedError extends Error {
13
+ #private;
14
+ constructor(proposals: Array<ProposalWithSender>, senderLeafIndex?: number);
15
+ get proposals(): Array<ProposalWithSender>;
16
+ get senderLeafIndex(): number | undefined;
17
+ }
18
+ /**
19
+ * A control-ledger entry as a handle holds it: the signed token (canonical
20
+ * persistent/wire form, the only thing forwardable to another party) paired with
21
+ * its verified form. The verified form is a one-way derivation, so keeping only it
22
+ * would leave a handle unable to forward its ledger.
23
+ */
24
+ export type HeldLedgerEntry = {
25
+ token: string;
26
+ verified: VerifiedLedgerEntry;
27
+ };
28
+ /** A held entry paired with its content id — one position in the ledger log. */
29
+ export type LedgerLogEntry = HeldLedgerEntry & {
30
+ entryID: string;
31
+ };
32
+ export type GroupHandleParams = {
33
+ state: ClientState;
34
+ credential: MemberCredential;
35
+ context: MlsContext;
36
+ /** The control-ledger log this handle starts from, in enactment order. A handle
37
+ * derived from another (commitInvite/removeMember) inherits the parent's, so the
38
+ * roster it folds does not revert to the anchor alone. */
39
+ ledger?: ReadonlyArray<LedgerLogEntry>;
40
+ cache: DIDCache;
41
+ resolver?: DIDResolver;
42
+ /** Default commit policy applied by processMessage. */
43
+ commitPolicy?: IncomingMessageCallback;
44
+ /** Fetch control-ledger entry bodies the local ledger lacks (commit pre-pass). */
45
+ resolveLedgerEntries?: (ids: Array<string>) => Promise<Array<string>>;
46
+ /** Surface an accepted commit's notarized non-`group.role` entries to the consumer. */
47
+ onLedgerEntries?: (entries: Array<VerifiedLedgerEntry>) => void;
48
+ };
49
+ /** Mutable wrapper around MLS group state + Enkaku credential. */
50
+ export declare class GroupHandle {
51
+ #private;
52
+ constructor(params: GroupHandleParams);
53
+ get groupID(): string;
54
+ get epoch(): bigint;
55
+ get treeHash(): Uint8Array;
56
+ get credential(): MemberCredential;
57
+ get state(): ClientState;
58
+ get context(): MlsContext;
59
+ get cache(): DIDCache;
60
+ get resolver(): DIDResolver | undefined;
61
+ /** The commit policy enforced by processMessage, if any. Carried onto
62
+ * handles derived from this one (commitInvite/removeMember). */
63
+ get commitPolicy(): IncomingMessageCallback | undefined;
64
+ /** The ledger-entry resolver, carried onto handles derived from this one. */
65
+ get resolveLedgerEntries(): ((ids: Array<string>) => Promise<Array<string>>) | undefined;
66
+ /** The accepted-commit entry sink, carried onto handles derived from this one. */
67
+ get onLedgerEntries(): ((entries: Array<VerifiedLedgerEntry>) => void) | undefined;
68
+ /** The genesis anchor seeded into this handle at construction. */
69
+ get anchor(): GroupAnchor;
70
+ /** The control-ledger log this handle holds, in enactment order, repeats and
71
+ * all. Carried onto handles derived from this one (commitInvite/removeMember). */
72
+ get ledger(): ReadonlyArray<LedgerLogEntry>;
73
+ /** The ordered signed tokens this handle holds, repeats included — the ledger's
74
+ * canonical persistent/wire form, and the list the authenticated head folds over.
75
+ * Feeds createInvite, restoreGroup, and host persistence. The verified entries are
76
+ * a derived cache with no export form: the only way in is applyLedgerEntries,
77
+ * which re-verifies. */
78
+ get ledgerTokens(): Array<string>;
79
+ /** The control roster folded from the anchor and every applied ledger entry. */
80
+ get roster(): RosterState;
81
+ /**
82
+ * Verify signed ledger tokens, append the valid ones in the order given, and
83
+ * refold the roster. Tokens that fail verification or whose groupID mismatches are
84
+ * dropped (defensive — the strict admin-issuer enforcement is the commit pre-pass).
85
+ * Every token is re-verified on the way in: no entry enters a ledger unverified,
86
+ * whatever the import path.
87
+ *
88
+ * A token the log already holds is appended again, not skipped: the log records
89
+ * what each commit enacted, not a set of claims, and a repeat is the only way to
90
+ * express a demotion back to a previously-held role. Nothing replays a commit into
91
+ * it (MLS applies each once, restoreGroup replays a token list once, processWelcome
92
+ * folds an invite once). Serialized on the handle's mutex.
93
+ */
94
+ applyLedgerEntries(tokens: Array<string>): Promise<void>;
95
+ /**
96
+ * Whether the ledger this handle holds is the whole ledger its GroupContext
97
+ * attests to: the head folded from the ids it holds, in order, against the
98
+ * authenticated `ledger_head`.
99
+ *
100
+ * Purely local — reads this handle's ledger and GroupContext, consults no peer.
101
+ * False means incomplete (e.g. an external-commit rejoin arrives with an *empty*
102
+ * ledger against a live, non-genesis head), and {@link bootstrapLedger} must run
103
+ * before the handle can be trusted to fold a roster or judge a commit.
104
+ *
105
+ * A group with no entries reads true, non-vacuously: the empty fold is the genesis
106
+ * head (a real 32-byte digest bound to this group), which matches only a head that
107
+ * has never moved — exactly a genuinely empty ledger.
108
+ */
109
+ isLedgerComplete(): Promise<boolean>;
110
+ /**
111
+ * The whole ordered ledger this handle holds, as signed tokens — what a responder
112
+ * serves to a bootstrapping peer. Order is load-bearing: the head is a chain
113
+ * digest, so a permuted list folds to a different head and is rejected.
114
+ */
115
+ getLedger(): Promise<Array<string>>;
116
+ /**
117
+ * Install a gathered ledger, verified against the authenticated head before a
118
+ * single entry is folded.
119
+ *
120
+ * The gathered list is the group's WHOLE ledger from genesis, not a delta: a list
121
+ * that reproduces the head this GroupContext carries *is* the entire ledger, in
122
+ * order, and replaces whatever this handle held.
123
+ *
124
+ * Check before fold, structurally: the head is recomputed over the incoming
125
+ * `tokens` while the handle's ledger, entry bodies, and roster are untouched, and
126
+ * the fold's results are installed in one assignment at the end. A rejected ledger
127
+ * writes nothing — no half-applied window, no rollback.
128
+ *
129
+ * Signatures alone do not cover this: a lying responder can hand back genuinely
130
+ * signed, correctly scoped tokens with one demotion omitted or two transposed —
131
+ * every signature verifies, yet the folded roster contains an admin the group
132
+ * demoted. Omission and reordering are exactly what the head chain protects and
133
+ * signatures do not. The bound: **a lying responder can withhold, never rewrite.**
134
+ *
135
+ * Throws {@link LedgerIncompleteError} on a head mismatch; the caller drops that
136
+ * responder and tries the next.
137
+ */
138
+ bootstrapLedger(tokens: Array<string>): Promise<void>;
139
+ get memberCount(): number;
140
+ findMemberLeafIndex(id: string): number | undefined;
141
+ /**
142
+ * The group's current members from the ratchet tree, in ascending leaf-index
143
+ * order. Leaves whose credential identity fails to parse are skipped (like
144
+ * findMemberLeafIndex). Reflects current #state — call before and after
145
+ * processMessage to diff a commit's membership change.
146
+ */
147
+ listMembers(): Array<GroupMember>;
148
+ /**
149
+ * Encrypt an application message for the group at this handle's current epoch,
150
+ * returning framed wire bytes. A handle a commit has already superseded (see
151
+ * {@link commitInvite}, {@link removeMember}, {@link commitLedgerEntries}) must not
152
+ * be reused to send — it silently emits at the now-stale epoch. Adopt the commit's
153
+ * `newGroup` and encrypt on that.
154
+ */
155
+ encrypt(plaintext: Uint8Array): Promise<Uint8Array>;
156
+ /**
157
+ * Read a Commit's MLS-authenticated committer against this handle, WITHOUT advancing
158
+ * state. `null` for bytes that are not a Commit.
159
+ *
160
+ * A member commit (PrivateMessage) has its committer encrypted under the epoch's
161
+ * sender-data secret: decrypt it to the sender leaf index and resolve that against this
162
+ * handle's ratchet tree — the same leaf->DID the commit policy sees as
163
+ * `didOfLeaf(senderLeafIndex)`. An external commit's committer rides its UpdatePath leaf
164
+ * instead (see {@link readExternalCommit}) — no tree lookup, no sender-data decrypt.
165
+ * `null` for anything that is neither.
166
+ *
167
+ * Runs on the handle mutex so the epoch secret, tree, and epoch are one snapshot against a
168
+ * concurrent processMessage. Non-mutating: sender-data decrypt is epoch-level and consumes
169
+ * no per-message key. Async only because the KDF and AEAD are.
170
+ */
171
+ readCommitHeader(commit: Uint8Array): Promise<{
172
+ epoch: bigint;
173
+ committerDID: string;
174
+ } | null>;
175
+ /**
176
+ * Process a received MLS message (Commit, Proposal, or application). Accepts
177
+ * wire-form bytes (preferred, e.g. from commitInvite/removeMember) or a pre-decoded
178
+ * ts-mls object (legacy). Param widens to `unknown` because `Uint8Array | unknown`
179
+ * collapses to `unknown`; the runtime `instanceof` selects the decode path.
180
+ */
181
+ processMessage(message: Uint8Array | unknown, opts?: {
182
+ commitPolicy?: IncomingMessageCallback;
183
+ }): Promise<Uint8Array | null>;
184
+ }
185
+ /** Build the CommitPolicyContext both the receive gate and the send-side pending
186
+ * filter judge against, so the two always agree. `entryIDs` are the ledger entry
187
+ * ids this commit enacts (drives the expected head); `candidateRoster` is the
188
+ * post-fold roster receivers install. */
189
+ export declare function buildCommitPolicyContext(handle: GroupHandle, args: {
190
+ baseRoster: RosterState;
191
+ candidateRoster: RosterState;
192
+ entryIDs: Array<string>;
193
+ externalCommitDID?: string;
194
+ }): CommitPolicyContext;
195
+ /** Build the handle a commit hands back: the post-commit state, everything else inherited. */
196
+ export declare function deriveGroup(group: GroupHandle, state: ClientState): GroupHandle;