-
Notifications
You must be signed in to change notification settings - Fork 5.6k
feat: Gator permissions controller integration #35627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
V00D00-child
merged 31 commits into
main
from
feat/gator-permissions-controller-integration
Sep 11, 2025
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
7f406f0
Add gator permissions controller to extension
V00D00-child 114ffef
bugfix: GatorPermissionsController Initialization Error
V00D00-child e5d632f
Add GatorPermissionsController to CONTROLLER_MESSENGERS
V00D00-child 2379b7f
Merge branch 'main' into feat/gator-permissions-controller-integration
V00D00-child 5b2e114
Drop npm preview of gator-permissions-controller for initial publishe…
V00D00-child 5230e87
Merge branch 'main' into feat/gator-permissions-controller-integration
V00D00-child 77b3d8f
Drop manual policies update to allow metamaskbot to update policies
V00D00-child 0770130
Update LavaMoat policies
metamaskbot 07ae174
Bugfix: Make GatorPermissionsController API Methods Available in all …
V00D00-child 9bf3f9a
Merge branch 'main' into feat/gator-permissions-controller-integration
V00D00-child 3ac4548
Resolve lint errors due to different BaseController versions
V00D00-child 92ac2f4
Fix lockfile dedupe
V00D00-child e567aef
Merge branch 'main' into feat/gator-permissions-controller-integration
V00D00-child a61d905
Update LavaMoat policies
metamaskbot 6068da5
Merge branch 'main' into feat/gator-permissions-controller-integration
V00D00-child fc0e1e1
Add GatorPermissionsController to e2e state-snapshot
V00D00-child a8381cb
Add GatorPermissionsController state props to metamask e2e state
V00D00-child ad5b560
Fix typo in test
V00D00-child 9312634
Fix lint errors and only include GatorPermissionsController in state-…
V00D00-child 6983ff6
Use GATOR_PERMISSIONS_PROVIDER_SNAP_ID build var. Remove enableGatorP…
V00D00-child eebfa3b
Merge branch 'main' into feat/gator-permissions-controller-integration
V00D00-child d9f98ba
bugfix: Invalid SnapId Environment Variable Causes Errors
V00D00-child 3de87bc
Ensure gatorPermissionsProviderSnapId is set undefined in e2e test
V00D00-child 0468526
Remove gatorPermissionsProviderSnapId from e2e test to allow it to po…
V00D00-child dc5f231
Only valid snapId is set otherwise we rely on default value set in co…
V00D00-child ad7fa04
Throws error when snapId is invalid
V00D00-child 3f1d820
Merge branch 'main' into feat/gator-permissions-controller-integration
V00D00-child c5012cd
Use empty string for GATOR_PERMISSIONS_PROVIDER_SNAP_ID
V00D00-child a891f16
Remove throwing error
V00D00-child 9ea607a
Reject invalid GATOR_PERMISSIONS_PROVIDER_SNAP_ID configuration (#35775)
jeffsmale90 42e94bd
Merge branch 'main' into feat/gator-permissions-controller-integration
jeffsmale90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
app/scripts/controller-init/gator-permissions/gator-permissions-controller-init.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| import { GatorPermissionsController } from '@metamask/gator-permissions-controller'; | ||
| import { Messenger } from '@metamask/base-controller'; | ||
| import { buildControllerInitRequestMock } from '../test/utils'; | ||
| import type { ControllerInitRequest } from '../types'; | ||
| import { isGatorPermissionsFeatureEnabled } from '../../../../shared/modules/environment'; | ||
| import { | ||
| getGatorPermissionsControllerMessenger, | ||
| GatorPermissionsControllerMessenger, | ||
| } from '../messengers/gator-permissions'; | ||
| import { GatorPermissionsControllerInit } from './gator-permissions-controller-init'; | ||
|
|
||
| jest.mock('@metamask/gator-permissions-controller'); | ||
| jest.mock('../../../../shared/modules/environment'); | ||
|
|
||
| function buildInitRequestMock(): jest.Mocked< | ||
| ControllerInitRequest<GatorPermissionsControllerMessenger> | ||
| > { | ||
| const baseControllerMessenger = new Messenger(); | ||
|
|
||
| return { | ||
| ...buildControllerInitRequestMock(), | ||
| controllerMessenger: getGatorPermissionsControllerMessenger( | ||
| baseControllerMessenger, | ||
| ), | ||
| initMessenger: undefined, | ||
| }; | ||
| } | ||
|
|
||
| describe('GatorPermissionsControllerInit', () => { | ||
| const MOCK_GATOR_PERMISSIONS_PROVIDER_SNAP_ID = 'npm:mock-snap-id'; | ||
| const GatorPermissionsControllerClassMock = jest.mocked( | ||
| GatorPermissionsController, | ||
| ); | ||
| const originalGatorPermissionProviderSnapId = | ||
| process.env.GATOR_PERMISSIONS_PROVIDER_SNAP_ID; | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetAllMocks(); | ||
| jest.mocked(isGatorPermissionsFeatureEnabled).mockReturnValue(true); | ||
| process.env.GATOR_PERMISSIONS_PROVIDER_SNAP_ID = | ||
| MOCK_GATOR_PERMISSIONS_PROVIDER_SNAP_ID; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (originalGatorPermissionProviderSnapId) { | ||
| process.env.GATOR_PERMISSIONS_PROVIDER_SNAP_ID = | ||
| originalGatorPermissionProviderSnapId; | ||
| } else { | ||
| delete process.env.GATOR_PERMISSIONS_PROVIDER_SNAP_ID; | ||
| } | ||
| }); | ||
|
|
||
| it('returns controller instance', () => { | ||
| const requestMock = buildInitRequestMock(); | ||
| expect( | ||
| GatorPermissionsControllerInit(requestMock).controller, | ||
| ).toBeInstanceOf(GatorPermissionsController); | ||
| }); | ||
|
|
||
| it('initializes with correct messenger and state(gator permissions feature enabled)', () => { | ||
| const requestMock = buildInitRequestMock(); | ||
| jest.mocked(isGatorPermissionsFeatureEnabled).mockReturnValue(true); | ||
| GatorPermissionsControllerInit(requestMock); | ||
|
|
||
| expect(GatorPermissionsControllerClassMock).toHaveBeenCalledWith({ | ||
| messenger: requestMock.controllerMessenger, | ||
| state: { | ||
| isGatorPermissionsEnabled: true, | ||
| gatorPermissionsProviderSnapId: MOCK_GATOR_PERMISSIONS_PROVIDER_SNAP_ID, | ||
| ...requestMock.persistedState.GatorPermissionsController, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('initializes with correct messenger and state(gator permissions feature disabled)', () => { | ||
| const requestMock = buildInitRequestMock(); | ||
| jest.mocked(isGatorPermissionsFeatureEnabled).mockReturnValue(false); | ||
| GatorPermissionsControllerInit(requestMock); | ||
|
|
||
| expect(GatorPermissionsControllerClassMock).toHaveBeenCalledWith({ | ||
| messenger: requestMock.controllerMessenger, | ||
| state: { | ||
| isGatorPermissionsEnabled: false, | ||
| gatorPermissionsProviderSnapId: MOCK_GATOR_PERMISSIONS_PROVIDER_SNAP_ID, | ||
| ...requestMock.persistedState.GatorPermissionsController, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('returns correct API methods', () => { | ||
| const requestMock = buildInitRequestMock(); | ||
| const result = GatorPermissionsControllerInit(requestMock); | ||
|
|
||
| expect(result.api).toEqual({ | ||
| fetchAndUpdateGatorPermissions: expect.any(Function), | ||
| }); | ||
| }); | ||
|
|
||
| it('handles undefined persistedState.GatorPermissionsController', () => { | ||
| const requestMock = buildInitRequestMock(); | ||
| requestMock.persistedState.GatorPermissionsController = undefined; | ||
| jest.mocked(isGatorPermissionsFeatureEnabled).mockReturnValue(true); | ||
|
|
||
| GatorPermissionsControllerInit(requestMock); | ||
|
|
||
| expect(GatorPermissionsControllerClassMock).toHaveBeenCalledWith({ | ||
| messenger: requestMock.controllerMessenger, | ||
| state: { | ||
| isGatorPermissionsEnabled: true, | ||
| gatorPermissionsProviderSnapId: MOCK_GATOR_PERMISSIONS_PROVIDER_SNAP_ID, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('resolves the default when GATOR_PERMISSIONS_PROVIDER_SNAP_ID is not specified', () => { | ||
| const requestMock = buildInitRequestMock(); | ||
|
|
||
| delete process.env.GATOR_PERMISSIONS_PROVIDER_SNAP_ID; | ||
|
|
||
| GatorPermissionsControllerInit(requestMock); | ||
|
|
||
| expect(GatorPermissionsControllerClassMock).toHaveBeenCalledWith({ | ||
| messenger: requestMock.controllerMessenger, | ||
| state: { | ||
| isGatorPermissionsEnabled: true, | ||
| }, | ||
| }); | ||
|
|
||
| const calledWithState = | ||
| GatorPermissionsControllerClassMock.mock.calls[0][0].state; | ||
|
|
||
| expect(calledWithState).toEqual({ | ||
| isGatorPermissionsEnabled: true, | ||
| }); | ||
|
|
||
| // GatorPermissionsController requires that the key does not exist if the snap id is not specified | ||
| expect( | ||
| Object.prototype.hasOwnProperty.call( | ||
| calledWithState, | ||
| 'gatorPermissionsProviderSnapId', | ||
| ), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| describe('GATOR_PERMISSIONS_PROVIDER_SNAP_ID incorrectly specified', () => { | ||
| ['', ' ', 'invalid-snap-id'].forEach((invalidSnapId) => { | ||
| it(`throws when provided invalid GATOR_PERMISSIONS_PROVIDER_SNAP_ID: ${invalidSnapId}`, () => { | ||
| const requestMock = buildInitRequestMock(); | ||
|
|
||
| process.env.GATOR_PERMISSIONS_PROVIDER_SNAP_ID = | ||
| invalidSnapId as string; | ||
|
|
||
| expect(() => GatorPermissionsControllerInit(requestMock)).toThrow( | ||
| 'GATOR_PERMISSIONS_PROVIDER_SNAP_ID must be set to a valid snap id', | ||
| ); | ||
|
|
||
| expect(GatorPermissionsControllerClassMock).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
64 changes: 64 additions & 0 deletions
64
app/scripts/controller-init/gator-permissions/gator-permissions-controller-init.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { | ||
| GatorPermissionsController, | ||
| GatorPermissionsControllerState, | ||
| } from '@metamask/gator-permissions-controller'; | ||
| import { assertIsValidSnapId } from '@metamask/snaps-utils'; | ||
| import { ControllerInitFunction } from '../types'; | ||
| import { isGatorPermissionsFeatureEnabled } from '../../../../shared/modules/environment'; | ||
| import { GatorPermissionsControllerMessenger } from '../messengers/gator-permissions'; | ||
|
|
||
| const generateDefaultGatorPermissionsControllerState = | ||
| (): Partial<GatorPermissionsControllerState> => { | ||
| const gatorPermissionsProviderSnapId = | ||
| process.env.GATOR_PERMISSIONS_PROVIDER_SNAP_ID; | ||
|
|
||
| // if GATOR_PERMISSIONS_PROVIDER_SNAP_ID is not specified, GatorPermissionsController will initialize it's default | ||
| if (gatorPermissionsProviderSnapId !== undefined) { | ||
| try { | ||
| assertIsValidSnapId(gatorPermissionsProviderSnapId); | ||
| } catch (error) { | ||
| throw new Error( | ||
| 'GATOR_PERMISSIONS_PROVIDER_SNAP_ID must be set to a valid snap id', | ||
| { | ||
| cause: error, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const isGatorPermissionsEnabled = isGatorPermissionsFeatureEnabled(); | ||
|
|
||
| const state: Partial<GatorPermissionsControllerState> = { | ||
| isGatorPermissionsEnabled, | ||
| }; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| if (gatorPermissionsProviderSnapId) { | ||
| state.gatorPermissionsProviderSnapId = gatorPermissionsProviderSnapId; | ||
| } | ||
|
|
||
| return state; | ||
| }; | ||
|
|
||
| export const GatorPermissionsControllerInit: ControllerInitFunction< | ||
| GatorPermissionsController, | ||
| GatorPermissionsControllerMessenger | ||
| > = ({ controllerMessenger, persistedState }) => { | ||
| const controller = new GatorPermissionsController({ | ||
| // Type mismatch due to different BaseController versions, GatorPermissionsController uses 8.3.0 while extension uses 8.2.0. | ||
| // We can remove once extension BaseController version is updated to 8.3.0. | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| messenger: controllerMessenger as any, | ||
| state: { | ||
| ...generateDefaultGatorPermissionsControllerState(), | ||
| ...persistedState.GatorPermissionsController, | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| controller, | ||
| api: { | ||
| fetchAndUpdateGatorPermissions: | ||
| controller.fetchAndUpdateGatorPermissions.bind(controller), | ||
| }, | ||
| }; | ||
| }; | ||
14 changes: 14 additions & 0 deletions
14
...ntroller-init/messengers/gator-permissions/gator-permissions-controller-messenger.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { Messenger, RestrictedMessenger } from '@metamask/base-controller'; | ||
| import { getGatorPermissionsControllerMessenger } from './gator-permissions-controller-messenger'; | ||
|
|
||
| describe('getGatorPermissionsControllerMessenger', () => { | ||
| it('returns a restricted messenger', () => { | ||
| const messenger = new Messenger<never, never>(); | ||
| const gatorPermissionsControllerMessenger = | ||
| getGatorPermissionsControllerMessenger(messenger); | ||
|
|
||
| expect(gatorPermissionsControllerMessenger).toBeInstanceOf( | ||
| RestrictedMessenger, | ||
| ); | ||
| }); | ||
| }); |
27 changes: 27 additions & 0 deletions
27
...ts/controller-init/messengers/gator-permissions/gator-permissions-controller-messenger.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { Messenger } from '@metamask/base-controller'; | ||
| import { GatorPermissionsControllerStateChangeEvent } from '@metamask/gator-permissions-controller'; | ||
| import { HandleSnapRequest, HasSnap } from '@metamask/snaps-controllers'; | ||
|
|
||
| export type GatorPermissionsControllerMessenger = ReturnType< | ||
| typeof getGatorPermissionsControllerMessenger | ||
| >; | ||
|
|
||
| type MessengerActions = HandleSnapRequest | HasSnap; | ||
| type MessengerEvents = GatorPermissionsControllerStateChangeEvent; | ||
|
|
||
| /** | ||
| * Get a restricted messenger for the Gator Permissions controller. This is scoped to the | ||
| * actions and events that the Gator Permissions controller is allowed to handle. | ||
| * | ||
| * @param messenger - The messenger to restrict. | ||
| * @returns The restricted messenger. | ||
| */ | ||
| export function getGatorPermissionsControllerMessenger( | ||
| messenger: Messenger<MessengerActions, MessengerEvents>, | ||
| ) { | ||
| return messenger.getRestricted({ | ||
| name: 'GatorPermissionsController', | ||
| allowedActions: ['SnapController:handleRequest', 'SnapController:has'], | ||
| allowedEvents: [], | ||
| }); | ||
| } |
2 changes: 2 additions & 0 deletions
2
app/scripts/controller-init/messengers/gator-permissions/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { getGatorPermissionsControllerMessenger } from './gator-permissions-controller-messenger'; | ||
| export type { GatorPermissionsControllerMessenger } from './gator-permissions-controller-messenger'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.