@invisible-labs/sdk 0.6.0-devnet.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.
Files changed (54) hide show
  1. package/README.md +210 -0
  2. package/dist/chunk-7DSVN3MV.js +1145 -0
  3. package/dist/chunk-7DSVN3MV.js.map +1 -0
  4. package/dist/chunk-JNNVPZEU.js +599 -0
  5. package/dist/chunk-JNNVPZEU.js.map +1 -0
  6. package/dist/chunk-JRMKCE3S.js +50 -0
  7. package/dist/chunk-JRMKCE3S.js.map +1 -0
  8. package/dist/chunk-LBC7ALYO.js +3783 -0
  9. package/dist/chunk-LBC7ALYO.js.map +1 -0
  10. package/dist/chunk-NCY4FCYU.js +1911 -0
  11. package/dist/chunk-NCY4FCYU.js.map +1 -0
  12. package/dist/chunk-OHIM2YWU.js +126 -0
  13. package/dist/chunk-OHIM2YWU.js.map +1 -0
  14. package/dist/chunk-SZAYO2L5.js +123 -0
  15. package/dist/chunk-SZAYO2L5.js.map +1 -0
  16. package/dist/chunk-TQNNTV5F.js +17 -0
  17. package/dist/chunk-TQNNTV5F.js.map +1 -0
  18. package/dist/chunk-VR6T6OJS.js +24 -0
  19. package/dist/chunk-VR6T6OJS.js.map +1 -0
  20. package/dist/chunk-XUWBGET5.js +221 -0
  21. package/dist/chunk-XUWBGET5.js.map +1 -0
  22. package/dist/coordinator-7Y45MCCZ.js +4 -0
  23. package/dist/coordinator-7Y45MCCZ.js.map +1 -0
  24. package/dist/createSession-D3ym-Ira.d.ts +177 -0
  25. package/dist/dkgWorker.d.ts +2 -0
  26. package/dist/dkgWorker.js +61 -0
  27. package/dist/dkgWorker.js.map +1 -0
  28. package/dist/events.d.ts +54 -0
  29. package/dist/events.js +143 -0
  30. package/dist/events.js.map +1 -0
  31. package/dist/frostRuntime-JESDHN6O.js +4 -0
  32. package/dist/frostRuntime-JESDHN6O.js.map +1 -0
  33. package/dist/index.d.ts +499 -0
  34. package/dist/index.js +132 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/lp.d.ts +388 -0
  37. package/dist/lp.js +1711 -0
  38. package/dist/lp.js.map +1 -0
  39. package/dist/presets.d.ts +43 -0
  40. package/dist/presets.js +70 -0
  41. package/dist/presets.js.map +1 -0
  42. package/dist/session-DFuy54C-.d.ts +31 -0
  43. package/dist/stats.d.ts +47 -0
  44. package/dist/stats.js +21 -0
  45. package/dist/stats.js.map +1 -0
  46. package/dist/storage.d.ts +82 -0
  47. package/dist/storage.js +246 -0
  48. package/dist/storage.js.map +1 -0
  49. package/dist/types-ZhV7TIQY.d.ts +76 -0
  50. package/dist/types.generated-CHGSbmLp.d.ts +67 -0
  51. package/dist/user.d.ts +377 -0
  52. package/dist/user.js +1225 -0
  53. package/dist/user.js.map +1 -0
  54. package/package.json +82 -0
@@ -0,0 +1,3783 @@
1
+ import Ajv2020 from 'ajv/dist/2020.js';
2
+
3
+ // ../../protocol/src/coordinator/errors.ts
4
+ var ErrorCode = {
5
+ /** Decoding failed (JSON parse, missing field, wrong type). */
6
+ ERR_INVALID_PAYLOAD: "ERR_INVALID_PAYLOAD",
7
+ /** A message arrived in a state where it is not allowed. */
8
+ ERR_UNEXPECTED_TYPE: "ERR_UNEXPECTED_TYPE",
9
+ /** Unknown swap_id (coordinator-side) or no swap_id assigned yet (client-side). */
10
+ ERR_UNKNOWN_SWAP: "ERR_UNKNOWN_SWAP",
11
+ /** Spending policy violated (amount cap, fee floor, deadline, ...). */
12
+ ERR_POLICY_VIOLATION: "ERR_POLICY_VIOLATION",
13
+ /** Recovery code does not match the stored commitment. */
14
+ ERR_INVALID_RECOVERY_CODE: "ERR_INVALID_RECOVERY_CODE",
15
+ /** The swap is no longer refundable (settlement in progress / completed). */
16
+ ERR_SWAP_NOT_REFUNDABLE: "ERR_SWAP_NOT_REFUNDABLE",
17
+ /** FROST DKG package was rejected (malformed, wrong round, ...). */
18
+ ERR_DKG_FAILURE: "ERR_DKG_FAILURE",
19
+ /** Catch-all for unexpected coordinator-side failures. */
20
+ ERR_INTERNAL: "ERR_INTERNAL",
21
+ /** Deposit did not satisfy coordinator validation or funding policy. */
22
+ ERR_INVALID_DEPOSIT: "ERR_INVALID_DEPOSIT",
23
+ /** Autonomous payout/refund signing failed after the swap was accepted. */
24
+ ERR_SETTLEMENT_FAILED: "ERR_SETTLEMENT_FAILED"
25
+ };
26
+ var VALID_ERROR_CODES = new Set(Object.values(ErrorCode));
27
+ function isValidErrorCode(value) {
28
+ return typeof value === "string" && VALID_ERROR_CODES.has(value);
29
+ }
30
+ var CoordinatorError = class extends Error {
31
+ constructor(code, message, failed_req_id) {
32
+ super(`[${code}] ${message}`);
33
+ this.name = "CoordinatorError";
34
+ this.code = code;
35
+ this.failed_req_id = failed_req_id;
36
+ }
37
+ toPayload() {
38
+ const p = { code: this.code, message: this.message };
39
+ if (this.failed_req_id !== void 0) {
40
+ return { ...p, failed_req_id: this.failed_req_id };
41
+ }
42
+ return p;
43
+ }
44
+ };
45
+
46
+ // ../../protocol/src/coordinator/messages.ts
47
+ var EnvelopeKind = {
48
+ REQUEST: "request",
49
+ RESPONSE: "response",
50
+ PUSH: "push",
51
+ ERROR: "error"
52
+ };
53
+ var VALID_KINDS = new Set(Object.values(EnvelopeKind));
54
+ function isValidEnvelopeKind(value) {
55
+ return typeof value === "string" && VALID_KINDS.has(value);
56
+ }
57
+ var CoordinatorMessageType = {
58
+ // Phase A: Swap/contract setup — unified user swap-open + LP position-open (JW-372 step 8).
59
+ ContractRequest: "ContractRequest",
60
+ // Phase B: DKG (Alice ↔ TEE)
61
+ DkgInit: "DkgInit",
62
+ DkgRound1: "DkgRound1",
63
+ DkgRound2: "DkgRound2",
64
+ DkgComplete: "DkgComplete",
65
+ // Phase C: Delegation
66
+ DelegateShare: "DelegateShare",
67
+ DelegateAck: "DelegateAck",
68
+ // Phase D: Settlement lifecycle
69
+ DepositConfirmed: "DepositConfirmed",
70
+ // Unified read-only status poll (user swap status + LP position) — JW-372 step 8.
71
+ Sync: "Sync",
72
+ // Actor-safe wake-up hint; authoritative state remains the Sync response.
73
+ SyncRequired: "SyncRequired",
74
+ // LP-only liquidity top-up command — JW-372 step 8 consolidation.
75
+ Refill: "Refill",
76
+ PayoutExecuted: "PayoutExecuted",
77
+ // Phase E: Withdrawals - unified user Recovery Code withdrawal + LP withdrawal (JW-372 step 8).
78
+ WithdrawRequest: "WithdrawRequest",
79
+ // Intake ack for the user WithdrawRequest arm: the durable scheduler/matcher/
80
+ // executor/confirmer pipeline owns the terminal outcome, surfaced via Sync.
81
+ WithdrawAccepted: "WithdrawAccepted",
82
+ // User withdrawal execution/replay surface (push or WithdrawAccepted-flow response).
83
+ WithdrawExecuted: "WithdrawExecuted",
84
+ // LP owner-authenticated command surface.
85
+ LpCommand: "LpCommand",
86
+ // Errors
87
+ Error: "Error"
88
+ };
89
+ var VALID_TYPES = new Set(Object.values(CoordinatorMessageType));
90
+ function isValidMessageType(value) {
91
+ return typeof value === "string" && VALID_TYPES.has(value);
92
+ }
93
+ var KIND_BY_TYPE = {
94
+ // Pure request/response
95
+ ContractRequest: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
96
+ DkgInit: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
97
+ DkgRound1: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
98
+ DkgRound2: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
99
+ DelegateShare: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
100
+ Sync: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
101
+ Refill: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
102
+ // request (user refund + LP redeem) + response (the synchronous LP redeem result).
103
+ WithdrawRequest: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
104
+ LpCommand: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
105
+ // Response only: intake ack for WithdrawRequest.
106
+ WithdrawAccepted: /* @__PURE__ */ new Set([EnvelopeKind.RESPONSE]),
107
+ // Push only
108
+ DkgComplete: /* @__PURE__ */ new Set([EnvelopeKind.PUSH]),
109
+ DelegateAck: /* @__PURE__ */ new Set([EnvelopeKind.PUSH]),
110
+ DepositConfirmed: /* @__PURE__ */ new Set([EnvelopeKind.PUSH]),
111
+ PayoutExecuted: /* @__PURE__ */ new Set([EnvelopeKind.PUSH]),
112
+ SyncRequired: /* @__PURE__ */ new Set([EnvelopeKind.PUSH]),
113
+ // Mixed: response to WithdrawRequest replay OR push (auto-withdrawal triggered by deadline)
114
+ WithdrawExecuted: /* @__PURE__ */ new Set([EnvelopeKind.RESPONSE, EnvelopeKind.PUSH]),
115
+ // Errors
116
+ Error: /* @__PURE__ */ new Set([EnvelopeKind.ERROR])
117
+ };
118
+ function isAllowedKindForType(kind, type) {
119
+ return KIND_BY_TYPE[type].has(kind);
120
+ }
121
+ var MessageDirection = {
122
+ CLIENT_TO_COORDINATOR: "c2co",
123
+ COORDINATOR_TO_CLIENT: "co2c",
124
+ BIDIRECTIONAL: "both"
125
+ };
126
+ var DIRECTION_BY_TYPE = {
127
+ // Client-initiated
128
+ ContractRequest: MessageDirection.BIDIRECTIONAL,
129
+ // request c→co, response co→c
130
+ DkgInit: MessageDirection.BIDIRECTIONAL,
131
+ DkgRound1: MessageDirection.BIDIRECTIONAL,
132
+ DkgRound2: MessageDirection.BIDIRECTIONAL,
133
+ DelegateShare: MessageDirection.BIDIRECTIONAL,
134
+ Sync: MessageDirection.BIDIRECTIONAL,
135
+ Refill: MessageDirection.BIDIRECTIONAL,
136
+ WithdrawRequest: MessageDirection.BIDIRECTIONAL,
137
+ // user withdrawal c→co (ack via WithdrawAccepted); LP withdrawal c→co + co→c
138
+ LpCommand: MessageDirection.BIDIRECTIONAL,
139
+ // Coordinator-initiated (push) or coordinator response without matching request
140
+ DkgComplete: MessageDirection.COORDINATOR_TO_CLIENT,
141
+ DelegateAck: MessageDirection.COORDINATOR_TO_CLIENT,
142
+ DepositConfirmed: MessageDirection.COORDINATOR_TO_CLIENT,
143
+ PayoutExecuted: MessageDirection.COORDINATOR_TO_CLIENT,
144
+ SyncRequired: MessageDirection.COORDINATOR_TO_CLIENT,
145
+ WithdrawExecuted: MessageDirection.COORDINATOR_TO_CLIENT,
146
+ // both response & push are co→c
147
+ WithdrawAccepted: MessageDirection.COORDINATOR_TO_CLIENT,
148
+ // response ack to WithdrawRequest
149
+ // Errors can flow either way (validation can fail on either side)
150
+ Error: MessageDirection.BIDIRECTIONAL
151
+ };
152
+
153
+ // ../../protocol/src/coordinator/schema.generated.ts
154
+ var COORDINATOR_BINARY_FIELD_NAMES = [
155
+ "auth_public_key",
156
+ "coordinator_attestation_hash",
157
+ "delegated_share",
158
+ "joint_pubkey",
159
+ "nonce",
160
+ "package",
161
+ "recovery_code",
162
+ "recovery_commitment",
163
+ "redemption_commitment",
164
+ "refill_public_key",
165
+ "round2_for_alice",
166
+ "round2_for_lp",
167
+ "share",
168
+ "signature",
169
+ "sync_commitment",
170
+ "sync_secret"
171
+ ];
172
+ var COORDINATOR_SCHEMA_PAYLOAD_CATEGORIES = {
173
+ "request:ContractRequest": "request",
174
+ "response:ContractRequest": "response",
175
+ "request:DkgInit": "request",
176
+ "response:DkgInit": "response",
177
+ "request:DkgRound1": "request",
178
+ "response:DkgRound1": "response",
179
+ "request:DkgRound2": "request",
180
+ "response:DkgRound2": "response",
181
+ "push:DkgComplete": "push",
182
+ "request:DelegateShare": "request",
183
+ "response:DelegateShare": "response",
184
+ "push:DelegateAck": "push",
185
+ "push:DepositConfirmed": "push",
186
+ "request:Sync": "request",
187
+ "response:Sync": "response",
188
+ "push:SyncRequired": "push",
189
+ "request:Refill": "request",
190
+ "response:Refill": "response",
191
+ "push:PayoutExecuted": "push",
192
+ "request:WithdrawRequest": "request",
193
+ "response:WithdrawRequest": "response",
194
+ "response:WithdrawExecuted": "response",
195
+ "push:WithdrawExecuted": "push",
196
+ "response:WithdrawAccepted": "response",
197
+ "request:LpCommand": "request",
198
+ "response:LpCommand": "response",
199
+ "error:Error": "error"
200
+ };
201
+
202
+ // ../../protocol/src/coordinator/payloads.ts
203
+ var VALID_SWAP_STATES = /* @__PURE__ */ new Set([
204
+ "new",
205
+ "awaiting_dkg",
206
+ "awaiting_deposit",
207
+ "awaiting_delegation",
208
+ "ready_for_settlement",
209
+ "settling",
210
+ "completed",
211
+ "refunded",
212
+ "failed"
213
+ ]);
214
+ function isValidSwapState(value) {
215
+ return typeof value === "string" && VALID_SWAP_STATES.has(value);
216
+ }
217
+ function payloadCategory(kind, type) {
218
+ const key = `${kind}:${type}`;
219
+ return COORDINATOR_SCHEMA_PAYLOAD_CATEGORIES[key] ?? null;
220
+ }
221
+
222
+ // ../../protocol/src/coordinator/payout-windows.json
223
+ var payout_windows_default = [
224
+ {
225
+ id: "instant",
226
+ label: "Instant",
227
+ mode: "instant",
228
+ window_seconds: null
229
+ },
230
+ {
231
+ id: "1h",
232
+ label: "1h",
233
+ mode: "scheduled",
234
+ window_seconds: 3600
235
+ },
236
+ {
237
+ id: "24h",
238
+ label: "24h",
239
+ mode: "scheduled",
240
+ window_seconds: 86400
241
+ },
242
+ {
243
+ id: "7d",
244
+ label: "7 days",
245
+ mode: "scheduled",
246
+ window_seconds: 604800
247
+ }
248
+ ];
249
+
250
+ // ../../protocol/src/coordinator/payoutWindows.ts
251
+ var DEFAULT_PAYOUT_WINDOW_ID = "instant";
252
+ function isRecord(value) {
253
+ return value !== null && typeof value === "object" && !Array.isArray(value);
254
+ }
255
+ function validatePayoutWindowOptions(value) {
256
+ if (!Array.isArray(value) || value.length === 0) {
257
+ throw new Error("payout window manifest must be a non-empty array");
258
+ }
259
+ const ids = /* @__PURE__ */ new Set();
260
+ const scheduledWindowSeconds = /* @__PURE__ */ new Set();
261
+ return value.map((item, index) => {
262
+ if (!isRecord(item)) {
263
+ throw new Error(`payout window manifest entry ${index} must be an object`);
264
+ }
265
+ const { id, label, mode, window_seconds: windowSeconds } = item;
266
+ if (typeof id !== "string" || id.trim() === "") {
267
+ throw new Error(`payout window manifest entry ${index} has invalid id`);
268
+ }
269
+ if (ids.has(id)) {
270
+ throw new Error(`payout window manifest has duplicate id ${id}`);
271
+ }
272
+ ids.add(id);
273
+ if (typeof label !== "string" || label.trim() === "") {
274
+ throw new Error(`payout window manifest entry ${id} has invalid label`);
275
+ }
276
+ if (mode === "instant") {
277
+ if (windowSeconds !== null) {
278
+ throw new Error(`instant payout window ${id} must not define window_seconds`);
279
+ }
280
+ return { id, label, mode, window_seconds: null };
281
+ }
282
+ if (mode === "scheduled") {
283
+ if (typeof windowSeconds !== "number" || !Number.isInteger(windowSeconds) || windowSeconds <= 0) {
284
+ throw new Error(`scheduled payout window ${id} has invalid window_seconds`);
285
+ }
286
+ if (scheduledWindowSeconds.has(windowSeconds)) {
287
+ throw new Error(`duplicate scheduled payout window ${windowSeconds}s`);
288
+ }
289
+ scheduledWindowSeconds.add(windowSeconds);
290
+ return { id, label, mode, window_seconds: windowSeconds };
291
+ }
292
+ throw new Error(`payout window manifest entry ${id} has unsupported mode`);
293
+ });
294
+ }
295
+ var PAYOUT_WINDOW_OPTIONS = validatePayoutWindowOptions(payout_windows_default);
296
+ function payoutWindowOptionById(id) {
297
+ return PAYOUT_WINDOW_OPTIONS.find((option) => option.id === id);
298
+ }
299
+ function isPayoutWindowId(id) {
300
+ return payoutWindowOptionById(id) !== void 0;
301
+ }
302
+ function payoutWindowLabelForId(id) {
303
+ return payoutWindowOptionById(id)?.label ?? id;
304
+ }
305
+ function scheduledPayoutWindowOptions() {
306
+ return PAYOUT_WINDOW_OPTIONS.filter(
307
+ (option) => option.mode === "scheduled" && option.window_seconds !== null
308
+ );
309
+ }
310
+ function scheduledPayoutWindowSeconds() {
311
+ return scheduledPayoutWindowOptions().map((option) => option.window_seconds);
312
+ }
313
+
314
+ // ../../protocol/src/coordinator/state.ts
315
+ var INITIAL_STATE = "new";
316
+ var TERMINAL_STATES = /* @__PURE__ */ new Set([
317
+ "completed",
318
+ "refunded",
319
+ "failed"
320
+ ]);
321
+ var TRANSITIONS = [
322
+ // --- new ---
323
+ // No swap_id yet. ContractRequest response assigns the swap_id and opens
324
+ // the DKG phase.
325
+ ["new", "response", "ContractRequest", "awaiting_dkg"],
326
+ // --- awaiting_dkg ---
327
+ // DKG rounds happen over several request/response exchanges. None of
328
+ // them move the phase forward by themselves; only the DkgComplete push
329
+ // transitions to awaiting_deposit.
330
+ ["awaiting_dkg", "response", "DkgInit", "awaiting_dkg"],
331
+ ["awaiting_dkg", "response", "DkgRound1", "awaiting_dkg"],
332
+ ["awaiting_dkg", "response", "DkgRound2", "awaiting_dkg"],
333
+ ["awaiting_dkg", "push", "DkgComplete", "awaiting_deposit"],
334
+ // --- awaiting_deposit ---
335
+ // DKG is done; the client delegates recovery/signing material before showing
336
+ // the fundable address. DelegateShare response only acknowledges request
337
+ // receipt. The client must observe DelegateAck before accepting a deposit
338
+ // confirmation as the next protocol step.
339
+ ["awaiting_deposit", "response", "DelegateShare", "awaiting_delegate_ack"],
340
+ ["awaiting_delegate_ack", "push", "DelegateAck", "awaiting_deposit"],
341
+ ["awaiting_deposit", "push", "DepositConfirmed", "ready_for_settlement"],
342
+ // --- delegation ---
343
+ // Older or restored sessions can still report awaiting_delegation when a
344
+ // deposit was observed before delegation. DelegateShare response is only
345
+ // request receipt; DelegateAck is the policy-bound acknowledgement.
346
+ ["awaiting_delegation", "response", "DelegateShare", "awaiting_delegate_ack_after_deposit"],
347
+ ["awaiting_delegate_ack_after_deposit", "push", "DelegateAck", "ready_for_settlement"],
348
+ ["awaiting_delegation", "push", "DelegateAck", "ready_for_settlement"],
349
+ // --- ready_for_settlement ---
350
+ // Coordinator can start executing fragments at any time. Each
351
+ // executed fragment is pushed; we move to `settling` on the first one.
352
+ // Completion is triggered by a PayoutExecuted push with `final: true`
353
+ // (handled as a special case in nextState, not in this table).
354
+ ["ready_for_settlement", "push", "PayoutExecuted", "settling"],
355
+ // --- settling ---
356
+ // More fragments may arrive. `settling` stays `settling` until the
357
+ // coordinator pushes a PayoutExecuted with `final: true` (see nextState).
358
+ ["settling", "push", "PayoutExecuted", "settling"],
359
+ // --- Withdrawal path (can be triggered from several states) ---
360
+ // WithdrawExecuted as a response to WithdrawRequest replay, OR as an
361
+ // unsolicited push (auto_deadline). Either way, terminal state is `refunded`.
362
+ ["awaiting_deposit", "response", "WithdrawExecuted", "refunded"],
363
+ ["awaiting_delegate_ack", "response", "WithdrawExecuted", "refunded"],
364
+ ["awaiting_delegation", "response", "WithdrawExecuted", "refunded"],
365
+ ["awaiting_delegate_ack_after_deposit", "response", "WithdrawExecuted", "refunded"],
366
+ ["ready_for_settlement", "response", "WithdrawExecuted", "refunded"],
367
+ ["settling", "response", "WithdrawExecuted", "refunded"],
368
+ ["awaiting_deposit", "push", "WithdrawExecuted", "refunded"],
369
+ ["awaiting_delegate_ack", "push", "WithdrawExecuted", "refunded"],
370
+ ["awaiting_delegation", "push", "WithdrawExecuted", "refunded"],
371
+ ["awaiting_delegate_ack_after_deposit", "push", "WithdrawExecuted", "refunded"],
372
+ ["ready_for_settlement", "push", "WithdrawExecuted", "refunded"],
373
+ ["settling", "push", "WithdrawExecuted", "refunded"]
374
+ ];
375
+ var transitionMap = /* @__PURE__ */ new Map();
376
+ for (const [from, kind, type, to] of TRANSITIONS) {
377
+ transitionMap.set(triggerKey(from, kind, type), to);
378
+ }
379
+ function triggerKey(state, kind, type) {
380
+ return `${state}|${kind}|${type}`;
381
+ }
382
+ function nextState(current, kind, type, hint) {
383
+ if (TERMINAL_STATES.has(current)) return null;
384
+ if (kind === "push" && type === "PayoutExecuted" && hint?.payoutFinal === true && (current === "ready_for_settlement" || current === "settling")) {
385
+ return "completed";
386
+ }
387
+ return transitionMap.get(triggerKey(current, kind, type)) ?? null;
388
+ }
389
+ function isAllowedInState(current, kind, type) {
390
+ if (TERMINAL_STATES.has(current)) {
391
+ return type === "Sync" && (kind === "request" || kind === "response");
392
+ }
393
+ if (type === "Error") return true;
394
+ if (current === "new") {
395
+ return type === "ContractRequest" && (kind === "request" || kind === "response");
396
+ }
397
+ if (type === "Sync") return true;
398
+ if (kind === "request") {
399
+ if (type === "WithdrawRequest") {
400
+ return current === "awaiting_deposit" || current === "awaiting_delegate_ack" || current === "awaiting_delegation" || current === "awaiting_delegate_ack_after_deposit" || current === "ready_for_settlement" || current === "settling";
401
+ }
402
+ if (current === "awaiting_dkg") {
403
+ return type === "DkgInit" || type === "DkgRound1" || type === "DkgRound2";
404
+ }
405
+ if (current === "awaiting_deposit" || current === "awaiting_delegation") {
406
+ return type === "DelegateShare";
407
+ }
408
+ return false;
409
+ }
410
+ return transitionMap.has(triggerKey(current, kind, type));
411
+ }
412
+
413
+ // ../../protocol/src/coordinator/coordinator-envelope.schema.json
414
+ var coordinator_envelope_schema_default = {
415
+ $schema: "https://json-schema.org/draft/2020-12/schema",
416
+ $id: "https://invisible.local/schemas/coordinator-envelope.v2.schema.json",
417
+ title: "Invisible Coordinator Envelope v2",
418
+ type: "object",
419
+ additionalProperties: false,
420
+ required: ["version", "kind", "type", "swap_id", "req_id", "timestamp_ms", "payload"],
421
+ properties: {
422
+ version: {
423
+ const: 2
424
+ },
425
+ kind: {
426
+ enum: ["request", "response", "push", "error"]
427
+ },
428
+ type: {
429
+ enum: [
430
+ "ContractRequest",
431
+ "DkgInit",
432
+ "DkgRound1",
433
+ "DkgRound2",
434
+ "DkgComplete",
435
+ "DelegateShare",
436
+ "DelegateAck",
437
+ "DepositConfirmed",
438
+ "Sync",
439
+ "SyncRequired",
440
+ "Refill",
441
+ "PayoutExecuted",
442
+ "WithdrawRequest",
443
+ "WithdrawExecuted",
444
+ "WithdrawAccepted",
445
+ "LpCommand",
446
+ "Error"
447
+ ]
448
+ },
449
+ swap_id: {
450
+ anyOf: [
451
+ {
452
+ type: "null"
453
+ },
454
+ {
455
+ type: "string",
456
+ minLength: 1,
457
+ maxLength: 128
458
+ }
459
+ ]
460
+ },
461
+ req_id: {
462
+ $ref: "#/$defs/u32"
463
+ },
464
+ timestamp_ms: {
465
+ $ref: "#/$defs/safeUInt"
466
+ },
467
+ payload: {
468
+ type: "object"
469
+ }
470
+ },
471
+ allOf: [
472
+ {
473
+ oneOf: [
474
+ {
475
+ title: "ContractRequest request",
476
+ properties: {
477
+ kind: {
478
+ const: "request"
479
+ },
480
+ type: {
481
+ const: "ContractRequest"
482
+ },
483
+ payload: {
484
+ $ref: "#/$defs/contractRequestRequest"
485
+ }
486
+ }
487
+ },
488
+ {
489
+ title: "ContractRequest response",
490
+ properties: {
491
+ kind: {
492
+ const: "response"
493
+ },
494
+ type: {
495
+ const: "ContractRequest"
496
+ },
497
+ payload: {
498
+ $ref: "#/$defs/contractRequestResponse"
499
+ }
500
+ }
501
+ },
502
+ {
503
+ title: "DkgInit request",
504
+ properties: {
505
+ kind: {
506
+ const: "request"
507
+ },
508
+ type: {
509
+ const: "DkgInit"
510
+ },
511
+ payload: {
512
+ $ref: "#/$defs/emptyObject"
513
+ }
514
+ }
515
+ },
516
+ {
517
+ title: "DkgInit response",
518
+ properties: {
519
+ kind: {
520
+ const: "response"
521
+ },
522
+ type: {
523
+ const: "DkgInit"
524
+ },
525
+ payload: {
526
+ $ref: "#/$defs/dkgInitResponse"
527
+ }
528
+ }
529
+ },
530
+ {
531
+ title: "DkgRound1 request",
532
+ properties: {
533
+ kind: {
534
+ const: "request"
535
+ },
536
+ type: {
537
+ const: "DkgRound1"
538
+ },
539
+ payload: {
540
+ $ref: "#/$defs/dkgRoundPackage"
541
+ }
542
+ }
543
+ },
544
+ {
545
+ title: "DkgRound1 response",
546
+ properties: {
547
+ kind: {
548
+ const: "response"
549
+ },
550
+ type: {
551
+ const: "DkgRound1"
552
+ },
553
+ payload: {
554
+ $ref: "#/$defs/dkgRound1Response"
555
+ }
556
+ }
557
+ },
558
+ {
559
+ title: "DkgRound2 request",
560
+ properties: {
561
+ kind: {
562
+ const: "request"
563
+ },
564
+ type: {
565
+ const: "DkgRound2"
566
+ },
567
+ payload: {
568
+ $ref: "#/$defs/dkgRoundPackage"
569
+ }
570
+ }
571
+ },
572
+ {
573
+ title: "DkgRound2 response",
574
+ properties: {
575
+ kind: {
576
+ const: "response"
577
+ },
578
+ type: {
579
+ const: "DkgRound2"
580
+ },
581
+ payload: {
582
+ $ref: "#/$defs/dkgRoundPackage"
583
+ }
584
+ }
585
+ },
586
+ {
587
+ title: "DkgComplete push",
588
+ properties: {
589
+ kind: {
590
+ const: "push"
591
+ },
592
+ type: {
593
+ const: "DkgComplete"
594
+ },
595
+ payload: {
596
+ $ref: "#/$defs/dkgComplete"
597
+ }
598
+ }
599
+ },
600
+ {
601
+ title: "DelegateShare request",
602
+ properties: {
603
+ kind: {
604
+ const: "request"
605
+ },
606
+ type: {
607
+ const: "DelegateShare"
608
+ },
609
+ payload: {
610
+ $ref: "#/$defs/delegateShare"
611
+ }
612
+ }
613
+ },
614
+ {
615
+ title: "DelegateShare response",
616
+ properties: {
617
+ kind: {
618
+ const: "response"
619
+ },
620
+ type: {
621
+ const: "DelegateShare"
622
+ },
623
+ payload: {
624
+ $ref: "#/$defs/delegateShareResponse"
625
+ }
626
+ }
627
+ },
628
+ {
629
+ title: "DelegateAck push",
630
+ properties: {
631
+ kind: {
632
+ const: "push"
633
+ },
634
+ type: {
635
+ const: "DelegateAck"
636
+ },
637
+ payload: {
638
+ $ref: "#/$defs/delegateAck"
639
+ }
640
+ }
641
+ },
642
+ {
643
+ title: "DepositConfirmed push",
644
+ properties: {
645
+ kind: {
646
+ const: "push"
647
+ },
648
+ type: {
649
+ const: "DepositConfirmed"
650
+ },
651
+ payload: {
652
+ $ref: "#/$defs/depositConfirmed"
653
+ }
654
+ }
655
+ },
656
+ {
657
+ title: "Sync request",
658
+ properties: {
659
+ kind: {
660
+ const: "request"
661
+ },
662
+ type: {
663
+ const: "Sync"
664
+ },
665
+ payload: {
666
+ $ref: "#/$defs/syncRequest"
667
+ }
668
+ }
669
+ },
670
+ {
671
+ title: "Sync response",
672
+ properties: {
673
+ kind: {
674
+ const: "response"
675
+ },
676
+ type: {
677
+ const: "Sync"
678
+ },
679
+ payload: {
680
+ $ref: "#/$defs/syncResponse"
681
+ }
682
+ }
683
+ },
684
+ {
685
+ title: "SyncRequired push",
686
+ properties: {
687
+ kind: {
688
+ const: "push"
689
+ },
690
+ type: {
691
+ const: "SyncRequired"
692
+ },
693
+ payload: {
694
+ $ref: "#/$defs/syncRequired"
695
+ }
696
+ }
697
+ },
698
+ {
699
+ title: "Refill request",
700
+ properties: {
701
+ kind: {
702
+ const: "request"
703
+ },
704
+ type: {
705
+ const: "Refill"
706
+ },
707
+ swap_id: {
708
+ type: "null"
709
+ },
710
+ payload: {
711
+ $ref: "#/$defs/refillRequest"
712
+ }
713
+ }
714
+ },
715
+ {
716
+ title: "Refill response",
717
+ properties: {
718
+ kind: {
719
+ const: "response"
720
+ },
721
+ type: {
722
+ const: "Refill"
723
+ },
724
+ swap_id: {
725
+ type: "null"
726
+ },
727
+ payload: {
728
+ $ref: "#/$defs/lpPositionResponse"
729
+ }
730
+ }
731
+ },
732
+ {
733
+ title: "PayoutExecuted push",
734
+ properties: {
735
+ kind: {
736
+ const: "push"
737
+ },
738
+ type: {
739
+ const: "PayoutExecuted"
740
+ },
741
+ payload: {
742
+ $ref: "#/$defs/executedFragment"
743
+ }
744
+ }
745
+ },
746
+ {
747
+ title: "WithdrawRequest request",
748
+ properties: {
749
+ kind: {
750
+ const: "request"
751
+ },
752
+ type: {
753
+ const: "WithdrawRequest"
754
+ },
755
+ payload: {
756
+ $ref: "#/$defs/withdrawRequestRequest"
757
+ }
758
+ }
759
+ },
760
+ {
761
+ title: "WithdrawRequest response",
762
+ $comment: "Only the LP withdrawal arm gets a synchronous WithdrawRequest-typed response (the withdrawal-execution case). The user arm is acknowledged via WithdrawAccepted; replayed or terminal recovery outcomes use WithdrawExecuted / withdraw_execution.",
763
+ properties: {
764
+ kind: {
765
+ const: "response"
766
+ },
767
+ type: {
768
+ const: "WithdrawRequest"
769
+ },
770
+ swap_id: {
771
+ type: "null"
772
+ },
773
+ payload: {
774
+ $ref: "#/$defs/lpWithdrawalExecutionResponse"
775
+ }
776
+ }
777
+ },
778
+ {
779
+ title: "WithdrawExecuted response",
780
+ properties: {
781
+ kind: {
782
+ const: "response"
783
+ },
784
+ type: {
785
+ const: "WithdrawExecuted"
786
+ },
787
+ payload: {
788
+ $ref: "#/$defs/withdrawExecuted"
789
+ }
790
+ }
791
+ },
792
+ {
793
+ title: "WithdrawExecuted push",
794
+ properties: {
795
+ kind: {
796
+ const: "push"
797
+ },
798
+ type: {
799
+ const: "WithdrawExecuted"
800
+ },
801
+ payload: {
802
+ $ref: "#/$defs/withdrawExecuted"
803
+ }
804
+ }
805
+ },
806
+ {
807
+ title: "WithdrawAccepted response",
808
+ properties: {
809
+ kind: {
810
+ const: "response"
811
+ },
812
+ type: {
813
+ const: "WithdrawAccepted"
814
+ },
815
+ payload: {
816
+ $ref: "#/$defs/withdrawAccepted"
817
+ }
818
+ }
819
+ },
820
+ {
821
+ title: "LpCommand request",
822
+ properties: {
823
+ kind: {
824
+ const: "request"
825
+ },
826
+ type: {
827
+ const: "LpCommand"
828
+ },
829
+ swap_id: {
830
+ type: "null"
831
+ },
832
+ payload: {
833
+ $ref: "#/$defs/lpSignedCommand"
834
+ }
835
+ },
836
+ allOf: [
837
+ {
838
+ $comment: "Verb actions are routed under their own envelope type (e.g. Sync). The LpCommand envelope only carries the LP lifecycle actions.",
839
+ type: "object",
840
+ properties: {
841
+ payload: {
842
+ type: "object",
843
+ properties: {
844
+ command: {
845
+ type: "object",
846
+ properties: {
847
+ action: {
848
+ enum: [
849
+ "dkg_init",
850
+ "dkg_round1",
851
+ "dkg_round2",
852
+ "dkg_finalize",
853
+ "funding_plan",
854
+ "funding_confirmation",
855
+ "make_available",
856
+ "reconcile_funding",
857
+ "prepare_top_up",
858
+ "top_up_status"
859
+ ]
860
+ }
861
+ }
862
+ }
863
+ }
864
+ }
865
+ }
866
+ }
867
+ ]
868
+ },
869
+ {
870
+ title: "LpCommand response",
871
+ properties: {
872
+ kind: {
873
+ const: "response"
874
+ },
875
+ type: {
876
+ const: "LpCommand"
877
+ },
878
+ swap_id: {
879
+ type: "null"
880
+ },
881
+ payload: {
882
+ $ref: "#/$defs/lpCommandLifecycleResponse"
883
+ }
884
+ }
885
+ },
886
+ {
887
+ title: "Error envelope",
888
+ properties: {
889
+ kind: {
890
+ const: "error"
891
+ },
892
+ type: {
893
+ const: "Error"
894
+ },
895
+ payload: {
896
+ $ref: "#/$defs/errorPayload"
897
+ }
898
+ }
899
+ }
900
+ ]
901
+ }
902
+ ],
903
+ $defs: {
904
+ safeUInt: {
905
+ type: "integer",
906
+ minimum: 0,
907
+ maximum: 9007199254740991
908
+ },
909
+ positiveSafeUInt: {
910
+ type: "integer",
911
+ minimum: 1,
912
+ maximum: 9007199254740991
913
+ },
914
+ entryAmountLamports: {
915
+ type: "integer",
916
+ minimum: 4e8,
917
+ maximum: 1e11
918
+ },
919
+ u32: {
920
+ type: "integer",
921
+ minimum: 0,
922
+ maximum: 4294967295
923
+ },
924
+ lpShardIndex: {
925
+ type: "integer",
926
+ minimum: 0,
927
+ maximum: 299
928
+ },
929
+ lpShardCount: {
930
+ type: "integer",
931
+ minimum: 0,
932
+ maximum: 300
933
+ },
934
+ positiveU32: {
935
+ type: "integer",
936
+ minimum: 1,
937
+ maximum: 4294967295
938
+ },
939
+ string: {
940
+ type: "string",
941
+ minLength: 1
942
+ },
943
+ base64urlBytes: {
944
+ type: "string",
945
+ minLength: 2,
946
+ pattern: "^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2}|[A-Za-z0-9_-]{3})?$"
947
+ },
948
+ base64url32Bytes: {
949
+ type: "string",
950
+ minLength: 43,
951
+ maxLength: 43,
952
+ pattern: "^[A-Za-z0-9_-]{43}$"
953
+ },
954
+ emptyObject: {
955
+ type: "object",
956
+ additionalProperties: false,
957
+ properties: {}
958
+ },
959
+ syncRequest: {
960
+ oneOf: [
961
+ {
962
+ $ref: "#/$defs/syncUserRequest"
963
+ },
964
+ {
965
+ $ref: "#/$defs/syncLpRequest"
966
+ }
967
+ ]
968
+ },
969
+ syncUserRequest: {
970
+ type: "object",
971
+ additionalProperties: false,
972
+ required: ["actor"],
973
+ properties: {
974
+ actor: {
975
+ const: "user"
976
+ },
977
+ sync_secret: {
978
+ $ref: "#/$defs/base64url32Bytes"
979
+ }
980
+ }
981
+ },
982
+ syncLpRequest: {
983
+ type: "object",
984
+ additionalProperties: false,
985
+ required: ["actor", "command", "signature"],
986
+ properties: {
987
+ actor: {
988
+ const: "lp"
989
+ },
990
+ command: {
991
+ $ref: "#/$defs/lpCommand"
992
+ },
993
+ signature: {
994
+ $ref: "#/$defs/base64url64Bytes"
995
+ }
996
+ },
997
+ allOf: [
998
+ {
999
+ $comment: "The Sync verb carries the LP position_get command.",
1000
+ type: "object",
1001
+ properties: {
1002
+ command: {
1003
+ type: "object",
1004
+ properties: {
1005
+ action: {
1006
+ const: "position_get"
1007
+ }
1008
+ }
1009
+ }
1010
+ }
1011
+ }
1012
+ ]
1013
+ },
1014
+ refillRequest: {
1015
+ $comment: "The Refill verb is LP-only: an LP-signed refill command. There is no user/protocol actor.",
1016
+ type: "object",
1017
+ additionalProperties: false,
1018
+ required: ["actor", "command", "signature"],
1019
+ properties: {
1020
+ actor: {
1021
+ const: "lp"
1022
+ },
1023
+ command: {
1024
+ $ref: "#/$defs/lpCommand"
1025
+ },
1026
+ signature: {
1027
+ $ref: "#/$defs/base64url64Bytes"
1028
+ }
1029
+ },
1030
+ allOf: [
1031
+ {
1032
+ type: "object",
1033
+ properties: {
1034
+ command: {
1035
+ type: "object",
1036
+ properties: {
1037
+ action: {
1038
+ const: "refill"
1039
+ }
1040
+ }
1041
+ }
1042
+ }
1043
+ }
1044
+ ]
1045
+ },
1046
+ swapRequest: {
1047
+ $comment: "The user arm of the ContractRequest verb: open a swap/contract on the Noise session.",
1048
+ type: "object",
1049
+ additionalProperties: false,
1050
+ required: ["actor", "amount_lamports", "payout_spec", "sync_commitment"],
1051
+ properties: {
1052
+ actor: {
1053
+ const: "user"
1054
+ },
1055
+ amount_lamports: {
1056
+ $ref: "#/$defs/entryAmountLamports"
1057
+ },
1058
+ payout_deadline_ms: {
1059
+ description: "Optional legacy absolute request deadline. Scheduled payout_spec.window_seconds selects the supported scheduled window.",
1060
+ $ref: "#/$defs/positiveSafeUInt"
1061
+ },
1062
+ payout_spec: {
1063
+ $ref: "#/$defs/payoutSpec"
1064
+ },
1065
+ sync_commitment: {
1066
+ $ref: "#/$defs/base64url32Bytes"
1067
+ }
1068
+ }
1069
+ },
1070
+ contractRequestRequest: {
1071
+ oneOf: [
1072
+ {
1073
+ $ref: "#/$defs/swapRequest"
1074
+ },
1075
+ {
1076
+ $ref: "#/$defs/contractRequestLpRequest"
1077
+ }
1078
+ ]
1079
+ },
1080
+ contractRequestLpRequest: {
1081
+ $comment: "The LP arm of the ContractRequest verb: an LP-signed position_open command.",
1082
+ type: "object",
1083
+ additionalProperties: false,
1084
+ required: ["actor", "command", "signature"],
1085
+ properties: {
1086
+ actor: {
1087
+ const: "lp"
1088
+ },
1089
+ command: {
1090
+ $ref: "#/$defs/lpCommand"
1091
+ },
1092
+ signature: {
1093
+ $ref: "#/$defs/base64url64Bytes"
1094
+ }
1095
+ },
1096
+ allOf: [
1097
+ {
1098
+ type: "object",
1099
+ properties: {
1100
+ command: {
1101
+ type: "object",
1102
+ properties: {
1103
+ action: {
1104
+ const: "position_open"
1105
+ }
1106
+ }
1107
+ }
1108
+ }
1109
+ }
1110
+ ]
1111
+ },
1112
+ contractRequestResponse: {
1113
+ oneOf: [
1114
+ {
1115
+ $ref: "#/$defs/swapRequestResponse"
1116
+ },
1117
+ {
1118
+ $ref: "#/$defs/lpPositionResponse"
1119
+ }
1120
+ ]
1121
+ },
1122
+ payoutSpec: {
1123
+ oneOf: [
1124
+ {
1125
+ $ref: "#/$defs/instantPayoutSpec"
1126
+ },
1127
+ {
1128
+ $ref: "#/$defs/scheduledPayoutSpec"
1129
+ }
1130
+ ]
1131
+ },
1132
+ instantPayoutSpec: {
1133
+ type: "object",
1134
+ additionalProperties: false,
1135
+ required: ["mode", "destination_address"],
1136
+ properties: {
1137
+ mode: {
1138
+ const: "instant"
1139
+ },
1140
+ destination_address: {
1141
+ $ref: "#/$defs/string"
1142
+ }
1143
+ }
1144
+ },
1145
+ scheduledPayoutSpec: {
1146
+ type: "object",
1147
+ additionalProperties: false,
1148
+ required: ["mode", "num_fragments", "window_seconds", "destination_addresses"],
1149
+ properties: {
1150
+ mode: {
1151
+ const: "scheduled"
1152
+ },
1153
+ num_fragments: {
1154
+ type: "integer",
1155
+ minimum: 1,
1156
+ maximum: 1
1157
+ },
1158
+ window_seconds: {
1159
+ $ref: "#/$defs/positiveSafeUInt"
1160
+ },
1161
+ destination_addresses: {
1162
+ type: "array",
1163
+ minItems: 1,
1164
+ maxItems: 1,
1165
+ items: {
1166
+ $ref: "#/$defs/string"
1167
+ }
1168
+ }
1169
+ }
1170
+ },
1171
+ spendingPolicyDestination: {
1172
+ type: "object",
1173
+ additionalProperties: false,
1174
+ required: ["destination_address"],
1175
+ properties: {
1176
+ destination_address: {
1177
+ $ref: "#/$defs/string"
1178
+ }
1179
+ }
1180
+ },
1181
+ spendingPolicySnapshot: {
1182
+ type: "object",
1183
+ additionalProperties: false,
1184
+ required: [
1185
+ "asset",
1186
+ "entry_amount_lamports",
1187
+ "total_committed_lamports",
1188
+ "payout_deadline_ms",
1189
+ "payout_schedule",
1190
+ "fragment_count",
1191
+ "payout_mode",
1192
+ "matching_mode",
1193
+ "fragmentation_allowed",
1194
+ "min_fee_bps",
1195
+ "fee_bps",
1196
+ "fee_lamports",
1197
+ "net_payout_lamports"
1198
+ ],
1199
+ allOf: [
1200
+ {
1201
+ oneOf: [
1202
+ {
1203
+ properties: {
1204
+ payout_mode: {
1205
+ const: "instant"
1206
+ }
1207
+ }
1208
+ },
1209
+ {
1210
+ required: ["payout_window_ms"],
1211
+ properties: {
1212
+ payout_mode: {
1213
+ const: "scheduled"
1214
+ },
1215
+ payout_window_ms: {
1216
+ $ref: "#/$defs/positiveSafeUInt"
1217
+ }
1218
+ }
1219
+ }
1220
+ ]
1221
+ }
1222
+ ],
1223
+ properties: {
1224
+ asset: {
1225
+ const: "SOL"
1226
+ },
1227
+ entry_amount_lamports: {
1228
+ $ref: "#/$defs/positiveSafeUInt"
1229
+ },
1230
+ total_committed_lamports: {
1231
+ $ref: "#/$defs/positiveSafeUInt"
1232
+ },
1233
+ payout_deadline_ms: {
1234
+ description: "Pre-reservation policy deadline. For scheduled payout_mode, do not treat this as the post-reservation execution SLA; payout_window_ms is the selected window.",
1235
+ $ref: "#/$defs/positiveSafeUInt"
1236
+ },
1237
+ payout_window_ms: {
1238
+ description: "Selected scheduled payout window in milliseconds. For scheduled payout_mode, this is the authoritative maximum execution window after liquidity reservation; the exact payout_due_at_ms stays internal.",
1239
+ $ref: "#/$defs/positiveSafeUInt"
1240
+ },
1241
+ payout_schedule: {
1242
+ type: "array",
1243
+ minItems: 1,
1244
+ maxItems: 1,
1245
+ items: {
1246
+ $ref: "#/$defs/spendingPolicyDestination"
1247
+ }
1248
+ },
1249
+ fragment_count: {
1250
+ type: "integer",
1251
+ minimum: 1,
1252
+ maximum: 1
1253
+ },
1254
+ payout_mode: {
1255
+ enum: ["instant", "scheduled"]
1256
+ },
1257
+ matching_mode: {
1258
+ const: "exact_1_to_1"
1259
+ },
1260
+ fragmentation_allowed: {
1261
+ const: "payout_side_only"
1262
+ },
1263
+ min_fee_bps: {
1264
+ type: "integer",
1265
+ minimum: 0,
1266
+ maximum: 1e4
1267
+ },
1268
+ fee_bps: {
1269
+ type: "integer",
1270
+ minimum: 0,
1271
+ maximum: 1e4
1272
+ },
1273
+ fee_lamports: {
1274
+ $ref: "#/$defs/safeUInt"
1275
+ },
1276
+ net_payout_lamports: {
1277
+ $ref: "#/$defs/safeUInt"
1278
+ }
1279
+ }
1280
+ },
1281
+ swapRequestResponse: {
1282
+ type: "object",
1283
+ additionalProperties: false,
1284
+ required: ["swap_id", "accepted", "policy_snapshot"],
1285
+ properties: {
1286
+ swap_id: {
1287
+ $ref: "#/$defs/string"
1288
+ },
1289
+ accepted: {
1290
+ const: true
1291
+ },
1292
+ policy_snapshot: {
1293
+ $ref: "#/$defs/spendingPolicySnapshot"
1294
+ }
1295
+ }
1296
+ },
1297
+ dkgInitResponse: {
1298
+ type: "object",
1299
+ additionalProperties: false,
1300
+ required: ["ready"],
1301
+ properties: {
1302
+ ready: {
1303
+ const: true
1304
+ }
1305
+ }
1306
+ },
1307
+ dkgRoundPackage: {
1308
+ type: "object",
1309
+ additionalProperties: false,
1310
+ required: ["package"],
1311
+ properties: {
1312
+ package: {
1313
+ $ref: "#/$defs/base64urlBytes"
1314
+ }
1315
+ }
1316
+ },
1317
+ dkgRound1Response: {
1318
+ type: "object",
1319
+ additionalProperties: false,
1320
+ required: ["package", "round2_for_alice"],
1321
+ properties: {
1322
+ package: {
1323
+ $ref: "#/$defs/base64urlBytes"
1324
+ },
1325
+ round2_for_alice: {
1326
+ $ref: "#/$defs/base64urlBytes"
1327
+ }
1328
+ }
1329
+ },
1330
+ dkgComplete: {
1331
+ type: "object",
1332
+ additionalProperties: false,
1333
+ required: ["joint_pubkey", "deposit_expires_at_ms"],
1334
+ properties: {
1335
+ joint_pubkey: {
1336
+ $ref: "#/$defs/base64url32Bytes"
1337
+ },
1338
+ deposit_expires_at_ms: {
1339
+ $ref: "#/$defs/safeUInt"
1340
+ }
1341
+ }
1342
+ },
1343
+ delegateShare: {
1344
+ type: "object",
1345
+ additionalProperties: false,
1346
+ required: ["share", "recovery_commitment"],
1347
+ properties: {
1348
+ share: {
1349
+ $ref: "#/$defs/base64url32Bytes"
1350
+ },
1351
+ recovery_commitment: {
1352
+ $ref: "#/$defs/base64url32Bytes"
1353
+ }
1354
+ }
1355
+ },
1356
+ delegateShareResponse: {
1357
+ type: "object",
1358
+ additionalProperties: false,
1359
+ required: ["delegated_at_ms"],
1360
+ properties: {
1361
+ delegated_at_ms: {
1362
+ $ref: "#/$defs/positiveSafeUInt"
1363
+ }
1364
+ }
1365
+ },
1366
+ delegateAck: {
1367
+ type: "object",
1368
+ additionalProperties: false,
1369
+ required: ["delegated_at_ms", "spending_policy"],
1370
+ properties: {
1371
+ delegated_at_ms: {
1372
+ $ref: "#/$defs/positiveSafeUInt"
1373
+ },
1374
+ spending_policy: {
1375
+ $ref: "#/$defs/spendingPolicySnapshot"
1376
+ }
1377
+ }
1378
+ },
1379
+ depositConfirmed: {
1380
+ type: "object",
1381
+ additionalProperties: false,
1382
+ required: ["tx_signature", "amount_lamports", "observed_at_ms"],
1383
+ properties: {
1384
+ tx_signature: {
1385
+ $ref: "#/$defs/string"
1386
+ },
1387
+ amount_lamports: {
1388
+ $ref: "#/$defs/positiveSafeUInt"
1389
+ },
1390
+ observed_at_ms: {
1391
+ $ref: "#/$defs/positiveSafeUInt"
1392
+ },
1393
+ origin_address: {
1394
+ $ref: "#/$defs/string"
1395
+ }
1396
+ }
1397
+ },
1398
+ plannedFragment: {
1399
+ type: "object",
1400
+ additionalProperties: false,
1401
+ required: ["index", "amount_lamports", "scheduled_at_ms", "destination_address"],
1402
+ properties: {
1403
+ index: {
1404
+ $ref: "#/$defs/u32"
1405
+ },
1406
+ amount_lamports: {
1407
+ $ref: "#/$defs/positiveSafeUInt"
1408
+ },
1409
+ scheduled_at_ms: {
1410
+ $ref: "#/$defs/positiveSafeUInt"
1411
+ },
1412
+ destination_address: {
1413
+ $ref: "#/$defs/string"
1414
+ }
1415
+ }
1416
+ },
1417
+ executedFragment: {
1418
+ type: "object",
1419
+ additionalProperties: false,
1420
+ required: [
1421
+ "index",
1422
+ "amount_lamports",
1423
+ "destination_address",
1424
+ "tx_signature",
1425
+ "executed_at_ms",
1426
+ "final"
1427
+ ],
1428
+ properties: {
1429
+ index: {
1430
+ $ref: "#/$defs/u32"
1431
+ },
1432
+ amount_lamports: {
1433
+ $ref: "#/$defs/positiveSafeUInt"
1434
+ },
1435
+ destination_address: {
1436
+ $ref: "#/$defs/string"
1437
+ },
1438
+ tx_signature: {
1439
+ $ref: "#/$defs/string"
1440
+ },
1441
+ executed_at_ms: {
1442
+ $ref: "#/$defs/positiveSafeUInt"
1443
+ },
1444
+ final: {
1445
+ type: "boolean"
1446
+ }
1447
+ }
1448
+ },
1449
+ payoutSchedule: {
1450
+ type: "object",
1451
+ additionalProperties: false,
1452
+ required: ["mode", "fragments"],
1453
+ properties: {
1454
+ mode: {
1455
+ enum: ["instant", "scheduled"]
1456
+ },
1457
+ fragments: {
1458
+ type: "array",
1459
+ maxItems: 64,
1460
+ items: {
1461
+ $ref: "#/$defs/plannedFragment"
1462
+ }
1463
+ }
1464
+ }
1465
+ },
1466
+ normalUserContractStatus: {
1467
+ enum: ["deposit", "activating", "active", "settling", "completed", "refunded", "expired"]
1468
+ },
1469
+ normalUserSettlementPhase: {
1470
+ enum: [
1471
+ "awaiting_deposit",
1472
+ "activating",
1473
+ "waiting_for_liquidity",
1474
+ "matched_scheduled",
1475
+ "locked_pending_payout",
1476
+ "submitted",
1477
+ "confirming",
1478
+ "completed",
1479
+ "refund_pending",
1480
+ "refunded",
1481
+ "expired"
1482
+ ]
1483
+ },
1484
+ normalUserRefundability: {
1485
+ enum: ["available_now", "request_pending", "blocked_by_lock", "stale", "unavailable"]
1486
+ },
1487
+ sourceLiquidityUse: {
1488
+ enum: [
1489
+ "not_materialized",
1490
+ "available",
1491
+ "locked_for_payout",
1492
+ "pending_finality",
1493
+ "spent",
1494
+ "released"
1495
+ ]
1496
+ },
1497
+ syncRetryInstruction: {
1498
+ type: "object",
1499
+ additionalProperties: false,
1500
+ required: ["should_retry", "poll_after_ms"],
1501
+ properties: {
1502
+ should_retry: {
1503
+ type: "boolean"
1504
+ },
1505
+ poll_after_ms: {
1506
+ anyOf: [
1507
+ {
1508
+ type: "null"
1509
+ },
1510
+ {
1511
+ $ref: "#/$defs/safeUInt"
1512
+ }
1513
+ ]
1514
+ }
1515
+ }
1516
+ },
1517
+ payoutWindowSyncSnapshot: {
1518
+ type: "object",
1519
+ additionalProperties: false,
1520
+ required: ["starts_at_ms", "deadline_ms"],
1521
+ properties: {
1522
+ starts_at_ms: {
1523
+ $ref: "#/$defs/safeUInt"
1524
+ },
1525
+ deadline_ms: {
1526
+ anyOf: [
1527
+ {
1528
+ type: "null"
1529
+ },
1530
+ {
1531
+ $ref: "#/$defs/safeUInt"
1532
+ }
1533
+ ]
1534
+ }
1535
+ }
1536
+ },
1537
+ normalUserContractSyncSnapshot: {
1538
+ type: "object",
1539
+ additionalProperties: false,
1540
+ required: [
1541
+ "contract_id",
1542
+ "status",
1543
+ "settlement_phase",
1544
+ "refundability",
1545
+ "source_liquidity_use",
1546
+ "destination_wallet",
1547
+ "deposit_amount_lamports",
1548
+ "deposit_expires_at_ms",
1549
+ "payout_window",
1550
+ "settlement_tx_hashes",
1551
+ "settled_amount_lamports",
1552
+ "protocol_fee_lamports",
1553
+ "network_fee_lost_lamports",
1554
+ "withdrawal_requested_lamports",
1555
+ "withdrawal_landed_lamports",
1556
+ "withdrawal_landed_fee_lamports",
1557
+ "withdrawal_canceled_lamports",
1558
+ "withdrawal_network_fee_loss_lamports",
1559
+ "withdrawal_tx_hashes",
1560
+ "retry",
1561
+ "state_version",
1562
+ "updated_at_ms"
1563
+ ],
1564
+ properties: {
1565
+ contract_id: {
1566
+ $ref: "#/$defs/string"
1567
+ },
1568
+ status: {
1569
+ $ref: "#/$defs/normalUserContractStatus"
1570
+ },
1571
+ settlement_phase: {
1572
+ $ref: "#/$defs/normalUserSettlementPhase"
1573
+ },
1574
+ refundability: {
1575
+ $ref: "#/$defs/normalUserRefundability"
1576
+ },
1577
+ source_liquidity_use: {
1578
+ $ref: "#/$defs/sourceLiquidityUse"
1579
+ },
1580
+ destination_wallet: {
1581
+ anyOf: [
1582
+ {
1583
+ type: "null"
1584
+ },
1585
+ {
1586
+ $ref: "#/$defs/string"
1587
+ }
1588
+ ]
1589
+ },
1590
+ deposit_amount_lamports: {
1591
+ $ref: "#/$defs/safeUInt"
1592
+ },
1593
+ deposit_expires_at_ms: {
1594
+ $ref: "#/$defs/safeUInt"
1595
+ },
1596
+ payout_window: {
1597
+ $ref: "#/$defs/payoutWindowSyncSnapshot"
1598
+ },
1599
+ settlement_tx_hashes: {
1600
+ type: "array",
1601
+ maxItems: 64,
1602
+ items: {
1603
+ $ref: "#/$defs/string"
1604
+ }
1605
+ },
1606
+ settled_amount_lamports: {
1607
+ $ref: "#/$defs/safeUInt"
1608
+ },
1609
+ protocol_fee_lamports: {
1610
+ $ref: "#/$defs/safeUInt"
1611
+ },
1612
+ network_fee_lost_lamports: {
1613
+ type: "array",
1614
+ maxItems: 64,
1615
+ items: {
1616
+ $ref: "#/$defs/safeUInt"
1617
+ }
1618
+ },
1619
+ withdrawal_requested_lamports: {
1620
+ $ref: "#/$defs/safeUInt"
1621
+ },
1622
+ withdrawal_landed_lamports: {
1623
+ $ref: "#/$defs/safeUInt"
1624
+ },
1625
+ withdrawal_landed_fee_lamports: {
1626
+ $ref: "#/$defs/safeUInt"
1627
+ },
1628
+ withdrawal_canceled_lamports: {
1629
+ $ref: "#/$defs/safeUInt"
1630
+ },
1631
+ withdrawal_network_fee_loss_lamports: {
1632
+ $ref: "#/$defs/safeUInt"
1633
+ },
1634
+ withdrawal_tx_hashes: {
1635
+ type: "array",
1636
+ maxItems: 64,
1637
+ items: {
1638
+ $ref: "#/$defs/string"
1639
+ }
1640
+ },
1641
+ retry: {
1642
+ $ref: "#/$defs/syncRetryInstruction"
1643
+ },
1644
+ state_version: {
1645
+ $ref: "#/$defs/safeUInt"
1646
+ },
1647
+ updated_at_ms: {
1648
+ $ref: "#/$defs/safeUInt"
1649
+ }
1650
+ }
1651
+ },
1652
+ normalUserActorSyncSnapshot: {
1653
+ type: "object",
1654
+ additionalProperties: false,
1655
+ required: ["actor", "snapshot"],
1656
+ properties: {
1657
+ actor: {
1658
+ const: "normal_user"
1659
+ },
1660
+ snapshot: {
1661
+ $ref: "#/$defs/normalUserContractSyncSnapshot"
1662
+ }
1663
+ }
1664
+ },
1665
+ syncResponse: {
1666
+ oneOf: [
1667
+ {
1668
+ $ref: "#/$defs/syncUserResponse"
1669
+ },
1670
+ {
1671
+ $ref: "#/$defs/lpPositionResponse"
1672
+ }
1673
+ ]
1674
+ },
1675
+ syncRequiredContractSubject: {
1676
+ type: "object",
1677
+ additionalProperties: false,
1678
+ required: ["kind", "id"],
1679
+ properties: {
1680
+ kind: {
1681
+ const: "contract"
1682
+ },
1683
+ id: {
1684
+ $ref: "#/$defs/string"
1685
+ }
1686
+ }
1687
+ },
1688
+ syncRequiredLpPositionSubject: {
1689
+ type: "object",
1690
+ additionalProperties: false,
1691
+ required: ["kind", "id"],
1692
+ properties: {
1693
+ kind: {
1694
+ const: "lp_position"
1695
+ },
1696
+ id: {
1697
+ $ref: "#/$defs/string"
1698
+ }
1699
+ }
1700
+ },
1701
+ syncRequiredContract: {
1702
+ type: "object",
1703
+ additionalProperties: false,
1704
+ required: ["subject", "event_kind", "state_version", "created_at_ms", "sync_required"],
1705
+ properties: {
1706
+ subject: {
1707
+ $ref: "#/$defs/syncRequiredContractSubject"
1708
+ },
1709
+ event_kind: {
1710
+ enum: [
1711
+ "contract_requested",
1712
+ "request_expired",
1713
+ "contract_activated",
1714
+ "match_requested",
1715
+ "reserved",
1716
+ "settlement_pending",
1717
+ "partially_fulfilled",
1718
+ "fulfilled",
1719
+ "refund_requested",
1720
+ "refunded",
1721
+ "source_liquidity_updated",
1722
+ "withdrawal_requested",
1723
+ "withdrawal_finalized"
1724
+ ]
1725
+ },
1726
+ state_version: {
1727
+ $ref: "#/$defs/safeUInt"
1728
+ },
1729
+ created_at_ms: {
1730
+ $ref: "#/$defs/safeUInt"
1731
+ },
1732
+ sync_required: {
1733
+ const: true
1734
+ }
1735
+ }
1736
+ },
1737
+ syncRequiredLpPosition: {
1738
+ type: "object",
1739
+ additionalProperties: false,
1740
+ required: ["subject", "event_kind", "state_version", "created_at_ms", "sync_required"],
1741
+ properties: {
1742
+ subject: {
1743
+ $ref: "#/$defs/syncRequiredLpPositionSubject"
1744
+ },
1745
+ event_kind: {
1746
+ enum: [
1747
+ "match_requested",
1748
+ "reserved",
1749
+ "withdrawal_requested",
1750
+ "withdrawal_finalized",
1751
+ "reimbursement_finalized"
1752
+ ]
1753
+ },
1754
+ state_version: {
1755
+ $ref: "#/$defs/safeUInt"
1756
+ },
1757
+ created_at_ms: {
1758
+ $ref: "#/$defs/safeUInt"
1759
+ },
1760
+ sync_required: {
1761
+ const: true
1762
+ }
1763
+ }
1764
+ },
1765
+ syncRequired: {
1766
+ oneOf: [
1767
+ {
1768
+ $ref: "#/$defs/syncRequiredContract"
1769
+ },
1770
+ {
1771
+ $ref: "#/$defs/syncRequiredLpPosition"
1772
+ }
1773
+ ]
1774
+ },
1775
+ syncUserResponse: {
1776
+ type: "object",
1777
+ additionalProperties: false,
1778
+ required: ["state", "fragments_executed"],
1779
+ properties: {
1780
+ state: {
1781
+ enum: [
1782
+ "new",
1783
+ "awaiting_dkg",
1784
+ "awaiting_deposit",
1785
+ "awaiting_delegation",
1786
+ "ready_for_settlement",
1787
+ "settling",
1788
+ "completed",
1789
+ "refunded",
1790
+ "failed"
1791
+ ]
1792
+ },
1793
+ fragments_executed: {
1794
+ type: "array",
1795
+ maxItems: 64,
1796
+ items: {
1797
+ $ref: "#/$defs/executedFragment"
1798
+ }
1799
+ },
1800
+ withdraw_execution: {
1801
+ $ref: "#/$defs/withdrawExecuted"
1802
+ },
1803
+ actor_sync: {
1804
+ $ref: "#/$defs/normalUserActorSyncSnapshot"
1805
+ }
1806
+ }
1807
+ },
1808
+ lpPositionResponse: {
1809
+ type: "object",
1810
+ additionalProperties: false,
1811
+ required: ["case", "value"],
1812
+ properties: {
1813
+ case: {
1814
+ const: "position"
1815
+ },
1816
+ value: {
1817
+ $ref: "#/$defs/lpPositionView"
1818
+ }
1819
+ }
1820
+ },
1821
+ withdrawRequestRequest: {
1822
+ oneOf: [
1823
+ {
1824
+ $ref: "#/$defs/withdrawRequest"
1825
+ },
1826
+ {
1827
+ $ref: "#/$defs/withdrawRequestProtocolRequest"
1828
+ },
1829
+ {
1830
+ $ref: "#/$defs/withdrawRequestLpRequest"
1831
+ }
1832
+ ]
1833
+ },
1834
+ withdrawRequestLpRequest: {
1835
+ $comment: "The LP arm of the WithdrawRequest verb: an LP-signed withdraw_request command with an explicit principal/fee bucket claim.",
1836
+ type: "object",
1837
+ additionalProperties: false,
1838
+ required: ["actor", "command", "signature"],
1839
+ properties: {
1840
+ actor: {
1841
+ const: "lp"
1842
+ },
1843
+ command: {
1844
+ $ref: "#/$defs/lpCommand"
1845
+ },
1846
+ signature: {
1847
+ $ref: "#/$defs/base64url64Bytes"
1848
+ }
1849
+ },
1850
+ allOf: [
1851
+ {
1852
+ type: "object",
1853
+ properties: {
1854
+ command: {
1855
+ type: "object",
1856
+ properties: {
1857
+ action: {
1858
+ const: "withdraw_request"
1859
+ }
1860
+ }
1861
+ }
1862
+ }
1863
+ }
1864
+ ]
1865
+ },
1866
+ withdrawRequestProtocolRequest: {
1867
+ $comment: "The protocol arm of the WithdrawRequest verb is a schema-valid V0 no-op. The coordinator returns `Protocol withdraw no-op yet` and creates no sealed executable work.",
1868
+ type: "object",
1869
+ additionalProperties: false,
1870
+ required: ["actor"],
1871
+ properties: {
1872
+ actor: {
1873
+ const: "protocol"
1874
+ }
1875
+ }
1876
+ },
1877
+ withdrawRequest: {
1878
+ $comment: "The user arm of the WithdrawRequest verb: a Recovery Code withdrawal on the Noise session.",
1879
+ type: "object",
1880
+ additionalProperties: false,
1881
+ required: ["recovery_code"],
1882
+ properties: {
1883
+ recovery_code: {
1884
+ $ref: "#/$defs/base64url32Bytes"
1885
+ }
1886
+ }
1887
+ },
1888
+ withdrawExecuted: {
1889
+ type: "object",
1890
+ additionalProperties: false,
1891
+ required: [
1892
+ "tx_signature",
1893
+ "amount_lamports",
1894
+ "withdrawn_at_ms",
1895
+ "trigger",
1896
+ "destination_address"
1897
+ ],
1898
+ properties: {
1899
+ tx_signature: {
1900
+ $ref: "#/$defs/string"
1901
+ },
1902
+ amount_lamports: {
1903
+ $ref: "#/$defs/positiveSafeUInt"
1904
+ },
1905
+ withdrawn_at_ms: {
1906
+ $ref: "#/$defs/positiveSafeUInt"
1907
+ },
1908
+ trigger: {
1909
+ enum: ["user_request", "auto_deadline"]
1910
+ },
1911
+ destination_address: {
1912
+ $ref: "#/$defs/string"
1913
+ }
1914
+ }
1915
+ },
1916
+ withdrawAccepted: {
1917
+ type: "object",
1918
+ additionalProperties: false,
1919
+ required: ["status", "amount_lamports", "destination_address", "accepted_at_ms"],
1920
+ properties: {
1921
+ status: {
1922
+ enum: ["accepted"]
1923
+ },
1924
+ amount_lamports: {
1925
+ $ref: "#/$defs/positiveSafeUInt"
1926
+ },
1927
+ destination_address: {
1928
+ $ref: "#/$defs/string"
1929
+ },
1930
+ accepted_at_ms: {
1931
+ $ref: "#/$defs/positiveSafeUInt"
1932
+ }
1933
+ }
1934
+ },
1935
+ errorPayload: {
1936
+ type: "object",
1937
+ additionalProperties: false,
1938
+ required: ["code", "message"],
1939
+ properties: {
1940
+ code: {
1941
+ enum: [
1942
+ "ERR_INVALID_PAYLOAD",
1943
+ "ERR_UNEXPECTED_TYPE",
1944
+ "ERR_UNKNOWN_SWAP",
1945
+ "ERR_POLICY_VIOLATION",
1946
+ "ERR_INVALID_RECOVERY_CODE",
1947
+ "ERR_SWAP_NOT_REFUNDABLE",
1948
+ "ERR_DKG_FAILURE",
1949
+ "ERR_INTERNAL",
1950
+ "ERR_INVALID_DEPOSIT",
1951
+ "ERR_SETTLEMENT_FAILED"
1952
+ ]
1953
+ },
1954
+ message: {
1955
+ $ref: "#/$defs/string"
1956
+ },
1957
+ failed_req_id: {
1958
+ $ref: "#/$defs/u32"
1959
+ }
1960
+ }
1961
+ },
1962
+ base64url64Bytes: {
1963
+ type: "string",
1964
+ minLength: 86,
1965
+ maxLength: 86,
1966
+ pattern: "^[A-Za-z0-9_-]{86}$"
1967
+ },
1968
+ base64urlMax16384Bytes: {
1969
+ type: "string",
1970
+ minLength: 2,
1971
+ maxLength: 21846,
1972
+ pattern: "^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2}|[A-Za-z0-9_-]{3})?$"
1973
+ },
1974
+ lpId: {
1975
+ type: "string",
1976
+ minLength: 3,
1977
+ maxLength: 96,
1978
+ pattern: "^[A-Za-z0-9_-]+$"
1979
+ },
1980
+ lpRedemptionId: {
1981
+ type: "string",
1982
+ minLength: 1,
1983
+ maxLength: 160,
1984
+ pattern: "^[A-Za-z0-9_:-]+$"
1985
+ },
1986
+ lpAddress: {
1987
+ type: "string",
1988
+ minLength: 1,
1989
+ maxLength: 128
1990
+ },
1991
+ lpSessionId: {
1992
+ type: "string",
1993
+ minLength: 1,
1994
+ maxLength: 128
1995
+ },
1996
+ lpTxSignature: {
1997
+ type: "string",
1998
+ minLength: 1,
1999
+ maxLength: 256
2000
+ },
2001
+ lpSignedCommand: {
2002
+ type: "object",
2003
+ additionalProperties: false,
2004
+ required: ["command", "signature"],
2005
+ properties: {
2006
+ command: {
2007
+ $ref: "#/$defs/lpCommand"
2008
+ },
2009
+ signature: {
2010
+ $ref: "#/$defs/base64url64Bytes"
2011
+ }
2012
+ }
2013
+ },
2014
+ lpCommand: {
2015
+ type: "object",
2016
+ additionalProperties: false,
2017
+ required: [
2018
+ "lp_position_id",
2019
+ "action",
2020
+ "nonce",
2021
+ "expires_at_ms",
2022
+ "coordinator_attestation_hash",
2023
+ "payload"
2024
+ ],
2025
+ properties: {
2026
+ lp_position_id: {
2027
+ $ref: "#/$defs/lpId"
2028
+ },
2029
+ action: {
2030
+ enum: [
2031
+ "position_open",
2032
+ "position_get",
2033
+ "dkg_init",
2034
+ "dkg_round1",
2035
+ "dkg_round2",
2036
+ "dkg_finalize",
2037
+ "refill",
2038
+ "funding_plan",
2039
+ "funding_confirmation",
2040
+ "make_available",
2041
+ "reconcile_funding",
2042
+ "prepare_top_up",
2043
+ "top_up_status",
2044
+ "withdraw_request"
2045
+ ]
2046
+ },
2047
+ nonce: {
2048
+ $ref: "#/$defs/base64url32Bytes"
2049
+ },
2050
+ expires_at_ms: {
2051
+ $ref: "#/$defs/positiveSafeUInt"
2052
+ },
2053
+ coordinator_attestation_hash: {
2054
+ $ref: "#/$defs/base64url32Bytes"
2055
+ },
2056
+ payload: {
2057
+ $ref: "#/$defs/lpCommandBody"
2058
+ }
2059
+ },
2060
+ allOf: [
2061
+ {
2062
+ oneOf: [
2063
+ {
2064
+ properties: {
2065
+ action: {
2066
+ const: "position_open"
2067
+ },
2068
+ payload: {
2069
+ $ref: "#/$defs/lpPositionOpenCase"
2070
+ }
2071
+ }
2072
+ },
2073
+ {
2074
+ properties: {
2075
+ action: {
2076
+ const: "position_get"
2077
+ },
2078
+ payload: {
2079
+ $ref: "#/$defs/lpPositionGetCase"
2080
+ }
2081
+ }
2082
+ },
2083
+ {
2084
+ properties: {
2085
+ action: {
2086
+ const: "dkg_init"
2087
+ },
2088
+ payload: {
2089
+ $ref: "#/$defs/lpDkgInitCase"
2090
+ }
2091
+ }
2092
+ },
2093
+ {
2094
+ properties: {
2095
+ action: {
2096
+ const: "dkg_round1"
2097
+ },
2098
+ payload: {
2099
+ $ref: "#/$defs/lpDkgRound1Case"
2100
+ }
2101
+ }
2102
+ },
2103
+ {
2104
+ properties: {
2105
+ action: {
2106
+ const: "dkg_round2"
2107
+ },
2108
+ payload: {
2109
+ $ref: "#/$defs/lpDkgRound2Case"
2110
+ }
2111
+ }
2112
+ },
2113
+ {
2114
+ properties: {
2115
+ action: {
2116
+ const: "dkg_finalize"
2117
+ },
2118
+ payload: {
2119
+ $ref: "#/$defs/lpDkgFinalizeCase"
2120
+ }
2121
+ }
2122
+ },
2123
+ {
2124
+ properties: {
2125
+ action: {
2126
+ const: "refill"
2127
+ },
2128
+ payload: {
2129
+ $ref: "#/$defs/lpRefillCase"
2130
+ }
2131
+ }
2132
+ },
2133
+ {
2134
+ properties: {
2135
+ action: {
2136
+ const: "funding_plan"
2137
+ },
2138
+ payload: {
2139
+ $ref: "#/$defs/lpFundingPlanCase"
2140
+ }
2141
+ }
2142
+ },
2143
+ {
2144
+ properties: {
2145
+ action: {
2146
+ const: "funding_confirmation"
2147
+ },
2148
+ payload: {
2149
+ $ref: "#/$defs/lpFundingConfirmationCase"
2150
+ }
2151
+ }
2152
+ },
2153
+ {
2154
+ properties: {
2155
+ action: {
2156
+ const: "make_available"
2157
+ },
2158
+ payload: {
2159
+ $ref: "#/$defs/lpMakeAvailableCase"
2160
+ }
2161
+ }
2162
+ },
2163
+ {
2164
+ properties: {
2165
+ action: {
2166
+ const: "reconcile_funding"
2167
+ },
2168
+ payload: {
2169
+ $ref: "#/$defs/lpReconcileFundingCase"
2170
+ }
2171
+ }
2172
+ },
2173
+ {
2174
+ properties: {
2175
+ action: {
2176
+ const: "prepare_top_up"
2177
+ },
2178
+ payload: {
2179
+ $ref: "#/$defs/lpPrepareTopUpCase"
2180
+ }
2181
+ }
2182
+ },
2183
+ {
2184
+ properties: {
2185
+ action: {
2186
+ const: "top_up_status"
2187
+ },
2188
+ payload: {
2189
+ $ref: "#/$defs/lpTopUpStatusCase"
2190
+ }
2191
+ }
2192
+ },
2193
+ {
2194
+ properties: {
2195
+ action: {
2196
+ const: "withdraw_request"
2197
+ },
2198
+ payload: {
2199
+ $ref: "#/$defs/lpWithdrawalRequestCase"
2200
+ }
2201
+ }
2202
+ }
2203
+ ]
2204
+ }
2205
+ ]
2206
+ },
2207
+ lpCommandBody: {
2208
+ oneOf: [
2209
+ {
2210
+ $ref: "#/$defs/lpPositionOpenCase"
2211
+ },
2212
+ {
2213
+ $ref: "#/$defs/lpPositionGetCase"
2214
+ },
2215
+ {
2216
+ $ref: "#/$defs/lpDkgInitCase"
2217
+ },
2218
+ {
2219
+ $ref: "#/$defs/lpDkgRound1Case"
2220
+ },
2221
+ {
2222
+ $ref: "#/$defs/lpDkgRound2Case"
2223
+ },
2224
+ {
2225
+ $ref: "#/$defs/lpDkgFinalizeCase"
2226
+ },
2227
+ {
2228
+ $ref: "#/$defs/lpRefillCase"
2229
+ },
2230
+ {
2231
+ $ref: "#/$defs/lpFundingPlanCase"
2232
+ },
2233
+ {
2234
+ $ref: "#/$defs/lpFundingConfirmationCase"
2235
+ },
2236
+ {
2237
+ $ref: "#/$defs/lpMakeAvailableCase"
2238
+ },
2239
+ {
2240
+ $ref: "#/$defs/lpReconcileFundingCase"
2241
+ },
2242
+ {
2243
+ $ref: "#/$defs/lpPrepareTopUpCase"
2244
+ },
2245
+ {
2246
+ $ref: "#/$defs/lpTopUpStatusCase"
2247
+ },
2248
+ {
2249
+ $ref: "#/$defs/lpWithdrawalRequestCase"
2250
+ }
2251
+ ]
2252
+ },
2253
+ lpPositionOpenCase: {
2254
+ type: "object",
2255
+ additionalProperties: false,
2256
+ required: ["case", "value"],
2257
+ properties: {
2258
+ case: {
2259
+ const: "positionOpen"
2260
+ },
2261
+ value: {
2262
+ $ref: "#/$defs/lpPositionOpen"
2263
+ }
2264
+ }
2265
+ },
2266
+ lpPositionGetCase: {
2267
+ type: "object",
2268
+ additionalProperties: false,
2269
+ required: ["case", "value"],
2270
+ properties: {
2271
+ case: {
2272
+ const: "positionGet"
2273
+ },
2274
+ value: {
2275
+ $ref: "#/$defs/lpPositionGet"
2276
+ }
2277
+ }
2278
+ },
2279
+ lpDkgInitCase: {
2280
+ type: "object",
2281
+ additionalProperties: false,
2282
+ required: ["case", "value"],
2283
+ properties: {
2284
+ case: {
2285
+ const: "dkgInit"
2286
+ },
2287
+ value: {
2288
+ $ref: "#/$defs/lpDkgInit"
2289
+ }
2290
+ }
2291
+ },
2292
+ lpDkgRound1Case: {
2293
+ type: "object",
2294
+ additionalProperties: false,
2295
+ required: ["case", "value"],
2296
+ properties: {
2297
+ case: {
2298
+ const: "dkgRound1"
2299
+ },
2300
+ value: {
2301
+ $ref: "#/$defs/lpDkgRound1"
2302
+ }
2303
+ }
2304
+ },
2305
+ lpDkgRound2Case: {
2306
+ type: "object",
2307
+ additionalProperties: false,
2308
+ required: ["case", "value"],
2309
+ properties: {
2310
+ case: {
2311
+ const: "dkgRound2"
2312
+ },
2313
+ value: {
2314
+ $ref: "#/$defs/lpDkgRound2"
2315
+ }
2316
+ }
2317
+ },
2318
+ lpDkgFinalizeCase: {
2319
+ type: "object",
2320
+ additionalProperties: false,
2321
+ required: ["case", "value"],
2322
+ properties: {
2323
+ case: {
2324
+ const: "dkgFinalize"
2325
+ },
2326
+ value: {
2327
+ $ref: "#/$defs/lpDkgFinalize"
2328
+ }
2329
+ }
2330
+ },
2331
+ lpRefillCase: {
2332
+ type: "object",
2333
+ additionalProperties: false,
2334
+ required: ["case", "value"],
2335
+ properties: {
2336
+ case: {
2337
+ const: "refill"
2338
+ },
2339
+ value: {
2340
+ $ref: "#/$defs/lpRefill"
2341
+ }
2342
+ }
2343
+ },
2344
+ lpFundingPlanCase: {
2345
+ type: "object",
2346
+ additionalProperties: false,
2347
+ required: ["case", "value"],
2348
+ properties: {
2349
+ case: {
2350
+ const: "fundingPlan"
2351
+ },
2352
+ value: {
2353
+ $ref: "#/$defs/lpFundingPlan"
2354
+ }
2355
+ }
2356
+ },
2357
+ lpFundingConfirmationCase: {
2358
+ type: "object",
2359
+ additionalProperties: false,
2360
+ required: ["case", "value"],
2361
+ properties: {
2362
+ case: {
2363
+ const: "fundingConfirmation"
2364
+ },
2365
+ value: {
2366
+ $ref: "#/$defs/lpFundingConfirmation"
2367
+ }
2368
+ }
2369
+ },
2370
+ lpMakeAvailableCase: {
2371
+ type: "object",
2372
+ additionalProperties: false,
2373
+ required: ["case", "value"],
2374
+ properties: {
2375
+ case: {
2376
+ const: "makeAvailable"
2377
+ },
2378
+ value: {
2379
+ $ref: "#/$defs/lpMakeAvailable"
2380
+ }
2381
+ }
2382
+ },
2383
+ lpReconcileFundingCase: {
2384
+ type: "object",
2385
+ additionalProperties: false,
2386
+ required: ["case", "value"],
2387
+ properties: {
2388
+ case: {
2389
+ const: "reconcileFunding"
2390
+ },
2391
+ value: {
2392
+ $ref: "#/$defs/emptyObject"
2393
+ }
2394
+ }
2395
+ },
2396
+ lpPrepareTopUpCase: {
2397
+ type: "object",
2398
+ additionalProperties: false,
2399
+ required: ["case", "value"],
2400
+ properties: {
2401
+ case: {
2402
+ const: "prepareTopUp"
2403
+ },
2404
+ value: {
2405
+ $ref: "#/$defs/emptyObject"
2406
+ }
2407
+ }
2408
+ },
2409
+ lpTopUpStatusCase: {
2410
+ type: "object",
2411
+ additionalProperties: false,
2412
+ required: ["case", "value"],
2413
+ properties: {
2414
+ case: {
2415
+ const: "topUpStatus"
2416
+ },
2417
+ value: {
2418
+ $ref: "#/$defs/lpTopUpStatus"
2419
+ }
2420
+ }
2421
+ },
2422
+ lpTopUpStatus: {
2423
+ type: "object",
2424
+ additionalProperties: false,
2425
+ required: ["top_up_id"],
2426
+ properties: {
2427
+ top_up_id: {
2428
+ $ref: "#/$defs/lpId"
2429
+ }
2430
+ }
2431
+ },
2432
+ lpPositionGet: {
2433
+ type: "object",
2434
+ additionalProperties: false,
2435
+ properties: {
2436
+ redemption_history_cursor: {
2437
+ $ref: "#/$defs/lpRedemptionId"
2438
+ }
2439
+ }
2440
+ },
2441
+ lpPositionOpen: {
2442
+ type: "object",
2443
+ additionalProperties: false,
2444
+ required: [
2445
+ "auth_public_key",
2446
+ "refill_public_key",
2447
+ "redemption_commitment",
2448
+ "committed_lamports",
2449
+ "shard_count",
2450
+ "shard_amount_lamports",
2451
+ "refill_threshold",
2452
+ "addresses",
2453
+ "created_at_ms"
2454
+ ],
2455
+ properties: {
2456
+ auth_public_key: {
2457
+ $ref: "#/$defs/base64url32Bytes"
2458
+ },
2459
+ refill_public_key: {
2460
+ $ref: "#/$defs/base64url32Bytes"
2461
+ },
2462
+ redemption_commitment: {
2463
+ $ref: "#/$defs/base64url32Bytes"
2464
+ },
2465
+ committed_lamports: {
2466
+ $ref: "#/$defs/positiveSafeUInt"
2467
+ },
2468
+ shard_count: {
2469
+ type: "integer",
2470
+ minimum: 1,
2471
+ maximum: 300
2472
+ },
2473
+ shard_amount_lamports: {
2474
+ $ref: "#/$defs/positiveSafeUInt"
2475
+ },
2476
+ refill_threshold: {
2477
+ type: "integer",
2478
+ minimum: 0,
2479
+ maximum: 300
2480
+ },
2481
+ addresses: {
2482
+ type: "array",
2483
+ maxItems: 300,
2484
+ items: {
2485
+ $ref: "#/$defs/lpAddress"
2486
+ }
2487
+ },
2488
+ created_at_ms: {
2489
+ $ref: "#/$defs/positiveSafeUInt"
2490
+ }
2491
+ }
2492
+ },
2493
+ lpDkgInit: {
2494
+ type: "object",
2495
+ additionalProperties: false,
2496
+ required: ["index"],
2497
+ properties: {
2498
+ index: {
2499
+ $ref: "#/$defs/lpShardIndex"
2500
+ }
2501
+ }
2502
+ },
2503
+ lpDkgRound1: {
2504
+ type: "object",
2505
+ additionalProperties: false,
2506
+ required: ["session_id", "package"],
2507
+ properties: {
2508
+ session_id: {
2509
+ $ref: "#/$defs/lpSessionId"
2510
+ },
2511
+ package: {
2512
+ $ref: "#/$defs/base64urlMax16384Bytes"
2513
+ }
2514
+ }
2515
+ },
2516
+ lpDkgRound2: {
2517
+ type: "object",
2518
+ additionalProperties: false,
2519
+ required: ["session_id", "package", "delegated_share"],
2520
+ properties: {
2521
+ session_id: {
2522
+ $ref: "#/$defs/lpSessionId"
2523
+ },
2524
+ package: {
2525
+ $ref: "#/$defs/base64urlMax16384Bytes"
2526
+ },
2527
+ delegated_share: {
2528
+ $ref: "#/$defs/base64urlMax16384Bytes"
2529
+ }
2530
+ }
2531
+ },
2532
+ lpDkgFinalize: {
2533
+ type: "object",
2534
+ additionalProperties: false,
2535
+ required: ["start_index", "addresses", "finalized_at_ms"],
2536
+ properties: {
2537
+ start_index: {
2538
+ $ref: "#/$defs/lpShardIndex"
2539
+ },
2540
+ addresses: {
2541
+ type: "array",
2542
+ minItems: 1,
2543
+ maxItems: 300,
2544
+ items: {
2545
+ $ref: "#/$defs/lpAddress"
2546
+ }
2547
+ },
2548
+ finalized_at_ms: {
2549
+ $ref: "#/$defs/positiveSafeUInt"
2550
+ }
2551
+ }
2552
+ },
2553
+ lpRefill: {
2554
+ type: "object",
2555
+ additionalProperties: false,
2556
+ required: ["shard_count", "shard_amount_lamports", "addresses", "authorized_at_ms"],
2557
+ properties: {
2558
+ shard_count: {
2559
+ type: "integer",
2560
+ minimum: 1,
2561
+ maximum: 50
2562
+ },
2563
+ shard_amount_lamports: {
2564
+ $ref: "#/$defs/positiveSafeUInt"
2565
+ },
2566
+ addresses: {
2567
+ type: "array",
2568
+ maxItems: 50,
2569
+ items: {
2570
+ $ref: "#/$defs/lpAddress"
2571
+ }
2572
+ },
2573
+ authorized_at_ms: {
2574
+ $ref: "#/$defs/positiveSafeUInt"
2575
+ }
2576
+ }
2577
+ },
2578
+ lpFundingPlan: {
2579
+ type: "object",
2580
+ additionalProperties: false,
2581
+ required: ["shard_count", "planned_at_ms"],
2582
+ properties: {
2583
+ shard_count: {
2584
+ type: "integer",
2585
+ minimum: 1,
2586
+ maximum: 50
2587
+ },
2588
+ planned_at_ms: {
2589
+ $ref: "#/$defs/positiveSafeUInt"
2590
+ }
2591
+ }
2592
+ },
2593
+ lpFundingConfirmation: {
2594
+ type: "object",
2595
+ additionalProperties: false,
2596
+ required: ["shard_id", "amount_lamports", "funding_tx_signature"],
2597
+ properties: {
2598
+ shard_id: {
2599
+ $ref: "#/$defs/lpShardId"
2600
+ },
2601
+ amount_lamports: {
2602
+ $ref: "#/$defs/positiveSafeUInt"
2603
+ },
2604
+ funding_tx_signature: {
2605
+ $ref: "#/$defs/lpTxSignature"
2606
+ }
2607
+ }
2608
+ },
2609
+ lpMakeAvailable: {
2610
+ type: "object",
2611
+ additionalProperties: false,
2612
+ required: ["shard_id"],
2613
+ properties: {
2614
+ shard_id: {
2615
+ $ref: "#/$defs/lpShardId"
2616
+ }
2617
+ }
2618
+ },
2619
+ lpCommandResponse: {
2620
+ $comment: "Union of all LP-signed command responses, regardless of carrier envelope. Envelope variants below narrow this: LpCommand gets lifecycle-only responses, WithdrawRequest gets withdrawalExecution.",
2621
+ oneOf: [
2622
+ {
2623
+ $ref: "#/$defs/lpCommandLifecycleResponse"
2624
+ },
2625
+ {
2626
+ $ref: "#/$defs/lpWithdrawalExecutionResponse"
2627
+ }
2628
+ ]
2629
+ },
2630
+ lpCommandLifecycleResponse: {
2631
+ oneOf: [
2632
+ {
2633
+ type: "object",
2634
+ additionalProperties: false,
2635
+ required: ["case", "value"],
2636
+ properties: {
2637
+ case: {
2638
+ const: "position"
2639
+ },
2640
+ value: {
2641
+ $ref: "#/$defs/lpPositionView"
2642
+ }
2643
+ }
2644
+ },
2645
+ {
2646
+ type: "object",
2647
+ additionalProperties: false,
2648
+ required: ["case", "value"],
2649
+ properties: {
2650
+ case: {
2651
+ const: "dkgInit"
2652
+ },
2653
+ value: {
2654
+ $ref: "#/$defs/lpDkgInitResponse"
2655
+ }
2656
+ }
2657
+ },
2658
+ {
2659
+ type: "object",
2660
+ additionalProperties: false,
2661
+ required: ["case", "value"],
2662
+ properties: {
2663
+ case: {
2664
+ const: "dkgRound1"
2665
+ },
2666
+ value: {
2667
+ $ref: "#/$defs/lpDkgRound1Response"
2668
+ }
2669
+ }
2670
+ },
2671
+ {
2672
+ type: "object",
2673
+ additionalProperties: false,
2674
+ required: ["case", "value"],
2675
+ properties: {
2676
+ case: {
2677
+ const: "dkgRound2"
2678
+ },
2679
+ value: {
2680
+ $ref: "#/$defs/lpDkgRound2Response"
2681
+ }
2682
+ }
2683
+ },
2684
+ {
2685
+ type: "object",
2686
+ additionalProperties: false,
2687
+ required: ["case", "value"],
2688
+ properties: {
2689
+ case: {
2690
+ const: "shards"
2691
+ },
2692
+ value: {
2693
+ type: "array",
2694
+ maxItems: 300,
2695
+ items: {
2696
+ $ref: "#/$defs/lpShardView"
2697
+ }
2698
+ }
2699
+ }
2700
+ },
2701
+ {
2702
+ type: "object",
2703
+ additionalProperties: false,
2704
+ required: ["case", "value"],
2705
+ properties: {
2706
+ case: {
2707
+ const: "shard"
2708
+ },
2709
+ value: {
2710
+ $ref: "#/$defs/lpShardView"
2711
+ }
2712
+ }
2713
+ },
2714
+ {
2715
+ type: "object",
2716
+ additionalProperties: false,
2717
+ required: ["case", "value"],
2718
+ properties: {
2719
+ case: {
2720
+ const: "topUpRequest"
2721
+ },
2722
+ value: {
2723
+ $ref: "#/$defs/lpTopUpRequest"
2724
+ }
2725
+ }
2726
+ }
2727
+ ]
2728
+ },
2729
+ lpTopUpRequest: {
2730
+ type: "object",
2731
+ additionalProperties: false,
2732
+ required: ["top_up_id", "lp_position_id", "deposit_address", "expires_at_ms", "status"],
2733
+ properties: {
2734
+ top_up_id: {
2735
+ $ref: "#/$defs/lpId"
2736
+ },
2737
+ lp_position_id: {
2738
+ $ref: "#/$defs/lpId"
2739
+ },
2740
+ deposit_address: {
2741
+ $ref: "#/$defs/lpAddress"
2742
+ },
2743
+ expires_at_ms: {
2744
+ $ref: "#/$defs/positiveSafeUInt"
2745
+ },
2746
+ status: {
2747
+ enum: ["watching", "funded", "expired"]
2748
+ }
2749
+ }
2750
+ },
2751
+ lpWithdrawalExecutionResponse: {
2752
+ type: "object",
2753
+ additionalProperties: false,
2754
+ required: ["case", "value"],
2755
+ properties: {
2756
+ case: {
2757
+ const: "withdrawalExecution"
2758
+ },
2759
+ value: {
2760
+ $ref: "#/$defs/lpWithdrawalExecutionView"
2761
+ }
2762
+ }
2763
+ },
2764
+ lpDkgInitResponse: {
2765
+ type: "object",
2766
+ additionalProperties: false,
2767
+ required: ["session_id"],
2768
+ properties: {
2769
+ session_id: {
2770
+ $ref: "#/$defs/lpSessionId"
2771
+ }
2772
+ }
2773
+ },
2774
+ lpDkgRound1Response: {
2775
+ type: "object",
2776
+ additionalProperties: false,
2777
+ required: ["package", "round2_for_lp"],
2778
+ properties: {
2779
+ package: {
2780
+ $ref: "#/$defs/base64urlMax16384Bytes"
2781
+ },
2782
+ round2_for_lp: {
2783
+ $ref: "#/$defs/base64urlMax16384Bytes"
2784
+ }
2785
+ }
2786
+ },
2787
+ lpDkgRound2Response: {
2788
+ type: "object",
2789
+ additionalProperties: false,
2790
+ required: ["lp_position_id", "shard_id", "index", "address", "joint_pubkey"],
2791
+ properties: {
2792
+ lp_position_id: {
2793
+ $ref: "#/$defs/lpId"
2794
+ },
2795
+ shard_id: {
2796
+ $ref: "#/$defs/lpShardId"
2797
+ },
2798
+ index: {
2799
+ $ref: "#/$defs/lpShardIndex"
2800
+ },
2801
+ address: {
2802
+ $ref: "#/$defs/lpAddress"
2803
+ },
2804
+ joint_pubkey: {
2805
+ $ref: "#/$defs/base64url32Bytes"
2806
+ }
2807
+ }
2808
+ },
2809
+ lpPositionView: {
2810
+ type: "object",
2811
+ additionalProperties: false,
2812
+ required: [
2813
+ "lp_position_id",
2814
+ "status",
2815
+ "target_shard_count",
2816
+ "auth_public_key",
2817
+ "refill_public_key",
2818
+ "redemption_commitment",
2819
+ "committed_lamports",
2820
+ "earned_lamports",
2821
+ "refill_threshold",
2822
+ "shards",
2823
+ "created_at_ms"
2824
+ ],
2825
+ properties: {
2826
+ lp_position_id: {
2827
+ $ref: "#/$defs/lpId"
2828
+ },
2829
+ status: {
2830
+ enum: ["dkg_pending", "active", "withdrawing", "closed"]
2831
+ },
2832
+ target_shard_count: {
2833
+ type: "integer",
2834
+ minimum: 0,
2835
+ maximum: 300
2836
+ },
2837
+ auth_public_key: {
2838
+ $ref: "#/$defs/base64url32Bytes"
2839
+ },
2840
+ refill_public_key: {
2841
+ $ref: "#/$defs/base64url32Bytes"
2842
+ },
2843
+ redemption_commitment: {
2844
+ $ref: "#/$defs/base64url32Bytes"
2845
+ },
2846
+ committed_lamports: {
2847
+ $ref: "#/$defs/safeUInt"
2848
+ },
2849
+ earned_lamports: {
2850
+ $ref: "#/$defs/safeUInt"
2851
+ },
2852
+ refill_threshold: {
2853
+ type: "integer",
2854
+ minimum: 0,
2855
+ maximum: 300
2856
+ },
2857
+ shards: {
2858
+ type: "array",
2859
+ maxItems: 300,
2860
+ items: {
2861
+ $ref: "#/$defs/lpShardView"
2862
+ }
2863
+ },
2864
+ created_at_ms: {
2865
+ $ref: "#/$defs/positiveSafeUInt"
2866
+ },
2867
+ actor_sync: {
2868
+ $ref: "#/$defs/lpActorSyncView"
2869
+ }
2870
+ }
2871
+ },
2872
+ lpActorSyncView: {
2873
+ type: "object",
2874
+ additionalProperties: false,
2875
+ required: [
2876
+ "lp_position_id",
2877
+ "status",
2878
+ "source_liquidity_use",
2879
+ "pending_reservation_count",
2880
+ "reserved_payout_lamports",
2881
+ "estimated_network_fee_lamports",
2882
+ "reimbursement_due_count",
2883
+ "reimbursement_due_source_count",
2884
+ "reimbursement_due_payout_lamports",
2885
+ "reimbursement_due_network_fee_lamports",
2886
+ "withdrawal_requested_lamports",
2887
+ "withdrawal_landed_lamports",
2888
+ "withdrawal_landed_fee_lamports",
2889
+ "withdrawal_canceled_lamports",
2890
+ "withdrawal_network_fee_loss_lamports",
2891
+ "withdrawal_tx_hashes",
2892
+ "state_version",
2893
+ "updated_at_ms"
2894
+ ],
2895
+ dependentRequired: {
2896
+ redeemable_principal_lamports: [
2897
+ "redeemable_fee_lamports",
2898
+ "lifetime_earned_lamports",
2899
+ "redeemable_principal_source_count",
2900
+ "redeemable_fee_source_count"
2901
+ ],
2902
+ redeemable_fee_lamports: [
2903
+ "redeemable_principal_lamports",
2904
+ "lifetime_earned_lamports",
2905
+ "redeemable_principal_source_count",
2906
+ "redeemable_fee_source_count"
2907
+ ],
2908
+ lifetime_earned_lamports: [
2909
+ "redeemable_principal_lamports",
2910
+ "redeemable_fee_lamports",
2911
+ "redeemable_principal_source_count",
2912
+ "redeemable_fee_source_count"
2913
+ ],
2914
+ redeemable_principal_source_count: [
2915
+ "redeemable_principal_lamports",
2916
+ "redeemable_fee_lamports",
2917
+ "lifetime_earned_lamports",
2918
+ "redeemable_fee_source_count"
2919
+ ],
2920
+ redeemable_fee_source_count: [
2921
+ "redeemable_principal_lamports",
2922
+ "redeemable_fee_lamports",
2923
+ "lifetime_earned_lamports",
2924
+ "redeemable_principal_source_count"
2925
+ ]
2926
+ },
2927
+ properties: {
2928
+ lp_position_id: {
2929
+ $ref: "#/$defs/lpId"
2930
+ },
2931
+ status: {
2932
+ enum: ["idle", "settling", "reimbursement_due"]
2933
+ },
2934
+ source_liquidity_use: {
2935
+ $ref: "#/$defs/sourceLiquidityUse"
2936
+ },
2937
+ redeemable_principal_lamports: {
2938
+ $ref: "#/$defs/safeUInt"
2939
+ },
2940
+ redeemable_fee_lamports: {
2941
+ $ref: "#/$defs/safeUInt"
2942
+ },
2943
+ lifetime_earned_lamports: {
2944
+ $ref: "#/$defs/safeUInt"
2945
+ },
2946
+ redeemable_principal_source_count: {
2947
+ $ref: "#/$defs/safeUInt"
2948
+ },
2949
+ redeemable_fee_source_count: {
2950
+ $ref: "#/$defs/safeUInt"
2951
+ },
2952
+ pending_reservation_count: {
2953
+ $ref: "#/$defs/safeUInt"
2954
+ },
2955
+ reserved_payout_lamports: {
2956
+ $ref: "#/$defs/safeUInt"
2957
+ },
2958
+ estimated_network_fee_lamports: {
2959
+ $ref: "#/$defs/safeUInt"
2960
+ },
2961
+ reimbursement_due_count: {
2962
+ $ref: "#/$defs/safeUInt"
2963
+ },
2964
+ reimbursement_due_source_count: {
2965
+ $ref: "#/$defs/safeUInt"
2966
+ },
2967
+ reimbursement_due_payout_lamports: {
2968
+ $ref: "#/$defs/safeUInt"
2969
+ },
2970
+ reimbursement_due_network_fee_lamports: {
2971
+ $ref: "#/$defs/safeUInt"
2972
+ },
2973
+ withdrawal_requested_lamports: {
2974
+ $ref: "#/$defs/safeUInt"
2975
+ },
2976
+ withdrawal_landed_lamports: {
2977
+ $ref: "#/$defs/safeUInt"
2978
+ },
2979
+ withdrawal_landed_fee_lamports: {
2980
+ $ref: "#/$defs/safeUInt"
2981
+ },
2982
+ withdrawal_canceled_lamports: {
2983
+ $ref: "#/$defs/safeUInt"
2984
+ },
2985
+ withdrawal_network_fee_loss_lamports: {
2986
+ $ref: "#/$defs/safeUInt"
2987
+ },
2988
+ withdrawal_tx_hashes: {
2989
+ type: "array",
2990
+ maxItems: 300,
2991
+ items: {
2992
+ $ref: "#/$defs/lpTxSignature"
2993
+ }
2994
+ },
2995
+ redemption_history: {
2996
+ type: "array",
2997
+ maxItems: 32,
2998
+ items: {
2999
+ $ref: "#/$defs/lpRedemptionHistory"
3000
+ }
3001
+ },
3002
+ redemption_history_has_more: {
3003
+ type: "boolean"
3004
+ },
3005
+ redemption_history_next_cursor: {
3006
+ $ref: "#/$defs/lpRedemptionId"
3007
+ },
3008
+ state_version: {
3009
+ $ref: "#/$defs/safeUInt"
3010
+ },
3011
+ updated_at_ms: {
3012
+ $ref: "#/$defs/safeUInt"
3013
+ }
3014
+ }
3015
+ },
3016
+ lpRedemptionHistory: {
3017
+ type: "object",
3018
+ additionalProperties: false,
3019
+ required: [
3020
+ "redemption_id",
3021
+ "state",
3022
+ "accepted_lamports",
3023
+ "accepted_deposit_principal_lamports",
3024
+ "accepted_earned_fee_lamports",
3025
+ "landed_lamports",
3026
+ "landed_deposit_principal_lamports",
3027
+ "landed_earned_fee_lamports",
3028
+ "canceled_lamports",
3029
+ "canceled_deposit_principal_lamports",
3030
+ "canceled_earned_fee_lamports",
3031
+ "network_fee_lamports",
3032
+ "network_fee_loss_lamports",
3033
+ "failed_attempts",
3034
+ "destination_addresses",
3035
+ "destination_count",
3036
+ "tx_signatures",
3037
+ "tx_signature_count",
3038
+ "details_truncated",
3039
+ "state_version",
3040
+ "created_at_ms",
3041
+ "updated_at_ms"
3042
+ ],
3043
+ properties: {
3044
+ redemption_id: {
3045
+ $ref: "#/$defs/lpRedemptionId"
3046
+ },
3047
+ state: {
3048
+ enum: ["requested", "match_requested", "reserved", "pending", "confirmed", "rejected"]
3049
+ },
3050
+ accepted_lamports: {
3051
+ $ref: "#/$defs/positiveSafeUInt"
3052
+ },
3053
+ accepted_deposit_principal_lamports: {
3054
+ $ref: "#/$defs/safeUInt"
3055
+ },
3056
+ accepted_earned_fee_lamports: {
3057
+ $ref: "#/$defs/safeUInt"
3058
+ },
3059
+ landed_lamports: {
3060
+ $ref: "#/$defs/safeUInt"
3061
+ },
3062
+ landed_deposit_principal_lamports: {
3063
+ $ref: "#/$defs/safeUInt"
3064
+ },
3065
+ landed_earned_fee_lamports: {
3066
+ $ref: "#/$defs/safeUInt"
3067
+ },
3068
+ canceled_lamports: {
3069
+ $ref: "#/$defs/safeUInt"
3070
+ },
3071
+ canceled_deposit_principal_lamports: {
3072
+ $ref: "#/$defs/safeUInt"
3073
+ },
3074
+ canceled_earned_fee_lamports: {
3075
+ $ref: "#/$defs/safeUInt"
3076
+ },
3077
+ network_fee_lamports: {
3078
+ $ref: "#/$defs/safeUInt"
3079
+ },
3080
+ network_fee_loss_lamports: {
3081
+ $ref: "#/$defs/safeUInt"
3082
+ },
3083
+ failed_attempts: {
3084
+ $ref: "#/$defs/safeUInt"
3085
+ },
3086
+ destination_addresses: {
3087
+ type: "array",
3088
+ minItems: 1,
3089
+ maxItems: 1e3,
3090
+ items: {
3091
+ $ref: "#/$defs/lpAddress"
3092
+ }
3093
+ },
3094
+ destination_count: {
3095
+ $ref: "#/$defs/safeUInt"
3096
+ },
3097
+ tx_signatures: {
3098
+ type: "array",
3099
+ maxItems: 32,
3100
+ items: {
3101
+ $ref: "#/$defs/lpTxSignature"
3102
+ }
3103
+ },
3104
+ tx_signature_count: {
3105
+ $ref: "#/$defs/safeUInt"
3106
+ },
3107
+ details_truncated: {
3108
+ type: "boolean"
3109
+ },
3110
+ state_version: {
3111
+ $ref: "#/$defs/safeUInt"
3112
+ },
3113
+ created_at_ms: {
3114
+ $ref: "#/$defs/safeUInt"
3115
+ },
3116
+ updated_at_ms: {
3117
+ $ref: "#/$defs/safeUInt"
3118
+ }
3119
+ }
3120
+ },
3121
+ lpShardView: {
3122
+ type: "object",
3123
+ additionalProperties: false,
3124
+ required: ["lp_position_id", "shard_id", "index", "address", "amount_lamports", "status"],
3125
+ properties: {
3126
+ lp_position_id: {
3127
+ $ref: "#/$defs/lpId"
3128
+ },
3129
+ shard_id: {
3130
+ $ref: "#/$defs/lpShardId"
3131
+ },
3132
+ index: {
3133
+ $ref: "#/$defs/lpShardIndex"
3134
+ },
3135
+ address: {
3136
+ $ref: "#/$defs/lpAddress"
3137
+ },
3138
+ amount_lamports: {
3139
+ $ref: "#/$defs/safeUInt"
3140
+ },
3141
+ required_lamports: {
3142
+ $ref: "#/$defs/positiveSafeUInt"
3143
+ },
3144
+ status: {
3145
+ enum: [
3146
+ "pregenerated",
3147
+ "funding_queued",
3148
+ "funded",
3149
+ "cooling",
3150
+ "available",
3151
+ "reserved",
3152
+ "retired",
3153
+ "redeemed"
3154
+ ]
3155
+ },
3156
+ funding_tx_signature: {
3157
+ $ref: "#/$defs/lpTxSignature"
3158
+ },
3159
+ cooling_until_ms: {
3160
+ $ref: "#/$defs/positiveSafeUInt"
3161
+ },
3162
+ payout_tx_signature: {
3163
+ $ref: "#/$defs/lpTxSignature"
3164
+ }
3165
+ }
3166
+ },
3167
+ lpShardId: {
3168
+ type: "string",
3169
+ minLength: 1,
3170
+ maxLength: 160
3171
+ },
3172
+ lpWithdrawalRequestCase: {
3173
+ type: "object",
3174
+ additionalProperties: false,
3175
+ required: ["case", "value"],
3176
+ properties: {
3177
+ case: {
3178
+ const: "withdrawRequest"
3179
+ },
3180
+ value: {
3181
+ $ref: "#/$defs/lpWithdrawalRequest"
3182
+ }
3183
+ }
3184
+ },
3185
+ lpWithdrawalRequest: {
3186
+ type: "object",
3187
+ additionalProperties: false,
3188
+ required: [
3189
+ "destination_addresses",
3190
+ "deposit_principal_lamports",
3191
+ "earned_fee_lamports",
3192
+ "requested_at_ms"
3193
+ ],
3194
+ properties: {
3195
+ destination_addresses: {
3196
+ type: "array",
3197
+ minItems: 1,
3198
+ maxItems: 1e3,
3199
+ uniqueItems: true,
3200
+ items: {
3201
+ $ref: "#/$defs/lpAddress"
3202
+ }
3203
+ },
3204
+ deposit_principal_lamports: {
3205
+ $ref: "#/$defs/safeUInt"
3206
+ },
3207
+ earned_fee_lamports: {
3208
+ $ref: "#/$defs/safeUInt"
3209
+ },
3210
+ requested_at_ms: {
3211
+ $ref: "#/$defs/positiveSafeUInt"
3212
+ }
3213
+ }
3214
+ },
3215
+ lpWithdrawalExecutionView: {
3216
+ type: "object",
3217
+ additionalProperties: false,
3218
+ required: ["withdrawal_id", "tx_signatures"],
3219
+ properties: {
3220
+ withdrawal_id: {
3221
+ type: "string",
3222
+ minLength: 1,
3223
+ maxLength: 160
3224
+ },
3225
+ tx_signatures: {
3226
+ type: "array",
3227
+ maxItems: 300,
3228
+ items: {
3229
+ $ref: "#/$defs/lpTxSignature"
3230
+ }
3231
+ }
3232
+ }
3233
+ }
3234
+ }
3235
+ };
3236
+
3237
+ // ../../protocol/src/coordinator/envelope.ts
3238
+ var ENVELOPE_VERSION = 2;
3239
+ var MAX_FRAME_SIZE = 256 * 1024;
3240
+ var COORDINATOR_ENVELOPE_SCHEMA = coordinator_envelope_schema_default;
3241
+ var LP_MAX_TARGET_SHARDS = 300;
3242
+ var LP_WITHDRAW_REQUEST_PATH = "lp_command.withdrawRequest";
3243
+ var binaryFieldNames = new Set(COORDINATOR_BINARY_FIELD_NAMES);
3244
+ var ajv = new Ajv2020({
3245
+ allErrors: true,
3246
+ strict: true
3247
+ });
3248
+ var validateWireEnvelope = ajv.compile(coordinator_envelope_schema_default);
3249
+ var textEncoder = new TextEncoder();
3250
+ var textDecoder = new TextDecoder("utf-8", { fatal: true });
3251
+ function encode(env) {
3252
+ return textEncoder.encode(JSON.stringify(toWireEnvelope(env)));
3253
+ }
3254
+ function safeEncode(env) {
3255
+ let wire;
3256
+ try {
3257
+ wire = toWireEnvelope(env);
3258
+ } catch (error) {
3259
+ return {
3260
+ ok: false,
3261
+ error: new CoordinatorError(
3262
+ ErrorCode.ERR_INVALID_PAYLOAD,
3263
+ error instanceof Error ? error.message : String(error)
3264
+ )
3265
+ };
3266
+ }
3267
+ if (!validateWireEnvelope(wire)) {
3268
+ return { ok: false, error: schemaError(wire, validateWireEnvelope.errors ?? []) };
3269
+ }
3270
+ const encoded = textEncoder.encode(JSON.stringify(wire));
3271
+ if (encoded.byteLength > MAX_FRAME_SIZE) {
3272
+ return {
3273
+ ok: false,
3274
+ error: new CoordinatorError(
3275
+ ErrorCode.ERR_INVALID_PAYLOAD,
3276
+ `frame too large: ${encoded.byteLength} > ${MAX_FRAME_SIZE}`
3277
+ )
3278
+ };
3279
+ }
3280
+ return { ok: true, value: encoded };
3281
+ }
3282
+ function encodeValidated(env) {
3283
+ const encoded = safeEncode(env);
3284
+ if (!encoded.ok) {
3285
+ throw encoded.error;
3286
+ }
3287
+ return encoded.value;
3288
+ }
3289
+ function lpSignedCommandEnvelope(command, signature) {
3290
+ switch (command.action) {
3291
+ case "position_get":
3292
+ return { type: CoordinatorMessageType.Sync, payload: { actor: "lp", command, signature } };
3293
+ case "refill":
3294
+ return { type: CoordinatorMessageType.Refill, payload: { actor: "lp", command, signature } };
3295
+ case "position_open":
3296
+ return {
3297
+ type: CoordinatorMessageType.ContractRequest,
3298
+ payload: { actor: "lp", command, signature }
3299
+ };
3300
+ case "withdraw_request":
3301
+ return {
3302
+ type: CoordinatorMessageType.WithdrawRequest,
3303
+ payload: { actor: "lp", command, signature }
3304
+ };
3305
+ default:
3306
+ return { type: CoordinatorMessageType.LpCommand, payload: { command, signature } };
3307
+ }
3308
+ }
3309
+ function encodeLpCommandForSigning(command) {
3310
+ validateLpCommandPreSchemaSemanticsForSigning(command);
3311
+ const { type, payload } = lpSignedCommandEnvelope(command, new Uint8Array(64));
3312
+ const probe = toWireEnvelope({
3313
+ version: ENVELOPE_VERSION,
3314
+ kind: EnvelopeKind.REQUEST,
3315
+ type,
3316
+ swap_id: null,
3317
+ req_id: 0,
3318
+ timestamp_ms: 0,
3319
+ payload
3320
+ });
3321
+ if (!validateWireEnvelope(probe)) {
3322
+ throw schemaError(probe, validateWireEnvelope.errors ?? []);
3323
+ }
3324
+ validateLpCommandSemanticsForSigning(command);
3325
+ return textEncoder.encode(canonicalJsonStringify(toWireValue(command)));
3326
+ }
3327
+ function validateLpCommandPreSchemaSemanticsForSigning(command) {
3328
+ if (command.payload.case !== "withdrawRequest") return;
3329
+ const value = command.payload.value;
3330
+ const destinationAddresses = value.destination_addresses;
3331
+ if (!Array.isArray(destinationAddresses)) return;
3332
+ validateUniqueLpAddresses(
3333
+ LP_WITHDRAW_REQUEST_PATH,
3334
+ destinationAddresses.filter((address) => typeof address === "string")
3335
+ );
3336
+ }
3337
+ function validateLpCommandSemanticsForSigning(command) {
3338
+ switch (command.payload.case) {
3339
+ case "positionOpen": {
3340
+ const { addresses, refill_threshold, shard_count } = command.payload.value;
3341
+ if (refill_threshold > shard_count) {
3342
+ throw new CoordinatorError(
3343
+ ErrorCode.ERR_INVALID_PAYLOAD,
3344
+ `lp_command.positionOpen.refill_threshold (${refill_threshold}) must be <= shard_count (${shard_count})`
3345
+ );
3346
+ }
3347
+ validateOptionalLpAddressCount("lp_command.positionOpen", addresses, shard_count);
3348
+ return;
3349
+ }
3350
+ case "refill": {
3351
+ const { addresses, shard_count } = command.payload.value;
3352
+ validateOptionalLpAddressCount("lp_command.refill", addresses, shard_count);
3353
+ return;
3354
+ }
3355
+ case "dkgFinalize": {
3356
+ const { addresses, start_index } = command.payload.value;
3357
+ validateLpDkgFinalizeRange("lp_command.dkgFinalize", start_index, addresses.length);
3358
+ return;
3359
+ }
3360
+ case "withdrawRequest": {
3361
+ const { deposit_principal_lamports, earned_fee_lamports, destination_addresses } = command.payload.value;
3362
+ const bucketTotal = deposit_principal_lamports + earned_fee_lamports;
3363
+ if (!Number.isSafeInteger(bucketTotal) || bucketTotal <= 0) {
3364
+ throw new CoordinatorError(
3365
+ ErrorCode.ERR_INVALID_PAYLOAD,
3366
+ `${LP_WITHDRAW_REQUEST_PATH} bucket claim must be positive`
3367
+ );
3368
+ }
3369
+ validateUniqueLpAddresses(LP_WITHDRAW_REQUEST_PATH, destination_addresses);
3370
+ return;
3371
+ }
3372
+ default:
3373
+ return;
3374
+ }
3375
+ }
3376
+ function validateOptionalLpAddressCount(path, addresses, shardCount) {
3377
+ if (addresses.length > 0 && addresses.length !== shardCount) {
3378
+ throw new CoordinatorError(
3379
+ ErrorCode.ERR_INVALID_PAYLOAD,
3380
+ `${path}.addresses length (${addresses.length}) must be 0 or equal shard_count (${shardCount})`
3381
+ );
3382
+ }
3383
+ }
3384
+ function validateUniqueLpAddresses(path, addresses) {
3385
+ const seen = /* @__PURE__ */ new Set();
3386
+ for (const address of addresses) {
3387
+ if (seen.has(address)) {
3388
+ throw new CoordinatorError(
3389
+ ErrorCode.ERR_INVALID_PAYLOAD,
3390
+ `${path} duplicate destination address: ${address}`
3391
+ );
3392
+ }
3393
+ seen.add(address);
3394
+ }
3395
+ }
3396
+ function validateLpDkgFinalizeRange(path, startIndex, addressCount) {
3397
+ if (addressCount === 0) return;
3398
+ const lastIndex = startIndex + addressCount - 1;
3399
+ if (lastIndex >= LP_MAX_TARGET_SHARDS) {
3400
+ throw new CoordinatorError(
3401
+ ErrorCode.ERR_INVALID_PAYLOAD,
3402
+ `${path}.addresses end index (${lastIndex}) must be <= ${LP_MAX_TARGET_SHARDS - 1}`
3403
+ );
3404
+ }
3405
+ }
3406
+ function decode(raw) {
3407
+ if (raw.byteLength === 0) {
3408
+ return {
3409
+ ok: false,
3410
+ error: new CoordinatorError(ErrorCode.ERR_INVALID_PAYLOAD, "empty frame")
3411
+ };
3412
+ }
3413
+ if (raw.byteLength > MAX_FRAME_SIZE) {
3414
+ return {
3415
+ ok: false,
3416
+ error: new CoordinatorError(
3417
+ ErrorCode.ERR_INVALID_PAYLOAD,
3418
+ `frame too large: ${raw.byteLength} > ${MAX_FRAME_SIZE}`
3419
+ )
3420
+ };
3421
+ }
3422
+ let parsed;
3423
+ try {
3424
+ parsed = JSON.parse(textDecoder.decode(raw));
3425
+ } catch (error) {
3426
+ return {
3427
+ ok: false,
3428
+ error: new CoordinatorError(
3429
+ ErrorCode.ERR_INVALID_PAYLOAD,
3430
+ `json decode failed: ${error instanceof Error ? error.message : String(error)}`
3431
+ )
3432
+ };
3433
+ }
3434
+ if (!validateWireEnvelope(parsed)) {
3435
+ return { ok: false, error: schemaError(parsed, validateWireEnvelope.errors ?? []) };
3436
+ }
3437
+ try {
3438
+ return { ok: true, value: fromWireEnvelope(parsed) };
3439
+ } catch (error) {
3440
+ if (error instanceof CoordinatorError) {
3441
+ return { ok: false, error };
3442
+ }
3443
+ return {
3444
+ ok: false,
3445
+ error: new CoordinatorError(
3446
+ ErrorCode.ERR_INVALID_PAYLOAD,
3447
+ error instanceof Error ? error.message : String(error)
3448
+ )
3449
+ };
3450
+ }
3451
+ }
3452
+ function toWireEnvelope(env) {
3453
+ return stripUndefined({
3454
+ version: env.version,
3455
+ kind: env.kind,
3456
+ type: env.type,
3457
+ swap_id: env.swap_id,
3458
+ req_id: env.req_id,
3459
+ timestamp_ms: env.timestamp_ms,
3460
+ payload: toWireValue(env.payload)
3461
+ });
3462
+ }
3463
+ function fromWireEnvelope(value) {
3464
+ if (!isRecord2(value)) {
3465
+ throw new CoordinatorError(ErrorCode.ERR_INVALID_PAYLOAD, "envelope must be an object");
3466
+ }
3467
+ const kind = value.kind;
3468
+ const type = value.type;
3469
+ const swapId = value.swap_id;
3470
+ const payload = fromWireValue(value.payload);
3471
+ if (kind === EnvelopeKind.RESPONSE && type === CoordinatorMessageType.ContractRequest && swapId !== null && isRecord2(payload) && typeof payload.swap_id === "string" && payload.swap_id !== swapId) {
3472
+ throw new CoordinatorError(
3473
+ ErrorCode.ERR_INVALID_PAYLOAD,
3474
+ `ContractRequest response: envelope.swap_id (${swapId}) does not match payload.swap_id (${payload.swap_id})`
3475
+ );
3476
+ }
3477
+ return {
3478
+ version: value.version,
3479
+ kind,
3480
+ type,
3481
+ swap_id: swapId,
3482
+ req_id: value.req_id,
3483
+ timestamp_ms: value.timestamp_ms,
3484
+ payload
3485
+ };
3486
+ }
3487
+ function toWireValue(value, fieldName) {
3488
+ if (value instanceof Uint8Array) {
3489
+ return bytesToBase64Url(value);
3490
+ }
3491
+ if (Array.isArray(value)) {
3492
+ return value.map((item) => toWireValue(item));
3493
+ }
3494
+ if (isRecord2(value)) {
3495
+ const out = {};
3496
+ for (const [key, item] of Object.entries(value)) {
3497
+ if (item !== void 0) {
3498
+ out[key] = toWireValue(item, key);
3499
+ }
3500
+ }
3501
+ return out;
3502
+ }
3503
+ if (fieldName !== void 0 && binaryFieldNames.has(fieldName) && typeof value !== "string") {
3504
+ return value;
3505
+ }
3506
+ return value;
3507
+ }
3508
+ function fromWireValue(value, fieldName) {
3509
+ if (typeof value === "string" && fieldName !== void 0 && binaryFieldNames.has(fieldName)) {
3510
+ return base64UrlToBytes(value, fieldName);
3511
+ }
3512
+ if (Array.isArray(value)) {
3513
+ return value.map((item) => fromWireValue(item));
3514
+ }
3515
+ if (isRecord2(value)) {
3516
+ const out = {};
3517
+ for (const [key, item] of Object.entries(value)) {
3518
+ out[key] = fromWireValue(item, key);
3519
+ }
3520
+ return out;
3521
+ }
3522
+ return value;
3523
+ }
3524
+ function schemaError(value, errors) {
3525
+ const code = kindTypeMismatch(value) ? ErrorCode.ERR_UNEXPECTED_TYPE : ErrorCode.ERR_INVALID_PAYLOAD;
3526
+ const expectedIssue = expectedProtocolIssue(value);
3527
+ const formatted = formatErrors(value, errors);
3528
+ const details = expectedIssue === null || formatted.includes(expectedIssue) ? formatted : `${expectedIssue}; ${formatted}`;
3529
+ return new CoordinatorError(code, `json schema validation failed: ${details}`);
3530
+ }
3531
+ function kindTypeMismatch(value) {
3532
+ if (!isRecord2(value)) return false;
3533
+ const kind = value.kind;
3534
+ const type = value.type;
3535
+ return isValidEnvelopeKind(kind) && isValidMessageType(type) && !isAllowedKindForType(kind, type);
3536
+ }
3537
+ function formatErrors(value, errors) {
3538
+ if (errors.length === 0) return "no details";
3539
+ const relevant = relevantErrors(errors);
3540
+ return relevant.slice(0, 8).map((error) => formatError(value, error)).filter(uniqueStrings).join("; ");
3541
+ }
3542
+ function expectedProtocolIssue(value) {
3543
+ if (!isRecord2(value)) return null;
3544
+ if (!isValidEnvelopeKind(value.kind)) return "kind has an unknown value";
3545
+ if (!isValidMessageType(value.type)) return "type has an unknown value";
3546
+ if (!isAllowedKindForType(value.kind, value.type)) {
3547
+ return `kind ${String(value.kind)} is not allowed for type ${String(value.type)}`;
3548
+ }
3549
+ const payload = value.payload;
3550
+ if (!isRecord2(payload)) return `${payloadPathRoot(value)} must be object`;
3551
+ const required = requiredPayloadFields(value.kind, value.type);
3552
+ const missing = required.filter((field) => !(field in payload));
3553
+ return missing.length > 0 ? missing.map((field) => `${payloadPathRoot(value)}.${field} is required`).join("; ") : null;
3554
+ }
3555
+ function requiredPayloadFields(kind, type) {
3556
+ const key = `${kind}:${type}`;
3557
+ switch (key) {
3558
+ case "request:ContractRequest":
3559
+ return ["actor", "amount_lamports", "payout_spec", "sync_commitment"];
3560
+ case "response:ContractRequest":
3561
+ return ["swap_id", "accepted", "policy_snapshot"];
3562
+ case "response:DkgInit":
3563
+ return ["ready"];
3564
+ case "request:DkgRound1":
3565
+ case "response:DkgRound2":
3566
+ case "request:DkgRound2":
3567
+ return ["package"];
3568
+ case "response:DkgRound1":
3569
+ return ["package", "round2_for_alice"];
3570
+ case "push:DkgComplete":
3571
+ return ["joint_pubkey", "deposit_expires_at_ms"];
3572
+ case "request:DelegateShare":
3573
+ return ["share", "recovery_commitment"];
3574
+ case "response:DelegateShare":
3575
+ return ["delegated_at_ms"];
3576
+ case "push:DelegateAck":
3577
+ return ["delegated_at_ms", "spending_policy"];
3578
+ case "push:DepositConfirmed":
3579
+ return ["tx_signature", "amount_lamports", "observed_at_ms"];
3580
+ case "request:Sync":
3581
+ return ["actor"];
3582
+ case "response:Sync":
3583
+ return ["state", "fragments_executed"];
3584
+ case "push:SyncRequired":
3585
+ return ["subject", "event_kind", "state_version", "created_at_ms", "sync_required"];
3586
+ case "request:Refill":
3587
+ return ["actor", "command", "signature"];
3588
+ case "response:Refill":
3589
+ return ["case", "value"];
3590
+ case "push:PayoutExecuted":
3591
+ return [
3592
+ "index",
3593
+ "amount_lamports",
3594
+ "destination_address",
3595
+ "tx_signature",
3596
+ "executed_at_ms",
3597
+ "final"
3598
+ ];
3599
+ case "request:WithdrawRequest":
3600
+ return ["recovery_code", "actor", "command", "signature"];
3601
+ case "response:WithdrawRequest":
3602
+ return ["case", "value"];
3603
+ case "request:LpCommand":
3604
+ return ["command", "signature"];
3605
+ case "response:LpCommand":
3606
+ return ["case", "value"];
3607
+ case "response:WithdrawExecuted":
3608
+ case "push:WithdrawExecuted":
3609
+ return [
3610
+ "tx_signature",
3611
+ "amount_lamports",
3612
+ "withdrawn_at_ms",
3613
+ "trigger",
3614
+ "destination_address"
3615
+ ];
3616
+ case "response:WithdrawAccepted":
3617
+ return ["status", "amount_lamports", "destination_address", "accepted_at_ms"];
3618
+ case "error:Error":
3619
+ return ["code", "message"];
3620
+ default:
3621
+ return [];
3622
+ }
3623
+ }
3624
+ function relevantErrors(errors) {
3625
+ const lpCommandErrors = errors.filter(
3626
+ (error) => error.instancePath.startsWith("/payload/command") || error.instancePath.startsWith("/payload/signature")
3627
+ );
3628
+ const specificLpCommandErrors = lpCommandErrors.filter(
3629
+ (error) => error.keyword !== "const" && error.keyword !== "required"
3630
+ );
3631
+ if (specificLpCommandErrors.length > 0) return specificLpCommandErrors;
3632
+ if (lpCommandErrors.length > 0) return lpCommandErrors;
3633
+ const actorSyncErrors = errors.filter(
3634
+ (error) => error.instancePath.startsWith("/payload/actor_sync")
3635
+ );
3636
+ if (actorSyncErrors.length > 0) return actorSyncErrors;
3637
+ const payloadErrors = errors.filter((error) => error.instancePath.startsWith("/payload"));
3638
+ const requiredPayloadErrors = errors.filter(
3639
+ (error) => error.keyword === "required" && error.instancePath === "/payload"
3640
+ );
3641
+ const topLevelErrors = errors.filter((error) => !error.schemaPath.includes("/oneOf/"));
3642
+ const relevant = [...payloadErrors, ...requiredPayloadErrors, ...topLevelErrors];
3643
+ return relevant.length > 0 ? relevant : errors;
3644
+ }
3645
+ function formatError(value, error) {
3646
+ const path = pathForError(value, error);
3647
+ switch (error.keyword) {
3648
+ case "required":
3649
+ return `${path} is required`;
3650
+ case "additionalProperties":
3651
+ return `${path} is not allowed`;
3652
+ case "const":
3653
+ return `${path} has the wrong literal value`;
3654
+ case "enum":
3655
+ return `${path} has an unknown value`;
3656
+ case "oneOf":
3657
+ case "anyOf":
3658
+ return `${path} does not match any allowed protocol shape`;
3659
+ case "pattern":
3660
+ return `${path} must use base64url without padding`;
3661
+ case "minLength":
3662
+ case "maxLength":
3663
+ case "minimum":
3664
+ case "maximum":
3665
+ case "type":
3666
+ return `${path} ${error.message ?? "is invalid"}`;
3667
+ default:
3668
+ return `${path} ${error.message ?? error.keyword}`;
3669
+ }
3670
+ }
3671
+ function pathForError(value, error) {
3672
+ const parts = pointerParts(error.instancePath);
3673
+ if (error.keyword === "required" && typeof error.params.missingProperty === "string") {
3674
+ parts.push(error.params.missingProperty);
3675
+ }
3676
+ if (error.keyword === "additionalProperties" && typeof error.params.additionalProperty === "string") {
3677
+ parts.push(error.params.additionalProperty);
3678
+ }
3679
+ if (parts[0] === "payload") {
3680
+ const payloadRoot = payloadPathRoot(value);
3681
+ const rest = parts.slice(1);
3682
+ return rest.length > 0 ? `${payloadRoot}.${rest.join(".")}` : payloadRoot;
3683
+ }
3684
+ return parts.length > 0 ? parts.join(".") : "envelope";
3685
+ }
3686
+ function payloadPathRoot(value) {
3687
+ if (!isRecord2(value) || typeof value.type !== "string") {
3688
+ return "payload";
3689
+ }
3690
+ const base = camelToSnake(value.type);
3691
+ if (value.kind === EnvelopeKind.RESPONSE) return `${base}_response`;
3692
+ return base;
3693
+ }
3694
+ function pointerParts(pointer) {
3695
+ if (pointer === "") return [];
3696
+ return pointer.split("/").slice(1).map((part) => part.replace(/~1/g, "/").replace(/~0/g, "~"));
3697
+ }
3698
+ function camelToSnake(value) {
3699
+ return value.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
3700
+ }
3701
+ function uniqueStrings(value, index, all) {
3702
+ return all.indexOf(value) === index;
3703
+ }
3704
+ function isRecord2(value) {
3705
+ return typeof value === "object" && value !== null && !Array.isArray(value);
3706
+ }
3707
+ function stripUndefined(value) {
3708
+ const out = {};
3709
+ for (const [key, item] of Object.entries(value)) {
3710
+ if (item !== void 0) {
3711
+ out[key] = item;
3712
+ }
3713
+ }
3714
+ return out;
3715
+ }
3716
+ function canonicalJsonStringify(value) {
3717
+ if (value === null || typeof value === "boolean" || typeof value === "number" || typeof value === "string") {
3718
+ return JSON.stringify(value);
3719
+ }
3720
+ if (Array.isArray(value)) {
3721
+ return `[${value.map((item) => canonicalJsonStringify(item)).join(",")}]`;
3722
+ }
3723
+ if (isRecord2(value)) {
3724
+ return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonicalJsonStringify(value[key])}`).join(",")}}`;
3725
+ }
3726
+ throw new CoordinatorError(ErrorCode.ERR_INVALID_PAYLOAD, "LP command contains non-JSON value");
3727
+ }
3728
+ function bytesToBase64Url(bytes) {
3729
+ let binary = "";
3730
+ const chunkSize = 32768;
3731
+ for (let i = 0; i < bytes.length; i += chunkSize) {
3732
+ binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
3733
+ }
3734
+ return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/u, "");
3735
+ }
3736
+ function base64UrlToBytes(value, field) {
3737
+ const remainder = value.length % 4;
3738
+ if (remainder === 1) {
3739
+ throw new CoordinatorError(ErrorCode.ERR_INVALID_PAYLOAD, `${field} is invalid base64url`);
3740
+ }
3741
+ const padded = value.replace(/-/g, "+").replace(/_/g, "/") + "=".repeat((4 - remainder) % 4);
3742
+ const binary = atob(padded);
3743
+ const out = new Uint8Array(binary.length);
3744
+ for (let i = 0; i < binary.length; i += 1) {
3745
+ out[i] = binary.charCodeAt(i);
3746
+ }
3747
+ return out;
3748
+ }
3749
+ var asSwapRequestPayload = (e) => e.payload;
3750
+ var asSwapRequestResponsePayload = (e) => e.payload;
3751
+ var asDkgInitPayload = (e) => e.payload;
3752
+ var asDkgInitResponsePayload = (e) => e.payload;
3753
+ var asDkgRound1Payload = (e) => e.payload;
3754
+ var asDkgRound1ResponsePayload = (e) => e.payload;
3755
+ var asDkgRound2Payload = (e) => e.payload;
3756
+ var asDkgRound2ResponsePayload = (e) => e.payload;
3757
+ var asDkgCompletePayload = (e) => e.payload;
3758
+ var asDelegateSharePayload = (e) => e.payload;
3759
+ var asDelegateShareResponsePayload = (e) => e.payload;
3760
+ var asDelegateAckPayload = (e) => e.payload;
3761
+ var asDepositConfirmedPayload = (e) => e.payload;
3762
+ var asSyncUserRequestPayload = (e) => e.payload;
3763
+ var asSyncUserResponsePayload = (e) => e.payload;
3764
+ var asSyncRequiredPayload = (e) => e.payload;
3765
+ var asPayoutExecutedPayload = (e) => e.payload;
3766
+ var asWithdrawRequestPayload = (e) => e.payload;
3767
+ var asWithdrawAcceptedPayload = (e) => e.payload;
3768
+ var asWithdrawExecutedPayload = (e) => e.payload;
3769
+ var asLpCommandResponsePayload = (e) => e.payload;
3770
+ var asErrorPayload = (e) => {
3771
+ const payload = e.payload;
3772
+ if (!isValidErrorCode(payload.code)) {
3773
+ throw new CoordinatorError(
3774
+ ErrorCode.ERR_INVALID_PAYLOAD,
3775
+ `unknown error code ${String(payload.code)}`
3776
+ );
3777
+ }
3778
+ return payload;
3779
+ };
3780
+
3781
+ export { COORDINATOR_BINARY_FIELD_NAMES, COORDINATOR_ENVELOPE_SCHEMA, COORDINATOR_SCHEMA_PAYLOAD_CATEGORIES, CoordinatorError, CoordinatorMessageType, DEFAULT_PAYOUT_WINDOW_ID, DIRECTION_BY_TYPE, ENVELOPE_VERSION, EnvelopeKind, ErrorCode, INITIAL_STATE, MAX_FRAME_SIZE, MessageDirection, PAYOUT_WINDOW_OPTIONS, TERMINAL_STATES, TRANSITIONS, VALID_SWAP_STATES, asDelegateAckPayload, asDelegateSharePayload, asDelegateShareResponsePayload, asDepositConfirmedPayload, asDkgCompletePayload, asDkgInitPayload, asDkgInitResponsePayload, asDkgRound1Payload, asDkgRound1ResponsePayload, asDkgRound2Payload, asDkgRound2ResponsePayload, asErrorPayload, asLpCommandResponsePayload, asPayoutExecutedPayload, asSwapRequestPayload, asSwapRequestResponsePayload, asSyncRequiredPayload, asSyncUserRequestPayload, asSyncUserResponsePayload, asWithdrawAcceptedPayload, asWithdrawExecutedPayload, asWithdrawRequestPayload, decode, encode, encodeLpCommandForSigning, encodeValidated, isAllowedInState, isAllowedKindForType, isPayoutWindowId, isValidEnvelopeKind, isValidErrorCode, isValidMessageType, isValidSwapState, lpSignedCommandEnvelope, nextState, payloadCategory, payoutWindowLabelForId, payoutWindowOptionById, safeEncode, scheduledPayoutWindowOptions, scheduledPayoutWindowSeconds };
3782
+ //# sourceMappingURL=chunk-LBC7ALYO.js.map
3783
+ //# sourceMappingURL=chunk-LBC7ALYO.js.map