@kumiai/mls 0.12.4 → 0.13.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.
Files changed (64) hide show
  1. package/lib/anchor.d.ts +6 -2
  2. package/lib/anchor.js +1 -1
  3. package/lib/authentication.d.ts +8 -3
  4. package/lib/authentication.js +139 -46
  5. package/lib/capability.js +16 -7
  6. package/lib/controller-ingest.d.ts +14 -2
  7. package/lib/controller-ingest.js +15 -8
  8. package/lib/controller-log.d.ts +12 -6
  9. package/lib/controller-log.js +29 -15
  10. package/lib/device-proof.d.ts +2 -1
  11. package/lib/device-proof.js +2 -1
  12. package/lib/embedded-resolver.d.ts +7 -2
  13. package/lib/embedded-resolver.js +16 -6
  14. package/lib/envelope-fold.d.ts +3 -2
  15. package/lib/envelope-fold.js +14 -6
  16. package/lib/fold.d.ts +17 -3
  17. package/lib/fold.js +9 -3
  18. package/lib/group-commit.d.ts +11 -2
  19. package/lib/group-commit.js +76 -32
  20. package/lib/group-context.d.ts +3 -2
  21. package/lib/group-context.js +6 -2
  22. package/lib/group-create.d.ts +5 -1
  23. package/lib/group-create.js +36 -14
  24. package/lib/group-credential.d.ts +13 -3
  25. package/lib/group-credential.js +17 -6
  26. package/lib/group-device.d.ts +32 -22
  27. package/lib/group-device.js +66 -51
  28. package/lib/group-handle.d.ts +84 -30
  29. package/lib/group-handle.js +368 -154
  30. package/lib/group-lifecycle.d.ts +44 -11
  31. package/lib/group-lifecycle.js +193 -85
  32. package/lib/group-membership.d.ts +6 -1
  33. package/lib/group-membership.js +2 -2
  34. package/lib/group-welcome.d.ts +5 -1
  35. package/lib/group-welcome.js +34 -10
  36. package/lib/head.d.ts +25 -5
  37. package/lib/head.js +17 -8
  38. package/lib/history.d.ts +14 -3
  39. package/lib/history.js +8 -5
  40. package/lib/index.d.ts +27 -1
  41. package/lib/key-package-codec.d.ts +4 -1
  42. package/lib/key-package-codec.js +1 -1
  43. package/lib/ledger.d.ts +14 -6
  44. package/lib/ledger.js +56 -19
  45. package/lib/lifecycle-proof.d.ts +5 -4
  46. package/lib/lifecycle-proof.js +111 -51
  47. package/lib/lifecycle.d.ts +18 -9
  48. package/lib/lifecycle.js +184 -81
  49. package/lib/member-root.d.ts +54 -15
  50. package/lib/member-root.js +115 -53
  51. package/lib/policy.d.ts +5 -1
  52. package/lib/policy.js +1 -1
  53. package/lib/recovery-verdict.d.ts +12 -3
  54. package/lib/recovery-verdict.js +56 -22
  55. package/lib/recovery.d.ts +20 -5
  56. package/lib/recovery.js +117 -46
  57. package/lib/registry.d.ts +46 -10
  58. package/lib/registry.js +64 -38
  59. package/lib/roster.d.ts +7 -1
  60. package/lib/roster.js +18 -7
  61. package/lib/secret-tree.d.ts +5 -1
  62. package/lib/secret-tree.js +1 -1
  63. package/lib/types.d.ts +3 -0
  64. package/package.json +9 -5
@@ -1,4 +1,5 @@
1
1
  import type { SignedEvent } from '@kokuin/controller';
2
+ import { type CryptoOptions } from '@kokuin/token';
2
3
  import type { VerifiedLedgerEntry } from './ledger.js';
3
4
  import type { DeviceValue } from './registry.js';
4
5
  /** What the tree yields about a leaf for the device-proof gate: its binding, prefix, and leaf key. */
@@ -37,4 +38,4 @@ export type DeviceProofContext = {
37
38
  *
38
39
  * Pure of the fold — runs only where the tree is present (never on a bootstrap re-fold).
39
40
  */
40
- export declare function verifyDeviceEntry(verified: VerifiedLedgerEntry<DeviceValue>, ctx: DeviceProofContext): Promise<boolean>;
41
+ export declare function verifyDeviceEntry(verified: VerifiedLedgerEntry<DeviceValue>, ctx: DeviceProofContext, options?: CryptoOptions): Promise<boolean>;
@@ -14,7 +14,7 @@ import { verifyManagementCapability } from './authentication.js';
14
14
  * leaf key and `exp`-bounded. The profile's log prefix comes from the issuer's OWN bound leaf.
15
15
  *
16
16
  * Pure of the fold — runs only where the tree is present (never on a bootstrap re-fold).
17
- */ export async function verifyDeviceEntry(verified, ctx) {
17
+ */ export async function verifyDeviceEntry(verified, ctx, options) {
18
18
  const issuer = normalizeDID(verified.issuer);
19
19
  const subject = normalizeDID(verified.entry.subject);
20
20
  const value = verified.entry.value;
@@ -43,6 +43,7 @@ import { verifyManagementCapability } from './authentication.js';
43
43
  if (binding?.controller == null || binding.prefix == null) return false;
44
44
  if (normalizeDID(binding.controller) !== authorizedProfile) return false;
45
45
  return await verifyManagementCapability({
46
+ ...options,
46
47
  capability: value.capability,
47
48
  prefix: binding.prefix,
48
49
  controllerID: authorizedProfile,
@@ -1,8 +1,13 @@
1
1
  import { type ControllerLog, type ControllerResolver, type SignedEvent } from '@kokuin/controller';
2
+ import { type CryptoOptions } from '@kokuin/token';
2
3
  import { type DeviceRegistry } from './registry.js';
3
- export declare function prefixRelation(recorded: ControllerLog | undefined, prefix: ReadonlyArray<SignedEvent>): 'contained' | 'ahead' | 'forked' | 'none';
4
+ export type PrefixRelationParams = {
5
+ recorded: ControllerLog | undefined;
6
+ prefix: ReadonlyArray<SignedEvent>;
7
+ };
8
+ export declare function prefixRelation({ recorded, prefix, }: PrefixRelationParams): 'contained' | 'ahead' | 'forked' | 'none';
4
9
  /** Root keys come from the leaf prefix; member authority comes from the recorded epoch. */
5
- export declare function createRegistryControllerResolver(params: {
10
+ export declare function createRegistryControllerResolver(params: CryptoOptions & {
6
11
  controllerID: string;
7
12
  prefix: ReadonlyArray<SignedEvent>;
8
13
  registry?: DeviceRegistry;
@@ -3,7 +3,7 @@ import { normalizeDID } from '@kokuin/token';
3
3
  import { asControllerLog, hasChainRevoke } from './controller-log.js';
4
4
  import { denySetOf, recordedLogOf } from './registry.js';
5
5
  const EMPTY = new Set();
6
- export function prefixRelation(recorded, prefix) {
6
+ export function prefixRelation({ recorded, prefix }) {
7
7
  const held = recorded?.chain.at(-1);
8
8
  const carried = prefix.at(-1);
9
9
  if (recorded == null || held == null || carried == null) return 'none';
@@ -14,11 +14,15 @@ export function prefixRelation(recorded, prefix) {
14
14
  }
15
15
  /** Root keys come from the leaf prefix; member authority comes from the recorded epoch. */ export function createRegistryControllerResolver(params) {
16
16
  const controller = normalizeDID(params.controllerID);
17
- const recorded = params.registry == null ? undefined : recordedLogOf(params.registry, controller);
17
+ const recorded = params.registry == null ? undefined : recordedLogOf({
18
+ registry: params.registry,
19
+ controller
20
+ });
18
21
  const denied = params.registry == null ? EMPTY : denySetOf(params.registry);
19
22
  const prefix = asControllerLog(params.prefix);
20
23
  const resolverFor = (log)=>{
21
24
  return createControllerResolver({
25
+ crypto: params.crypto,
22
26
  loadLog: async (did)=>normalizeDID(did) === controller && !hasChainRevoke(log.chain) ? log : undefined
23
27
  });
24
28
  };
@@ -38,10 +42,16 @@ export function prefixRelation(recorded, prefix) {
38
42
  return denied;
39
43
  },
40
44
  ...params.registry == null ? {} : {
41
- async resolveMember (did, query) {
42
- if (normalizeDID(did) !== controller) throw new Error(`Unknown DID: ${did}`);
43
- if (prefixRelation(recorded, params.prefix) !== 'contained') throw new Error('Recorded controller log does not contain the leaf prefix');
44
- const member = await members.resolveMember?.(did, query);
45
+ async resolveMember ({ subject, member: query }) {
46
+ if (normalizeDID(subject) !== controller) throw new Error(`Unknown DID: ${subject}`);
47
+ if (prefixRelation({
48
+ recorded,
49
+ prefix: params.prefix
50
+ }) !== 'contained') throw new Error('Recorded controller log does not contain the leaf prefix');
51
+ const member = await members.resolveMember?.({
52
+ subject,
53
+ member: query
54
+ });
45
55
  if (member == null) throw new Error('Controller member resolution is unavailable');
46
56
  return member;
47
57
  }
@@ -1,3 +1,4 @@
1
+ import { type CryptoOptions } from '@kokuin/token';
1
2
  import { RevokeProofError } from './errors.js';
2
3
  import type { FoldInput } from './fold.js';
3
4
  import type { VerifiedLedgerEntry } from './ledger.js';
@@ -28,7 +29,7 @@ export type FoldEnvelopeContext = {
28
29
  /** The controller bound to the issuer's pre-commit leaf, if any. */
29
30
  memberController: (did: string) => string | undefined;
30
31
  };
31
- export type FoldEnvelopeParams = {
32
+ export type FoldEnvelopeParams = CryptoOptions & {
32
33
  baseRoster: RosterState;
33
34
  baseRegistry: DeviceRegistry;
34
35
  entries: Array<FoldInput>;
@@ -51,4 +52,4 @@ export type FoldEnvelopeParams = {
51
52
  * subsumes `kumiai.role`'s own authority rule. State-so-far, not a pre-commit
52
53
  * snapshot, so an envelope of `[promote Bob, entry-issued-by-Bob]` is accepted.
53
54
  */
54
- export declare function foldEnvelope(params: FoldEnvelopeParams): EnvelopeFoldResult;
55
+ export declare function foldEnvelope(params: FoldEnvelopeParams): Promise<EnvelopeFoldResult>;
@@ -21,8 +21,8 @@ function isRoleValue(value) {
21
21
  * entry's issuer must be an admin at its own position — the universal invariant that
22
22
  * subsumes `kumiai.role`'s own authority rule. State-so-far, not a pre-commit
23
23
  * snapshot, so an envelope of `[promote Bob, entry-issued-by-Bob]` is accepted.
24
- */ export function foldEnvelope(params) {
25
- const { baseRoster, baseRegistry, entries, groupID, context } = params;
24
+ */ export async function foldEnvelope(params) {
25
+ const { baseRoster, baseRegistry, entries, groupID, context, crypto } = params;
26
26
  let workingRoster = {
27
27
  roles: new Map(baseRoster.roles)
28
28
  };
@@ -61,13 +61,15 @@ function isRoleValue(value) {
61
61
  };
62
62
  }
63
63
  try {
64
- workingRegistry = registryApply({
64
+ workingRegistry = await registryApply({
65
65
  issuer,
66
66
  entry: {
67
67
  ...entry,
68
68
  value
69
69
  }
70
- }, workingRegistry, context?.controllerID);
70
+ }, workingRegistry, context?.controllerID, {
71
+ crypto
72
+ });
71
73
  } catch (error) {
72
74
  return {
73
75
  ok: false,
@@ -101,7 +103,10 @@ function isRoleValue(value) {
101
103
  }
102
104
  // The universal invariant, now authority-aware: the issuer's AUTHORITY (controller ?? id,
103
105
  // read off the registry-so-far) must be an admin in state-so-far.
104
- if (workingRoster.roles.get(authority(workingRegistry, issuer)) !== 'admin') {
106
+ if (workingRoster.roles.get(authority({
107
+ registry: workingRegistry,
108
+ issuer
109
+ })) !== 'admin') {
105
110
  return {
106
111
  ok: false,
107
112
  reason: `non-admin issuer '${issuer}'`,
@@ -123,7 +128,10 @@ function isRoleValue(value) {
123
128
  value: entry.value
124
129
  }
125
130
  };
126
- workingRoster = roleReducer.apply(roleEntry, workingRoster);
131
+ workingRoster = roleReducer.apply({
132
+ verified: roleEntry,
133
+ stateSoFar: workingRoster
134
+ });
127
135
  // A group with zero admins can never again add, remove, promote, or demote.
128
136
  if (adminCount(workingRoster) === 0) {
129
137
  return {
package/lib/fold.d.ts CHANGED
@@ -1,5 +1,13 @@
1
1
  import type { GroupAnchor } from './anchor.js';
2
2
  import type { VerifiedLedgerEntry } from './ledger.js';
3
+ export type LedgerReducerVerifyAuthorityParams<TValue, TState> = {
4
+ verified: VerifiedLedgerEntry<TValue>;
5
+ stateSoFar: TState;
6
+ };
7
+ export type LedgerReducerApplyParams<TValue, TState> = {
8
+ verified: VerifiedLedgerEntry<TValue>;
9
+ stateSoFar: TState;
10
+ };
3
11
  /**
4
12
  * A per-type projection over the control ledger. The fold stays free of any
5
13
  * admin/circle semantics: a reducer owns its own initial state (derived from
@@ -16,9 +24,9 @@ export type LedgerReducer<TValue, TState> = {
16
24
  */
17
25
  seed(anchor: GroupAnchor): TState;
18
26
  /** Is the verified issuer allowed to make this claim, given the state so far? */
19
- verifyAuthority(verified: VerifiedLedgerEntry<TValue>, stateSoFar: TState): boolean;
27
+ verifyAuthority(params: LedgerReducerVerifyAuthorityParams<TValue, TState>): boolean;
20
28
  /** Fold step: return the next state after applying an authorized claim. */
21
- apply(verified: VerifiedLedgerEntry<TValue>, stateSoFar: TState): TState;
29
+ apply(params: LedgerReducerApplyParams<TValue, TState>): TState;
22
30
  };
23
31
  /**
24
32
  * A verified entry paired with its content-addressed id. The id identifies the
@@ -35,6 +43,12 @@ export type FoldDrop = {
35
43
  type: string;
36
44
  reason: string;
37
45
  };
46
+ export type FoldLedgerParams<TValue, TState> = {
47
+ entries: Array<FoldInput<TValue>>;
48
+ anchor: GroupAnchor;
49
+ reducer: LedgerReducer<TValue, TState>;
50
+ onDrop?: (drop: FoldDrop) => void;
51
+ };
38
52
  /**
39
53
  * Replay the ledger into a single reducer's projection.
40
54
  *
@@ -65,4 +79,4 @@ export type FoldDrop = {
65
79
  * authority-failed drops are expected, so the caller decides whether to surface
66
80
  * them rather than the fold emitting to the console.
67
81
  */
68
- export declare function foldLedger<TValue, TState>(entries: Array<FoldInput<TValue>>, anchor: GroupAnchor, reducer: LedgerReducer<TValue, TState>, onDrop?: (drop: FoldDrop) => void): TState;
82
+ export declare function foldLedger<TValue, TState>({ entries, anchor, reducer, onDrop, }: FoldLedgerParams<TValue, TState>): TState;
package/lib/fold.js CHANGED
@@ -27,7 +27,7 @@
27
27
  * `onDrop` observer is supplied — the fold runs on every authority check, where
28
28
  * authority-failed drops are expected, so the caller decides whether to surface
29
29
  * them rather than the fold emitting to the console.
30
- */ export function foldLedger(entries, anchor, reducer, onDrop) {
30
+ */ export function foldLedger({ entries, anchor, reducer, onDrop }) {
31
31
  let state = reducer.seed(anchor);
32
32
  for (const { verified, entryID } of entries){
33
33
  const { entry } = verified;
@@ -39,7 +39,10 @@
39
39
  });
40
40
  continue;
41
41
  }
42
- if (!reducer.verifyAuthority(verified, state)) {
42
+ if (!reducer.verifyAuthority({
43
+ verified,
44
+ stateSoFar: state
45
+ })) {
43
46
  onDrop?.({
44
47
  entryID,
45
48
  type: entry.type,
@@ -47,7 +50,10 @@
47
50
  });
48
51
  continue;
49
52
  }
50
- state = reducer.apply(verified, state);
53
+ state = reducer.apply({
54
+ verified,
55
+ stateSoFar: state
56
+ });
51
57
  }
52
58
  return state;
53
59
  }
@@ -78,6 +78,10 @@ export type CommitLedgerEntriesResult = {
78
78
  /** Post-commit epoch the group is now at (== newGroup.epoch). */
79
79
  epoch: bigint;
80
80
  };
81
+ export type CommitLedgerEntriesParams = {
82
+ group: GroupHandle;
83
+ tokens: Array<string>;
84
+ };
81
85
  /**
82
86
  * The admin write path for the control ledger: a commit carrying no membership
83
87
  * proposal, only the entries it enacts and the head move covering them. An entry that
@@ -97,7 +101,7 @@ export type CommitLedgerEntriesResult = {
97
101
  * The mutex only serializes concurrent calls against one handle. A source whose commit
98
102
  * was accepted is superseded and must never be reused.
99
103
  */
100
- export declare function commitLedgerEntries(group: GroupHandle, tokens: Array<string>): Promise<CommitLedgerEntriesResult>;
104
+ export declare function commitLedgerEntries({ group, tokens, }: CommitLedgerEntriesParams): Promise<CommitLedgerEntriesResult>;
101
105
  export type CommitInviteResult = {
102
106
  /** The input invite, updated with the complete ledger, including any preservation clock. */
103
107
  invite: Invite;
@@ -112,6 +116,11 @@ export type CommitInviteResult = {
112
116
  * readMessageEpoch). */
113
117
  epoch: bigint;
114
118
  };
119
+ export type CommitInviteParams = {
120
+ group: GroupHandle;
121
+ keyPackage: KeyPackage;
122
+ invite: Invite;
123
+ };
115
124
  /**
116
125
  * Commit an invite by adding the invitee's key package. Produces an MLS Commit +
117
126
  * Welcome.
@@ -134,4 +143,4 @@ export type CommitInviteResult = {
134
143
  * The mutex only serializes concurrent calls against one handle; it does not make a
135
144
  * second commit from a superseded handle safe.
136
145
  */
137
- export declare function commitInvite(group: GroupHandle, keyPackage: KeyPackage, invite: Invite): Promise<CommitInviteResult>;
146
+ export declare function commitInvite({ group, keyPackage, invite, }: CommitInviteParams): Promise<CommitInviteResult>;
@@ -50,32 +50,47 @@ const INVITE_ENTRIES = new WeakMap();
50
50
  ...params.entries ?? []
51
51
  ];
52
52
  for (const token of appended){
53
- const verified = await verifyLedgerEntry(token);
53
+ const verified = await verifyLedgerEntry({
54
+ token,
55
+ crypto: group.crypto
56
+ });
54
57
  if (verified == null || verified.entry.groupID !== group.groupID || verified.entry.type.startsWith('kumiai.') || normalizeDID(verified.issuer) !== normalizeDID(identity.id)) throw new Error('createInvite: entries must be consumer entries signed by the inviter');
55
58
  }
56
59
  if (params.controllerLog != null) {
57
- const delta = controllerLogDelta(group, params.controllerLog);
58
- if (delta != null) appended.unshift(await signLedgerEntry(identity, {
59
- type: DEVICE_ENTRY_TYPE,
60
- groupID: group.groupID,
61
- subject: group.anchor.controller,
62
- value: {
63
- op: 'log',
64
- proof: delta
60
+ const delta = await controllerLogDelta({
61
+ group,
62
+ log: params.controllerLog
63
+ });
64
+ if (delta != null) appended.unshift(await signLedgerEntry({
65
+ identity,
66
+ entry: {
67
+ type: DEVICE_ENTRY_TYPE,
68
+ groupID: group.groupID,
69
+ subject: group.anchor.controller,
70
+ value: {
71
+ op: 'log',
72
+ proof: delta
73
+ }
65
74
  }
66
75
  }));
67
76
  }
68
77
  } else {
69
78
  if (params.permission == null) throw new Error('createInvite: standard invites require permission');
70
- if (group.roster.roles.get(authority(group.registry, identity.id)) !== 'admin') {
79
+ if (group.roster.roles.get(authority({
80
+ registry: group.registry,
81
+ issuer: identity.id
82
+ })) !== 'admin') {
71
83
  throw new Error('createInvite: the inviter must be an admin in the group roster');
72
84
  }
73
85
  appended = [
74
- await signLedgerEntry(identity, {
75
- type: ROLE_ENTRY_TYPE,
76
- groupID: group.groupID,
77
- subject: recipientDID,
78
- value: params.permission
86
+ await signLedgerEntry({
87
+ identity,
88
+ entry: {
89
+ type: ROLE_ENTRY_TYPE,
90
+ groupID: group.groupID,
91
+ subject: recipientDID,
92
+ value: params.permission
93
+ }
79
94
  })
80
95
  ];
81
96
  }
@@ -106,7 +121,10 @@ const INVITE_ENTRIES = new WeakMap();
106
121
  if (current == null) {
107
122
  throw new Error('group has no ledger head extension; it cannot enact ledger entries');
108
123
  }
109
- const next = buildLedgerHeadExtension(extendHead(current.head, entryIDs));
124
+ const next = buildLedgerHeadExtension(extendHead({
125
+ head: current.head,
126
+ entryIDs
127
+ }));
110
128
  return group.state.groupContext.extensions.map((ext)=>ext.extensionType === LEDGER_HEAD_EXTENSION_TYPE ? next : ext);
111
129
  }
112
130
  export function proposedTree(state, proposals) {
@@ -165,7 +183,10 @@ export function proposedTree(state, proposals) {
165
183
  // every receiver, so fail here rather than emitting a commit nobody will apply.
166
184
  // Authority-aware: a device of an admin profile commits as that profile. Device-only commits
167
185
  // (register/add/revoke/label) are authorized by proofs, not a role, so they pass requireAdmin:false.
168
- if (requireAdmin && group.anchor.controller == null && group.roster.roles.get(authority(group.registry, group.credential.id)) !== 'admin') {
186
+ if (requireAdmin && group.anchor.controller == null && group.roster.roles.get(authority({
187
+ registry: group.registry,
188
+ issuer: group.credential.id
189
+ })) !== 'admin') {
169
190
  throw new Error('the committer must be an admin in the group roster');
170
191
  }
171
192
  // Fold the entries exactly as every receiver will; refuse to author a commit the
@@ -176,7 +197,10 @@ export function proposedTree(state, proposals) {
176
197
  // dead paper no matter who commits it.
177
198
  const inputs = [];
178
199
  for (const token of enacted){
179
- const verified = await verifyLedgerEntry(token);
200
+ const verified = await verifyLedgerEntry({
201
+ token,
202
+ crypto: group.crypto
203
+ });
180
204
  if (verified == null) {
181
205
  throw new Error('cannot enact a ledger entry whose signature does not verify');
182
206
  }
@@ -185,7 +209,8 @@ export function proposedTree(state, proposals) {
185
209
  entryID: ledgerEntryDigest(token)
186
210
  });
187
211
  }
188
- const foldEntries = ()=>foldEnvelope({
212
+ const foldEntries = async ()=>await foldEnvelope({
213
+ crypto: group.crypto,
189
214
  baseRoster: group.roster,
190
215
  baseRegistry: group.registry,
191
216
  entries: inputs,
@@ -195,7 +220,7 @@ export function proposedTree(state, proposals) {
195
220
  memberController: (did)=>group.bindingOfDID(did)?.controller
196
221
  }
197
222
  });
198
- let fold = foldEntries();
223
+ let fold = await foldEntries();
199
224
  if (!fold.ok) {
200
225
  if (fold.error != null) throw fold.error;
201
226
  throw new Error(`cannot enact ledger entry ${fold.entryID}: ${fold.reason}`);
@@ -208,30 +233,42 @@ export function proposedTree(state, proposals) {
208
233
  senderLeafIndex: sourceState.privatePath.leafIndex
209
234
  }))
210
235
  ]);
211
- const clocks = await preservationClock(group, tree, fold.registry);
236
+ const clocks = await preservationClock({
237
+ group,
238
+ candidateTree: tree,
239
+ candidateRegistry: fold.registry
240
+ });
212
241
  for (const clock of clocks){
213
- const verified = await verifyLedgerEntry(clock);
242
+ const verified = await verifyLedgerEntry({
243
+ token: clock,
244
+ crypto: group.crypto
245
+ });
214
246
  if (verified == null) throw new Error('Invalid preservation clock');
215
247
  enacted.push(clock);
216
248
  inputs.push({
217
249
  verified,
218
250
  entryID: ledgerEntryDigest(clock)
219
251
  });
220
- fold = foldEntries();
252
+ fold = await foldEntries();
221
253
  if (!fold.ok) throw fold.error ?? new Error(fold.reason);
222
254
  }
223
255
  // Same reason as the fold guard above: verify device entries against THIS group's tree and
224
256
  // registry so the committer never authors a commit every receiver's own gate will reject.
225
257
  const proofCtx = {
226
258
  bindingOfDID: (d)=>group.bindingOfDID(d),
227
- controllerOf: (d)=>controllerOf(group.registry, d)
259
+ controllerOf: (d)=>controllerOf({
260
+ registry: group.registry,
261
+ deviceDID: d
262
+ })
228
263
  };
229
264
  for (const input of inputs){
230
265
  if (input.verified.entry.type !== DEVICE_ENTRY_TYPE) continue;
231
266
  if (group.anchor.controller != null && input.verified.entry.value.op !== 'beacon') {
232
267
  continue;
233
268
  }
234
- const ok = await verifyDeviceEntry(input.verified, proofCtx);
269
+ const ok = await verifyDeviceEntry(input.verified, proofCtx, {
270
+ crypto: group.crypto
271
+ });
235
272
  if (!ok) {
236
273
  throw new Error(`cannot enact device entry ${input.entryID}: proof verification failed`);
237
274
  }
@@ -280,10 +317,14 @@ export function proposedTree(state, proposals) {
280
317
  ]
281
318
  };
282
319
  try {
283
- gate.check(incoming);
320
+ await gate.check(incoming);
284
321
  } catch (cause) {
285
322
  if (params.pendingProposals == null) throw cause;
286
- const error = new CommitRejectedError(incoming.proposals, incoming.senderLeafIndex, rejectionReason(cause));
323
+ const error = new CommitRejectedError({
324
+ proposals: incoming.proposals,
325
+ senderLeafIndex: incoming.senderLeafIndex,
326
+ reason: rejectionReason(cause)
327
+ });
287
328
  error.cause = cause;
288
329
  throw error;
289
330
  }
@@ -380,7 +421,7 @@ export function proposedTree(state, proposals) {
380
421
  * Serialise reserve, build and consume against the source handle.
381
422
  * The mutex only serializes concurrent calls against one handle. A source whose commit
382
423
  * was accepted is superseded and must never be reused.
383
- */ export async function commitLedgerEntries(group, tokens) {
424
+ */ export async function commitLedgerEntries({ group, tokens }) {
384
425
  return mutexFor(group).run(async ()=>{
385
426
  if (tokens.length === 0) {
386
427
  throw new Error('commitLedgerEntries: no ledger entries to commit');
@@ -390,7 +431,7 @@ export function proposedTree(state, proposals) {
390
431
  extraProposals: [],
391
432
  enacted: tokens
392
433
  });
393
- const newGroup = deriveGroup(group, result.newState);
434
+ const newGroup = await deriveGroup(group, result.newState);
394
435
  await newGroup.applyLedgerEntries(result.enacted);
395
436
  return {
396
437
  commitMessage: encode(mlsMessageEncoder, result.commit),
@@ -420,7 +461,7 @@ export function proposedTree(state, proposals) {
420
461
  * commit: two commits from the same source handle both frame at its epoch and diverge.
421
462
  * The mutex only serializes concurrent calls against one handle; it does not make a
422
463
  * second commit from a superseded handle safe.
423
- */ export async function commitInvite(group, keyPackage, invite) {
464
+ */ export async function commitInvite({ group, keyPackage, invite }) {
424
465
  return mutexFor(group).run(async ()=>{
425
466
  if (invite.groupID !== group.groupID) {
426
467
  throw new Error(`commitInvite: invite is for group ${invite.groupID}, not ${group.groupID}`);
@@ -430,7 +471,10 @@ export function proposedTree(state, proposals) {
430
471
  if (group.anchor.controller == null) {
431
472
  let grantedTo = null;
432
473
  for (const token of enacted){
433
- const verified = await verifyLedgerEntry(token);
474
+ const verified = await verifyLedgerEntry({
475
+ token,
476
+ crypto: group.crypto
477
+ });
434
478
  if (verified?.entry.type === ROLE_ENTRY_TYPE && verified.entry.groupID === group.groupID) {
435
479
  grantedTo = verified.entry.subject;
436
480
  }
@@ -490,7 +534,7 @@ export function proposedTree(state, proposals) {
490
534
  enacted,
491
535
  ratchetTreeExtension: true
492
536
  });
493
- const newGroup = deriveGroup(group, result.newState);
537
+ const newGroup = await deriveGroup(group, result.newState);
494
538
  if (result.welcome == null) {
495
539
  throw new Error('commitInvite: expected a Welcome message for the add proposal');
496
540
  }
@@ -1,3 +1,4 @@
1
+ import type { CryptoOptions } from '@kokuin/token';
1
2
  import { type Capabilities, type ClientConfig, type GroupContextExtension, type KeyRetentionConfig, type MlsContext } from 'ts-mls';
2
3
  import type { GroupAnchor } from './anchor.js';
3
4
  import { type JudgingContext } from './authentication.js';
@@ -36,7 +37,7 @@ export declare function kumiaiClientConfig(): ClientConfig;
36
37
  /** Copy a client config without sharing its mutable parts with the source context. */
37
38
  export declare function copyClientConfig(config: ClientConfig): ClientConfig;
38
39
  /** Late-bound provider of a context's device deny set, pointed at the live handle after construction. */
39
- export type DeviceDenyHolder = {
40
+ export type DeviceDenyHolder = CryptoOptions & {
40
41
  provider: () => ReadonlySet<string>;
41
42
  judging?: () => JudgingContext;
42
43
  leafLifetime?: number;
@@ -47,7 +48,7 @@ export declare function deviceDenyHolderFor(context: MlsContext): DeviceDenyHold
47
48
  /** Run with an isolated authentication service without changing the source context. */
48
49
  export declare function withJudging<T>(context: MlsContext, judging: JudgingContext, fn: (context: MlsContext) => Promise<T>): Promise<T>;
49
50
  export declare function resolveMlsContext(options?: GroupOptions, anchor?: GroupAnchor): Promise<MlsContext>;
50
- export declare function createMlsContext(cipherSuite: MlsContext['cipherSuite'], anchor?: GroupAnchor, clientConfig?: ClientConfig): MlsContext;
51
+ export declare function createMlsContext(cipherSuite: MlsContext['cipherSuite'], anchor?: GroupAnchor, clientConfig?: ClientConfig, options?: CryptoOptions): MlsContext;
51
52
  /**
52
53
  * Build the leaf-node capabilities for a member joining or creating a group. RFC
53
54
  * 9420 requires a leaf to advertise every non-default GroupContext extension type
@@ -82,6 +82,9 @@ const DENY_HOLDERS = new WeakMap();
82
82
  }
83
83
  function authenticationFor(holder) {
84
84
  return createDIDAuthenticationService({
85
+ get crypto () {
86
+ return holder.crypto;
87
+ },
85
88
  deviceDenySet: ()=>holder.provider(),
86
89
  judging: ()=>holder.judging?.() ?? {
87
90
  stage: 'full',
@@ -95,12 +98,13 @@ function authenticationFor(holder) {
95
98
  export async function resolveMlsContext(options, anchor) {
96
99
  const name = options?.ciphersuiteName ?? DEFAULT_CIPHERSUITE;
97
100
  const cipherSuite = await getCiphersuiteImpl(name, options?.cryptoProvider ?? nobleCryptoProvider);
98
- return createMlsContext(cipherSuite, anchor);
101
+ return createMlsContext(cipherSuite, anchor, undefined, options);
99
102
  }
100
- export function createMlsContext(cipherSuite, anchor, clientConfig) {
103
+ export function createMlsContext(cipherSuite, anchor, clientConfig, options) {
101
104
  // The deny set cannot be known here — the handle that folds it does not exist yet. Bind a mutable
102
105
  // holder the auth service reads; the GroupHandle constructor points it at its own currentDenySet().
103
106
  const holder = {
107
+ crypto: options?.crypto,
104
108
  provider: ()=>EMPTY_DENY,
105
109
  leafLifetime: anchor?.controller == null ? undefined : anchor.leafLifetime ?? 86_400,
106
110
  trustedGrantLifetime: anchor?.controller == null ? undefined : anchor.trustedGrantLifetime ?? 2_592_000
@@ -7,8 +7,12 @@ export type CreateGroupResult = {
7
7
  group: GroupHandle;
8
8
  credential: MemberCredential;
9
9
  };
10
+ export type CreateGroupParams = {
11
+ identity: OwnIdentity;
12
+ groupID: string;
13
+ } & GroupOptions;
10
14
  /** Create a new MLS group. The identity becomes the sole member and admin. */
11
- export declare function createGroup(identity: OwnIdentity, groupID: string, options?: GroupOptions): Promise<CreateGroupResult>;
15
+ export declare function createGroup({ identity, groupID, ...options }: CreateGroupParams): Promise<CreateGroupResult>;
12
16
  export type RestoreGroupParams = {
13
17
  state: ClientState;
14
18
  credential: MemberCredential;