@@ -19,7 +19,7 @@ import type { WasmLib as FrostWasmLib } from "@toruslabs/tss-frost-lib";
1919import BN from "bn.js" ;
2020import bowser from "bowser" ;
2121import { ec as EC } from "elliptic" ;
22- import { io } from "socket.io-client" ;
22+ import { Socket } from "socket.io-client" ;
2323
2424import {
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-
14376export 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