@kumiai/mls-rpc 0.13.3 → 0.13.5
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 +22 -0
- package/lib/encryptor.d.ts +60 -0
- package/lib/encryptor.js +275 -0
- package/lib/group-state-store.d.ts +75 -0
- package/lib/group-state-store.js +7 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +1 -0
- package/package.json +12 -5
package/README.md
CHANGED
|
@@ -31,6 +31,28 @@ depend on both.
|
|
|
31
31
|
- `deriveEntryKey`, `sealEntries`, `openEntries`, `ENTRY_SEAL_LABEL` — the ledger-entry seal.
|
|
32
32
|
- `createRecoveryPending`, `deriveRecoverySecret`, `RECOVERY_LABEL` — recovery request keys and the
|
|
33
33
|
default recovery secret.
|
|
34
|
+
- `MLSEncryptor({ registry, groupID, peerDID, role })` — the `@kumiai/hub-tunnel` `Encryptor` for
|
|
35
|
+
one peer of a group, over an `MLSEncryptorRegistry` (see below).
|
|
36
|
+
|
|
37
|
+
## `MLSEncryptor`
|
|
38
|
+
|
|
39
|
+
The registry runs each `withSenderHandle` and `withReceiveHandle` callback under the group's mutex,
|
|
40
|
+
shared by every encryptor of the group. Concurrent `decrypt` calls open in batches of up to 64 in
|
|
41
|
+
one `withReceiveHandle` call, which reports how many frames advanced the ratchet.
|
|
42
|
+
|
|
43
|
+
A `decrypt` rejects with:
|
|
44
|
+
- `DuplicateFrameError` for a frame any encryptor of the same registry and group already opened. The
|
|
45
|
+
ratchet is not touched. The memory spans the last 1024 opened frames.
|
|
46
|
+
- `ExpiredFrameError` for a frame whose key is no longer retained, with the ts-mls error as `cause`.
|
|
47
|
+
- a plain `Error` when the MLS sender is not `peerDID`, when `peerDID` has left the group, or for a
|
|
48
|
+
past-epoch or nameless frame that is not a reply to an outstanding dialer request.
|
|
49
|
+
|
|
50
|
+
Every failure except a duplicate goes to the optional `reportDecryptFailure`.
|
|
51
|
+
|
|
52
|
+
`watchPeerRemoval(dispose)` calls `dispose` once, and forgets every outstanding request, when the
|
|
53
|
+
peer may have left. That is an `onEpochChanged` epoch that is not the next one or does not name the
|
|
54
|
+
peer, or a frame opened at a tree without it. Wrap the hub with `trackPublish(hub)`: a failed publish
|
|
55
|
+
retires the request it carried, so a late reply to it is refused.
|
|
34
56
|
|
|
35
57
|
## Share one access instance
|
|
36
58
|
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type Encryptor, type MailboxHub } from '@kumiai/hub-tunnel';
|
|
2
|
+
import { type GroupHandle } from '@kumiai/mls';
|
|
3
|
+
/** A received frame of a group that this device could not open. */
|
|
4
|
+
export type DecryptFailure = {
|
|
5
|
+
groupID: string;
|
|
6
|
+
/** The epoch the frame was sealed at, when its header was readable. */
|
|
7
|
+
epoch?: number;
|
|
8
|
+
error: unknown;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The group access an `MLSEncryptor` needs. Both `with*Handle` calls hold the group's mutex
|
|
12
|
+
* for the whole callback, shared by every encryptor of the group.
|
|
13
|
+
*/
|
|
14
|
+
export type MLSEncryptorRegistry = {
|
|
15
|
+
/** Run an application encryption. The registry reserves the sender generation it spends. */
|
|
16
|
+
withSenderHandle<T>(groupID: string, fn: (handle: GroupHandle) => Promise<T>): Promise<T>;
|
|
17
|
+
/** Run a batch of decryptions. `accepted` counts the frames that advanced the ratchet. */
|
|
18
|
+
withReceiveHandle<T>(groupID: string, fn: (handle: GroupHandle) => Promise<{
|
|
19
|
+
result: T;
|
|
20
|
+
accepted: number;
|
|
21
|
+
}>): Promise<T>;
|
|
22
|
+
groupEpoch?(groupID: string): number | null | undefined;
|
|
23
|
+
/** `members` holds normalized DIDs, or is absent when the roster could not be read. */
|
|
24
|
+
onEpochChanged?(listener: (groupID: string, epoch: number, members?: ReadonlySet<string>) => void): () => void;
|
|
25
|
+
/** Must not throw: a throw here rejects every frame of the batch, not just the failed one. */
|
|
26
|
+
reportDecryptFailure?(failure: DecryptFailure): void;
|
|
27
|
+
};
|
|
28
|
+
export type MLSEncryptorParams = {
|
|
29
|
+
registry: MLSEncryptorRegistry;
|
|
30
|
+
groupID: string;
|
|
31
|
+
peerDID: string;
|
|
32
|
+
role: 'dialer' | 'listener';
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Adapts an {@link MLSEncryptorRegistry} to the `@kumiai/hub-tunnel` `Encryptor`. The
|
|
36
|
+
* registry's per-group mutex serializes encrypt and decrypt across every encryptor bound to
|
|
37
|
+
* the same `(registry, groupID)`. Concurrent decrypt calls share one receive batch.
|
|
38
|
+
*/
|
|
39
|
+
export declare class MLSEncryptor implements Encryptor {
|
|
40
|
+
#private;
|
|
41
|
+
constructor(params: MLSEncryptorParams);
|
|
42
|
+
encrypt(plaintext: Uint8Array): Promise<Uint8Array>;
|
|
43
|
+
/**
|
|
44
|
+
* Wrap `hub` so a failed publish retires the request its frame carried. Spreads `hub`: pass a
|
|
45
|
+
* plain object, since a class instance loses its prototype methods and getters.
|
|
46
|
+
*/
|
|
47
|
+
trackPublish(hub: MailboxHub): MailboxHub;
|
|
48
|
+
clear(): void;
|
|
49
|
+
/**
|
|
50
|
+
* Forgets one session's outstanding requests, for an encryptor several sessions share: `clear()`
|
|
51
|
+
* would also forget the requests of the sessions still open.
|
|
52
|
+
*/
|
|
53
|
+
retireSession(sessionID: string): void;
|
|
54
|
+
/**
|
|
55
|
+
* Call `dispose` once the peer may have left the group: an epoch change that is not the next
|
|
56
|
+
* epoch or does not name the peer, or a frame opened at a tree without it. Returns the unwatch.
|
|
57
|
+
*/
|
|
58
|
+
watchPeerRemoval(dispose: () => Promise<void>): () => void;
|
|
59
|
+
decrypt(ciphertext: Uint8Array): Promise<Uint8Array>;
|
|
60
|
+
}
|
package/lib/encryptor.js
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { normalizeDID } from '@kokuin/token';
|
|
2
|
+
import { DuplicateFrameError, decodeEnvelope, decodeFrame, ExpiredFrameError } from '@kumiai/hub-tunnel';
|
|
3
|
+
import { isGenerationExpired, readMessageEpoch } from '@kumiai/mls';
|
|
4
|
+
import { blake3 } from '@noble/hashes/blake3.js';
|
|
5
|
+
import { defer } from '@sozai/async';
|
|
6
|
+
import { toB64 } from '@sozai/codec';
|
|
7
|
+
// How many opened ciphertexts a group remembers. A hub redelivers a frame whose ack was lost at
|
|
8
|
+
// the next connect, so this only has to span the frames of one unflushed ack batch.
|
|
9
|
+
const OPENED_FRAME_MEMORY = 1024;
|
|
10
|
+
/**
|
|
11
|
+
* The ciphertexts each group of a registry has opened, as digests, oldest first. Shared by every
|
|
12
|
+
* encryptor of the group: a redelivered frame reaches whichever reader drains the topic now, and the
|
|
13
|
+
* ratchet that opened it once cannot open it again.
|
|
14
|
+
*/ const openedFrames = new WeakMap();
|
|
15
|
+
function openedFramesOf(registry, groupID) {
|
|
16
|
+
let groups = openedFrames.get(registry);
|
|
17
|
+
if (groups == null) {
|
|
18
|
+
groups = new Map();
|
|
19
|
+
openedFrames.set(registry, groups);
|
|
20
|
+
}
|
|
21
|
+
let opened = groups.get(groupID);
|
|
22
|
+
if (opened == null) {
|
|
23
|
+
opened = new Set();
|
|
24
|
+
groups.set(groupID, opened);
|
|
25
|
+
}
|
|
26
|
+
return opened;
|
|
27
|
+
}
|
|
28
|
+
function rememberOpened(opened, digest) {
|
|
29
|
+
opened.add(digest);
|
|
30
|
+
if (opened.size > OPENED_FRAME_MEMORY) opened.delete(opened.values().next().value);
|
|
31
|
+
}
|
|
32
|
+
function requestID(frame) {
|
|
33
|
+
if (frame.kind !== 'message') return null;
|
|
34
|
+
const { typ, rid } = frame.body.payload;
|
|
35
|
+
return (typ === 'request' || typ === 'stream' || typ === 'channel') && typeof rid === 'string' ? rid : null;
|
|
36
|
+
}
|
|
37
|
+
function replyID(frame) {
|
|
38
|
+
if (frame.kind !== 'message') return null;
|
|
39
|
+
const { typ, rid } = frame.body.payload;
|
|
40
|
+
return (typ === 'result' || typ === 'receive' || typ === 'error') && typeof rid === 'string' ? rid : null;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Adapts an {@link MLSEncryptorRegistry} to the `@kumiai/hub-tunnel` `Encryptor`. The
|
|
44
|
+
* registry's per-group mutex serializes encrypt and decrypt across every encryptor bound to
|
|
45
|
+
* the same `(registry, groupID)`. Concurrent decrypt calls share one receive batch.
|
|
46
|
+
*/ export class MLSEncryptor {
|
|
47
|
+
#registry;
|
|
48
|
+
#groupID;
|
|
49
|
+
#peerDID;
|
|
50
|
+
#role;
|
|
51
|
+
#outstanding = new Map();
|
|
52
|
+
#pendingPublish = new Map();
|
|
53
|
+
#closeForRemoval;
|
|
54
|
+
#pending = [];
|
|
55
|
+
#inFlight = new Map();
|
|
56
|
+
#flushScheduled = false;
|
|
57
|
+
constructor(params){
|
|
58
|
+
this.#registry = params.registry;
|
|
59
|
+
this.#groupID = params.groupID;
|
|
60
|
+
this.#peerDID = normalizeDID(params.peerDID);
|
|
61
|
+
this.#role = params.role;
|
|
62
|
+
}
|
|
63
|
+
async encrypt(plaintext) {
|
|
64
|
+
const frame = decodeFrame(plaintext);
|
|
65
|
+
const encrypted = await this.#registry.withSenderHandle(this.#groupID, (handle)=>handle.encrypt({
|
|
66
|
+
plaintext
|
|
67
|
+
}));
|
|
68
|
+
if (this.#role === 'dialer') {
|
|
69
|
+
const rid = requestID(frame);
|
|
70
|
+
if (rid !== null) {
|
|
71
|
+
const pending = this.#outstanding.get(frame.sessionID) ?? new Set();
|
|
72
|
+
pending.add(rid);
|
|
73
|
+
this.#outstanding.set(frame.sessionID, pending);
|
|
74
|
+
this.#pendingPublish.set(toB64(encrypted), {
|
|
75
|
+
sessionID: frame.sessionID,
|
|
76
|
+
rid
|
|
77
|
+
});
|
|
78
|
+
} else if (frame.kind === 'session-end') {
|
|
79
|
+
this.#retireSession(frame.sessionID);
|
|
80
|
+
} else if (frame.kind === 'message' && frame.body.payload.typ === 'abort') {
|
|
81
|
+
const cancelled = frame.body.payload.rid;
|
|
82
|
+
if (typeof cancelled === 'string') this.#retireRequest(frame.sessionID, cancelled);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return encrypted;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Wrap `hub` so a failed publish retires the request its frame carried. Spreads `hub`: pass a
|
|
89
|
+
* plain object, since a class instance loses its prototype methods and getters.
|
|
90
|
+
*/ trackPublish(hub) {
|
|
91
|
+
return {
|
|
92
|
+
...hub,
|
|
93
|
+
publish: async (params)=>{
|
|
94
|
+
let ciphertext;
|
|
95
|
+
try {
|
|
96
|
+
ciphertext = decodeEnvelope(params.payload).ciphertext;
|
|
97
|
+
} catch {
|
|
98
|
+
// Forward a malformed payload to the hub unchanged.
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
return await hub.publish(params);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
const pending = ciphertext === undefined ? undefined : this.#pendingPublish.get(ciphertext);
|
|
104
|
+
if (pending !== undefined) this.#retireRequest(pending.sessionID, pending.rid);
|
|
105
|
+
throw error;
|
|
106
|
+
} finally{
|
|
107
|
+
if (ciphertext !== undefined) this.#pendingPublish.delete(ciphertext);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
clear() {
|
|
113
|
+
this.#outstanding.clear();
|
|
114
|
+
this.#pendingPublish.clear();
|
|
115
|
+
}
|
|
116
|
+
#retireRequest(sessionID, rid) {
|
|
117
|
+
const pending = this.#outstanding.get(sessionID);
|
|
118
|
+
pending?.delete(rid);
|
|
119
|
+
if (pending?.size === 0) this.#outstanding.delete(sessionID);
|
|
120
|
+
for (const [ciphertext, published] of this.#pendingPublish){
|
|
121
|
+
if (published.sessionID === sessionID && published.rid === rid) this.#pendingPublish.delete(ciphertext);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Forgets one session's outstanding requests, for an encryptor several sessions share: `clear()`
|
|
126
|
+
* would also forget the requests of the sessions still open.
|
|
127
|
+
*/ retireSession(sessionID) {
|
|
128
|
+
this.#retireSession(sessionID);
|
|
129
|
+
}
|
|
130
|
+
#retireSession(sessionID) {
|
|
131
|
+
this.#outstanding.delete(sessionID);
|
|
132
|
+
for (const [ciphertext, published] of this.#pendingPublish){
|
|
133
|
+
if (published.sessionID === sessionID) this.#pendingPublish.delete(ciphertext);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Call `dispose` once the peer may have left the group: an epoch change that is not the next
|
|
138
|
+
* epoch or does not name the peer, or a frame opened at a tree without it. Returns the unwatch.
|
|
139
|
+
*/ watchPeerRemoval(dispose) {
|
|
140
|
+
let active = true;
|
|
141
|
+
let removed = false;
|
|
142
|
+
let observedEpoch = this.#registry.groupEpoch?.(this.#groupID);
|
|
143
|
+
const close = ()=>{
|
|
144
|
+
if (!active || removed) return;
|
|
145
|
+
removed = true;
|
|
146
|
+
this.clear();
|
|
147
|
+
void dispose().catch(()=>{});
|
|
148
|
+
};
|
|
149
|
+
this.#closeForRemoval = close;
|
|
150
|
+
const unsubscribe = this.#registry.onEpochChanged?.((groupID, epoch, members)=>{
|
|
151
|
+
if (groupID !== this.#groupID) return;
|
|
152
|
+
try {
|
|
153
|
+
if (observedEpoch == null || epoch !== observedEpoch + 1 || members?.has(this.#peerDID) !== true) close();
|
|
154
|
+
} catch {
|
|
155
|
+
close();
|
|
156
|
+
}
|
|
157
|
+
observedEpoch = epoch;
|
|
158
|
+
});
|
|
159
|
+
return ()=>{
|
|
160
|
+
active = false;
|
|
161
|
+
unsubscribe?.();
|
|
162
|
+
if (this.#closeForRemoval === close) this.#closeForRemoval = undefined;
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
decrypt(ciphertext) {
|
|
166
|
+
const key = toB64(ciphertext);
|
|
167
|
+
const cached = this.#inFlight.get(key);
|
|
168
|
+
if (cached != null) return cached;
|
|
169
|
+
const { promise, resolve, reject } = defer();
|
|
170
|
+
this.#pending.push({
|
|
171
|
+
ciphertext,
|
|
172
|
+
resolve,
|
|
173
|
+
reject
|
|
174
|
+
});
|
|
175
|
+
if (!this.#flushScheduled) {
|
|
176
|
+
this.#flushScheduled = true;
|
|
177
|
+
queueMicrotask(()=>this.#flush());
|
|
178
|
+
}
|
|
179
|
+
this.#inFlight.set(key, promise);
|
|
180
|
+
void promise.finally(()=>{
|
|
181
|
+
this.#inFlight.delete(key);
|
|
182
|
+
}).catch(()=>{});
|
|
183
|
+
return promise;
|
|
184
|
+
}
|
|
185
|
+
#flush() {
|
|
186
|
+
this.#flushScheduled = false;
|
|
187
|
+
const batch = this.#pending.splice(0);
|
|
188
|
+
void (async ()=>{
|
|
189
|
+
for(let offset = 0; offset < batch.length; offset += 64){
|
|
190
|
+
const chunk = batch.slice(offset, offset + 64);
|
|
191
|
+
const outcomes = await this.#registry.withReceiveHandle(this.#groupID, async (handle)=>{
|
|
192
|
+
const results = [];
|
|
193
|
+
const opened = openedFramesOf(this.#registry, this.#groupID);
|
|
194
|
+
for (const item of chunk){
|
|
195
|
+
const digest = toB64(blake3(item.ciphertext));
|
|
196
|
+
if (opened.has(digest)) {
|
|
197
|
+
results.push({
|
|
198
|
+
ok: false,
|
|
199
|
+
error: new DuplicateFrameError('frame already opened')
|
|
200
|
+
});
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
const epoch = readMessageEpoch(item.ciphertext);
|
|
205
|
+
const { payload, senderDID } = await handle.decrypt({
|
|
206
|
+
message: item.ciphertext
|
|
207
|
+
});
|
|
208
|
+
// Spent now, whatever the checks below decide: a redelivery cannot open again.
|
|
209
|
+
rememberOpened(opened, digest);
|
|
210
|
+
if (senderDID !== undefined && normalizeDID(senderDID) !== this.#peerDID) throw new Error('MLS sender does not match hub publisher');
|
|
211
|
+
if (handle.findMemberLeafIndex(this.#peerDID) === undefined) {
|
|
212
|
+
this.clear();
|
|
213
|
+
this.#closeForRemoval?.();
|
|
214
|
+
throw new Error('MLS peer is no longer a group member');
|
|
215
|
+
}
|
|
216
|
+
const current = epoch !== undefined && epoch === handle.epoch;
|
|
217
|
+
const frame = decodeFrame(payload);
|
|
218
|
+
if (senderDID === undefined || !current) {
|
|
219
|
+
if (this.#role === 'dialer' && frame.kind === 'session-end' && this.#outstanding.has(frame.sessionID)) {
|
|
220
|
+
this.#retireSession(frame.sessionID);
|
|
221
|
+
}
|
|
222
|
+
const rid = replyID(frame);
|
|
223
|
+
if (this.#role !== 'dialer' || rid === null || !this.#outstanding.get(frame.sessionID)?.has(rid)) throw new Error('MLS stale or nameless frame is not an outstanding reply');
|
|
224
|
+
}
|
|
225
|
+
if (this.#role === 'dialer' && frame.kind === 'session-end') this.#retireSession(frame.sessionID);
|
|
226
|
+
const rid = replyID(frame);
|
|
227
|
+
if (this.#role === 'dialer' && rid !== null && frame.kind === 'message' && (frame.body.payload.typ === 'result' || frame.body.payload.typ === 'error')) {
|
|
228
|
+
this.#retireRequest(frame.sessionID, rid);
|
|
229
|
+
}
|
|
230
|
+
results.push({
|
|
231
|
+
ok: true,
|
|
232
|
+
bytes: payload
|
|
233
|
+
});
|
|
234
|
+
} catch (error) {
|
|
235
|
+
results.push({
|
|
236
|
+
ok: false,
|
|
237
|
+
// Lost, not corrupt: the reader fails what depended on it rather than waiting.
|
|
238
|
+
error: isGenerationExpired(error) ? new ExpiredFrameError('frame key is no longer retained', {
|
|
239
|
+
cause: error
|
|
240
|
+
}) : error
|
|
241
|
+
});
|
|
242
|
+
this.#registry.reportDecryptFailure?.({
|
|
243
|
+
groupID: this.#groupID,
|
|
244
|
+
...epochOf(item.ciphertext),
|
|
245
|
+
error
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
result: results,
|
|
251
|
+
accepted: results.filter((outcome)=>outcome.ok).length
|
|
252
|
+
};
|
|
253
|
+
});
|
|
254
|
+
outcomes.forEach((outcome, index)=>{
|
|
255
|
+
const item = chunk[index];
|
|
256
|
+
if (outcome.ok) item.resolve(outcome.bytes);
|
|
257
|
+
else item.reject(outcome.error);
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
})().catch((error)=>{
|
|
261
|
+
for (const item of batch)item.reject(error);
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
// The sealed epoch for a decrypt-failure report, when the header is readable.
|
|
266
|
+
function epochOf(ciphertext) {
|
|
267
|
+
try {
|
|
268
|
+
const epoch = readMessageEpoch(ciphertext);
|
|
269
|
+
return epoch === undefined ? {} : {
|
|
270
|
+
epoch: Number(epoch)
|
|
271
|
+
};
|
|
272
|
+
} catch {
|
|
273
|
+
return {};
|
|
274
|
+
}
|
|
275
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { SerializedGroupState } from '@kumiai/mls';
|
|
2
|
+
/** The row a compare-and-swap write expects to replace. */
|
|
3
|
+
export type GroupStateVersion = {
|
|
4
|
+
epoch: number;
|
|
5
|
+
stateVersion: number;
|
|
6
|
+
};
|
|
7
|
+
export type StoredGroupState = {
|
|
8
|
+
deleted: false;
|
|
9
|
+
serialized: SerializedGroupState;
|
|
10
|
+
/** Orders secret-tree mutations within one epoch. */
|
|
11
|
+
stateVersion: number;
|
|
12
|
+
/** Highest sender generation reserved in this epoch; 0 after an epoch change. */
|
|
13
|
+
senderGenerationHWM: number;
|
|
14
|
+
} | {
|
|
15
|
+
deleted: true;
|
|
16
|
+
epoch: number;
|
|
17
|
+
stateVersion: number;
|
|
18
|
+
};
|
|
19
|
+
export type SaveGroupStateParams = {
|
|
20
|
+
groupID: string;
|
|
21
|
+
serialized: SerializedGroupState;
|
|
22
|
+
stateVersion: number;
|
|
23
|
+
/** `null` inserts only when no row exists, a tombstone included. */
|
|
24
|
+
expected: GroupStateVersion | null;
|
|
25
|
+
};
|
|
26
|
+
export type DeleteGroupStateParams = {
|
|
27
|
+
groupID: string;
|
|
28
|
+
expected: GroupStateVersion;
|
|
29
|
+
};
|
|
30
|
+
export type ReserveSenderGenerationsParams = {
|
|
31
|
+
groupID: string;
|
|
32
|
+
epoch: number;
|
|
33
|
+
/** The sender generation encryption has reached. */
|
|
34
|
+
current: number;
|
|
35
|
+
/** How many generations past `current` to reserve. */
|
|
36
|
+
size: number;
|
|
37
|
+
/** The high-water mark the caller last saw; a different stored mark returns `null`. */
|
|
38
|
+
expectedHWM: number;
|
|
39
|
+
/**
|
|
40
|
+
* Stored in place, without moving the version, when the row is still at `stateVersion` and
|
|
41
|
+
* this reservation is still the latest. Its sender generation must not exceed `current`.
|
|
42
|
+
*/
|
|
43
|
+
senderState?: {
|
|
44
|
+
mlsState: Uint8Array;
|
|
45
|
+
stateVersion: number;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* One device's persisted MLS group state, one row per group. The host binds the device and the
|
|
50
|
+
* clock. Writes are compare-and-swap: a `false` answer means another writer won.
|
|
51
|
+
*
|
|
52
|
+
* The port owns no transaction. A host may bind an instance to one of its own, so the row write
|
|
53
|
+
* commits or rolls back with the host's other writes.
|
|
54
|
+
*/
|
|
55
|
+
export type GroupStateStore = {
|
|
56
|
+
/** Tombstones are hidden unless `includeDeleted` is set. */
|
|
57
|
+
get(groupID: string, options?: {
|
|
58
|
+
includeDeleted?: boolean;
|
|
59
|
+
}): Promise<StoredGroupState | undefined>;
|
|
60
|
+
/**
|
|
61
|
+
* With `expected`, replaces the row at exactly that epoch and version, a tombstone included.
|
|
62
|
+
* Returns false when the row moved or `serialized.epoch` is below `expected.epoch`, and throws
|
|
63
|
+
* when `stateVersion` does not advance past `expected.stateVersion`.
|
|
64
|
+
*/
|
|
65
|
+
save(params: SaveGroupStateParams): Promise<boolean>;
|
|
66
|
+
/** Tombstones the row at exactly `expected`, advancing its version by one. */
|
|
67
|
+
delete(params: DeleteGroupStateParams): Promise<boolean>;
|
|
68
|
+
/** Groups with a live row; tombstones excluded. */
|
|
69
|
+
listGroupIDs(): Promise<Array<string>>;
|
|
70
|
+
/**
|
|
71
|
+
* Raises the sender high-water mark to at least `current + size` and returns it, or `null`
|
|
72
|
+
* when the stored mark is not `expectedHWM`. Throws when no live row is at `epoch`.
|
|
73
|
+
*/
|
|
74
|
+
reserveSenderGenerations(params: ReserveSenderGenerationsParams): Promise<number | null>;
|
|
75
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One device's persisted MLS group state, one row per group. The host binds the device and the
|
|
3
|
+
* clock. Writes are compare-and-swap: a `false` answer means another writer won.
|
|
4
|
+
*
|
|
5
|
+
* The port owns no transaction. A host may bind an instance to one of its own, so the row write
|
|
6
|
+
* commits or rolls back with the host's other writes.
|
|
7
|
+
*/ export { };
|
package/lib/index.d.ts
CHANGED
|
@@ -23,6 +23,8 @@ export { guardedReplace, type HandleAccess, retainsKnownEffects, type SimpleHand
|
|
|
23
23
|
export { type ApplyCommitParams, type ApplyCommitResult, applyCommit } from './apply-commit.js';
|
|
24
24
|
export type { DeriveEntryKeyParams, OpenEntriesParams, SealEntriesParams } from './crypto.js';
|
|
25
25
|
export { createGroupCrypto, deriveEntryKey, ENTRY_SEAL_LABEL, type GroupCryptoParams, openEntries, sealEntries, } from './crypto.js';
|
|
26
|
+
export { type DecryptFailure, MLSEncryptor, type MLSEncryptorParams, type MLSEncryptorRegistry, } from './encryptor.js';
|
|
27
|
+
export type { DeleteGroupStateParams, GroupStateStore, GroupStateVersion, ReserveSenderGenerationsParams, SaveGroupStateParams, StoredGroupState, } from './group-state-store.js';
|
|
26
28
|
export type { RecoveryPendingPutParams } from './mls.js';
|
|
27
29
|
export { createGroupMLS, createLedgerEntrySlot, createRecoveryPending, deriveRecoverySecret, type GroupMLSParams, type LedgerEntrySlot, RECOVERY_LABEL, type RecoveryPending, } from './mls.js';
|
|
28
30
|
export { type PublishRevokeProofParams, type PublishRevokeResult, publishRevokeProof, type RevokeJournalOptions, } from './revoke.js';
|
package/lib/index.js
CHANGED
|
@@ -20,5 +20,6 @@
|
|
|
20
20
|
*/ export { guardedReplace, retainsKnownEffects, simpleHandleAccess } from './access.js';
|
|
21
21
|
export { applyCommit } from './apply-commit.js';
|
|
22
22
|
export { createGroupCrypto, deriveEntryKey, ENTRY_SEAL_LABEL, openEntries, sealEntries } from './crypto.js';
|
|
23
|
+
export { MLSEncryptor } from './encryptor.js';
|
|
23
24
|
export { createGroupMLS, createLedgerEntrySlot, createRecoveryPending, deriveRecoverySecret, RECOVERY_LABEL } from './mls.js';
|
|
24
25
|
export { publishRevokeProof } from './revoke.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kumiai/mls-rpc",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.5",
|
|
4
4
|
"description": "The @kumiai/rpc consumer ports (GroupCrypto, GroupMLS) implemented over @kumiai/mls",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mls",
|
|
@@ -25,14 +25,21 @@
|
|
|
25
25
|
"lib/*"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@kokuin/token": "^0.
|
|
29
|
-
"@kumiai/
|
|
30
|
-
"@kumiai/
|
|
28
|
+
"@kokuin/token": "^0.8.1",
|
|
29
|
+
"@kumiai/hub-tunnel": "^0.13.4",
|
|
30
|
+
"@kumiai/mls": "^0.13.7",
|
|
31
|
+
"@kumiai/rpc": "^0.13.4",
|
|
31
32
|
"@noble/ciphers": "^2.4.0",
|
|
33
|
+
"@noble/hashes": "^2.4.0",
|
|
34
|
+
"@sozai/async": "^0.4.6",
|
|
35
|
+
"@sozai/codec": "^0.5.0",
|
|
32
36
|
"@sozai/runtime": "^0.1.1"
|
|
33
37
|
},
|
|
34
38
|
"devDependencies": {
|
|
35
|
-
"@kumiai/
|
|
39
|
+
"@kumiai/hub-conformance": "^0.13.1",
|
|
40
|
+
"@kumiai/hub-protocol": "^0.13.3",
|
|
41
|
+
"@kumiai/rpc-conformance": "^0.13.1",
|
|
42
|
+
"@sozai/bytes": "^0.1.0",
|
|
36
43
|
"ts-mls": "2.0.0-rc.13",
|
|
37
44
|
"vitest": "^5.1.2"
|
|
38
45
|
},
|