Skip to content

Commit 4ab37ef

Browse files
authored
chore(game-bridge): remove immutable x functions from game bridge (#2794)
1 parent bbcb69f commit 4ab37ef

File tree

4 files changed

+171
-905
lines changed

4 files changed

+171
-905
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
"devDependencies": {
1010
"@actions/core": "^1.10.1",
1111
"@emotion/react": "^11.11.3",
12+
"@jest/test-sequencer": "^29.7.0",
1213
"@nx/js": "^19.7.2",
1314
"@swc-node/register": "^1.10.9",
1415
"@swc/cli": "^0.4.0",
1516
"@swc/core": "^1.3.36",
16-
"@jest/test-sequencer": "^29.7.0",
1717
"@types/chai": "^4.3.16",
1818
"@typescript-eslint/eslint-plugin": "^5.57.1",
1919
"@typescript-eslint/parser": "^5.57.1",
@@ -24,6 +24,7 @@
2424
"eslint-config-airbnb": "^19.0.4",
2525
"eslint-config-airbnb-typescript": "^17.0.0",
2626
"eslint-plugin-react-refresh": "latest",
27+
"events": "^3.3.0",
2728
"http-server": "^14.1.1",
2829
"husky": "^8.0.3",
2930
"lint-staged": "^13.2.0",
@@ -33,8 +34,8 @@
3334
"string_decoder": "^1.3.0",
3435
"syncpack": "^13.0.0",
3536
"tsup": "8.3.0",
36-
"typescript": "^5.6.2",
3737
"typedoc": "^0.26.5",
38+
"typescript": "^5.6.2",
3839
"wsrun": "^5.2.4"
3940
},
4041
"engines": {

packages/game-bridge/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"@imtbl/metrics": "workspace:*",
77
"@imtbl/passport": "workspace:*",
88
"@imtbl/x-client": "workspace:*",
9-
"@imtbl/x-provider": "workspace:*",
109
"ethers": "^6.13.4"
1110
},
1211
"devDependencies": {

packages/game-bridge/src/index.ts

Lines changed: 0 additions & 245 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable no-console */
22
import * as passport from '@imtbl/passport';
33
import * as config from '@imtbl/config';
4-
import * as provider from '@imtbl/x-provider';
54
import * as xClient from '@imtbl/x-client';
65
import {
76
track,
@@ -43,24 +42,15 @@ const sdkVersionSha = '__SDK_VERSION_SHA__';
4342
const PASSPORT_FUNCTIONS = {
4443
init: 'init',
4544
relogin: 'relogin',
46-
reconnect: 'reconnect',
4745
getPKCEAuthUrl: 'getPKCEAuthUrl',
4846
loginPKCE: 'loginPKCE',
49-
connectPKCE: 'connectPKCE',
5047
getAccessToken: 'getAccessToken',
5148
getIdToken: 'getIdToken',
5249
logout: 'logout',
5350
getEmail: 'getEmail',
5451
getPassportId: 'getPassportId',
5552
getLinkedAddresses: 'getLinkedAddresses',
5653
storeTokens: 'storeTokens',
57-
imx: {
58-
getAddress: 'getAddress',
59-
isRegisteredOffchain: 'isRegisteredOffchain',
60-
registerOffchain: 'registerOffchain',
61-
transfer: 'imxTransfer',
62-
batchNftTransfer: 'imxBatchNftTransfer',
63-
},
6454
zkEvm: {
6555
connectEvm: 'connectEvm',
6656
sendTransaction: 'zkEvmSendTransaction',
@@ -72,64 +62,12 @@ const PASSPORT_FUNCTIONS = {
7262
},
7363
};
7464

75-
function getHttpErrorSummary(err: unknown): {
76-
status?: number;
77-
fullUrl?: string;
78-
traceId?: string;
79-
requestId?: string;
80-
cfRay?: string;
81-
} {
82-
const e: any = err as any;
83-
const status: number | undefined = e?.response?.status;
84-
const url: string | undefined = e?.config?.url;
85-
const baseURL: string | undefined = e?.config?.baseURL;
86-
let fullUrl = typeof url === 'string' && typeof baseURL === 'string' && !/^https?:\/\//i.test(url)
87-
? `${baseURL}${url}`
88-
: url;
89-
90-
// Remove query parameters to avoid exposing sensitive data (tokens, API keys, etc.)
91-
if (typeof fullUrl === 'string') {
92-
const queryIndex = fullUrl.indexOf('?');
93-
if (queryIndex !== -1) {
94-
fullUrl = fullUrl.substring(0, queryIndex);
95-
}
96-
}
97-
98-
const headers: Record<string, string> | undefined = e?.response?.headers;
99-
const traceId = headers?.['x-amzn-trace-id'] ?? headers?.['x-trace-id'];
100-
const requestId = headers?.['x-amzn-requestid']
101-
?? headers?.['x-amzn-request-id']
102-
?? headers?.['x-request-id'];
103-
const cfRay = headers?.['cf-ray'];
104-
105-
return {
106-
status,
107-
fullUrl,
108-
traceId,
109-
requestId,
110-
cfRay,
111-
};
112-
}
113-
114-
function parseHttpStatusSuffix(message: string): { status?: number; url?: string } {
115-
// Matches our existing suffix formats like:
116-
// "... [httpStatus=500 url=https://...]" or
117-
// "... [httpStatus=500 url=https://... trace=... ...]"
118-
const m = message.match(/\[httpStatus=([^\s\]]+)\s+url=([^\s\]]+)[^\]]*\]/);
119-
if (!m) return {};
120-
const statusStr = m[1];
121-
const url = m[2];
122-
const status = statusStr && /^[0-9]+$/.test(statusStr) ? Number(statusStr) : undefined;
123-
return { status, url };
124-
}
125-
12665
// To notify game engine that this file is loaded
12766
const initRequest = 'init';
12867
const initRequestId = '1';
12968

13069
let passportClient: passport.Passport | null;
13170
let passportInitData: string | null = null;
132-
let providerInstance: provider.IMXProvider | null;
13371
let zkEvmProviderInstance: passport.Provider | null;
13472
let versionInfo: VersionInfo | null;
13573

@@ -212,25 +150,6 @@ const getPassportClient = (): passport.Passport => {
212150
return passportClient;
213151
};
214152

215-
const setProvider = (
216-
passportProvider: provider.IMXProvider | null | undefined,
217-
): boolean => {
218-
if (passportProvider !== null && passportProvider !== undefined) {
219-
providerInstance = passportProvider;
220-
console.log('IMX provider set');
221-
return true;
222-
}
223-
console.log('No IMX provider');
224-
return false;
225-
};
226-
227-
const getProvider = (): provider.IMXProvider => {
228-
if (providerInstance == null) {
229-
throw new Error('No IMX provider');
230-
}
231-
return providerInstance;
232-
};
233-
234153
const setZkEvmProvider = (zkEvmProvider: passport.Provider | null | undefined): boolean => {
235154
if (zkEvmProvider !== null && zkEvmProvider !== undefined) {
236155
zkEvmProviderInstance = zkEvmProvider;
@@ -397,32 +316,6 @@ window.callFunction = async (jsonData: string) => {
397316
});
398317
break;
399318
}
400-
case PASSPORT_FUNCTIONS.reconnect: {
401-
let providerSet = false;
402-
const userInfo = await getPassportClient().login({
403-
useCachedSession: true,
404-
});
405-
if (userInfo) {
406-
const passportProvider = await getPassportClient().connectImx();
407-
providerSet = setProvider(passportProvider);
408-
identify({ passportId: userInfo?.sub });
409-
}
410-
411-
if (!providerSet) {
412-
throw new Error('Failed to reconnect');
413-
}
414-
415-
trackDuration(moduleName, 'performedReconnect', mt(markStart), {
416-
succeeded: userInfo !== null,
417-
});
418-
callbackToGame({
419-
responseFor: fxName,
420-
requestId,
421-
success: providerSet,
422-
error: null,
423-
});
424-
break;
425-
}
426319
case PASSPORT_FUNCTIONS.getPKCEAuthUrl: {
427320
const request = data ? JSON.parse(data) : {};
428321
const directLoginOptions: passport.DirectLoginOptions | undefined = request?.directLoginOptions;
@@ -454,34 +347,8 @@ window.callFunction = async (jsonData: string) => {
454347
});
455348
break;
456349
}
457-
case PASSPORT_FUNCTIONS.connectPKCE: {
458-
const request = JSON.parse(data);
459-
const profile = await getPassportClient().loginWithPKCEFlowCallback(
460-
request.authorizationCode,
461-
request.state,
462-
);
463-
const passportProvider = await getPassportClient().connectImx();
464-
const providerSet = setProvider(passportProvider);
465-
466-
if (!providerSet) {
467-
throw new Error('Failed to connect via PKCE');
468-
}
469-
470-
identify({ passportId: profile.sub });
471-
trackDuration(moduleName, 'performedConnectPkce', mt(markStart), {
472-
succeeded: providerSet,
473-
});
474-
callbackToGame({
475-
responseFor: fxName,
476-
requestId,
477-
success: providerSet,
478-
error: null,
479-
});
480-
break;
481-
}
482350
case PASSPORT_FUNCTIONS.logout: {
483351
const logoutUrl = await getPassportClient().getLogoutUrl();
484-
providerInstance = null;
485352
zkEvmProviderInstance = null;
486353
trackDuration(moduleName, 'performedGetLogoutUrl', mt(markStart));
487354
callbackToGame({
@@ -590,84 +457,6 @@ window.callFunction = async (jsonData: string) => {
590457
});
591458
break;
592459
}
593-
case PASSPORT_FUNCTIONS.imx.getAddress: {
594-
const address = await getProvider().getAddress();
595-
trackDuration(moduleName, 'performedImxGetAddress', mt(markStart));
596-
callbackToGame({
597-
responseFor: fxName,
598-
requestId,
599-
success: true,
600-
error: null,
601-
result: address,
602-
});
603-
break;
604-
}
605-
case PASSPORT_FUNCTIONS.imx.isRegisteredOffchain: {
606-
const registered = await getProvider().isRegisteredOffchain();
607-
trackDuration(moduleName, 'performedImxIsRegisteredOffchain', mt(markStart));
608-
callbackToGame({
609-
responseFor: fxName,
610-
requestId,
611-
success: true,
612-
error: null,
613-
result: registered,
614-
});
615-
break;
616-
}
617-
case PASSPORT_FUNCTIONS.imx.registerOffchain: {
618-
const response = await getProvider().registerOffchain();
619-
trackDuration(moduleName, 'performedImxRegisterOffchain', mt(markStart));
620-
callbackToGame({
621-
...{
622-
responseFor: fxName,
623-
requestId,
624-
success: true,
625-
error: null,
626-
},
627-
...response,
628-
});
629-
break;
630-
}
631-
case PASSPORT_FUNCTIONS.imx.transfer: {
632-
const unsignedTransferRequest = JSON.parse(data);
633-
const response = await getProvider().transfer(unsignedTransferRequest);
634-
trackDuration(moduleName, 'performedImxTransfer', mt(markStart), {
635-
requestId,
636-
transferRequest: JSON.stringify(unsignedTransferRequest),
637-
transferResponse: JSON.stringify(response),
638-
});
639-
callbackToGame({
640-
...{
641-
responseFor: fxName,
642-
requestId,
643-
success: true,
644-
error: null,
645-
},
646-
...response,
647-
});
648-
break;
649-
}
650-
case PASSPORT_FUNCTIONS.imx.batchNftTransfer: {
651-
const nftTransferDetails = JSON.parse(data);
652-
const response = await getProvider().batchNftTransfer(
653-
nftTransferDetails,
654-
);
655-
trackDuration(moduleName, 'performedImxBatchNftTransfer', mt(markStart), {
656-
requestId,
657-
transferRequest: JSON.stringify(nftTransferDetails),
658-
transferResponse: JSON.stringify(response),
659-
});
660-
callbackToGame({
661-
...{
662-
responseFor: fxName,
663-
requestId,
664-
success: true,
665-
error: null,
666-
},
667-
...response,
668-
});
669-
break;
670-
}
671460
case PASSPORT_FUNCTIONS.zkEvm.connectEvm: {
672461
const zkEvmProvider = await getPassportClient().connectEvm();
673462
const providerSet = setZkEvmProvider(zkEvmProvider);
@@ -869,40 +658,6 @@ window.callFunction = async (jsonData: string) => {
869658
}
870659

871660
// Make endpoint visible in Unity Output for debugging (CI logs don't include JS console).
872-
const {
873-
status,
874-
fullUrl,
875-
traceId,
876-
requestId: httpRequestId,
877-
cfRay,
878-
} = getHttpErrorSummary(error);
879-
if (
880-
fxName === PASSPORT_FUNCTIONS.imx.registerOffchain
881-
&& wrappedError instanceof Error
882-
) {
883-
// Some upstream errors embed "[httpStatus=... url=...]" only in the message string
884-
// without preserving axios-like fields. Parse what we can so we can still enrich.
885-
const parsed = parseHttpStatusSuffix(wrappedError.message);
886-
const effectiveStatus = status ?? parsed.status;
887-
const effectiveUrl = fullUrl ?? parsed.url;
888-
const suffix = ` [httpStatus=${effectiveStatus ?? 'unknown'}`
889-
+ ` url=${effectiveUrl ?? 'unknown'}`
890-
+ ` trace=${traceId ?? 'unknown'}`
891-
+ ` reqId=${httpRequestId ?? 'unknown'}`
892-
+ ` cfRay=${cfRay ?? 'unknown'}]`;
893-
// If a previous layer already added a minimal suffix like:
894-
// "... [httpStatus=500 url=...]"
895-
// upgrade it to include trace/reqId/resp instead of skipping.
896-
if (wrappedError.message.includes('[httpStatus=')) {
897-
if (!wrappedError.message.includes('trace=')) {
898-
const upgraded = wrappedError.message.replace(/\[httpStatus=[^\]]*\]/g, suffix.trim());
899-
wrappedError = new Error(upgraded);
900-
}
901-
} else {
902-
wrappedError = new Error(`${wrappedError.message}${suffix}`);
903-
}
904-
}
905-
906661
const errorType = error instanceof passport.PassportError
907662
? error?.type
908663
: undefined;

0 commit comments

Comments
 (0)