From bf67f11ba8c56958f2d171f3d8f374c9810b6323 Mon Sep 17 00:00:00 2001 From: Samuel Castleman <142861450+scastleman-immutable@users.noreply.github.com> Date: Mon, 29 Jan 2024 13:44:07 +1100 Subject: [PATCH] retrieve chain id from api instead of hardcoding (#1370) --- packages/passport/sdk/src/Passport.int.test.ts | 2 ++ packages/passport/sdk/src/mocks/zkEvm/msw.ts | 13 +++++++++++++ packages/passport/sdk/src/test/mocks.ts | 12 ++++++++++++ .../sdk/src/zkEvm/user/registerZkEvmUser.test.ts | 7 +++++-- .../sdk/src/zkEvm/user/registerZkEvmUser.ts | 6 ++++-- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/passport/sdk/src/Passport.int.test.ts b/packages/passport/sdk/src/Passport.int.test.ts index d9b4ceedae..2f5558b0c7 100644 --- a/packages/passport/sdk/src/Passport.int.test.ts +++ b/packages/passport/sdk/src/Passport.int.test.ts @@ -139,6 +139,7 @@ describe('Passport', () => { mockSigninSilent.mockResolvedValueOnce(mockOidcUserZkevm); useMswHandlers([ mswHandlers.counterfactualAddress.success, + mswHandlers.api.chains.success, ]); const zkEvmProvider = getZkEvmProvider(); @@ -160,6 +161,7 @@ describe('Passport', () => { mockSigninSilent.mockResolvedValue(mockOidcUserZkevm); useMswHandlers([ mswHandlers.counterfactualAddress.internalServerError, + mswHandlers.api.chains.success, ]); const zkEvmProvider = getZkEvmProvider(); diff --git a/packages/passport/sdk/src/mocks/zkEvm/msw.ts b/packages/passport/sdk/src/mocks/zkEvm/msw.ts index 3d67504b17..bbe4ea3433 100644 --- a/packages/passport/sdk/src/mocks/zkEvm/msw.ts +++ b/packages/passport/sdk/src/mocks/zkEvm/msw.ts @@ -115,6 +115,19 @@ export const mswHandlers = { success: rest.post('https://api.sandbox.immutable.com/guardian/v1/transactions/evm/evaluate', (req, res, ctx) => res(ctx.status(200))), }, }, + api: { + chains: { + success: rest.get('https://api.sandbox.immutable.com/v1/chains', async (req, res, ctx) => res(ctx.json({ + result: [ + { + id: 'eip155:13473', + name: 'Immutable zkEVM Test', + rpc_url: 'https://rpc.testnet.immutable.com', + }, + ], + }))), + }, + }, }; let mswWorker: SetupServer; diff --git a/packages/passport/sdk/src/test/mocks.ts b/packages/passport/sdk/src/test/mocks.ts index 0c384cc9c8..75a481393f 100644 --- a/packages/passport/sdk/src/test/mocks.ts +++ b/packages/passport/sdk/src/test/mocks.ts @@ -58,3 +58,15 @@ export const mockLinkedAddresses = { ], }, }; + +export const mockListChains = { + data: { + result: [ + { + id: 'eip155:13473', + name: 'Immutable zkEVM Test', + rpc_url: 'https://rpc.testnet.immutable.com', + }, + ], + }, +}; diff --git a/packages/passport/sdk/src/zkEvm/user/registerZkEvmUser.test.ts b/packages/passport/sdk/src/zkEvm/user/registerZkEvmUser.test.ts index 0a4b5142e3..e90f01f7e6 100644 --- a/packages/passport/sdk/src/zkEvm/user/registerZkEvmUser.test.ts +++ b/packages/passport/sdk/src/zkEvm/user/registerZkEvmUser.test.ts @@ -4,7 +4,7 @@ import { MultiRollupApiClients } from '@imtbl/generated-clients'; import { ChainId, ChainName } from 'network/chains'; import { registerZkEvmUser } from './registerZkEvmUser'; import AuthManager from '../../authManager'; -import { mockUser, mockUserZkEvm } from '../../test/mocks'; +import { mockListChains, mockUser, mockUserZkEvm } from '../../test/mocks'; jest.mock('@ethersproject/providers'); jest.mock('@imtbl/toolkit'); @@ -23,6 +23,9 @@ describe('registerZkEvmUser', () => { passportApi: { createCounterfactualAddressV2: jest.fn(), }, + chainsApi: { + listChains: jest.fn(), + }, }; const jsonRPCProvider = { ready: { @@ -41,6 +44,7 @@ describe('registerZkEvmUser', () => { getSignerMock.mockReturnValue(ethSignerMock); ethSignerMock.getAddress.mockResolvedValue(ethereumAddress); (signRaw as jest.Mock).mockResolvedValue(ethereumSignature); + multiRollupApiClients.chainsApi.listChains.mockImplementation(() => mockListChains); }); describe('when createCounterfactualAddressV2 doesn\'t return a 201', () => { @@ -48,7 +52,6 @@ describe('registerZkEvmUser', () => { multiRollupApiClients.passportApi.createCounterfactualAddressV2.mockImplementation(() => { throw new Error('Internal server error'); }); - await expect(async () => registerZkEvmUser({ authManager: authManager as unknown as AuthManager, magicProvider, diff --git a/packages/passport/sdk/src/zkEvm/user/registerZkEvmUser.ts b/packages/passport/sdk/src/zkEvm/user/registerZkEvmUser.ts index 753504eb85..b47ff5aa6e 100644 --- a/packages/passport/sdk/src/zkEvm/user/registerZkEvmUser.ts +++ b/packages/passport/sdk/src/zkEvm/user/registerZkEvmUser.ts @@ -1,7 +1,7 @@ import { ExternalProvider, JsonRpcProvider, Web3Provider } from '@ethersproject/providers'; import { MultiRollupApiClients } from '@imtbl/generated-clients'; import { signRaw } from '@imtbl/toolkit'; -import { CHAIN_NAME_MAP } from 'network/constants'; +import { getEip155ChainId } from 'zkEvm/walletHelpers'; import { UserZkEvm } from '../../types'; import AuthManager from '../../authManager'; import { JsonRpcError, RpcErrorCode } from '../JsonRpcError'; @@ -34,9 +34,11 @@ export async function registerZkEvmUser({ const headers = { Authorization: `Bearer ${accessToken}` }; const { chainId } = await jsonRpcProvider.ready; + const eipChainId = getEip155ChainId(chainId); try { - const chainName = CHAIN_NAME_MAP.get(chainId); + const chainList = (await multiRollupApiClients.chainsApi.listChains()).data.result; + const chainName = chainList.find((chain) => chain.id === eipChainId)?.name; if (!chainName) { throw new JsonRpcError(RpcErrorCode.INTERNAL_ERROR, `Chain name does not exist on for chain id ${chainId}`); }