@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
package/lib/group-lifecycle.js
CHANGED
|
@@ -1,18 +1,77 @@
|
|
|
1
|
-
import { didFromInception } from '@kokuin/controller';
|
|
1
|
+
import { didFromInception, digestOf, pruneLog } from '@kokuin/controller';
|
|
2
2
|
import { createSigningIdentityForDID, normalizeDID } from '@kokuin/token';
|
|
3
3
|
import { defaultProposalTypes, encode, isDefaultProposal, mlsMessageEncoder, nodeTypes } from 'ts-mls';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { effectDelta, ingestControllerLog } from './controller-ingest.js';
|
|
5
|
+
import { closureOf, foldControllerLog, isDetachedFoldReason } from './controller-log.js';
|
|
6
6
|
import { parseMLSCredentialIdentity } from './credential.js';
|
|
7
|
+
import { foldEnvelope } from './envelope-fold.js';
|
|
7
8
|
import { LeafBindingError, RevokeProofError } from './errors.js';
|
|
8
9
|
import { commitWithEntries } from './group-commit.js';
|
|
9
10
|
import { assertBindingAuthorTime } from './group-credential.js';
|
|
10
11
|
import { deriveGroup, mutexFor } from './group-handle.js';
|
|
11
12
|
import { HISTORY_HORIZON, historySize } from './history.js';
|
|
12
|
-
import { signLedgerEntry } from './ledger.js';
|
|
13
|
-
import { isLapsed, leafAt, treeTime, validateEntry } from './lifecycle.js';
|
|
14
|
-
import { authenticateLifecycleProof,
|
|
15
|
-
import {
|
|
13
|
+
import { ledgerEntryDigest, signLedgerEntry, verifyLedgerEntry } from './ledger.js';
|
|
14
|
+
import { attestedTreeTime, isLapsed, leafAt, treeTime, validateEntry } from './lifecycle.js';
|
|
15
|
+
import { authenticateLifecycleProof, lifecycleProofEffects, lifecycleProofLog, mergeLifecycleProofLogs, verifyLifecycleProof } from './lifecycle-proof.js';
|
|
16
|
+
import { claimsFor, effectiveAttestingMembers, effectiveAuthorityOf } from './member-root.js';
|
|
17
|
+
import { DEVICE_ENTRY_TYPE, isResetOnly, recordedLogOf } from './registry.js';
|
|
18
|
+
export function controllerLogDelta(group, log) {
|
|
19
|
+
const controller = group.anchor.controller;
|
|
20
|
+
if (controller == null) throw new RevokeProofError('wrong-controller');
|
|
21
|
+
const recorded = recordedLogOf(group.registry, controller);
|
|
22
|
+
if (recorded == null) throw new RevokeProofError('detached');
|
|
23
|
+
const first = log.chain[0]?.event;
|
|
24
|
+
if (first == null) throw new RevokeProofError('no-rev');
|
|
25
|
+
if (first.t === 'icp' && normalizeDID(didFromInception(first)) !== normalizeDID(controller) || log.chain.some(({ event })=>event.t !== 'icp' && (event.i == null || normalizeDID(event.i) !== normalizeDID(controller)))) throw new RevokeProofError('wrong-controller');
|
|
26
|
+
const folded = foldControllerLog(controller, log);
|
|
27
|
+
if (!folded.ok) throw new RevokeProofError(isDetachedFoldReason(folded.reason) ? 'detached' : 'not-authority-signed');
|
|
28
|
+
const pruned = pruneLog(controller, log);
|
|
29
|
+
const incoming = ingestControllerLog(controller, undefined, pruned);
|
|
30
|
+
const previous = ingestControllerLog(controller, undefined, recorded);
|
|
31
|
+
const reset = incoming.after.head.gen > previous.after.head.gen;
|
|
32
|
+
const merged = incoming.log;
|
|
33
|
+
const common = reset ? 1 : Math.min(recorded.chain.length, merged.chain.length);
|
|
34
|
+
for(let index = 0; index < common; index++){
|
|
35
|
+
const prior = recorded.chain[index];
|
|
36
|
+
const next = merged.chain[index];
|
|
37
|
+
if (prior == null || next == null || digestOf(prior.event) !== digestOf(next.event)) throw new RevokeProofError('detached');
|
|
38
|
+
}
|
|
39
|
+
const seen = new Set(recorded.events.map(({ event })=>digestOf(event)));
|
|
40
|
+
const delta = {
|
|
41
|
+
chain: reset ? merged.chain : merged.chain.slice(recorded.chain.length),
|
|
42
|
+
events: merged.events.filter(({ event })=>!seen.has(digestOf(event)))
|
|
43
|
+
};
|
|
44
|
+
if (delta.chain.length === 0 && delta.events.length === 0) return null;
|
|
45
|
+
ingestControllerLog(controller, recorded, delta);
|
|
46
|
+
return delta;
|
|
47
|
+
}
|
|
48
|
+
export async function publishControllerLog(group, log) {
|
|
49
|
+
return mutexFor(group).run(async ()=>{
|
|
50
|
+
const delta = controllerLogDelta(group, log);
|
|
51
|
+
if (delta == null) return null;
|
|
52
|
+
const controller = group.anchor.controller;
|
|
53
|
+
if (controller == null) throw new RevokeProofError('wrong-controller');
|
|
54
|
+
const ingested = ingestControllerLog(controller, recordedLogOf(group.registry, controller), delta, group.registry.controllers.get(normalizeDID(controller)));
|
|
55
|
+
const effects = effectDelta(ingested.before?.head, ingested.after.head);
|
|
56
|
+
if (effects.generation || effects.denied.length > 0 || effects.removed.length > 0) throw new RevokeProofError('effects-mismatch');
|
|
57
|
+
const token = await signLedgerEntry(ownIdentity(group), {
|
|
58
|
+
type: DEVICE_ENTRY_TYPE,
|
|
59
|
+
groupID: group.groupID,
|
|
60
|
+
subject: controller,
|
|
61
|
+
value: {
|
|
62
|
+
op: 'log',
|
|
63
|
+
proof: delta
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
return buildResult({
|
|
67
|
+
group,
|
|
68
|
+
proposals: [],
|
|
69
|
+
tokens: [
|
|
70
|
+
token
|
|
71
|
+
]
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
16
75
|
/**
|
|
17
76
|
* Author a by-reference Remove for this handle's own leaf at its current epoch.
|
|
18
77
|
* Once a member processes a pending self-removal, application encrypt throws until
|
|
@@ -29,11 +88,7 @@ import { DEVICE_ENTRY_TYPE, isResetOnly } from './registry.js';
|
|
|
29
88
|
return mutexFor(group).run(async ()=>{
|
|
30
89
|
const pendingProposals = Object.fromEntries(Object.entries(group.state.unappliedProposals).filter(([, { proposal, senderLeafIndex }])=>isDefaultProposal(proposal) && proposal.proposalType === defaultProposalTypes.remove && proposal.remove.removed === senderLeafIndex && senderLeafIndex !== group.state.privatePath.leafIndex));
|
|
31
90
|
if (Object.keys(pendingProposals).length === 0) return null;
|
|
32
|
-
const
|
|
33
|
-
for (const { proposal } of Object.values(pendingProposals)){
|
|
34
|
-
if (isDefaultProposal(proposal) && proposal.proposalType === defaultProposalTypes.remove) tree[proposal.remove.removed * 2] = undefined;
|
|
35
|
-
}
|
|
36
|
-
const tokens = await clockEntries(group, tree);
|
|
91
|
+
const tokens = [];
|
|
37
92
|
const result = await commitWithEntries({
|
|
38
93
|
group,
|
|
39
94
|
extraProposals: [],
|
|
@@ -42,7 +97,7 @@ import { DEVICE_ENTRY_TYPE, isResetOnly } from './registry.js';
|
|
|
42
97
|
requireAdmin: false
|
|
43
98
|
});
|
|
44
99
|
const newGroup = deriveGroup(group, result.newState);
|
|
45
|
-
await newGroup.applyLedgerEntries(
|
|
100
|
+
await newGroup.applyLedgerEntries(result.enacted);
|
|
46
101
|
return {
|
|
47
102
|
commitMessage: encode(mlsMessageEncoder, result.commit),
|
|
48
103
|
newGroup,
|
|
@@ -56,20 +111,11 @@ function ownIdentity(group) {
|
|
|
56
111
|
const parsed = parseMLSCredentialIdentity(leaf.credential.identity);
|
|
57
112
|
return createSigningIdentityForDID(parsed.longForm ?? parsed.id, group.state.signaturePrivateKey);
|
|
58
113
|
}
|
|
59
|
-
async function
|
|
114
|
+
export async function preservationClock(group, candidateTree, candidateRegistry) {
|
|
60
115
|
const controller = group.anchor.controller;
|
|
61
116
|
if (controller == null) return [];
|
|
62
117
|
const before = treeTime(group, controller);
|
|
63
|
-
|
|
64
|
-
for (const node of tree){
|
|
65
|
-
if (node?.nodeType !== nodeTypes.leaf || !('identity' in node.leaf.credential)) continue;
|
|
66
|
-
const binding = parseMLSCredentialIdentity(node.leaf.credential.identity).controller;
|
|
67
|
-
if (binding == null || normalizeDID(binding.id) !== normalizeDID(controller)) continue;
|
|
68
|
-
const payload = readCapability(binding.capability).payload;
|
|
69
|
-
const parent = Array.isArray(payload.cap) ? payload.cap[0] : payload.cap;
|
|
70
|
-
after = Math.max(after, parent == null ? payload.iat : readCapability(parent).payload.iat);
|
|
71
|
-
}
|
|
72
|
-
if (after >= before) return [];
|
|
118
|
+
if (attestedTreeTime(candidateTree, candidateRegistry, controller) >= before) return [];
|
|
73
119
|
return [
|
|
74
120
|
await signLedgerEntry(ownIdentity(group), {
|
|
75
121
|
type: DEVICE_ENTRY_TYPE,
|
|
@@ -82,6 +128,36 @@ async function clockEntries(group, tree) {
|
|
|
82
128
|
})
|
|
83
129
|
];
|
|
84
130
|
}
|
|
131
|
+
export async function attestTreeTime(group, time = Date.now() / 1000) {
|
|
132
|
+
return mutexFor(group).run(async ()=>{
|
|
133
|
+
const controller = group.anchor.controller;
|
|
134
|
+
if (controller == null) return null;
|
|
135
|
+
const projection = group.registry.controllers.get(normalizeDID(controller));
|
|
136
|
+
if (projection?.recordedLog == null) return null;
|
|
137
|
+
const folded = foldControllerLog(controller, projection.recordedLog);
|
|
138
|
+
if (!folded.ok || folded.head.saturated) return null;
|
|
139
|
+
const members = effectiveAttestingMembers(folded.head, projection, time).filter(([, member])=>normalizeDID(member.did) === normalizeDID(group.credential.id));
|
|
140
|
+
if (members.length === 0) return null;
|
|
141
|
+
if (members.every(([, member])=>time <= (projection.attestations.get(member.key) ?? 0))) return null;
|
|
142
|
+
const token = await signLedgerEntry(ownIdentity(group), {
|
|
143
|
+
type: DEVICE_ENTRY_TYPE,
|
|
144
|
+
groupID: group.groupID,
|
|
145
|
+
subject: controller,
|
|
146
|
+
value: {
|
|
147
|
+
op: 'clock',
|
|
148
|
+
time,
|
|
149
|
+
attest: true
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
return buildResult({
|
|
153
|
+
group,
|
|
154
|
+
proposals: [],
|
|
155
|
+
tokens: [
|
|
156
|
+
token
|
|
157
|
+
]
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
}
|
|
85
161
|
async function buildResult(params) {
|
|
86
162
|
const { group, proposals, tokens, commitState } = params;
|
|
87
163
|
const result = await commitWithEntries({
|
|
@@ -95,14 +171,14 @@ async function buildResult(params) {
|
|
|
95
171
|
const historical = result.newState.historicalReceiverData.get(group.epoch);
|
|
96
172
|
if (historical != null && commitState != null) historical.ratchetTree = group.state.ratchetTree;
|
|
97
173
|
const newGroup = deriveGroup(group, result.newState);
|
|
98
|
-
await newGroup.applyLedgerEntries(
|
|
174
|
+
await newGroup.applyLedgerEntries(result.enacted);
|
|
99
175
|
return {
|
|
100
176
|
commitMessage: encode(mlsMessageEncoder, result.commit),
|
|
101
177
|
newGroup,
|
|
102
178
|
epoch: newGroup.epoch
|
|
103
179
|
};
|
|
104
180
|
}
|
|
105
|
-
export async function renewLeaf(group, binding) {
|
|
181
|
+
export async function renewLeaf(group, binding, options) {
|
|
106
182
|
return mutexFor(group).run(async ()=>{
|
|
107
183
|
assertBindingAuthorTime(binding);
|
|
108
184
|
const index = group.state.privatePath.leafIndex * 2;
|
|
@@ -118,14 +194,53 @@ export async function renewLeaf(group, binding) {
|
|
|
118
194
|
controller: binding
|
|
119
195
|
}));
|
|
120
196
|
tree[index] = own;
|
|
121
|
-
|
|
197
|
+
const tokens = [];
|
|
198
|
+
let candidate = group.registry;
|
|
199
|
+
if (options?.controllerLog != null) {
|
|
200
|
+
const delta = controllerLogDelta(group, options.controllerLog);
|
|
201
|
+
if (delta != null) {
|
|
202
|
+
const token = await signLedgerEntry(ownIdentity(group), {
|
|
203
|
+
type: DEVICE_ENTRY_TYPE,
|
|
204
|
+
groupID: group.groupID,
|
|
205
|
+
subject: binding.id,
|
|
206
|
+
value: {
|
|
207
|
+
op: 'log',
|
|
208
|
+
proof: delta
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
const verified = await verifyLedgerEntry(token);
|
|
212
|
+
if (verified == null) throw new Error('Invalid authored log entry');
|
|
213
|
+
const folded = foldEnvelope({
|
|
214
|
+
baseRoster: group.roster,
|
|
215
|
+
baseRegistry: group.registry,
|
|
216
|
+
entries: [
|
|
217
|
+
{
|
|
218
|
+
verified,
|
|
219
|
+
entryID: ledgerEntryDigest(token)
|
|
220
|
+
}
|
|
221
|
+
],
|
|
222
|
+
groupID: group.groupID,
|
|
223
|
+
context: group.anchor.controller == null ? undefined : {
|
|
224
|
+
controllerID: group.anchor.controller,
|
|
225
|
+
memberController: (did)=>group.bindingOfDID(did)?.controller
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
if (!folded.ok) throw folded.error ?? new Error(folded.reason);
|
|
229
|
+
candidate = folded.registry;
|
|
230
|
+
tokens.push(token);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
await validateEntry(group, own.leaf, original.leaf, {
|
|
234
|
+
registry: candidate,
|
|
235
|
+
stage: 'full',
|
|
236
|
+
mode: 'admit'
|
|
237
|
+
});
|
|
122
238
|
if (group.anchor.controller != null) {
|
|
123
239
|
const entries = group.ledger.map(({ verified })=>verified);
|
|
124
240
|
const before = historySize(group.state.ratchetTree, entries);
|
|
125
241
|
const after = historySize(tree, entries);
|
|
126
242
|
if (after > HISTORY_HORIZON && after > before) throw new LeafBindingError('history-horizon');
|
|
127
243
|
}
|
|
128
|
-
const tokens = await clockEntries(group, tree);
|
|
129
244
|
return buildResult({
|
|
130
245
|
group,
|
|
131
246
|
proposals: [],
|
|
@@ -141,12 +256,10 @@ export async function removeLapsedLeaves(group) {
|
|
|
141
256
|
return mutexFor(group).run(async ()=>{
|
|
142
257
|
const removed = [];
|
|
143
258
|
const proposals = [];
|
|
144
|
-
const tree = group.state.ratchetTree.slice();
|
|
145
259
|
for (const member of group.listMembers()){
|
|
146
|
-
const leaf = leafAt(
|
|
260
|
+
const leaf = leafAt(group.state.ratchetTree, member.leafIndex);
|
|
147
261
|
if (leaf == null || !isLapsed(group, leaf)) continue;
|
|
148
262
|
removed.push(member.id);
|
|
149
|
-
tree[member.leafIndex * 2] = undefined;
|
|
150
263
|
proposals.push({
|
|
151
264
|
proposalType: defaultProposalTypes.remove,
|
|
152
265
|
remove: {
|
|
@@ -157,7 +270,7 @@ export async function removeLapsedLeaves(group) {
|
|
|
157
270
|
if (removed.length === 0) return {
|
|
158
271
|
removed
|
|
159
272
|
};
|
|
160
|
-
const tokens =
|
|
273
|
+
const tokens = [];
|
|
161
274
|
return {
|
|
162
275
|
removed,
|
|
163
276
|
result: await buildResult({
|
|
@@ -168,78 +281,48 @@ export async function removeLapsedLeaves(group) {
|
|
|
168
281
|
};
|
|
169
282
|
});
|
|
170
283
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
let did;
|
|
179
|
-
try {
|
|
180
|
-
did = didFromInception(first.event);
|
|
181
|
-
} catch (cause) {
|
|
182
|
-
const rejection = new RevokeProofError('not-authority-signed');
|
|
183
|
-
rejection.cause = cause;
|
|
184
|
-
throw rejection;
|
|
185
|
-
}
|
|
186
|
-
if (normalizeDID(did) !== normalizeDID(controller)) throw new RevokeProofError('wrong-controller');
|
|
187
|
-
}
|
|
188
|
-
if (log.some(({ event })=>event.t !== 'icp' && event.i !== controller)) throw new RevokeProofError('wrong-controller');
|
|
189
|
-
const folded = foldChain(controller, log);
|
|
190
|
-
if (!folded.ok) {
|
|
191
|
-
const detached = isDetachedFoldReason(folded.reason);
|
|
192
|
-
throw new RevokeProofError(detached ? 'detached' : 'not-authority-signed');
|
|
193
|
-
}
|
|
194
|
-
// Drop events that leave the folded state unchanged.
|
|
195
|
-
const advancing = log.filter((_, index)=>index === 0 || folded.states[index]?.digest !== folded.states[index - 1]?.digest);
|
|
196
|
-
const canonical = foldChain(controller, advancing);
|
|
197
|
-
if (!canonical.ok) throw new RevokeProofError('not-authority-signed');
|
|
198
|
-
let resetIndex = -1;
|
|
199
|
-
for(let index = 1; index < advancing.length; index++){
|
|
200
|
-
if (advancing[index]?.event.g !== advancing[index - 1]?.event.g) resetIndex = index;
|
|
201
|
-
}
|
|
202
|
-
const projection = group.registry.controllers.get(normalizeDID(controller));
|
|
203
|
-
const recorded = projection?.recordedLog ?? [];
|
|
204
|
-
const floor = projection?.genFloor ?? 0;
|
|
205
|
-
if (reset) {
|
|
206
|
-
if (resetIndex < 0) throw new RevokeProofError('no-rev');
|
|
207
|
-
return [
|
|
208
|
-
advancing[0],
|
|
209
|
-
...advancing.slice(resetIndex)
|
|
210
|
-
];
|
|
211
|
-
}
|
|
212
|
-
const recordedFold = foldChain(controller, recorded);
|
|
213
|
-
const recordedHead = recordedFold.ok ? recordedFold.states.at(-1) : undefined;
|
|
214
|
-
const generation = canonical.states.at(-1)?.gen ?? 0;
|
|
215
|
-
if (recorded.length > 0 && generation > (recordedHead?.gen ?? floor)) throw new RevokeProofError('needs-reset');
|
|
216
|
-
const start = recorded.length === 0 ? Math.max(1, resetIndex) : 1;
|
|
217
|
-
let end = advancing.findIndex((_, index)=>index >= start && revokesSubject(advancing, canonical.states, index, subject) && (canonical.states[index]?.gen ?? 0) >= floor);
|
|
218
|
-
if (end < 0) {
|
|
219
|
-
if (generation < floor) throw new RevokeProofError('generation-floor');
|
|
220
|
-
throw new RevokeProofError('no-rev');
|
|
221
|
-
}
|
|
222
|
-
if (recorded.length === 0) {
|
|
223
|
-
return [
|
|
224
|
-
advancing[0],
|
|
225
|
-
...advancing.slice(start, end + 1)
|
|
226
|
-
];
|
|
227
|
-
}
|
|
228
|
-
const headIndex = canonical.states.findIndex(({ digest })=>digest === recordedHead?.digest);
|
|
229
|
-
if (headIndex < 0) throw new RevokeProofError('detached');
|
|
230
|
-
end = Math.max(headIndex, end);
|
|
231
|
-
return advancing.slice(headIndex + 1, end + 1);
|
|
284
|
+
function alreadyApplied(group, claims) {
|
|
285
|
+
return claims.length > 0 && claims.every((claim)=>{
|
|
286
|
+
if (claim.kind === 'remove') return group.registry.controllers.get(normalizeDID(group.anchor.controller))?.removedMembers.has(claim.member.add) === true;
|
|
287
|
+
const did = normalizeDID(claim.did);
|
|
288
|
+
const record = group.registry.devices.get(did);
|
|
289
|
+
return record?.status === 'revoked' && !isResetOnly(record) && group.findMemberLeafIndex(did) == null;
|
|
290
|
+
});
|
|
232
291
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
if (
|
|
240
|
-
return
|
|
241
|
-
|
|
242
|
-
|
|
292
|
+
export function removalEvidence(controller, log, rmv) {
|
|
293
|
+
const signed = log.events.find(({ event })=>digestOf(event) === rmv);
|
|
294
|
+
if (signed?.event.t !== 'rmv') return undefined;
|
|
295
|
+
const closure = closureOf(controller, log, [
|
|
296
|
+
rmv
|
|
297
|
+
]);
|
|
298
|
+
if (closure == null) return undefined;
|
|
299
|
+
return {
|
|
300
|
+
controller: normalizeDID(controller),
|
|
301
|
+
epoch: signed.event.e,
|
|
302
|
+
closure
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
export function survivorTargets(group, add) {
|
|
306
|
+
const controller = group.anchor.controller;
|
|
307
|
+
if (controller == null) return [];
|
|
308
|
+
return group.listMembers().flatMap(({ id })=>{
|
|
309
|
+
const binding = group.bindingOfDID(id);
|
|
310
|
+
if (binding?.controller !== normalizeDID(controller) || binding.prefix == null) return [];
|
|
311
|
+
const authority = effectiveAuthorityOf({
|
|
312
|
+
did: id,
|
|
313
|
+
binding: {
|
|
314
|
+
...binding,
|
|
315
|
+
id: binding.controller,
|
|
316
|
+
prefix: binding.prefix
|
|
317
|
+
},
|
|
318
|
+
leafKey: binding.leafKey
|
|
319
|
+
}, group.registry, controller);
|
|
320
|
+
return 'root' in authority && authority.root.kind === 'member' && authority.root.add === add ? [
|
|
321
|
+
{
|
|
322
|
+
did: id,
|
|
323
|
+
leafKey: binding.leafKey.slice()
|
|
324
|
+
}
|
|
325
|
+
] : [];
|
|
243
326
|
});
|
|
244
327
|
}
|
|
245
328
|
export async function revokeWithProof(group, params) {
|
|
@@ -248,80 +331,95 @@ export async function revokeWithProof(group, params) {
|
|
|
248
331
|
const controller = group.anchor.controller;
|
|
249
332
|
if (controller == null) throw new RevokeProofError('wrong-controller');
|
|
250
333
|
const reset = 'reset' in params;
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
334
|
+
const requested = reset ? [] : (params.deny ?? []).map(normalizeDID);
|
|
335
|
+
const removals = reset ? [] : (params.remove ?? []).map(({ did, add })=>({
|
|
336
|
+
did: normalizeDID(did),
|
|
337
|
+
add
|
|
338
|
+
}));
|
|
339
|
+
if (!reset && requested.length === 0 && removals.length === 0) throw new RevokeProofError('no-rev');
|
|
340
|
+
if (params.log.chain.length === 0) throw new RevokeProofError('no-rev');
|
|
341
|
+
const projection = group.registry.controllers.get(normalizeDID(controller));
|
|
342
|
+
if (params.log.chain.every(({ event })=>event.g < (projection?.genFloor ?? 0))) throw new RevokeProofError('generation-floor');
|
|
343
|
+
let proof = controllerLogDelta(group, params.log);
|
|
344
|
+
if (!reset && params.evidence != null && proof != null) {
|
|
345
|
+
const evidence = params.evidence.filter((held)=>normalizeDID(held.controller) === normalizeDID(controller) && held.closure.events.some(({ event })=>event.t === 'rmv' && event.e === held.epoch && removals.some(({ add })=>event.add === add)));
|
|
346
|
+
const supplied = mergeLifecycleProofLogs(controller, {
|
|
347
|
+
chain: params.log.chain,
|
|
348
|
+
events: proof.events
|
|
349
|
+
}, {
|
|
350
|
+
chain: params.log.chain,
|
|
351
|
+
events: evidence.flatMap(({ closure })=>closure.events)
|
|
352
|
+
});
|
|
353
|
+
proof = {
|
|
354
|
+
...proof,
|
|
355
|
+
events: supplied.events
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
const ingested = proof == null ? null : ingestControllerLog(controller, projection?.recordedLog, proof, projection);
|
|
359
|
+
if (!reset && ingested != null && ingested.after.head.gen !== ingested.before?.head.gen) throw new RevokeProofError('needs-reset');
|
|
360
|
+
if (!reset) {
|
|
361
|
+
const requestedClaims = [
|
|
362
|
+
...requested.length === 0 || projection?.recordedLog == null ? [] : claimsFor(controller, undefined, projection.recordedLog, projection.genFloor).filter((claim)=>claim.kind === 'deny' && requested.includes(normalizeDID(claim.did))),
|
|
363
|
+
...removals.flatMap((member)=>{
|
|
364
|
+
const removed = projection?.removedMembers.get(member.add);
|
|
365
|
+
return removed?.did === member.did ? [
|
|
366
|
+
{
|
|
367
|
+
kind: 'remove',
|
|
368
|
+
member,
|
|
369
|
+
ref: removed.proofRef
|
|
370
|
+
}
|
|
371
|
+
] : [];
|
|
372
|
+
})
|
|
373
|
+
];
|
|
374
|
+
if (requestedClaims.length === new Set(requested).size + new Set(removals.map(({ add })=>add)).size && alreadyApplied(group, requestedClaims)) return {
|
|
375
|
+
status: 'already-revoked'
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
if (proof == null || ingested == null) throw new RevokeProofError('no-rev');
|
|
379
|
+
const effects = claimsFor(controller, projection?.recordedLog, lifecycleProofLog(controller, ingested, proof), projection?.genFloor ?? 0);
|
|
380
|
+
if (!reset && (requested.some((did)=>!effects.some((claim)=>claim.kind === 'deny' && normalizeDID(claim.did) === did)) || removals.some((member)=>!effects.some((claim)=>claim.kind === 'remove' && claim.member.add === member.add && normalizeDID(claim.member.did) === member.did)))) throw new RevokeProofError('no-rev');
|
|
255
381
|
const value = {
|
|
256
382
|
op: reset ? 'reset' : 'revoke',
|
|
257
|
-
proof
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
383
|
+
proof,
|
|
384
|
+
effects,
|
|
385
|
+
revoked: [],
|
|
386
|
+
removed: [],
|
|
387
|
+
...!reset && params.survivors != null ? {
|
|
388
|
+
survivors: [
|
|
389
|
+
...params.survivors
|
|
390
|
+
]
|
|
391
|
+
} : {}
|
|
264
392
|
};
|
|
265
393
|
const verified = {
|
|
266
394
|
issuer: group.credential.id,
|
|
267
395
|
entry: {
|
|
268
396
|
type: DEVICE_ENTRY_TYPE,
|
|
269
397
|
groupID: group.groupID,
|
|
270
|
-
subject,
|
|
398
|
+
subject: controller,
|
|
271
399
|
value
|
|
272
400
|
}
|
|
273
401
|
};
|
|
274
|
-
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
];
|
|
280
|
-
const leaves = group.listMembers().map((member)=>{
|
|
281
|
-
const binding = group.bindingOfDID(member.id);
|
|
282
|
-
const payload = binding?.capability == null ? undefined : readCapability(binding.capability).payload;
|
|
283
|
-
const folded = binding?.prefix == null ? undefined : foldChain(controller, binding.prefix);
|
|
284
|
-
return {
|
|
285
|
-
...member,
|
|
286
|
-
issuer: payload?.cap == null ? undefined : normalizeDID(payload.iss),
|
|
287
|
-
generation: folded?.ok ? folded.states.at(-1)?.gen : undefined,
|
|
288
|
-
exempt: isExemptMember(group, member.leafIndex)
|
|
289
|
-
};
|
|
290
|
-
});
|
|
291
|
-
if (reset) for (const leaf of leaves){
|
|
292
|
-
if (leaf.generation != null && leaf.generation < authenticated.genFloor) revoked.push({
|
|
293
|
-
did: normalizeDID(leaf.id)
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
const direct = new Set(revoked.map(({ did })=>did));
|
|
297
|
-
for (const leaf of leaves){
|
|
298
|
-
if (leaf.issuer != null && direct.has(leaf.issuer) && !direct.has(normalizeDID(leaf.id)) && !leaf.exempt) revoked.push({
|
|
299
|
-
did: normalizeDID(leaf.id),
|
|
300
|
-
cascadedFrom: leaf.issuer
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
value.revoked = revoked;
|
|
304
|
-
if (revoked.some(({ did })=>did === normalizeDID(group.credential.id))) return {
|
|
402
|
+
authenticateLifecycleProof(controller, verified, group.registry);
|
|
403
|
+
const derived = await lifecycleProofEffects(group, verified);
|
|
404
|
+
value.revoked = derived.revoked;
|
|
405
|
+
value.removed = derived.removed;
|
|
406
|
+
if (derived.removeLeafIndices.includes(group.state.privatePath.leafIndex)) return {
|
|
305
407
|
status: 'self-affected',
|
|
306
408
|
subject: group.credential.id
|
|
307
409
|
};
|
|
308
|
-
|
|
309
|
-
const
|
|
310
|
-
const proposals = effects.removeLeafIndices.map((removed)=>{
|
|
311
|
-
tree[removed * 2] = undefined;
|
|
312
|
-
return {
|
|
410
|
+
await verifyLifecycleProof(group, verified);
|
|
411
|
+
const proposals = derived.removeLeafIndices.map((removed)=>({
|
|
313
412
|
proposalType: defaultProposalTypes.remove,
|
|
314
413
|
remove: {
|
|
315
414
|
removed
|
|
316
415
|
}
|
|
317
|
-
};
|
|
318
|
-
});
|
|
416
|
+
}));
|
|
319
417
|
const tokens = [
|
|
320
|
-
await signLedgerEntry(ownIdentity(group), verified.entry)
|
|
321
|
-
...await clockEntries(group, tree)
|
|
418
|
+
await signLedgerEntry(ownIdentity(group), verified.entry)
|
|
322
419
|
];
|
|
323
420
|
return {
|
|
324
421
|
status: 'built',
|
|
422
|
+
effects,
|
|
325
423
|
result: await buildResult({
|
|
326
424
|
group,
|
|
327
425
|
proposals,
|
package/lib/group-membership.js
CHANGED
|
@@ -30,7 +30,7 @@ import { deriveGroup, mutexFor } from './group-handle.js';
|
|
|
30
30
|
enacted
|
|
31
31
|
});
|
|
32
32
|
const newGroup = deriveGroup(group, result.newState);
|
|
33
|
-
await newGroup.applyLedgerEntries(enacted);
|
|
33
|
+
await newGroup.applyLedgerEntries(result.enacted);
|
|
34
34
|
return {
|
|
35
35
|
commitMessage: encode(mlsMessageEncoder, result.commit),
|
|
36
36
|
newGroup,
|
package/lib/group-welcome.d.ts
CHANGED
|
@@ -70,4 +70,8 @@ export type JoinGroupExternalResult = {
|
|
|
70
70
|
sourceTree: ClientState['ratchetTree'];
|
|
71
71
|
sourceRegistry: DeviceRegistry;
|
|
72
72
|
};
|
|
73
|
+
/**
|
|
74
|
+
* Core callers holding known state must call assertRetainsKnownEffects in beforeBinding.
|
|
75
|
+
* mls-rpc installs this check before binding and rechecks at adoption.
|
|
76
|
+
*/
|
|
73
77
|
export declare function joinGroupExternal(params: JoinGroupExternalParams): Promise<JoinGroupExternalResult>;
|
package/lib/group-welcome.js
CHANGED
|
@@ -32,7 +32,12 @@ export async function processWelcome(params) {
|
|
|
32
32
|
...context,
|
|
33
33
|
authService: {
|
|
34
34
|
async validateCredential (credential, signaturePublicKey) {
|
|
35
|
-
await verifyLeafCredential(credential, signaturePublicKey
|
|
35
|
+
await verifyLeafCredential(credential, signaturePublicKey, {
|
|
36
|
+
judging: ()=>({
|
|
37
|
+
stage: 'structural',
|
|
38
|
+
mode: 'rejudge'
|
|
39
|
+
})
|
|
40
|
+
});
|
|
36
41
|
return true;
|
|
37
42
|
}
|
|
38
43
|
}
|
|
@@ -115,7 +120,10 @@ export async function processWelcome(params) {
|
|
|
115
120
|
}
|
|
116
121
|
return result;
|
|
117
122
|
}
|
|
118
|
-
|
|
123
|
+
/**
|
|
124
|
+
* Core callers holding known state must call assertRetainsKnownEffects in beforeBinding.
|
|
125
|
+
* mls-rpc installs this check before binding and rechecks at adoption.
|
|
126
|
+
*/ export async function joinGroupExternal(params) {
|
|
119
127
|
const { identity, groupInfo: groupInfoBytes, credential, resync, options, authenticatedData, controller, ledgerEntries } = params;
|
|
120
128
|
// Resync replaces the caller's own prior leaf, so the rejoining identity must match
|
|
121
129
|
// the presented credential. A friendly precheck, not the security boundary: on a
|
|
@@ -155,6 +163,10 @@ export async function joinGroupExternal(params) {
|
|
|
155
163
|
if (anchor.controller != null && parsed.controller == null) throw new LeafBindingError('floating-refused');
|
|
156
164
|
if (anchor.controller != null && parsed.controller != null && normalizeDID(parsed.controller.id) !== normalizeDID(anchor.controller)) throw new LeafBindingError('controller-mismatch');
|
|
157
165
|
await verifyLeafCredential(leafCredential, signaturePublicKey, {
|
|
166
|
+
judging: ()=>({
|
|
167
|
+
stage: 'structural',
|
|
168
|
+
mode: 'rejudge'
|
|
169
|
+
}),
|
|
158
170
|
leafLifetime: ()=>anchor.controller == null ? undefined : anchor.leafLifetime ?? 86_400,
|
|
159
171
|
trustedGrantLifetime: ()=>anchor.controller == null ? undefined : anchor.trustedGrantLifetime ?? 2_592_000
|
|
160
172
|
});
|
package/lib/history.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
+
import { type ControllerLog } from '@kokuin/controller';
|
|
1
2
|
import { type ClientState } from 'ts-mls';
|
|
2
3
|
import type { VerifiedLedgerEntry } from './ledger.js';
|
|
3
4
|
export declare const HISTORY_HORIZON = 393216;
|
|
5
|
+
export declare const MAX_EPOCH_EVENTS_FOR_LEAVES = 32;
|
|
6
|
+
export declare const SETTLE_PROMPT_EPOCH_EVENTS = 24;
|
|
7
|
+
export declare function epochEventCount(log: ControllerLog): number;
|
|
8
|
+
export declare function settleAdvisory(log: ControllerLog): 'ok' | 'prompt' | 'blocked';
|
|
4
9
|
export declare function controllerLogSize(events: ReadonlyArray<unknown>): number;
|
|
10
|
+
export declare function proofSize(value: unknown): number;
|
|
5
11
|
/** Count validated controller history. Callers apply the horizon only to lifecycle groups. */
|
|
6
12
|
export declare function historySize(tree: ClientState['ratchetTree'], entries: ReadonlyArray<VerifiedLedgerEntry>): number;
|
package/lib/history.js
CHANGED
|
@@ -1,11 +1,30 @@
|
|
|
1
|
+
import { didFromInception, isControllerLog } from '@kokuin/controller';
|
|
1
2
|
import { defaultCredentialTypes, isDefaultCredential, nodeTypes } from 'ts-mls';
|
|
3
|
+
import { foldControllerLog } from './controller-log.js';
|
|
2
4
|
import { parseMLSCredentialIdentity } from './credential.js';
|
|
3
5
|
import { DEVICE_ENTRY_TYPE } from './registry.js';
|
|
4
6
|
export const HISTORY_HORIZON = 393_216;
|
|
7
|
+
export const MAX_EPOCH_EVENTS_FOR_LEAVES = 32;
|
|
8
|
+
export const SETTLE_PROMPT_EPOCH_EVENTS = 24;
|
|
9
|
+
export function epochEventCount(log) {
|
|
10
|
+
const inception = log.chain[0]?.event;
|
|
11
|
+
if (inception?.t !== 'icp') throw new Error('Missing controller inception');
|
|
12
|
+
const folded = foldControllerLog(didFromInception(inception), log);
|
|
13
|
+
if (!folded.ok) throw new Error(folded.reason);
|
|
14
|
+
return folded.applied.length;
|
|
15
|
+
}
|
|
16
|
+
export function settleAdvisory(log) {
|
|
17
|
+
const count = epochEventCount(log);
|
|
18
|
+
if (count > MAX_EPOCH_EVENTS_FOR_LEAVES) return 'blocked';
|
|
19
|
+
return count >= SETTLE_PROMPT_EPOCH_EVENTS ? 'prompt' : 'ok';
|
|
20
|
+
}
|
|
5
21
|
const utf8 = new TextEncoder();
|
|
6
22
|
export function controllerLogSize(events) {
|
|
7
23
|
return events.reduce((total, event)=>total + utf8.encode(JSON.stringify(event)).length, 0);
|
|
8
24
|
}
|
|
25
|
+
export function proofSize(value) {
|
|
26
|
+
return isControllerLog(value) ? controllerLogSize(value.chain) + controllerLogSize(value.events) : 0;
|
|
27
|
+
}
|
|
9
28
|
/** Count validated controller history. Callers apply the horizon only to lifecycle groups. */ export function historySize(tree, entries) {
|
|
10
29
|
let size = 0;
|
|
11
30
|
for (const node of tree){
|
|
@@ -20,7 +39,7 @@ export function controllerLogSize(events) {
|
|
|
20
39
|
continue;
|
|
21
40
|
}
|
|
22
41
|
const proof = entry.value.proof;
|
|
23
|
-
|
|
42
|
+
size += proofSize(proof);
|
|
24
43
|
}
|
|
25
44
|
return size;
|
|
26
45
|
}
|