@kumiai/mls-rpc 0.9.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/README.md +146 -24
- package/lib/access.d.ts +20 -0
- package/lib/access.js +49 -0
- package/lib/apply-commit.d.ts +38 -0
- package/lib/apply-commit.js +109 -0
- package/lib/crypto.d.ts +19 -11
- package/lib/crypto.js +137 -50
- package/lib/index.d.ts +5 -2
- package/lib/index.js +5 -2
- package/lib/mls.d.ts +20 -9
- package/lib/mls.js +265 -87
- package/lib/recovery.d.ts +46 -0
- package/lib/recovery.js +181 -0
- package/lib/revoke.d.ts +19 -0
- package/lib/revoke.js +45 -0
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -17,21 +17,102 @@ depend on both.
|
|
|
17
17
|
|
|
18
18
|
## Exports
|
|
19
19
|
|
|
20
|
-
- `
|
|
21
|
-
|
|
20
|
+
- `simpleHandleAccess({ handle, adopt, persist? })` — serialised access to a live handle; the
|
|
21
|
+
`HandleAccess` type for a host that brings its own.
|
|
22
|
+
- `createGroupCrypto({ access, entryLabel?, runtime?, pending? })` — `GroupCrypto` over that access.
|
|
23
|
+
- `createGroupMLS({ access, identity, entrySlot, recoverySecret? })` — `GroupMLS` over the same
|
|
24
|
+
access.
|
|
25
|
+
- `applyCommit({ handle, commit, entrySlot, ownDID, persist?, ... })` — apply a received Commit inside the host's own
|
|
26
|
+
transaction (see below).
|
|
22
27
|
- `createLedgerEntrySlot()` — the per-commit ledger-entry resolver seam (see below).
|
|
23
|
-
- `
|
|
28
|
+
- `deriveEntryKey`, `sealEntries`, `openEntries`, `ENTRY_SEAL_LABEL` — the ledger-entry seal.
|
|
29
|
+
- `createRecoveryPending`, `deriveRecoverySecret`, `RECOVERY_LABEL` — recovery request keys and the
|
|
30
|
+
default recovery secret.
|
|
24
31
|
|
|
25
|
-
##
|
|
32
|
+
## Share one access instance
|
|
26
33
|
|
|
27
|
-
Both factories take
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
Both factories take the same `access` instance. The adapter reads the current handle through
|
|
35
|
+
`handle: () => GroupHandle`, because authored commits and recovery replace it wholesale. A host
|
|
36
|
+
adopts an authored commit through `access.replace(next)` after acceptance. The adapter saves before
|
|
37
|
+
publishing the replacement and publishes its synchronous epoch hint after a successful save.
|
|
31
38
|
|
|
32
39
|
For a *received* commit there is nothing to adopt: ts-mls's `processMessage` advances the handle in
|
|
33
40
|
place. A host that treated every commit as adopt-later would double-apply received ones.
|
|
34
41
|
|
|
42
|
+
Migrating from 0.9: build one adapter from the old `handle` / `adopt` / `persist` parameters and pass
|
|
43
|
+
it to both factories in their place.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
const access = simpleHandleAccess({ handle: () => handle, adopt, persist })
|
|
47
|
+
const crypto = createGroupCrypto({ access, pending })
|
|
48
|
+
const mls = createGroupMLS({ access, identity, entrySlot })
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### A host's own `HandleAccess`
|
|
52
|
+
|
|
53
|
+
A host with its own handle store implements the port instead:
|
|
54
|
+
|
|
55
|
+
- `read(fn)` lends the handle for the callback only. `mutate(fn)` serialises with every other
|
|
56
|
+
operation and resolves after the state is stored; `fn`'s `persist` argument goes to
|
|
57
|
+
`processMessage` and `bootstrapLedger`. `replace(next)` stores before publishing. A transactional
|
|
58
|
+
host may restore a fresh handle per mutation, discard it on rollback, and publish on commit.
|
|
59
|
+
- `epoch()` is a synchronous hint. Publish it only after the outermost successful commit, and restore
|
|
60
|
+
it on rollback. No port decision reads it: every one uses an epoch read under the lock.
|
|
61
|
+
- `open(fn, persistOpened)` is the durable open. `fn` stages the open and hands its state and record
|
|
62
|
+
to the callback it is given. A host may capture them, release its lock, and then call
|
|
63
|
+
`persistOpened` to write both in one transaction guarded by the state row's revision, publishing
|
|
64
|
+
the working handle only after that commits, and only if no newer write was published meanwhile.
|
|
65
|
+
On a revision conflict it retries from a fresh handle. Faults from `persistOpened`, from the staging
|
|
66
|
+
callback the adapter hands `fn`, and from anything the adapter raises around `fn` come back as
|
|
67
|
+
`AppFrameStorageError`, which the lane retries.
|
|
68
|
+
|
|
69
|
+
## Durable logged app delivery
|
|
70
|
+
|
|
71
|
+
Pass a `pending` adapter to `createGroupCrypto` to opt in. Its `persistOpened(stagedState, record)`
|
|
72
|
+
must write the encoded post-open `ClientState` as the group's handle state **and** insert the
|
|
73
|
+
`PendingAppFrame` in one atomic transaction. `list()` returns all uncompleted records after a
|
|
74
|
+
restart, ordered by `(frame.segment, frame.position)` by this port. `complete(id)` clears one
|
|
75
|
+
record and resolves for an unknown id. Deduplicate inserts by `record.frame.id`.
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
const access = simpleHandleAccess({
|
|
79
|
+
handle: () => handle,
|
|
80
|
+
adopt: (next) => { handle = next },
|
|
81
|
+
persist: (current) => store.save(current),
|
|
82
|
+
})
|
|
83
|
+
const crypto = createGroupCrypto({
|
|
84
|
+
access,
|
|
85
|
+
pending: {
|
|
86
|
+
persistOpened: async (stagedState, record) => {
|
|
87
|
+
await store.transaction(async (tx) => {
|
|
88
|
+
await tx.saveHandleState(groupID, stagedState)
|
|
89
|
+
await tx.insertPendingIfAbsent(record.frame.id, record)
|
|
90
|
+
})
|
|
91
|
+
},
|
|
92
|
+
list: () => store.listPending(groupID),
|
|
93
|
+
complete: (id) => store.completePending(id),
|
|
94
|
+
},
|
|
95
|
+
})
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The example's `saveHandleState` must also enforce the host's save ordering and same-epoch
|
|
99
|
+
monotonic version rule. Saves issued before `persistOpened` must finish before it; a delayed
|
|
100
|
+
older state at the same epoch must be rejected. `persistOpened` runs under the handle mutex and
|
|
101
|
+
the peer's commit mutex: it must not call either object. Never hold a transaction or the sole
|
|
102
|
+
database connection while awaiting a peer or handle call; a handler should commit its own
|
|
103
|
+
transaction before awaiting `commit()` or `dispatch()`. A single-connection host that reverses
|
|
104
|
+
this order can deadlock. Hosts adopting this in Kubun must first remove transaction-then-registry
|
|
105
|
+
waits on paths overlapping app opens and prove the lock order against SQLite.
|
|
106
|
+
|
|
107
|
+
With `pending`, `unwrap(bytes, { expectedAAD, frame })` calls `GroupHandle.decryptStaged` and
|
|
108
|
+
resolves only after the atomic write. A persistence failure becomes `AppFrameStorageError` for
|
|
109
|
+
the RPC lane to retry; an unopenable frame is classified dead. Without `pending`, the adapter saves
|
|
110
|
+
the post-open ratchet state without a pending record, so a crash after `unwrap` and before the
|
|
111
|
+
handler finishes loses that frame: `pending` is the at-least-once path. `frameAAD(bytes)` exposes
|
|
112
|
+
the cleartext AAD as a routing hint. The full `expectedAAD` on open authenticates it.
|
|
113
|
+
The 0.10 app AAD carries version and log intent. Older bare-topic app frames cannot interoperate
|
|
114
|
+
with 0.10 peers. See the `@kumiai/rpc` README for the delivery and retention boundary.
|
|
115
|
+
|
|
35
116
|
## `createLedgerEntrySlot` is mandatory, and must be installed where the handle is built
|
|
36
117
|
|
|
37
118
|
`GroupMLS.processCommit` is handed a `resolveLedgerEntries` scoped to **one** commit's frame — the
|
|
@@ -40,7 +121,7 @@ once, in `GroupOptions`, and offers no way to change it afterwards. So the indir
|
|
|
40
121
|
installed when the group is *built*:
|
|
41
122
|
|
|
42
123
|
```ts
|
|
43
|
-
import { createGroupCrypto, createGroupMLS, createLedgerEntrySlot } from '@kumiai/mls-rpc'
|
|
124
|
+
import { createGroupCrypto, createGroupMLS, createLedgerEntrySlot, simpleHandleAccess } from '@kumiai/mls-rpc'
|
|
44
125
|
|
|
45
126
|
const entrySlot = createLedgerEntrySlot()
|
|
46
127
|
// Every construction site: createGroup / processWelcome / restoreGroup.
|
|
@@ -49,21 +130,65 @@ const { group } = await createGroup(identity, groupID, {
|
|
|
49
130
|
})
|
|
50
131
|
|
|
51
132
|
let handle = group
|
|
52
|
-
const
|
|
53
|
-
const mls = createGroupMLS({
|
|
133
|
+
const access = simpleHandleAccess({
|
|
54
134
|
handle: () => handle,
|
|
55
|
-
adopt: (next) => {
|
|
56
|
-
|
|
57
|
-
|
|
135
|
+
adopt: (next) => { handle = next },
|
|
136
|
+
persist: (current) => store.save(current),
|
|
137
|
+
})
|
|
138
|
+
const crypto = createGroupCrypto({ access })
|
|
139
|
+
const mls = createGroupMLS({
|
|
140
|
+
access,
|
|
58
141
|
identity,
|
|
59
142
|
entrySlot,
|
|
60
|
-
persist: async (current) => await store.save(current),
|
|
61
143
|
})
|
|
62
144
|
```
|
|
63
145
|
|
|
64
146
|
Passing anything else means a commit resolves its entries against whatever resolver the handle
|
|
65
147
|
happened to be born with.
|
|
66
148
|
|
|
149
|
+
The optional adapter `persist` callback receives the tentative state after an accepted commit,
|
|
150
|
+
the bootstrapped handle, or an ordinary ratchet operation. Writes must
|
|
151
|
+
be atomic: rejection must leave storage unchanged, since the in-memory handle rolls back and no
|
|
152
|
+
control notifications fire. Received-message and bootstrap persist runs under the handle mutex;
|
|
153
|
+
it must not call back into that handle. Recovery persists the rejoined handle before adoption. If
|
|
154
|
+
adoption throws, storage already holds the new handle and a restart loads it.
|
|
155
|
+
|
|
156
|
+
## Applying a commit inside a host transaction
|
|
157
|
+
|
|
158
|
+
`processCommit` is `applyCommit` inside `access.mutate`, plus entry resolution before the lock. A host
|
|
159
|
+
that projects commits into its own tables calls `applyCommit` inside its own transaction instead, from
|
|
160
|
+
its own `GroupMLS.processCommit`, so the peer still sees each result and rotates its anchor:
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
const result = await applyCommit({
|
|
164
|
+
handle,
|
|
165
|
+
commit,
|
|
166
|
+
senderDID,
|
|
167
|
+
resolveLedgerEntries: (ids) => resolved(ids), // must not take the handle lock
|
|
168
|
+
entrySlot,
|
|
169
|
+
ownDID: identity.id,
|
|
170
|
+
persist,
|
|
171
|
+
})
|
|
172
|
+
if (result.applied) {
|
|
173
|
+
// write result.rosterAfter, result.surfacedEntries, ... and commit
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
It refuses non-Commit bytes, a frame at another epoch, and this member's own authenticated commit
|
|
178
|
+
before any resolver or mutation runs. `applied` means the handle took the commit's state; `advanced`
|
|
179
|
+
means the epoch moved. A commit removing this member is applied without advancing. The result
|
|
180
|
+
carries both rosters, the ledger length before, the committer, and the verified non-`kumiai.*`
|
|
181
|
+
entries the commit appended, repeats kept. `MissingLedgerEntriesError`, resolver faults and store
|
|
182
|
+
faults propagate: map them to a throw from `processCommit`, never to `advanced: false`, which the peer
|
|
183
|
+
steps over as a commit it will never apply.
|
|
184
|
+
|
|
185
|
+
## Recovery secret
|
|
186
|
+
|
|
187
|
+
`exportRecoverySecret` defaults to `deriveRecoverySecret`, the anchor KDF. A host whose groups
|
|
188
|
+
already rendezvous on another secret passes `recoverySecret: (handle) => ...` for every restore of
|
|
189
|
+
those groups: both the commit and the rendezvous topic follow it. A result under 16 bytes or a throw
|
|
190
|
+
is refused; the default is never tried as a fallback.
|
|
191
|
+
|
|
67
192
|
## Two seals, one exporter
|
|
68
193
|
|
|
69
194
|
`wrap`/`unwrap` carry app traffic and are ratchet-backed: each open consumes a message key and
|
|
@@ -83,15 +208,12 @@ leave a peer stepping over frames without ever classifying one and never learnin
|
|
|
83
208
|
Documented on the factories themselves; the conformance suite in `@kumiai/rpc-conformance` pins each
|
|
84
209
|
one. The ones a host is most likely to trip on:
|
|
85
210
|
|
|
86
|
-
- **`unwrap` opens
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
epoch's bytes computes every other's. This exports from the epoch's exporter secret, which a
|
|
93
|
-
removed member cannot reach forward from. That difference is the entire security property the
|
|
94
|
-
app-lane topic rests on, and it is real only here.
|
|
211
|
+
- **`unwrap` opens only at the current epoch.** A readable frame at any other epoch throws
|
|
212
|
+
`FrameEpochError` before decryption, under the handle lock. ts-mls could open a few past epochs,
|
|
213
|
+
but `GroupHandle.decrypt` resolves senders at the current epoch, so such a frame never had a
|
|
214
|
+
sender.
|
|
215
|
+
- **`exportSecret` derives from MLS exporter state.** The fake uses HMAC to model epoch separation,
|
|
216
|
+
while the real port derives from the handle's exporter secret.
|
|
95
217
|
- **`wrap` mutates.** It consumes a per-message ratchet key, so sealing the same plaintext twice
|
|
96
218
|
gives different bytes.
|
|
97
219
|
- **`exportRecoverySecret` is derived from the group's genesis anchor, which is public.** MLS has no
|
package/lib/access.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { GroupHandle } from '@kumiai/mls';
|
|
2
|
+
import type { PendingAppFrame, SendAdmission } from '@kumiai/rpc';
|
|
3
|
+
type PersistOpened = (stagedState: Uint8Array, record: PendingAppFrame) => Promise<void>;
|
|
4
|
+
export type HandleAccess = {
|
|
5
|
+
/** Published with admission after mutation/open and after replacement adoption resolves. */
|
|
6
|
+
epoch(): number;
|
|
7
|
+
/** Lock-free snapshot. During adoption it describes the epoch being left. */
|
|
8
|
+
admission(): SendAdmission;
|
|
9
|
+
read<TValue>(fn: (handle: GroupHandle) => TValue | Promise<TValue>): Promise<TValue>;
|
|
10
|
+
mutate<TValue>(fn: (handle: GroupHandle, persist: (handle: GroupHandle) => Promise<void>) => Promise<TValue>): Promise<TValue>;
|
|
11
|
+
replace(next: GroupHandle): Promise<void>;
|
|
12
|
+
open<TValue>(fn: (handle: GroupHandle, persistOpened: PersistOpened) => Promise<TValue>, persistOpened: PersistOpened): Promise<TValue>;
|
|
13
|
+
};
|
|
14
|
+
export type SimpleHandleAccessParams = {
|
|
15
|
+
handle: () => GroupHandle;
|
|
16
|
+
adopt: (next: GroupHandle) => void | Promise<void>;
|
|
17
|
+
persist?: (handle: GroupHandle) => void | Promise<void>;
|
|
18
|
+
};
|
|
19
|
+
export declare function simpleHandleAccess(params: SimpleHandleAccessParams): HandleAccess;
|
|
20
|
+
export {};
|
package/lib/access.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export function simpleHandleAccess(params) {
|
|
2
|
+
let publishedAdmission = params.handle().sendAdmission();
|
|
3
|
+
let tail = Promise.resolve();
|
|
4
|
+
const serialise = async (fn)=>{
|
|
5
|
+
const previous = tail;
|
|
6
|
+
let release = ()=>{};
|
|
7
|
+
tail = new Promise((resolve)=>{
|
|
8
|
+
release = resolve;
|
|
9
|
+
});
|
|
10
|
+
await previous;
|
|
11
|
+
try {
|
|
12
|
+
return await fn();
|
|
13
|
+
} finally{
|
|
14
|
+
release();
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const save = async (handle)=>{
|
|
18
|
+
await params.persist?.(handle);
|
|
19
|
+
};
|
|
20
|
+
return {
|
|
21
|
+
epoch: ()=>publishedAdmission.epoch,
|
|
22
|
+
admission: ()=>publishedAdmission,
|
|
23
|
+
read: (fn)=>serialise(async ()=>await fn(params.handle())),
|
|
24
|
+
mutate: (fn)=>serialise(async ()=>{
|
|
25
|
+
const handle = params.handle();
|
|
26
|
+
let saved = false;
|
|
27
|
+
const persist = async (current)=>{
|
|
28
|
+
await save(current);
|
|
29
|
+
saved = true;
|
|
30
|
+
};
|
|
31
|
+
const result = await fn(handle, persist);
|
|
32
|
+
if (!saved) await save(handle);
|
|
33
|
+
publishedAdmission = handle.sendAdmission();
|
|
34
|
+
return result;
|
|
35
|
+
}),
|
|
36
|
+
replace: (next)=>serialise(async ()=>{
|
|
37
|
+
await save(next);
|
|
38
|
+
await params.adopt(next);
|
|
39
|
+
publishedAdmission = next.sendAdmission();
|
|
40
|
+
next.confirmAdopted();
|
|
41
|
+
}),
|
|
42
|
+
open: (fn, persistOpened)=>serialise(async ()=>{
|
|
43
|
+
const handle = params.handle();
|
|
44
|
+
const result = await fn(handle, persistOpened);
|
|
45
|
+
publishedAdmission = handle.sendAdmission();
|
|
46
|
+
return result;
|
|
47
|
+
})
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type GroupHandle, type VerifiedLedgerEntry } from '@kumiai/mls';
|
|
2
|
+
import type { CommitContext, RecoveryRefusalReason, RosterEntry } from '@kumiai/rpc';
|
|
3
|
+
import type { LedgerEntrySlot } from './mls.js';
|
|
4
|
+
export type ApplyCommitParams = CommitContext & {
|
|
5
|
+
handle: GroupHandle;
|
|
6
|
+
commit: Uint8Array;
|
|
7
|
+
entrySlot: LedgerEntrySlot;
|
|
8
|
+
/** This member's DID: its own authenticated commit is never applied as received. */
|
|
9
|
+
ownDID: string;
|
|
10
|
+
/** Runs before the handle adopts the new state. */
|
|
11
|
+
persist?: ((handle: GroupHandle) => Promise<void>) | undefined;
|
|
12
|
+
};
|
|
13
|
+
export type ApplyCommitResult = {
|
|
14
|
+
refusal?: RecoveryRefusalReason;
|
|
15
|
+
/** The handle took the commit's state. A host keeps it even when `advanced` is false. */
|
|
16
|
+
applied: boolean;
|
|
17
|
+
/** The epoch moved. A commit removing this member is applied without advancing it. */
|
|
18
|
+
advanced: boolean;
|
|
19
|
+
epochBefore: number;
|
|
20
|
+
epochAfter: number;
|
|
21
|
+
rosterBefore: Array<RosterEntry>;
|
|
22
|
+
rosterAfter: Array<RosterEntry>;
|
|
23
|
+
/** Verified non-control entries the commit appended, in log order, repeats kept. */
|
|
24
|
+
surfacedEntries: Array<VerifiedLedgerEntry>;
|
|
25
|
+
ledgerLengthBefore: number;
|
|
26
|
+
committerDID?: string;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Apply a received Commit to `handle` in place, for a caller that already holds the handle's
|
|
30
|
+
* lock and owns the surrounding transaction. `resolveLedgerEntries` runs inside that lock
|
|
31
|
+
* and must not take it again.
|
|
32
|
+
*
|
|
33
|
+
* `persist` runs before the handle adopts the new state; its failure propagates and leaves the
|
|
34
|
+
* handle where it was. `MissingLedgerEntriesError` and resolver faults propagate for the caller to
|
|
35
|
+
* classify. Any other refusal returns `applied: false`, and a throw after the handle took the
|
|
36
|
+
* commit (a callback after durable acceptance) reports it applied rather than inviting a replay.
|
|
37
|
+
*/
|
|
38
|
+
export declare function applyCommit(params: ApplyCommitParams): Promise<ApplyCommitResult>;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { normalizeDID } from '@kokuin/token';
|
|
2
|
+
import { CommitRejectedError, MissingLedgerEntriesError, readMessageEpoch } from '@kumiai/mls';
|
|
3
|
+
/** Reserved by `@kumiai/mls` for its own control entries; never surfaced to a host. */ const CONTROL_TYPE_PREFIX = 'kumiai.';
|
|
4
|
+
function rosterOf(handle) {
|
|
5
|
+
return handle.listMembers().map((member)=>({
|
|
6
|
+
did: member.id,
|
|
7
|
+
leafIndex: member.leafIndex,
|
|
8
|
+
longForm: member.longForm
|
|
9
|
+
}));
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Apply a received Commit to `handle` in place, for a caller that already holds the handle's
|
|
13
|
+
* lock and owns the surrounding transaction. `resolveLedgerEntries` runs inside that lock
|
|
14
|
+
* and must not take it again.
|
|
15
|
+
*
|
|
16
|
+
* `persist` runs before the handle adopts the new state; its failure propagates and leaves the
|
|
17
|
+
* handle where it was. `MissingLedgerEntriesError` and resolver faults propagate for the caller to
|
|
18
|
+
* classify. Any other refusal returns `applied: false`, and a throw after the handle took the
|
|
19
|
+
* commit (a callback after durable acceptance) reports it applied rather than inviting a replay.
|
|
20
|
+
*/ export async function applyCommit(params) {
|
|
21
|
+
const { handle, commit, entrySlot, ownDID, persist } = params;
|
|
22
|
+
const before = handle.epoch;
|
|
23
|
+
const rosterBefore = rosterOf(handle);
|
|
24
|
+
const ledgerLengthBefore = handle.ledger.length;
|
|
25
|
+
const refused = (committerDID, refusal = 'invalid')=>({
|
|
26
|
+
refusal,
|
|
27
|
+
applied: false,
|
|
28
|
+
advanced: false,
|
|
29
|
+
epochBefore: Number(before),
|
|
30
|
+
epochAfter: Number(before),
|
|
31
|
+
rosterBefore,
|
|
32
|
+
rosterAfter: rosterBefore,
|
|
33
|
+
surfacedEntries: [],
|
|
34
|
+
ledgerLengthBefore,
|
|
35
|
+
...committerDID != null && {
|
|
36
|
+
committerDID
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
const surfaced = ()=>handle.ledger.slice(ledgerLengthBefore).map((entry)=>entry.verified).filter((verified)=>!verified.entry.type.startsWith(CONTROL_TYPE_PREFIX));
|
|
40
|
+
const header = await handle.readCommitHeader(commit);
|
|
41
|
+
if (header == null || readMessageEpoch(commit) !== before) return refused();
|
|
42
|
+
const committerDID = header.committerDID;
|
|
43
|
+
if (committerDID != null && normalizeDID(committerDID) === normalizeDID(ownDID)) {
|
|
44
|
+
return refused(committerDID);
|
|
45
|
+
}
|
|
46
|
+
let persistFailed = false;
|
|
47
|
+
let persisted = false;
|
|
48
|
+
let resolverFailed = false;
|
|
49
|
+
let threw = false;
|
|
50
|
+
let refusal = 'invalid';
|
|
51
|
+
const resolve = params.resolveLedgerEntries;
|
|
52
|
+
entrySlot.install(resolve && (async (ids)=>{
|
|
53
|
+
try {
|
|
54
|
+
return await resolve(ids);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
// A fault fetching bodies says nothing about the commit, which may apply on retry.
|
|
57
|
+
resolverFailed = true;
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}));
|
|
61
|
+
try {
|
|
62
|
+
await handle.processMessage(commit, {
|
|
63
|
+
...persist != null && {
|
|
64
|
+
persist: async (current)=>{
|
|
65
|
+
try {
|
|
66
|
+
await persist(current);
|
|
67
|
+
persisted = true;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
persistFailed = true;
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (error instanceof MissingLedgerEntriesError || persistFailed || resolverFailed) throw error;
|
|
77
|
+
threw = true;
|
|
78
|
+
if (error instanceof CommitRejectedError) refusal = error.reason;
|
|
79
|
+
} finally{
|
|
80
|
+
entrySlot.install(undefined);
|
|
81
|
+
}
|
|
82
|
+
if (handle.epoch === before) {
|
|
83
|
+
// A commit removing this member changes the tree without ratcheting the handle, and a host
|
|
84
|
+
// callback may throw after that state was stored.
|
|
85
|
+
const rosterAfter = rosterOf(handle);
|
|
86
|
+
const changed = rosterAfter.length !== rosterBefore.length || rosterAfter.some((entry, index)=>entry.did !== rosterBefore[index]?.did);
|
|
87
|
+
if (threw && !persisted && !changed) return refused(committerDID, refusal);
|
|
88
|
+
const { refusal: _refusal, ...unchanged } = refused(committerDID);
|
|
89
|
+
return {
|
|
90
|
+
...unchanged,
|
|
91
|
+
applied: true,
|
|
92
|
+
rosterAfter,
|
|
93
|
+
surfacedEntries: surfaced()
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
applied: true,
|
|
98
|
+
advanced: true,
|
|
99
|
+
epochBefore: Number(before),
|
|
100
|
+
epochAfter: Number(handle.epoch),
|
|
101
|
+
rosterBefore,
|
|
102
|
+
rosterAfter: rosterOf(handle),
|
|
103
|
+
surfacedEntries: surfaced(),
|
|
104
|
+
ledgerLengthBefore,
|
|
105
|
+
...committerDID != null && {
|
|
106
|
+
committerDID
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
package/lib/crypto.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
1
|
+
import { type GroupHandle } from '@kumiai/mls';
|
|
2
|
+
import { type GroupCrypto, type PendingAppFrame, type PendingAppFrames } from '@kumiai/rpc';
|
|
3
3
|
import { type Runtime } from '@sozai/runtime';
|
|
4
|
+
import type { HandleAccess } from './access.js';
|
|
4
5
|
/**
|
|
5
6
|
* Label the ledger-entry seal key is exported under. Distinct from any label a caller passes to
|
|
6
7
|
* `exportSecret`: that names a topic secret handed to anything that derives one, while this key
|
|
@@ -9,12 +10,11 @@ import { type Runtime } from '@sozai/runtime';
|
|
|
9
10
|
* from a caller (see below).
|
|
10
11
|
*/
|
|
11
12
|
export declare const ENTRY_SEAL_LABEL = "kumiai/ledger-entries/v1";
|
|
13
|
+
export declare function deriveEntryKey(handle: GroupHandle, label?: string): Promise<Uint8Array>;
|
|
14
|
+
export declare function sealEntries(key: Uint8Array, entries: Uint8Array, runtime?: Runtime): Uint8Array;
|
|
15
|
+
export declare function openEntries(key: Uint8Array, sealed: Uint8Array): Uint8Array;
|
|
12
16
|
export type GroupCryptoParams = {
|
|
13
|
-
|
|
14
|
-
* The peer's current handle. A function, not a value: the handle is replaced when the peer
|
|
15
|
-
* adopts its own commit, and closing over a fixed handle would silently seal at a dead epoch.
|
|
16
|
-
*/
|
|
17
|
-
handle: () => GroupHandle;
|
|
17
|
+
access: HandleAccess;
|
|
18
18
|
/**
|
|
19
19
|
* Override the ledger-entry seal's exporter label. Members must agree on it, or they can't
|
|
20
20
|
* apply each other's commits.
|
|
@@ -22,16 +22,19 @@ export type GroupCryptoParams = {
|
|
|
22
22
|
entryLabel?: string;
|
|
23
23
|
/** Runtime providing platform primitives. Defaults to `createRuntime()`. */
|
|
24
24
|
runtime?: Runtime;
|
|
25
|
+
/** Atomic host store for post-open handle state and the pending record. */
|
|
26
|
+
pending?: {
|
|
27
|
+
persistOpened(stagedState: Uint8Array, record: PendingAppFrame): Promise<void>;
|
|
28
|
+
list(): Promise<Array<PendingAppFrame>>;
|
|
29
|
+
complete(id: string): Promise<void>;
|
|
30
|
+
};
|
|
25
31
|
};
|
|
26
32
|
/**
|
|
27
33
|
* {@link GroupCrypto} over a live {@link GroupHandle} — the real port, against real MLS.
|
|
28
34
|
*
|
|
29
35
|
* ## Where this diverges from the fake in `@kumiai/rpc`'s test fixtures
|
|
30
36
|
*
|
|
31
|
-
* 1. `unwrap`
|
|
32
|
-
* via ts-mls's retained key material (the fake refuses everything but its current epoch, so
|
|
33
|
-
* the fake is stricter — both are valid implementations of the port; group-rpc must not
|
|
34
|
-
* depend on the window, which is spent by epoch transitions, not time).
|
|
37
|
+
* 1. `unwrap` gates the frame epoch before ts-mls's bounded past-window decrypt.
|
|
35
38
|
*
|
|
36
39
|
* 2. `exportSecret` is one-way; the fake's is not. The fake XORs epoch and label into a fixed
|
|
37
40
|
* base, so one epoch's bytes yield every other epoch's for that label. This exports from the
|
|
@@ -46,4 +49,9 @@ export type GroupCryptoParams = {
|
|
|
46
49
|
* a sealed app frame and a commit — returning `null` (never throwing) for anything ts-mls
|
|
47
50
|
* won't decode. The fake answers only for its own two encodings.
|
|
48
51
|
*/
|
|
52
|
+
export declare function createGroupCrypto(params: GroupCryptoParams & {
|
|
53
|
+
pending: NonNullable<GroupCryptoParams['pending']>;
|
|
54
|
+
}): GroupCrypto & {
|
|
55
|
+
pending: PendingAppFrames;
|
|
56
|
+
};
|
|
49
57
|
export declare function createGroupCrypto(params: GroupCryptoParams): GroupCrypto;
|