@kumiai/rpc 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/classify.d.ts +132 -0
- package/lib/classify.js +48 -0
- package/lib/commit-frame.d.ts +32 -0
- package/lib/commit-frame.js +43 -0
- package/lib/commit.d.ts +188 -0
- package/lib/commit.js +50 -0
- package/lib/crypto.d.ts +205 -32
- package/lib/crypto.js +16 -8
- package/lib/cursor.d.ts +33 -0
- package/lib/cursor.js +26 -0
- package/lib/directed-crypto.d.ts +17 -0
- package/lib/directed-crypto.js +80 -0
- package/lib/directed.d.ts +11 -4
- package/lib/directed.js +168 -29
- package/lib/handshake.d.ts +13 -6
- package/lib/handshake.js +12 -7
- package/lib/hub-mux.d.ts +51 -12
- package/lib/hub-mux.js +66 -26
- package/lib/index.d.ts +9 -5
- package/lib/index.js +8 -3
- package/lib/ledger-entries.d.ts +22 -0
- package/lib/ledger-entries.js +78 -0
- package/lib/peer.d.ts +105 -15
- package/lib/peer.js +1009 -100
- package/lib/recovery.d.ts +27 -7
- package/lib/recovery.js +73 -18
- package/lib/topic.d.ts +18 -7
- package/lib/topic.js +18 -7
- package/package.json +11 -10
- package/lib/memory-group-mls.d.ts +0 -21
- package/lib/memory-group-mls.js +0 -55
package/lib/crypto.d.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import type { ByteTransform, Unwrap } from '@kumiai/broadcast';
|
|
2
2
|
/**
|
|
3
|
-
* Consumer-supplied MLS crypto port
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* never imports MLS.
|
|
3
|
+
* Consumer-supplied MLS crypto port: epoch number, an epoch-bound topic-derivation secret,
|
|
4
|
+
* byte-level encrypt (`wrap`) and decrypt-plus-recover-sender (`unwrap`). group-rpc never
|
|
5
|
+
* imports MLS.
|
|
7
6
|
*
|
|
8
|
-
* `epoch()` and `exportSecret()` are read on init and
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* sender (`senderDID`) recovered from the ciphertext.
|
|
7
|
+
* `epoch()` and `exportSecret()` are read on init and every {@link "peer".GroupPeer.resync}.
|
|
8
|
+
* `wrap`/`unwrap` close over the live group, so they always use current epoch state. `unwrap`
|
|
9
|
+
* returns the authenticated sender (`senderDID`) recovered from the ciphertext.
|
|
12
10
|
*/
|
|
13
11
|
export type GroupCrypto = {
|
|
14
12
|
epoch(): number;
|
|
@@ -16,45 +14,220 @@ export type GroupCrypto = {
|
|
|
16
14
|
wrap: ByteTransform;
|
|
17
15
|
unwrap: Unwrap;
|
|
18
16
|
};
|
|
17
|
+
/**
|
|
18
|
+
* What a Commit says about itself, readable WITHOUT applying it and without decrypting: the
|
|
19
|
+
* epoch it is framed at, and its author. Both classify a frame before the peer touches it, and
|
|
20
|
+
* both come from the commit's own bytes (authenticated by its signature), never from the
|
|
21
|
+
* frame's transport sender — that is the hub's word, and the hub is not trusted.
|
|
22
|
+
*/
|
|
23
|
+
export type CommitHeader = {
|
|
24
|
+
/** The epoch the Commit is framed at — the epoch every member that can apply it is at. */
|
|
25
|
+
epoch: number;
|
|
26
|
+
/** The MLS-authenticated author of the Commit. Not the publisher of the frame. */
|
|
27
|
+
committerDID: string;
|
|
28
|
+
};
|
|
19
29
|
/** Context delivered alongside a received Commit on the raw handshake lane. */
|
|
20
30
|
export type CommitContext = {
|
|
21
31
|
/**
|
|
22
|
-
* The hub-authenticated publisher of the frame
|
|
23
|
-
*
|
|
24
|
-
* internally) — auxiliary information (logging, rate-limiting), not an
|
|
25
|
-
* authorization boundary.
|
|
32
|
+
* The hub-authenticated publisher of the frame — the transport sender, NOT the
|
|
33
|
+
* MLS committer. Auxiliary (logging, rate-limiting), not an authorization boundary.
|
|
26
34
|
*/
|
|
27
35
|
senderDID?: string;
|
|
36
|
+
/**
|
|
37
|
+
* The bodies riding this Commit's own frame: the signed control-ledger tokens it enacts,
|
|
38
|
+
* sealed under the epoch the Commit is framed at. The port wires this into the MLS handle's
|
|
39
|
+
* entry resolver for this commit's duration, so the named entries resolve from the frame that
|
|
40
|
+
* carries them — no prior delivery, no store.
|
|
41
|
+
*
|
|
42
|
+
* A resolver, not a value, and load-bearing: it opens the blob only when the port asks, and
|
|
43
|
+
* the port asks only for a Commit it is applying — framed at this peer's epoch, which is the
|
|
44
|
+
* epoch the blob is sealed under. A frame this peer cannot apply never has its blob touched.
|
|
45
|
+
* The port binds each token to the id it asked for by digesting it (a responder can fail to
|
|
46
|
+
* answer, never inject), and gets nothing for a blob it cannot open.
|
|
47
|
+
*/
|
|
48
|
+
resolveLedgerEntries?: (ids: Array<string>) => Promise<Array<string>>;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* The external commit that rejoins a stranded peer, BUILT and not adopted — the recovery twin
|
|
52
|
+
* of {@link "commit".PendingCommit}, non-mutating for the same reason: the derived handle is
|
|
53
|
+
* adopted only if the hub accepts the commit, or a peer that adopted first sits on a branch of
|
|
54
|
+
* its own the moment it loses the compare-and-set.
|
|
55
|
+
*
|
|
56
|
+
* Carries no entries — a GroupInfo has nowhere to put an entry envelope — which is why a heal
|
|
57
|
+
* is TWO commits: this one rejoins, and the entries the peer still owes ride an ordinary
|
|
58
|
+
* `commit()` behind it.
|
|
59
|
+
*/
|
|
60
|
+
export type PendingRecovery = {
|
|
61
|
+
/** The external-commit bytes, framed at the epoch the sealed GroupInfo described. */
|
|
62
|
+
commit: Uint8Array;
|
|
63
|
+
/**
|
|
64
|
+
* Adopt the rejoined handle. Runs only if the hub accepts the external commit; the ONLY place
|
|
65
|
+
* it may be adopted.
|
|
66
|
+
*
|
|
67
|
+
* The rejoined handle's ledger is EMPTY — a GroupInfo carries a head and no entries — so until
|
|
68
|
+
* the ledger is bootstrapped the handle is internally inconsistent: a roster reset, not a
|
|
69
|
+
* neutral one.
|
|
70
|
+
*/
|
|
71
|
+
onAccepted: () => Promise<void>;
|
|
28
72
|
};
|
|
29
73
|
/**
|
|
30
|
-
* Consumer-supplied MLS lifecycle port. Sibling to {@link GroupCrypto}:
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* handshake topic, run these methods, resync when the epoch advances); the
|
|
35
|
-
* consumer owns the MLS state and any storage/atomicity, entirely below this
|
|
36
|
-
* interface.
|
|
74
|
+
* Consumer-supplied MLS lifecycle port. Sibling to {@link GroupCrypto}: this drives the
|
|
75
|
+
* handshake lane — applying Commits to advance the epoch, re-syncing a stranded peer. group-rpc
|
|
76
|
+
* owns transport + orchestration; the consumer owns MLS state and any storage/atomicity below
|
|
77
|
+
* this interface.
|
|
37
78
|
*/
|
|
38
79
|
export type GroupMLS = {
|
|
39
80
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
81
|
+
* Read what a Commit says about itself — epoch and committer — WITHOUT applying it or opening
|
|
82
|
+
* anything. `null` for bytes that are not a Commit. Lets the lane classify a frame (epoch =
|
|
83
|
+
* this peer's to apply? committer = this peer's own?) before touching it; neither question may
|
|
84
|
+
* be answered by trying and failing.
|
|
85
|
+
*/
|
|
86
|
+
readCommitHeader(commit: Uint8Array): CommitHeader | null;
|
|
87
|
+
/**
|
|
88
|
+
* Apply a received Commit and durably persist the result, returning whether the epoch
|
|
89
|
+
* advanced (the signal to resync the app lane). Must be durable before it resolves.
|
|
90
|
+
*
|
|
91
|
+
* A frame it cannot apply is `{ advanced: false }`, NEVER a throw: a throw leaves the cursor
|
|
92
|
+
* put and re-reads the frame, so a port that throws on a Commit it was never in a position to
|
|
93
|
+
* apply wedges the lane on that frame forever (a late joiner would wedge on its own add-commit,
|
|
94
|
+
* the first frame it reads). A Commit at another epoch, or one policy refuses, is both
|
|
95
|
+
* `{ advanced: false }`.
|
|
96
|
+
*
|
|
97
|
+
* Throws for exactly ONE outcome: a Commit it SHOULD apply but cannot because the ledger
|
|
98
|
+
* entries it names will not resolve from the Commit's own frame — the only place they ride.
|
|
99
|
+
* The lane does NOT retry; the bodies never arrive later (sealed under the Commit's framed
|
|
100
|
+
* epoch, so a peer that cannot open them is one no member at that epoch can), and the lane
|
|
101
|
+
* steps over the frame as POISON — cursor advances, never re-read. The throw distinguishes
|
|
102
|
+
* that poison case from a port that broke its contract on a frame it should have applied, which
|
|
103
|
+
* the lane DOES re-read. See {@link isMissingLedgerEntries}.
|
|
44
104
|
*/
|
|
45
105
|
processCommit(commit: Uint8Array, context: CommitContext): Promise<{
|
|
46
106
|
advanced: boolean;
|
|
47
107
|
}>;
|
|
48
|
-
/** Export current group state for a recovery responder to send a stranded peer. */
|
|
49
|
-
exportGroupInfo(): Promise<Uint8Array>;
|
|
50
|
-
/** Re-sync from a recovery reply, returning whether the epoch advanced. */
|
|
51
|
-
applyRecovery(groupInfo: Uint8Array): Promise<{
|
|
52
|
-
advanced: boolean;
|
|
53
|
-
}>;
|
|
54
108
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
109
|
+
* Mint the rendezvous request this peer publishes to ask the group for its state: an HPKE
|
|
110
|
+
* keypair for this one request, its public half in a token signed by this member's identity
|
|
111
|
+
* key. The private half is retained by the port keyed by `requestID`, and is what makes the
|
|
112
|
+
* reply openable by this peer alone. Minted per request, not from the leaf, because the peers
|
|
113
|
+
* that most need a heal (commit accepted and lost, on a discarded branch) no longer hold the
|
|
114
|
+
* leaf key the group can see — their own commit rotated it and the new key died unpersisted.
|
|
115
|
+
*
|
|
116
|
+
* The port MUST bound retention of that private half itself — the lane has no release hook. A
|
|
117
|
+
* gather/rejoin that times out and a `recover()` that fails before opening a reply both drop
|
|
118
|
+
* their `requestID` on the rpc side (the waiter maps clear on finish/timeout/dispose) but tell
|
|
119
|
+
* the port nothing, so a port that only evicts on reply-open accumulates one key per timed-out
|
|
120
|
+
* round. A TTL off the request's mint time is the natural bound: a request older than one
|
|
121
|
+
* requester deadline can no longer be answered. The bound lives here rather than as a lane
|
|
122
|
+
* release call that would obligate every {@link GroupMLS} to a new method.
|
|
123
|
+
*/
|
|
124
|
+
createRecoveryRequest(requestID: string): Promise<Uint8Array>;
|
|
125
|
+
/**
|
|
126
|
+
* Answer another member's request: verify the token, check the requester still holds a leaf in
|
|
127
|
+
* THIS member's current ratchet tree, seal current GroupInfo to the ephemeral key inside the
|
|
128
|
+
* signed request, AND vouch for it — the reply carries a membership attestation this member
|
|
129
|
+
* signs with its DID key, binding the group, the request and the exact GroupInfo bytes. The
|
|
130
|
+
* seal is HPKE base mode and authenticates no one, so the attestation is what lets the
|
|
131
|
+
* requester tell this member's reply from an observer's forgery.
|
|
132
|
+
*
|
|
133
|
+
* Authorization is roster-intrinsic, not a check the caller can forget: a removed member gets
|
|
134
|
+
* nothing from any responder that has applied its removal. Throws for a request it refuses, and
|
|
135
|
+
* the peer stays silent.
|
|
136
|
+
*/
|
|
137
|
+
sealGroupInfo(request: Uint8Array): Promise<Uint8Array>;
|
|
138
|
+
/**
|
|
139
|
+
* Open a sealed reply with the key minted for `requestID` and BUILD the external commit that
|
|
140
|
+
* rejoins this peer. Non-mutating: the rejoined handle is adopted in
|
|
141
|
+
* {@link PendingRecovery.onAccepted} and nowhere else, because the commit still has to win a
|
|
142
|
+
* compare-and-set at the head.
|
|
143
|
+
*
|
|
144
|
+
* `null` for bytes this peer cannot open OR cannot trust. HPKE base mode needs only the
|
|
145
|
+
* requester's public ephemeral key — all of which rides the public request in the clear — so a
|
|
146
|
+
* hub-injected or observer-forged reply may DECRYPT. What refuses it: the responder must prove
|
|
147
|
+
* membership by signing the reply with its DID key (open side requires the signer to hold a
|
|
148
|
+
* leaf in this peer's last-known tree), and the offered GroupInfo's group id and genesis anchor
|
|
149
|
+
* must match the group being healed. A reply the AEAD refuses, or one that fails either check,
|
|
150
|
+
* is `null`. A throw is tolerated and read the same way.
|
|
151
|
+
*/
|
|
152
|
+
applyRecovery(sealed: Uint8Array, requestID: string): Promise<PendingRecovery | null>;
|
|
153
|
+
/**
|
|
154
|
+
* Whether the ledger this handle holds is the whole ledger its OWN GroupContext attests to:
|
|
155
|
+
* the head folded from the entries it holds, in their order, against the authenticated head it
|
|
156
|
+
* carries. Purely local. False means incomplete — {@link bootstrapLedger} must run before this
|
|
157
|
+
* handle can be trusted to fold a roster or judge an incoming commit. A handle that rejoined by
|
|
158
|
+
* external commit reads false: empty ledger against a live, non-genesis head.
|
|
159
|
+
*/
|
|
160
|
+
isLedgerComplete(): Promise<boolean>;
|
|
161
|
+
/**
|
|
162
|
+
* The WHOLE ordered ledger this handle holds, as signed tokens — what a peer about to rejoin
|
|
163
|
+
* snapshots as the entries it may still owe, and filters its re-enactment against once the
|
|
164
|
+
* group's ledger is back. Purely local, never on the wire (a responder uses {@link sealLedger}).
|
|
165
|
+
*
|
|
166
|
+
* Order is load-bearing: the head is a chain digest, so a permuted list of the same tokens
|
|
167
|
+
* folds to a different head and the requester rejects it.
|
|
168
|
+
*/
|
|
169
|
+
getLedger(): Promise<Array<string>>;
|
|
170
|
+
/**
|
|
171
|
+
* Answer another member's ledger gather: verify the token, check the requester still holds a
|
|
172
|
+
* leaf in THIS member's current ratchet tree, and seal this handle's whole ordered ledger to
|
|
173
|
+
* the ephemeral key inside the signed request.
|
|
174
|
+
*
|
|
175
|
+
* Same roster-intrinsic authorization {@link sealGroupInfo} makes, and not a nicety: the ledger
|
|
176
|
+
* is the group's whole authority state on a public secretless topic, and a responder that
|
|
177
|
+
* sealed without checking would hand every role and promotion to any stranger who minted a
|
|
178
|
+
* request, encrypted to the stranger's key. Throws for a request it refuses; the peer stays silent.
|
|
179
|
+
*
|
|
180
|
+
* The seal is EPOCH-INDEPENDENT and must remain so: the requester crashed between rejoin and
|
|
181
|
+
* bootstrap and the lane gathers before it pulls, so it can be at an older epoch than every
|
|
182
|
+
* responder, and a reply sealed under the responder's current epoch would be unopenable by it.
|
|
183
|
+
*/
|
|
184
|
+
sealLedger(request: Uint8Array): Promise<Uint8Array>;
|
|
185
|
+
/**
|
|
186
|
+
* Open a sealed gather reply with the key minted for `requestID`, returning the responder's
|
|
187
|
+
* whole ordered ledger as signed tokens.
|
|
188
|
+
*
|
|
189
|
+
* `null` when the AEAD refuses the bytes — sealed to another ephemeral key, or bound to another
|
|
190
|
+
* member, request, or reply kind. It does NOT prove the sealer was a member: unlike the
|
|
191
|
+
* GroupInfo reply the ledger reply carries no attestation, and HPKE base mode authenticates no
|
|
192
|
+
* one, so an observer of the request can forge a reply that decrypts. The key is NOT consumed:
|
|
193
|
+
* every responder answers, and the requester must open the next reply after dropping one.
|
|
194
|
+
*
|
|
195
|
+
* Opening proves NOTHING about who sealed the tokens. The bound is {@link bootstrapLedger}'s
|
|
196
|
+
* head check: a reply reproducing this group's authenticated head is its whole ledger in order,
|
|
197
|
+
* and a forged, lying, reordered or truncated one fails and is dropped — withhold, never
|
|
198
|
+
* rewrite. A forged reply that merely decrypts costs one gather attempt: denial of service, not
|
|
199
|
+
* compromise.
|
|
200
|
+
*/
|
|
201
|
+
openSealedLedger(sealed: Uint8Array, requestID: string): Promise<Array<string> | null>;
|
|
202
|
+
/**
|
|
203
|
+
* Install a gathered ledger, verified against the authenticated head BEFORE a single entry is
|
|
204
|
+
* folded. REPLACES the ledger this handle holds, sound because the check is a fold from
|
|
205
|
+
* genesis: a list reproducing the head this handle's own GroupContext carries IS the group's
|
|
206
|
+
* entire ledger, in order.
|
|
207
|
+
*
|
|
208
|
+
* Throws for a list whose recomputed head does not match — a lying responder can withhold,
|
|
209
|
+
* never rewrite — and the peer drops that responder and folds the next reply instead.
|
|
210
|
+
*/
|
|
211
|
+
bootstrapLedger(tokens: Array<string>): Promise<void>;
|
|
212
|
+
/**
|
|
213
|
+
* The epoch-independent secret for the non-rotating handshake/recovery topic. Stable for the
|
|
214
|
+
* group's whole life so a stranded peer on any epoch can always derive the rendezvous.
|
|
58
215
|
*/
|
|
59
216
|
exportRecoverySecret(): Uint8Array | Promise<Uint8Array>;
|
|
60
217
|
};
|
|
218
|
+
/**
|
|
219
|
+
* The one throw {@link GroupMLS.processCommit} is allowed: the Commit named ledger entries whose
|
|
220
|
+
* bodies would not resolve from the frame the Commit rides in.
|
|
221
|
+
*
|
|
222
|
+
* The lane treats it as POISON: step over the frame, advance the cursor, never re-read. The
|
|
223
|
+
* bodies are sealed under the Commit's framed epoch, so a peer that cannot resolve them is one no
|
|
224
|
+
* member at that epoch can — nobody applies the commit, the group never moves past that epoch,
|
|
225
|
+
* and the next honest commit is framed at the same epoch and compare-and-sets behind it.
|
|
226
|
+
* Everything else the port says about a frame it will not apply is `{ advanced: false }`, poison
|
|
227
|
+
* on the same terms: advances, never retried, does not heal.
|
|
228
|
+
*
|
|
229
|
+
* Matched by NAME, not class: the port is consumer-supplied and its error type is not this
|
|
230
|
+
* package's to import. A missing-entries error named anything else is treated as an unknown
|
|
231
|
+
* failure — so the name is contract.
|
|
232
|
+
*/
|
|
233
|
+
export declare function isMissingLedgerEntries(error: unknown): boolean;
|
package/lib/crypto.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
|
|
2
|
+
* The one throw {@link GroupMLS.processCommit} is allowed: the Commit named ledger entries whose
|
|
3
|
+
* bodies would not resolve from the frame the Commit rides in.
|
|
4
|
+
*
|
|
5
|
+
* The lane treats it as POISON: step over the frame, advance the cursor, never re-read. The
|
|
6
|
+
* bodies are sealed under the Commit's framed epoch, so a peer that cannot resolve them is one no
|
|
7
|
+
* member at that epoch can — nobody applies the commit, the group never moves past that epoch,
|
|
8
|
+
* and the next honest commit is framed at the same epoch and compare-and-sets behind it.
|
|
9
|
+
* Everything else the port says about a frame it will not apply is `{ advanced: false }`, poison
|
|
10
|
+
* on the same terms: advances, never retried, does not heal.
|
|
11
|
+
*
|
|
12
|
+
* Matched by NAME, not class: the port is consumer-supplied and its error type is not this
|
|
13
|
+
* package's to import. A missing-entries error named anything else is treated as an unknown
|
|
14
|
+
* failure — so the name is contract.
|
|
15
|
+
*/ export function isMissingLedgerEntries(error) {
|
|
16
|
+
return error instanceof Error && error.name === 'MissingLedgerEntriesError';
|
|
17
|
+
}
|
package/lib/cursor.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hub hands a peer two kinds of position, both a bare `string`:
|
|
3
|
+
*
|
|
4
|
+
* - A **log position** is a place in a *topic's log*: `fetchTopic`'s `after`, `head`,
|
|
5
|
+
* `oldest`. Every reader of the topic sees the same log in the same order.
|
|
6
|
+
* - A **delivery position** is a place in *this recipient's delivery queue*:
|
|
7
|
+
* `hub/receive`'s `after` and `ack`. It runs across every subscribed topic, skips the
|
|
8
|
+
* recipient's own frames, and is emptied by acking.
|
|
9
|
+
*
|
|
10
|
+
* Different sequences, different frames, different orders. Crossing them silently mis-pages
|
|
11
|
+
* (skipped or re-read commits) with no type error, since both are strings. Branded so a
|
|
12
|
+
* delivery position cannot be assigned where a log position is expected, and minting either
|
|
13
|
+
* requires saying which you mean.
|
|
14
|
+
*/
|
|
15
|
+
declare const logPositionBrand: unique symbol;
|
|
16
|
+
declare const deliveryPositionBrand: unique symbol;
|
|
17
|
+
/** A position in a topic's log. Read it from a `fetchTopic` result or a log publish. */
|
|
18
|
+
export type LogPosition = string & {
|
|
19
|
+
readonly [logPositionBrand]: true;
|
|
20
|
+
};
|
|
21
|
+
/** A position in a recipient's delivery queue. Read it from a pushed message. */
|
|
22
|
+
export type DeliveryPosition = string & {
|
|
23
|
+
readonly [deliveryPositionBrand]: true;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Name a sequenceID as a position in a topic's log. Legitimate sources: the entries,
|
|
27
|
+
* `head` and `oldest` of a `fetchTopic` result, and the sequenceID a `retain: 'log'`
|
|
28
|
+
* publish returns. A sequenceID from a *pushed* message is NOT one of them.
|
|
29
|
+
*/
|
|
30
|
+
export declare function asLogPosition(sequenceID: string): LogPosition;
|
|
31
|
+
/** Name a sequenceID as a position in this recipient's delivery queue (i.e. an ack). */
|
|
32
|
+
export declare function asDeliveryPosition(sequenceID: string): DeliveryPosition;
|
|
33
|
+
export {};
|
package/lib/cursor.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hub hands a peer two kinds of position, both a bare `string`:
|
|
3
|
+
*
|
|
4
|
+
* - A **log position** is a place in a *topic's log*: `fetchTopic`'s `after`, `head`,
|
|
5
|
+
* `oldest`. Every reader of the topic sees the same log in the same order.
|
|
6
|
+
* - A **delivery position** is a place in *this recipient's delivery queue*:
|
|
7
|
+
* `hub/receive`'s `after` and `ack`. It runs across every subscribed topic, skips the
|
|
8
|
+
* recipient's own frames, and is emptied by acking.
|
|
9
|
+
*
|
|
10
|
+
* Different sequences, different frames, different orders. Crossing them silently mis-pages
|
|
11
|
+
* (skipped or re-read commits) with no type error, since both are strings. Branded so a
|
|
12
|
+
* delivery position cannot be assigned where a log position is expected, and minting either
|
|
13
|
+
* requires saying which you mean.
|
|
14
|
+
*/ // Nominal brand keys: `declare const … : unique symbol` mints a fresh type-only tag with no runtime
|
|
15
|
+
// value — the declaration IS the definition, nothing to import. Each tags one string type below so
|
|
16
|
+
// the two cannot be assigned to each other.
|
|
17
|
+
/**
|
|
18
|
+
* Name a sequenceID as a position in a topic's log. Legitimate sources: the entries,
|
|
19
|
+
* `head` and `oldest` of a `fetchTopic` result, and the sequenceID a `retain: 'log'`
|
|
20
|
+
* publish returns. A sequenceID from a *pushed* message is NOT one of them.
|
|
21
|
+
*/ export function asLogPosition(sequenceID) {
|
|
22
|
+
return sequenceID;
|
|
23
|
+
}
|
|
24
|
+
/** Name a sequenceID as a position in this recipient's delivery queue (i.e. an ack). */ export function asDeliveryPosition(sequenceID) {
|
|
25
|
+
return sequenceID;
|
|
26
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ByteTransform, Unwrap } from '@kumiai/broadcast';
|
|
2
|
+
import type { MailboxHub } from '@kumiai/hub-tunnel';
|
|
3
|
+
export type SealDirectedHubParams = {
|
|
4
|
+
hub: MailboxHub;
|
|
5
|
+
wrap: ByteTransform;
|
|
6
|
+
unwrap: Unwrap;
|
|
7
|
+
/** When set, inbound frames whose recovered senderDID != this are dropped. */
|
|
8
|
+
expectedSenderDID?: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Wrap a MailboxHub so directed frames are sealed with `wrap` on publish and opened
|
|
12
|
+
* with `unwrap` on receive. The recovered MLS `senderDID` replaces the
|
|
13
|
+
* hub-asserted one (a lying hub cannot forge it); frames that fail to open,
|
|
14
|
+
* that unwrap without a recovered sender, or whose recovered sender !=
|
|
15
|
+
* `expectedSenderDID`, are dropped.
|
|
16
|
+
*/
|
|
17
|
+
export declare function sealDirectedHub(params: SealDirectedHubParams): MailboxHub;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
function normalizeUnwrap(result) {
|
|
2
|
+
return result instanceof Uint8Array ? {
|
|
3
|
+
payload: result
|
|
4
|
+
} : result;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Wrap a MailboxHub so directed frames are sealed with `wrap` on publish and opened
|
|
8
|
+
* with `unwrap` on receive. The recovered MLS `senderDID` replaces the
|
|
9
|
+
* hub-asserted one (a lying hub cannot forge it); frames that fail to open,
|
|
10
|
+
* that unwrap without a recovered sender, or whose recovered sender !=
|
|
11
|
+
* `expectedSenderDID`, are dropped.
|
|
12
|
+
*/ export function sealDirectedHub(params) {
|
|
13
|
+
const { hub, wrap, unwrap, expectedSenderDID } = params;
|
|
14
|
+
return {
|
|
15
|
+
async publish (publishParams) {
|
|
16
|
+
const sealed = await wrap(publishParams.payload);
|
|
17
|
+
return hub.publish({
|
|
18
|
+
senderDID: publishParams.senderDID,
|
|
19
|
+
topicID: publishParams.topicID,
|
|
20
|
+
payload: sealed
|
|
21
|
+
});
|
|
22
|
+
},
|
|
23
|
+
subscribe: (subscriberDID, topicID)=>hub.subscribe(subscriberDID, topicID),
|
|
24
|
+
unsubscribe: (subscriberDID, topicID)=>hub.unsubscribe?.(subscriberDID, topicID),
|
|
25
|
+
receive (subscriberDID) {
|
|
26
|
+
const inner = hub.receive(subscriberDID);
|
|
27
|
+
const innerIterator = inner[Symbol.asyncIterator]();
|
|
28
|
+
const iterator = {
|
|
29
|
+
async next () {
|
|
30
|
+
while(true){
|
|
31
|
+
const result = await innerIterator.next();
|
|
32
|
+
if (result.done) {
|
|
33
|
+
return {
|
|
34
|
+
value: undefined,
|
|
35
|
+
done: true
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const message = result.value;
|
|
39
|
+
let opened;
|
|
40
|
+
try {
|
|
41
|
+
opened = normalizeUnwrap(await unwrap(message.payload));
|
|
42
|
+
} catch {
|
|
43
|
+
continue; // un-openable (garbage / another lane) — drop
|
|
44
|
+
}
|
|
45
|
+
if (opened.senderDID == null) {
|
|
46
|
+
continue; // no authenticated sender — never fall back to the hub-asserted one
|
|
47
|
+
}
|
|
48
|
+
if (expectedSenderDID != null && opened.senderDID !== expectedSenderDID) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
value: {
|
|
53
|
+
sequenceID: message.sequenceID,
|
|
54
|
+
senderDID: opened.senderDID,
|
|
55
|
+
topicID: message.topicID,
|
|
56
|
+
payload: opened.payload
|
|
57
|
+
},
|
|
58
|
+
done: false
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
return () {
|
|
63
|
+
innerIterator.return?.();
|
|
64
|
+
return Promise.resolve({
|
|
65
|
+
value: undefined,
|
|
66
|
+
done: true
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
return {
|
|
71
|
+
[Symbol.asyncIterator] () {
|
|
72
|
+
return iterator;
|
|
73
|
+
},
|
|
74
|
+
return () {
|
|
75
|
+
inner.return?.();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
package/lib/directed.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Client } from '@enkaku/client';
|
|
2
2
|
import type { ProtocolDefinition } from '@enkaku/protocol';
|
|
3
3
|
import { type ProcedureHandlers } from '@enkaku/server';
|
|
4
|
+
import type { ByteTransform, Unwrap } from '@kumiai/broadcast';
|
|
4
5
|
import type { HubMux } from './hub-mux.js';
|
|
5
6
|
export type DirectedClientParams = {
|
|
6
7
|
mux: HubMux;
|
|
@@ -8,6 +9,8 @@ export type DirectedClientParams = {
|
|
|
8
9
|
memberDID: string;
|
|
9
10
|
secret: Uint8Array;
|
|
10
11
|
epoch: number;
|
|
12
|
+
wrap: ByteTransform;
|
|
13
|
+
unwrap: Unwrap;
|
|
11
14
|
getRandomID?: () => string;
|
|
12
15
|
};
|
|
13
16
|
/**
|
|
@@ -22,15 +25,19 @@ export type InboxAcceptorParams<Protocol extends ProtocolDefinition> = {
|
|
|
22
25
|
mux: HubMux;
|
|
23
26
|
localDID: string;
|
|
24
27
|
selfInboxTopic: string;
|
|
25
|
-
/** Map an
|
|
28
|
+
/** Map an authenticated senderDID to the topic we send replies on (their inbox). */
|
|
26
29
|
resolveSendTopic: (senderDID: string) => string;
|
|
27
30
|
protocol: Protocol;
|
|
28
31
|
handlers: ProcedureHandlers<Protocol>;
|
|
32
|
+
wrap: ByteTransform;
|
|
33
|
+
unwrap: Unwrap;
|
|
29
34
|
};
|
|
30
35
|
/**
|
|
31
|
-
* Accept directed RPC
|
|
32
|
-
*
|
|
33
|
-
*
|
|
36
|
+
* Accept directed RPC. A single sealed drain of `selfInboxTopic` opens each inbound frame with
|
|
37
|
+
* `unwrap`, binds every session to the MLS-authenticated sender recovered from the ciphertext,
|
|
38
|
+
* and feeds decrypted bytes into a per-session in-memory transport whose replies are sealed
|
|
39
|
+
* with `wrap`. Frames whose recovered sender does not match the session binding are dropped, so
|
|
40
|
+
* a malicious hub can neither read the lane nor forge/splice a sender.
|
|
34
41
|
*/
|
|
35
42
|
export declare function createInboxAcceptor<Protocol extends ProtocolDefinition>(params: InboxAcceptorParams<Protocol>): {
|
|
36
43
|
dispose: () => Promise<void>;
|