@kumiai/mls-rpc 0.10.0 → 0.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/access.d.ts +4 -1
- package/lib/access.js +7 -5
- package/lib/apply-commit.d.ts +2 -1
- package/lib/apply-commit.js +8 -4
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/mls.d.ts +5 -1
- package/lib/mls.js +107 -24
- package/lib/recovery.d.ts +46 -0
- package/lib/recovery.js +185 -0
- package/lib/revoke.d.ts +19 -0
- package/lib/revoke.js +45 -0
- package/package.json +6 -6
package/lib/access.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { GroupHandle } from '@kumiai/mls';
|
|
2
|
-
import type { PendingAppFrame } from '@kumiai/rpc';
|
|
2
|
+
import type { PendingAppFrame, SendAdmission } from '@kumiai/rpc';
|
|
3
3
|
type PersistOpened = (stagedState: Uint8Array, record: PendingAppFrame) => Promise<void>;
|
|
4
4
|
export type HandleAccess = {
|
|
5
|
+
/** Published with admission after mutation/open and after replacement adoption resolves. */
|
|
5
6
|
epoch(): number;
|
|
7
|
+
/** Lock-free snapshot. During adoption it describes the epoch being left. */
|
|
8
|
+
admission(): SendAdmission;
|
|
6
9
|
read<TValue>(fn: (handle: GroupHandle) => TValue | Promise<TValue>): Promise<TValue>;
|
|
7
10
|
mutate<TValue>(fn: (handle: GroupHandle, persist: (handle: GroupHandle) => Promise<void>) => Promise<TValue>): Promise<TValue>;
|
|
8
11
|
replace(next: GroupHandle): Promise<void>;
|
package/lib/access.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export function simpleHandleAccess(params) {
|
|
2
|
-
let
|
|
2
|
+
let publishedAdmission = params.handle().sendAdmission();
|
|
3
3
|
let tail = Promise.resolve();
|
|
4
4
|
const serialise = async (fn)=>{
|
|
5
5
|
const previous = tail;
|
|
@@ -18,7 +18,8 @@ export function simpleHandleAccess(params) {
|
|
|
18
18
|
await params.persist?.(handle);
|
|
19
19
|
};
|
|
20
20
|
return {
|
|
21
|
-
epoch: ()=>
|
|
21
|
+
epoch: ()=>publishedAdmission.epoch,
|
|
22
|
+
admission: ()=>publishedAdmission,
|
|
22
23
|
read: (fn)=>serialise(async ()=>await fn(params.handle())),
|
|
23
24
|
mutate: (fn)=>serialise(async ()=>{
|
|
24
25
|
const handle = params.handle();
|
|
@@ -29,18 +30,19 @@ export function simpleHandleAccess(params) {
|
|
|
29
30
|
};
|
|
30
31
|
const result = await fn(handle, persist);
|
|
31
32
|
if (!saved) await save(handle);
|
|
32
|
-
|
|
33
|
+
publishedAdmission = handle.sendAdmission();
|
|
33
34
|
return result;
|
|
34
35
|
}),
|
|
35
36
|
replace: (next)=>serialise(async ()=>{
|
|
36
37
|
await save(next);
|
|
37
38
|
await params.adopt(next);
|
|
38
|
-
|
|
39
|
+
publishedAdmission = next.sendAdmission();
|
|
40
|
+
next.confirmAdopted();
|
|
39
41
|
}),
|
|
40
42
|
open: (fn, persistOpened)=>serialise(async ()=>{
|
|
41
43
|
const handle = params.handle();
|
|
42
44
|
const result = await fn(handle, persistOpened);
|
|
43
|
-
|
|
45
|
+
publishedAdmission = handle.sendAdmission();
|
|
44
46
|
return result;
|
|
45
47
|
})
|
|
46
48
|
};
|
package/lib/apply-commit.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type GroupHandle, type VerifiedLedgerEntry } from '@kumiai/mls';
|
|
2
|
-
import type { CommitContext, RosterEntry } from '@kumiai/rpc';
|
|
2
|
+
import type { CommitContext, RecoveryRefusalReason, RosterEntry } from '@kumiai/rpc';
|
|
3
3
|
import type { LedgerEntrySlot } from './mls.js';
|
|
4
4
|
export type ApplyCommitParams = CommitContext & {
|
|
5
5
|
handle: GroupHandle;
|
|
@@ -11,6 +11,7 @@ export type ApplyCommitParams = CommitContext & {
|
|
|
11
11
|
persist?: ((handle: GroupHandle) => Promise<void>) | undefined;
|
|
12
12
|
};
|
|
13
13
|
export type ApplyCommitResult = {
|
|
14
|
+
refusal?: RecoveryRefusalReason;
|
|
14
15
|
/** The handle took the commit's state. A host keeps it even when `advanced` is false. */
|
|
15
16
|
applied: boolean;
|
|
16
17
|
/** The epoch moved. A commit removing this member is applied without advancing it. */
|
package/lib/apply-commit.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { normalizeDID } from '@kokuin/token';
|
|
2
|
-
import { MissingLedgerEntriesError, readMessageEpoch } from '@kumiai/mls';
|
|
2
|
+
import { CommitRejectedError, MissingLedgerEntriesError, readMessageEpoch } from '@kumiai/mls';
|
|
3
3
|
/** Reserved by `@kumiai/mls` for its own control entries; never surfaced to a host. */ const CONTROL_TYPE_PREFIX = 'kumiai.';
|
|
4
4
|
function rosterOf(handle) {
|
|
5
5
|
return handle.listMembers().map((member)=>({
|
|
@@ -22,7 +22,8 @@ function rosterOf(handle) {
|
|
|
22
22
|
const before = handle.epoch;
|
|
23
23
|
const rosterBefore = rosterOf(handle);
|
|
24
24
|
const ledgerLengthBefore = handle.ledger.length;
|
|
25
|
-
const refused = (committerDID)=>({
|
|
25
|
+
const refused = (committerDID, refusal = 'invalid')=>({
|
|
26
|
+
refusal,
|
|
26
27
|
applied: false,
|
|
27
28
|
advanced: false,
|
|
28
29
|
epochBefore: Number(before),
|
|
@@ -46,6 +47,7 @@ function rosterOf(handle) {
|
|
|
46
47
|
let persisted = false;
|
|
47
48
|
let resolverFailed = false;
|
|
48
49
|
let threw = false;
|
|
50
|
+
let refusal = 'invalid';
|
|
49
51
|
const resolve = params.resolveLedgerEntries;
|
|
50
52
|
entrySlot.install(resolve && (async (ids)=>{
|
|
51
53
|
try {
|
|
@@ -73,6 +75,7 @@ function rosterOf(handle) {
|
|
|
73
75
|
} catch (error) {
|
|
74
76
|
if (error instanceof MissingLedgerEntriesError || persistFailed || resolverFailed) throw error;
|
|
75
77
|
threw = true;
|
|
78
|
+
if (error instanceof CommitRejectedError) refusal = error.reason;
|
|
76
79
|
} finally{
|
|
77
80
|
entrySlot.install(undefined);
|
|
78
81
|
}
|
|
@@ -81,9 +84,10 @@ function rosterOf(handle) {
|
|
|
81
84
|
// callback may throw after that state was stored.
|
|
82
85
|
const rosterAfter = rosterOf(handle);
|
|
83
86
|
const changed = rosterAfter.length !== rosterBefore.length || rosterAfter.some((entry, index)=>entry.did !== rosterBefore[index]?.did);
|
|
84
|
-
if (threw && !persisted && !changed) return refused(committerDID);
|
|
87
|
+
if (threw && !persisted && !changed) return refused(committerDID, refusal);
|
|
88
|
+
const { refusal: _refusal, ...unchanged } = refused(committerDID);
|
|
85
89
|
return {
|
|
86
|
-
...
|
|
90
|
+
...unchanged,
|
|
87
91
|
applied: true,
|
|
88
92
|
rosterAfter,
|
|
89
93
|
surfacedEntries: surfaced()
|
package/lib/index.d.ts
CHANGED
|
@@ -22,3 +22,4 @@ export { type HandleAccess, type SimpleHandleAccessParams, simpleHandleAccess }
|
|
|
22
22
|
export { type ApplyCommitParams, type ApplyCommitResult, applyCommit } from './apply-commit.js';
|
|
23
23
|
export { createGroupCrypto, deriveEntryKey, ENTRY_SEAL_LABEL, type GroupCryptoParams, openEntries, sealEntries, } from './crypto.js';
|
|
24
24
|
export { createGroupMLS, createLedgerEntrySlot, createRecoveryPending, deriveRecoverySecret, type GroupMLSParams, type LedgerEntrySlot, RECOVERY_LABEL, type RecoveryPending, } from './mls.js';
|
|
25
|
+
export { type PublishRevokeProofParams, type PublishRevokeResult, publishRevokeProof, type RevokeJournalOptions, } from './revoke.js';
|
package/lib/index.js
CHANGED
|
@@ -21,3 +21,4 @@
|
|
|
21
21
|
export { applyCommit } from './apply-commit.js';
|
|
22
22
|
export { createGroupCrypto, deriveEntryKey, ENTRY_SEAL_LABEL, openEntries, sealEntries } from './crypto.js';
|
|
23
23
|
export { createGroupMLS, createLedgerEntrySlot, createRecoveryPending, deriveRecoverySecret, RECOVERY_LABEL } from './mls.js';
|
|
24
|
+
export { publishRevokeProof } from './revoke.js';
|
package/lib/mls.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import type { OwnIdentity } from '@kokuin/token';
|
|
|
2
2
|
import { type GroupHandle } from '@kumiai/mls';
|
|
3
3
|
import type { GroupMLS } from '@kumiai/rpc';
|
|
4
4
|
import type { HandleAccess } from './access.js';
|
|
5
|
+
import { type RecoveryBinding } from './recovery.js';
|
|
6
|
+
export declare const groupHandleAccess: WeakMap<GroupMLS, HandleAccess>;
|
|
5
7
|
/**
|
|
6
8
|
* The label the non-rotating recovery secret is derived under. This secret is the root of
|
|
7
9
|
* BOTH the commit topic and the rendezvous topic — everything downstream of it depends on
|
|
@@ -45,10 +47,12 @@ export type GroupMLSParams = {
|
|
|
45
47
|
* here for every restore of that group: the commit and rendezvous topics follow the choice.
|
|
46
48
|
*/
|
|
47
49
|
recoverySecret?: (handle: GroupHandle) => Promise<Uint8Array>;
|
|
50
|
+
/** Fresh own-leaf binding, at most once per attempt. Return null without waiting on the user. */
|
|
51
|
+
recoveryBinding?: RecoveryBinding;
|
|
48
52
|
};
|
|
49
53
|
export type RecoveryPending = {
|
|
50
54
|
get(requestID: string): Uint8Array | null;
|
|
51
|
-
put(requestID: string, ephemeralPrivateKey: Uint8Array): void;
|
|
55
|
+
put(requestID: string, ephemeralPrivateKey: Uint8Array, deadlineMs?: number): void;
|
|
52
56
|
delete(requestID: string): void;
|
|
53
57
|
};
|
|
54
58
|
export declare function createRecoveryPending(options?: {
|
package/lib/mls.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { createRecoveryRequest, encodeGroupAnchor,
|
|
1
|
+
import { confirmationKey, createRecoveryRequest, encodeGroupAnchor, ledgerEntryDigest, MissingLedgerEntriesError, openRecoveryGroupInfo, openRecoveryVerdict, openSealedLedger, readCommitEntryIDs, readMessageEpoch, recoverySignerEligible, sealGroupInfo, sealLedger, sealRecoveryVerdict, verifyRecoveryRequest } from '@kumiai/mls';
|
|
2
2
|
import { applyCommit } from './apply-commit.js';
|
|
3
|
+
import { createRecoveryBindingState, createVerdictJudge, knownRecoveryRegistry } from './recovery.js';
|
|
4
|
+
export const groupHandleAccess = new WeakMap();
|
|
3
5
|
const utf8 = new TextEncoder();
|
|
4
6
|
/**
|
|
5
7
|
* The label the non-rotating recovery secret is derived under. This secret is the root of
|
|
@@ -42,9 +44,9 @@ export function createRecoveryPending(options) {
|
|
|
42
44
|
requests.delete(requestID);
|
|
43
45
|
};
|
|
44
46
|
const sweep = ()=>{
|
|
45
|
-
const
|
|
47
|
+
const now = Date.now();
|
|
46
48
|
for (const [id, request] of requests){
|
|
47
|
-
if (request.
|
|
49
|
+
if (request.expiresAt <= now) remove(id, request);
|
|
48
50
|
}
|
|
49
51
|
};
|
|
50
52
|
return {
|
|
@@ -52,19 +54,20 @@ export function createRecoveryPending(options) {
|
|
|
52
54
|
sweep();
|
|
53
55
|
return requests.get(requestID)?.ephemeralPrivateKey ?? null;
|
|
54
56
|
},
|
|
55
|
-
put: (requestID, ephemeralPrivateKey)=>{
|
|
57
|
+
put: (requestID, ephemeralPrivateKey, deadlineMs = 0)=>{
|
|
58
|
+
const retentionMs = Math.max(ttlMS, deadlineMs);
|
|
56
59
|
sweep();
|
|
57
60
|
const previous = requests.get(requestID);
|
|
58
61
|
if (previous != null) remove(requestID, previous);
|
|
59
62
|
const timer = setTimeout(()=>{
|
|
60
63
|
const held = requests.get(requestID);
|
|
61
64
|
if (held?.ephemeralPrivateKey === ephemeralPrivateKey) remove(requestID, held);
|
|
62
|
-
},
|
|
65
|
+
}, retentionMs);
|
|
63
66
|
const nodeTimer = timer;
|
|
64
67
|
nodeTimer.unref?.();
|
|
65
68
|
requests.set(requestID, {
|
|
66
69
|
ephemeralPrivateKey,
|
|
67
|
-
|
|
70
|
+
expiresAt: Date.now() + retentionMs,
|
|
68
71
|
timer
|
|
69
72
|
});
|
|
70
73
|
},
|
|
@@ -106,10 +109,55 @@ export async function deriveRecoverySecret(handle) {
|
|
|
106
109
|
*/ export function createGroupMLS(params) {
|
|
107
110
|
const { access, identity, entrySlot } = params;
|
|
108
111
|
const pending = createRecoveryPending();
|
|
109
|
-
|
|
112
|
+
const recovery = createRecoveryBindingState(identity, params.recoveryBinding);
|
|
113
|
+
const port = {
|
|
114
|
+
async verifyRecoveryRequest (request) {
|
|
115
|
+
try {
|
|
116
|
+
const verified = await verifyRecoveryRequest(new TextDecoder().decode(request));
|
|
117
|
+
return await access.read((group)=>verified.groupID === group.groupID ? {
|
|
118
|
+
groupID: verified.groupID,
|
|
119
|
+
requestID: verified.requestID,
|
|
120
|
+
requesterDID: verified.requesterDID
|
|
121
|
+
} : null);
|
|
122
|
+
} catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
async confirmationKey (position, commitDigest) {
|
|
127
|
+
return await access.read(async (group)=>({
|
|
128
|
+
epoch: Number(group.epoch),
|
|
129
|
+
key: await confirmationKey(group, position, commitDigest)
|
|
130
|
+
}));
|
|
131
|
+
},
|
|
132
|
+
async sealRecoveryVerdict (request, verdict) {
|
|
133
|
+
return await access.read((group)=>sealRecoveryVerdict({
|
|
134
|
+
group,
|
|
135
|
+
identity,
|
|
136
|
+
request: new TextDecoder().decode(request),
|
|
137
|
+
verdict
|
|
138
|
+
}));
|
|
139
|
+
},
|
|
140
|
+
async openRecoveryVerdict (sealed, requestID) {
|
|
141
|
+
const held = pending.get(requestID);
|
|
142
|
+
if (held == null) return null;
|
|
143
|
+
try {
|
|
144
|
+
return await access.read((group)=>openRecoveryVerdict({
|
|
145
|
+
group,
|
|
146
|
+
sealed,
|
|
147
|
+
requestID,
|
|
148
|
+
ephemeralPrivateKey: held
|
|
149
|
+
}));
|
|
150
|
+
} catch {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
async prepareRecovery () {
|
|
155
|
+
return await access.read((group)=>recovery.prepare(group));
|
|
156
|
+
},
|
|
110
157
|
async readEpoch () {
|
|
111
158
|
return await access.read((group)=>Number(group.epoch));
|
|
112
159
|
},
|
|
160
|
+
sendAdmission: ()=>access.admission(),
|
|
113
161
|
async rosterEntries () {
|
|
114
162
|
return await access.read((group)=>group.listMembers().map((member)=>({
|
|
115
163
|
did: member.id,
|
|
@@ -152,6 +200,7 @@ export async function deriveRecoverySecret(handle) {
|
|
|
152
200
|
for (const token of tokens ?? [])resolved.set(ledgerEntryDigest(token), token);
|
|
153
201
|
const ignored = Symbol('ignored commit');
|
|
154
202
|
let refusedEpoch;
|
|
203
|
+
let refusal;
|
|
155
204
|
try {
|
|
156
205
|
return await access.mutate(async (group, persist)=>{
|
|
157
206
|
if (group.epoch !== frameEpoch) {
|
|
@@ -176,6 +225,7 @@ export async function deriveRecoverySecret(handle) {
|
|
|
176
225
|
// Thrown so the adapter does not save a handle the commit left untouched.
|
|
177
226
|
if (!result.applied) {
|
|
178
227
|
refusedEpoch = result.epochBefore;
|
|
228
|
+
refusal = result.refusal;
|
|
179
229
|
throw ignored;
|
|
180
230
|
}
|
|
181
231
|
return {
|
|
@@ -188,18 +238,21 @@ export async function deriveRecoverySecret(handle) {
|
|
|
188
238
|
if (error === ignored && refusedEpoch != null) return {
|
|
189
239
|
advanced: false,
|
|
190
240
|
epochBefore: refusedEpoch,
|
|
191
|
-
epochAfter: refusedEpoch
|
|
241
|
+
epochAfter: refusedEpoch,
|
|
242
|
+
...refusal == null ? {} : {
|
|
243
|
+
refusal
|
|
244
|
+
}
|
|
192
245
|
};
|
|
193
246
|
throw error;
|
|
194
247
|
}
|
|
195
248
|
},
|
|
196
|
-
async createRecoveryRequest (requestID) {
|
|
249
|
+
async createRecoveryRequest (requestID, deadlineMs) {
|
|
197
250
|
const { request, ephemeralPrivateKey } = await access.read((group)=>createRecoveryRequest({
|
|
198
251
|
group,
|
|
199
252
|
identity,
|
|
200
253
|
requestID
|
|
201
254
|
}));
|
|
202
|
-
pending.put(requestID, ephemeralPrivateKey);
|
|
255
|
+
pending.put(requestID, ephemeralPrivateKey, Math.max(REQUEST_TTL_MS, deadlineMs ?? 0));
|
|
203
256
|
return utf8.encode(request);
|
|
204
257
|
},
|
|
205
258
|
async sealGroupInfo (request) {
|
|
@@ -214,9 +267,9 @@ export async function deriveRecoverySecret(handle) {
|
|
|
214
267
|
async applyRecovery (sealed, requestID) {
|
|
215
268
|
const held = pending.get(requestID);
|
|
216
269
|
if (held == null) return null;
|
|
217
|
-
let
|
|
270
|
+
let reply;
|
|
218
271
|
try {
|
|
219
|
-
|
|
272
|
+
reply = await access.read((group)=>openRecoveryGroupInfo({
|
|
220
273
|
group,
|
|
221
274
|
sealed,
|
|
222
275
|
requestID,
|
|
@@ -227,21 +280,49 @@ export async function deriveRecoverySecret(handle) {
|
|
|
227
280
|
// fails the membership attestation, and both are `null`.
|
|
228
281
|
return null;
|
|
229
282
|
}
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
283
|
+
const candidate = await access.read(async (group)=>{
|
|
284
|
+
const rejoined = await recovery.join(group, reply);
|
|
285
|
+
if (rejoined == null || 'renewalRequired' in rejoined) return rejoined;
|
|
286
|
+
return {
|
|
287
|
+
...rejoined,
|
|
288
|
+
knownRegistry: knownRecoveryRegistry(group.registry, rejoined.sourceRegistry)
|
|
289
|
+
};
|
|
290
|
+
});
|
|
291
|
+
if (candidate == null || 'renewalRequired' in candidate) return candidate;
|
|
292
|
+
const rejoined = candidate;
|
|
293
|
+
if (rejoined.group.anchor.controller != null && !recoverySignerEligible({
|
|
294
|
+
group: rejoined.group,
|
|
295
|
+
known: rejoined.knownRegistry,
|
|
296
|
+
signer: reply.signer
|
|
297
|
+
})) return null;
|
|
298
|
+
const judge = createVerdictJudge({
|
|
299
|
+
group: rejoined.group,
|
|
300
|
+
sourceTree: rejoined.sourceTree,
|
|
301
|
+
known: rejoined.knownRegistry,
|
|
302
|
+
requestID
|
|
303
|
+
});
|
|
304
|
+
let accepted;
|
|
305
|
+
const result = {
|
|
306
|
+
group: rejoined.group,
|
|
307
|
+
sourceTree: rejoined.sourceTree,
|
|
308
|
+
groupInfo: reply.groupInfo,
|
|
309
|
+
knownRegistry: rejoined.knownRegistry,
|
|
310
|
+
replyLedger: reply.ledger,
|
|
311
|
+
signer: reply.signer,
|
|
237
312
|
commit: rejoined.commitMessage,
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
313
|
+
epoch: Number(rejoined.group.epoch),
|
|
314
|
+
markBindingUnusable: rejoined.markBindingUnusable,
|
|
315
|
+
confirmationKey: judge.confirmationKey,
|
|
316
|
+
judgeVerdict: judge.judgeVerdict,
|
|
317
|
+
onAccepted: ()=>{
|
|
318
|
+
accepted ??= access.replace(rejoined.group).catch((error)=>{
|
|
319
|
+
accepted = undefined;
|
|
320
|
+
throw error;
|
|
321
|
+
});
|
|
322
|
+
return accepted;
|
|
243
323
|
}
|
|
244
324
|
};
|
|
325
|
+
return result;
|
|
245
326
|
},
|
|
246
327
|
async isLedgerComplete () {
|
|
247
328
|
return await access.read((group)=>group.isLedgerComplete());
|
|
@@ -301,4 +382,6 @@ export async function deriveRecoverySecret(handle) {
|
|
|
301
382
|
return secret;
|
|
302
383
|
}
|
|
303
384
|
};
|
|
385
|
+
groupHandleAccess.set(port, access);
|
|
386
|
+
return port;
|
|
304
387
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type OwnIdentity } from '@kokuin/token';
|
|
2
|
+
import { type ClientState, type ControllerBinding, type DeviceRegistry, type GroupHandle } from '@kumiai/mls';
|
|
3
|
+
import type { OpenedRecoveryVerdict, PendingRecovery } from '@kumiai/rpc';
|
|
4
|
+
export type RecoveryBindingRequest = {
|
|
5
|
+
groupID: string;
|
|
6
|
+
controllerID: string;
|
|
7
|
+
current: ControllerBinding;
|
|
8
|
+
};
|
|
9
|
+
export type RecoveryBinding = (request: RecoveryBindingRequest) => Promise<ControllerBinding | null>;
|
|
10
|
+
export type RecoveryReply = {
|
|
11
|
+
groupInfo: Uint8Array;
|
|
12
|
+
ledger: Array<string>;
|
|
13
|
+
signer: string;
|
|
14
|
+
};
|
|
15
|
+
export type VerdictJudgeParams = {
|
|
16
|
+
group: GroupHandle;
|
|
17
|
+
sourceTree: ClientState['ratchetTree'];
|
|
18
|
+
known: DeviceRegistry;
|
|
19
|
+
requestID: string;
|
|
20
|
+
};
|
|
21
|
+
/** Retained with the speculative handle for subsequent signer judgement. */
|
|
22
|
+
export type RecoveryCandidate = PendingRecovery & {
|
|
23
|
+
group: GroupHandle;
|
|
24
|
+
sourceTree: ClientState['ratchetTree'];
|
|
25
|
+
groupInfo: Uint8Array;
|
|
26
|
+
knownRegistry: DeviceRegistry;
|
|
27
|
+
replyLedger: Array<string>;
|
|
28
|
+
signer: string;
|
|
29
|
+
};
|
|
30
|
+
export declare function knownRecoveryRegistry(known: DeviceRegistry, reply: DeviceRegistry): DeviceRegistry;
|
|
31
|
+
export declare function createRecoveryBindingState(identity: OwnIdentity, host?: RecoveryBinding): {
|
|
32
|
+
prepare(group: GroupHandle): Promise<"ready" | "renewal-required">;
|
|
33
|
+
join(group: GroupHandle, reply: RecoveryReply): Promise<{
|
|
34
|
+
markBindingUnusable: () => void;
|
|
35
|
+
commitMessage: Uint8Array;
|
|
36
|
+
group: GroupHandle;
|
|
37
|
+
sourceTree: ClientState["ratchetTree"];
|
|
38
|
+
sourceRegistry: DeviceRegistry;
|
|
39
|
+
} | {
|
|
40
|
+
readonly renewalRequired: true;
|
|
41
|
+
} | null>;
|
|
42
|
+
};
|
|
43
|
+
export declare function createVerdictJudge(params: VerdictJudgeParams): {
|
|
44
|
+
confirmationKey(position: string, commitDigest: string): Promise<Uint8Array<ArrayBufferLike>>;
|
|
45
|
+
judgeVerdict(opened: OpenedRecoveryVerdict): "authoritative" | "advisory";
|
|
46
|
+
};
|
package/lib/recovery.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { normalizeDID } from '@kokuin/token';
|
|
2
|
+
import { assertRecoveryBinding, confirmationKey, confirmationTag, joinGroupExternal, LeafBindingError, LeafLapsedError, parseMLSCredentialIdentity, RevokeProofError, recoverySignerEligible } from '@kumiai/mls';
|
|
3
|
+
export function knownRecoveryRegistry(known, reply) {
|
|
4
|
+
const devices = new Map(reply.devices);
|
|
5
|
+
for (const [did, record] of known.devices){
|
|
6
|
+
if (record.status === 'revoked' || !devices.has(did)) devices.set(did, record);
|
|
7
|
+
}
|
|
8
|
+
const controllers = new Map(reply.controllers);
|
|
9
|
+
for (const [id, projection] of known.controllers){
|
|
10
|
+
const offered = controllers.get(id);
|
|
11
|
+
controllers.set(id, offered == null ? projection : {
|
|
12
|
+
...offered,
|
|
13
|
+
genFloor: Math.max(projection.genFloor, offered.genFloor),
|
|
14
|
+
timeFloor: Math.max(projection.timeFloor, offered.timeFloor)
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
devices,
|
|
19
|
+
controllers
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export function createRecoveryBindingState(identity, host) {
|
|
23
|
+
let binding;
|
|
24
|
+
let asked = false;
|
|
25
|
+
let prepared = false;
|
|
26
|
+
const unusable = new Set();
|
|
27
|
+
const bindingID = (value)=>JSON.stringify([
|
|
28
|
+
value.id,
|
|
29
|
+
value.prefix,
|
|
30
|
+
value.capability,
|
|
31
|
+
value.holderGrant
|
|
32
|
+
]);
|
|
33
|
+
function markBindingUnusable(id) {
|
|
34
|
+
if (id == null) return;
|
|
35
|
+
unusable.delete(id);
|
|
36
|
+
unusable.add(id);
|
|
37
|
+
while(unusable.size > 16){
|
|
38
|
+
const oldest = unusable.values().next().value;
|
|
39
|
+
if (oldest != null) unusable.delete(oldest);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function usable(group, candidate) {
|
|
43
|
+
if (unusable.has(bindingID(candidate))) return false;
|
|
44
|
+
try {
|
|
45
|
+
await assertRecoveryBinding({
|
|
46
|
+
group,
|
|
47
|
+
identity,
|
|
48
|
+
controller: candidate
|
|
49
|
+
});
|
|
50
|
+
return true;
|
|
51
|
+
} catch {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function current(group) {
|
|
56
|
+
const leaf = group.bindingOfDID(identity.id);
|
|
57
|
+
return leaf?.controller == null || leaf.prefix == null || leaf.capability == null ? undefined : {
|
|
58
|
+
id: leaf.controller,
|
|
59
|
+
prefix: leaf.prefix,
|
|
60
|
+
capability: leaf.capability,
|
|
61
|
+
...leaf.holderGrant == null ? {} : {
|
|
62
|
+
holderGrant: leaf.holderGrant
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
async function request(group) {
|
|
67
|
+
if (asked) return undefined;
|
|
68
|
+
asked = true;
|
|
69
|
+
const held = current(group);
|
|
70
|
+
if (host == null || held == null || group.anchor.controller == null) return undefined;
|
|
71
|
+
return await host({
|
|
72
|
+
groupID: group.groupID,
|
|
73
|
+
controllerID: group.anchor.controller,
|
|
74
|
+
current: held
|
|
75
|
+
}) ?? undefined;
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
async prepare (group) {
|
|
79
|
+
asked = false;
|
|
80
|
+
prepared = true;
|
|
81
|
+
binding = current(group);
|
|
82
|
+
if (group.anchor.controller == null) return 'ready';
|
|
83
|
+
if (binding != null && await usable(group, binding)) return 'ready';
|
|
84
|
+
binding = await request(group);
|
|
85
|
+
return binding != null && await usable(group, binding) ? 'ready' : 'renewal-required';
|
|
86
|
+
},
|
|
87
|
+
async join (group, reply) {
|
|
88
|
+
if (!prepared) {
|
|
89
|
+
prepared = true;
|
|
90
|
+
binding = current(group);
|
|
91
|
+
}
|
|
92
|
+
let signerEligible = group.anchor.controller == null;
|
|
93
|
+
const build = (controller)=>{
|
|
94
|
+
signerEligible = group.anchor.controller == null;
|
|
95
|
+
return joinGroupExternal({
|
|
96
|
+
identity,
|
|
97
|
+
groupInfo: reply.groupInfo,
|
|
98
|
+
credential: group.credential,
|
|
99
|
+
resync: true,
|
|
100
|
+
controller,
|
|
101
|
+
beforeBinding: async (pending, source)=>{
|
|
102
|
+
if (group.anchor.controller == null) return;
|
|
103
|
+
if (!recoverySignerEligible({
|
|
104
|
+
group: pending,
|
|
105
|
+
known: knownRecoveryRegistry(group.registry, source.registry),
|
|
106
|
+
signer: reply.signer
|
|
107
|
+
})) throw new Error('Ineligible recovery attestation signer');
|
|
108
|
+
signerEligible = true;
|
|
109
|
+
if (controller == null || !await usable(group, controller)) throw new LeafBindingError('floating-refused');
|
|
110
|
+
},
|
|
111
|
+
options: {
|
|
112
|
+
commitPolicy: group.commitPolicy,
|
|
113
|
+
resolveLedgerEntries: group.resolveLedgerEntries,
|
|
114
|
+
onLedgerEntries: group.onLedgerEntries
|
|
115
|
+
},
|
|
116
|
+
...group.anchor.controller == null ? {} : {
|
|
117
|
+
ledgerEntries: reply.ledger
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
const candidate = async (controller)=>{
|
|
122
|
+
const result = await build(controller);
|
|
123
|
+
const carried = current(result.group);
|
|
124
|
+
const id = carried == null ? undefined : bindingID(carried);
|
|
125
|
+
return {
|
|
126
|
+
...result,
|
|
127
|
+
markBindingUnusable: ()=>markBindingUnusable(id)
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
try {
|
|
131
|
+
return await candidate(binding);
|
|
132
|
+
} catch (error) {
|
|
133
|
+
if (!signerEligible || group.anchor.controller == null || !(error instanceof LeafBindingError || error instanceof LeafLapsedError || error instanceof RevokeProofError)) return null;
|
|
134
|
+
binding = await request(group);
|
|
135
|
+
if (binding == null || !await usable(group, binding)) return {
|
|
136
|
+
renewalRequired: true
|
|
137
|
+
};
|
|
138
|
+
try {
|
|
139
|
+
return await candidate(binding);
|
|
140
|
+
} catch (replacementError) {
|
|
141
|
+
if (!signerEligible) return null;
|
|
142
|
+
return replacementError instanceof LeafBindingError || replacementError instanceof LeafLapsedError || replacementError instanceof RevokeProofError ? {
|
|
143
|
+
renewalRequired: true
|
|
144
|
+
} : null;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
export function createVerdictJudge(params) {
|
|
151
|
+
const { group, sourceTree, known, requestID } = params;
|
|
152
|
+
let tuple;
|
|
153
|
+
return {
|
|
154
|
+
async confirmationKey (position, commitDigest) {
|
|
155
|
+
if (tuple != null && (tuple.position !== position || tuple.commitDigest !== commitDigest)) throw new Error('Pending recovery already bound to another commit');
|
|
156
|
+
tuple ??= {
|
|
157
|
+
position,
|
|
158
|
+
commitDigest
|
|
159
|
+
};
|
|
160
|
+
const key = await confirmationKey(group, position, commitDigest);
|
|
161
|
+
tuple.tag = confirmationTag(key, requestID);
|
|
162
|
+
return key;
|
|
163
|
+
},
|
|
164
|
+
judgeVerdict (opened) {
|
|
165
|
+
const { signer, verdict } = opened;
|
|
166
|
+
if (normalizeDID(signer) === normalizeDID(group.credential.id) || tuple == null || verdict.groupID !== group.groupID || verdict.requestID !== requestID || verdict.position !== tuple.position || verdict.commitDigest !== tuple.commitDigest) return 'advisory';
|
|
167
|
+
if (verdict.verdict === 'confirmed' && (verdict.epoch !== Number(group.epoch) || verdict.tag !== tuple.tag)) return 'advisory';
|
|
168
|
+
if (group.anchor.controller != null) return recoverySignerEligible({
|
|
169
|
+
group,
|
|
170
|
+
known,
|
|
171
|
+
signer
|
|
172
|
+
}) ? 'authoritative' : 'advisory';
|
|
173
|
+
if (verdict.verdict === 'confirmed') return 'authoritative';
|
|
174
|
+
for (const node of sourceTree){
|
|
175
|
+
if (node == null || !('leaf' in node)) continue;
|
|
176
|
+
const credential = node.leaf.credential;
|
|
177
|
+
if (!('identity' in credential)) continue;
|
|
178
|
+
try {
|
|
179
|
+
if (normalizeDID(parseMLSCredentialIdentity(credential.identity).id) === normalizeDID(signer)) return 'authoritative';
|
|
180
|
+
} catch {}
|
|
181
|
+
}
|
|
182
|
+
return 'advisory';
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
}
|
package/lib/revoke.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type GroupHandle, type RevokeBuildResult, type RevokeWithProofParams } from '@kumiai/mls';
|
|
2
|
+
import type { GroupMLS, GroupPeer, GroupProtocolDefinition } from '@kumiai/rpc';
|
|
3
|
+
export type RevokeJournalOptions = {
|
|
4
|
+
/** Must restore the derived handle INCLUDING its ledger through the host's adoptJournalled. */
|
|
5
|
+
serializeJournal: (derived: GroupHandle) => Promise<Uint8Array> | Uint8Array;
|
|
6
|
+
};
|
|
7
|
+
export type PublishRevokeResult = {
|
|
8
|
+
status: 'committed';
|
|
9
|
+
epoch: number;
|
|
10
|
+
} | Exclude<RevokeBuildResult, {
|
|
11
|
+
status: 'built';
|
|
12
|
+
}>;
|
|
13
|
+
export type PublishRevokeProofParams<Protocols extends Record<string, GroupProtocolDefinition>> = {
|
|
14
|
+
peer: GroupPeer<Protocols>;
|
|
15
|
+
mls: GroupMLS;
|
|
16
|
+
input: RevokeWithProofParams;
|
|
17
|
+
options: RevokeJournalOptions;
|
|
18
|
+
};
|
|
19
|
+
export declare function publishRevokeProof<Protocols extends Record<string, GroupProtocolDefinition>>(params: PublishRevokeProofParams<Protocols>): Promise<PublishRevokeResult>;
|
package/lib/revoke.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { revokeWithProof } from '@kumiai/mls';
|
|
2
|
+
import { groupHandleAccess } from './mls.js';
|
|
3
|
+
export async function publishRevokeProof(params) {
|
|
4
|
+
const { peer, mls, input, options } = params;
|
|
5
|
+
const access = groupHandleAccess.get(mls);
|
|
6
|
+
if (access == null) throw new Error('publishRevokeProof requires an adapter-created GroupMLS');
|
|
7
|
+
const finished = Symbol('proof needs no commit');
|
|
8
|
+
let outcome;
|
|
9
|
+
try {
|
|
10
|
+
await peer.commit(async ()=>{
|
|
11
|
+
const built = await access.read(async (group)=>{
|
|
12
|
+
const result = await revokeWithProof(group, input);
|
|
13
|
+
return {
|
|
14
|
+
result,
|
|
15
|
+
priorTokens: new Set(group.ledgerTokens)
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
if (built.result.status !== 'built') {
|
|
19
|
+
outcome = built.result;
|
|
20
|
+
throw finished;
|
|
21
|
+
}
|
|
22
|
+
const { newGroup, commitMessage, epoch } = built.result.result;
|
|
23
|
+
const journal = await options.serializeJournal(newGroup);
|
|
24
|
+
return {
|
|
25
|
+
commit: commitMessage,
|
|
26
|
+
bodies: newGroup.ledgerTokens.filter((token)=>!built.priorTokens.has(token)),
|
|
27
|
+
kind: 'ledger',
|
|
28
|
+
journal,
|
|
29
|
+
onAccepted: async ()=>{
|
|
30
|
+
await access.replace(newGroup);
|
|
31
|
+
outcome = {
|
|
32
|
+
status: 'committed',
|
|
33
|
+
epoch: Number(epoch)
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}, {
|
|
38
|
+
holdLogSends: true
|
|
39
|
+
});
|
|
40
|
+
} catch (error) {
|
|
41
|
+
if (error !== finished) throw error;
|
|
42
|
+
}
|
|
43
|
+
if (outcome == null) throw new Error('Proof commit completed without an outcome');
|
|
44
|
+
return outcome;
|
|
45
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kumiai/mls-rpc",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.2",
|
|
4
4
|
"description": "The @kumiai/rpc consumer ports (GroupCrypto, GroupMLS) implemented over @kumiai/mls",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mls",
|
|
@@ -25,16 +25,16 @@
|
|
|
25
25
|
"lib/*"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@kokuin/token": "^0.5.
|
|
29
|
-
"@kumiai/mls": "^0.10.
|
|
30
|
-
"@kumiai/rpc": "^0.10.
|
|
28
|
+
"@kokuin/token": "^0.5.1",
|
|
29
|
+
"@kumiai/mls": "^0.10.2",
|
|
30
|
+
"@kumiai/rpc": "^0.10.7",
|
|
31
31
|
"@noble/ciphers": "^2.4.0",
|
|
32
32
|
"@sozai/runtime": "^0.1.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@kumiai/rpc-conformance": "^0.10.
|
|
35
|
+
"@kumiai/rpc-conformance": "^0.10.1",
|
|
36
36
|
"ts-mls": "2.0.0-rc.13",
|
|
37
|
-
"vitest": "^5.0.
|
|
37
|
+
"vitest": "^5.0.3"
|
|
38
38
|
},
|
|
39
39
|
"publishConfig": {
|
|
40
40
|
"access": "public"
|