Skip to content

Commit be2d208

Browse files
pass socket in constructor
1 parent 7fc1a94 commit be2d208

File tree

5 files changed

+83
-77
lines changed

5 files changed

+83
-77
lines changed

demo/redirect-flow-example/package-lock.json

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/redirect-flow-example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"jsrsasign": "^10.6.1",
2222
"react": "^18.3.1",
2323
"react-dom": "^18.3.1",
24-
"socket.io-client": "^4.8.0",
24+
"socket.io-client": "^4.8.1",
2525
"typescript": "^5.6.3",
2626
"web3": "^4.13.0"
2727
},

demo/redirect-flow-example/src/App.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import { KeyType, Point } from "@tkey/common-types";
2121
import { tssLib as originalTssLib } from "@toruslabs/tss-dkls-lib";
2222
// import{ tssLib } from "@toruslabs/tss-frost-lib";
2323
import { fetchLocalConfig } from "@toruslabs/fnd-base";
24-
import { setupSockets as originalSetupSockets, createSockets as originalCreateSockets } from "@toruslabs/tss-client";
25-
import { io } from "socket.io-client";
24+
import { io, Socket } from "socket.io-client";
2625

2726
import "./App.css";
2827
import jwt, { Algorithm } from "jsonwebtoken";
@@ -56,6 +55,29 @@ const tssLib = {
5655
...originalTssLib,
5756
};
5857

58+
// Custom socket setup function
59+
// Custom function to create sockets with polling only
60+
const createSocketsWithPolling = (wsEndpoints: string[], sessionId: string, socketPath = "/tss/socket.io"): (Socket | null)[] => {
61+
console.log("Creating sockets with sessionId:", sessionId);
62+
return wsEndpoints.map((wsEndpoint) => {
63+
if (wsEndpoint === null || wsEndpoint === undefined) {
64+
return null;
65+
}
66+
return io(wsEndpoint, {
67+
path: socketPath,
68+
transports: ["websocket","polling"],
69+
query: { sessionId },
70+
extraHeaders: {
71+
"x-web3-session-id": sessionId,
72+
},
73+
withCredentials: true,
74+
reconnectionDelayMax: 10000,
75+
reconnectionAttempts: 10,
76+
});
77+
});
78+
};
79+
80+
5981
const coreKitInstance = new Web3AuthMPCCoreKit({
6082
web3AuthClientId: "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ",
6183
web3AuthNetwork: selectedNetwork,
@@ -65,7 +87,7 @@ const coreKitInstance = new Web3AuthMPCCoreKit({
6587
// sessionTime: 3600, // <== can provide variable session time based on user subscribed plan
6688
tssLib, // Using our custom TSS lib with polling-only sockets
6789
useDKG: false,
68-
});
90+
}, createSocketsWithPolling);
6991

7092
const privateKey = "MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCCD7oLrcKae+jVZPGx52Cb/lKhdKxpXjl9eGNa1MlY57A==";
7193
const jwtPrivateKey = `-----BEGIN PRIVATE KEY-----\n${privateKey}\n-----END PRIVATE KEY-----`;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3auth/mpc-core-kit",
3-
"version": "3.4.3",
3+
"version": "3.4.4",
44
"description": "MPC CoreKit SDK for web3Auth",
55
"keywords": [
66
"web3Auth/mpc-core-kit",

src/mpcCoreKit.ts

Lines changed: 52 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type { WasmLib as FrostWasmLib } from "@toruslabs/tss-frost-lib";
1919
import BN from "bn.js";
2020
import bowser from "bowser";
2121
import { ec as EC } from "elliptic";
22-
import { io } from "socket.io-client";
22+
import { Socket } from "socket.io-client";
2323

2424
import {
2525
ERRORS,
@@ -73,73 +73,6 @@ import {
7373
scalarBNToBufferSEC1,
7474
} from "./utils";
7575

76-
// Custom socket setup function
77-
// Custom function to create sockets with polling only
78-
const createSocketsWithPolling = (wsEndpoints: string[], sessionId: string, socketPath = "/tss/socket.io") => {
79-
console.log("Creating sockets with sessionId:", sessionId);
80-
return wsEndpoints.map((wsEndpoint) => {
81-
if (wsEndpoint === null || wsEndpoint === undefined) {
82-
return null;
83-
}
84-
return io(wsEndpoint, {
85-
path: socketPath,
86-
transports: ["polling"],
87-
query: { sessionId },
88-
extraHeaders: {
89-
"x-web3-session-id": sessionId,
90-
},
91-
withCredentials: true,
92-
reconnectionDelayMax: 10000,
93-
reconnectionAttempts: 10,
94-
});
95-
});
96-
};
97-
98-
// Custom setup sockets function
99-
const customSetupSockets = async (tssWSEndpoints: string[], sessionId: string, socketPath = "/tss/socket.io") => {
100-
console.log("Custom socket setup starting with polling transport:", { tssWSEndpoints, sessionId });
101-
102-
try {
103-
// Create sockets with polling only
104-
const sockets = createSocketsWithPolling(tssWSEndpoints, sessionId, socketPath);
105-
106-
// Add monitoring
107-
sockets.forEach((socket, index) => {
108-
if (socket) {
109-
socket.on("connect", () => {
110-
console.log(`Socket ${index} connected to ${tssWSEndpoints[index]} using polling`);
111-
});
112-
113-
socket.on("disconnect", () => {
114-
console.log(`Socket ${index} disconnected from ${tssWSEndpoints[index]}`);
115-
});
116-
117-
socket.on("error", (error: unknown) => {
118-
console.error(`Socket ${index} error:`, error);
119-
});
120-
}
121-
});
122-
123-
// Wait for connections
124-
await new Promise((resolve) => {
125-
const checkConnectionTimer = setInterval(() => {
126-
for (let i = 0; i < sockets.length; i++) {
127-
const socket = sockets[i];
128-
if (socket && !socket.connected) return;
129-
}
130-
clearInterval(checkConnectionTimer);
131-
resolve(true);
132-
}, 100);
133-
});
134-
135-
console.log("All sockets connected successfully using polling transport");
136-
return sockets;
137-
} catch (error) {
138-
console.error("Custom socket setup failed:", error);
139-
throw error;
140-
}
141-
};
142-
14376
export class Web3AuthMPCCoreKit implements ICoreKit {
14477
public state: Web3AuthState = { accountIndex: 0 };
14578

@@ -171,7 +104,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
171104

172105
private preSigningHook?: PreSigningHookType;
173106

174-
constructor(options: Web3AuthOptions) {
107+
constructor(
108+
options: Web3AuthOptions,
109+
private readonly createSocketsWithPolling: (wsEndpoints: string[], sessionId: string, socketPath?: string) => (Socket | null)[]
110+
) {
175111
if (!options.web3AuthClientId) {
176112
throw CoreKitError.clientIdInvalid();
177113
}
@@ -208,6 +144,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
208144
}
209145

210146
TorusUtils.setSessionTime(this.options.sessionTime);
147+
this.createSocketsWithPolling = createSocketsWithPolling;
211148
}
212149

213150
get tKey(): TKeyTSS {
@@ -805,7 +742,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
805742
} = generateTSSEndpoints(torusNodeTSSEndpoints, parties, clientIndex, nodeIndexes);
806743

807744
// Setup sockets.
808-
const sockets = await customSetupSockets(tssWSEndpoints, randomSessionNonce);
745+
const sockets = await this.customSetupSockets(tssWSEndpoints, randomSessionNonce);
809746

810747
const dklsCoeff = getDKLSCoeff(true, participatingServerDKGIndexes, tssShareIndex);
811748
const denormalisedShare = dklsCoeff.mul(tssShare).umod(secp256k1.curve.n);
@@ -1591,4 +1528,49 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
15911528
return (this._tssLib as V3TSSLibType).lib as DKLSWasmLib | FrostWasmLib;
15921529
}
15931530
}
1531+
1532+
// Custom setup sockets function
1533+
private async customSetupSockets(tssWSEndpoints: string[], sessionId: string, socketPath = "/tss/socket.io") {
1534+
console.log("Custom socket setup starting with polling transport:", { tssWSEndpoints, sessionId });
1535+
1536+
try {
1537+
// Create sockets with polling only
1538+
const sockets = this.createSocketsWithPolling(tssWSEndpoints, sessionId, socketPath);
1539+
1540+
// Add monitoring
1541+
sockets.forEach((socket, index) => {
1542+
if (socket) {
1543+
socket.on("connect", () => {
1544+
console.log(`Socket ${index} connected to ${tssWSEndpoints[index]} using polling`);
1545+
});
1546+
1547+
socket.on("disconnect", () => {
1548+
console.log(`Socket ${index} disconnected from ${tssWSEndpoints[index]}`);
1549+
});
1550+
1551+
socket.on("error", (error: unknown) => {
1552+
console.error(`Socket ${index} error:`, error);
1553+
});
1554+
}
1555+
});
1556+
1557+
// Wait for connections
1558+
await new Promise((resolve) => {
1559+
const checkConnectionTimer = setInterval(() => {
1560+
for (let i = 0; i < sockets.length; i++) {
1561+
const socket = sockets[i];
1562+
if (socket && !socket.connected) return;
1563+
}
1564+
clearInterval(checkConnectionTimer);
1565+
resolve(true);
1566+
}, 100);
1567+
});
1568+
1569+
console.log("All sockets connected successfully using polling transport");
1570+
return sockets;
1571+
} catch (error) {
1572+
console.error("Custom socket setup failed:", error);
1573+
throw error;
1574+
}
1575+
}
15941576
}

0 commit comments

Comments
 (0)