@kumiai/mls 0.6.0 → 0.7.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.
@@ -0,0 +1,232 @@
1
+ import { normalizeDID } from '@kokuin/token';
2
+ import { adminCount, ROLE_ENTRY_TYPE, roleReducer } from './roster.js';
3
+ /** The reserved control type carrying a device-registry mutation. One branch, one namespace slot. */ export const DEVICE_ENTRY_TYPE = 'kumiai.device';
4
+ export function registrySeed() {
5
+ return {
6
+ devices: new Map(),
7
+ controllers: new Map()
8
+ };
9
+ }
10
+ /** Structural guard: a value carrying a known `op` (and, for register/add, a string controller). */ export function isDeviceValue(value) {
11
+ if (value == null || typeof value !== 'object') return false;
12
+ const v = value;
13
+ if (v.op !== 'register' && v.op !== 'add' && v.op !== 'revoke' && v.op !== 'label' && v.op !== 'beacon') {
14
+ return false;
15
+ }
16
+ if ((v.op === 'register' || v.op === 'add') && typeof v.controller !== 'string') return false;
17
+ if (v.op === 'beacon' && (typeof v.logLength !== 'number' || typeof v.headDigest !== 'string')) {
18
+ return false;
19
+ }
20
+ if (v.label !== undefined && typeof v.label !== 'string') return false;
21
+ if (v.controller !== undefined && typeof v.controller !== 'string') return false;
22
+ if (v.capability !== undefined && typeof v.capability !== 'string') return false;
23
+ if (v.logLength !== undefined && typeof v.logLength !== 'number') return false;
24
+ if (v.headDigest !== undefined && typeof v.headDigest !== 'string') return false;
25
+ return true;
26
+ }
27
+ /**
28
+ * The registry fold step. Pure, order-dependent, authorization-free — a device entry's authority
29
+ * is the acceptance pipeline's job (a self-register leaf attestation, or a management capability),
30
+ * never the fold's. On the trusted fold path a revoke/label always concerns a subject a prior
31
+ * register bound, so an absent subject is a no-op guard rather than a real case.
32
+ */ export function registryApply(verified, state) {
33
+ const subject = normalizeDID(verified.entry.subject);
34
+ const value = verified.entry.value;
35
+ const devices = new Map(state.devices);
36
+ const controllers = new Map(state.controllers);
37
+ const existing = devices.get(subject);
38
+ switch(value.op){
39
+ case 'register':
40
+ case 'add':
41
+ {
42
+ // Terminal revocation: once a subject is revoked, no later register/add re-activates it. The
43
+ // fold only ever subtracts authority — to re-authorize a device, a fresh device DID is minted.
44
+ // The record is left frozen at 'revoked' (the acceptance gate still verified the entry, but the
45
+ // fold does not resurrect a revoked binding). Keeps determinism: every member applies this rule.
46
+ if (existing?.status === 'revoked') {
47
+ return {
48
+ devices,
49
+ controllers
50
+ };
51
+ }
52
+ // `controller` is structurally guaranteed present for register/add by isDeviceValue.
53
+ const controller = normalizeDID(value.controller);
54
+ devices.set(subject, {
55
+ controller,
56
+ status: 'active',
57
+ ...value.label !== undefined ? {
58
+ label: value.label
59
+ } : existing?.label !== undefined ? {
60
+ label: existing.label
61
+ } : {}
62
+ });
63
+ return {
64
+ devices,
65
+ controllers
66
+ };
67
+ }
68
+ case 'revoke':
69
+ {
70
+ if (existing == null) return {
71
+ devices,
72
+ controllers
73
+ };
74
+ devices.set(subject, {
75
+ ...existing,
76
+ status: 'revoked'
77
+ });
78
+ return {
79
+ devices,
80
+ controllers
81
+ };
82
+ }
83
+ case 'label':
84
+ {
85
+ if (existing == null) return {
86
+ devices,
87
+ controllers
88
+ };
89
+ devices.set(subject, {
90
+ ...existing,
91
+ ...value.label !== undefined ? {
92
+ label: value.label
93
+ } : {}
94
+ });
95
+ return {
96
+ devices,
97
+ controllers
98
+ };
99
+ }
100
+ case 'beacon':
101
+ {
102
+ // Advisory, self-scoped: `subject` is the CONTROLLER DID. Last-write-wins; never touches
103
+ // `devices` or the deny set, never gates validation. Guarded present by isDeviceValue.
104
+ controllers.set(subject, {
105
+ logLength: value.logLength,
106
+ headDigest: value.headDigest
107
+ });
108
+ return {
109
+ devices,
110
+ controllers
111
+ };
112
+ }
113
+ }
114
+ }
115
+ /** The profile a device is bound to in the folded registry, or undefined — the authority input. */ export function controllerOf(registry, deviceDID) {
116
+ return registry.devices.get(normalizeDID(deviceDID))?.controller;
117
+ }
118
+ /** The advisory beacon a controller last announced, or undefined. Never a validation input. */ export function beaconOf(registry, controllerDID) {
119
+ return registry.controllers.get(normalizeDID(controllerDID));
120
+ }
121
+ /**
122
+ * The deny set Slice 1's seam consumes: the device DIDs at `status: 'revoked'` NOW. Matched, never
123
+ * enumerated by consumers (`has`), holding device DIDs only, per the kokuin deny-set rule.
124
+ */ export function denySetOf(registry) {
125
+ const denied = new Set();
126
+ for (const [did, record] of registry.devices){
127
+ if (record.status === 'revoked') denied.add(did);
128
+ }
129
+ return denied;
130
+ }
131
+ /**
132
+ * The universal rule: an ACTIVE binding resolves the issuer to its controller; anything else
133
+ * resolves to the issuer itself. A revoked binding confers no authority — terminal revocation only
134
+ * ever subtracts — so a revoked device re-entering as a floating leaf, or a role entry it signed
135
+ * before revocation but enacted after, must not resolve to its former controller. (Note the split:
136
+ * {@link controllerOf} is the RAW lookup, deliberately status-blind, since the deviceRevoked event
137
+ * emission and the revoke/label gate read the surviving binding of a just-revoked device.)
138
+ */ export function authority(registry, issuer) {
139
+ const norm = normalizeDID(issuer);
140
+ const record = registry.devices.get(norm);
141
+ return record != null && record.status === 'active' ? record.controller : norm;
142
+ }
143
+ /**
144
+ * Fold the whole control ledger into BOTH projections in one ordered pass, so a role entry's
145
+ * authority resolves against the registry-so-far (device entries strictly earlier), never a
146
+ * later binding. This is the determinism-preserving replacement for driving two independent
147
+ * per-type folds: {@link foldLedger}'s own doc warns that a reducer whose authority reads another
148
+ * entry type cannot be driven by a per-type applier — the roster reducer now reads the registry,
149
+ * so both advance together here.
150
+ *
151
+ * A `kumiai.device` entry updates the registry (trusted — proofs are the pipeline's gate). A
152
+ * `kumiai.role` entry is authorized by `roster.roles.get(authority(registry, issuer)) === 'admin'`
153
+ * and must not empty the admin set. Every other type, a groupID mismatch, or a malformed value is
154
+ * dropped (never thrown), routed through `onDrop` exactly as {@link foldRoster}.
155
+ */ export function foldControl(entries, anchor, groupID, onDrop) {
156
+ let roster = roleReducer.seed(anchor);
157
+ let registry = registrySeed();
158
+ for (const { verified, entryID } of entries){
159
+ const { entry, issuer } = verified;
160
+ if (entry.groupID !== groupID) {
161
+ onDrop?.({
162
+ entryID,
163
+ type: entry.type,
164
+ reason: `cross-group entry for '${groupID}'`
165
+ });
166
+ continue;
167
+ }
168
+ if (entry.type === DEVICE_ENTRY_TYPE) {
169
+ if (!isDeviceValue(entry.value)) {
170
+ onDrop?.({
171
+ entryID,
172
+ type: entry.type,
173
+ reason: 'malformed kumiai.device value'
174
+ });
175
+ continue;
176
+ }
177
+ registry = registryApply({
178
+ issuer,
179
+ entry: {
180
+ ...entry,
181
+ value: entry.value
182
+ }
183
+ }, registry);
184
+ continue;
185
+ }
186
+ if (entry.type !== ROLE_ENTRY_TYPE) {
187
+ onDrop?.({
188
+ entryID,
189
+ type: entry.type,
190
+ reason: `unrelated type '${entry.type}'`
191
+ });
192
+ continue;
193
+ }
194
+ if (entry.value !== 'admin' && entry.value !== 'member') {
195
+ onDrop?.({
196
+ entryID,
197
+ type: entry.type,
198
+ reason: 'invalid role value'
199
+ });
200
+ continue;
201
+ }
202
+ const auth = authority(registry, issuer);
203
+ if (roster.roles.get(auth) !== 'admin') {
204
+ onDrop?.({
205
+ entryID,
206
+ type: entry.type,
207
+ reason: `authority '${auth}' of issuer '${normalizeDID(issuer)}' is not admin`
208
+ });
209
+ continue;
210
+ }
211
+ const next = roleReducer.apply({
212
+ issuer,
213
+ entry: {
214
+ ...entry,
215
+ value: entry.value
216
+ }
217
+ }, roster);
218
+ if (adminCount(next) === 0) {
219
+ onDrop?.({
220
+ entryID,
221
+ type: entry.type,
222
+ reason: 'would empty the admin set'
223
+ });
224
+ continue;
225
+ }
226
+ roster = next;
227
+ }
228
+ return {
229
+ roster,
230
+ registry
231
+ };
232
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumiai/mls",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "E2EE group identity and membership for Enkaku using MLS",
5
5
  "keywords": [
6
6
  "mls",
@@ -25,11 +25,14 @@
25
25
  "lib/*"
26
26
  ],
27
27
  "dependencies": {
28
- "@kokuin/token": "^0.4.0",
29
- "@noble/ciphers": "^2.1.1",
28
+ "@kokuin/capability": "^0.3.0",
29
+ "@kokuin/controller": "^0.1.0",
30
+ "@kokuin/token": "^0.5.0",
31
+ "@noble/ciphers": "^2.3.0",
30
32
  "@noble/curves": "^2.3.0",
31
33
  "@noble/hashes": "^2.3.0",
32
34
  "@sozai/codec": "^0.4.0",
35
+ "@sozai/event": "^0.1.3",
33
36
  "@sozai/runtime": "^0.1.0",
34
37
  "ts-mls": "2.0.0-rc.13"
35
38
  },