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
1 change: 0 additions & 1 deletion packages/permission-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@metamask/approval-controller": "workspace:^",
"@metamask/base-controller": "workspace:^",
"@metamask/controller-utils": "workspace:^",
"@metamask/types": "^1.1.0",
"@metamask/utils": "^5.0.2",
"@types/deep-freeze-strict": "^1.1.0",
"deep-freeze-strict": "^1.1.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-enable @typescript-eslint/no-unused-vars */
import { Mutable } from '@metamask/types';
import deepFreeze from 'deep-freeze-strict';
import { castDraft, Draft, Patch } from 'immer';
import { nanoid } from 'nanoid';
import { EthereumRpcError } from 'eth-rpc-errors';
import { hasProperty, Json } from '@metamask/utils';
import { hasProperty, Json, Mutable } from '@metamask/utils';
import {
AcceptRequest as AcceptApprovalRequest,
AddApprovalRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import type {
JsonRpcEngineEndCallback,
PendingJsonRpcResponse,
PermittedHandlerExport,
} from '@metamask/types';
import type { PendingJsonRpcResponse } from '@metamask/utils';
import type { JsonRpcEngineEndCallback } from 'json-rpc-engine';
import type { PermittedHandlerExport } from '../utils';
import { MethodNames } from '../utils';

import type { PermissionConstraint } from '../Permission';
import type { SubjectPermissions } from '../PermissionController';

export const getPermissionsHandler: PermittedHandlerExport<
GetPermissionsHooks,
void,
undefined,
PermissionConstraint[]
> = {
methodNames: [MethodNames.getPermissions],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { ethErrors } from 'eth-rpc-errors';
import type {
JsonRpcEngineEndCallback,
JsonRpcRequest,
PendingJsonRpcResponse,
PermittedHandlerExport,
} from '@metamask/types';
import type { JsonRpcEngineEndCallback } from 'json-rpc-engine';
import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
import { isPlainObject } from '@metamask/controller-utils';
import type { PermittedHandlerExport } from '../utils';
import { MethodNames } from '../utils';
import { invalidParams } from '../errors';
import type { PermissionConstraint, RequestedPermissions } from '../Permission';
Expand Down
48 changes: 48 additions & 0 deletions packages/permission-controller/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import type {
Json,
JsonRpcParams,
JsonRpcRequest,
PendingJsonRpcResponse,
} from '@metamask/utils';
import type {
JsonRpcEngineEndCallback,
JsonRpcEngineNextCallback,
} from 'json-rpc-engine';
import {
CaveatSpecificationConstraint,
CaveatSpecificationMap,
Expand Down Expand Up @@ -25,3 +35,41 @@ export type ExtractSpecifications<
| CaveatSpecificationMap<CaveatSpecificationConstraint>
| PermissionSpecificationMap<PermissionSpecificationConstraint>,
> = SpecificationsMap[keyof SpecificationsMap];

/**
* A middleware function for handling a permitted method.
*/
export type HandlerMiddlewareFunction<
T,
U extends JsonRpcParams,
V extends Json,
> = (
req: JsonRpcRequest<U>,
res: PendingJsonRpcResponse<V>,
next: JsonRpcEngineNextCallback,
end: JsonRpcEngineEndCallback,
hooks: T,
) => void | Promise<void>;

/**
* We use a mapped object type in order to create a type that requires the
* presence of the names of all hooks for the given handler.
* This can then be used to select only the necessary hooks whenever a method
* is called for purposes of POLA.
*/
export type HookNames<T> = {
[Property in keyof T]: true;
};

/**
* A handler for a permitted method.
*/
export type PermittedHandlerExport<
T,
U extends JsonRpcParams,
V extends Json,
> = {
implementation: HandlerMiddlewareFunction<T, U, V>;
hookNames: HookNames<T>;
methodNames: string[];
};
8 changes: 0 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1835,7 +1835,6 @@ __metadata:
"@metamask/auto-changelog": ^3.1.0
"@metamask/base-controller": "workspace:^"
"@metamask/controller-utils": "workspace:^"
"@metamask/types": ^1.1.0
"@metamask/utils": ^5.0.2
"@types/deep-freeze-strict": ^1.1.0
"@types/jest": ^27.4.1
Expand Down Expand Up @@ -1998,13 +1997,6 @@ __metadata:
languageName: unknown
linkType: soft

"@metamask/types@npm:^1.1.0":
version: 1.1.0
resolution: "@metamask/types@npm:1.1.0"
checksum: 500e8c076a2b0a6d688c8c465101256f090547d99c9a5585efe3d1db7a494b6b2712b127043fb316912caffc4fff78976b9a7780780abb68305e8a3bf812689c
languageName: node
linkType: hard

"@metamask/utils@npm:^3.0.1, @metamask/utils@npm:^3.0.3, @metamask/utils@npm:^3.4.1":
version: 3.6.0
resolution: "@metamask/utils@npm:3.6.0"
Expand Down