@kumiai/mls 0.10.9 → 0.12.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/README.md +29 -1
- package/lib/authentication.d.ts +8 -4
- package/lib/authentication.js +210 -54
- 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 +10 -0
- package/lib/controller-log.js +71 -0
- 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 -15
- package/lib/embedded-resolver.js +53 -16
- 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 +102 -16
- package/lib/group-lifecycle.d.ts +31 -5
- package/lib/group-lifecycle.js +253 -154
- 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 +196 -132
- package/lib/lifecycle.d.ts +9 -6
- package/lib/lifecycle.js +196 -86
- package/lib/member-root.d.ts +88 -0
- package/lib/member-root.js +469 -0
- package/lib/recovery-verdict.d.ts +1 -1
- package/lib/recovery-verdict.js +32 -15
- package/lib/registry.d.ts +17 -6
- package/lib/registry.js +205 -21
- 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,17 +1,77 @@
|
|
|
1
|
-
import { didFromInception,
|
|
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 {
|
|
4
|
+
import { effectDelta, ingestControllerLog } from './controller-ingest.js';
|
|
5
|
+
import { closureOf, foldControllerLog, isDetachedFoldReason } from './controller-log.js';
|
|
5
6
|
import { parseMLSCredentialIdentity } from './credential.js';
|
|
7
|
+
import { foldEnvelope } from './envelope-fold.js';
|
|
6
8
|
import { LeafBindingError, RevokeProofError } from './errors.js';
|
|
7
9
|
import { commitWithEntries } from './group-commit.js';
|
|
8
10
|
import { assertBindingAuthorTime } from './group-credential.js';
|
|
9
11
|
import { deriveGroup, mutexFor } from './group-handle.js';
|
|
10
12
|
import { HISTORY_HORIZON, historySize } from './history.js';
|
|
11
|
-
import { signLedgerEntry } from './ledger.js';
|
|
12
|
-
import { isLapsed, leafAt, treeTime, validateEntry } from './lifecycle.js';
|
|
13
|
-
import { authenticateLifecycleProof,
|
|
14
|
-
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
|
+
}
|
|
15
75
|
/**
|
|
16
76
|
* Author a by-reference Remove for this handle's own leaf at its current epoch.
|
|
17
77
|
* Once a member processes a pending self-removal, application encrypt throws until
|
|
@@ -28,11 +88,7 @@ import { DEVICE_ENTRY_TYPE, isResetOnly } from './registry.js';
|
|
|
28
88
|
return mutexFor(group).run(async ()=>{
|
|
29
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));
|
|
30
90
|
if (Object.keys(pendingProposals).length === 0) return null;
|
|
31
|
-
const
|
|
32
|
-
for (const { proposal } of Object.values(pendingProposals)){
|
|
33
|
-
if (isDefaultProposal(proposal) && proposal.proposalType === defaultProposalTypes.remove) tree[proposal.remove.removed * 2] = undefined;
|
|
34
|
-
}
|
|
35
|
-
const tokens = await clockEntries(group, tree);
|
|
91
|
+
const tokens = [];
|
|
36
92
|
const result = await commitWithEntries({
|
|
37
93
|
group,
|
|
38
94
|
extraProposals: [],
|
|
@@ -41,7 +97,7 @@ import { DEVICE_ENTRY_TYPE, isResetOnly } from './registry.js';
|
|
|
41
97
|
requireAdmin: false
|
|
42
98
|
});
|
|
43
99
|
const newGroup = deriveGroup(group, result.newState);
|
|
44
|
-
await newGroup.applyLedgerEntries(
|
|
100
|
+
await newGroup.applyLedgerEntries(result.enacted);
|
|
45
101
|
return {
|
|
46
102
|
commitMessage: encode(mlsMessageEncoder, result.commit),
|
|
47
103
|
newGroup,
|
|
@@ -55,20 +111,11 @@ function ownIdentity(group) {
|
|
|
55
111
|
const parsed = parseMLSCredentialIdentity(leaf.credential.identity);
|
|
56
112
|
return createSigningIdentityForDID(parsed.longForm ?? parsed.id, group.state.signaturePrivateKey);
|
|
57
113
|
}
|
|
58
|
-
async function
|
|
114
|
+
export async function preservationClock(group, candidateTree, candidateRegistry) {
|
|
59
115
|
const controller = group.anchor.controller;
|
|
60
116
|
if (controller == null) return [];
|
|
61
117
|
const before = treeTime(group, controller);
|
|
62
|
-
|
|
63
|
-
for (const node of tree){
|
|
64
|
-
if (node?.nodeType !== nodeTypes.leaf || !('identity' in node.leaf.credential)) continue;
|
|
65
|
-
const binding = parseMLSCredentialIdentity(node.leaf.credential.identity).controller;
|
|
66
|
-
if (binding == null || normalizeDID(binding.id) !== normalizeDID(controller)) continue;
|
|
67
|
-
const payload = readCapability(binding.capability).payload;
|
|
68
|
-
const parent = Array.isArray(payload.cap) ? payload.cap[0] : payload.cap;
|
|
69
|
-
after = Math.max(after, parent == null ? payload.iat : readCapability(parent).payload.iat);
|
|
70
|
-
}
|
|
71
|
-
if (after >= before) return [];
|
|
118
|
+
if (attestedTreeTime(candidateTree, candidateRegistry, controller) >= before) return [];
|
|
72
119
|
return [
|
|
73
120
|
await signLedgerEntry(ownIdentity(group), {
|
|
74
121
|
type: DEVICE_ENTRY_TYPE,
|
|
@@ -81,6 +128,36 @@ async function clockEntries(group, tree) {
|
|
|
81
128
|
})
|
|
82
129
|
];
|
|
83
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
|
+
}
|
|
84
161
|
async function buildResult(params) {
|
|
85
162
|
const { group, proposals, tokens, commitState } = params;
|
|
86
163
|
const result = await commitWithEntries({
|
|
@@ -94,14 +171,14 @@ async function buildResult(params) {
|
|
|
94
171
|
const historical = result.newState.historicalReceiverData.get(group.epoch);
|
|
95
172
|
if (historical != null && commitState != null) historical.ratchetTree = group.state.ratchetTree;
|
|
96
173
|
const newGroup = deriveGroup(group, result.newState);
|
|
97
|
-
await newGroup.applyLedgerEntries(
|
|
174
|
+
await newGroup.applyLedgerEntries(result.enacted);
|
|
98
175
|
return {
|
|
99
176
|
commitMessage: encode(mlsMessageEncoder, result.commit),
|
|
100
177
|
newGroup,
|
|
101
178
|
epoch: newGroup.epoch
|
|
102
179
|
};
|
|
103
180
|
}
|
|
104
|
-
export async function renewLeaf(group, binding) {
|
|
181
|
+
export async function renewLeaf(group, binding, options) {
|
|
105
182
|
return mutexFor(group).run(async ()=>{
|
|
106
183
|
assertBindingAuthorTime(binding);
|
|
107
184
|
const index = group.state.privatePath.leafIndex * 2;
|
|
@@ -117,14 +194,53 @@ export async function renewLeaf(group, binding) {
|
|
|
117
194
|
controller: binding
|
|
118
195
|
}));
|
|
119
196
|
tree[index] = own;
|
|
120
|
-
|
|
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
|
+
});
|
|
121
238
|
if (group.anchor.controller != null) {
|
|
122
239
|
const entries = group.ledger.map(({ verified })=>verified);
|
|
123
240
|
const before = historySize(group.state.ratchetTree, entries);
|
|
124
241
|
const after = historySize(tree, entries);
|
|
125
242
|
if (after > HISTORY_HORIZON && after > before) throw new LeafBindingError('history-horizon');
|
|
126
243
|
}
|
|
127
|
-
const tokens = await clockEntries(group, tree);
|
|
128
244
|
return buildResult({
|
|
129
245
|
group,
|
|
130
246
|
proposals: [],
|
|
@@ -140,12 +256,10 @@ export async function removeLapsedLeaves(group) {
|
|
|
140
256
|
return mutexFor(group).run(async ()=>{
|
|
141
257
|
const removed = [];
|
|
142
258
|
const proposals = [];
|
|
143
|
-
const tree = group.state.ratchetTree.slice();
|
|
144
259
|
for (const member of group.listMembers()){
|
|
145
|
-
const leaf = leafAt(
|
|
260
|
+
const leaf = leafAt(group.state.ratchetTree, member.leafIndex);
|
|
146
261
|
if (leaf == null || !isLapsed(group, leaf)) continue;
|
|
147
262
|
removed.push(member.id);
|
|
148
|
-
tree[member.leafIndex * 2] = undefined;
|
|
149
263
|
proposals.push({
|
|
150
264
|
proposalType: defaultProposalTypes.remove,
|
|
151
265
|
remove: {
|
|
@@ -156,7 +270,7 @@ export async function removeLapsedLeaves(group) {
|
|
|
156
270
|
if (removed.length === 0) return {
|
|
157
271
|
removed
|
|
158
272
|
};
|
|
159
|
-
const tokens =
|
|
273
|
+
const tokens = [];
|
|
160
274
|
return {
|
|
161
275
|
removed,
|
|
162
276
|
result: await buildResult({
|
|
@@ -167,78 +281,48 @@ export async function removeLapsedLeaves(group) {
|
|
|
167
281
|
};
|
|
168
282
|
});
|
|
169
283
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
let did;
|
|
178
|
-
try {
|
|
179
|
-
did = didFromInception(first.event);
|
|
180
|
-
} catch (cause) {
|
|
181
|
-
const rejection = new RevokeProofError('not-authority-signed');
|
|
182
|
-
rejection.cause = cause;
|
|
183
|
-
throw rejection;
|
|
184
|
-
}
|
|
185
|
-
if (normalizeDID(did) !== normalizeDID(controller)) throw new RevokeProofError('wrong-controller');
|
|
186
|
-
}
|
|
187
|
-
if (log.some(({ event })=>event.t !== 'icp' && event.i !== controller)) throw new RevokeProofError('wrong-controller');
|
|
188
|
-
const folded = foldLog(controller, log);
|
|
189
|
-
if (!folded.ok) {
|
|
190
|
-
const detached = /sequence|digest|prior|inception must/.test(folded.reason);
|
|
191
|
-
throw new RevokeProofError(detached ? 'detached' : 'not-authority-signed');
|
|
192
|
-
}
|
|
193
|
-
// Drop events that leave the folded state unchanged.
|
|
194
|
-
const advancing = log.filter((_, index)=>index === 0 || folded.states[index]?.digest !== folded.states[index - 1]?.digest);
|
|
195
|
-
const canonical = foldLog(controller, advancing);
|
|
196
|
-
if (!canonical.ok) throw new RevokeProofError('not-authority-signed');
|
|
197
|
-
let resetIndex = -1;
|
|
198
|
-
for(let index = 1; index < advancing.length; index++){
|
|
199
|
-
if (advancing[index]?.event.g !== advancing[index - 1]?.event.g) resetIndex = index;
|
|
200
|
-
}
|
|
201
|
-
const projection = group.registry.controllers.get(normalizeDID(controller));
|
|
202
|
-
const recorded = projection?.recordedLog ?? [];
|
|
203
|
-
const floor = projection?.genFloor ?? 0;
|
|
204
|
-
if (reset) {
|
|
205
|
-
if (resetIndex < 0) throw new RevokeProofError('no-rev');
|
|
206
|
-
return [
|
|
207
|
-
advancing[0],
|
|
208
|
-
...advancing.slice(resetIndex)
|
|
209
|
-
];
|
|
210
|
-
}
|
|
211
|
-
const recordedFold = foldLog(controller, recorded);
|
|
212
|
-
const recordedHead = recordedFold.ok ? recordedFold.states.at(-1) : undefined;
|
|
213
|
-
const generation = canonical.states.at(-1)?.gen ?? 0;
|
|
214
|
-
if (recorded.length > 0 && generation > (recordedHead?.gen ?? floor)) throw new RevokeProofError('needs-reset');
|
|
215
|
-
const start = recorded.length === 0 ? Math.max(1, resetIndex) : 1;
|
|
216
|
-
let end = advancing.findIndex(({ event }, index)=>index >= start && event.t === 'rev' && 'x' in event && typeof event.x === 'string' && normalizeDID(event.x) === subject && (canonical.states[index]?.gen ?? 0) >= floor);
|
|
217
|
-
if (end < 0) {
|
|
218
|
-
if (generation < floor) throw new RevokeProofError('generation-floor');
|
|
219
|
-
throw new RevokeProofError('no-rev');
|
|
220
|
-
}
|
|
221
|
-
if (recorded.length === 0) {
|
|
222
|
-
return [
|
|
223
|
-
advancing[0],
|
|
224
|
-
...advancing.slice(start, end + 1)
|
|
225
|
-
];
|
|
226
|
-
}
|
|
227
|
-
const headIndex = canonical.states.findIndex(({ digest })=>digest === recordedHead?.digest);
|
|
228
|
-
if (headIndex < 0) throw new RevokeProofError('detached');
|
|
229
|
-
end = Math.max(headIndex, end);
|
|
230
|
-
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
|
+
});
|
|
231
291
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
if (
|
|
239
|
-
return
|
|
240
|
-
|
|
241
|
-
|
|
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
|
+
] : [];
|
|
242
326
|
});
|
|
243
327
|
}
|
|
244
328
|
export async function revokeWithProof(group, params) {
|
|
@@ -247,80 +331,95 @@ export async function revokeWithProof(group, params) {
|
|
|
247
331
|
const controller = group.anchor.controller;
|
|
248
332
|
if (controller == null) throw new RevokeProofError('wrong-controller');
|
|
249
333
|
const reset = 'reset' in params;
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
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');
|
|
254
381
|
const value = {
|
|
255
382
|
op: reset ? 'reset' : 'revoke',
|
|
256
|
-
proof
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
383
|
+
proof,
|
|
384
|
+
effects,
|
|
385
|
+
revoked: [],
|
|
386
|
+
removed: [],
|
|
387
|
+
...!reset && params.survivors != null ? {
|
|
388
|
+
survivors: [
|
|
389
|
+
...params.survivors
|
|
390
|
+
]
|
|
391
|
+
} : {}
|
|
263
392
|
};
|
|
264
393
|
const verified = {
|
|
265
394
|
issuer: group.credential.id,
|
|
266
395
|
entry: {
|
|
267
396
|
type: DEVICE_ENTRY_TYPE,
|
|
268
397
|
groupID: group.groupID,
|
|
269
|
-
subject,
|
|
398
|
+
subject: controller,
|
|
270
399
|
value
|
|
271
400
|
}
|
|
272
401
|
};
|
|
273
|
-
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
];
|
|
279
|
-
const leaves = group.listMembers().map((member)=>{
|
|
280
|
-
const binding = group.bindingOfDID(member.id);
|
|
281
|
-
const payload = binding?.capability == null ? undefined : readCapability(binding.capability).payload;
|
|
282
|
-
const folded = binding?.prefix == null ? undefined : foldLog(controller, binding.prefix);
|
|
283
|
-
return {
|
|
284
|
-
...member,
|
|
285
|
-
issuer: payload?.cap == null ? undefined : normalizeDID(payload.iss),
|
|
286
|
-
generation: folded?.ok ? folded.states.at(-1)?.gen : undefined,
|
|
287
|
-
exempt: isExemptMember(group, member.leafIndex)
|
|
288
|
-
};
|
|
289
|
-
});
|
|
290
|
-
if (reset) for (const leaf of leaves){
|
|
291
|
-
if (leaf.generation != null && leaf.generation < authenticated.genFloor) revoked.push({
|
|
292
|
-
did: normalizeDID(leaf.id)
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
const direct = new Set(revoked.map(({ did })=>did));
|
|
296
|
-
for (const leaf of leaves){
|
|
297
|
-
if (leaf.issuer != null && direct.has(leaf.issuer) && !direct.has(normalizeDID(leaf.id)) && !leaf.exempt) revoked.push({
|
|
298
|
-
did: normalizeDID(leaf.id),
|
|
299
|
-
cascadedFrom: leaf.issuer
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
value.revoked = revoked;
|
|
303
|
-
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 {
|
|
304
407
|
status: 'self-affected',
|
|
305
408
|
subject: group.credential.id
|
|
306
409
|
};
|
|
307
|
-
|
|
308
|
-
const
|
|
309
|
-
const proposals = effects.removeLeafIndices.map((removed)=>{
|
|
310
|
-
tree[removed * 2] = undefined;
|
|
311
|
-
return {
|
|
410
|
+
await verifyLifecycleProof(group, verified);
|
|
411
|
+
const proposals = derived.removeLeafIndices.map((removed)=>({
|
|
312
412
|
proposalType: defaultProposalTypes.remove,
|
|
313
413
|
remove: {
|
|
314
414
|
removed
|
|
315
415
|
}
|
|
316
|
-
};
|
|
317
|
-
});
|
|
416
|
+
}));
|
|
318
417
|
const tokens = [
|
|
319
|
-
await signLedgerEntry(ownIdentity(group), verified.entry)
|
|
320
|
-
...await clockEntries(group, tree)
|
|
418
|
+
await signLedgerEntry(ownIdentity(group), verified.entry)
|
|
321
419
|
];
|
|
322
420
|
return {
|
|
323
421
|
status: 'built',
|
|
422
|
+
effects,
|
|
324
423
|
result: await buildResult({
|
|
325
424
|
group,
|
|
326
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
|
}
|