@kumiai/mls 0.5.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.
- package/lib/authentication.d.ts +25 -1
- package/lib/authentication.js +143 -37
- package/lib/credential.d.ts +29 -7
- package/lib/credential.js +24 -14
- package/lib/crypto.js +1 -11
- package/lib/device-proof.d.ts +33 -0
- package/lib/device-proof.js +56 -0
- package/lib/embedded-resolver.d.ts +16 -0
- package/lib/embedded-resolver.js +21 -0
- package/lib/envelope-fold.d.ts +7 -4
- package/lib/envelope-fold.js +31 -3
- package/lib/group-commit.d.ts +4 -1
- package/lib/group-commit.js +32 -6
- package/lib/group-context.d.ts +6 -0
- package/lib/group-context.js +16 -2
- package/lib/group-create.js +1 -7
- package/lib/group-device.d.ts +67 -0
- package/lib/group-device.js +206 -0
- package/lib/group-handle.d.ts +70 -7
- package/lib/group-handle.js +194 -24
- package/lib/group-welcome.js +1 -7
- package/lib/group.d.ts +2 -1
- package/lib/group.js +1 -0
- package/lib/index.d.ts +6 -3
- package/lib/index.js +4 -3
- package/lib/policy.d.ts +20 -0
- package/lib/policy.js +32 -11
- package/lib/registry.d.ts +89 -0
- package/lib/registry.js +232 -0
- package/lib/types.d.ts +0 -5
- package/package.json +9 -6
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { GroupAnchor } from './anchor.js';
|
|
2
|
+
import type { FoldDrop, FoldInput } from './fold.js';
|
|
3
|
+
import type { VerifiedLedgerEntry } from './ledger.js';
|
|
4
|
+
import { type RosterState } from './roster.js';
|
|
5
|
+
/** The reserved control type carrying a device-registry mutation. One branch, one namespace slot. */
|
|
6
|
+
export declare const DEVICE_ENTRY_TYPE = "kumiai.device";
|
|
7
|
+
/** The device lifecycle operations plus the advisory controller-log beacon. */
|
|
8
|
+
export type DeviceOp = 'register' | 'add' | 'revoke' | 'label' | 'beacon';
|
|
9
|
+
/**
|
|
10
|
+
* A `kumiai.device` entry's `value`. `controller` names the profile a register/add binds to;
|
|
11
|
+
* `label` renames; `capability` carries the management-capability proof a manage op is verified
|
|
12
|
+
* against IN THE ACCEPTANCE PIPELINE — the pure fold never reads it (recorded-once trust).
|
|
13
|
+
*/
|
|
14
|
+
export type DeviceValue = {
|
|
15
|
+
op: DeviceOp;
|
|
16
|
+
controller?: string;
|
|
17
|
+
label?: string;
|
|
18
|
+
capability?: string;
|
|
19
|
+
/** Beacon only: the length of the controller's FULL log at announcement time. */
|
|
20
|
+
logLength?: number;
|
|
21
|
+
/** Beacon only: the head digest of the controller's FULL log at announcement time. */
|
|
22
|
+
headDigest?: string;
|
|
23
|
+
};
|
|
24
|
+
/** A folded device binding. `controller` is the profile DID; `status` gates the deny set. */
|
|
25
|
+
export type DeviceRecord = {
|
|
26
|
+
controller: string;
|
|
27
|
+
status: 'active' | 'revoked';
|
|
28
|
+
label?: string;
|
|
29
|
+
};
|
|
30
|
+
/** An advisory pointer to a controller's FULL log head. Never a validation input. */
|
|
31
|
+
export type ControllerBeacon = {
|
|
32
|
+
logLength: number;
|
|
33
|
+
headDigest: string;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* The group-folded device registry: `device DID -> record`, keyed by normalized DID. A pure
|
|
37
|
+
* function of the accepted `kumiai.device` entries, folded beside {@link RosterState}. Two views
|
|
38
|
+
* derive from `devices` and are never stored: {@link controllerOf} and {@link denySetOf}.
|
|
39
|
+
* `controllers` is a second, independent per-controller projection fed by the `beacon` op.
|
|
40
|
+
*/
|
|
41
|
+
export type DeviceRegistry = {
|
|
42
|
+
devices: ReadonlyMap<string, DeviceRecord>;
|
|
43
|
+
controllers: ReadonlyMap<string, ControllerBeacon>;
|
|
44
|
+
};
|
|
45
|
+
export declare function registrySeed(): DeviceRegistry;
|
|
46
|
+
/** Structural guard: a value carrying a known `op` (and, for register/add, a string controller). */
|
|
47
|
+
export declare function isDeviceValue(value: unknown): value is DeviceValue;
|
|
48
|
+
/**
|
|
49
|
+
* The registry fold step. Pure, order-dependent, authorization-free — a device entry's authority
|
|
50
|
+
* is the acceptance pipeline's job (a self-register leaf attestation, or a management capability),
|
|
51
|
+
* never the fold's. On the trusted fold path a revoke/label always concerns a subject a prior
|
|
52
|
+
* register bound, so an absent subject is a no-op guard rather than a real case.
|
|
53
|
+
*/
|
|
54
|
+
export declare function registryApply(verified: VerifiedLedgerEntry<DeviceValue>, state: DeviceRegistry): DeviceRegistry;
|
|
55
|
+
/** The profile a device is bound to in the folded registry, or undefined — the authority input. */
|
|
56
|
+
export declare function controllerOf(registry: DeviceRegistry, deviceDID: string): string | undefined;
|
|
57
|
+
/** The advisory beacon a controller last announced, or undefined. Never a validation input. */
|
|
58
|
+
export declare function beaconOf(registry: DeviceRegistry, controllerDID: string): ControllerBeacon | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* The deny set Slice 1's seam consumes: the device DIDs at `status: 'revoked'` NOW. Matched, never
|
|
61
|
+
* enumerated by consumers (`has`), holding device DIDs only, per the kokuin deny-set rule.
|
|
62
|
+
*/
|
|
63
|
+
export declare function denySetOf(registry: DeviceRegistry): ReadonlySet<string>;
|
|
64
|
+
/**
|
|
65
|
+
* The universal rule: an ACTIVE binding resolves the issuer to its controller; anything else
|
|
66
|
+
* resolves to the issuer itself. A revoked binding confers no authority — terminal revocation only
|
|
67
|
+
* ever subtracts — so a revoked device re-entering as a floating leaf, or a role entry it signed
|
|
68
|
+
* before revocation but enacted after, must not resolve to its former controller. (Note the split:
|
|
69
|
+
* {@link controllerOf} is the RAW lookup, deliberately status-blind, since the deviceRevoked event
|
|
70
|
+
* emission and the revoke/label gate read the surviving binding of a just-revoked device.)
|
|
71
|
+
*/
|
|
72
|
+
export declare function authority(registry: DeviceRegistry, issuer: string): string;
|
|
73
|
+
/**
|
|
74
|
+
* Fold the whole control ledger into BOTH projections in one ordered pass, so a role entry's
|
|
75
|
+
* authority resolves against the registry-so-far (device entries strictly earlier), never a
|
|
76
|
+
* later binding. This is the determinism-preserving replacement for driving two independent
|
|
77
|
+
* per-type folds: {@link foldLedger}'s own doc warns that a reducer whose authority reads another
|
|
78
|
+
* entry type cannot be driven by a per-type applier — the roster reducer now reads the registry,
|
|
79
|
+
* so both advance together here.
|
|
80
|
+
*
|
|
81
|
+
* A `kumiai.device` entry updates the registry (trusted — proofs are the pipeline's gate). A
|
|
82
|
+
* `kumiai.role` entry is authorized by `roster.roles.get(authority(registry, issuer)) === 'admin'`
|
|
83
|
+
* and must not empty the admin set. Every other type, a groupID mismatch, or a malformed value is
|
|
84
|
+
* dropped (never thrown), routed through `onDrop` exactly as {@link foldRoster}.
|
|
85
|
+
*/
|
|
86
|
+
export declare function foldControl(entries: Array<FoldInput>, anchor: GroupAnchor, groupID: string, onDrop?: (drop: FoldDrop) => void): {
|
|
87
|
+
roster: RosterState;
|
|
88
|
+
registry: DeviceRegistry;
|
|
89
|
+
};
|
package/lib/registry.js
ADDED
|
@@ -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/lib/types.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { DIDCache, DIDResolver } from '@kokuin/token';
|
|
2
1
|
import type { Capabilities, CryptoProvider, GroupContextExtension, IncomingMessageCallback, KeyPackage, PrivateKeyPackage } from 'ts-mls';
|
|
3
2
|
import type { VerifiedLedgerEntry } from './ledger.js';
|
|
4
3
|
export type GroupOptions = {
|
|
@@ -44,10 +43,6 @@ export type GroupOptions = {
|
|
|
44
43
|
* into the roster, everything else is handed to the consumer here.
|
|
45
44
|
*/
|
|
46
45
|
onLedgerEntries?: (entries: Array<VerifiedLedgerEntry>) => void;
|
|
47
|
-
/** Optional DID cache for resolving did:peer:4 issuers when verifying ledger entries. Default: in-memory. */
|
|
48
|
-
cache?: DIDCache;
|
|
49
|
-
/** Optional resolver for did:peer:4 short forms not in cache. */
|
|
50
|
-
resolver?: DIDResolver;
|
|
51
46
|
};
|
|
52
47
|
export type Invite = {
|
|
53
48
|
/** Group ID the invite is for */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kumiai/mls",
|
|
3
|
-
"version": "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/
|
|
29
|
-
"@
|
|
30
|
-
"@
|
|
31
|
-
"@noble/
|
|
32
|
-
"@
|
|
28
|
+
"@kokuin/capability": "^0.3.0",
|
|
29
|
+
"@kokuin/controller": "^0.1.0",
|
|
30
|
+
"@kokuin/token": "^0.5.0",
|
|
31
|
+
"@noble/ciphers": "^2.3.0",
|
|
32
|
+
"@noble/curves": "^2.3.0",
|
|
33
|
+
"@noble/hashes": "^2.3.0",
|
|
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
|
},
|