Skip to content

Commit

Permalink
Filter removed permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeRx committed Nov 4, 2024
1 parent 59f242f commit a7e496e
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions packages/snaps-rpc-methods/src/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ import {
} from './restricted';
import { selectHooks } from './utils';

const REMOVED_PERMISSIONS = Object.freeze(['snap_manageAccounts']);

/**
* Filters out permissions that have been removed from the Snap API.
*
* @param initialPermission - The initial permission to filter.
* @returns Whether the permission has been removed.
*/
export const filterRemovedPermissions = (
initialPermission: [string, unknown],
) => {
const [value] = initialPermission;
return REMOVED_PERMISSIONS.some((permission) => permission === value);
};

/**
* Map initial permissions as defined in a Snap manifest to something that can
* be processed by the PermissionsController. Each caveat mapping function
Expand All @@ -30,22 +45,32 @@ export function processSnapPermissions(
initialPermissions: SnapPermissions,
): Record<string, Pick<PermissionConstraint, 'caveats'>> {
return Object.fromEntries(
Object.entries(initialPermissions).map(([initialPermission, value]) => {
if (hasProperty(caveatMappers, initialPermission)) {
return [initialPermission, caveatMappers[initialPermission](value)];
} else if (hasProperty(endowmentCaveatMappers, initialPermission)) {
Object.entries(initialPermissions)
.filter(filterRemovedPermissions)
.map(([initialPermission, value]) => {
if (
REMOVED_PERMISSIONS.some(
(permission) => permission === initialPermission,
)
) {
return [];
}

if (hasProperty(caveatMappers, initialPermission)) {
return [initialPermission, caveatMappers[initialPermission](value)];
} else if (hasProperty(endowmentCaveatMappers, initialPermission)) {
return [
initialPermission,
endowmentCaveatMappers[initialPermission](value),
];
}

// If we have no mapping, this may be a non-snap permission, return as-is
return [
initialPermission,
endowmentCaveatMappers[initialPermission](value),
value as Pick<PermissionConstraint, 'caveats'>,
];
}

// If we have no mapping, this may be a non-snap permission, return as-is
return [
initialPermission,
value as Pick<PermissionConstraint, 'caveats'>,
];
}),
}),
);
}

Expand Down

0 comments on commit a7e496e

Please sign in to comment.