@invisible-labs/sdk 0.6.0-devnet.4 → 0.6.0-devnet.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +70 -13
  2. package/dist/.invisible-sdk-build-target.json +2 -2
  3. package/dist/{chunk-UADJCUPK.js → chunk-3MDHAS4D.js} +6 -5
  4. package/dist/chunk-3MDHAS4D.js.map +1 -0
  5. package/dist/{chunk-E4DCEDAG.js → chunk-3TGQ4CZ3.js} +178 -2
  6. package/dist/chunk-3TGQ4CZ3.js.map +1 -0
  7. package/dist/chunk-DCTYBUY3.js +258 -0
  8. package/dist/chunk-DCTYBUY3.js.map +1 -0
  9. package/dist/{chunk-NHEHCUS5.js → chunk-EBWTAXNT.js} +3 -3
  10. package/dist/{chunk-NHEHCUS5.js.map → chunk-EBWTAXNT.js.map} +1 -1
  11. package/dist/{chunk-V5HXJRBW.js → chunk-HJFMDMPY.js} +3 -3
  12. package/dist/chunk-HJFMDMPY.js.map +1 -0
  13. package/dist/{chunk-GHBQF3A7.js → chunk-QRN46R3F.js} +5 -2
  14. package/dist/chunk-QRN46R3F.js.map +1 -0
  15. package/dist/chunk-Y7AK6RBV.js +4189 -0
  16. package/dist/chunk-Y7AK6RBV.js.map +1 -0
  17. package/dist/{coordinator-ZNLGAYXD.js → coordinator-PEWHNZ7I.js} +3 -3
  18. package/dist/{coordinator-ZNLGAYXD.js.map → coordinator-PEWHNZ7I.js.map} +1 -1
  19. package/dist/events.js +3 -3
  20. package/dist/index.d.ts +14 -4
  21. package/dist/index.js +74 -11
  22. package/dist/index.js.map +1 -1
  23. package/dist/lp.d.ts +33 -9
  24. package/dist/lp.js +305 -45
  25. package/dist/lp.js.map +1 -1
  26. package/dist/presets.d.ts +6 -2
  27. package/dist/presets.js +8 -8
  28. package/dist/presets.js.map +1 -1
  29. package/dist/stats.js +1 -1
  30. package/dist/storage.js +2 -243
  31. package/dist/storage.js.map +1 -1
  32. package/dist/{types.generated-DJBTjLpP.d.ts → types.generated-YhFu9ZBR.d.ts} +29 -4
  33. package/dist/user.d.ts +31 -4
  34. package/dist/user.js +409 -57
  35. package/dist/user.js.map +1 -1
  36. package/package.json +4 -1
  37. package/dist/chunk-5NNEO7IK.js +0 -1150
  38. package/dist/chunk-5NNEO7IK.js.map +0 -1
  39. package/dist/chunk-BRDLLGCZ.js +0 -59
  40. package/dist/chunk-BRDLLGCZ.js.map +0 -1
  41. package/dist/chunk-DIA6U2CV.js +0 -1927
  42. package/dist/chunk-DIA6U2CV.js.map +0 -1
  43. package/dist/chunk-E4DCEDAG.js.map +0 -1
  44. package/dist/chunk-GHBQF3A7.js.map +0 -1
  45. package/dist/chunk-UADJCUPK.js.map +0 -1
  46. package/dist/chunk-V5HXJRBW.js.map +0 -1
package/README.md CHANGED
@@ -19,21 +19,28 @@ coordinator. The normal-user surface uses one functional sequence over a caller-
19
19
  npm install @invisible-labs/sdk@devnet
20
20
  ```
21
21
 
22
+ The public package requires Node.js 22 or newer for server runtimes. Browser consumers need Web
23
+ Crypto and WebSocket. The default transport uses the platform `WebSocket`.
24
+
22
25
  ## Session boundary
23
26
 
24
27
  The public artifact exposes a production-only root facade:
25
28
 
26
29
  ```ts
27
30
  import { closeSession, createSession } from "@invisible-labs/sdk";
31
+ import { INVISIBLE_COORDINATOR_URL } from "@invisible-labs/sdk/presets";
28
32
 
29
- const session = await createSession({
30
- coordinatorUrl: "wss://tee-azure.invisible.exchange/ws-noise",
31
- });
33
+ const session = await createSession({ coordinatorUrl: INVISIBLE_COORDINATOR_URL });
32
34
 
33
35
  // Use the session with @invisible-labs/sdk/user or /lp, then:
34
36
  closeSession(session);
35
37
  ```
36
38
 
39
+ The public constructor uses `coordinatorUrl` to open and attest the session. Its accepted `storage`
40
+ and `wallet` fields are compatibility-only and are not consumed by the published constructor, so
41
+ they do not enable persistence or signing. Persist recovery material explicitly through the user
42
+ callbacks and your own storage boundary.
43
+
37
44
  The public build embeds the approved production attestation tuple. It accepts a
38
45
  non-loopback `wss://` URL, but it does not accept `requiredMode`, a coordinator
39
46
  pool, release-pin overrides, or local-attestation controls. The private
@@ -71,8 +78,9 @@ import {
71
78
  requestRefund,
72
79
  sync,
73
80
  } from "@invisible-labs/sdk/user";
81
+ import { INVISIBLE_COORDINATOR_URL } from "@invisible-labs/sdk/presets";
74
82
 
75
- const coordinatorUrl = "wss://tee-azure.invisible.exchange/ws-noise";
83
+ const coordinatorUrl = INVISIBLE_COORDINATOR_URL;
76
84
  const payoutPolicy = {
77
85
  destinations: [{ address: receiverPublicKey, sharePercent: 100 }],
78
86
  totalDeadlineMs: 0,
@@ -102,16 +110,17 @@ try {
102
110
 
103
111
  `contractRequest` resolves after durable delegation and before the deposit is observed. Its result
104
112
  contains `swapId`, `depositAddress`, `depositExpiresAtMs`, `syncSecret`, and the generated Recovery
105
- Code. Persist the sync secret and Recovery Code before asking the user to deposit. Send exactly
113
+ Code. With client persistence disabled, persist the sync secret and Recovery Code before asking the user to deposit. In opt-in mode, the SDK writes them at the flow boundaries. Send exactly
106
114
  `amountLamports` to the deposit address. The `sync` response contains actor-scoped lifecycle truth
107
115
  and the coordinator response timestamp; it does not expose an internal payout due timestamp. A
108
116
  refund receipt also carries the coordinator-authoritative accepted amount; use that value for
109
117
  pending refund accounting and use `sync` for final landing, cancellation, and fee-loss outcomes.
110
118
 
111
- For crash recovery before delegation, persist `policySnapshot` from the contract result and call
112
- `resumePreDelegation` with it, the `resumeSecret`, and the Recovery Code commitment. When resuming
113
- DKG, `onDelegateShareReady` is invoked before `DkgRound2` so the generated Alice share can be
114
- persisted. Resume fails closed if the coordinator acknowledges a different spending policy.
119
+ For crash recovery before delegation, the automatic mode does not persist `resumeSecret` or
120
+ delegate shares. With client persistence disabled, persist `policySnapshot` from the contract result
121
+ and call `resumePreDelegation` with it, the `resumeSecret`, and the Recovery Code commitment. When
122
+ resuming DKG, `onDelegateShareReady` is invoked before `DkgRound2` so the generated Alice share can
123
+ be persisted. Resume fails closed if the coordinator acknowledges a different spending policy.
115
124
 
116
125
  ## Private-transfer amount limits
117
126
 
@@ -147,8 +156,43 @@ Other target surfaces throw `NotImplementedError` until each is finalized. No co
147
156
 
148
157
  ## Recovery material and storage
149
158
 
150
- The SDK does not persist recovery material automatically. Consumers that need restart or tab-close
151
- recovery must store the relevant values themselves:
159
+ Client persistence is opt-in. `clientPersistence: false` or an omitted option performs no storage
160
+ reads or writes; supplying `storage` alone does not enable it. Enable SDK-owned normal-user records
161
+ with the public session option:
162
+
163
+ ```ts
164
+ const session = await createSession({
165
+ coordinatorUrl,
166
+ clientPersistence: true,
167
+ });
168
+ ```
169
+
170
+ With the option enabled, an explicit `StorageAdapter` takes precedence. Without one, the SDK uses
171
+ `chrome.storage.local` in a Chrome extension or `localStorage` in an ordinary web page. An extension
172
+ never falls back to page storage, and unsupported or denied storage fails before the coordinator
173
+ connection opens.
174
+
175
+ The `/user` entrypoint exposes `listPersistedTransfers`, `getPersistedTransfer`,
176
+ `removePersistedTransfer`, and `purgePersistedTransfers`. Listing and reading are local-only and
177
+ never trigger sync, refund, resume, signing, or another mutation. A fresh session must still run
178
+ Noise and attestation before the caller explicitly passes restored bytes to `sync` or
179
+ `requestRefund`.
180
+
181
+ Each SDK-owned record contains one `swapId`, bounded amount/destination/policy metadata, lifecycle
182
+ timestamps, optional coordinator `stateVersion`, `syncSecret`, and the Recovery Code when the SDK
183
+ generated or received it. It never stores `resumeSecret`, delegate shares, LP or wallet material,
184
+ transport or attestation state, UI state, raw transactions, or logs. Terminal records retain
185
+ bounded non-secret metadata and remove bearer secrets. The limits are 64 KiB per record, 100
186
+ records, and 1 MiB for the SDK namespace.
187
+
188
+ The storage is not a secure vault. Browser and extension storage may be readable or removable by
189
+ same-origin JavaScript, a compromised extension, XSS, local profile access, or browser/profile
190
+ clearing. Use a caller-controlled encrypted adapter when that tradeoff is unacceptable. If a write
191
+ fails after coordinator acceptance, the SDK throws a typed `StorageError` with the affected swap
192
+ and `remoteAccepted: true`; do not blindly retry the coordinator command.
193
+
194
+ When client persistence is disabled, consumers that need restart or tab-close recovery must store
195
+ the relevant values themselves:
152
196
 
153
197
  | Material | Needed for | Guidance |
154
198
  | ----------------------- | ----------------------------------------------- | ------------------------------------------------------- |
@@ -156,8 +200,11 @@ recovery must store the relevant values themselves:
156
200
  | User Recovery Code | Request an origin-bound private-transfer refund | Treat it like a seed phrase and persist it immediately. |
157
201
  | LP Position Code | Recover and operate an LP position | Keep it out of logs and treat it like a seed phrase. |
158
202
 
159
- The web app persists private-transfer recovery material locally so users can resume flows. SDK
160
- consumers can choose `browserStorage`, `extensionStorage`, or their own encrypted storage layer.
203
+ The web app persists private-transfer recovery material locally as a product tradeoff so users can
204
+ resume flows. SDK consumers can opt in, choose `browserStorage` or `extensionStorage`, or provide
205
+ their own encrypted storage layer for caller-managed material. Browser and extension adapters store
206
+ caller-supplied bytes; they do not encrypt them. Encrypt before storing or use an access-controlled
207
+ vault for production recovery material.
161
208
 
162
209
  LP DKG requests are bounded. If a browser worker stops responding, the SDK terminates the local
163
210
  workers and attempts the signed position-scoped DKG abort command within a bounded cleanup window
@@ -233,3 +280,13 @@ commands so the selected build target and artifact marker are checked.
233
280
 
234
281
  Private smoke runs may rebuild the private SDK. Public smoke must use
235
282
  `--prebuilt-sdk` so it cannot rebuild after the public artifact gates.
283
+
284
+ For an explicitly authorized Devnet deposit-address probe, append
285
+ `--no-fund-contract-probe --payout-address <public-key>` to the private smoke with
286
+ `--runtime-profile prod-devnet-simplified-locked`. It requests the SDK minimum
287
+ entry amount, completes DKG/delegation, and requires authenticated Sync to report
288
+ `awaiting_deposit`. It creates an unfunded coordinator contract; it sends no SOL.
289
+ The `TEE_NO_FUND_CONTRACT_SMOKE` receipt contains only the deposit address, expiry,
290
+ and status. Recovery material stays in process memory and is discarded on exit.
291
+ Never fund this disposable probe address. This proves address preparation only,
292
+ not deposit observation, settlement, persistence after restart, or Owner isolation.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "package": "@invisible-labs/sdk",
3
3
  "target": "public",
4
- "productionBinaryHash": "92237e1cc0d528a12288ff5a2d99eff38ab387080d31eb4146526daa6c973860",
4
+ "productionBinaryHash": "e08f16d1c303f402e8210302e85ff8ce497e4152d7511143849c7771d56a1663",
5
5
  "productionMrtd": "a2e61f1316e9e367e9c1f7a0adc98c48eb13875c399c85d286cee5ea2b05e57f2df9f61d903b0ae7ac5607d843dfffc1",
6
- "productionRuntimeProfile": "prod-devnet-simplified"
6
+ "productionRuntimeProfile": "prod-devnet-simplified-locked"
7
7
  }
@@ -1,5 +1,5 @@
1
- import { getSessionState } from './chunk-V5HXJRBW.js';
2
- import { TransportError, CommandError } from './chunk-GHBQF3A7.js';
1
+ import { getSessionState } from './chunk-HJFMDMPY.js';
2
+ import { TransportError, CommandError } from './chunk-QRN46R3F.js';
3
3
 
4
4
  // src/coordinator/protocolClient.ts
5
5
  var MAX_COORDINATOR_REQUEST_ID = 4294967295;
@@ -12,6 +12,7 @@ var EnvelopeKind = {
12
12
  };
13
13
  var CoordinatorMessageType = {
14
14
  ContractRequest: "ContractRequest",
15
+ LpCreateAuthorization: "LpCreateAuthorization",
15
16
  DkgInit: "DkgInit",
16
17
  DkgRound1: "DkgRound1",
17
18
  DkgRound2: "DkgRound2",
@@ -31,7 +32,7 @@ var CoordinatorMessageType = {
31
32
  };
32
33
  var protocolPromise = null;
33
34
  function loadProtocol() {
34
- protocolPromise ??= import('./coordinator-ZNLGAYXD.js');
35
+ protocolPromise ??= import('./coordinator-PEWHNZ7I.js');
35
36
  return protocolPromise;
36
37
  }
37
38
  var runtimes = /* @__PURE__ */ new WeakMap();
@@ -324,5 +325,5 @@ function rejectAll(runtime, error) {
324
325
  }
325
326
 
326
327
  export { CoordinatorMessageType, EnvelopeKind, closeProtocolRuntime, onEnvelope, requestEnvelope };
327
- //# sourceMappingURL=chunk-UADJCUPK.js.map
328
- //# sourceMappingURL=chunk-UADJCUPK.js.map
328
+ //# sourceMappingURL=chunk-3MDHAS4D.js.map
329
+ //# sourceMappingURL=chunk-3MDHAS4D.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/coordinator/protocolClient.ts"],"names":["pending"],"mappings":";;;;AAWA,IAAM,0BAAA,GAA6B,UAAA;AACnC,IAAM,sBAAA,GAAyB,CAAA;AAExB,IAAM,YAAA,GAAe;AAAA,EAC1B,OAAA,EAAS,SAAA;AAAA,EACT,QAAA,EAAU,UAAA;AAAA,EACV,IAAA,EAAM,MAAA;AAAA,EACN,KAAA,EAAO;AACT;AAIO,IAAM,sBAAA,GAAyB;AAAA,EACpC,eAAA,EAAiB,iBAAA;AAAA,EACjB,qBAAA,EAAuB,uBAAA;AAAA,EACvB,OAAA,EAAS,SAAA;AAAA,EACT,SAAA,EAAW,WAAA;AAAA,EACX,SAAA,EAAW,WAAA;AAAA,EACX,WAAA,EAAa,aAAA;AAAA,EACb,aAAA,EAAe,eAAA;AAAA,EACf,WAAA,EAAa,aAAA;AAAA,EACb,gBAAA,EAAkB,kBAAA;AAAA,EAClB,IAAA,EAAM,MAAA;AAAA,EACN,YAAA,EAAc,cAAA;AAAA,EACd,MAAA,EAAQ,QAAA;AAAA,EACR,cAAA,EAAgB,gBAAA;AAAA,EAChB,eAAA,EAAiB,iBAAA;AAAA,EACjB,gBAAA,EAAkB,kBAAA;AAAA,EAClB,gBAAA,EAAkB,kBAAA;AAAA,EAClB,SAAA,EAAW,WAAA;AAAA,EACX,KAAA,EAAO;AACT;AA4BA,IAAI,eAAA,GAAkD,IAAA;AAEtD,SAAS,YAAA,GAAwC;AAC/C,EAAA,eAAA,KAAoB,OAAO,2BAAsB,CAAA;AACjD,EAAA,OAAO,eAAA;AACT;AAmDA,IAAM,QAAA,uBAAe,OAAA,EAAuC;AAE5D,eAAsB,gBACpB,OAAA,EACA,IAAA,EACA,OAAA,EACA,OAAA,GAAkC,EAAC,EAChB;AACnB,EAAA,qBAAA,CAAsB,QAAQ,MAAM,CAAA;AACpC,EAAA,MAAM,QAAA,GA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AAG;AACxD,IAAA,OAAA,CAAQ,OAAA,CAAQ,OAAO,KAAK,CAAA;AAC5B,IAAA,qBAAA,CAAsB,OAAO,CAAA;AAC7B,IAAA,OAAA,CAAQ,OAAO,KAAK,CAAA;AAAA,EACtB;AACA,EAAA,KAAA,MAAW,CAAC,IAAA,EAAM,OAAO,KAAK,OAAA,CAAQ,WAAA,CAAY,SAAQ,EAAG;AAC3D,IAAA,OAAA,CAAQ,WAAA,CAAY,OAAO,IAAI,CAAA;AAC/B,IAAA,KAAA,MAAW,UAAU,OAAA,EAAS;AAC5B,MAAA,IAAI,MAAA,CAAO,KAAA,EAAO,YAAA,CAAa,MAAA,CAAO,KAAK,CAAA;AAC3C,MAAA,MAAA,CAAO,OAAO,KAAK,CAAA;AAAA,IACrB;AAAA,EACF;AACF","file":"chunk-3MDHAS4D.js","sourcesContent":["import {\n CommandError,\n TransportError,\n type CommandErrorCode,\n type CoordinatorWireErrorCode,\n} from \"../core/errors.js\";\nimport type { Session } from \"../core/session.js\";\nimport { getSessionState, type SessionState } from \"../session/state.js\";\nimport type { Unsubscribe } from \"../transport/types.js\";\n\nconst COORDINATOR_ENVELOPE_VERSION = 2;\nconst MAX_COORDINATOR_REQUEST_ID = 0xffff_ffff;\nconst OUT_OF_BAND_REQUEST_ID = 0;\n\nexport const EnvelopeKind = {\n REQUEST: \"request\",\n RESPONSE: \"response\",\n PUSH: \"push\",\n ERROR: \"error\",\n} as const;\n\nexport type EnvelopeKind = (typeof EnvelopeKind)[keyof typeof EnvelopeKind];\n\nexport const CoordinatorMessageType = {\n ContractRequest: \"ContractRequest\",\n LpCreateAuthorization: \"LpCreateAuthorization\",\n DkgInit: \"DkgInit\",\n DkgRound1: \"DkgRound1\",\n DkgRound2: \"DkgRound2\",\n DkgComplete: \"DkgComplete\",\n DelegateShare: \"DelegateShare\",\n DelegateAck: \"DelegateAck\",\n DepositConfirmed: \"DepositConfirmed\",\n Sync: \"Sync\",\n SyncRequired: \"SyncRequired\",\n Refill: \"Refill\",\n PayoutExecuted: \"PayoutExecuted\",\n WithdrawRequest: \"WithdrawRequest\",\n WithdrawAccepted: \"WithdrawAccepted\",\n WithdrawExecuted: \"WithdrawExecuted\",\n LpCommand: \"LpCommand\",\n Error: \"Error\",\n} as const;\n\nexport type CoordinatorMessageType =\n (typeof CoordinatorMessageType)[keyof typeof CoordinatorMessageType];\n\nexport type Envelope = {\n readonly version: typeof COORDINATOR_ENVELOPE_VERSION;\n readonly kind: EnvelopeKind;\n readonly type: CoordinatorMessageType;\n readonly swap_id: string | null;\n readonly req_id: number;\n readonly timestamp_ms: number;\n readonly payload: unknown;\n};\n\ntype ProtocolModule = {\n readonly ENVELOPE_VERSION: typeof COORDINATOR_ENVELOPE_VERSION;\n readonly EnvelopeKind: typeof EnvelopeKind;\n readonly CoordinatorMessageType: typeof CoordinatorMessageType;\n readonly encodeValidated: (envelope: Envelope) => Uint8Array;\n readonly decode: (raw: Uint8Array) => { ok: true; value: Envelope } | { ok: false; error: Error };\n readonly asErrorPayload: (envelope: Envelope) => {\n readonly code: string;\n readonly failed_req_id?: number;\n readonly message: string;\n };\n};\n\nlet protocolPromise: Promise<ProtocolModule> | null = null;\n\nfunction loadProtocol(): Promise<ProtocolModule> {\n protocolPromise ??= import(\"protocol/coordinator\") as Promise<ProtocolModule>;\n return protocolPromise;\n}\n\nexport type ProtocolLogDirection = \"in\" | \"out\" | \"info\";\nexport type ProtocolLogHandler = (message: string, direction: ProtocolLogDirection) => void;\nexport type ProtocolEnvelopeHandler = (envelope: Envelope) => void;\n\nexport interface RequestEnvelopeOptions {\n readonly swapId?: string | null;\n readonly timeoutMs?: number;\n readonly signal?: AbortSignal;\n readonly onLog?: ProtocolLogHandler;\n /** Reject this request when the coordinator reports a swap-scoped error without a request id. */\n readonly rejectMatchingOutOfBandErrors?: boolean;\n /** Contract allocation responses must carry the server-assigned swap_id. */\n readonly requireNonNullResponseSwapId?: boolean;\n}\n\nexport interface AwaitPushOptions {\n /** Exact business routing key. Omit only for legacy wildcard waiters. */\n readonly swapId?: string | null;\n readonly timeoutMs?: number | null;\n}\n\ntype PendingRequest = {\n readonly expectedSwapId: string | null;\n readonly resolve: (envelope: Envelope) => void;\n readonly reject: (error: Error) => void;\n readonly timer: ReturnType<typeof setTimeout> | null;\n readonly signal?: AbortSignal;\n readonly abortHandler?: () => void;\n readonly swapId: string | null;\n readonly rejectMatchingOutOfBandErrors: boolean;\n readonly requireNonNullResponseSwapId: boolean;\n};\n\ntype PushWaiter = {\n readonly swapId?: string | null;\n readonly resolve: (envelope: Envelope) => void;\n readonly reject: (error: Error) => void;\n readonly timer: ReturnType<typeof setTimeout> | null;\n};\n\ntype ProtocolRuntime = {\n reqCounter: number;\n readonly pending: Map<number, PendingRequest>;\n readonly pushWaiters: Map<CoordinatorMessageType, PushWaiter[]>;\n readonly envelopeHandlers: Set<ProtocolEnvelopeHandler>;\n unsubscribeChannel: Unsubscribe | null;\n unsubscribeTransport: Unsubscribe | null;\n};\n\nconst runtimes = new WeakMap<SessionState, ProtocolRuntime>();\n\nexport async function requestEnvelope(\n session: Session,\n type: CoordinatorMessageType,\n payload: unknown,\n options: RequestEnvelopeOptions = {},\n): Promise<Envelope> {\n throwIfRequestAborted(options.signal);\n const protocol = await loadProtocol();\n throwIfRequestAborted(options.signal);\n const state = getSessionState(session);\n const runtime = ensureProtocolRuntime(state);\n const channel = state.channel;\n if (!state.attested || channel === null) {\n throw new TransportError(\"CONNECTION_LOST\", \"coordinator request requires an attested channel\");\n }\n\n if (runtime.reqCounter >= MAX_COORDINATOR_REQUEST_ID) {\n throw new CommandError(\"REJECTED_STATE\", \"coordinator request id space exhausted\");\n }\n const reqId = runtime.reqCounter + 1;\n runtime.reqCounter = reqId;\n const envelope: Envelope = {\n version: protocol.ENVELOPE_VERSION,\n kind: protocol.EnvelopeKind.REQUEST,\n type,\n swap_id: options.swapId ?? null,\n req_id: reqId,\n timestamp_ms: Date.now(),\n payload,\n };\n const frame = protocol.encodeValidated(envelope);\n\n return new Promise((resolve, reject) => {\n const abortHandler =\n options.signal === undefined\n ? undefined\n : () => rejectPending(runtime, reqId, createRequestAbortError());\n const timer =\n options.timeoutMs === undefined\n ? null\n : setTimeout(() => {\n rejectPending(\n runtime,\n reqId,\n new CommandError(\n \"REJECTED_STALE_JOB\",\n `coordinator request timed out for ${type} after ${options.timeoutMs}ms`,\n ),\n );\n }, options.timeoutMs);\n\n runtime.pending.set(reqId, {\n expectedSwapId: options.swapId ?? null,\n resolve,\n reject,\n timer,\n signal: options.signal,\n abortHandler,\n swapId: envelope.swap_id,\n rejectMatchingOutOfBandErrors: options.rejectMatchingOutOfBandErrors === true,\n requireNonNullResponseSwapId: options.requireNonNullResponseSwapId === true,\n });\n if (abortHandler !== undefined) {\n options.signal?.addEventListener(\"abort\", abortHandler, { once: true });\n if (options.signal?.aborted === true) {\n abortHandler();\n return;\n }\n }\n options.onLog?.(`-> ${type} req_id=${reqId}`, \"out\");\n try {\n channel.send(frame);\n } catch (error) {\n rejectPending(runtime, reqId, error instanceof Error ? error : new Error(String(error)));\n }\n });\n}\n\nexport function awaitPush(\n session: Session,\n type: CoordinatorMessageType,\n options: AwaitPushOptions = {},\n): Promise<Envelope> {\n const state = getSessionState(session);\n const runtime = ensureProtocolRuntime(state);\n const timeoutMs = options.timeoutMs === undefined ? 30_000 : options.timeoutMs;\n\n return new Promise((resolve, reject) => {\n const timer =\n timeoutMs === null\n ? null\n : setTimeout(() => {\n removePushWaiter(runtime, type, waiter);\n reject(new CommandError(\"REJECTED_STALE_JOB\", `timed out waiting for ${type} push`));\n }, timeoutMs);\n const waiter: PushWaiter = { swapId: options.swapId, resolve, reject, timer };\n const waiters = runtime.pushWaiters.get(type) ?? [];\n waiters.push(waiter);\n runtime.pushWaiters.set(type, waiters);\n });\n}\n\nexport function onEnvelope(session: Session, handler: ProtocolEnvelopeHandler): Unsubscribe {\n const runtime = ensureProtocolRuntime(getSessionState(session));\n runtime.envelopeHandlers.add(handler);\n return () => runtime.envelopeHandlers.delete(handler);\n}\n\nexport function closeProtocolRuntime(session: Session): void {\n const state = getSessionState(session);\n const runtime = runtimes.get(state);\n if (!runtime) return;\n const error = new TransportError(\"CONNECTION_LOST\", \"coordinator protocol runtime closed\");\n rejectAll(runtime, error);\n runtime.unsubscribeChannel?.();\n runtime.unsubscribeTransport?.();\n runtimes.delete(state);\n}\n\nfunction ensureProtocolRuntime(state: SessionState): ProtocolRuntime {\n const existing = runtimes.get(state);\n if (existing) return existing;\n\n const runtime: ProtocolRuntime = {\n reqCounter: 0,\n pending: new Map(),\n pushWaiters: new Map(),\n envelopeHandlers: new Set(),\n unsubscribeChannel: null,\n unsubscribeTransport: null,\n };\n\n if (state.channel === null) {\n throw new TransportError(\"CONNECTION_LOST\", \"coordinator protocol requires a live channel\");\n }\n runtime.unsubscribeChannel = state.channel.onMessage((frame) => handleFrame(runtime, frame));\n runtime.unsubscribeTransport = state.transport.onStateChange((transportState) => {\n if (transportState === \"closing\" || transportState === \"closed\") {\n rejectAll(runtime, new TransportError(\"CONNECTION_LOST\", \"coordinator transport closed\"));\n }\n });\n\n runtimes.set(state, runtime);\n return runtime;\n}\n\nfunction handleFrame(runtime: ProtocolRuntime, frame: Uint8Array): void {\n void handleFrameAsync(runtime, frame);\n}\n\nasync function handleFrameAsync(runtime: ProtocolRuntime, frame: Uint8Array): Promise<void> {\n const protocol = await loadProtocol();\n const decoded = protocol.decode(frame);\n if (!decoded.ok) {\n rejectAll(runtime, decoded.error);\n return;\n }\n\n const envelope = decoded.value;\n if (envelope.kind === protocol.EnvelopeKind.RESPONSE) {\n const pending = runtime.pending.get(envelope.req_id);\n if (pending && !pendingRequestMatchesSwapId(pending, envelope.swap_id)) {\n rejectPending(\n runtime,\n envelope.req_id,\n requestSwapIdMismatchError(envelope.req_id, pending.expectedSwapId, envelope.swap_id),\n );\n return;\n }\n }\n\n const errorPayload =\n envelope.kind === protocol.EnvelopeKind.ERROR ? protocol.asErrorPayload(envelope) : null;\n if (errorPayload?.failed_req_id !== undefined) {\n const pending = runtime.pending.get(errorPayload.failed_req_id);\n if (pending && !pendingRequestMatchesSwapId(pending, envelope.swap_id, false)) {\n rejectPending(\n runtime,\n errorPayload.failed_req_id,\n requestSwapIdMismatchError(\n errorPayload.failed_req_id,\n pending.expectedSwapId,\n envelope.swap_id,\n ),\n );\n return;\n }\n }\n\n for (const handler of runtime.envelopeHandlers) handler(envelope);\n\n if (envelope.kind === protocol.EnvelopeKind.ERROR) {\n const payload = errorPayload!;\n const error = commandErrorFromCoordinatorError(payload);\n if (payload.failed_req_id === undefined || payload.failed_req_id === OUT_OF_BAND_REQUEST_ID) {\n if (envelope.swap_id !== null) {\n rejectMatchingOutOfBandErrors(runtime, envelope.swap_id, error);\n }\n return;\n }\n const pending = runtime.pending.get(payload.failed_req_id);\n if (!pending) return;\n if (!pendingRequestMatchesSwapId(pending, envelope.swap_id, false)) {\n rejectPending(\n runtime,\n payload.failed_req_id,\n requestSwapIdMismatchError(payload.failed_req_id, pending.expectedSwapId, envelope.swap_id),\n );\n return;\n }\n rejectPending(runtime, payload.failed_req_id, error);\n return;\n }\n\n if (envelope.kind === protocol.EnvelopeKind.PUSH) {\n resolvePushWaiter(runtime, envelope);\n return;\n }\n\n if (envelope.kind !== protocol.EnvelopeKind.RESPONSE) return;\n const pending = runtime.pending.get(envelope.req_id);\n if (!pending) return;\n if (!pendingRequestMatchesSwapId(pending, envelope.swap_id)) {\n rejectPending(\n runtime,\n envelope.req_id,\n requestSwapIdMismatchError(envelope.req_id, pending.expectedSwapId, envelope.swap_id),\n );\n return;\n }\n runtime.pending.delete(envelope.req_id);\n cleanupPendingRequest(pending);\n pending.resolve(envelope);\n}\n\nfunction pendingRequestMatchesSwapId(\n pending: PendingRequest,\n responseSwapId: string | null,\n enforceNonNullResponseSwapId = true,\n): boolean {\n if (\n enforceNonNullResponseSwapId &&\n pending.requireNonNullResponseSwapId &&\n responseSwapId === null\n ) {\n return false;\n }\n return pending.expectedSwapId === null || pending.expectedSwapId === responseSwapId;\n}\n\nfunction requestSwapIdMismatchError(\n reqId: number,\n expectedSwapId: string | null,\n receivedSwapId: string | null,\n): CommandError {\n return new CommandError(\n \"REJECTED_STATE\",\n `coordinator response swap_id mismatch for req_id=${reqId}: expected ${expectedSwapId ?? \"null\"}, received ${receivedSwapId ?? \"null\"}`,\n { failedReqId: reqId },\n );\n}\n\nfunction resolvePushWaiter(runtime: ProtocolRuntime, envelope: Envelope): void {\n const waiters = runtime.pushWaiters.get(envelope.type);\n const waiterIndex = waiters?.findIndex(\n (candidate) => candidate.swapId === undefined || candidate.swapId === envelope.swap_id,\n );\n const waiter =\n waiters && waiterIndex !== undefined && waiterIndex >= 0\n ? waiters.splice(waiterIndex, 1)[0]\n : undefined;\n if (!waiter || !waiters) return;\n if (waiters.length === 0) {\n runtime.pushWaiters.delete(envelope.type);\n }\n if (waiter.timer) clearTimeout(waiter.timer);\n waiter.resolve(envelope);\n}\n\nfunction rejectPending(runtime: ProtocolRuntime, reqId: number, error: Error): void {\n const pending = runtime.pending.get(reqId);\n if (!pending) return;\n runtime.pending.delete(reqId);\n cleanupPendingRequest(pending);\n pending.reject(error);\n}\n\nfunction cleanupPendingRequest(pending: PendingRequest): void {\n if (pending.timer) clearTimeout(pending.timer);\n if (pending.signal !== undefined && pending.abortHandler !== undefined) {\n pending.signal.removeEventListener(\"abort\", pending.abortHandler);\n }\n}\n\nfunction throwIfRequestAborted(signal: AbortSignal | undefined): void {\n if (signal?.aborted === true) throw createRequestAbortError();\n}\n\nfunction createRequestAbortError(): DOMException {\n return new DOMException(\"coordinator request aborted\", \"AbortError\");\n}\n\nfunction rejectMatchingOutOfBandErrors(\n runtime: ProtocolRuntime,\n swapId: string,\n error: Error,\n): void {\n for (const [reqId, pending] of runtime.pending) {\n if (!pending.rejectMatchingOutOfBandErrors || pending.swapId !== swapId) continue;\n rejectPending(runtime, reqId, error);\n }\n}\n\nfunction commandErrorFromCoordinatorError(payload: {\n readonly code: string;\n readonly failed_req_id?: number;\n readonly message: string;\n}): CommandError {\n const coordinatorCode = toCoordinatorWireErrorCode(payload.code);\n return new CommandError(commandCodeFromCoordinatorCode(coordinatorCode), payload.message, {\n coordinatorCode,\n ...(payload.failed_req_id !== undefined ? { failedReqId: payload.failed_req_id } : {}),\n });\n}\n\nfunction commandCodeFromCoordinatorCode(code: CoordinatorWireErrorCode): CommandErrorCode {\n switch (code) {\n case \"ERR_UNEXPECTED_TYPE\":\n case \"ERR_UNKNOWN_SWAP\":\n case \"ERR_SWAP_NOT_REFUNDABLE\":\n return \"REJECTED_STATE\";\n case \"ERR_INTERNAL\":\n case \"ERR_SETTLEMENT_FAILED\":\n return \"REJECTED_STALE_JOB\";\n case \"ERR_INVALID_PAYLOAD\":\n case \"ERR_POLICY_VIOLATION\":\n case \"ERR_INVALID_RECOVERY_CODE\":\n case \"ERR_DKG_FAILURE\":\n case \"ERR_INVALID_DEPOSIT\":\n return \"REJECTED_GUARD\";\n }\n}\n\nfunction toCoordinatorWireErrorCode(code: string): CoordinatorWireErrorCode {\n switch (code) {\n case \"ERR_INVALID_PAYLOAD\":\n case \"ERR_UNEXPECTED_TYPE\":\n case \"ERR_UNKNOWN_SWAP\":\n case \"ERR_POLICY_VIOLATION\":\n case \"ERR_INVALID_RECOVERY_CODE\":\n case \"ERR_SWAP_NOT_REFUNDABLE\":\n case \"ERR_DKG_FAILURE\":\n case \"ERR_INTERNAL\":\n case \"ERR_INVALID_DEPOSIT\":\n case \"ERR_SETTLEMENT_FAILED\":\n return code;\n default:\n return \"ERR_INTERNAL\";\n }\n}\n\nfunction removePushWaiter(\n runtime: ProtocolRuntime,\n type: CoordinatorMessageType,\n waiter: PushWaiter,\n): void {\n const waiters = runtime.pushWaiters.get(type);\n if (!waiters) return;\n const index = waiters.indexOf(waiter);\n if (index >= 0) waiters.splice(index, 1);\n if (waiters.length === 0) runtime.pushWaiters.delete(type);\n}\n\nfunction rejectAll(runtime: ProtocolRuntime, error: Error): void {\n for (const [reqId, pending] of runtime.pending.entries()) {\n runtime.pending.delete(reqId);\n cleanupPendingRequest(pending);\n pending.reject(error);\n }\n for (const [type, waiters] of runtime.pushWaiters.entries()) {\n runtime.pushWaiters.delete(type);\n for (const waiter of waiters) {\n if (waiter.timer) clearTimeout(waiter.timer);\n waiter.reject(error);\n }\n }\n}\n"]}
@@ -57,6 +57,8 @@ function isValidEnvelopeKind(value) {
57
57
  var CoordinatorMessageType = {
58
58
  // Phase A: Swap/contract setup — unified user swap-open + LP position-open (JW-372 step 8).
59
59
  ContractRequest: "ContractRequest",
60
+ // V0 LP creation admission over the attested Noise session.
61
+ LpCreateAuthorization: "LpCreateAuthorization",
60
62
  // Phase B: DKG (Alice ↔ TEE)
61
63
  DkgInit: "DkgInit",
62
64
  DkgRound1: "DkgRound1",
@@ -93,6 +95,7 @@ function isValidMessageType(value) {
93
95
  var KIND_BY_TYPE = {
94
96
  // Pure request/response
95
97
  ContractRequest: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
98
+ LpCreateAuthorization: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
96
99
  DkgInit: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
97
100
  DkgRound1: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
98
101
  DkgRound2: /* @__PURE__ */ new Set([EnvelopeKind.REQUEST, EnvelopeKind.RESPONSE]),
@@ -127,6 +130,7 @@ var DIRECTION_BY_TYPE = {
127
130
  // Client-initiated
128
131
  ContractRequest: MessageDirection.BIDIRECTIONAL,
129
132
  // request c→co, response co→c
133
+ LpCreateAuthorization: MessageDirection.BIDIRECTIONAL,
130
134
  DkgInit: MessageDirection.BIDIRECTIONAL,
131
135
  DkgRound1: MessageDirection.BIDIRECTIONAL,
132
136
  DkgRound2: MessageDirection.BIDIRECTIONAL,
@@ -174,6 +178,8 @@ var COORDINATOR_BINARY_FIELD_NAMES = [
174
178
  var COORDINATOR_SCHEMA_PAYLOAD_CATEGORIES = {
175
179
  "request:ContractRequest": "request",
176
180
  "response:ContractRequest": "response",
181
+ "request:LpCreateAuthorization": "request",
182
+ "response:LpCreateAuthorization": "response",
177
183
  "request:DkgInit": "request",
178
184
  "response:DkgInit": "response",
179
185
  "request:DkgRound1": "request",
@@ -430,6 +436,7 @@ var coordinator_envelope_schema_default = {
430
436
  type: {
431
437
  enum: [
432
438
  "ContractRequest",
439
+ "LpCreateAuthorization",
433
440
  "DkgInit",
434
441
  "DkgRound1",
435
442
  "DkgRound2",
@@ -501,6 +508,24 @@ var coordinator_envelope_schema_default = {
501
508
  }
502
509
  }
503
510
  },
511
+ {
512
+ title: "LpCreateAuthorization request",
513
+ properties: {
514
+ kind: { const: "request" },
515
+ type: { const: "LpCreateAuthorization" },
516
+ swap_id: { type: "null" },
517
+ payload: { $ref: "#/$defs/lpCreateAuthorizationRequest" }
518
+ }
519
+ },
520
+ {
521
+ title: "LpCreateAuthorization response",
522
+ properties: {
523
+ kind: { const: "response" },
524
+ type: { const: "LpCreateAuthorization" },
525
+ swap_id: { type: "null" },
526
+ payload: { $ref: "#/$defs/lpCreateAuthorizationResponse" }
527
+ }
528
+ },
504
529
  {
505
530
  title: "DkgInit request",
506
531
  properties: {
@@ -1134,6 +1159,114 @@ var coordinator_envelope_schema_default = {
1134
1159
  }
1135
1160
  ]
1136
1161
  },
1162
+ lpCreateAuthorizationRequest: {
1163
+ oneOf: [
1164
+ { $ref: "#/$defs/lpCreateAuthorizationChallengeRequest" },
1165
+ { $ref: "#/$defs/lpCreateAuthorizationProofRequest" }
1166
+ ]
1167
+ },
1168
+ lpCreateAuthorizationResponse: {
1169
+ oneOf: [
1170
+ { $ref: "#/$defs/lpCreateAuthorizationChallengeResponse" },
1171
+ { $ref: "#/$defs/lpCreateAuthorizationAuthorizedResponse" }
1172
+ ]
1173
+ },
1174
+ lpCreateAuthorizationChallengeRequest: {
1175
+ type: "object",
1176
+ additionalProperties: false,
1177
+ required: ["case", "value"],
1178
+ properties: {
1179
+ case: { const: "challenge" },
1180
+ value: { $ref: "#/$defs/lpCreateAuthorizationChallenge" }
1181
+ }
1182
+ },
1183
+ lpCreateAuthorizationProofRequest: {
1184
+ type: "object",
1185
+ additionalProperties: false,
1186
+ required: ["case", "value"],
1187
+ properties: {
1188
+ case: { const: "proof" },
1189
+ value: { $ref: "#/$defs/lpCreateAuthorizationProof" }
1190
+ }
1191
+ },
1192
+ lpCreateAuthorizationChallengeResponse: {
1193
+ type: "object",
1194
+ additionalProperties: false,
1195
+ required: ["case", "value"],
1196
+ properties: {
1197
+ case: { const: "challenge" },
1198
+ value: { $ref: "#/$defs/lpCreateAuthorizationChallengeResponseValue" }
1199
+ }
1200
+ },
1201
+ lpCreateAuthorizationAuthorizedResponse: {
1202
+ type: "object",
1203
+ additionalProperties: false,
1204
+ required: ["case", "value"],
1205
+ properties: {
1206
+ case: { const: "authorized" },
1207
+ value: { $ref: "#/$defs/lpCreateAuthorizationAuthorized" }
1208
+ }
1209
+ },
1210
+ lpCreateAuthorizationChallenge: {
1211
+ type: "object",
1212
+ additionalProperties: false,
1213
+ required: ["public_key", "intent"],
1214
+ properties: {
1215
+ public_key: { $ref: "#/$defs/lpSolanaAddress" },
1216
+ intent: { $ref: "#/$defs/lpCreatePositionIntent" }
1217
+ }
1218
+ },
1219
+ lpCreateAuthorizationProof: {
1220
+ type: "object",
1221
+ additionalProperties: false,
1222
+ required: ["authorization_id", "public_key", "message", "signature"],
1223
+ properties: {
1224
+ authorization_id: { $ref: "#/$defs/lpAuthorizationId" },
1225
+ public_key: { $ref: "#/$defs/lpSolanaAddress" },
1226
+ message: { $ref: "#/$defs/lpSiwsMessage" },
1227
+ signature: { $ref: "#/$defs/base64url64Bytes" }
1228
+ }
1229
+ },
1230
+ lpCreateAuthorizationChallengeResponseValue: {
1231
+ type: "object",
1232
+ additionalProperties: false,
1233
+ required: ["authorization_id", "message", "expires_at_ms", "valid_for_ms"],
1234
+ properties: {
1235
+ authorization_id: { $ref: "#/$defs/lpAuthorizationId" },
1236
+ message: { $ref: "#/$defs/lpSiwsMessage" },
1237
+ expires_at_ms: { $ref: "#/$defs/positiveSafeUInt" },
1238
+ valid_for_ms: { $ref: "#/$defs/positiveSafeUInt" }
1239
+ }
1240
+ },
1241
+ lpCreateAuthorizationAuthorized: {
1242
+ type: "object",
1243
+ additionalProperties: false,
1244
+ required: ["authorization_id"],
1245
+ properties: {
1246
+ authorization_id: { $ref: "#/$defs/lpAuthorizationId" }
1247
+ }
1248
+ },
1249
+ lpCreatePositionIntent: {
1250
+ type: "object",
1251
+ additionalProperties: false,
1252
+ required: [
1253
+ "committed_lamports",
1254
+ "shard_count",
1255
+ "shard_amount_lamports",
1256
+ "refill_threshold"
1257
+ ],
1258
+ properties: {
1259
+ committed_lamports: { $ref: "#/$defs/positiveSafeUInt" },
1260
+ shard_count: { $ref: "#/$defs/lpShardCount" },
1261
+ shard_amount_lamports: { $ref: "#/$defs/positiveSafeUInt" },
1262
+ refill_threshold: { $ref: "#/$defs/lpCreationRefillThreshold" }
1263
+ }
1264
+ },
1265
+ lpCreationRefillThreshold: {
1266
+ type: "integer",
1267
+ minimum: 0,
1268
+ maximum: 300
1269
+ },
1137
1270
  payoutSpec: {
1138
1271
  oneOf: [
1139
1272
  {
@@ -2038,6 +2171,23 @@ var coordinator_envelope_schema_default = {
2038
2171
  maxLength: 96,
2039
2172
  pattern: "^[A-Za-z0-9_-]+$"
2040
2173
  },
2174
+ lpAuthorizationId: {
2175
+ type: "string",
2176
+ minLength: 16,
2177
+ maxLength: 128,
2178
+ pattern: "^[A-Za-z0-9_-]+$"
2179
+ },
2180
+ lpSolanaAddress: {
2181
+ type: "string",
2182
+ minLength: 32,
2183
+ maxLength: 44,
2184
+ pattern: "^[1-9A-HJ-NP-Za-km-z]+$"
2185
+ },
2186
+ lpSiwsMessage: {
2187
+ type: "string",
2188
+ minLength: 1,
2189
+ maxLength: 4096
2190
+ },
2041
2191
  lpRedemptionId: {
2042
2192
  type: "string",
2043
2193
  minLength: 1,
@@ -2530,6 +2680,7 @@ var coordinator_envelope_schema_default = {
2530
2680
  type: "object",
2531
2681
  additionalProperties: false,
2532
2682
  required: [
2683
+ "creation_authorization_id",
2533
2684
  "auth_public_key",
2534
2685
  "refill_public_key",
2535
2686
  "redemption_commitment",
@@ -2541,6 +2692,9 @@ var coordinator_envelope_schema_default = {
2541
2692
  "created_at_ms"
2542
2693
  ],
2543
2694
  properties: {
2695
+ creation_authorization_id: {
2696
+ $ref: "#/$defs/lpAuthorizationId"
2697
+ },
2544
2698
  auth_public_key: {
2545
2699
  $ref: "#/$defs/base64url32Bytes"
2546
2700
  },
@@ -2979,11 +3133,30 @@ var coordinator_envelope_schema_default = {
2979
3133
  created_at_ms: {
2980
3134
  $ref: "#/$defs/positiveSafeUInt"
2981
3135
  },
3136
+ redemption_permission: {
3137
+ $ref: "#/$defs/lpRedemptionPermission"
3138
+ },
2982
3139
  actor_sync: {
2983
3140
  $ref: "#/$defs/lpActorSyncView"
2984
3141
  }
2985
3142
  }
2986
3143
  },
3144
+ lpRedemptionPermission: {
3145
+ type: "object",
3146
+ additionalProperties: false,
3147
+ required: ["state", "grant_revision", "observed_at_ms"],
3148
+ properties: {
3149
+ state: {
3150
+ enum: ["unknown", "locked", "enabled"]
3151
+ },
3152
+ grant_revision: {
3153
+ $ref: "#/$defs/safeUInt"
3154
+ },
3155
+ observed_at_ms: {
3156
+ $ref: "#/$defs/positiveSafeUInt"
3157
+ }
3158
+ }
3159
+ },
2987
3160
  lpActorSyncView: {
2988
3161
  type: "object",
2989
3162
  additionalProperties: false,
@@ -3712,6 +3885,9 @@ function requiredPayloadFields(kind, type) {
3712
3885
  return ["actor", "amount_lamports", "payout_spec", "sync_commitment"];
3713
3886
  case "response:ContractRequest":
3714
3887
  return ["swap_id", "accepted", "policy_snapshot"];
3888
+ case "request:LpCreateAuthorization":
3889
+ case "response:LpCreateAuthorization":
3890
+ return ["case", "value"];
3715
3891
  case "response:DkgInit":
3716
3892
  return ["ready"];
3717
3893
  case "request:DkgRound1":
@@ -3944,5 +4120,5 @@ var asErrorPayload = (e) => {
3944
4120
  };
3945
4121
 
3946
4122
  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 };
3947
- //# sourceMappingURL=chunk-E4DCEDAG.js.map
3948
- //# sourceMappingURL=chunk-E4DCEDAG.js.map
4123
+ //# sourceMappingURL=chunk-3TGQ4CZ3.js.map
4124
+ //# sourceMappingURL=chunk-3TGQ4CZ3.js.map