Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Aug 21, 2024
1 parent 09bcf58 commit 031019a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
59 changes: 59 additions & 0 deletions packages/snaps-controllers/src/snaps/SnapController.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4628,6 +4628,65 @@ describe('SnapController', () => {
snapController.destroy();
});

it('grants permitted chains permission to Snaps with `endowment:ethereum-provider`', async () => {
const rootMessenger = getControllerMessenger();
const messenger = getSnapControllerMessenger(rootMessenger);

rootMessenger.registerActionHandler(
'PermissionController:getPermissions',
() => ({}),
);

rootMessenger.registerActionHandler(
'SelectedNetworkController:getNetworkClientIdForDomain',
() => 'mainnet',
);

rootMessenger.registerActionHandler(
'NetworkController:getNetworkClientById',
() => ({
// @ts-expect-error - Partial network client.
configuration: {
chainId: '0x1',
},
}),
);

const { manifest } = await getMockSnapFilesWithUpdatedChecksum({
manifest: getSnapManifest({
initialPermissions: {
'endowment:page-home': {},
'endowment:ethereum-provider': {},
},
}),
});

const snapController = getSnapController(
getSnapControllerOptions({
messenger,
detectSnapLocation: loopbackDetect({ manifest }),
}),
);

await snapController.installSnaps(MOCK_ORIGIN, {
[MOCK_SNAP_ID]: {},
});

const approvedPermissions = {
'endowment:ethereum-provider': {
caveats: [],
},
permittedChains: {},
};

expect(messenger.call).toHaveBeenCalledWith(
'PermissionController:grantPermissions',
{ approvedPermissions, subject: { origin: MOCK_SNAP_ID } },
);

snapController.destroy();
});

it('supports preinstalled snaps', async () => {
const rootMessenger = getControllerMessenger();
jest.spyOn(rootMessenger, 'call');
Expand Down
2 changes: 2 additions & 0 deletions packages/snaps-controllers/src/snaps/SnapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3637,6 +3637,8 @@ export class SnapController extends BaseController<
*/
#getPermissionsToGrant(snapId: SnapId, newPermissions: RequestedPermissions) {
if (Object.keys(newPermissions).includes(SnapEndowments.EthereumProvider)) {
// This will return the globally selected network if the Snap doesn't have
// one set.
const networkClientId = this.messagingSystem.call(
'SelectedNetworkController:getNetworkClientIdForDomain',
snapId,
Expand Down
2 changes: 2 additions & 0 deletions packages/snaps-controllers/src/test-utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ export const getSnapControllerMessenger = (
'ExecutionService:terminateAllSnaps',
'ExecutionService:terminateSnap',
'ExecutionService:handleRpcRequest',
'NetworkController:getNetworkClientById',
'PermissionController:getEndowments',
'PermissionController:hasPermission',
'PermissionController:hasPermissions',
Expand All @@ -481,6 +482,7 @@ export const getSnapControllerMessenger = (
'PermissionController:getSubjectNames',
'PhishingController:maybeUpdateState',
'PhishingController:testOrigin',
'SelectedNetworkController:getNetworkClientIdForDomain',
'SnapController:get',
'SnapController:handleRequest',
'SnapController:getSnapState',
Expand Down

0 comments on commit 031019a

Please sign in to comment.