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
18 changes: 1 addition & 17 deletions eslint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -1106,24 +1106,13 @@
}
},
"packages/gator-permissions-controller/src/GatorPermissionsController.test.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 2
},
"id-denylist": {
"count": 2
},
"no-new": {
"count": 1
}
},
"packages/gator-permissions-controller/src/GatorPermissionsController.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 16
}
},
"packages/gator-permissions-controller/src/decodePermission/decodePermission.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 1
"count": 8
}
},
"packages/gator-permissions-controller/src/decodePermission/utils.test.ts": {
Expand All @@ -1142,11 +1131,6 @@
"count": 1
}
},
"packages/gator-permissions-controller/src/test/mocks.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 1
}
},
"packages/logging-controller/src/LoggingController.test.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 1
Expand Down
2 changes: 2 additions & 0 deletions packages/gator-permissions-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bump `@metamask/snaps-utils` from `^11.0.0` to `^11.7.0` ([#7550](https://github.com/MetaMask/core/pull/7550))
- Upgrade `@metamask/utils` from `^11.8.1` to `^11.9.0` ([#7511](https://github.com/MetaMask/core/pull/7511))
- Bump `@metamask/transaction-controller` from `^62.5.0` to `^62.9.1` ([#7430](https://github.com/MetaMask/core/pull/7430), [#7494](https://github.com/MetaMask/core/pull/7494), [#7596](https://github.com/MetaMask/core/pull/7596), [#7602](https://github.com/MetaMask/core/pull/7602), [#7604](https://github.com/MetaMask/core/pull/7604))
- **BREAKING:** Gator Permissions Controller and Gator Permission Decoder core types have been updated to comply with 7715 spec revisions ([#7613](https://github.com/MetaMask/core/pull/7613))
- Bump `@metamask/7715-permission-type` from `^0.4.0` to `^0.5.0`

## [0.8.0]

Expand Down
2 changes: 1 addition & 1 deletion packages/gator-permissions-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
},
"dependencies": {
"@metamask/7715-permission-types": "^0.4.0",
"@metamask/7715-permission-types": "^0.5.0",
"@metamask/base-controller": "^9.0.0",
"@metamask/delegation-core": "^0.2.0",
"@metamask/delegation-deployments": "^0.12.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { AccountSigner } from '@metamask/7715-permission-types';
import { deriveStateFromMetadata } from '@metamask/base-controller';
import {
createTimestampTerms,
Expand Down Expand Up @@ -45,45 +44,43 @@ const MOCK_CHAIN_ID_1: Hex = '0xaa36a7';
const MOCK_CHAIN_ID_2: Hex = '0x1';
const MOCK_GATOR_PERMISSIONS_PROVIDER_SNAP_ID =
'local:http://localhost:8082' as SnapId;
const MOCK_GATOR_PERMISSIONS_STORAGE_ENTRIES: StoredGatorPermission<
AccountSigner,
PermissionTypesWithCustom
>[] = mockGatorPermissionsStorageEntriesFactory({
[MOCK_CHAIN_ID_1]: {
nativeTokenStream: 5,
nativeTokenPeriodic: 5,
erc20TokenStream: 5,
erc20TokenPeriodic: 5,
custom: {
count: 2,
data: [
{
customData: 'customData-0',
},
{
customData: 'customData-1',
},
],
const MOCK_GATOR_PERMISSIONS_STORAGE_ENTRIES: StoredGatorPermission<PermissionTypesWithCustom>[] =
mockGatorPermissionsStorageEntriesFactory({
[MOCK_CHAIN_ID_1]: {
nativeTokenStream: 5,
nativeTokenPeriodic: 5,
erc20TokenStream: 5,
erc20TokenPeriodic: 5,
custom: {
count: 2,
data: [
{
customData: 'customData-0',
},
{
customData: 'customData-1',
},
],
},
},
},
[MOCK_CHAIN_ID_2]: {
nativeTokenStream: 5,
nativeTokenPeriodic: 5,
erc20TokenStream: 5,
erc20TokenPeriodic: 5,
custom: {
count: 2,
data: [
{
customData: 'customData-0',
},
{
customData: 'customData-1',
},
],
[MOCK_CHAIN_ID_2]: {
nativeTokenStream: 5,
nativeTokenPeriodic: 5,
erc20TokenStream: 5,
erc20TokenPeriodic: 5,
custom: {
count: 2,
data: [
{
customData: 'customData-0',
},
{
customData: 'customData-1',
},
],
},
},
},
});
});

describe('GatorPermissionsController', () => {
describe('constructor', () => {
Expand Down Expand Up @@ -246,13 +243,15 @@ describe('GatorPermissionsController', () => {
expect(controller.state.isFetchingGatorPermissions).toBe(false);

// check that the gator permissions map is sanitized
const sanitizedCheck = (permissionType: keyof GatorPermissionsMap) => {
const sanitizedCheck = (
permissionType: keyof GatorPermissionsMap,
): void => {
const flattenedStoredGatorPermissions = Object.values(
result[permissionType],
).flat();
flattenedStoredGatorPermissions.forEach((permission) => {
expect(permission.permissionResponse.signer).toBeUndefined();
expect(permission.permissionResponse.dependencyInfo).toBeUndefined();
expect(permission.permissionResponse.to).toBeUndefined();
expect(permission.permissionResponse.dependencies).toBeUndefined();
});
};

Expand Down Expand Up @@ -288,11 +287,8 @@ describe('GatorPermissionsController', () => {
const revocationEntry = {
permissionResponse: {
chainId,
address: '0x0000000000000000000000000000000000000001',
signer: {
type: 'account',
data: { address: '0x0000000000000000000000000000000000000002' },
},
from: '0x0000000000000000000000000000000000000001',
to: '0x0000000000000000000000000000000000000002',
permission: {
type: 'erc20-token-revocation',
isAdjustmentAllowed: false,
Expand All @@ -301,10 +297,8 @@ describe('GatorPermissionsController', () => {
data: {} as any,
},
context: '0xdeadbeef',
dependencyInfo: [],
signerMeta: {
delegationManager: '0x0000000000000000000000000000000000000003',
},
dependencies: [],
delegationManager: '0x0000000000000000000000000000000000000003',
},
siteOrigin: 'https://example.org',
} as unknown;
Expand Down Expand Up @@ -490,10 +484,11 @@ describe('GatorPermissionsController', () => {
'registerActionHandler',
);

new GatorPermissionsController({
const controller = new GatorPermissionsController({
messenger,
});

expect(controller.state.isGatorPermissionsEnabled).toBe(false);
expect(mockRegisterActionHandler).toHaveBeenCalledWith(
'GatorPermissionsController:fetchAndUpdateGatorPermissions',
expect.any(Function),
Expand Down Expand Up @@ -607,7 +602,9 @@ describe('GatorPermissionsController', () => {
const delegateAddressB =
'0x2222222222222222222222222222222222222222' as Hex;
const metamaskOrigin = 'https://metamask.io';
const buildMetadata = (justification: string) => ({
const buildMetadata = (
justification: string,
): { justification: string; origin: string } => ({
justification,
origin: metamaskOrigin,
});
Expand Down Expand Up @@ -692,11 +689,8 @@ describe('GatorPermissionsController', () => {
});

expect(result.chainId).toBe(numberToHex(chainId));
expect(result.address).toBe(delegator);
expect(result.signer).toStrictEqual({
type: 'account',
data: { address: delegate },
});
expect(result.from).toBe(delegator);
expect(result.to).toStrictEqual(delegate);
expect(result.permission.type).toBe('native-token-stream');
expect(result.expiry).toBe(timestampBeforeThreshold);
// amounts are hex-encoded in decoded data; startTime is numeric
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Signer } from '@metamask/7715-permission-types';
import type {
ControllerGetStateAction,
ControllerStateChangeEvent,
Expand Down Expand Up @@ -337,19 +336,19 @@ export default class GatorPermissionsController extends BaseController<
this.#registerMessageHandlers();
}

#setIsFetchingGatorPermissions(isFetchingGatorPermissions: boolean) {
#setIsFetchingGatorPermissions(isFetchingGatorPermissions: boolean): void {
this.update((state) => {
state.isFetchingGatorPermissions = isFetchingGatorPermissions;
});
}

#setIsGatorPermissionsEnabled(isGatorPermissionsEnabled: boolean) {
#setIsGatorPermissionsEnabled(isGatorPermissionsEnabled: boolean): void {
this.update((state) => {
state.isGatorPermissionsEnabled = isGatorPermissionsEnabled;
});
}

#addPendingRevocationToState(txId: string, permissionContext: Hex) {
#addPendingRevocationToState(txId: string, permissionContext: Hex): void {
this.update((state) => {
state.pendingRevocations = [
...state.pendingRevocations,
Expand All @@ -358,15 +357,17 @@ export default class GatorPermissionsController extends BaseController<
});
}

#removePendingRevocationFromStateByTxId(txId: string) {
#removePendingRevocationFromStateByTxId(txId: string): void {
this.update((state) => {
state.pendingRevocations = state.pendingRevocations.filter(
(pendingRevocations) => pendingRevocations.txId !== txId,
);
});
}

#removePendingRevocationFromStateByPermissionContext(permissionContext: Hex) {
#removePendingRevocationFromStateByPermissionContext(
permissionContext: Hex,
): void {
this.update((state) => {
state.pendingRevocations = state.pendingRevocations.filter(
(pendingRevocations) =>
Expand Down Expand Up @@ -425,7 +426,7 @@ export default class GatorPermissionsController extends BaseController<
*
* @throws {GatorPermissionsNotEnabledError} If the gator permissions are not enabled.
*/
#assertGatorPermissionsEnabled() {
#assertGatorPermissionsEnabled(): void {
if (!this.state.isGatorPermissionsEnabled) {
throw new GatorPermissionsNotEnabledError();
}
Expand All @@ -445,9 +446,7 @@ export default class GatorPermissionsController extends BaseController<
}: {
snapId: SnapId;
params?: Json;
}): Promise<
StoredGatorPermission<Signer, PermissionTypesWithCustom>[] | null
> {
}): Promise<StoredGatorPermission<PermissionTypesWithCustom>[] | null> {
try {
const response = (await this.messenger.call(
'SnapController:handleRequest',
Expand All @@ -462,7 +461,7 @@ export default class GatorPermissionsController extends BaseController<
...(params !== undefined && { params }),
},
},
)) as StoredGatorPermission<Signer, PermissionTypesWithCustom>[] | null;
)) as StoredGatorPermission<PermissionTypesWithCustom>[] | null;

return response;
} catch (error) {
Expand All @@ -480,19 +479,16 @@ export default class GatorPermissionsController extends BaseController<

/**
* Sanitizes a stored gator permission for client exposure.
* Removes internal fields (dependencyInfo, signer)
* Removes internal fields (dependencies, to)
*
* @param storedGatorPermission - The stored gator permission to sanitize.
* @returns The sanitized stored gator permission.
*/
#sanitizeStoredGatorPermission(
storedGatorPermission: StoredGatorPermission<
Signer,
PermissionTypesWithCustom
>,
): StoredGatorPermissionSanitized<Signer, PermissionTypesWithCustom> {
storedGatorPermission: StoredGatorPermission<PermissionTypesWithCustom>,
): StoredGatorPermissionSanitized<PermissionTypesWithCustom> {
const { permissionResponse } = storedGatorPermission;
const { dependencyInfo, signer, ...rest } = permissionResponse;
const { dependencies, to, ...rest } = permissionResponse;
return {
...storedGatorPermission,
permissionResponse: {
Expand All @@ -509,7 +505,7 @@ export default class GatorPermissionsController extends BaseController<
*/
#categorizePermissionsDataByTypeAndChainId(
storedGatorPermissions:
| StoredGatorPermission<Signer, PermissionTypesWithCustom>[]
| StoredGatorPermission<PermissionTypesWithCustom>[]
| null,
): GatorPermissionsMap {
const gatorPermissionsMap = createEmptyGatorPermissionsMap();
Expand Down Expand Up @@ -570,14 +566,14 @@ export default class GatorPermissionsController extends BaseController<
/**
* Enables gator permissions for the user.
*/
public async enableGatorPermissions() {
public async enableGatorPermissions(): Promise<void> {
this.#setIsGatorPermissionsEnabled(true);
}

/**
* Clears the gator permissions map and disables the feature.
*/
public async disableGatorPermissions() {
public async disableGatorPermissions(): Promise<void> {
this.update((state) => {
state.isGatorPermissionsEnabled = false;
state.gatorPermissionsMapSerialized = serializeGatorPermissionsMap(
Expand Down Expand Up @@ -641,7 +637,7 @@ export default class GatorPermissionsController extends BaseController<
* This method validates the caller origin, decodes the provided `permissionContext`
* into delegations, identifies the permission type from the caveat enforcers,
* extracts the permission-specific data and expiry, and reconstructs a
* {@link DecodedPermission} containing chainId, account addresses, signer, type and data.
* {@link DecodedPermission} containing chainId, account addresses, to, type and data.
*
* @param args - The arguments to this function.
* @param args.origin - The caller's origin; must match the configured permissions provider Snap id.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1204,11 +1204,8 @@ describe('decodePermission', () => {
});

expect(result.chainId).toBe(numberToHex(chainId));
expect(result.address).toBe(delegator);
expect(result.signer).toStrictEqual({
type: 'account',
data: { address: delegate },
});
expect(result.from).toBe(delegator);
expect(result.to).toStrictEqual(delegate);
expect(result.permission).toStrictEqual({
type: permissionType,
data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,15 @@ export const reconstructDecodedPermission = ({
data: DecodedPermission['permission']['data'];
justification: string;
specifiedOrigin: string;
}) => {
}): DecodedPermission => {
if (authority !== ROOT_AUTHORITY) {
throw new Error('Invalid authority');
}

const permission: DecodedPermission = {
chainId: numberToHex(chainId),
address: delegator,
signer: { type: 'account', data: { address: delegate } },
from: delegator,
to: delegate,
permission: {
type: permissionType,
data,
Expand Down
Loading
Loading