@acta-markets/ts-sdk 0.0.2-beta → 0.0.4-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/actaClient.d.ts +3 -1
- package/dist/actaClient.js +2 -2
- package/dist/cjs/actaClient.js +2 -2
- package/dist/cjs/ws/client.js +45 -9
- package/dist/cjs/ws/client.test.js +56 -0
- package/dist/ws/client.d.ts +9 -2
- package/dist/ws/client.js +45 -9
- package/dist/ws/client.test.js +56 -0
- package/dist/ws/types.d.ts +9 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -82,7 +82,11 @@ client.ws!.on("chainEvent", (e) => console.log("chain event", e));
|
|
|
82
82
|
|
|
83
83
|
```ts
|
|
84
84
|
client.ws!.on("markets", (markets) => console.log("markets", markets));
|
|
85
|
-
client.ws!.on("positions", (positions) =>
|
|
85
|
+
client.ws!.on("positions", (positions) => {
|
|
86
|
+
console.log("positions", positions);
|
|
87
|
+
// taker position payload includes market expiry as unix seconds
|
|
88
|
+
console.log("first expiry", positions[0]?.expiry_ts);
|
|
89
|
+
});
|
|
86
90
|
|
|
87
91
|
client.ws!.getMarkets();
|
|
88
92
|
// positions require auth:
|
package/dist/actaClient.d.ts
CHANGED
|
@@ -38,7 +38,9 @@ export declare class ActaClient {
|
|
|
38
38
|
/** Convenience: `client.connectWs(auth)` instead of `client.ws!.connect(auth)`. */
|
|
39
39
|
connectWs(authProvider: AuthProvider): this;
|
|
40
40
|
/** Convenience: connect and trigger auth flow in one call. */
|
|
41
|
-
connectWsAndAuthenticate(authProvider: AuthProvider
|
|
41
|
+
connectWsAndAuthenticate(authProvider: AuthProvider, options?: {
|
|
42
|
+
sessionId?: string;
|
|
43
|
+
}): this;
|
|
42
44
|
/** Convenience: connect WS without triggering auth (anonymous browsing). */
|
|
43
45
|
connectWsAnonymous(): this;
|
|
44
46
|
/** Convenience: disconnect WS (and stop reconnect timers). */
|
package/dist/actaClient.js
CHANGED
|
@@ -82,8 +82,8 @@ export class ActaClient {
|
|
|
82
82
|
return this;
|
|
83
83
|
}
|
|
84
84
|
/** Convenience: connect and trigger auth flow in one call. */
|
|
85
|
-
connectWsAndAuthenticate(authProvider) {
|
|
86
|
-
this.assertWs().connectAndAuthenticate(authProvider);
|
|
85
|
+
connectWsAndAuthenticate(authProvider, options) {
|
|
86
|
+
this.assertWs().connectAndAuthenticate(authProvider, options);
|
|
87
87
|
return this;
|
|
88
88
|
}
|
|
89
89
|
/** Convenience: connect WS without triggering auth (anonymous browsing). */
|
package/dist/cjs/actaClient.js
CHANGED
|
@@ -85,8 +85,8 @@ class ActaClient {
|
|
|
85
85
|
return this;
|
|
86
86
|
}
|
|
87
87
|
/** Convenience: connect and trigger auth flow in one call. */
|
|
88
|
-
connectWsAndAuthenticate(authProvider) {
|
|
89
|
-
this.assertWs().connectAndAuthenticate(authProvider);
|
|
88
|
+
connectWsAndAuthenticate(authProvider, options) {
|
|
89
|
+
this.assertWs().connectAndAuthenticate(authProvider, options);
|
|
90
90
|
return this;
|
|
91
91
|
}
|
|
92
92
|
/** Convenience: connect WS without triggering auth (anonymous browsing). */
|
package/dist/cjs/ws/client.js
CHANGED
|
@@ -79,6 +79,7 @@ class ActaWsClient extends TypedEventEmitter {
|
|
|
79
79
|
authProvider = null;
|
|
80
80
|
authRequested = false;
|
|
81
81
|
startAuthSent = false;
|
|
82
|
+
pendingResumeSessionId = null;
|
|
82
83
|
connectionState = "disconnected";
|
|
83
84
|
sessionId = null;
|
|
84
85
|
helloSent = false;
|
|
@@ -122,22 +123,25 @@ class ActaWsClient extends TypedEventEmitter {
|
|
|
122
123
|
connectAnonymous() {
|
|
123
124
|
this.authProvider = null;
|
|
124
125
|
this.authRequested = false;
|
|
126
|
+
this.pendingResumeSessionId = null;
|
|
125
127
|
this.shouldReconnect = this.options.autoReconnect;
|
|
126
128
|
this.doConnect();
|
|
127
129
|
}
|
|
128
130
|
connect(authProvider) {
|
|
129
131
|
this.authProvider = authProvider;
|
|
130
132
|
this.authRequested = false;
|
|
133
|
+
this.pendingResumeSessionId = null;
|
|
131
134
|
this.shouldReconnect = this.options.autoReconnect;
|
|
132
135
|
this.doConnect();
|
|
133
136
|
}
|
|
134
|
-
connectAndAuthenticate(authProvider) {
|
|
137
|
+
connectAndAuthenticate(authProvider, options) {
|
|
135
138
|
this.authProvider = authProvider;
|
|
136
139
|
this.authRequested = true;
|
|
140
|
+
this.pendingResumeSessionId = options?.sessionId ?? null;
|
|
137
141
|
this.shouldReconnect = this.options.autoReconnect;
|
|
138
142
|
if (this.ws?.readyState === WS_OPEN) {
|
|
139
143
|
if (this.welcomeReceived && !this.startAuthSent) {
|
|
140
|
-
void this.
|
|
144
|
+
void this.beginAuthHandshake().catch((err) => {
|
|
141
145
|
this.emit("error", err);
|
|
142
146
|
this.setConnectionState("error");
|
|
143
147
|
});
|
|
@@ -149,6 +153,7 @@ class ActaWsClient extends TypedEventEmitter {
|
|
|
149
153
|
async authenticate(authProvider) {
|
|
150
154
|
this.authProvider = authProvider;
|
|
151
155
|
this.authRequested = true;
|
|
156
|
+
this.pendingResumeSessionId = null;
|
|
152
157
|
if (this.ws?.readyState === WS_OPEN) {
|
|
153
158
|
if (this.welcomeReceived && !this.startAuthSent) {
|
|
154
159
|
await this.sendStartAuth();
|
|
@@ -319,6 +324,10 @@ class ActaWsClient extends TypedEventEmitter {
|
|
|
319
324
|
this.send({ type: "Ping" });
|
|
320
325
|
}
|
|
321
326
|
}
|
|
327
|
+
resumeAuth(sessionId) {
|
|
328
|
+
this.pendingResumeSessionId = sessionId;
|
|
329
|
+
this.sendResumeAuth(sessionId);
|
|
330
|
+
}
|
|
322
331
|
doConnect() {
|
|
323
332
|
if (this.ws) {
|
|
324
333
|
this.cleanup();
|
|
@@ -379,7 +388,7 @@ class ActaWsClient extends TypedEventEmitter {
|
|
|
379
388
|
this.emit("welcome", message.data);
|
|
380
389
|
this.flushPendingMessages();
|
|
381
390
|
if (this.authRequested && this.authProvider && !this.startAuthSent) {
|
|
382
|
-
void this.
|
|
391
|
+
void this.beginAuthHandshake().catch((err) => {
|
|
383
392
|
this.emit("error", err);
|
|
384
393
|
this.setConnectionState("error");
|
|
385
394
|
});
|
|
@@ -404,10 +413,10 @@ class ActaWsClient extends TypedEventEmitter {
|
|
|
404
413
|
void this.handleAuthRequest(message.data.challenge);
|
|
405
414
|
break;
|
|
406
415
|
case "AuthSuccess":
|
|
407
|
-
this.handleAuthSuccess(message.data.session_id);
|
|
416
|
+
this.handleAuthSuccess(message.data.session_id, message.data.expires_at);
|
|
408
417
|
break;
|
|
409
418
|
case "AuthError":
|
|
410
|
-
this.handleAuthError(message.data.reason);
|
|
419
|
+
this.handleAuthError(message.data.reason, message.data.message);
|
|
411
420
|
break;
|
|
412
421
|
case "Snapshot":
|
|
413
422
|
this.handleSnapshot(message.data);
|
|
@@ -609,6 +618,13 @@ class ActaWsClient extends TypedEventEmitter {
|
|
|
609
618
|
this.setConnectionState("error");
|
|
610
619
|
}
|
|
611
620
|
}
|
|
621
|
+
async beginAuthHandshake() {
|
|
622
|
+
if (this.pendingResumeSessionId) {
|
|
623
|
+
this.sendResumeAuth(this.pendingResumeSessionId);
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
await this.sendStartAuth();
|
|
627
|
+
}
|
|
612
628
|
async sendStartAuth() {
|
|
613
629
|
if (!this.authProvider)
|
|
614
630
|
throw new Error("No auth provider configured");
|
|
@@ -616,6 +632,10 @@ class ActaWsClient extends TypedEventEmitter {
|
|
|
616
632
|
const pubkey = await this.authProvider.getPublicKey();
|
|
617
633
|
this.send({ type: "StartAuth", data: { pubkey } });
|
|
618
634
|
}
|
|
635
|
+
sendResumeAuth(sessionId) {
|
|
636
|
+
this.startAuthSent = true;
|
|
637
|
+
this.send({ type: "ResumeAuth", data: { session_id: sessionId } });
|
|
638
|
+
}
|
|
619
639
|
sendHello() {
|
|
620
640
|
const hello = {
|
|
621
641
|
protocol_version: this.options.protocolVersion,
|
|
@@ -634,10 +654,11 @@ class ActaWsClient extends TypedEventEmitter {
|
|
|
634
654
|
this.send(msg);
|
|
635
655
|
}
|
|
636
656
|
}
|
|
637
|
-
handleAuthSuccess(sessionId) {
|
|
657
|
+
handleAuthSuccess(sessionId, expiresAt) {
|
|
638
658
|
this.sessionId = sessionId;
|
|
659
|
+
this.pendingResumeSessionId = null;
|
|
639
660
|
this.setConnectionState("authenticated");
|
|
640
|
-
this.emit("authenticated", sessionId);
|
|
661
|
+
this.emit("authenticated", sessionId, expiresAt);
|
|
641
662
|
if (this.subscribedChannels.size > 0) {
|
|
642
663
|
const channels = Array.from(this.subscribedChannels);
|
|
643
664
|
const data = this.hasMarketScope
|
|
@@ -646,9 +667,23 @@ class ActaWsClient extends TypedEventEmitter {
|
|
|
646
667
|
this.send({ type: "Subscribe", data });
|
|
647
668
|
}
|
|
648
669
|
}
|
|
649
|
-
handleAuthError(reason) {
|
|
670
|
+
handleAuthError(reason, message) {
|
|
671
|
+
this.emit("authError", reason);
|
|
672
|
+
if (reason === "session_expired" &&
|
|
673
|
+
this.authRequested &&
|
|
674
|
+
this.authProvider &&
|
|
675
|
+
this.pendingResumeSessionId) {
|
|
676
|
+
this.pendingResumeSessionId = null;
|
|
677
|
+
this.startAuthSent = false;
|
|
678
|
+
void this.sendStartAuth().catch((err) => {
|
|
679
|
+
this.emit("error", err);
|
|
680
|
+
this.setConnectionState("error");
|
|
681
|
+
});
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
650
684
|
this.setConnectionState("error");
|
|
651
|
-
|
|
685
|
+
const details = message ? `${reason}: ${message}` : reason;
|
|
686
|
+
this.emit("error", new Error(`Authentication failed: ${details}`));
|
|
652
687
|
}
|
|
653
688
|
handleSnapshot(snapshot) {
|
|
654
689
|
this.state.stats = snapshot.stats;
|
|
@@ -848,6 +883,7 @@ class ActaWsClient extends TypedEventEmitter {
|
|
|
848
883
|
this.helloSent = false;
|
|
849
884
|
this.welcomeReceived = false;
|
|
850
885
|
this.startAuthSent = false;
|
|
886
|
+
this.pendingResumeSessionId = null;
|
|
851
887
|
this.pendingMessages = [];
|
|
852
888
|
this.setConnectionState("disconnected");
|
|
853
889
|
}
|
|
@@ -121,6 +121,62 @@ describe("ActaWsClient", () => {
|
|
|
121
121
|
expect(authChallenge.data.challenge).toBe("challenge-text");
|
|
122
122
|
}
|
|
123
123
|
});
|
|
124
|
+
it("connectAndAuthenticate sends ResumeAuth when sessionId is provided", async () => {
|
|
125
|
+
const { client, socket } = makeHarness();
|
|
126
|
+
const auth = makeAuthProvider("WalletPubkey", "WalletSignature");
|
|
127
|
+
client.connectAndAuthenticate(auth, { sessionId: "resume-session-id" });
|
|
128
|
+
const ws = socket();
|
|
129
|
+
ws.triggerOpen();
|
|
130
|
+
ws.triggerMessage(WELCOME_MESSAGE);
|
|
131
|
+
await flushMicrotasks();
|
|
132
|
+
const sentTypes = ws.sent.map((payload) => parseClientMessage(payload).type);
|
|
133
|
+
expect(sentTypes).toEqual(["Hello", "ResumeAuth"]);
|
|
134
|
+
const resumeAuth = parseClientMessage(ws.sent[1]);
|
|
135
|
+
expect(resumeAuth.type).toBe("ResumeAuth");
|
|
136
|
+
if (resumeAuth.type === "ResumeAuth") {
|
|
137
|
+
expect(resumeAuth.data.session_id).toBe("resume-session-id");
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
it("falls back to StartAuth when ResumeAuth session_expired", async () => {
|
|
141
|
+
const { client, socket } = makeHarness();
|
|
142
|
+
const auth = makeAuthProvider("WalletPubkey", "WalletSignature");
|
|
143
|
+
client.connectAndAuthenticate(auth, { sessionId: "expired-session" });
|
|
144
|
+
const ws = socket();
|
|
145
|
+
ws.triggerOpen();
|
|
146
|
+
ws.triggerMessage(WELCOME_MESSAGE);
|
|
147
|
+
await flushMicrotasks();
|
|
148
|
+
ws.triggerMessage({
|
|
149
|
+
type: "AuthError",
|
|
150
|
+
data: { reason: "session_expired" },
|
|
151
|
+
});
|
|
152
|
+
await flushMicrotasks();
|
|
153
|
+
const sentTypes = ws.sent.map((payload) => parseClientMessage(payload).type);
|
|
154
|
+
expect(sentTypes).toEqual(["Hello", "ResumeAuth", "StartAuth"]);
|
|
155
|
+
});
|
|
156
|
+
it("emits authenticated with expiresAt from AuthSuccess", () => {
|
|
157
|
+
const { client } = makeHarness();
|
|
158
|
+
const seen = [];
|
|
159
|
+
client.on("authenticated", (sessionId, expiresAt) => {
|
|
160
|
+
seen.push({ sessionId, expiresAt });
|
|
161
|
+
});
|
|
162
|
+
client.handleMessage({
|
|
163
|
+
type: "AuthSuccess",
|
|
164
|
+
data: { session_id: "auth-session", expires_at: 1_710_086_400 },
|
|
165
|
+
});
|
|
166
|
+
expect(seen).toEqual([
|
|
167
|
+
{ sessionId: "auth-session", expiresAt: 1_710_086_400 },
|
|
168
|
+
]);
|
|
169
|
+
});
|
|
170
|
+
it("emits authError reason on AuthError", () => {
|
|
171
|
+
const { client } = makeHarness();
|
|
172
|
+
const reasons = [];
|
|
173
|
+
client.on("authError", (reason) => reasons.push(reason));
|
|
174
|
+
client.handleMessage({
|
|
175
|
+
type: "AuthError",
|
|
176
|
+
data: { reason: "invalid_signature", message: "bad signature bytes" },
|
|
177
|
+
});
|
|
178
|
+
expect(reasons).toEqual(["invalid_signature"]);
|
|
179
|
+
});
|
|
124
180
|
it("drop_oldest policy keeps the latest queued messages", () => {
|
|
125
181
|
const { client, socket } = makeHarness({
|
|
126
182
|
maxPendingMessages: 2,
|
package/dist/ws/client.d.ts
CHANGED
|
@@ -56,7 +56,8 @@ export type ActaWsClientEvents = {
|
|
|
56
56
|
connected: () => void;
|
|
57
57
|
welcome: (msg: WelcomeMessage) => void;
|
|
58
58
|
versionMismatch: (msg: VersionMismatchMessage) => void;
|
|
59
|
-
authenticated: (sessionId: string) => void;
|
|
59
|
+
authenticated: (sessionId: string, expiresAt: number | null) => void;
|
|
60
|
+
authError: (reason: string) => void;
|
|
60
61
|
disconnected: (code: number, reason: string) => void;
|
|
61
62
|
error: (error: Error) => void;
|
|
62
63
|
stateChange: (state: ConnectionState) => void;
|
|
@@ -147,6 +148,7 @@ export declare class ActaWsClient extends TypedEventEmitter<ActaWsClientEvents>
|
|
|
147
148
|
private authProvider;
|
|
148
149
|
private authRequested;
|
|
149
150
|
private startAuthSent;
|
|
151
|
+
private pendingResumeSessionId;
|
|
150
152
|
private connectionState;
|
|
151
153
|
private sessionId;
|
|
152
154
|
private helloSent;
|
|
@@ -163,7 +165,9 @@ export declare class ActaWsClient extends TypedEventEmitter<ActaWsClientEvents>
|
|
|
163
165
|
constructor(options: ActaWsClientOptions);
|
|
164
166
|
connectAnonymous(): void;
|
|
165
167
|
connect(authProvider: AuthProvider): void;
|
|
166
|
-
connectAndAuthenticate(authProvider: AuthProvider
|
|
168
|
+
connectAndAuthenticate(authProvider: AuthProvider, options?: {
|
|
169
|
+
sessionId?: string;
|
|
170
|
+
}): void;
|
|
167
171
|
authenticate(authProvider: AuthProvider): Promise<void>;
|
|
168
172
|
disconnect(): void;
|
|
169
173
|
getConnectionState(): ConnectionState;
|
|
@@ -228,6 +232,7 @@ export declare class ActaWsClient extends TypedEventEmitter<ActaWsClientEvents>
|
|
|
228
232
|
subscribe(channels: WsChannel[], markets?: string[]): void;
|
|
229
233
|
unsubscribe(channels: WsChannel[]): void;
|
|
230
234
|
ping(): void;
|
|
235
|
+
resumeAuth(sessionId: string): void;
|
|
231
236
|
private doConnect;
|
|
232
237
|
private handleMessage;
|
|
233
238
|
/** Taker-only: request current indicative prices for a market + position_type. */
|
|
@@ -235,7 +240,9 @@ export declare class ActaWsClient extends TypedEventEmitter<ActaWsClientEvents>
|
|
|
235
240
|
/** Maker-only: respond to an indicative request (unsigned, non-binding). */
|
|
236
241
|
sendIndicativePricesResponse(resp: IndicativePricesResponseMessage): void;
|
|
237
242
|
private handleAuthRequest;
|
|
243
|
+
private beginAuthHandshake;
|
|
238
244
|
private sendStartAuth;
|
|
245
|
+
private sendResumeAuth;
|
|
239
246
|
private sendHello;
|
|
240
247
|
private flushPendingMessages;
|
|
241
248
|
private handleAuthSuccess;
|
package/dist/ws/client.js
CHANGED
|
@@ -76,6 +76,7 @@ export class ActaWsClient extends TypedEventEmitter {
|
|
|
76
76
|
authProvider = null;
|
|
77
77
|
authRequested = false;
|
|
78
78
|
startAuthSent = false;
|
|
79
|
+
pendingResumeSessionId = null;
|
|
79
80
|
connectionState = "disconnected";
|
|
80
81
|
sessionId = null;
|
|
81
82
|
helloSent = false;
|
|
@@ -119,22 +120,25 @@ export class ActaWsClient extends TypedEventEmitter {
|
|
|
119
120
|
connectAnonymous() {
|
|
120
121
|
this.authProvider = null;
|
|
121
122
|
this.authRequested = false;
|
|
123
|
+
this.pendingResumeSessionId = null;
|
|
122
124
|
this.shouldReconnect = this.options.autoReconnect;
|
|
123
125
|
this.doConnect();
|
|
124
126
|
}
|
|
125
127
|
connect(authProvider) {
|
|
126
128
|
this.authProvider = authProvider;
|
|
127
129
|
this.authRequested = false;
|
|
130
|
+
this.pendingResumeSessionId = null;
|
|
128
131
|
this.shouldReconnect = this.options.autoReconnect;
|
|
129
132
|
this.doConnect();
|
|
130
133
|
}
|
|
131
|
-
connectAndAuthenticate(authProvider) {
|
|
134
|
+
connectAndAuthenticate(authProvider, options) {
|
|
132
135
|
this.authProvider = authProvider;
|
|
133
136
|
this.authRequested = true;
|
|
137
|
+
this.pendingResumeSessionId = options?.sessionId ?? null;
|
|
134
138
|
this.shouldReconnect = this.options.autoReconnect;
|
|
135
139
|
if (this.ws?.readyState === WS_OPEN) {
|
|
136
140
|
if (this.welcomeReceived && !this.startAuthSent) {
|
|
137
|
-
void this.
|
|
141
|
+
void this.beginAuthHandshake().catch((err) => {
|
|
138
142
|
this.emit("error", err);
|
|
139
143
|
this.setConnectionState("error");
|
|
140
144
|
});
|
|
@@ -146,6 +150,7 @@ export class ActaWsClient extends TypedEventEmitter {
|
|
|
146
150
|
async authenticate(authProvider) {
|
|
147
151
|
this.authProvider = authProvider;
|
|
148
152
|
this.authRequested = true;
|
|
153
|
+
this.pendingResumeSessionId = null;
|
|
149
154
|
if (this.ws?.readyState === WS_OPEN) {
|
|
150
155
|
if (this.welcomeReceived && !this.startAuthSent) {
|
|
151
156
|
await this.sendStartAuth();
|
|
@@ -316,6 +321,10 @@ export class ActaWsClient extends TypedEventEmitter {
|
|
|
316
321
|
this.send({ type: "Ping" });
|
|
317
322
|
}
|
|
318
323
|
}
|
|
324
|
+
resumeAuth(sessionId) {
|
|
325
|
+
this.pendingResumeSessionId = sessionId;
|
|
326
|
+
this.sendResumeAuth(sessionId);
|
|
327
|
+
}
|
|
319
328
|
doConnect() {
|
|
320
329
|
if (this.ws) {
|
|
321
330
|
this.cleanup();
|
|
@@ -376,7 +385,7 @@ export class ActaWsClient extends TypedEventEmitter {
|
|
|
376
385
|
this.emit("welcome", message.data);
|
|
377
386
|
this.flushPendingMessages();
|
|
378
387
|
if (this.authRequested && this.authProvider && !this.startAuthSent) {
|
|
379
|
-
void this.
|
|
388
|
+
void this.beginAuthHandshake().catch((err) => {
|
|
380
389
|
this.emit("error", err);
|
|
381
390
|
this.setConnectionState("error");
|
|
382
391
|
});
|
|
@@ -401,10 +410,10 @@ export class ActaWsClient extends TypedEventEmitter {
|
|
|
401
410
|
void this.handleAuthRequest(message.data.challenge);
|
|
402
411
|
break;
|
|
403
412
|
case "AuthSuccess":
|
|
404
|
-
this.handleAuthSuccess(message.data.session_id);
|
|
413
|
+
this.handleAuthSuccess(message.data.session_id, message.data.expires_at);
|
|
405
414
|
break;
|
|
406
415
|
case "AuthError":
|
|
407
|
-
this.handleAuthError(message.data.reason);
|
|
416
|
+
this.handleAuthError(message.data.reason, message.data.message);
|
|
408
417
|
break;
|
|
409
418
|
case "Snapshot":
|
|
410
419
|
this.handleSnapshot(message.data);
|
|
@@ -606,6 +615,13 @@ export class ActaWsClient extends TypedEventEmitter {
|
|
|
606
615
|
this.setConnectionState("error");
|
|
607
616
|
}
|
|
608
617
|
}
|
|
618
|
+
async beginAuthHandshake() {
|
|
619
|
+
if (this.pendingResumeSessionId) {
|
|
620
|
+
this.sendResumeAuth(this.pendingResumeSessionId);
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
await this.sendStartAuth();
|
|
624
|
+
}
|
|
609
625
|
async sendStartAuth() {
|
|
610
626
|
if (!this.authProvider)
|
|
611
627
|
throw new Error("No auth provider configured");
|
|
@@ -613,6 +629,10 @@ export class ActaWsClient extends TypedEventEmitter {
|
|
|
613
629
|
const pubkey = await this.authProvider.getPublicKey();
|
|
614
630
|
this.send({ type: "StartAuth", data: { pubkey } });
|
|
615
631
|
}
|
|
632
|
+
sendResumeAuth(sessionId) {
|
|
633
|
+
this.startAuthSent = true;
|
|
634
|
+
this.send({ type: "ResumeAuth", data: { session_id: sessionId } });
|
|
635
|
+
}
|
|
616
636
|
sendHello() {
|
|
617
637
|
const hello = {
|
|
618
638
|
protocol_version: this.options.protocolVersion,
|
|
@@ -631,10 +651,11 @@ export class ActaWsClient extends TypedEventEmitter {
|
|
|
631
651
|
this.send(msg);
|
|
632
652
|
}
|
|
633
653
|
}
|
|
634
|
-
handleAuthSuccess(sessionId) {
|
|
654
|
+
handleAuthSuccess(sessionId, expiresAt) {
|
|
635
655
|
this.sessionId = sessionId;
|
|
656
|
+
this.pendingResumeSessionId = null;
|
|
636
657
|
this.setConnectionState("authenticated");
|
|
637
|
-
this.emit("authenticated", sessionId);
|
|
658
|
+
this.emit("authenticated", sessionId, expiresAt);
|
|
638
659
|
if (this.subscribedChannels.size > 0) {
|
|
639
660
|
const channels = Array.from(this.subscribedChannels);
|
|
640
661
|
const data = this.hasMarketScope
|
|
@@ -643,9 +664,23 @@ export class ActaWsClient extends TypedEventEmitter {
|
|
|
643
664
|
this.send({ type: "Subscribe", data });
|
|
644
665
|
}
|
|
645
666
|
}
|
|
646
|
-
handleAuthError(reason) {
|
|
667
|
+
handleAuthError(reason, message) {
|
|
668
|
+
this.emit("authError", reason);
|
|
669
|
+
if (reason === "session_expired" &&
|
|
670
|
+
this.authRequested &&
|
|
671
|
+
this.authProvider &&
|
|
672
|
+
this.pendingResumeSessionId) {
|
|
673
|
+
this.pendingResumeSessionId = null;
|
|
674
|
+
this.startAuthSent = false;
|
|
675
|
+
void this.sendStartAuth().catch((err) => {
|
|
676
|
+
this.emit("error", err);
|
|
677
|
+
this.setConnectionState("error");
|
|
678
|
+
});
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
647
681
|
this.setConnectionState("error");
|
|
648
|
-
|
|
682
|
+
const details = message ? `${reason}: ${message}` : reason;
|
|
683
|
+
this.emit("error", new Error(`Authentication failed: ${details}`));
|
|
649
684
|
}
|
|
650
685
|
handleSnapshot(snapshot) {
|
|
651
686
|
this.state.stats = snapshot.stats;
|
|
@@ -845,6 +880,7 @@ export class ActaWsClient extends TypedEventEmitter {
|
|
|
845
880
|
this.helloSent = false;
|
|
846
881
|
this.welcomeReceived = false;
|
|
847
882
|
this.startAuthSent = false;
|
|
883
|
+
this.pendingResumeSessionId = null;
|
|
848
884
|
this.pendingMessages = [];
|
|
849
885
|
this.setConnectionState("disconnected");
|
|
850
886
|
}
|
package/dist/ws/client.test.js
CHANGED
|
@@ -119,6 +119,62 @@ describe("ActaWsClient", () => {
|
|
|
119
119
|
expect(authChallenge.data.challenge).toBe("challenge-text");
|
|
120
120
|
}
|
|
121
121
|
});
|
|
122
|
+
it("connectAndAuthenticate sends ResumeAuth when sessionId is provided", async () => {
|
|
123
|
+
const { client, socket } = makeHarness();
|
|
124
|
+
const auth = makeAuthProvider("WalletPubkey", "WalletSignature");
|
|
125
|
+
client.connectAndAuthenticate(auth, { sessionId: "resume-session-id" });
|
|
126
|
+
const ws = socket();
|
|
127
|
+
ws.triggerOpen();
|
|
128
|
+
ws.triggerMessage(WELCOME_MESSAGE);
|
|
129
|
+
await flushMicrotasks();
|
|
130
|
+
const sentTypes = ws.sent.map((payload) => parseClientMessage(payload).type);
|
|
131
|
+
expect(sentTypes).toEqual(["Hello", "ResumeAuth"]);
|
|
132
|
+
const resumeAuth = parseClientMessage(ws.sent[1]);
|
|
133
|
+
expect(resumeAuth.type).toBe("ResumeAuth");
|
|
134
|
+
if (resumeAuth.type === "ResumeAuth") {
|
|
135
|
+
expect(resumeAuth.data.session_id).toBe("resume-session-id");
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
it("falls back to StartAuth when ResumeAuth session_expired", async () => {
|
|
139
|
+
const { client, socket } = makeHarness();
|
|
140
|
+
const auth = makeAuthProvider("WalletPubkey", "WalletSignature");
|
|
141
|
+
client.connectAndAuthenticate(auth, { sessionId: "expired-session" });
|
|
142
|
+
const ws = socket();
|
|
143
|
+
ws.triggerOpen();
|
|
144
|
+
ws.triggerMessage(WELCOME_MESSAGE);
|
|
145
|
+
await flushMicrotasks();
|
|
146
|
+
ws.triggerMessage({
|
|
147
|
+
type: "AuthError",
|
|
148
|
+
data: { reason: "session_expired" },
|
|
149
|
+
});
|
|
150
|
+
await flushMicrotasks();
|
|
151
|
+
const sentTypes = ws.sent.map((payload) => parseClientMessage(payload).type);
|
|
152
|
+
expect(sentTypes).toEqual(["Hello", "ResumeAuth", "StartAuth"]);
|
|
153
|
+
});
|
|
154
|
+
it("emits authenticated with expiresAt from AuthSuccess", () => {
|
|
155
|
+
const { client } = makeHarness();
|
|
156
|
+
const seen = [];
|
|
157
|
+
client.on("authenticated", (sessionId, expiresAt) => {
|
|
158
|
+
seen.push({ sessionId, expiresAt });
|
|
159
|
+
});
|
|
160
|
+
client.handleMessage({
|
|
161
|
+
type: "AuthSuccess",
|
|
162
|
+
data: { session_id: "auth-session", expires_at: 1_710_086_400 },
|
|
163
|
+
});
|
|
164
|
+
expect(seen).toEqual([
|
|
165
|
+
{ sessionId: "auth-session", expiresAt: 1_710_086_400 },
|
|
166
|
+
]);
|
|
167
|
+
});
|
|
168
|
+
it("emits authError reason on AuthError", () => {
|
|
169
|
+
const { client } = makeHarness();
|
|
170
|
+
const reasons = [];
|
|
171
|
+
client.on("authError", (reason) => reasons.push(reason));
|
|
172
|
+
client.handleMessage({
|
|
173
|
+
type: "AuthError",
|
|
174
|
+
data: { reason: "invalid_signature", message: "bad signature bytes" },
|
|
175
|
+
});
|
|
176
|
+
expect(reasons).toEqual(["invalid_signature"]);
|
|
177
|
+
});
|
|
122
178
|
it("drop_oldest policy keeps the latest queued messages", () => {
|
|
123
179
|
const { client, socket } = makeHarness({
|
|
124
180
|
maxPendingMessages: 2,
|
package/dist/ws/types.d.ts
CHANGED
|
@@ -78,6 +78,11 @@ export type ClientMessage = {
|
|
|
78
78
|
data: {
|
|
79
79
|
pubkey: string;
|
|
80
80
|
};
|
|
81
|
+
} | {
|
|
82
|
+
type: "ResumeAuth";
|
|
83
|
+
data: {
|
|
84
|
+
session_id: string;
|
|
85
|
+
};
|
|
81
86
|
} | {
|
|
82
87
|
type: "AuthChallenge";
|
|
83
88
|
data: AuthChallengeData;
|
|
@@ -255,11 +260,13 @@ export type ServerMessage = {
|
|
|
255
260
|
type: "AuthSuccess";
|
|
256
261
|
data: {
|
|
257
262
|
session_id: string;
|
|
263
|
+
expires_at: WsI64 | null;
|
|
258
264
|
};
|
|
259
265
|
} | {
|
|
260
266
|
type: "AuthError";
|
|
261
267
|
data: {
|
|
262
268
|
reason: ErrorMessage;
|
|
269
|
+
message?: string;
|
|
263
270
|
};
|
|
264
271
|
} | {
|
|
265
272
|
type: "RfqCreated";
|
|
@@ -818,6 +825,8 @@ export type PositionInfo = {
|
|
|
818
825
|
/** Net premium amount from on-chain position state (quote token base units). */
|
|
819
826
|
total_premium: WsU64;
|
|
820
827
|
created_at: WsU64;
|
|
828
|
+
/** Market expiry timestamp (unix seconds), sourced from markets table join. */
|
|
829
|
+
expiry_ts: WsU64;
|
|
821
830
|
};
|
|
822
831
|
export type TradeInfo = {
|
|
823
832
|
id: UuidString;
|