Skip to content

Commit c399d94

Browse files
committed
We should enabled it on the following networks:
Ethereum Mainnet (already done) BNB chain Polygon Arbitrum Optimism Avalanche Test case for supported chain id Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Add linea mainnet to supported chain IDs. Lint fixes Fix tests Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
1 parent a264d25 commit c399d94

File tree

3 files changed

+79
-36
lines changed

3 files changed

+79
-36
lines changed

src/ppom-controller.test.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,32 @@ describe('PPOMController', () => {
192192
}).rejects.toThrow('User has securityAlertsEnabled set to false');
193193
});
194194

195-
it('should throw error if the user is not on ethereum mainnet', async () => {
195+
it('should NOT throw error if the user is on any supported chain id', async () => {
196+
buildFetchSpy();
197+
ppomController = buildPPOMController({
198+
chainId: '0x38',
199+
state: {
200+
versionInfo: [
201+
{
202+
name: 'blob',
203+
chainId: '0x38',
204+
version: '1.0.0',
205+
checksum:
206+
'409a7f83ac6b31dc8c77e3ec18038f209bd2f545e0f4177c2e2381aa4e067b49',
207+
filePath: 'blob',
208+
},
209+
],
210+
},
211+
});
212+
jest.runOnlyPendingTimers();
213+
const result = await ppomController.usePPOM(async () => {
214+
return Promise.resolve(buildDummyResponse());
215+
});
216+
217+
expect(result).toStrictEqual(dummyResponse);
218+
});
219+
220+
it('should throw error if the user is not on supported chain id', async () => {
196221
buildFetchSpy();
197222
ppomController = buildPPOMController({
198223
chainId: '0x2',
@@ -203,7 +228,7 @@ describe('PPOMController', () => {
203228
return Promise.resolve();
204229
});
205230
}).rejects.toThrow(
206-
'Blockaid validation is available only on ethereum mainnet',
231+
'Blockaid validation is not available on selected network',
207232
);
208233
});
209234

@@ -593,11 +618,14 @@ describe('PPOMController', () => {
593618
jest.useFakeTimers().setSystemTime(new Date('2023-01-04'));
594619
callBack({ providerConfig: { chainId: '0x6' } });
595620

596-
expect(Object.keys(ppomController.state.chainStatus)).toHaveLength(5);
621+
jest.useFakeTimers().setSystemTime(new Date('2023-01-04'));
622+
callBack({ providerConfig: { chainId: '0x8' } });
623+
624+
expect(Object.keys(ppomController.state.chainStatus)).toHaveLength(6);
597625

598626
jest.useFakeTimers().setSystemTime(new Date('2023-01-06'));
599627
callBack({ providerConfig: { chainId: '0x7' } });
600-
expect(Object.keys(ppomController.state.chainStatus)).toHaveLength(5);
628+
expect(Object.keys(ppomController.state.chainStatus)).toHaveLength(6);
601629

602630
expect(ppomController.state.chainStatus['0x1']).toBeUndefined();
603631
});

src/ppom-controller.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ const ALLOWED_PROVIDER_CALLS = [
5151
'trace_filter',
5252
];
5353

54-
const ETHEREUM_CHAIN_ID = '0x1';
54+
export const SUPPORTED_CHAIN_IDS = [
55+
'0x1', // mainnet chain id
56+
'0x38', // bnb chain id
57+
'0x89', // polygon chain id
58+
'0xa4b1', // arbitrum chain id
59+
'0xa', // optimism chain id
60+
'0xa86a', // avalanche chain id
61+
'0xe708', // Linea chain id
62+
];
5563

5664
/**
5765
* @type PPOMFileVersion
@@ -343,7 +351,7 @@ export class PPOMController extends BaseControllerV2<
343351
throw Error('User has securityAlertsEnabled set to false');
344352
}
345353
if (!this.#networkIsSupported(this.#chainId)) {
346-
throw Error('Blockaid validation is available only on ethereum mainnet');
354+
throw Error('Blockaid validation is not available on selected network');
347355
}
348356

349357
await this.#reinitPPOMForNetworkIfRequired();
@@ -387,11 +395,10 @@ export class PPOMController extends BaseControllerV2<
387395
}
388396

389397
/*
390-
* The function check if ethereum chainId is supported for validation
391-
* Currently it checks for only Ethereum Mainnet but it will include more networks in future.
398+
* The function check if chainId is supported for validation
392399
*/
393400
#networkIsSupported(chainId: string) {
394-
return chainId === ETHEREUM_CHAIN_ID;
401+
return SUPPORTED_CHAIN_IDS.includes(chainId);
395402
}
396403

397404
/*
@@ -474,7 +481,7 @@ export class PPOMController extends BaseControllerV2<
474481
// eslint-disable-next-line @typescript-eslint/no-floating-promises
475482
this.#initialisePPOM();
476483
this.#checkScheduleFileDownloadForAllChains();
477-
this.#getNewFilesForChain(ETHEREUM_CHAIN_ID).finally(() => {
484+
this.#getNewFilesForChain(this.#chainId).finally(() => {
478485
if (this.#ppomInitialisationCallback) {
479486
this.#ppomInitialisationCallback();
480487
}
@@ -738,8 +745,9 @@ export class PPOMController extends BaseControllerV2<
738745
const currentTimestamp = new Date().getTime();
739746

740747
const chainIds = Object.keys(this.state.chainStatus).filter(
741-
(id) => id !== ETHEREUM_CHAIN_ID,
748+
(id) => id !== this.#chainId,
742749
);
750+
743751
const oldChaninIds: any[] = chainIds.filter(
744752
(chainId) =>
745753
(this.state.chainStatus[chainId] as any).lastVisited <
@@ -807,7 +815,7 @@ export class PPOMController extends BaseControllerV2<
807815
if (isLastFileOfNetwork) {
808816
// if this was last file for the chainId set dataFetched for chainId to true
809817
await this.#setChainIdDataFetched(fileVersionInfo.chainId);
810-
if (fileVersionInfo.chainId === ETHEREUM_CHAIN_ID) {
818+
if (fileVersionInfo.chainId === this.#chainId) {
811819
await this.#reinitPPOM();
812820
}
813821
}

test/test-utils.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { ControllerMessenger } from '@metamask/base-controller';
22
import * as ControllerUtils from '@metamask/controller-utils';
33

4-
import { PPOMController } from '../src/ppom-controller';
5-
import { StorageKey } from '../src/ppom-storage';
4+
import { PPOMController, SUPPORTED_CHAIN_IDS } from '../src/ppom-controller';
5+
import { FileMetadata, StorageKey } from '../src/ppom-storage';
66

77
export const buildDummyResponse = (
88
resultType = 'DUMMY_RESULT_TYPE',
@@ -37,28 +37,35 @@ export const storageBackendReturningData = buildStorageBackend({
3737
Promise.resolve(DUMMY_ARRAY_BUFFER_DATA),
3838
});
3939

40-
export const VERSION_INFO = [
41-
{
42-
name: 'blob',
43-
chainId: '0x1',
44-
version: '1.0.0',
45-
checksum:
46-
'409a7f83ac6b31dc8c77e3ec18038f209bd2f545e0f4177c2e2381aa4e067b49',
47-
signature:
48-
'0x304402206d433e9172960de6717d94ae263e47eefacd3584a3274a452f8f9567b3a797db02201b2e423188fb3f9daa6ce6a8723f69df26bd3ceeee81f77250526b91e093614f',
49-
filePath: 'blob',
50-
},
51-
{
52-
name: 'data',
53-
chainId: '0x1',
54-
version: '1.0.3',
55-
checksum:
56-
'409a7f83ac6b31dc8c77e3ec18038f209bd2f545e0f4177c2e2381aa4e067b49',
57-
signature:
58-
'0x304402206d433e9172960de6717d94ae263e47eefacd3584a3274a452f8f9567b3a797db02201b2e423188fb3f9daa6ce6a8723f69df26bd3ceeee81f77250526b91e093614f',
59-
filePath: 'data',
60-
},
61-
];
40+
export const VERSION_INFO: (FileMetadata & {
41+
signature: string;
42+
filePath: string;
43+
})[] = [];
44+
45+
SUPPORTED_CHAIN_IDS.forEach((chainId) => {
46+
VERSION_INFO.push(
47+
{
48+
name: `${chainId}_blob`,
49+
chainId,
50+
version: '1.0.0',
51+
checksum:
52+
'409a7f83ac6b31dc8c77e3ec18038f209bd2f545e0f4177c2e2381aa4e067b49',
53+
signature:
54+
'0x304402206d433e9172960de6717d94ae263e47eefacd3584a3274a452f8f9567b3a797db02201b2e423188fb3f9daa6ce6a8723f69df26bd3ceeee81f77250526b91e093614f',
55+
filePath: `${chainId}_blob`,
56+
},
57+
{
58+
name: `${chainId}_blob`,
59+
chainId,
60+
version: '1.0.3',
61+
checksum:
62+
'409a7f83ac6b31dc8c77e3ec18038f209bd2f545e0f4177c2e2381aa4e067b49',
63+
signature:
64+
'0x304402206d433e9172960de6717d94ae263e47eefacd3584a3274a452f8f9567b3a797db02201b2e423188fb3f9daa6ce6a8723f69df26bd3ceeee81f77250526b91e093614f',
65+
filePath: `${chainId}_data`,
66+
},
67+
);
68+
});
6269

6370
const PPOM_VERSION_PATH = 'https://ppom_cdn_base_url/ppom_version.json';
6471

0 commit comments

Comments
 (0)