@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
package/lib/anchor.d.ts CHANGED
@@ -55,8 +55,12 @@ export declare function encodeGroupAnchor(anchor: GroupAnchor): Uint8Array;
55
55
  */
56
56
  export declare function decodeGroupAnchor(bytes: Uint8Array): GroupAnchor | null;
57
57
  export declare function buildGroupAnchorExtension(anchor: GroupAnchor): GroupContextExtension;
58
+ export type BuildCurrentGroupAnchorExtensionParams = {
59
+ creatorDID: string;
60
+ app?: unknown;
61
+ };
58
62
  /** Build the anchor extension for a freshly created group at the current version. */
59
- export declare function buildCurrentGroupAnchorExtension(creatorDID: string, app?: unknown): GroupContextExtension;
63
+ export declare function buildCurrentGroupAnchorExtension({ creatorDID, app, }: BuildCurrentGroupAnchorExtensionParams): GroupContextExtension;
60
64
  /**
61
65
  * Leaf capabilities advertising all three control GroupContext extension types (genesis
62
66
  * anchor, ledger head, reserved). RFC 9420 requires every leaf to advertise each custom
@@ -88,4 +92,4 @@ export declare function readGroupAnchorExtension(handle: GroupHandle): GroupCont
88
92
  * so this returns a non-null anchor and the member joins. Only the payload a v1 build cannot
89
93
  * interpret is withheld.
90
94
  */
91
- export declare function readGroupAnchor(handle: GroupHandle): GroupAnchor | null;
95
+ export declare function readGroupAnchor(handle: Pick<GroupHandle, 'state'>): GroupAnchor | null;
package/lib/anchor.js CHANGED
@@ -91,7 +91,7 @@ export function buildGroupAnchorExtension(anchor) {
91
91
  extensionData: encodeGroupAnchor(anchor)
92
92
  });
93
93
  }
94
- /** Build the anchor extension for a freshly created group at the current version. */ export function buildCurrentGroupAnchorExtension(creatorDID, app) {
94
+ /** Build the anchor extension for a freshly created group at the current version. */ export function buildCurrentGroupAnchorExtension({ creatorDID, app }) {
95
95
  return buildGroupAnchorExtension({
96
96
  creatorDID,
97
97
  version: CURRENT_VERSION,
@@ -1,4 +1,5 @@
1
1
  import type { SignedEvent } from '@kokuin/controller';
2
+ import { type CryptoOptions } from '@kokuin/token';
2
3
  import type { AuthenticationService, Credential } from 'ts-mls';
3
4
  import { type MLSCredentialIdentity } from './credential.js';
4
5
  import { type DeviceRegistry } from './registry.js';
@@ -12,7 +13,11 @@ export type JudgingContext = {
12
13
  stage: 'structural' | 'full';
13
14
  mode: 'admit' | 'retain' | 'rejudge';
14
15
  };
15
- export type DIDAuthenticationDependencies = {
16
+ export type DIDAuthenticationDependenciesMayKeepDeniedIssuerParams = {
17
+ credential: Credential;
18
+ signaturePublicKey: Uint8Array;
19
+ };
20
+ export type DIDAuthenticationDependencies = CryptoOptions & {
16
21
  deviceDenySet?: () => ReadonlySet<string>;
17
22
  judging?: () => JudgingContext;
18
23
  leafLifetime?: () => number | undefined;
@@ -22,7 +27,7 @@ export type DIDAuthenticationDependencies = {
22
27
  * answers for the group (its anchor names a controller) and for the leaf (it is the credential
23
28
  * already in the tree at that index, or it arrives in a Welcome tree). Absent: never.
24
29
  */
25
- mayKeepDeniedIssuer?: (credential: Credential, signaturePublicKey: Uint8Array) => boolean;
30
+ mayKeepDeniedIssuer?: (params: DIDAuthenticationDependenciesMayKeepDeniedIssuerParams) => boolean;
26
31
  };
27
32
  /** Structural evidence gate also used before MLS invokes its boolean authentication adapter. */
28
33
  export declare function checkHolderEvidence(parsed: MLSCredentialIdentity, signaturePublicKey: Uint8Array, trustedGrantLifetime?: number): string | undefined;
@@ -35,7 +40,7 @@ export declare function hasHolderEvidence(parsed: MLSCredentialIdentity, signatu
35
40
  /** Internal throwing boundary for entry gates; ts-mls consumes the boolean adapter below. */
36
41
  export declare function verifyLeafCredential(credential: Credential, signaturePublicKey: Uint8Array, deps?: DIDAuthenticationDependencies): Promise<void>;
37
42
  /** Management grants are direct controller grants, verified at their issuance time. */
38
- export declare function verifyManagementCapability(params: {
43
+ export declare function verifyManagementCapability(params: CryptoOptions & {
39
44
  capability?: string;
40
45
  prefix: Array<SignedEvent>;
41
46
  controllerID: string;
@@ -33,7 +33,9 @@ function constantTimeEqual(a, b) {
33
33
  if (parsed.longForm == null) return false;
34
34
  let decoded;
35
35
  try {
36
- decoded = decodePeer4(parsed.longForm);
36
+ decoded = decodePeer4({
37
+ longForm: parsed.longForm
38
+ });
37
39
  } catch {
38
40
  return false;
39
41
  }
@@ -74,7 +76,9 @@ function isDenied(denied, did) {
74
76
  return denied.has(did) || denied.has(normalizeDID(did));
75
77
  }
76
78
  /** Authenticate only embedded proof, using the token's issuance as its reference time. */ async function verifyPinnedCapability(params) {
77
- if (!params.controllerID.startsWith('did:kokuin:') || !foldChain(params.controllerID, params.prefix).ok) {
79
+ if (!params.controllerID.startsWith('did:kokuin:') || !(await foldChain(params.controllerID, params.prefix, {
80
+ crypto: params.crypto
81
+ })).ok) {
78
82
  throw new LeafBindingError('controller-mismatch');
79
83
  }
80
84
  const token = readCapability(params.capability);
@@ -88,7 +92,10 @@ function isDenied(denied, did) {
88
92
  if (normalizeDID(payload.aud) !== normalizeDID(params.audience)) {
89
93
  throw new LeafBindingError('audience-mismatch');
90
94
  }
91
- if (!hasPermission(params.permission, payload)) throw new LeafBindingError('permission-denied');
95
+ if (!hasPermission({
96
+ expected: params.permission,
97
+ granted: payload
98
+ })) throw new LeafBindingError('permission-denied');
92
99
  const leafKey = capabilityKey(payload);
93
100
  if (!constantTimeEqual(leafKey.publicKey, params.leafKey)) {
94
101
  throw new LeafBindingError('confirmation-invalid');
@@ -96,7 +103,8 @@ function isDenied(denied, did) {
96
103
  const denySet = params.denySet ?? EMPTY_DENY;
97
104
  const base = params.resolver ?? createRegistryControllerResolver({
98
105
  controllerID: params.controllerID,
99
- prefix: params.prefix
106
+ prefix: params.prefix,
107
+ crypto: params.crypto
100
108
  }).frozen();
101
109
  const resolver = {
102
110
  ...base,
@@ -107,7 +115,10 @@ function isDenied(denied, did) {
107
115
  };
108
116
  const check = async (claims)=>{
109
117
  if (params.stage === 'structural') return;
110
- await checkCapability(params.permission, claims, {
118
+ await checkCapability({
119
+ crypto: params.crypto,
120
+ permission: params.permission,
121
+ payload: claims,
111
122
  atTime: payload.iat,
112
123
  methods: [
113
124
  resolver
@@ -119,7 +130,9 @@ function isDenied(denied, did) {
119
130
  };
120
131
  if (payload.cap === undefined) {
121
132
  assertControllerGrant(payload, params.controllerID);
122
- const verified = await verifyToken(params.capability, {
133
+ const verified = await verifyToken({
134
+ crypto: params.crypto,
135
+ token: params.capability,
123
136
  methods: [
124
137
  resolver
125
138
  ],
@@ -151,13 +164,31 @@ function isDenied(denied, did) {
151
164
  if (params.stage !== 'structural' && isDenied(denySet, payload.iss)) throw new LeafBindingError('denied-issuer');
152
165
  const issuerKey = capabilityKey(parent.payload);
153
166
  if (token.header.alg !== issuerKey.alg) throw new LeafBindingError('signature-invalid');
154
- if (!await getVerifier(token.header.alg)(fromB64U(token.signature), fromUTF(token.data), issuerKey.publicKey)) {
167
+ if (!await getVerifier({
168
+ algorithm: token.header.alg,
169
+ crypto: params.crypto
170
+ })({
171
+ signature: fromB64U(token.signature),
172
+ message: fromUTF(token.data),
173
+ publicKey: issuerKey.publicKey
174
+ })) {
155
175
  throw new LeafBindingError('signature-invalid');
156
176
  }
157
- assertNonExpired(payload, payload.iat);
158
- assertValidIssuedAt(payload, payload.iat);
159
- assertValidNotBefore(payload, payload.iat);
160
- const verifiedParent = await verifyToken(parentRaw, {
177
+ assertNonExpired({
178
+ payload: payload,
179
+ atTime: payload.iat
180
+ });
181
+ assertValidIssuedAt({
182
+ payload: payload,
183
+ atTime: payload.iat
184
+ });
185
+ assertValidNotBefore({
186
+ payload: payload,
187
+ atTime: payload.iat
188
+ });
189
+ const verifiedParent = await verifyToken({
190
+ crypto: params.crypto,
191
+ token: parentRaw,
161
192
  atTime: payload.iat,
162
193
  methods: [
163
194
  resolver
@@ -166,7 +197,11 @@ function isDenied(denied, did) {
166
197
  allowUnsigned: false
167
198
  });
168
199
  assertCapabilityToken(verifiedParent);
169
- assertValidDelegation(verifiedParent.payload, payload, payload.iat);
200
+ assertValidDelegation({
201
+ from: verifiedParent.payload,
202
+ to: payload,
203
+ atTime: payload.iat
204
+ });
170
205
  // A self-issued child bypasses kokuin's parent walk.
171
206
  if (normalizeDID(payload.iss) === normalizeDID(payload.sub)) await check(parent.payload);
172
207
  await check(payload);
@@ -193,9 +228,12 @@ function withoutIssuer(denied, capability) {
193
228
  assertControllerGrant(grant, binding.id);
194
229
  assertCapabilityLifetime(grant, Math.min(trustedGrantLifetime, MAX_TRUSTED_GRANT_LIFETIME));
195
230
  if (grant.cap !== undefined || grant.iat > leafPayload.iat || grant.exp <= leafPayload.iat || grant.nbf != null && grant.nbf > leafPayload.iat || normalizeDID(grant.aud) !== normalizeDID(parsed.id) || !constantTimeEqual(capabilityKey(grant).publicKey, signaturePublicKey) || !hasPermission({
196
- act: MLS_LEAF_ACT,
197
- res: MLS_LEAF_RES
198
- }, grant)) throw new LeafBindingError('holder-evidence-mismatch');
231
+ expected: {
232
+ act: MLS_LEAF_ACT,
233
+ res: MLS_LEAF_RES
234
+ },
235
+ granted: grant
236
+ })) throw new LeafBindingError('holder-evidence-mismatch');
199
237
  } catch (cause) {
200
238
  if (cause instanceof LeafBindingError && cause.reason === 'lifetime-cap' && readCapability(binding.holderGrant).payload.mbr != null) throw cause;
201
239
  const error = new LeafBindingError('holder-evidence-mismatch');
@@ -231,8 +269,11 @@ function withoutIssuer(denied, capability) {
231
269
  };
232
270
  const registry = judging.registry;
233
271
  const structural = judging.stage === 'structural';
272
+ const options = {
273
+ crypto: deps.crypto
274
+ };
234
275
  const denied = structural ? EMPTY_DENY : deps.deviceDenySet?.() ?? (registry == null ? EMPTY_DENY : denySetOf(registry));
235
- if (isDenied(denied, parsed.id) && (registry == null || !mayReadmitResetDevice(registry, parsed))) throw new LeafBindingError('denied-id');
276
+ if (isDenied(denied, parsed.id) && (registry == null || !await mayReadmitResetDevice(registry, parsed, options))) throw new LeafBindingError('denied-id');
236
277
  // Exempt only this holder; every issuer remains subject to the full deny set.
237
278
  const denySet = isDenied(denied, parsed.id) ? new Set([
238
279
  ...denied
@@ -240,42 +281,63 @@ function withoutIssuer(denied, capability) {
240
281
  const binding = parsed.controller;
241
282
  if (binding == null) return;
242
283
  if (!structural && registry != null) {
243
- const folded = foldChain(binding.id, binding.prefix);
284
+ const folded = await foldChain(binding.id, binding.prefix, options);
244
285
  if (!folded.ok) throw new LeafBindingError('controller-mismatch');
245
286
  if ((folded.states.at(-1)?.gen ?? 0) < (registry.controllers.get(normalizeDID(binding.id))?.genFloor ?? 0)) throw new LeafBindingError('generation-floor');
246
287
  }
247
288
  try {
248
- const roots = memberRootsOf(binding, parsed.id);
289
+ const roots = memberRootsOf({
290
+ binding,
291
+ leafDID: parsed.id
292
+ });
249
293
  const primary = roots[0];
250
- const checkEpochCap = ()=>{
294
+ const checkEpochCap = async ()=>{
251
295
  if (judging.mode !== 'admit' || primary?.root.kind !== 'member' || registry == null) return;
252
- const log = recordedLogOf(registry, binding.id);
253
- if (log != null && epochEventCount(log) > MAX_EPOCH_EVENTS_FOR_LEAVES) throw new LeafBindingError('epoch-cap');
296
+ const log = recordedLogOf({
297
+ registry,
298
+ controller: binding.id
299
+ });
300
+ if (log != null && await epochEventCount({
301
+ log,
302
+ ...options
303
+ }) > MAX_EPOCH_EVENTS_FOR_LEAVES) throw new LeafBindingError('epoch-cap');
254
304
  };
255
305
  if (!structural && registry == null && roots.some(({ root })=>root.kind === 'member')) throw new LeafBindingError('controller-mismatch');
256
306
  const frozen = createRegistryControllerResolver({
257
307
  controllerID: binding.id,
258
308
  prefix: binding.prefix,
259
- registry: structural ? undefined : registry
309
+ registry: structural ? undefined : registry,
310
+ crypto: deps.crypto
260
311
  }).frozen();
261
312
  if (binding.member != null) {
262
- if (!binding.id.startsWith('did:kokuin:') || !foldChain(binding.id, binding.prefix).ok) throw new LeafBindingError('controller-mismatch');
313
+ if (!binding.id.startsWith('did:kokuin:') || !(await foldChain(binding.id, binding.prefix, options)).ok) throw new LeafBindingError('controller-mismatch');
263
314
  if (structural) return;
264
315
  if (registry == null) throw new LeafBindingError('controller-mismatch');
265
316
  const root = primary?.root;
266
317
  if (root == null) throw new LeafBindingError('member-unavailable');
267
- const status = memberRootStatus(registry, binding.id, root);
318
+ const status = await memberRootStatus({
319
+ registry,
320
+ controller: binding.id,
321
+ root,
322
+ ...options
323
+ });
268
324
  if (status.status === 'removed') {
269
325
  if (judging.mode === 'admit') throw new LeafBindingError('member-removed');
270
- const authority = effectiveAuthorityOf({
271
- did: parsed.id,
272
- binding,
273
- leafKey: signaturePublicKey
274
- }, registry, binding.id);
326
+ const authority = await effectiveAuthorityOf({
327
+ leaf: {
328
+ did: parsed.id,
329
+ binding,
330
+ leafKey: signaturePublicKey
331
+ },
332
+ registry,
333
+ controller: binding.id,
334
+ ...options
335
+ });
275
336
  if (!('root' in authority)) throw new LeafBindingError('member-removed');
276
337
  const substitute = authority.slot === 'holder' ? binding.holderGrant : registry.controllers.get(normalizeDID(binding.id))?.survivorGrants.get(normalizeDID(parsed.id));
277
338
  if (substitute == null) throw new LeafBindingError('member-removed');
278
339
  await verifyPinnedCapability({
340
+ crypto: deps.crypto,
279
341
  capability: substitute,
280
342
  resolver: frozen,
281
343
  prefix: binding.prefix,
@@ -297,16 +359,22 @@ function withoutIssuer(denied, capability) {
297
359
  if (judging.mode !== 'retain') throw new LeafBindingError('member-unavailable');
298
360
  };
299
361
  const projection = registry.controllers.get(normalizeDID(binding.id));
300
- if (status.status !== 'effective' || prefixRelation(projection?.recordedLog, binding.prefix) !== 'contained') {
362
+ if (status.status !== 'effective' || prefixRelation({
363
+ recorded: projection?.recordedLog,
364
+ prefix: binding.prefix
365
+ }) !== 'contained') {
301
366
  unavailable();
302
367
  return;
303
368
  }
304
369
  let member;
305
370
  try {
306
- member = await frozen.resolveMember?.(binding.id, {
307
- did: parsed.id,
308
- add: binding.member.add,
309
- gen: binding.member.g
371
+ member = await frozen.resolveMember?.({
372
+ subject: binding.id,
373
+ member: {
374
+ did: parsed.id,
375
+ add: binding.member.add,
376
+ gen: binding.member.g
377
+ }
310
378
  });
311
379
  } catch {
312
380
  unavailable();
@@ -317,11 +385,12 @@ function withoutIssuer(denied, capability) {
317
385
  return;
318
386
  }
319
387
  if (!constantTimeEqual(member.key.publicKey, signaturePublicKey)) throw new LeafBindingError('confirmation-invalid');
320
- checkEpochCap();
388
+ await checkEpochCap();
321
389
  return;
322
390
  }
323
391
  if (binding.capability == null) throw new LeafBindingError('controller-mismatch');
324
392
  const common = {
393
+ crypto: deps.crypto,
325
394
  prefix: binding.prefix,
326
395
  controllerID: binding.id,
327
396
  audience: parsed.id,
@@ -360,27 +429,40 @@ function withoutIssuer(denied, capability) {
360
429
  const holderGrant = checkHolderEvidence(parsed, signaturePublicKey, common.trustedGrantLifetime);
361
430
  if (holderGrant != null) await verifyHolder(holderGrant, 'structural');
362
431
  if (structural) return;
363
- checkEpochCap();
432
+ await checkEpochCap();
364
433
  if (primary?.root.kind === 'member') {
365
434
  const status = registry == null ? {
366
435
  status: 'unavailable'
367
- } : memberRootStatus(registry, binding.id, primary.root);
436
+ } : await memberRootStatus({
437
+ registry,
438
+ controller: binding.id,
439
+ root: primary.root,
440
+ ...options
441
+ });
368
442
  if (judging.mode === 'admit') {
369
443
  if (status.status === 'removed') throw new LeafBindingError('member-removed');
370
444
  if (status.status === 'unavailable') throw new LeafBindingError('member-unavailable');
371
445
  }
372
446
  const projection = registry?.controllers.get(normalizeDID(binding.id));
373
- const unavailable = status.status === 'unavailable' || status.status !== 'removed' && prefixRelation(projection?.recordedLog, binding.prefix) !== 'contained';
447
+ const unavailable = status.status === 'unavailable' || status.status !== 'removed' && prefixRelation({
448
+ recorded: projection?.recordedLog,
449
+ prefix: binding.prefix
450
+ }) !== 'contained';
374
451
  if (unavailable) {
375
452
  if (judging.mode === 'retain') return;
376
453
  throw new LeafBindingError('member-unavailable');
377
454
  }
378
455
  if (registry == null) throw new LeafBindingError('member-unavailable');
379
- const authority = effectiveAuthorityOf({
380
- did: parsed.id,
381
- binding,
382
- leafKey: signaturePublicKey
383
- }, registry, binding.id);
456
+ const authority = await effectiveAuthorityOf({
457
+ leaf: {
458
+ did: parsed.id,
459
+ binding,
460
+ leafKey: signaturePublicKey
461
+ },
462
+ registry,
463
+ controller: binding.id,
464
+ ...options
465
+ });
384
466
  if ('status' in authority) {
385
467
  if (authority.status === 'unavailable' && judging.mode === 'retain') return;
386
468
  throw new LeafBindingError(authority.status === 'removed' ? 'member-removed' : 'member-unavailable');
@@ -393,7 +475,10 @@ function withoutIssuer(denied, capability) {
393
475
  }
394
476
  }
395
477
  // The holder grant does not inherit the leaf capability's issuer exception.
396
- const leafDenySet = deps.mayKeepDeniedIssuer?.(credential, signaturePublicKey) && hasHolderEvidence(parsed, signaturePublicKey, denySet, common.trustedGrantLifetime) ? withoutIssuer(denySet, binding.capability) : denySet;
478
+ const leafDenySet = deps.mayKeepDeniedIssuer?.({
479
+ credential,
480
+ signaturePublicKey
481
+ }) && hasHolderEvidence(parsed, signaturePublicKey, denySet, common.trustedGrantLifetime) ? withoutIssuer(denySet, binding.capability) : denySet;
397
482
  await verifyPinnedCapability({
398
483
  ...common,
399
484
  capability: binding.capability,
@@ -402,8 +487,16 @@ function withoutIssuer(denied, capability) {
402
487
  if (holderGrant != null) {
403
488
  const holder = roots.find(({ slot })=>slot === 'holder');
404
489
  if (holder?.root.kind === 'member' && registry != null) {
405
- const status = memberRootStatus(registry, binding.id, holder.root);
406
- if (status.status === 'unavailable' || status.status !== 'removed' && prefixRelation(registry.controllers.get(normalizeDID(binding.id))?.recordedLog, binding.prefix) !== 'contained') {
490
+ const status = await memberRootStatus({
491
+ registry,
492
+ controller: binding.id,
493
+ root: holder.root,
494
+ ...options
495
+ });
496
+ if (status.status === 'unavailable' || status.status !== 'removed' && prefixRelation({
497
+ recorded: registry.controllers.get(normalizeDID(binding.id))?.recordedLog,
498
+ prefix: binding.prefix
499
+ }) !== 'contained') {
407
500
  if (judging.mode === 'retain') return;
408
501
  throw new LeafBindingError('member-unavailable');
409
502
  }
package/lib/capability.js CHANGED
@@ -114,9 +114,12 @@ export async function mintLeafCapability(params) {
114
114
  if (parent.payload.cap !== undefined) throw new LeafBindingError('chain-depth');
115
115
  assertControllerGrant(parent.payload, params.controllerID);
116
116
  if (!hasPermission({
117
- act: MLS_LEAF_ACT,
118
- res: MLS_LEAF_RES
119
- }, parent.payload)) {
117
+ expected: {
118
+ act: MLS_LEAF_ACT,
119
+ res: MLS_LEAF_RES
120
+ },
121
+ granted: parent.payload
122
+ })) {
120
123
  throw new Error('Parent does not grant leaf authentication');
121
124
  }
122
125
  assertCapabilityLifetime(parent.payload, MAX_TRUSTED_GRANT_LIFETIME);
@@ -132,11 +135,15 @@ export async function mintLeafCapability(params) {
132
135
  throw new Error('Parent capability is not valid at issuance');
133
136
  }
134
137
  return stringifyToken(await params.signer.signToken({
135
- ...payload,
136
- cap: params.parent
138
+ payload: {
139
+ ...payload,
140
+ cap: params.parent
141
+ }
137
142
  }));
138
143
  }
139
- return stringifyToken(await params.signer.signToken(payload));
144
+ return stringifyToken(await params.signer.signToken({
145
+ payload: payload
146
+ }));
140
147
  }
141
148
  export async function mintTrustedGrant(params) {
142
149
  const payload = issuingPayload(params, MAX_TRUSTED_GRANT_LIFETIME);
@@ -149,5 +156,7 @@ export async function mintTrustedGrant(params) {
149
156
  iss: params.signer.id
150
157
  }, params.controllerID);
151
158
  }
152
- return stringifyToken(await params.signer.signToken(payload));
159
+ return stringifyToken(await params.signer.signToken({
160
+ payload: payload
161
+ }));
153
162
  }
@@ -1,15 +1,27 @@
1
1
  import { type ControllerLog, type FoldResult, type KeyState } from '@kokuin/controller';
2
+ import { type CryptoOptions } from '@kokuin/token';
2
3
  import type { ControllerProjection } from './registry.js';
3
4
  export type FoldOk = Extract<FoldResult, {
4
5
  ok: true;
5
6
  }>;
6
- export declare function ingestControllerLog(controller: string, recorded: ControllerLog | undefined, proof: ControllerLog, previous?: ControllerProjection): {
7
+ export type IngestedControllerLog = {
7
8
  log: ControllerLog;
8
9
  before?: FoldOk;
9
10
  after: FoldOk;
10
11
  introduced: Array<string>;
11
12
  };
12
- export declare function effectDelta(before: KeyState | undefined, after: KeyState): {
13
+ export type IngestControllerLogParams = {
14
+ controller: string;
15
+ recorded: ControllerLog | undefined;
16
+ proof: ControllerLog;
17
+ previous?: ControllerProjection;
18
+ } & CryptoOptions;
19
+ export declare function ingestControllerLog({ controller, recorded, proof, previous, ...options }: IngestControllerLogParams): Promise<IngestedControllerLog>;
20
+ export type EffectDeltaParams = {
21
+ before: KeyState | undefined;
22
+ after: KeyState;
23
+ };
24
+ export declare function effectDelta({ before, after }: EffectDeltaParams): {
13
25
  denied: Array<string>;
14
26
  removed: Array<{
15
27
  did: string;
@@ -3,12 +3,12 @@ import { normalizeDID } from '@kokuin/token';
3
3
  import { foldControllerLog, isDetachedFoldReason } from './controller-log.js';
4
4
  import { RevokeProofError } from './errors.js';
5
5
  const authenticatedFolds = new WeakMap();
6
- export function ingestControllerLog(controller, recorded, proof, previous) {
6
+ export async function ingestControllerLog({ controller, recorded, proof, previous, ...options }) {
7
7
  const did = normalizeDID(controller);
8
8
  let before;
9
9
  if (recorded != null) {
10
10
  const cached = authenticatedFolds.get(recorded);
11
- const folded = cached?.controller === did && cached.digest === digestOf(recorded) ? structuredClone(cached.folded) : foldControllerLog(did, recorded);
11
+ const folded = cached?.controller === did && cached.crypto === options?.crypto && cached.digest === digestOf(recorded) ? structuredClone(cached.folded) : await foldControllerLog(did, recorded, options);
12
12
  if (!folded.ok) throw new RevokeProofError('not-authority-signed');
13
13
  before = folded;
14
14
  }
@@ -33,10 +33,10 @@ export function ingestControllerLog(controller, recorded, proof, previous) {
33
33
  chain,
34
34
  events: proof.events
35
35
  };
36
- const folded = foldControllerLog(did, {
36
+ const folded = await foldControllerLog(did, {
37
37
  chain,
38
38
  events: []
39
- });
39
+ }, options);
40
40
  if (!folded.ok) {
41
41
  throw new RevokeProofError(isDetachedFoldReason(folded.reason) ? 'detached' : 'not-authority-signed');
42
42
  }
@@ -46,8 +46,14 @@ export function ingestControllerLog(controller, recorded, proof, previous) {
46
46
  if (first?.t === 'icp' && before != null && folded.head.gen <= before.head.gen) throw new RevokeProofError('detached');
47
47
  let authoritative;
48
48
  try {
49
- const loaded = recorded == null ? joined : mergeLogs(did, recorded, joined);
50
- authoritative = authoritativeLog({
49
+ const loaded = recorded == null ? joined : await mergeLogs({
50
+ ...options,
51
+ did: did,
52
+ left: recorded,
53
+ right: joined
54
+ });
55
+ authoritative = await authoritativeLog({
56
+ ...options,
51
57
  did,
52
58
  loaded,
53
59
  seen: recorded,
@@ -77,7 +83,8 @@ export function ingestControllerLog(controller, recorded, proof, previous) {
77
83
  authenticatedFolds.set(log, {
78
84
  controller: did,
79
85
  digest: digestOf(log),
80
- folded: structuredClone(after)
86
+ folded: structuredClone(after),
87
+ crypto: options?.crypto
81
88
  });
82
89
  return {
83
90
  log,
@@ -88,7 +95,7 @@ export function ingestControllerLog(controller, recorded, proof, previous) {
88
95
  introduced
89
96
  };
90
97
  }
91
- export function effectDelta(before, after) {
98
+ export function effectDelta({ before, after }) {
92
99
  const denials = (state)=>{
93
100
  return new Set([
94
101
  ...state?.deny ?? []
@@ -1,12 +1,18 @@
1
1
  import { type ControllerLog, type FoldResult, type SignedEvent } from '@kokuin/controller';
2
+ import { type CryptoOptions } from '@kokuin/token';
2
3
  export declare const CHAIN_REVOKE_REFUSED = "chain revoke refused";
3
4
  export declare function asControllerLog(chain: ReadonlyArray<SignedEvent>): ControllerLog;
4
5
  export declare function hasChainRevoke(chain: ReadonlyArray<unknown>): boolean;
5
- export declare function foldChain(did: string, chain: ReadonlyArray<SignedEvent>): FoldResult;
6
- export declare function foldControllerLog(did: string, log: ControllerLog): FoldResult;
7
- export declare function revokeIndices(did: string, chain: ReadonlyArray<SignedEvent>, subject: string): Array<number>;
8
- export declare function memberDropIndices(did: string, chain: ReadonlyArray<SignedEvent>, add: string): Array<number>;
6
+ export declare function foldChain(did: string, chain: ReadonlyArray<SignedEvent>, options?: CryptoOptions): Promise<FoldResult>;
7
+ export declare function foldControllerLog(did: string, log: ControllerLog, options?: CryptoOptions): Promise<FoldResult>;
8
+ export declare function revokeIndices(did: string, chain: ReadonlyArray<SignedEvent>, subject: string, options?: CryptoOptions): Promise<Array<number>>;
9
+ export type MemberDropIndicesParams = {
10
+ did: string;
11
+ chain: ReadonlyArray<SignedEvent>;
12
+ add: string;
13
+ } & CryptoOptions;
14
+ export declare function memberDropIndices({ did, chain, add, ...options }: MemberDropIndicesParams): Promise<Array<number>>;
9
15
  /** Same-generation `rot` indices whose snapshot newly carries `add` as `member`'s removed incarnation. */
10
- export declare function memberTombstoneIndices(did: string, chain: ReadonlyArray<SignedEvent>, add: string, member: string): Array<number>;
11
- export declare function closureOf(did: string, log: ControllerLog, targets: ReadonlyArray<string>): ControllerLog | undefined;
16
+ export declare function memberTombstoneIndices(did: string, chain: ReadonlyArray<SignedEvent>, add: string, member: string, options?: CryptoOptions): Promise<Array<number>>;
17
+ export declare function closureOf(did: string, log: ControllerLog, targets: ReadonlyArray<string>, options?: CryptoOptions): Promise<ControllerLog | undefined>;
12
18
  export declare function isDetachedFoldReason(reason: string): boolean;
@@ -16,10 +16,10 @@ export function hasChainRevoke(chain) {
16
16
  return event != null && typeof event === 'object' && 't' in event && event.t === 'rev';
17
17
  });
18
18
  }
19
- export function foldChain(did, chain) {
20
- return foldControllerLog(did, asControllerLog(chain));
19
+ export async function foldChain(did, chain, options) {
20
+ return foldControllerLog(did, asControllerLog(chain), options);
21
21
  }
22
- export function foldControllerLog(did, log) {
22
+ export async function foldControllerLog(did, log, options) {
23
23
  // Kokuin 0.2 skips unsigned crit:false events, but a chain rev must never fold.
24
24
  if (hasChainRevoke(log.chain)) {
25
25
  const index = log.chain.findIndex((signed)=>hasChainRevoke([
@@ -31,15 +31,24 @@ export function foldControllerLog(did, log) {
31
31
  index
32
32
  };
33
33
  }
34
- return foldLog(did, log);
34
+ return foldLog({
35
+ ...options,
36
+ did: did,
37
+ input: log
38
+ });
35
39
  }
36
- export function revokeIndices(did, chain, subject) {
37
- return deviceRevokeIndices(did, [
38
- ...chain
39
- ], subject);
40
+ export async function revokeIndices(did, chain, subject, options) {
41
+ return deviceRevokeIndices({
42
+ ...options,
43
+ did: did,
44
+ chain: [
45
+ ...chain
46
+ ],
47
+ subject: subject
48
+ });
40
49
  }
41
- export function memberDropIndices(did, chain, add) {
42
- const folded = foldChain(did, chain);
50
+ export async function memberDropIndices({ did, chain, add, ...options }) {
51
+ const folded = await foldChain(did, chain, options);
43
52
  if (!folded.ok) return [];
44
53
  const indices = [];
45
54
  for(let index = 1; index < chain.length; index++){
@@ -49,8 +58,8 @@ export function memberDropIndices(did, chain, add) {
49
58
  }
50
59
  return indices;
51
60
  }
52
- /** Same-generation `rot` indices whose snapshot newly carries `add` as `member`'s removed incarnation. */ export function memberTombstoneIndices(did, chain, add, member) {
53
- const folded = foldChain(did, chain);
61
+ /** Same-generation `rot` indices whose snapshot newly carries `add` as `member`'s removed incarnation. */ export async function memberTombstoneIndices(did, chain, add, member, options) {
62
+ const folded = await foldChain(did, chain, options);
54
63
  if (!folded.ok) return [];
55
64
  const indices = [];
56
65
  for(let index = 1; index < chain.length; index++){
@@ -60,15 +69,20 @@ export function memberDropIndices(did, chain, add) {
60
69
  }
61
70
  return indices;
62
71
  }
63
- export function closureOf(did, log, targets) {
64
- const folded = foldControllerLog(did, log);
72
+ export async function closureOf(did, log, targets, options) {
73
+ const folded = await foldControllerLog(did, log, options);
65
74
  if (!folded.ok || folded.head.saturated) return undefined;
66
75
  const applied = new Set(folded.applied);
67
76
  if (targets.some((digest)=>!applied.has(digest))) return undefined;
68
77
  const needed = new Set(targets);
69
78
  const events = [];
70
79
  // Applied copies are topologically ordered and closed under ancestry in an unsaturated epoch.
71
- for (const signed of pruneLog(did, log).events.toReversed()){
80
+ const pruned = await pruneLog({
81
+ ...options,
82
+ did: did,
83
+ input: log
84
+ });
85
+ for (const signed of pruned.events.toReversed()){
72
86
  if (!needed.has(digestOf(signed.event))) continue;
73
87
  events.push(signed);
74
88
  for (const parent of signed.event.p)needed.add(parent);