@kumiai/mls 0.9.0 → 0.10.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.
- package/README.md +23 -0
- package/lib/anchor.d.ts +10 -4
- package/lib/anchor.js +26 -2
- package/lib/authentication.d.ts +11 -14
- package/lib/authentication.js +127 -86
- package/lib/capability.d.ts +37 -0
- package/lib/capability.js +141 -0
- package/lib/codec.js +2 -1
- package/lib/device-proof.d.ts +1 -0
- package/lib/device-proof.js +8 -12
- package/lib/envelope-fold.d.ts +16 -1
- package/lib/envelope-fold.js +49 -9
- package/lib/errors.d.ts +18 -0
- package/lib/errors.js +33 -0
- package/lib/group-commit.d.ts +22 -35
- package/lib/group-commit.js +122 -103
- package/lib/group-context.d.ts +6 -2
- package/lib/group-context.js +11 -4
- package/lib/group-create.js +56 -11
- package/lib/group-credential.d.ts +3 -1
- package/lib/group-credential.js +17 -3
- package/lib/group-device.js +34 -21
- package/lib/group-handle.d.ts +60 -24
- package/lib/group-handle.js +331 -77
- package/lib/group-info.d.ts +8 -0
- package/lib/group-info.js +29 -1
- package/lib/group-lifecycle.d.ts +32 -0
- package/lib/group-lifecycle.js +300 -0
- package/lib/group-membership.js +7 -3
- package/lib/group-welcome.d.ts +13 -4
- package/lib/group-welcome.js +113 -21
- package/lib/group.d.ts +3 -2
- package/lib/group.js +2 -1
- package/lib/history.d.ts +6 -0
- package/lib/history.js +26 -0
- package/lib/index.d.ts +10 -4
- package/lib/index.js +9 -3
- package/lib/lifecycle-proof.d.ts +24 -0
- package/lib/lifecycle-proof.js +193 -0
- package/lib/lifecycle.d.ts +44 -0
- package/lib/lifecycle.js +385 -0
- package/lib/policy.d.ts +20 -0
- package/lib/policy.js +8 -4
- package/lib/recovery-verdict.d.ts +43 -0
- package/lib/recovery-verdict.js +135 -0
- package/lib/recovery.d.ts +70 -0
- package/lib/recovery.js +110 -36
- package/lib/registry.d.ts +34 -7
- package/lib/registry.js +113 -15
- package/lib/roster.d.ts +1 -1
- package/lib/roster.js +2 -2
- package/lib/types.d.ts +13 -8
- package/lib/welcome-refs.d.ts +2 -1
- package/package.json +6 -6
package/lib/group-handle.js
CHANGED
|
@@ -2,17 +2,21 @@ import { normalizeDID } from '@kokuin/token';
|
|
|
2
2
|
import { EventEmitter } from '@sozai/event';
|
|
3
3
|
import { contentTypes, createApplicationMessage, decode, encode, mlsExporter, mlsMessageDecoder, mlsMessageEncoder, processMessage as mlsProcessMessage, nodeTypes, senderTypes, wireformats } from 'ts-mls';
|
|
4
4
|
import { readGroupAnchor } from './anchor.js';
|
|
5
|
-
import {
|
|
5
|
+
import { verifyLeafCredential } from './authentication.js';
|
|
6
|
+
import { encodeClientState } from './codec.js';
|
|
7
|
+
import { didFromCredential, parseMLSCredentialIdentity } from './credential.js';
|
|
6
8
|
import { verifyDeviceEntry } from './device-proof.js';
|
|
7
9
|
import { decodeControlEnvelope } from './envelope.js';
|
|
8
10
|
import { foldEnvelope, GROUP_TYPE_PREFIX } from './envelope-fold.js';
|
|
9
|
-
import {
|
|
11
|
+
import { LeafLapsedError, RevokeProofError } from './errors.js';
|
|
12
|
+
import { createMlsContext, deviceDenyHolderFor } from './group-context.js';
|
|
10
13
|
import { readMessageEpoch } from './group-info.js';
|
|
11
14
|
import { assertHeadMatches, computeHead, decodeLedgerHead, encodeLedgerHead, extendHead, headsMatch, readLedgerHead, readLedgerHeadExtension } from './head.js';
|
|
12
15
|
import { ledgerEntryDigest, verifyLedgerEntry } from './ledger.js';
|
|
16
|
+
import { assertSenderNotLapsed, isLapsed, leafAt, prepareLifecycleGate, rejectionReason } from './lifecycle.js';
|
|
13
17
|
import { createMutex } from './mutex.js';
|
|
14
18
|
import { defaultCommitPolicy, MissingLedgerEntriesError } from './policy.js';
|
|
15
|
-
import { beaconOf, controllerOf, DEVICE_ENTRY_TYPE, denySetOf, foldControl, isDeviceValue } from './registry.js';
|
|
19
|
+
import { beaconOf, controllerOf, DEVICE_ENTRY_TYPE, denySetOf, foldControl, isDeviceValue, revocationOf } from './registry.js';
|
|
16
20
|
import { readSenderLeafIndex } from './sender-data.js';
|
|
17
21
|
/** One serializer per live handle, so its state-mutating operations run one at a
|
|
18
22
|
* time in issue order. Keyed weakly: the entry is collected with the handle, and
|
|
@@ -46,12 +50,17 @@ export function mutexFor(handle) {
|
|
|
46
50
|
* commit. The handle is left at its pre-commit epoch. */ export class CommitRejectedError extends Error {
|
|
47
51
|
#proposals;
|
|
48
52
|
#senderLeafIndex;
|
|
49
|
-
|
|
53
|
+
#reason;
|
|
54
|
+
constructor(proposals, senderLeafIndex, reason = 'policy'){
|
|
50
55
|
super('Commit rejected by group commit policy');
|
|
51
56
|
this.name = 'CommitRejectedError';
|
|
57
|
+
this.#reason = reason;
|
|
52
58
|
this.#proposals = proposals;
|
|
53
59
|
this.#senderLeafIndex = senderLeafIndex;
|
|
54
60
|
}
|
|
61
|
+
get reason() {
|
|
62
|
+
return this.#reason;
|
|
63
|
+
}
|
|
55
64
|
get proposals() {
|
|
56
65
|
return this.#proposals;
|
|
57
66
|
}
|
|
@@ -62,12 +71,12 @@ export function mutexFor(handle) {
|
|
|
62
71
|
/**
|
|
63
72
|
* Wrap a consumer commit policy to capture a rejected commit's proposals for
|
|
64
73
|
* CommitRejectedError. ts-mls does not surface them on its result, so record them
|
|
65
|
-
*
|
|
74
|
+
* before either the proposal or path gate can reject. Undefined when no policy.
|
|
66
75
|
*/ function wrapCommitPolicy(callback, capture) {
|
|
67
76
|
if (callback == null) return undefined;
|
|
68
77
|
return (incoming)=>{
|
|
69
78
|
const action = callback(incoming);
|
|
70
|
-
if (
|
|
79
|
+
if (incoming.kind === 'commit') {
|
|
71
80
|
capture.rejected = {
|
|
72
81
|
proposals: incoming.proposals,
|
|
73
82
|
senderLeafIndex: incoming.senderLeafIndex == null ? undefined : Number(incoming.senderLeafIndex)
|
|
@@ -186,6 +195,8 @@ export function mutexFor(handle) {
|
|
|
186
195
|
* the same wherever it appears in the log, so this store is keyed. */ #entryBodies;
|
|
187
196
|
#roster;
|
|
188
197
|
#registry;
|
|
198
|
+
#admission;
|
|
199
|
+
#pendingControlEvents;
|
|
189
200
|
constructor(params){
|
|
190
201
|
this.#state = params.state;
|
|
191
202
|
this.#credential = params.credential;
|
|
@@ -214,12 +225,36 @@ export function mutexFor(handle) {
|
|
|
214
225
|
const folded = foldLedgerControl(this.#ledger, anchor, this.groupID);
|
|
215
226
|
this.#roster = folded.roster;
|
|
216
227
|
this.#registry = folded.registry;
|
|
217
|
-
//
|
|
218
|
-
// derived (post-commit) handle re-points it to itself — always the newest, live registry. A
|
|
219
|
-
// context not built by resolveMlsContext (none in the codebase) simply has no holder.
|
|
228
|
+
// Authentication reads only this handle's registry and lifetime limits.
|
|
220
229
|
const denyHolder = deviceDenyHolderFor(this.#context);
|
|
221
|
-
if (denyHolder != null)
|
|
230
|
+
if (denyHolder != null) {
|
|
231
|
+
denyHolder.provider = ()=>this.currentDenySet();
|
|
232
|
+
denyHolder.leafLifetime = anchor.controller == null ? undefined : anchor.leafLifetime ?? 86_400;
|
|
233
|
+
denyHolder.trustedGrantLifetime = anchor.controller == null ? undefined : anchor.trustedGrantLifetime ?? 2_592_000;
|
|
234
|
+
}
|
|
222
235
|
EMITTERS.set(this, params.events ?? new EventEmitter());
|
|
236
|
+
this.#pendingControlEvents = params.speculative ? [] : undefined;
|
|
237
|
+
this.#admission = this.#computeAdmission();
|
|
238
|
+
}
|
|
239
|
+
#computeAdmission() {
|
|
240
|
+
const leaf = leafAt(this.#state.ratchetTree, this.#state.privatePath.leafIndex);
|
|
241
|
+
const epoch = Number(this.epoch);
|
|
242
|
+
return Object.freeze(leaf != null && isLapsed(this, leaf) ? {
|
|
243
|
+
epoch,
|
|
244
|
+
admissible: false,
|
|
245
|
+
reason: 'lapsed'
|
|
246
|
+
} : {
|
|
247
|
+
epoch,
|
|
248
|
+
admissible: true
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
sendAdmission() {
|
|
252
|
+
return this.#admission;
|
|
253
|
+
}
|
|
254
|
+
confirmAdopted() {
|
|
255
|
+
const pending = this.#pendingControlEvents;
|
|
256
|
+
this.#pendingControlEvents = undefined;
|
|
257
|
+
if (pending != null) this.emitControlEvents(pending);
|
|
223
258
|
}
|
|
224
259
|
get groupID() {
|
|
225
260
|
return this.#credential.groupID;
|
|
@@ -300,19 +335,31 @@ export function mutexFor(handle) {
|
|
|
300
335
|
}
|
|
301
336
|
/**
|
|
302
337
|
* @internal Fire notification events for the device entries just enacted in one operation.
|
|
303
|
-
* Called
|
|
338
|
+
* Called after acceptance or adoption — NEVER from
|
|
304
339
|
* the constructor or a fresh-join applyLedgerEntries, so a joiner is not replayed the whole
|
|
305
340
|
* history as live events (it reads revokedDevices()/beaconOf for current state instead). Uses
|
|
306
|
-
* `fire`: a throwing listener is swallowed and cannot break the fold. Reads
|
|
307
|
-
* POST-fold registry.
|
|
341
|
+
* `fire`: a throwing listener is swallowed and cannot break the fold. Reads the post-fold registry.
|
|
308
342
|
*/ emitControlEvents(enacted) {
|
|
343
|
+
if (this.#pendingControlEvents != null) {
|
|
344
|
+
this.#pendingControlEvents.push(...enacted);
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
309
347
|
const emitter = emitterOf(this);
|
|
310
348
|
const revoked = [];
|
|
311
349
|
for (const { entry } of enacted){
|
|
312
350
|
if (entry.type !== DEVICE_ENTRY_TYPE || !isDeviceValue(entry.value)) continue;
|
|
313
351
|
const value = entry.value;
|
|
314
352
|
const subject = normalizeDID(entry.subject);
|
|
315
|
-
if (value.op === 'revoke') {
|
|
353
|
+
if (this.#anchor.controller != null && (value.op === 'revoke' || value.op === 'reset')) {
|
|
354
|
+
for (const effect of value.revoked ?? []){
|
|
355
|
+
const device = normalizeDID(effect.did);
|
|
356
|
+
const record = revocationOf(this, device);
|
|
357
|
+
if (record != null) revoked.push({
|
|
358
|
+
device,
|
|
359
|
+
...record
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
} else if (value.op === 'revoke') {
|
|
316
363
|
const controller = controllerOf(this.#registry, subject);
|
|
317
364
|
if (controller != null) revoked.push({
|
|
318
365
|
device: subject,
|
|
@@ -328,6 +375,21 @@ export function mutexFor(handle) {
|
|
|
328
375
|
}
|
|
329
376
|
if (revoked.length > 0) emitter.fire('deviceRevoked', revoked);
|
|
330
377
|
}
|
|
378
|
+
/** Deliver independent host notifications after acceptance and any requested persist. */ #notifyAccepted(surfaced, enactedDevice) {
|
|
379
|
+
const errors = [];
|
|
380
|
+
try {
|
|
381
|
+
if (surfaced.length > 0) this.#onLedgerEntries?.(surfaced);
|
|
382
|
+
} catch (error) {
|
|
383
|
+
errors.push(error);
|
|
384
|
+
}
|
|
385
|
+
try {
|
|
386
|
+
this.emitControlEvents(enactedDevice);
|
|
387
|
+
} catch (error) {
|
|
388
|
+
errors.push(error);
|
|
389
|
+
}
|
|
390
|
+
if (errors.length === 1) throw errors[0];
|
|
391
|
+
if (errors.length > 1) throw new AggregateError(errors, 'host notifications failed');
|
|
392
|
+
}
|
|
331
393
|
/**
|
|
332
394
|
* Verify signed ledger tokens, append the valid ones in the order given, and
|
|
333
395
|
* refold the roster. Tokens that fail verification or whose groupID mismatches are
|
|
@@ -361,6 +423,10 @@ export function mutexFor(handle) {
|
|
|
361
423
|
const folded = foldLedgerControl(this.#ledger, this.#anchor, this.groupID);
|
|
362
424
|
this.#roster = folded.roster;
|
|
363
425
|
this.#registry = folded.registry;
|
|
426
|
+
this.#admission = this.#computeAdmission();
|
|
427
|
+
if (this.#pendingControlEvents != null) {
|
|
428
|
+
this.emitControlEvents(appended.filter(({ entry })=>entry.type === DEVICE_ENTRY_TYPE));
|
|
429
|
+
}
|
|
364
430
|
return appended;
|
|
365
431
|
});
|
|
366
432
|
}
|
|
@@ -415,8 +481,10 @@ export function mutexFor(handle) {
|
|
|
415
481
|
* signatures do not. The bound: **a lying responder can withhold, never rewrite.**
|
|
416
482
|
*
|
|
417
483
|
* Throws {@link LedgerIncompleteError} on a head mismatch; the caller drops that
|
|
418
|
-
* responder and tries the next.
|
|
419
|
-
|
|
484
|
+
* responder and tries the next. `persist` runs under this handle's mutex and must not
|
|
485
|
+
* call back into it. It must write atomically: rejection means nothing was stored.
|
|
486
|
+
* Host callbacks run after successful persist; a callback throw does not undo bootstrap.
|
|
487
|
+
*/ async bootstrapLedger(tokens, opts) {
|
|
420
488
|
return mutexFor(this).run(async ()=>{
|
|
421
489
|
const authenticated = readLedgerHead(this);
|
|
422
490
|
if (authenticated == null) {
|
|
@@ -448,6 +516,15 @@ export function mutexFor(handle) {
|
|
|
448
516
|
// Same dedupe as `surfaced`, narrowed to device entries — feeds emitControlEvents below so a
|
|
449
517
|
// heal fires deviceRevoked for entries this bootstrap actually adds, not ones already held.
|
|
450
518
|
const enactedDevice = log.filter(({ entryID, verified })=>!this.#entryBodies.has(entryID) && verified.entry.type === DEVICE_ENTRY_TYPE).map(({ verified })=>verified);
|
|
519
|
+
const previous = {
|
|
520
|
+
state: this.#state,
|
|
521
|
+
ledger: this.#ledger,
|
|
522
|
+
entryBodies: this.#entryBodies,
|
|
523
|
+
roster: this.#roster,
|
|
524
|
+
registry: this.#registry,
|
|
525
|
+
admission: this.#admission
|
|
526
|
+
};
|
|
527
|
+
const folded = foldLedgerControl(log, this.#anchor, this.groupID);
|
|
451
528
|
this.#ledger = log;
|
|
452
529
|
this.#entryBodies = new Map(log.map(({ entryID, token, verified })=>[
|
|
453
530
|
entryID,
|
|
@@ -456,9 +533,20 @@ export function mutexFor(handle) {
|
|
|
456
533
|
verified
|
|
457
534
|
}
|
|
458
535
|
]));
|
|
459
|
-
const folded = foldLedgerControl(log, this.#anchor, this.groupID);
|
|
460
536
|
this.#roster = folded.roster;
|
|
461
537
|
this.#registry = folded.registry;
|
|
538
|
+
this.#admission = this.#computeAdmission();
|
|
539
|
+
try {
|
|
540
|
+
await opts?.persist?.(this);
|
|
541
|
+
} catch (error) {
|
|
542
|
+
this.#state = previous.state;
|
|
543
|
+
this.#ledger = previous.ledger;
|
|
544
|
+
this.#entryBodies = previous.entryBodies;
|
|
545
|
+
this.#roster = previous.roster;
|
|
546
|
+
this.#registry = previous.registry;
|
|
547
|
+
this.#admission = previous.admission;
|
|
548
|
+
throw error;
|
|
549
|
+
}
|
|
462
550
|
// SURFACED HERE TOO, not only on the commit path. A heal is how a peer that missed commits
|
|
463
551
|
// catches up — it rejoins, gathers the whole ledger, and lands here — so a bootstrap that
|
|
464
552
|
// said nothing left a host consuming `onLedgerEntries` as an event stream permanently
|
|
@@ -467,8 +555,7 @@ export function mutexFor(handle) {
|
|
|
467
555
|
//
|
|
468
556
|
// Deduped against what this handle already held, so a peer bootstrapping over a partial
|
|
469
557
|
// ledger is not re-notified of entries it has already seen.
|
|
470
|
-
|
|
471
|
-
this.emitControlEvents(enactedDevice);
|
|
558
|
+
this.#notifyAccepted(surfaced, enactedDevice);
|
|
472
559
|
});
|
|
473
560
|
}
|
|
474
561
|
get memberCount() {
|
|
@@ -539,10 +626,10 @@ export function mutexFor(handle) {
|
|
|
539
626
|
* The diff sees MEMBERSHIP, and only membership: an Add, a Remove, or both in
|
|
540
627
|
* one commit (compare as a set — a count misses that last case). It does NOT
|
|
541
628
|
* see an external-commit REJOIN by a member the group still holds. That member
|
|
542
|
-
* keeps its id, so the id set is unchanged;
|
|
543
|
-
*
|
|
544
|
-
*
|
|
545
|
-
*
|
|
629
|
+
* keeps its id, so the id set is unchanged; the new leaf takes the leftmost blank, which is the
|
|
630
|
+
* just-vacated slot only when no earlier blank exists — otherwise the rejoiner's `leafIndex`
|
|
631
|
+
* moves. Only the id-set projection of this result is guaranteed unchanged across a rejoin;
|
|
632
|
+
* `leafIndex` is not, so a before/after diff on `leafIndex` cannot be relied on either.
|
|
546
633
|
* A caller that must detect a rejoin reads the COMMIT instead — an external
|
|
547
634
|
* commit is structurally one: a public message from a non-member carrying a
|
|
548
635
|
* commit, recognizable from its own bytes before it is applied, and carrying
|
|
@@ -591,6 +678,8 @@ export function mutexFor(handle) {
|
|
|
591
678
|
* `expectedAAD`) needs no key. Defaults to empty.
|
|
592
679
|
*/ async encrypt(plaintext, opts) {
|
|
593
680
|
return mutexFor(this).run(async ()=>{
|
|
681
|
+
const own = leafAt(this.#state.ratchetTree, this.#state.privatePath.leafIndex);
|
|
682
|
+
if (own != null && isLapsed(this, own)) throw new LeafLapsedError('lapsed');
|
|
594
683
|
const { newState, message, consumed } = await createApplicationMessage({
|
|
595
684
|
context: this.#context,
|
|
596
685
|
state: this.#state,
|
|
@@ -606,12 +695,10 @@ export function mutexFor(handle) {
|
|
|
606
695
|
* Open an application message sealed for the group and recover WHO SENT IT — the
|
|
607
696
|
* counterpart to {@link encrypt}, and the read half of the group's application lane.
|
|
608
697
|
*
|
|
609
|
-
* Throws for bytes this handle cannot open
|
|
610
|
-
*
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
* — it is how a frame says "not mine" — and such a caller must not read the throw as
|
|
614
|
-
* corruption. Use {@link readMessageEpoch} to tell those two cases apart before trying.
|
|
698
|
+
* Throws for bytes this handle cannot open. A future-epoch frame cannot open yet;
|
|
699
|
+
* ts-mls can open some past-epoch frames while their retained key material lasts.
|
|
700
|
+
* That window is bounded by epoch transitions. Use {@link readMessageEpoch} to
|
|
701
|
+
* distinguish an epoch mismatch from malformed bytes before trying.
|
|
615
702
|
*
|
|
616
703
|
* WHY THE SENDER IS AUTHENTICATED and not merely claimed: the leaf index comes from the
|
|
617
704
|
* message's sender-data, encrypted under this epoch's sender-data secret, and the
|
|
@@ -643,7 +730,15 @@ export function mutexFor(handle) {
|
|
|
643
730
|
}
|
|
644
731
|
// Read the sender BEFORE opening: the sender-data secret is epoch-level and this
|
|
645
732
|
// consumes no ratchet key, so a frame that then fails to open has cost nothing.
|
|
646
|
-
const
|
|
733
|
+
const historical = this.#state.historicalReceiverData.get(pm.epoch);
|
|
734
|
+
const epochTree = historical?.ratchetTree ?? this.#state.ratchetTree;
|
|
735
|
+
const leafIndex = await readSenderLeafIndex(this.#context, historical?.senderDataSecret ?? this.#state.keySchedule.senderDataSecret, pm);
|
|
736
|
+
assertSenderNotLapsed({
|
|
737
|
+
group: this,
|
|
738
|
+
tree: epochTree,
|
|
739
|
+
leafIndex,
|
|
740
|
+
extensions: (historical?.groupContext ?? this.#state.groupContext).extensions
|
|
741
|
+
});
|
|
647
742
|
const result = await mlsProcessMessage({
|
|
648
743
|
context: this.#context,
|
|
649
744
|
state: this.#state,
|
|
@@ -654,7 +749,8 @@ export function mutexFor(handle) {
|
|
|
654
749
|
if (result.kind !== 'applicationMessage') {
|
|
655
750
|
throw new Error('decrypt: frame was not an application message');
|
|
656
751
|
}
|
|
657
|
-
const
|
|
752
|
+
const senderLeaf = leafIndex == null ? undefined : leafAt(epochTree, leafIndex);
|
|
753
|
+
const senderDID = senderLeaf == null ? undefined : didFromCredential(senderLeaf.credential);
|
|
658
754
|
return {
|
|
659
755
|
payload: result.message,
|
|
660
756
|
aad: result.aad,
|
|
@@ -665,19 +761,51 @@ export function mutexFor(handle) {
|
|
|
665
761
|
});
|
|
666
762
|
}
|
|
667
763
|
/**
|
|
668
|
-
*
|
|
669
|
-
*
|
|
670
|
-
*
|
|
671
|
-
*
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
764
|
+
* Open an application frame, but make its post-open state durable before adopting it.
|
|
765
|
+
* The callback runs under this handle's mutex and must store `stagedState` as the
|
|
766
|
+
* group's handle state. It must not call this handle or the peer. If opening or
|
|
767
|
+
* persistence fails, the live state and its ratchet secrets remain untouched.
|
|
768
|
+
*/ async decryptStaged(message, opts, persist) {
|
|
769
|
+
const decoded = decode(mlsMessageDecoder, message);
|
|
770
|
+
if (decoded == null) throw new Error('decryptStaged: failed to decode MLSMessage');
|
|
771
|
+
return mutexFor(this).run(async ()=>{
|
|
772
|
+
const pm = readPrivateFrame(decoded, contentTypes.application);
|
|
773
|
+
if (pm == null) throw new Error('decryptStaged: not a PrivateMessage application frame');
|
|
774
|
+
if (opts.expectedAAD != null && !bytesEqual(pm.authenticatedData, opts.expectedAAD)) {
|
|
775
|
+
throw new Error('decryptStaged: frame authenticated data does not match expected AAD');
|
|
776
|
+
}
|
|
777
|
+
const historical = this.#state.historicalReceiverData.get(pm.epoch);
|
|
778
|
+
const epochTree = historical?.ratchetTree ?? this.#state.ratchetTree;
|
|
779
|
+
const leafIndex = await readSenderLeafIndex(this.#context, historical?.senderDataSecret ?? this.#state.keySchedule.senderDataSecret, pm);
|
|
780
|
+
assertSenderNotLapsed({
|
|
781
|
+
group: this,
|
|
782
|
+
tree: epochTree,
|
|
783
|
+
leafIndex,
|
|
784
|
+
extensions: (historical?.groupContext ?? this.#state.groupContext).extensions
|
|
785
|
+
});
|
|
786
|
+
const result = await mlsProcessMessage({
|
|
787
|
+
context: this.#context,
|
|
788
|
+
state: this.#state,
|
|
789
|
+
message: decoded
|
|
790
|
+
});
|
|
791
|
+
if (result.kind !== 'applicationMessage') {
|
|
792
|
+
throw new Error('decryptStaged: frame was not an application message');
|
|
793
|
+
}
|
|
794
|
+
const senderLeaf = leafIndex == null ? undefined : leafAt(epochTree, leafIndex);
|
|
795
|
+
const senderDID = senderLeaf == null ? undefined : didFromCredential(senderLeaf.credential);
|
|
796
|
+
if (senderDID == null) throw new Error('decryptStaged: unnamed sender');
|
|
797
|
+
const opened = {
|
|
798
|
+
payload: result.message,
|
|
799
|
+
senderDID,
|
|
800
|
+
aad: result.aad
|
|
801
|
+
};
|
|
802
|
+
await persist(encodeClientState(result.newState), opened);
|
|
803
|
+
this.#state = result.newState;
|
|
804
|
+
zeroAll(result.consumed);
|
|
805
|
+
return opened;
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
/** Resolve ledger inputs before MLS, then run mandatory gates before caller role policy. */ async #prepareCommitPipeline(decoded, opts) {
|
|
681
809
|
const callerPolicy = opts?.commitPolicy ?? this.#commitPolicy;
|
|
682
810
|
const capture = {};
|
|
683
811
|
const commit = readPrivateCommit(decoded);
|
|
@@ -758,9 +886,19 @@ export function mutexFor(handle) {
|
|
|
758
886
|
]);
|
|
759
887
|
return input;
|
|
760
888
|
});
|
|
761
|
-
const foldResult = foldEnvelope(
|
|
889
|
+
const foldResult = foldEnvelope({
|
|
890
|
+
baseRoster: this.#roster,
|
|
891
|
+
baseRegistry: this.#registry,
|
|
892
|
+
entries: ordered,
|
|
893
|
+
groupID: this.groupID,
|
|
894
|
+
context: this.#anchor.controller == null ? undefined : {
|
|
895
|
+
controllerID: this.#anchor.controller,
|
|
896
|
+
memberController: (did)=>this.bindingOfDID(did)?.controller
|
|
897
|
+
}
|
|
898
|
+
});
|
|
762
899
|
if (!foldResult.ok) {
|
|
763
900
|
precomputedReject = true;
|
|
901
|
+
capture.proofError = foldResult.error;
|
|
764
902
|
} else {
|
|
765
903
|
candidateRoster = foldResult.roster;
|
|
766
904
|
candidateRegistry = foldResult.registry;
|
|
@@ -772,6 +910,9 @@ export function mutexFor(handle) {
|
|
|
772
910
|
};
|
|
773
911
|
for (const { verified } of acceptedEntries){
|
|
774
912
|
if (verified.entry.type !== DEVICE_ENTRY_TYPE) continue;
|
|
913
|
+
if (this.#anchor.controller != null && verified.entry.value.op !== 'beacon') {
|
|
914
|
+
continue;
|
|
915
|
+
}
|
|
775
916
|
const ok = await verifyDeviceEntry(verified, proofCtx);
|
|
776
917
|
if (!ok) {
|
|
777
918
|
precomputedReject = true;
|
|
@@ -795,14 +936,40 @@ export function mutexFor(handle) {
|
|
|
795
936
|
externalCommitDID
|
|
796
937
|
}
|
|
797
938
|
});
|
|
939
|
+
let gate;
|
|
940
|
+
try {
|
|
941
|
+
const frame = decoded;
|
|
942
|
+
const externalLeaf = frame.wireformat === wireformats.mls_public_message && frame.publicMessage.content.contentType === contentTypes.commit && frame.publicMessage.senderType === senderTypes.new_member_commit ? frame.publicMessage.content.commit.path?.leafNode : undefined;
|
|
943
|
+
gate = await prepareLifecycleGate({
|
|
944
|
+
group: this,
|
|
945
|
+
entries: acceptedEntries.map(({ verified })=>verified),
|
|
946
|
+
candidateRegistry,
|
|
947
|
+
context,
|
|
948
|
+
externalLeaf
|
|
949
|
+
});
|
|
950
|
+
} catch (error) {
|
|
951
|
+
precomputedReject = true;
|
|
952
|
+
capture.reason = rejectionReason(error);
|
|
953
|
+
if (error instanceof RevokeProofError) capture.proofError = error;
|
|
954
|
+
}
|
|
798
955
|
const combined = (incoming)=>{
|
|
799
956
|
// A decode/fold failure is a hard reject even under a caller policy: the
|
|
800
957
|
// ledger the commit depends on is unresolvable or malformed.
|
|
801
|
-
if (precomputedReject)
|
|
958
|
+
if (precomputedReject) {
|
|
959
|
+
capture.reason ??= capture.proofError == null ? 'invalid' : rejectionReason(capture.proofError);
|
|
960
|
+
return 'reject';
|
|
961
|
+
}
|
|
962
|
+
try {
|
|
963
|
+
gate?.check(incoming);
|
|
964
|
+
} catch (error) {
|
|
965
|
+
capture.reason = rejectionReason(error);
|
|
966
|
+
if (error instanceof RevokeProofError) capture.proofError = error;
|
|
967
|
+
return 'reject';
|
|
968
|
+
}
|
|
802
969
|
if (callerPolicy != null) return callerPolicy(incoming);
|
|
803
|
-
return defaultCommitPolicy(incoming, context);
|
|
970
|
+
return this.#anchor.controller == null ? defaultCommitPolicy(incoming, context) : 'accept';
|
|
804
971
|
};
|
|
805
|
-
const applyOnAccept = ()=>{
|
|
972
|
+
const applyOnAccept = (notify = true)=>{
|
|
806
973
|
// Appended, never deduped: the log records what this commit enacted, at the
|
|
807
974
|
// position the head chained it.
|
|
808
975
|
for (const { token, verified, entryID } of acceptedEntries){
|
|
@@ -818,13 +985,20 @@ export function mutexFor(handle) {
|
|
|
818
985
|
}
|
|
819
986
|
this.#roster = candidateRoster;
|
|
820
987
|
this.#registry = candidateRegistry;
|
|
821
|
-
|
|
822
|
-
|
|
988
|
+
this.#admission = this.#computeAdmission();
|
|
989
|
+
const emit = ()=>{
|
|
990
|
+
this.#notifyAccepted(surfaced, acceptedEntries.filter(({ verified })=>verified.entry.type === DEVICE_ENTRY_TYPE).map(({ verified })=>verified));
|
|
991
|
+
};
|
|
992
|
+
if (notify) emit();
|
|
993
|
+
return emit;
|
|
823
994
|
};
|
|
824
995
|
return {
|
|
825
996
|
callback: wrapCommitPolicy(combined, capture),
|
|
826
997
|
capture,
|
|
827
|
-
applyOnAccept
|
|
998
|
+
applyOnAccept,
|
|
999
|
+
postApply: async (state)=>{
|
|
1000
|
+
await gate?.postApply(state);
|
|
1001
|
+
}
|
|
828
1002
|
};
|
|
829
1003
|
}
|
|
830
1004
|
/**
|
|
@@ -900,7 +1074,8 @@ export function mutexFor(handle) {
|
|
|
900
1074
|
leafKey: node.leaf.signaturePublicKey,
|
|
901
1075
|
...parsed.controller != null ? {
|
|
902
1076
|
controller: parsed.controller.id,
|
|
903
|
-
prefix: parsed.controller.prefix
|
|
1077
|
+
prefix: parsed.controller.prefix,
|
|
1078
|
+
capability: parsed.controller.capability
|
|
904
1079
|
} : {}
|
|
905
1080
|
};
|
|
906
1081
|
}
|
|
@@ -960,10 +1135,10 @@ export function mutexFor(handle) {
|
|
|
960
1135
|
//
|
|
961
1136
|
// `external: true` is reported, not just consumed to find the committer: a resync rejoin
|
|
962
1137
|
// is invisible to every before/after diff a caller could run — it changes no member DID
|
|
963
|
-
// (the roster already held it)
|
|
964
|
-
//
|
|
965
|
-
//
|
|
966
|
-
//
|
|
1138
|
+
// (the roster already held it); its leaf lands on the leftmost blank and may shift, but
|
|
1139
|
+
// the DID set is unchanged. A caller that must know the group's membership shifted has no
|
|
1140
|
+
// other way to see it, so the fact is surfaced here rather than discarded. Absent means a
|
|
1141
|
+
// member commit.
|
|
967
1142
|
const external = readExternalCommit(decoded);
|
|
968
1143
|
if (external != null) {
|
|
969
1144
|
// It IS a commit, so it gets a header even when the leaf credential will not parse to a
|
|
@@ -1008,8 +1183,11 @@ export function mutexFor(handle) {
|
|
|
1008
1183
|
/**
|
|
1009
1184
|
* Process a received MLS message (Commit, Proposal, or application). Accepts
|
|
1010
1185
|
* wire-form bytes (preferred, e.g. from commitInvite/removeMember) or a pre-decoded
|
|
1011
|
-
* ts-mls
|
|
1012
|
-
*
|
|
1186
|
+
* ts-mls framed message (legacy). The runtime `instanceof` selects the decode path.
|
|
1187
|
+
* `persist` covers accepted commits and proposals, not application messages or their
|
|
1188
|
+
* receive ratchet. It runs under this handle's mutex and must not call back into it.
|
|
1189
|
+
* It must write atomically: rejection means nothing was stored. Host callbacks run
|
|
1190
|
+
* after successful persist; a callback throw does not undo an advance.
|
|
1013
1191
|
*/ async processMessage(message, opts) {
|
|
1014
1192
|
let decoded = message;
|
|
1015
1193
|
if (message instanceof Uint8Array) {
|
|
@@ -1020,32 +1198,107 @@ export function mutexFor(handle) {
|
|
|
1020
1198
|
decoded = parsed;
|
|
1021
1199
|
}
|
|
1022
1200
|
return mutexFor(this).run(async ()=>{
|
|
1023
|
-
const { callback, capture, applyOnAccept } = await this.#prepareCommitPipeline(decoded, opts);
|
|
1024
|
-
const
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1201
|
+
const { callback, capture, applyOnAccept, postApply } = await this.#prepareCommitPipeline(decoded, opts);
|
|
1202
|
+
const previousState = this.#state;
|
|
1203
|
+
const previousAdmission = this.#admission;
|
|
1204
|
+
const application = readPrivateFrame(decoded, contentTypes.application);
|
|
1205
|
+
if (application != null) {
|
|
1206
|
+
const historical = this.#state.historicalReceiverData.get(application.epoch);
|
|
1207
|
+
const index = await readSenderLeafIndex(this.#context, historical?.senderDataSecret ?? this.#state.keySchedule.senderDataSecret, application);
|
|
1208
|
+
assertSenderNotLapsed({
|
|
1209
|
+
group: this,
|
|
1210
|
+
tree: historical?.ratchetTree ?? this.#state.ratchetTree,
|
|
1211
|
+
leafIndex: index,
|
|
1212
|
+
extensions: (historical?.groupContext ?? this.#state.groupContext).extensions
|
|
1213
|
+
});
|
|
1214
|
+
}
|
|
1215
|
+
let result;
|
|
1216
|
+
try {
|
|
1217
|
+
result = await mlsProcessMessage({
|
|
1218
|
+
context: {
|
|
1219
|
+
...this.#context,
|
|
1220
|
+
authService: {
|
|
1221
|
+
validateCredential: async (credential, key)=>{
|
|
1222
|
+
try {
|
|
1223
|
+
await verifyLeafCredential(credential, key, {
|
|
1224
|
+
deviceDenySet: ()=>this.currentDenySet(),
|
|
1225
|
+
leafLifetime: ()=>this.#anchor.controller == null ? undefined : this.#anchor.leafLifetime ?? 86_400,
|
|
1226
|
+
trustedGrantLifetime: ()=>this.#anchor.controller == null ? undefined : this.#anchor.trustedGrantLifetime ?? 2_592_000
|
|
1227
|
+
});
|
|
1228
|
+
return true;
|
|
1229
|
+
} catch (error) {
|
|
1230
|
+
capture.reason = rejectionReason(error);
|
|
1231
|
+
return false;
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
},
|
|
1236
|
+
state: this.#state,
|
|
1237
|
+
message: decoded,
|
|
1238
|
+
...callback != null && {
|
|
1239
|
+
callback
|
|
1240
|
+
}
|
|
1241
|
+
});
|
|
1242
|
+
} catch (error) {
|
|
1243
|
+
if (application != null) throw error;
|
|
1244
|
+
const rejection = new CommitRejectedError(capture.rejected?.proposals ?? [], capture.rejected?.senderLeafIndex, capture.reason ?? 'invalid');
|
|
1245
|
+
rejection.cause = error;
|
|
1246
|
+
if (error instanceof Error) rejection.message += `: ${error.message}`;
|
|
1247
|
+
throw rejection;
|
|
1248
|
+
}
|
|
1034
1249
|
if (result.kind === 'newState' && result.actionTaken === 'reject') {
|
|
1035
|
-
|
|
1250
|
+
const rejection = new CommitRejectedError(capture.rejected?.proposals ?? [], capture.rejected?.senderLeafIndex, capture.reason ?? 'policy');
|
|
1251
|
+
if (capture.proofError != null) rejection.cause = capture.proofError;
|
|
1252
|
+
throw rejection;
|
|
1253
|
+
}
|
|
1254
|
+
if (result.kind === 'newState') {
|
|
1255
|
+
try {
|
|
1256
|
+
await postApply(result.newState);
|
|
1257
|
+
} catch (error) {
|
|
1258
|
+
const rejection = new CommitRejectedError(capture.rejected?.proposals ?? [], capture.rejected?.senderLeafIndex, rejectionReason(error));
|
|
1259
|
+
rejection.cause = error;
|
|
1260
|
+
throw rejection;
|
|
1261
|
+
}
|
|
1036
1262
|
}
|
|
1263
|
+
this.#state = result.newState;
|
|
1037
1264
|
if (result.kind === 'applicationMessage') {
|
|
1265
|
+
zeroAll(result.consumed);
|
|
1038
1266
|
return result.message;
|
|
1039
1267
|
}
|
|
1040
|
-
|
|
1268
|
+
if (opts?.persist != null) {
|
|
1269
|
+
const previousLedger = this.#ledger;
|
|
1270
|
+
const previousEntryBodies = this.#entryBodies;
|
|
1271
|
+
const previousRoster = this.#roster;
|
|
1272
|
+
const previousRegistry = this.#registry;
|
|
1273
|
+
// The accept callback appends to these collections. Give the tentative
|
|
1274
|
+
// state its own copies so rollback restores the exact old objects.
|
|
1275
|
+
this.#ledger = [
|
|
1276
|
+
...previousLedger
|
|
1277
|
+
];
|
|
1278
|
+
this.#entryBodies = new Map(previousEntryBodies);
|
|
1279
|
+
const notify = applyOnAccept(false);
|
|
1280
|
+
try {
|
|
1281
|
+
await opts.persist(this);
|
|
1282
|
+
} catch (error) {
|
|
1283
|
+
this.#state = previousState;
|
|
1284
|
+
this.#ledger = previousLedger;
|
|
1285
|
+
this.#entryBodies = previousEntryBodies;
|
|
1286
|
+
this.#roster = previousRoster;
|
|
1287
|
+
this.#registry = previousRegistry;
|
|
1288
|
+
this.#admission = previousAdmission;
|
|
1289
|
+
throw error;
|
|
1290
|
+
}
|
|
1291
|
+
zeroAll(result.consumed);
|
|
1292
|
+
notify();
|
|
1293
|
+
} else {
|
|
1294
|
+
zeroAll(result.consumed);
|
|
1295
|
+
applyOnAccept();
|
|
1296
|
+
}
|
|
1041
1297
|
return null;
|
|
1042
1298
|
});
|
|
1043
1299
|
}
|
|
1044
1300
|
}
|
|
1045
|
-
/** Build
|
|
1046
|
-
* filter judge against, so the two always agree. `entryIDs` are the ledger entry
|
|
1047
|
-
* ids this commit enacts (drives the expected head); `candidateRoster` is the
|
|
1048
|
-
* post-fold roster receivers install. */ export function buildCommitPolicyContext(handle, args) {
|
|
1301
|
+
/** Build policy inputs shared by author and receiver gates. Entry ids drive the expected head. */ export function buildCommitPolicyContext(handle, args) {
|
|
1049
1302
|
const leafToDID = new Map();
|
|
1050
1303
|
for (const member of handle.listMembers())leafToDID.set(member.leafIndex, member.id);
|
|
1051
1304
|
const headExt = readLedgerHeadExtension(handle);
|
|
@@ -1078,11 +1331,12 @@ export function mutexFor(handle) {
|
|
|
1078
1331
|
return new GroupHandle({
|
|
1079
1332
|
state,
|
|
1080
1333
|
credential: group.credential,
|
|
1081
|
-
context: group.context,
|
|
1334
|
+
context: createMlsContext(group.context.cipherSuite, group.anchor),
|
|
1082
1335
|
ledger: group.ledger,
|
|
1083
1336
|
commitPolicy: group.commitPolicy,
|
|
1084
1337
|
resolveLedgerEntries: group.resolveLedgerEntries,
|
|
1085
1338
|
onLedgerEntries: group.onLedgerEntries,
|
|
1086
|
-
events: emitterOf(group)
|
|
1339
|
+
events: emitterOf(group),
|
|
1340
|
+
speculative: true
|
|
1087
1341
|
});
|
|
1088
1342
|
}
|