@kumiai/mls 0.11.0 → 0.12.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 +30 -1
- package/lib/authentication.d.ts +6 -2
- package/lib/authentication.js +207 -51
- package/lib/capability.d.ts +4 -0
- package/lib/capability.js +22 -10
- package/lib/controller-ingest.d.ts +19 -0
- package/lib/controller-ingest.js +117 -0
- package/lib/controller-log.d.ts +6 -3
- package/lib/controller-log.js +52 -13
- package/lib/credential.d.ts +8 -5
- package/lib/credential.js +32 -3
- package/lib/device-proof.d.ts +5 -0
- package/lib/embedded-resolver.d.ts +8 -18
- package/lib/embedded-resolver.js +52 -24
- package/lib/errors.d.ts +8 -2
- package/lib/errors.js +11 -0
- package/lib/group-commit.d.ts +11 -2
- package/lib/group-commit.js +154 -46
- package/lib/group-context.d.ts +4 -2
- package/lib/group-context.js +30 -6
- package/lib/group-create.js +44 -16
- package/lib/group-credential.js +22 -1
- package/lib/group-device.js +5 -15
- package/lib/group-handle.d.ts +12 -1
- package/lib/group-handle.js +89 -12
- package/lib/group-lifecycle.d.ts +31 -5
- package/lib/group-lifecycle.js +253 -155
- package/lib/group-membership.js +1 -1
- package/lib/group-welcome.d.ts +4 -0
- package/lib/group-welcome.js +14 -2
- package/lib/history.d.ts +6 -0
- package/lib/history.js +20 -1
- package/lib/index.d.ts +12 -7
- package/lib/index.js +11 -6
- package/lib/lifecycle-proof.d.ts +15 -6
- package/lib/lifecycle-proof.js +195 -126
- package/lib/lifecycle.d.ts +9 -6
- package/lib/lifecycle.js +194 -84
- package/lib/member-root.d.ts +88 -0
- package/lib/member-root.js +480 -0
- package/lib/recovery-verdict.d.ts +1 -1
- package/lib/recovery-verdict.js +30 -13
- package/lib/registry.d.ts +17 -6
- package/lib/registry.js +206 -20
- package/lib/stale-leaf.d.ts +2 -0
- package/lib/stale-leaf.js +9 -4
- package/lib/types.d.ts +3 -0
- package/package.json +4 -4
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
import { decodeKey, digestOf } from '@kokuin/controller';
|
|
2
|
+
import { normalizeDID } from '@kokuin/token';
|
|
3
|
+
import { capabilityKey, readCapability } from './capability.js';
|
|
4
|
+
import { effectDelta } from './controller-ingest.js';
|
|
5
|
+
import { foldControllerLog } from './controller-log.js';
|
|
6
|
+
import { LeafBindingError, RevokeProofError } from './errors.js';
|
|
7
|
+
/** Decode only; callers authenticate the tokens and membership references. */ export function memberRootsOf(binding, leafDID) {
|
|
8
|
+
const tokenRoot = (raw)=>{
|
|
9
|
+
const { iss, mbr } = readCapability(raw).payload;
|
|
10
|
+
return mbr == null ? {
|
|
11
|
+
kind: 'root',
|
|
12
|
+
controller: binding.id
|
|
13
|
+
} : {
|
|
14
|
+
kind: 'member',
|
|
15
|
+
did: iss,
|
|
16
|
+
add: mbr.add,
|
|
17
|
+
gen: mbr.g
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
if (binding.member != null) {
|
|
21
|
+
return [
|
|
22
|
+
{
|
|
23
|
+
slot: 'membership',
|
|
24
|
+
root: {
|
|
25
|
+
kind: 'member',
|
|
26
|
+
did: leafDID,
|
|
27
|
+
add: binding.member.add,
|
|
28
|
+
gen: binding.member.g
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
if (binding.capability == null) throw new LeafBindingError('controller-mismatch');
|
|
34
|
+
const { cap } = readCapability(binding.capability).payload;
|
|
35
|
+
const parent = Array.isArray(cap) ? cap[0] : cap;
|
|
36
|
+
const roots = [
|
|
37
|
+
parent == null ? {
|
|
38
|
+
slot: 'capability',
|
|
39
|
+
root: tokenRoot(binding.capability)
|
|
40
|
+
} : {
|
|
41
|
+
slot: 'parent',
|
|
42
|
+
root: tokenRoot(parent)
|
|
43
|
+
}
|
|
44
|
+
];
|
|
45
|
+
if (binding.holderGrant != null) roots.push({
|
|
46
|
+
slot: 'holder',
|
|
47
|
+
root: tokenRoot(binding.holderGrant)
|
|
48
|
+
});
|
|
49
|
+
return roots;
|
|
50
|
+
}
|
|
51
|
+
function foldedLog(controller, log) {
|
|
52
|
+
const folded = foldControllerLog(controller, log);
|
|
53
|
+
if (!folded.ok) throw new RevokeProofError('effects-mismatch');
|
|
54
|
+
return folded;
|
|
55
|
+
}
|
|
56
|
+
function denies(state, did) {
|
|
57
|
+
return [
|
|
58
|
+
...state.deny
|
|
59
|
+
].some((entry)=>normalizeDID(entry) === normalizeDID(did));
|
|
60
|
+
}
|
|
61
|
+
function effectProofs(did, log) {
|
|
62
|
+
const folded = foldedLog(did, log);
|
|
63
|
+
const events = new Map(log.events.map((signed)=>[
|
|
64
|
+
digestOf(signed.event),
|
|
65
|
+
signed
|
|
66
|
+
]));
|
|
67
|
+
const anchors = new Map(folded.states.map((state, index)=>[
|
|
68
|
+
state.digest,
|
|
69
|
+
index
|
|
70
|
+
]));
|
|
71
|
+
const views = new Map([
|
|
72
|
+
[
|
|
73
|
+
folded.head.epoch,
|
|
74
|
+
{
|
|
75
|
+
folded,
|
|
76
|
+
applied: new Set(folded.applied)
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
]);
|
|
80
|
+
const drops = new Map();
|
|
81
|
+
// A settle that carries a removal in snapshot `u` proves it even when no chain state held it live.
|
|
82
|
+
const tombstones = new Map();
|
|
83
|
+
const revokes = new Map();
|
|
84
|
+
const snapshots = new Map();
|
|
85
|
+
const denials = folded.states.map((state)=>new Set([
|
|
86
|
+
...state.deny
|
|
87
|
+
].map(normalizeDID)));
|
|
88
|
+
const rootOnly = log.chain.every(({ event }, index)=>event.t === (index === 0 ? 'icp' : 'rot'));
|
|
89
|
+
const append = (indices, target, index)=>{
|
|
90
|
+
const held = indices.get(target) ?? [];
|
|
91
|
+
held.push(index);
|
|
92
|
+
indices.set(target, held);
|
|
93
|
+
};
|
|
94
|
+
for(let index = 1; index < folded.states.length; index++){
|
|
95
|
+
const state = folded.states[index];
|
|
96
|
+
const prior = folded.states[index - 1];
|
|
97
|
+
if (state == null || prior == null || log.chain[index]?.event.t !== 'rot') continue;
|
|
98
|
+
for (const target of denials[index] ?? []){
|
|
99
|
+
if (state.gen > prior.gen) append(snapshots, target, index);
|
|
100
|
+
else if (rootOnly && state.gen === prior.gen && !denials[index - 1]?.has(target)) append(revokes, target, index);
|
|
101
|
+
}
|
|
102
|
+
if (state.gen !== prior.gen) continue;
|
|
103
|
+
for (const [add, member] of prior.members){
|
|
104
|
+
if (!member.removed && !state.members.has(add)) append(drops, add, index);
|
|
105
|
+
}
|
|
106
|
+
for (const add of state.tombstones?.keys() ?? []){
|
|
107
|
+
if (!prior.tombstones?.has(add)) append(tombstones, add, index);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const viewAt = (epoch)=>{
|
|
111
|
+
if (epoch == null) return views.get(folded.head.epoch);
|
|
112
|
+
const held = views.get(epoch);
|
|
113
|
+
if (held != null) return held;
|
|
114
|
+
const anchor = anchors.get(epoch);
|
|
115
|
+
if (anchor == null) return undefined;
|
|
116
|
+
const historical = foldControllerLog(did, {
|
|
117
|
+
chain: log.chain.slice(0, anchor + 1),
|
|
118
|
+
events: log.events
|
|
119
|
+
});
|
|
120
|
+
const view = {
|
|
121
|
+
folded: historical,
|
|
122
|
+
applied: new Set(historical.ok ? historical.applied : [])
|
|
123
|
+
};
|
|
124
|
+
views.set(epoch, view);
|
|
125
|
+
return view;
|
|
126
|
+
};
|
|
127
|
+
return {
|
|
128
|
+
folded,
|
|
129
|
+
events,
|
|
130
|
+
drops,
|
|
131
|
+
tombstones,
|
|
132
|
+
revokes,
|
|
133
|
+
snapshots,
|
|
134
|
+
viewAt
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function checkEffect(log, claim, genFloor, proofs) {
|
|
138
|
+
let folded = proofs.folded;
|
|
139
|
+
const ref = claim.ref;
|
|
140
|
+
if ('event' in ref) {
|
|
141
|
+
const view = proofs.viewAt(ref.epoch);
|
|
142
|
+
if (view == null) throw new RevokeProofError('no-rev');
|
|
143
|
+
if (!view.folded.ok) throw new RevokeProofError('effects-mismatch');
|
|
144
|
+
folded = view.folded;
|
|
145
|
+
const signed = proofs.events.get(ref.event);
|
|
146
|
+
if (signed == null) throw new RevokeProofError('no-rev');
|
|
147
|
+
const event = signed.event;
|
|
148
|
+
if (!view.applied.has(ref.event)) throw new RevokeProofError('effects-mismatch');
|
|
149
|
+
if (event.g < genFloor) throw new RevokeProofError('generation-floor');
|
|
150
|
+
if (claim.kind === 'deny') {
|
|
151
|
+
if (event.t !== 'rev' || normalizeDID(event.x) !== normalizeDID(claim.did)) throw new RevokeProofError('effects-mismatch');
|
|
152
|
+
} else {
|
|
153
|
+
const member = folded.head.members.get(claim.member.add);
|
|
154
|
+
if (event.t !== 'rmv' || event.add !== claim.member.add || member == null || normalizeDID(member.did) !== normalizeDID(claim.member.did)) throw new RevokeProofError('effects-mismatch');
|
|
155
|
+
}
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const index = 'snapshot' in ref ? ref.snapshot : ref.chainIndex;
|
|
159
|
+
const state = folded.states[index];
|
|
160
|
+
const prior = folded.states[index - 1];
|
|
161
|
+
if (state == null || prior == null || log.chain[index]?.event.t !== 'rot') throw new RevokeProofError('no-rev');
|
|
162
|
+
if ('snapshot' in ref) {
|
|
163
|
+
if (claim.kind !== 'deny' || state.gen <= prior.gen || !denies(state, claim.did)) throw new RevokeProofError('effects-mismatch');
|
|
164
|
+
} else if (claim.kind === 'deny') {
|
|
165
|
+
if (!proofs.revokes.get(normalizeDID(claim.did))?.includes(index)) throw new RevokeProofError('no-rev');
|
|
166
|
+
} else {
|
|
167
|
+
const member = prior.members.get(claim.member.add);
|
|
168
|
+
if (member != null && normalizeDID(member.did) !== normalizeDID(claim.member.did)) throw new RevokeProofError('effects-mismatch');
|
|
169
|
+
if (!proofs.drops.get(claim.member.add)?.includes(index)) {
|
|
170
|
+
const tombstone = state.tombstones?.get(claim.member.add);
|
|
171
|
+
if (tombstone == null || !proofs.tombstones.get(claim.member.add)?.includes(index)) throw new RevokeProofError('no-rev');
|
|
172
|
+
if (normalizeDID(tombstone.did) !== normalizeDID(claim.member.did)) throw new RevokeProofError('effects-mismatch');
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (state.gen < genFloor) throw new RevokeProofError('generation-floor');
|
|
176
|
+
}
|
|
177
|
+
export function effectOf(controller, log, claim, genFloor) {
|
|
178
|
+
checkEffect(log, claim, genFloor, effectProofs(normalizeDID(controller), log));
|
|
179
|
+
}
|
|
180
|
+
export function effectProofContext(controller, log) {
|
|
181
|
+
const did = normalizeDID(controller);
|
|
182
|
+
const proofs = effectProofs(did, log);
|
|
183
|
+
return {
|
|
184
|
+
effectOf: (claim, genFloor)=>checkEffect(log, claim, genFloor, proofs),
|
|
185
|
+
claimsFor: (recorded, genFloor)=>claimsWithProofs(did, recorded, log, genFloor, proofs)
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
export function claimsFor(controller, recorded, merged, genFloor) {
|
|
189
|
+
return effectProofContext(controller, merged).claimsFor(recorded, genFloor);
|
|
190
|
+
}
|
|
191
|
+
function claimsWithProofs(did, recorded, merged, genFloor, proofs) {
|
|
192
|
+
const before = recorded == null ? undefined : foldedLog(did, recorded);
|
|
193
|
+
const after = proofs.folded;
|
|
194
|
+
const delta = effectDelta(before?.head, after.head);
|
|
195
|
+
const eventClaims = new Map();
|
|
196
|
+
// Authenticate each supplied epoch once, including retained pre-settlement closures.
|
|
197
|
+
for (const [digest, { event }] of proofs.events){
|
|
198
|
+
if (event.t !== 'rev' && event.t !== 'rmv') continue;
|
|
199
|
+
const view = proofs.viewAt(event.e);
|
|
200
|
+
if (view == null || !view.folded.ok || !view.applied.has(digest)) continue;
|
|
201
|
+
const ref = {
|
|
202
|
+
event: digest,
|
|
203
|
+
...event.e === after.head.epoch ? {} : {
|
|
204
|
+
epoch: event.e
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
let target;
|
|
208
|
+
let claim;
|
|
209
|
+
if (event.t === 'rev') {
|
|
210
|
+
target = `deny:${normalizeDID(event.x)}`;
|
|
211
|
+
claim = {
|
|
212
|
+
kind: 'deny',
|
|
213
|
+
did: normalizeDID(event.x),
|
|
214
|
+
ref
|
|
215
|
+
};
|
|
216
|
+
} else {
|
|
217
|
+
const member = view.folded.head.members.get(event.add);
|
|
218
|
+
if (member == null) continue;
|
|
219
|
+
target = `remove:${event.add}`;
|
|
220
|
+
claim = {
|
|
221
|
+
kind: 'remove',
|
|
222
|
+
member: {
|
|
223
|
+
did: normalizeDID(member.did),
|
|
224
|
+
add: event.add
|
|
225
|
+
},
|
|
226
|
+
ref
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
const held = eventClaims.get(target) ?? [];
|
|
230
|
+
held.push({
|
|
231
|
+
claim,
|
|
232
|
+
gen: event.g
|
|
233
|
+
});
|
|
234
|
+
eventClaims.set(target, held);
|
|
235
|
+
}
|
|
236
|
+
const removed = new Map(delta.removed.map((member)=>[
|
|
237
|
+
member.add,
|
|
238
|
+
member
|
|
239
|
+
]));
|
|
240
|
+
if (before == null) {
|
|
241
|
+
for (const state of [
|
|
242
|
+
...after.states,
|
|
243
|
+
after.head
|
|
244
|
+
]){
|
|
245
|
+
for (const [add, member] of state.members){
|
|
246
|
+
if (after.head.members.get(add)?.effective) continue;
|
|
247
|
+
if (member.removed || proofs.drops.has(add)) removed.set(add, {
|
|
248
|
+
did: normalizeDID(member.did),
|
|
249
|
+
add
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const claims = [
|
|
255
|
+
...delta.denied.map((did)=>({
|
|
256
|
+
kind: 'deny',
|
|
257
|
+
did
|
|
258
|
+
})),
|
|
259
|
+
...[
|
|
260
|
+
...removed.values()
|
|
261
|
+
].sort((a, b)=>a.add < b.add ? -1 : a.add > b.add ? 1 : 0).map((member)=>({
|
|
262
|
+
kind: 'remove',
|
|
263
|
+
member
|
|
264
|
+
}))
|
|
265
|
+
];
|
|
266
|
+
return claims.flatMap((claim)=>{
|
|
267
|
+
const floor = claim.kind === 'deny' ? Math.max(genFloor, after.head.gen) : genFloor;
|
|
268
|
+
const target = claim.kind === 'deny' ? `deny:${claim.did}` : `remove:${claim.member.add}`;
|
|
269
|
+
const event = eventClaims.get(target)?.find(({ claim: candidate, gen })=>{
|
|
270
|
+
return gen >= floor && (claim.kind === 'deny' || candidate.kind === 'remove' && candidate.member.did === claim.member.did);
|
|
271
|
+
});
|
|
272
|
+
if (event != null) {
|
|
273
|
+
checkEffect(merged, event.claim, floor, proofs);
|
|
274
|
+
return [
|
|
275
|
+
event.claim
|
|
276
|
+
];
|
|
277
|
+
}
|
|
278
|
+
const atFloor = (index)=>(after.states[index]?.gen ?? -1) >= floor;
|
|
279
|
+
// Drops before tombstones: a 0.12.0 receiver accepts only the drop.
|
|
280
|
+
const index = claim.kind === 'deny' ? proofs.revokes.get(claim.did)?.find(atFloor) : proofs.drops.get(claim.member.add)?.find(atFloor) ?? proofs.tombstones.get(claim.member.add)?.find(atFloor);
|
|
281
|
+
let ref = index == null ? undefined : {
|
|
282
|
+
chainIndex: index
|
|
283
|
+
};
|
|
284
|
+
if (claim.kind === 'deny') {
|
|
285
|
+
const snapshot = proofs.snapshots.get(claim.did)?.find((index)=>(after.states[index]?.gen ?? -1) >= floor);
|
|
286
|
+
if (ref == null && snapshot != null) ref = {
|
|
287
|
+
snapshot
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
if (ref != null) {
|
|
291
|
+
const candidate = {
|
|
292
|
+
...claim,
|
|
293
|
+
ref
|
|
294
|
+
};
|
|
295
|
+
checkEffect(merged, candidate, floor, proofs);
|
|
296
|
+
return [
|
|
297
|
+
candidate
|
|
298
|
+
];
|
|
299
|
+
}
|
|
300
|
+
// Generation changes and DID denials end authority without an explicit removal proof.
|
|
301
|
+
if (claim.kind === 'remove' && before != null && !after.head.members.get(claim.member.add)?.removed && !proofs.drops.has(claim.member.add) && !eventClaims.has(target) && (before.head.gen !== after.head.gen || denies(after.head, claim.member.did))) return [];
|
|
302
|
+
throw new RevokeProofError('no-rev');
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
const projectionFolds = new WeakMap();
|
|
306
|
+
function projectionFold(controller, projection) {
|
|
307
|
+
if (projection?.recordedLog == null) return undefined;
|
|
308
|
+
let folded = projectionFolds.get(projection);
|
|
309
|
+
if (folded == null) {
|
|
310
|
+
folded = foldControllerLog(controller, projection.recordedLog);
|
|
311
|
+
projectionFolds.set(projection, folded);
|
|
312
|
+
}
|
|
313
|
+
return folded;
|
|
314
|
+
}
|
|
315
|
+
export function effectiveAttestingMembers(head, projection, time) {
|
|
316
|
+
if (head == null || head.saturated || head.gen < (projection?.genFloor ?? 0)) return [];
|
|
317
|
+
return [
|
|
318
|
+
...head.members
|
|
319
|
+
].filter(([add, member])=>member.effective && !member.removed && !projection?.removedMembers.has(add) && (time == null || time <= member.lease));
|
|
320
|
+
}
|
|
321
|
+
export function memberRootStatus(registry, controller, root) {
|
|
322
|
+
if (root.kind === 'root') return {
|
|
323
|
+
status: 'root'
|
|
324
|
+
};
|
|
325
|
+
const did = normalizeDID(controller);
|
|
326
|
+
const projection = registry.controllers.get(did);
|
|
327
|
+
if (projection?.removedMembers.has(root.add)) return {
|
|
328
|
+
status: 'removed'
|
|
329
|
+
};
|
|
330
|
+
const folded = projectionFold(did, projection);
|
|
331
|
+
if (folded == null) return {
|
|
332
|
+
status: 'unavailable',
|
|
333
|
+
why: 'no-log'
|
|
334
|
+
};
|
|
335
|
+
if (!folded.ok) return {
|
|
336
|
+
status: 'unavailable',
|
|
337
|
+
why: 'prefix'
|
|
338
|
+
};
|
|
339
|
+
if (root.gen !== folded.head.gen || root.gen < (projection?.genFloor ?? 0)) return {
|
|
340
|
+
status: 'removed'
|
|
341
|
+
};
|
|
342
|
+
if (folded.head.saturated) return {
|
|
343
|
+
status: 'unavailable',
|
|
344
|
+
why: 'saturated'
|
|
345
|
+
};
|
|
346
|
+
const member = folded.head.members.get(root.add);
|
|
347
|
+
if (member == null) return {
|
|
348
|
+
status: 'unavailable',
|
|
349
|
+
why: 'unknown-incarnation'
|
|
350
|
+
};
|
|
351
|
+
if (member.removed || !member.effective || normalizeDID(member.did) !== normalizeDID(root.did)) return {
|
|
352
|
+
status: 'removed'
|
|
353
|
+
};
|
|
354
|
+
return {
|
|
355
|
+
status: 'effective',
|
|
356
|
+
key: decodeKey(member.key).publicKey,
|
|
357
|
+
lease: member.lease
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
function substituteRoot(raw, leaf) {
|
|
361
|
+
try {
|
|
362
|
+
const { payload } = readCapability(raw);
|
|
363
|
+
const key = capabilityKey(payload).publicKey;
|
|
364
|
+
if (normalizeDID(payload.aud) !== normalizeDID(leaf.did) || key.length !== leaf.leafKey.length || key.some((byte, index)=>byte !== leaf.leafKey[index])) return undefined;
|
|
365
|
+
return payload.mbr == null ? {
|
|
366
|
+
kind: 'root',
|
|
367
|
+
controller: leaf.binding.id
|
|
368
|
+
} : {
|
|
369
|
+
kind: 'member',
|
|
370
|
+
did: payload.iss,
|
|
371
|
+
add: payload.mbr.add,
|
|
372
|
+
gen: payload.mbr.g
|
|
373
|
+
};
|
|
374
|
+
} catch {
|
|
375
|
+
return undefined;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
export function effectiveAuthorityOf(leaf, registry, controller) {
|
|
379
|
+
const did = normalizeDID(controller);
|
|
380
|
+
if (normalizeDID(leaf.binding.id) !== did) return {
|
|
381
|
+
status: 'unavailable'
|
|
382
|
+
};
|
|
383
|
+
const roots = memberRootsOf(leaf.binding, leaf.did);
|
|
384
|
+
const primary = roots[0];
|
|
385
|
+
if (primary == null) return {
|
|
386
|
+
status: 'unavailable'
|
|
387
|
+
};
|
|
388
|
+
const status = memberRootStatus(registry, did, primary.root);
|
|
389
|
+
const projection = registry.controllers.get(did);
|
|
390
|
+
const chain = projection?.recordedLog?.chain;
|
|
391
|
+
if (chain != null && leaf.binding.prefix.some((signed, index)=>{
|
|
392
|
+
const recorded = chain[index];
|
|
393
|
+
return recorded == null || digestOf(recorded.event) !== digestOf(signed.event);
|
|
394
|
+
})) return {
|
|
395
|
+
status: status.status === 'removed' ? 'removed' : 'unavailable'
|
|
396
|
+
};
|
|
397
|
+
if (status.status === 'root' || status.status === 'effective') {
|
|
398
|
+
const holder = roots.find(({ slot })=>slot === 'holder');
|
|
399
|
+
if (holder != null) {
|
|
400
|
+
const held = memberRootStatus(registry, did, holder.root);
|
|
401
|
+
if (held.status === 'removed' || held.status === 'unavailable') return {
|
|
402
|
+
status: held.status
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
return primary;
|
|
406
|
+
}
|
|
407
|
+
if (status.status === 'unavailable') return {
|
|
408
|
+
status: 'unavailable'
|
|
409
|
+
};
|
|
410
|
+
const substitutes = [
|
|
411
|
+
{
|
|
412
|
+
slot: 'holder',
|
|
413
|
+
raw: leaf.binding.holderGrant
|
|
414
|
+
},
|
|
415
|
+
{
|
|
416
|
+
slot: 'survivor',
|
|
417
|
+
raw: projection?.survivorGrants.get(normalizeDID(leaf.did))
|
|
418
|
+
}
|
|
419
|
+
];
|
|
420
|
+
for (const { slot, raw } of substitutes){
|
|
421
|
+
if (raw == null) continue;
|
|
422
|
+
const root = substituteRoot(raw, leaf);
|
|
423
|
+
if (root == null) continue;
|
|
424
|
+
const status = memberRootStatus(registry, did, root);
|
|
425
|
+
if (status.status === 'root' || status.status === 'effective') return {
|
|
426
|
+
root,
|
|
427
|
+
slot,
|
|
428
|
+
expiry: readCapability(raw).payload.exp
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
return {
|
|
432
|
+
status: 'removed'
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
export function cascadeOf(leaves, claims, before, after, controller) {
|
|
436
|
+
const revoked = claims.filter((claim)=>claim.kind === 'deny').map((claim)=>({
|
|
437
|
+
did: normalizeDID(claim.did)
|
|
438
|
+
}));
|
|
439
|
+
const direct = new Set(revoked.map(({ did })=>did));
|
|
440
|
+
const affected = new Set(direct);
|
|
441
|
+
const removed = [];
|
|
442
|
+
const removeLeafIndices = [];
|
|
443
|
+
for (const leaf of leaves){
|
|
444
|
+
const did = normalizeDID(leaf.did);
|
|
445
|
+
if (normalizeDID(leaf.binding.id) !== normalizeDID(controller)) continue;
|
|
446
|
+
const capability = leaf.binding.capability == null ? undefined : readCapability(leaf.binding.capability).payload;
|
|
447
|
+
const issuer = capability?.cap == null ? undefined : normalizeDID(capability.iss);
|
|
448
|
+
const holderRoot = leaf.binding.holderGrant == null ? undefined : substituteRoot(leaf.binding.holderGrant, leaf);
|
|
449
|
+
const holderStatus = holderRoot == null ? undefined : memberRootStatus(after, controller, holderRoot);
|
|
450
|
+
const authority = effectiveAuthorityOf(leaf, after, controller);
|
|
451
|
+
const evidenced = !direct.has(did) && after.devices.get(did)?.status !== 'revoked' && (holderStatus?.status === 'root' || holderStatus?.status === 'effective' || 'root' in authority && authority.slot === 'survivor');
|
|
452
|
+
if (issuer != null && direct.has(issuer) && !direct.has(did) && !evidenced) {
|
|
453
|
+
if (!affected.has(did)) revoked.push({
|
|
454
|
+
did,
|
|
455
|
+
cascadedFrom: issuer
|
|
456
|
+
});
|
|
457
|
+
affected.add(did);
|
|
458
|
+
}
|
|
459
|
+
if (affected.has(did)) {
|
|
460
|
+
removeLeafIndices.push(leaf.leafIndex);
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
if (!('status' in authority) || authority.status !== 'removed') continue;
|
|
464
|
+
const prior = effectiveAuthorityOf(leaf, before, controller);
|
|
465
|
+
const roots = memberRootsOf(leaf.binding, leaf.did).map(({ root })=>root);
|
|
466
|
+
if ('root' in prior) roots.unshift(prior.root);
|
|
467
|
+
const root = roots.find((root)=>root.kind === 'member' && memberRootStatus(after, controller, root).status === 'removed');
|
|
468
|
+
if (root?.kind !== 'member') continue;
|
|
469
|
+
removed.push({
|
|
470
|
+
did,
|
|
471
|
+
cascadedFrom: normalizeDID(root.did)
|
|
472
|
+
});
|
|
473
|
+
removeLeafIndices.push(leaf.leafIndex);
|
|
474
|
+
}
|
|
475
|
+
return {
|
|
476
|
+
revoked,
|
|
477
|
+
removed,
|
|
478
|
+
removeLeafIndices
|
|
479
|
+
};
|
|
480
|
+
}
|
|
@@ -35,7 +35,7 @@ export declare function sealRecoveryVerdict(params: SealRecoveryVerdictParams):
|
|
|
35
35
|
export declare function openRecoveryVerdict(params: OpenSealedGroupInfoParams): Promise<OpenedRecoveryVerdict>;
|
|
36
36
|
export type RecoverySignerEligibleParams = {
|
|
37
37
|
group: GroupHandle;
|
|
38
|
-
|
|
38
|
+
registry: DeviceRegistry;
|
|
39
39
|
signer: string;
|
|
40
40
|
/** Seconds since epoch; defaults to the current time. */
|
|
41
41
|
now?: number;
|
package/lib/recovery-verdict.js
CHANGED
|
@@ -3,6 +3,8 @@ import { hmac } from '@noble/hashes/hmac.js';
|
|
|
3
3
|
import { sha256 } from '@noble/hashes/sha2.js';
|
|
4
4
|
import { readCapability } from './capability.js';
|
|
5
5
|
import { foldChain } from './controller-log.js';
|
|
6
|
+
import { attestedTreeTime, leafExpiry } from './lifecycle.js';
|
|
7
|
+
import { effectiveAuthorityOf, memberRootStatus } from './member-root.js';
|
|
6
8
|
import { openSealedReply, sealToRequest, verifyRecoveryRequest } from './recovery.js';
|
|
7
9
|
const utf8 = new TextEncoder();
|
|
8
10
|
const VERDICT_TYPE = 'kumiai.recovery-verdict';
|
|
@@ -109,26 +111,41 @@ export async function openRecoveryVerdict(params) {
|
|
|
109
111
|
};
|
|
110
112
|
}
|
|
111
113
|
export function recoverySignerEligible(params) {
|
|
112
|
-
const { group,
|
|
114
|
+
const { group, registry, signer, now = Date.now() / 1000 } = params;
|
|
113
115
|
const controller = group.anchor.controller;
|
|
114
116
|
if (controller == null) return false;
|
|
115
117
|
try {
|
|
116
118
|
const binding = group.bindingOfDID(signer);
|
|
117
|
-
if (binding?.controller !== controller || binding.
|
|
118
|
-
const payload = readCapability(binding.capability).payload;
|
|
119
|
-
if (
|
|
119
|
+
if (binding?.controller !== controller || binding.prefix == null) return false;
|
|
120
|
+
const payload = binding.member != null || binding.capability == null ? undefined : readCapability(binding.capability).payload;
|
|
121
|
+
if (registry.devices.get(normalizeDID(signer))?.status === 'revoked' || payload?.cap != null && registry.devices.get(normalizeDID(payload.iss))?.status === 'revoked') return false;
|
|
120
122
|
const folded = foldChain(controller, binding.prefix);
|
|
121
|
-
const projection =
|
|
123
|
+
const projection = registry.controllers.get(normalizeDID(controller));
|
|
122
124
|
if (!folded.ok || (folded.states.at(-1)?.gen ?? 0) < (projection?.genFloor ?? 0)) return false;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
125
|
+
const bound = {
|
|
126
|
+
...binding,
|
|
127
|
+
id: controller,
|
|
128
|
+
prefix: binding.prefix
|
|
129
|
+
};
|
|
130
|
+
const authority = effectiveAuthorityOf({
|
|
131
|
+
did: signer,
|
|
132
|
+
binding: bound,
|
|
133
|
+
leafKey: binding.leafKey
|
|
134
|
+
}, registry, controller);
|
|
135
|
+
if ('status' in authority) return false;
|
|
136
|
+
if (binding.member != null && authority.slot === 'membership') {
|
|
137
|
+
const status = memberRootStatus(registry, controller, {
|
|
138
|
+
kind: 'member',
|
|
139
|
+
did: signer,
|
|
140
|
+
add: binding.member.add,
|
|
141
|
+
gen: binding.member.g
|
|
142
|
+
});
|
|
143
|
+
if (status.status !== 'effective' || status.key.length !== binding.leafKey.length || status.key.some((byte, index)=>byte !== binding.leafKey[index])) return false;
|
|
130
144
|
}
|
|
131
|
-
|
|
145
|
+
const expiry = leafExpiry(bound, registry, controller);
|
|
146
|
+
if (expiry == null) return false;
|
|
147
|
+
const time = attestedTreeTime(group.state.ratchetTree, registry, controller);
|
|
148
|
+
return expiry >= Math.max(time, now - 300);
|
|
132
149
|
} catch {
|
|
133
150
|
return false;
|
|
134
151
|
}
|
package/lib/registry.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type ControllerLog } from '@kokuin/controller';
|
|
2
2
|
import type { GroupAnchor } from './anchor.js';
|
|
3
3
|
import type { MLSCredentialIdentity } from './credential.js';
|
|
4
4
|
import type { FoldDrop, FoldInput } from './fold.js';
|
|
5
5
|
import type { GroupHandle } from './group-handle.js';
|
|
6
6
|
import type { VerifiedLedgerEntry } from './ledger.js';
|
|
7
|
+
import { type EffectClaim, type ProofRef, type RemovedEffect, type RemovedMember } from './member-root.js';
|
|
7
8
|
import { type RosterState } from './roster.js';
|
|
8
9
|
/** The reserved control type carrying a device-registry mutation. One branch, one namespace slot. */
|
|
9
10
|
export declare const DEVICE_ENTRY_TYPE = "kumiai.device";
|
|
10
11
|
/** The device lifecycle operations plus the advisory controller-log beacon. */
|
|
11
|
-
export type DeviceOp = 'register' | 'add' | 'revoke' | 'label' | 'beacon' | 'reset' | 'clock';
|
|
12
|
+
export type DeviceOp = 'register' | 'add' | 'revoke' | 'label' | 'beacon' | 'reset' | 'clock' | 'log' | 'seed';
|
|
12
13
|
/**
|
|
13
14
|
* A `kumiai.device` entry's `value`. `controller` names the profile a register/add binds to;
|
|
14
15
|
* `label` renames; `capability` carries the management-capability proof a manage op is verified
|
|
@@ -19,9 +20,13 @@ export type DeviceValue = {
|
|
|
19
20
|
controller?: string;
|
|
20
21
|
label?: string;
|
|
21
22
|
capability?: string;
|
|
22
|
-
proof?:
|
|
23
|
+
proof?: ControllerLog;
|
|
24
|
+
effects?: Array<EffectClaim>;
|
|
25
|
+
removed?: Array<RemovedEffect>;
|
|
26
|
+
survivors?: Array<string>;
|
|
23
27
|
revoked?: Array<RevokedEffect>;
|
|
24
28
|
time?: number;
|
|
29
|
+
attest?: true;
|
|
25
30
|
/** Beacon only: the length of the controller's FULL log at announcement time. */
|
|
26
31
|
logLength?: number;
|
|
27
32
|
/** Beacon only: the head digest of the controller's FULL log at announcement time. */
|
|
@@ -33,7 +38,7 @@ export type RevokedEffect = {
|
|
|
33
38
|
};
|
|
34
39
|
export type Revocation = {
|
|
35
40
|
controller: string;
|
|
36
|
-
|
|
41
|
+
proofRef: ProofRef;
|
|
37
42
|
reason?: 'reset';
|
|
38
43
|
cascadedFrom?: string;
|
|
39
44
|
};
|
|
@@ -42,14 +47,17 @@ export type DeviceRecord = {
|
|
|
42
47
|
controller: string;
|
|
43
48
|
status: 'active' | 'revoked';
|
|
44
49
|
label?: string;
|
|
45
|
-
|
|
50
|
+
proofRef?: ProofRef;
|
|
46
51
|
reason?: 'reset';
|
|
47
52
|
cascadedFrom?: string;
|
|
48
53
|
};
|
|
49
54
|
export type ControllerProjection = {
|
|
50
|
-
recordedLog
|
|
55
|
+
recordedLog?: ControllerLog;
|
|
51
56
|
genFloor: number;
|
|
52
57
|
timeFloor: number;
|
|
58
|
+
attestations: ReadonlyMap<string, number>;
|
|
59
|
+
removedMembers: ReadonlyMap<string, RemovedMember>;
|
|
60
|
+
survivorGrants: ReadonlyMap<string, string>;
|
|
53
61
|
beacon?: ControllerBeacon;
|
|
54
62
|
};
|
|
55
63
|
export declare function controllerProjection(): ControllerProjection;
|
|
@@ -86,6 +94,9 @@ export declare function mayReadmitResetDevice(registry: DeviceRegistry, parsed:
|
|
|
86
94
|
export declare function controllerOf(registry: DeviceRegistry, deviceDID: string): string | undefined;
|
|
87
95
|
/** The advisory beacon a controller last announced, or undefined. Never a validation input. */
|
|
88
96
|
export declare function beaconOf(registry: DeviceRegistry, controllerDID: string): ControllerBeacon | undefined;
|
|
97
|
+
export declare function recordedLogOf(registry: DeviceRegistry, controller: string): ControllerLog | undefined;
|
|
98
|
+
export declare function assertRetainsKnownEffects(controller: string, known: DeviceRegistry, candidate: DeviceRegistry): void;
|
|
99
|
+
export declare function registryForLog(controller: string, log: ControllerLog): DeviceRegistry;
|
|
89
100
|
/**
|
|
90
101
|
* The deny set Slice 1's seam consumes: the device DIDs at `status: 'revoked'` NOW. Matched, never
|
|
91
102
|
* enumerated by consumers (`has`), holding device DIDs only, per the kokuin deny-set rule.
|