Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@metamask/base-controller": "^3.0.0",
"@metamask/controller-utils": "^4.0.0",
"@metamask/base-controller": "^3.2.1",
"@metamask/controller-utils": "^5.0.0",
"@metamask/network-controller": "^15.2.0",
Comment on lines +34 to +36

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Not strictly necessary but all of these have newer versions available now.

"await-semaphore": "^0.1.3",
"elliptic": "^6.5.4",
"json-rpc-random-id": "^1.0.1"
Expand Down
128 changes: 113 additions & 15 deletions src/ppom-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,14 @@ describe('PPOMController', () => {
await ppomController.usePPOM(async () => {
return Promise.resolve();
});
callBack({ providerConfig: { chainId: '0x2' } });
callBack({ providerConfig: { chainId: '0x1' } });
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x2' },
});
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x1' },
});
buildFetchSpy(
undefined,
{
Expand All @@ -368,6 +374,62 @@ describe('PPOMController', () => {
'Aborting validation as not all files could not be downloaded for the network with chainId: 0x1',
);
});

it('should pass instance of provider to ppom from the network registry if networkClientId is provided', async () => {
buildFetchSpy();
ppomController = buildPPOMController({
provider: {
sendAsync: (_arg1: any, arg2: any) => {
arg2(undefined, 'DUMMY_VALUE_FROM_PROVIDER_PROXY');
},
},
});
jest.spyOn(ppomController.messagingSystem, 'call').mockReturnValue({
configuration: {
chainId: '0x1',
},
provider: {
sendAsync: (_arg1: any, arg2: any) => {
arg2(undefined, 'DUMMY_VALUE_FROM_NETWORK_REGISTRY');
},
},
});
jest.runOnlyPendingTimers();
await flushPromises();

await ppomController.usePPOM(async (ppom: any) => {
const result = await ppom.testJsonRPCRequest();
expect(result).toBe('DUMMY_VALUE_FROM_NETWORK_REGISTRY');
}, 'networkClientId1');
expect(ppomController.messagingSystem.call).toHaveBeenCalledWith(
'NetworkController:getNetworkClientById',
'networkClientId1',
);
});

it('should use chain ID from the network registry if networkClientId is provided', async () => {
buildFetchSpy();
ppomController = buildPPOMController();
jest.spyOn(ppomController.messagingSystem, 'call').mockReturnValue({
configuration: {
chainId: '0x5',
},
});
jest.runOnlyPendingTimers();
await flushPromises();

await expect(async () => {
await ppomController.usePPOM(async () => {
return Promise.resolve();
}, 'networkClientId1');
}).rejects.toThrow(
'Blockaid validation is available only on ethereum mainnet',
);
expect(ppomController.messagingSystem.call).toHaveBeenCalledWith(
'NetworkController:getNetworkClientById',
'networkClientId1',
);
});
});

describe('updatePPOM', () => {
Expand Down Expand Up @@ -434,7 +496,10 @@ describe('PPOMController', () => {
},
});
jest.runOnlyPendingTimers();
callBack({ providerConfig: { chainId: '0x2' } });
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x2' },
});
await ppomController.updatePPOM();
jest.runOnlyPendingTimers();
await flushPromises();
Expand Down Expand Up @@ -468,7 +533,10 @@ describe('PPOMController', () => {
},
});
jest.runOnlyPendingTimers();
callBack({ providerConfig: { chainId: '0x2' } });
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x2' },
});
expect(Object.keys(ppomController.state.chainStatus)).toHaveLength(2);
await ppomController.updatePPOM();
jest.runOnlyPendingTimers();
Expand Down Expand Up @@ -520,8 +588,14 @@ describe('PPOMController', () => {
await flushPromises();
const chainIdData1 = ppomController.state.chainStatus['0x1'];
expect(chainIdData1).toBeDefined();
callBack({ providerConfig: { chainId: '0x2' } });
callBack({ providerConfig: { chainId: '0x3' } });
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x2' },
});
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x3' },
});
jest.advanceTimersByTime(NETWORK_CACHE_DURATION);
jest.runOnlyPendingTimers();
await flushPromises();
Expand All @@ -548,7 +622,7 @@ describe('PPOMController', () => {
});

describe('onNetworkChange', () => {
it('should add network to chainStatus if not already added', () => {
it('should add current network to chainStatus if not already added', () => {
buildFetchSpy();
let callBack: any;
ppomController = buildPPOMController({
Expand All @@ -559,7 +633,10 @@ describe('PPOMController', () => {

const chainIdData1 = ppomController.state.chainStatus['0x1'];
expect(chainIdData1).toBeDefined();
callBack({ providerConfig: { chainId: '0x2' } });
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x2' },
});
const chainIdData2 = ppomController.state.chainStatus['0x2'];
expect(chainIdData2).toBeDefined();
});
Expand All @@ -579,8 +656,14 @@ describe('PPOMController', () => {

jest.useFakeTimers().setSystemTime(new Date('2023-01-02'));

callBack({ providerConfig: { chainId: '0x2' } });
callBack({ providerConfig: { chainId: '0x1' } });
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x2' },
});
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x1' },
});
const lastVisitedAfter =
ppomController.state.chainStatus['0x1'].lastVisited;
expect(lastVisitedBefore !== lastVisitedAfter).toBe(true);
Expand All @@ -599,21 +682,36 @@ describe('PPOMController', () => {
expect(Object.keys(ppomController.state.chainStatus)).toHaveLength(1);

jest.useFakeTimers().setSystemTime(new Date('2023-01-02'));
callBack({ providerConfig: { chainId: '0x2' } });
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x2' },
});

jest.useFakeTimers().setSystemTime(new Date('2023-01-05'));
callBack({ providerConfig: { chainId: '0x5' } });
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x5' },
});

jest.useFakeTimers().setSystemTime(new Date('2023-01-03'));
callBack({ providerConfig: { chainId: '0x3' } });
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x3' },
});

jest.useFakeTimers().setSystemTime(new Date('2023-01-04'));
callBack({ providerConfig: { chainId: '0x4' } });
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x4' },
});

expect(Object.keys(ppomController.state.chainStatus)).toHaveLength(5);

jest.useFakeTimers().setSystemTime(new Date('2023-01-06'));
callBack({ providerConfig: { chainId: '0x6' } });
callBack({
networkConfigurations: {},
providerConfig: { chainId: '0x6' },
});
expect(Object.keys(ppomController.state.chainStatus)).toHaveLength(5);

expect(ppomController.state.chainStatus['0x1']).toBeUndefined();
Expand Down
Loading