Skip to content
Merged
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
61 changes: 61 additions & 0 deletions packages/permission-controller/src/PermissionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4654,6 +4654,67 @@ describe('PermissionController', () => {

expect(requestPermissionsSpy).toHaveBeenCalledTimes(1);
});

it('action: PermissionController:updateCaveat', async () => {
const messenger = getUnrestrictedMessenger();
const state = {
subjects: {
'metamask.io': {
origin: 'metamask.io',
permissions: {
wallet_getSecretArray: {
id: 'escwEx9JrOxGZKZk3RkL4',
parentCapability: 'wallet_getSecretArray',
invoker: 'metamask.io',
caveats: [
{ type: CaveatTypes.filterArrayResponse, value: ['bar'] },
],
date: 1632618373085,
},
},
},
},
};
const options = getPermissionControllerOptions({
messenger: getPermissionControllerMessenger(messenger),
state,
});

const controller = new PermissionController<
DefaultPermissionSpecifications,
DefaultCaveatSpecifications
>(options);

const updateCaveatSpy = jest.spyOn(controller, 'updateCaveat');

await messenger.call(
Comment thread
hmalik88 marked this conversation as resolved.
'PermissionController:updateCaveat',
'metamask.io',
'wallet_getSecretArray',
CaveatTypes.filterArrayResponse,
['baz'],
);

expect(updateCaveatSpy).toHaveBeenCalledTimes(1);
expect(controller.state).toStrictEqual({
subjects: {
'metamask.io': {
origin: 'metamask.io',
permissions: {
wallet_getSecretArray: {
id: 'escwEx9JrOxGZKZk3RkL4',
parentCapability: 'wallet_getSecretArray',
invoker: 'metamask.io',
caveats: [
{ type: CaveatTypes.filterArrayResponse, value: ['baz'] },
],
date: 1632618373085,
},
},
},
},
});
});
});

describe('permission middleware', () => {
Expand Down
23 changes: 22 additions & 1 deletion packages/permission-controller/src/PermissionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ export type RevokePermissionForAllSubjects = {
handler: GenericPermissionController['revokePermissionForAllSubjects'];
};

/**
* Updates a caveat value for a specified caveat type belonging to a specific target and origin.
*/
export type UpdateCaveat = {
type: `${typeof controllerName}:updateCaveat`;
handler: GenericPermissionController['updateCaveat'];
};

/**
* Clears all permissions from the {@link PermissionController}.
*/
Expand Down Expand Up @@ -289,7 +297,8 @@ export type PermissionControllerActions =
| RequestPermissions
| RevokeAllPermissions
| RevokePermissionForAllSubjects
| RevokePermissions;
| RevokePermissions
| UpdateCaveat;

/**
* The generic state change event of the {@link PermissionController}.
Expand Down Expand Up @@ -755,6 +764,18 @@ export class PermissionController<
`${controllerName}:revokePermissions` as const,
this.revokePermissions.bind(this),
);

this.messagingSystem.registerActionHandler(
`${controllerName}:updateCaveat` as const,
(origin, target, caveatType, caveatValue) => {
this.updateCaveat(
Comment thread
hmalik88 marked this conversation as resolved.
origin,
target,
caveatType as ExtractAllowedCaveatTypes<ControllerPermissionSpecification>,
Comment thread
hmalik88 marked this conversation as resolved.
caveatValue,
);
},
);
}

/**
Expand Down