@kumiai/mls-rpc 0.10.0 → 0.10.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/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 publishedEpoch = Number(params.handle().epoch);
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: ()=>publishedEpoch,
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
- publishedEpoch = Number(handle.epoch);
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
- publishedEpoch = Number(next.epoch);
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
- publishedEpoch = Number(handle.epoch);
45
+ publishedAdmission = handle.sendAdmission();
44
46
  return result;
45
47
  })
46
48
  };
@@ -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. */
@@ -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
- ...refused(committerDID),
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, joinGroupExternal, ledgerEntryDigest, MissingLedgerEntriesError, openSealedGroupInfo, openSealedLedger, readCommitEntryIDs, readMessageEpoch, sealGroupInfo, sealLedger } from '@kumiai/mls';
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 cutoff = Date.now() - ttlMS;
47
+ const now = Date.now();
46
48
  for (const [id, request] of requests){
47
- if (request.mintedAt <= cutoff) remove(id, 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
- }, ttlMS);
65
+ }, retentionMs);
63
66
  const nodeTimer = timer;
64
67
  nodeTimer.unref?.();
65
68
  requests.set(requestID, {
66
69
  ephemeralPrivateKey,
67
- mintedAt: Date.now(),
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
- return {
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 groupInfo;
270
+ let reply;
218
271
  try {
219
- groupInfo = await access.read((group)=>openSealedGroupInfo({
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 rejoined = await access.read((group)=>joinGroupExternal({
231
- identity,
232
- groupInfo,
233
- credential: group.credential,
234
- resync: true
235
- }));
236
- return {
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
- // Adopted ONLY if the hub accepts the commit. A peer that adopted first would sit
239
- // on a branch of its own the moment it lost the compare-and-set.
240
- onAccepted: async ()=>{
241
- await access.replace(rejoined.group);
242
- if (pending.get(requestID) === held) pending.delete(requestID);
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
+ };
@@ -0,0 +1,181 @@
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
+ ]);
32
+ function markBindingUnusable(id) {
33
+ if (id == null) return;
34
+ unusable.delete(id);
35
+ unusable.add(id);
36
+ while(unusable.size > 16){
37
+ const oldest = unusable.values().next().value;
38
+ if (oldest != null) unusable.delete(oldest);
39
+ }
40
+ }
41
+ async function usable(group, candidate) {
42
+ if (unusable.has(bindingID(candidate))) return false;
43
+ try {
44
+ await assertRecoveryBinding({
45
+ group,
46
+ identity,
47
+ controller: candidate
48
+ });
49
+ return true;
50
+ } catch {
51
+ return false;
52
+ }
53
+ }
54
+ function current(group) {
55
+ const leaf = group.bindingOfDID(identity.id);
56
+ return leaf?.controller == null || leaf.prefix == null || leaf.capability == null ? undefined : {
57
+ id: leaf.controller,
58
+ prefix: leaf.prefix,
59
+ capability: leaf.capability
60
+ };
61
+ }
62
+ async function request(group) {
63
+ if (asked) return undefined;
64
+ asked = true;
65
+ const held = current(group);
66
+ if (host == null || held == null || group.anchor.controller == null) return undefined;
67
+ return await host({
68
+ groupID: group.groupID,
69
+ controllerID: group.anchor.controller,
70
+ current: held
71
+ }) ?? undefined;
72
+ }
73
+ return {
74
+ async prepare (group) {
75
+ asked = false;
76
+ prepared = true;
77
+ binding = current(group);
78
+ if (group.anchor.controller == null) return 'ready';
79
+ if (binding != null && await usable(group, binding)) return 'ready';
80
+ binding = await request(group);
81
+ return binding != null && await usable(group, binding) ? 'ready' : 'renewal-required';
82
+ },
83
+ async join (group, reply) {
84
+ if (!prepared) {
85
+ prepared = true;
86
+ binding = current(group);
87
+ }
88
+ let signerEligible = group.anchor.controller == null;
89
+ const build = (controller)=>{
90
+ signerEligible = group.anchor.controller == null;
91
+ return joinGroupExternal({
92
+ identity,
93
+ groupInfo: reply.groupInfo,
94
+ credential: group.credential,
95
+ resync: true,
96
+ controller,
97
+ beforeBinding: async (pending, source)=>{
98
+ if (group.anchor.controller == null) return;
99
+ if (!recoverySignerEligible({
100
+ group: pending,
101
+ known: knownRecoveryRegistry(group.registry, source.registry),
102
+ signer: reply.signer
103
+ })) throw new Error('Ineligible recovery attestation signer');
104
+ signerEligible = true;
105
+ if (controller == null || !await usable(group, controller)) throw new LeafBindingError('floating-refused');
106
+ },
107
+ options: {
108
+ commitPolicy: group.commitPolicy,
109
+ resolveLedgerEntries: group.resolveLedgerEntries,
110
+ onLedgerEntries: group.onLedgerEntries
111
+ },
112
+ ...group.anchor.controller == null ? {} : {
113
+ ledgerEntries: reply.ledger
114
+ }
115
+ });
116
+ };
117
+ const candidate = async (controller)=>{
118
+ const result = await build(controller);
119
+ const carried = current(result.group);
120
+ const id = carried == null ? undefined : bindingID(carried);
121
+ return {
122
+ ...result,
123
+ markBindingUnusable: ()=>markBindingUnusable(id)
124
+ };
125
+ };
126
+ try {
127
+ return await candidate(binding);
128
+ } catch (error) {
129
+ if (!signerEligible || group.anchor.controller == null || !(error instanceof LeafBindingError || error instanceof LeafLapsedError || error instanceof RevokeProofError)) return null;
130
+ binding = await request(group);
131
+ if (binding == null || !await usable(group, binding)) return {
132
+ renewalRequired: true
133
+ };
134
+ try {
135
+ return await candidate(binding);
136
+ } catch (replacementError) {
137
+ if (!signerEligible) return null;
138
+ return replacementError instanceof LeafBindingError || replacementError instanceof LeafLapsedError || replacementError instanceof RevokeProofError ? {
139
+ renewalRequired: true
140
+ } : null;
141
+ }
142
+ }
143
+ }
144
+ };
145
+ }
146
+ export function createVerdictJudge(params) {
147
+ const { group, sourceTree, known, requestID } = params;
148
+ let tuple;
149
+ return {
150
+ async confirmationKey (position, commitDigest) {
151
+ if (tuple != null && (tuple.position !== position || tuple.commitDigest !== commitDigest)) throw new Error('Pending recovery already bound to another commit');
152
+ tuple ??= {
153
+ position,
154
+ commitDigest
155
+ };
156
+ const key = await confirmationKey(group, position, commitDigest);
157
+ tuple.tag = confirmationTag(key, requestID);
158
+ return key;
159
+ },
160
+ judgeVerdict (opened) {
161
+ const { signer, verdict } = opened;
162
+ 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';
163
+ if (verdict.verdict === 'confirmed' && (verdict.epoch !== Number(group.epoch) || verdict.tag !== tuple.tag)) return 'advisory';
164
+ if (group.anchor.controller != null) return recoverySignerEligible({
165
+ group,
166
+ known,
167
+ signer
168
+ }) ? 'authoritative' : 'advisory';
169
+ if (verdict.verdict === 'confirmed') return 'authoritative';
170
+ for (const node of sourceTree){
171
+ if (node == null || !('leaf' in node)) continue;
172
+ const credential = node.leaf.credential;
173
+ if (!('identity' in credential)) continue;
174
+ try {
175
+ if (normalizeDID(parseMLSCredentialIdentity(credential.identity).id) === normalizeDID(signer)) return 'authoritative';
176
+ } catch {}
177
+ }
178
+ return 'advisory';
179
+ }
180
+ };
181
+ }
@@ -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.0",
3
+ "version": "0.10.1",
4
4
  "description": "The @kumiai/rpc consumer ports (GroupCrypto, GroupMLS) implemented over @kumiai/mls",
5
5
  "keywords": [
6
6
  "mls",
@@ -26,15 +26,15 @@
26
26
  ],
27
27
  "dependencies": {
28
28
  "@kokuin/token": "^0.5.0",
29
- "@kumiai/mls": "^0.10.0",
30
- "@kumiai/rpc": "^0.10.0",
29
+ "@kumiai/mls": "^0.10.1",
30
+ "@kumiai/rpc": "^0.10.3",
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.0",
35
+ "@kumiai/rpc-conformance": "^0.10.1",
36
36
  "ts-mls": "2.0.0-rc.13",
37
- "vitest": "^5.0.1"
37
+ "vitest": "^5.0.3"
38
38
  },
39
39
  "publishConfig": {
40
40
  "access": "public"