@serfab/cadre-core 1.9.0 → 1.11.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/dist/cadre-node.d.ts +148 -44
- package/dist/cadre-node.js +363 -139
- package/dist/cadre-node.js.map +1 -1
- package/dist/control-authorization.d.ts +2 -2
- package/dist/control-authorization.js +1 -0
- package/dist/control-authorization.js.map +1 -1
- package/dist/control-cohort.d.ts +1 -1
- package/dist/control-cohort.js +1 -1
- package/dist/control-database.d.ts +105 -14
- package/dist/control-database.js +242 -73
- package/dist/control-database.js.map +1 -1
- package/dist/control-formation-recorder.d.ts +6 -6
- package/dist/control-formation-recorder.js +7 -8
- package/dist/control-formation-recorder.js.map +1 -1
- package/dist/control-schema.d.ts +1 -1
- package/dist/control-schema.js +1066 -979
- package/dist/control-schema.js.map +1 -1
- package/dist/control-write-retry.d.ts +21 -15
- package/dist/control-write-retry.js +21 -15
- package/dist/control-write-retry.js.map +1 -1
- package/dist/diagnostics/connection-path.d.ts +1 -18
- package/dist/diagnostics/connection-path.js +1 -20
- package/dist/diagnostics/connection-path.js.map +1 -1
- package/dist/identity-key.d.ts +0 -30
- package/dist/identity-key.js +7 -40
- package/dist/identity-key.js.map +1 -1
- package/dist/index.d.ts +8 -5
- package/dist/index.js +12 -6
- package/dist/index.js.map +1 -1
- package/dist/invitation-bootstrap.d.ts +32 -0
- package/dist/invitation-bootstrap.js +30 -0
- package/dist/invitation-bootstrap.js.map +1 -0
- package/dist/link-budget.d.ts +204 -119
- package/dist/link-budget.js +208 -120
- package/dist/link-budget.js.map +1 -1
- package/dist/membership-connection-gater.d.ts +3 -3
- package/dist/membership-connection-gater.js +3 -3
- package/dist/peer-dial.d.ts +18 -14
- package/dist/peer-dial.js +18 -14
- package/dist/peer-dial.js.map +1 -1
- package/dist/peer-join-backfill.d.ts +3 -3
- package/dist/pending-join-runner.d.ts +186 -0
- package/dist/pending-join-runner.js +649 -0
- package/dist/pending-join-runner.js.map +1 -0
- package/dist/push-fanout.d.ts +1 -1
- package/dist/push-fanout.js +1 -1
- package/dist/relay-stun.d.ts +24 -0
- package/dist/relay-stun.js +51 -0
- package/dist/relay-stun.js.map +1 -0
- package/dist/seed-bootstrap.d.ts +2 -2
- package/dist/seed-bootstrap.js +5 -5
- package/dist/strand-addr-protocol.js +2 -2
- package/dist/strand-formation-deadlines.d.ts +5 -5
- package/dist/strand-formation-deadlines.js +5 -5
- package/dist/strand-formation-manager.d.ts +18 -4
- package/dist/strand-formation-manager.js +48 -33
- package/dist/strand-formation-manager.js.map +1 -1
- package/dist/strand-formation-protocol.d.ts +71 -14
- package/dist/strand-formation-protocol.js +222 -47
- package/dist/strand-formation-protocol.js.map +1 -1
- package/dist/strand-formation-rejection.d.ts +94 -0
- package/dist/strand-formation-rejection.js +77 -0
- package/dist/strand-formation-rejection.js.map +1 -0
- package/dist/strand-instance-manager.js +4 -1
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-membership-writer.js +32 -32
- package/dist/strand-solicitation.d.ts +11 -3
- package/dist/strand-solicitation.js +9 -0
- package/dist/strand-solicitation.js.map +1 -1
- package/dist/types.d.ts +103 -17
- package/dist/types.js +5 -9
- package/dist/types.js.map +1 -1
- package/package.json +22 -22
- package/src/cadre-node.ts +8654 -8400
- package/src/control-authorization.ts +2 -1
- package/src/control-cohort.ts +128 -128
- package/src/control-database.ts +303 -82
- package/src/control-formation-recorder.ts +353 -354
- package/src/control-read-retry.ts +216 -216
- package/src/control-schema.ts +1078 -991
- package/src/control-write-retry.ts +567 -561
- package/src/diagnostics/connection-path.ts +1 -29
- package/src/formation-approval.ts +610 -610
- package/src/hibernation-manager.ts +638 -638
- package/src/identity-key.ts +7 -62
- package/src/index.ts +644 -627
- package/src/invitation-bootstrap.ts +40 -0
- package/src/joined-strand-store.ts +498 -498
- package/src/link-budget.ts +684 -583
- package/src/membership-connection-gater.ts +570 -570
- package/src/node-local-snapshot.ts +298 -298
- package/src/peer-dial.ts +315 -311
- package/src/peer-join-backfill.ts +710 -710
- package/src/pending-join-runner.ts +735 -0
- package/src/push-fanout.ts +1 -1
- package/src/relay-reservation.ts +1121 -1121
- package/src/relay-server.ts +98 -98
- package/src/relay-stun.ts +58 -0
- package/src/seed-bootstrap.ts +1448 -1448
- package/src/strand-addr-protocol.ts +547 -547
- package/src/strand-database.ts +287 -287
- package/src/strand-first-sync-gate.ts +391 -391
- package/src/strand-formation-deadlines.ts +227 -227
- package/src/strand-formation-manager.ts +725 -700
- package/src/strand-formation-protocol.ts +1036 -802
- package/src/strand-formation-rejection.ts +129 -0
- package/src/strand-fret-addrs.ts +145 -145
- package/src/strand-instance-manager.ts +1789 -1786
- package/src/strand-membership-writer.ts +1974 -1974
- package/src/strand-network-state-file.ts +40 -40
- package/src/strand-network-state.ts +243 -243
- package/src/strand-revocation-enforcer.ts +654 -654
- package/src/strand-solicitation.ts +532 -519
- package/src/strand-wake-protocol.ts +401 -401
- package/src/types.ts +2222 -2140
- package/dist/diagnostics/webrtc-turn-tracker.d.ts +0 -50
- package/dist/diagnostics/webrtc-turn-tracker.js +0 -196
- package/dist/diagnostics/webrtc-turn-tracker.js.map +0 -1
- package/src/diagnostics/webrtc-turn-tracker.ts +0 -222
|
@@ -1,354 +1,353 @@
|
|
|
1
|
-
import debug from 'debug';
|
|
2
|
-
import { FormationAbortedError, InvitationExhaustedError, type ControlDatabase } from './control-database.js';
|
|
3
|
-
import { mintStrandId } from './strand-id.js';
|
|
4
|
-
import {
|
|
5
|
-
createHttpFormationApprover,
|
|
6
|
-
verifyFormationApproval,
|
|
7
|
-
FormationApprovalError,
|
|
8
|
-
type FormationApprover,
|
|
9
|
-
type FormationApproval,
|
|
10
|
-
type FormationApprovalRequest
|
|
11
|
-
} from './formation-approval.js';
|
|
12
|
-
import type {
|
|
13
|
-
AuthorizedFormationUsage,
|
|
14
|
-
FormationUsageParams,
|
|
15
|
-
FormationUsageRecorder,
|
|
16
|
-
ResolvedHostStrand
|
|
17
|
-
} from './strand-solicitation.js';
|
|
18
|
-
import type { OpenInvitation } from './types.js';
|
|
19
|
-
|
|
20
|
-
const log = debug('sereus:cadre:formation-recorder');
|
|
21
|
-
|
|
22
|
-
/** Far-future sentinel for an invitation that never expires (max valid JS Date). */
|
|
23
|
-
const NEVER_EXPIRES = new Date(8640000000000000);
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* {@link FormationUsageRecorder} backed by the real `CadreControl` tables.
|
|
27
|
-
*
|
|
28
|
-
* It reads `FormationInvite` / `FormationUsage` to answer token-validity and
|
|
29
|
-
* usage questions, and writes the consent row that records a redemption. Two
|
|
30
|
-
* provisioning shapes are supported, keyed on whether the invite binds a host strand:
|
|
31
|
-
*
|
|
32
|
-
* - **Bound (provision-then-record):** the host strand already exists (owner-signed
|
|
33
|
-
* up front and named by the invite's `StrandId`), so {@link resolveStrand} reports it
|
|
34
|
-
* and {@link authorizeUsage} then its `record()` (or {@link recordUsage}, both at once)
|
|
35
|
-
* writes the consent row against that pre-existing strand
|
|
36
|
-
* (record-only) rather than inserting a new `Strand`.
|
|
37
|
-
* - **Unbound (responder-provisions):** the invite carries no `StrandId`, so
|
|
38
|
-
* {@link provisionAndRecord} mints a fresh strand and records consent against it
|
|
39
|
-
* ATOMICALLY (one `FormationUsage` row), closing the single-use hole the older
|
|
40
|
-
* never-record fallback left open.
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* Usage accounting follows the schema's `FormationUsage.Authorized` semantics:
|
|
46
|
-
* a null `TotalUses` means unlimited uses; otherwise the invite is "used up"
|
|
47
|
-
* once the recorded usage count reaches `TotalUses`.
|
|
48
|
-
*
|
|
49
|
-
* When the invite carries a `ValidationUrl`, both write paths first obtain an outside
|
|
50
|
-
* approval ({@link obtainApproval}) — the recorder is the one place where the nonce that is
|
|
51
|
-
* SIGNED and the nonce that is INSERTED are trivially the same value, since it also performs
|
|
52
|
-
* the write. That makes this class do network I/O; failures surface as
|
|
53
|
-
* {@link FormationApprovalError}s the manager maps to protocol rejection reasons. The bound
|
|
54
|
-
* path is split in two ({@link authorizeUsage}, then the returned `record()`) so the manager
|
|
55
|
-
* can issue the joiner's membership pass between the approval and the write; the handle
|
|
56
|
-
* carries the approved fields across, which keeps the signed and inserted nonce the same.
|
|
57
|
-
*/
|
|
58
|
-
export class ControlFormationUsageRecorder implements FormationUsageRecorder {
|
|
59
|
-
private readonly approver: FormationApprover;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* `approver` defaults to the real HTTP hook client — deliberately ON:
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
* A
|
|
97
|
-
*
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* - {@link
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
'
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
//
|
|
225
|
-
//
|
|
226
|
-
//
|
|
227
|
-
// CHECK
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
* -
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
* `FormationUsage` row
|
|
310
|
-
* `
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
*
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
*
|
|
323
|
-
*
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
const
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
}
|
|
1
|
+
import debug from 'debug';
|
|
2
|
+
import { FormationAbortedError, InvitationExhaustedError, type ControlDatabase } from './control-database.js';
|
|
3
|
+
import { mintStrandId } from './strand-id.js';
|
|
4
|
+
import {
|
|
5
|
+
createHttpFormationApprover,
|
|
6
|
+
verifyFormationApproval,
|
|
7
|
+
FormationApprovalError,
|
|
8
|
+
type FormationApprover,
|
|
9
|
+
type FormationApproval,
|
|
10
|
+
type FormationApprovalRequest
|
|
11
|
+
} from './formation-approval.js';
|
|
12
|
+
import type {
|
|
13
|
+
AuthorizedFormationUsage,
|
|
14
|
+
FormationUsageParams,
|
|
15
|
+
FormationUsageRecorder,
|
|
16
|
+
ResolvedHostStrand
|
|
17
|
+
} from './strand-solicitation.js';
|
|
18
|
+
import type { OpenInvitation } from './types.js';
|
|
19
|
+
|
|
20
|
+
const log = debug('sereus:cadre:formation-recorder');
|
|
21
|
+
|
|
22
|
+
/** Far-future sentinel for an invitation that never expires (max valid JS Date). */
|
|
23
|
+
const NEVER_EXPIRES = new Date(8640000000000000);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* {@link FormationUsageRecorder} backed by the real `CadreControl` tables.
|
|
27
|
+
*
|
|
28
|
+
* It reads `FormationInvite` / `FormationUsage` to answer token-validity and
|
|
29
|
+
* usage questions, and writes the consent row that records a redemption. Two
|
|
30
|
+
* provisioning shapes are supported, keyed on whether the invite binds a host strand:
|
|
31
|
+
*
|
|
32
|
+
* - **Bound (provision-then-record):** the host strand already exists (owner-signed
|
|
33
|
+
* up front and named by the invite's `StrandId`), so {@link resolveStrand} reports it
|
|
34
|
+
* and {@link authorizeUsage} then its `record()` (or {@link recordUsage}, both at once)
|
|
35
|
+
* writes the consent row against that pre-existing strand
|
|
36
|
+
* (record-only) rather than inserting a new `Strand`.
|
|
37
|
+
* - **Unbound (responder-provisions):** the invite carries no `StrandId`, so
|
|
38
|
+
* {@link provisionAndRecord} mints a fresh strand and records consent against it
|
|
39
|
+
* ATOMICALLY (one `FormationUsage` row), closing the single-use hole the older
|
|
40
|
+
* never-record fallback left open.
|
|
41
|
+
*
|
|
42
|
+
* Every `CadreNode`'s formation responder uses it unless the embedder passes another
|
|
43
|
+
* recorder to `initializeStrandSolicitation`.
|
|
44
|
+
*
|
|
45
|
+
* Usage accounting follows the schema's `FormationUsage.Authorized` semantics:
|
|
46
|
+
* a null `TotalUses` means unlimited uses; otherwise the invite is "used up"
|
|
47
|
+
* once the recorded usage count reaches `TotalUses`.
|
|
48
|
+
*
|
|
49
|
+
* When the invite carries a `ValidationUrl`, both write paths first obtain an outside
|
|
50
|
+
* approval ({@link obtainApproval}) — the recorder is the one place where the nonce that is
|
|
51
|
+
* SIGNED and the nonce that is INSERTED are trivially the same value, since it also performs
|
|
52
|
+
* the write. That makes this class do network I/O; failures surface as
|
|
53
|
+
* {@link FormationApprovalError}s the manager maps to protocol rejection reasons. The bound
|
|
54
|
+
* path is split in two ({@link authorizeUsage}, then the returned `record()`) so the manager
|
|
55
|
+
* can issue the joiner's membership pass between the approval and the write; the handle
|
|
56
|
+
* carries the approved fields across, which keeps the signed and inserted nonce the same.
|
|
57
|
+
*/
|
|
58
|
+
export class ControlFormationUsageRecorder implements FormationUsageRecorder {
|
|
59
|
+
private readonly approver: FormationApprover;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* `approver` defaults to the real HTTP hook client — deliberately ON: `CadreNode` builds this
|
|
63
|
+
* recorder for every node's responder with no options, so an opt-in approver would leave
|
|
64
|
+
* every real deployment unable to redeem a `ValidationUrl`-bearing invite. Tests inject a fake.
|
|
65
|
+
*/
|
|
66
|
+
constructor(
|
|
67
|
+
private readonly controlDatabase: ControlDatabase,
|
|
68
|
+
options?: { approver?: FormationApprover }
|
|
69
|
+
) {
|
|
70
|
+
this.approver = options?.approver ?? createHttpFormationApprover();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** A token is valid when a matching, unexpired `FormationInvite` exists. */
|
|
74
|
+
async isTokenValid(token: string): Promise<{ valid: boolean; reason?: 'unknown' | 'expired'; invitation?: OpenInvitation }> {
|
|
75
|
+
const invite = await this.controlDatabase.queryFormationInvite(token);
|
|
76
|
+
if (!invite) {
|
|
77
|
+
return { valid: false, reason: 'unknown' };
|
|
78
|
+
}
|
|
79
|
+
if (invite.expiresAtMs !== null && invite.expiresAtMs <= Date.now()) {
|
|
80
|
+
log('Token expired: %s', token);
|
|
81
|
+
return { valid: false, reason: 'expired' };
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
valid: true,
|
|
85
|
+
invitation: {
|
|
86
|
+
token: invite.token,
|
|
87
|
+
sAppId: invite.sAppId,
|
|
88
|
+
expiration: invite.expiresAtMs !== null ? new Date(invite.expiresAtMs) : NEVER_EXPIRES,
|
|
89
|
+
bootstrap: [],
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* A token is "used" when its recorded usage count has reached the invite's
|
|
96
|
+
* `TotalUses`. A null `TotalUses` (unlimited) is never used up; an unknown
|
|
97
|
+
* token is reported not-used (validity is handled by {@link isTokenValid}).
|
|
98
|
+
*/
|
|
99
|
+
async isTokenUsed(token: string): Promise<boolean> {
|
|
100
|
+
const invite = await this.controlDatabase.queryFormationInvite(token);
|
|
101
|
+
if (!invite || invite.totalUses === null) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
const uses = await this.controlDatabase.countFormationUsage(token);
|
|
105
|
+
return uses >= invite.totalUses;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Any `FormationInvite` row still unexpired and not fully consumed — the
|
|
110
|
+
* durable half of the control-network connection gate's "does this node
|
|
111
|
+
* expect a stranger?" question. Survives a restart and sees invites
|
|
112
|
+
* replicated in from sibling nodes of the same cadre.
|
|
113
|
+
*/
|
|
114
|
+
async hasOutstandingInvitation(): Promise<boolean> {
|
|
115
|
+
return await this.controlDatabase.hasOutstandingFormationInvite();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Obtain the approval material for ONE redemption, when the invite demands it.
|
|
120
|
+
*
|
|
121
|
+
* `validationUrl === null` → `{}`: no approval required. The `usageStampId` nonce is
|
|
122
|
+
* ALWAYS supplied by the caller — the JOINER mints it, signs its own consent over it, and
|
|
123
|
+
* sends it in the contact message — so the approver signs over the identical nonce in both
|
|
124
|
+
* paths. Two local pre-checks run before anything is written:
|
|
125
|
+
*
|
|
126
|
+
* - {@link verifyFormationApproval} — did the hook sign the exact fields we hold?
|
|
127
|
+
* - {@link ControlDatabase.queryValidationKeyStampId} — is the approval's key enrolled?
|
|
128
|
+
*
|
|
129
|
+
* Both pre-checks exist purely for legibility — they turn a bad approval into a named
|
|
130
|
+
* {@link FormationApprovalError} instead of an opaque `CHECK constraint failed: Authorized`
|
|
131
|
+
* at commit. The database re-verifies against the STORED `ValidationKey` row and remains
|
|
132
|
+
* the security authority; do not "simplify" the database check away in favour of these.
|
|
133
|
+
*/
|
|
134
|
+
private async obtainApproval(
|
|
135
|
+
request: Omit<FormationApprovalRequest, 'validationUrl'> & { validationUrl: string | null },
|
|
136
|
+
signal?: AbortSignal
|
|
137
|
+
): Promise<{ validationKey?: string; validationSignature?: string }> {
|
|
138
|
+
const { validationUrl, ...fields } = request;
|
|
139
|
+
if (validationUrl === null) {
|
|
140
|
+
return {};
|
|
141
|
+
}
|
|
142
|
+
if (signal?.aborted) {
|
|
143
|
+
throw new FormationAbortedError(fields.token, 'approval');
|
|
144
|
+
}
|
|
145
|
+
const fullRequest: FormationApprovalRequest = { ...fields, validationUrl };
|
|
146
|
+
const approval = await this.askApprover(fullRequest, signal);
|
|
147
|
+
if (!verifyFormationApproval(fullRequest, approval)) {
|
|
148
|
+
throw new FormationApprovalError(
|
|
149
|
+
'malformed',
|
|
150
|
+
`Approval signature does not verify over this redemption (token ${fields.token})`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
if (await this.controlDatabase.queryValidationKeyStampId(approval.validationKey) === null) {
|
|
154
|
+
throw new FormationApprovalError(
|
|
155
|
+
'unenrolled',
|
|
156
|
+
'Approval is signed by a key that is not an enrolled ValidationKey'
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
return approval;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Ask the approver, re-labelling a caller-abort as a {@link FormationAbortedError}.
|
|
164
|
+
*
|
|
165
|
+
* The HTTP client relays a caller-abort onto its own request and reports it as an
|
|
166
|
+
* `unavailable` {@link FormationApprovalError} — which the manager cannot tell from a
|
|
167
|
+
* genuinely dead hook, and so maps to a rejection reason and RETURNS. Every other abort on
|
|
168
|
+
* this path THROWS, which is what makes the manager rethrow and leave the reply to the
|
|
169
|
+
* listener's timeout path; a mid-hook abort belongs in the same class. The approval error
|
|
170
|
+
* is kept as `cause` so the transport-level detail is not lost.
|
|
171
|
+
*/
|
|
172
|
+
private async askApprover(
|
|
173
|
+
request: FormationApprovalRequest,
|
|
174
|
+
signal?: AbortSignal
|
|
175
|
+
): Promise<FormationApproval> {
|
|
176
|
+
try {
|
|
177
|
+
return await this.approver.requestApproval(request, signal);
|
|
178
|
+
} catch (err) {
|
|
179
|
+
if (signal?.aborted) {
|
|
180
|
+
throw new FormationAbortedError(request.token, 'approval', { cause: err });
|
|
181
|
+
}
|
|
182
|
+
throw err;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Record consent against an **already-existing** host strand (record-only): the
|
|
188
|
+
* single `FormationUsage` insert auto-commits and the deferred `StrandExists`
|
|
189
|
+
* CHECK is satisfied by the pre-existing strand. This is the provision-then-record
|
|
190
|
+
* commitment — the strand was minted owner-signed up front, so we do NOT
|
|
191
|
+
* re-insert it (which would double-insert the same PK). `peerKey` is written as the
|
|
192
|
+
* usage `PeerKey`, which BOTH signed digests cover: the joiner's own consent signature
|
|
193
|
+
* (`peerSignature`, verified by the schema's `PeerConsented` CHECK) and — when the invite
|
|
194
|
+
* carries a `ValidationUrl` — the approver's sign-off, so it is the joiner an approval is
|
|
195
|
+
* spent on AND provably the joiner that agreed. Use
|
|
196
|
+
* {@link ControlDatabase.redeemInvitation} for the consent-creates-strand path instead.
|
|
197
|
+
*
|
|
198
|
+
* Authorizes ({@link authorizeUsage}) and writes in one step, for callers with nothing to do
|
|
199
|
+
* in between.
|
|
200
|
+
*/
|
|
201
|
+
async recordUsage(params: FormationUsageParams): Promise<void> {
|
|
202
|
+
await (await this.authorizeUsage(params)).record();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Everything {@link recordUsage} does before its write, so the manager can issue the
|
|
207
|
+
* joiner's membership pass into the host strand only once the redemption is cleared, and
|
|
208
|
+
* spend the token only after that.
|
|
209
|
+
*
|
|
210
|
+
* Reads the invite first (one extra read per redemption) to learn its `ValidationUrl` and
|
|
211
|
+
* seat budget, obtains the approval through {@link obtainApproval} when one is demanded,
|
|
212
|
+
* re-checks the abort (the approval call may have been long), and pre-checks the seat
|
|
213
|
+
* budget ({@link assertSeatFree}). The returned `record()` closes over the captured fields
|
|
214
|
+
* and approval, so the nonce the approver signed is the nonce inserted without the caller
|
|
215
|
+
* ever passing it back.
|
|
216
|
+
*/
|
|
217
|
+
async authorizeUsage(params: FormationUsageParams): Promise<AuthorizedFormationUsage> {
|
|
218
|
+
const { token, peerKey, peerSignature, usageStampId, strandId, disclosure, signal } = params;
|
|
219
|
+
if (signal?.aborted) {
|
|
220
|
+
throw new FormationAbortedError(token, 'usage recording');
|
|
221
|
+
}
|
|
222
|
+
const invite = await this.controlDatabase.queryFormationInvite(token);
|
|
223
|
+
// NOTE: a missing invite row reads as "no approval required" here and in
|
|
224
|
+
// `provisionAndRecord`, so an invite that vanished between `resolveStrand` and this read
|
|
225
|
+
// would be written unapproved. Safe only because the database's `FormationUsage.Authorized`
|
|
226
|
+
// CHECK demands a matching `FormationInvite` and rolls the write back regardless; if that
|
|
227
|
+
// CHECK ever loosens, reject on `invite === null` instead of defaulting to null.
|
|
228
|
+
const approval = await this.obtainApproval({
|
|
229
|
+
token, usageStampId, strandId, peerKey, disclosure,
|
|
230
|
+
validationUrl: invite?.validationUrl ?? null,
|
|
231
|
+
}, signal);
|
|
232
|
+
if (signal?.aborted) {
|
|
233
|
+
throw new FormationAbortedError(token, 'usage recording');
|
|
234
|
+
}
|
|
235
|
+
const totalUses = invite?.totalUses ?? null;
|
|
236
|
+
await this.assertSeatFree(token, totalUses);
|
|
237
|
+
return {
|
|
238
|
+
record: async () => {
|
|
239
|
+
await this.controlDatabase.recordFormationUsage({
|
|
240
|
+
token, strandId, peerKey, peerSignature, usageStampId, disclosure, signal,
|
|
241
|
+
totalUses, ...approval,
|
|
242
|
+
});
|
|
243
|
+
log('Recorded formation usage: token=%s strand=%s', token, strandId);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Refuse a redemption whose invite has no seat left, BEFORE anything is written — the
|
|
250
|
+
* authoritative check is the one `recordFormationUsage` repeats inside its write lock.
|
|
251
|
+
* Catches a same-node rival that took the last seat while this redemption waited on the
|
|
252
|
+
* approval hook, which `runSession`'s up-front `isTokenUsed` check could not see.
|
|
253
|
+
*
|
|
254
|
+
* NOTE: two redemptions that both pass this check within the time one of them takes to
|
|
255
|
+
* issue its membership pass still both issue one, and the loser's pass is orphaned
|
|
256
|
+
* (expiry-bounded, never handed out). Holding the control write lock across issuance would
|
|
257
|
+
* close it, but `lockedWithRetry` re-runs locked bodies on transient failures (a re-run
|
|
258
|
+
* would double-issue) and every control write would stall behind a strand-database write.
|
|
259
|
+
* If orphaned passes from same-node races ever matter, reserve the seat before issuance.
|
|
260
|
+
*/
|
|
261
|
+
private async assertSeatFree(token: string, totalUses: number | null): Promise<void> {
|
|
262
|
+
if (totalUses === null) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const used = await this.controlDatabase.countFormationUsage(token);
|
|
266
|
+
if (used >= totalUses) {
|
|
267
|
+
throw new InvitationExhaustedError(token, used, totalUses);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Classify the host strand this invite binds to (see {@link ResolvedHostStrand}):
|
|
273
|
+
*
|
|
274
|
+
* - no invite / no `StrandId` → `unbound` (responder-provisions path).
|
|
275
|
+
* - `StrandId` set AND the strand row is present → `bound`, carrying its
|
|
276
|
+
* `MemberPrivateKey` (the closed-strand read-gating secret) for delivery to a
|
|
277
|
+
* validated invitee.
|
|
278
|
+
* - `StrandId` set but the strand row is absent → `missing` (the host strand has not
|
|
279
|
+
* converged on this responder yet); the manager rejects cleanly instead of recording
|
|
280
|
+
* usage against a non-existent strand, which would fail the deferred `StrandExists`
|
|
281
|
+
* CHECK at commit and drop the result frame.
|
|
282
|
+
*/
|
|
283
|
+
async resolveStrand(token: string): Promise<ResolvedHostStrand> {
|
|
284
|
+
const invite = await this.controlDatabase.queryFormationInvite(token);
|
|
285
|
+
if (!invite || !invite.strandId) {
|
|
286
|
+
return { kind: 'unbound' };
|
|
287
|
+
}
|
|
288
|
+
const strand = await this.controlDatabase.queryStrand(invite.strandId);
|
|
289
|
+
if (!strand) {
|
|
290
|
+
return { kind: 'missing', strandId: invite.strandId };
|
|
291
|
+
}
|
|
292
|
+
return {
|
|
293
|
+
kind: 'bound',
|
|
294
|
+
strandId: invite.strandId,
|
|
295
|
+
memberPrivateKey: strand.MemberPrivateKey ?? null,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Provision a NEW strand for an UNBOUND invite and record consent against it in ONE
|
|
301
|
+
* transaction (the responder-provisions fallback, now single-use-enforced).
|
|
302
|
+
*
|
|
303
|
+
* Mints a fresh, globally-unique strand id with {@link mintStrandId} — an
|
|
304
|
+
* unguessable CSPRNG id, and one `strand-id.ts` has already checked is usable as a
|
|
305
|
+
* storage scope key and network name — then delegates to
|
|
306
|
+
* {@link ControlDatabase.redeemInvitation}, whose
|
|
307
|
+
* single `begin … commit` inserts the consent-authorized `Strand` row AND the matching
|
|
308
|
+
* `FormationUsage` row together (both deferred CHECKs see both rows at commit). That one
|
|
309
|
+
* `FormationUsage` row makes the unbound redemption single-use exactly like the bound
|
|
310
|
+
* path: the next redemption of a `TotalUses:1` invite sees `count 1 >= 1` and is rejected.
|
|
311
|
+
*
|
|
312
|
+
* The strand is open (`'o'`) — an unbound responder-provisioned strand has no membership
|
|
313
|
+
* key — so the returned `memberPrivateKey` is null. `sAppId` is accepted for parity/future
|
|
314
|
+
* use; `redeemInvitation` does not currently thread it into the `Strand` row.
|
|
315
|
+
*
|
|
316
|
+
* A concurrent redemption of the same single-use invite still THROWS for the loser — the
|
|
317
|
+
* manager maps that to a clean protocol rejection (it never lets the dropped insert close
|
|
318
|
+
* the stream silently): the local write queue serializes the two writes, so the loser's
|
|
319
|
+
* seat check reads the winner's committed row against the cap and is refused as a named
|
|
320
|
+
* `InvitationExhaustedError`. A CROSS-node race is not a collision to retry at all — each
|
|
321
|
+
* redemption writes under its own `UsageStampId`, both rows land and survive convergence,
|
|
322
|
+
* and the cap can over-admit by the number of concurrent redeemers. That over-admission is
|
|
323
|
+
* the accepted trade (visible in the append-only record, reversible by owner-gated
|
|
324
|
+
* removal), chosen over a fought-over shared key whose failure mode was the silent loss of
|
|
325
|
+
* a consented join.
|
|
326
|
+
*/
|
|
327
|
+
async provisionAndRecord(params: {
|
|
328
|
+
token: string;
|
|
329
|
+
peerKey: string;
|
|
330
|
+
peerSignature: string;
|
|
331
|
+
usageStampId: string;
|
|
332
|
+
sAppId: string;
|
|
333
|
+
disclosure: string;
|
|
334
|
+
signal?: AbortSignal;
|
|
335
|
+
}): Promise<{ strandId: string; memberPrivateKey: string | null }> {
|
|
336
|
+
const { token, peerKey, peerSignature, usageStampId, disclosure, signal } = params;
|
|
337
|
+
if (signal?.aborted) {
|
|
338
|
+
throw new FormationAbortedError(token, 'redemption');
|
|
339
|
+
}
|
|
340
|
+
const strandId = mintStrandId();
|
|
341
|
+
const invite = await this.controlDatabase.queryFormationInvite(token);
|
|
342
|
+
const approval = await this.obtainApproval({
|
|
343
|
+
token, usageStampId, strandId, peerKey, disclosure,
|
|
344
|
+
validationUrl: invite?.validationUrl ?? null,
|
|
345
|
+
}, signal);
|
|
346
|
+
await this.controlDatabase.redeemInvitation({
|
|
347
|
+
token, strandId, peerKey, peerSignature, usageStampId, disclosure, signal,
|
|
348
|
+
totalUses: invite?.totalUses ?? null, ...approval,
|
|
349
|
+
});
|
|
350
|
+
log('Provisioned + recorded unbound strand: token=%s strand=%s', token, strandId);
|
|
351
|
+
return { strandId, memberPrivateKey: null };
|
|
352
|
+
}
|
|
353
|
+
}
|