Skip to content

Commit 6f14cea

Browse files
legobeatMajorLift
authored andcommitted
deps(permission-controller): update to @metamask/json-rpc-engine, @metamask/rpc-errors (#1749)
1 parent 9ad9a28 commit 6f14cea

15 files changed

+150
-72
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
diff --git a/dist/types/restricted/getLocale.d.ts b/dist/types/restricted/getLocale.d.ts
2+
index 2941d2733042664c341776c7bc840ba0813994ca..0188bbd4de0cd013159a36b736ad9baf94c18c92 100644
3+
--- a/dist/types/restricted/getLocale.d.ts
4+
+++ b/dist/types/restricted/getLocale.d.ts
5+
@@ -1,6 +1,6 @@
6+
import type { PermissionSpecificationBuilder, ValidPermissionSpecification, RestrictedMethodOptions } from '@metamask/permission-controller';
7+
import { PermissionType } from '@metamask/permission-controller';
8+
-import type { NonEmptyArray } from '@metamask/utils';
9+
+import type { Json, NonEmptyArray } from '@metamask/utils';
10+
import type { MethodHooksObject } from '../utils';
11+
declare const methodName = "snap_getLocale";
12+
export declare type GetLocaleMethodHooks = {
13+
@@ -43,5 +43,5 @@ export declare const getLocaleBuilder: Readonly<{
14+
* @param hooks.getLocale - A function that returns the user selected locale.
15+
* @returns The user selected locale.
16+
*/
17+
-export declare function getImplementation({ getLocale }: GetLocaleMethodHooks): (_args: RestrictedMethodOptions<void>) => Promise<string>;
18+
+export declare function getImplementation({ getLocale }: GetLocaleMethodHooks): (_args: RestrictedMethodOptions<Record<string, Json> | Json[]>) => Promise<string>;
19+
export {};

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
"simple-git-hooks": {
3434
"pre-push": "yarn lint"
3535
},
36+
"resolutions": {
37+
"@metamask/rpc-methods@^0.38.1-flask.1": "patch:@metamask/rpc-methods@npm%3A0.38.1-flask.1#./.yarn/patches/@metamask-rpc-methods-npm-0.38.1-flask.1-081e1eb5b3.patch"
38+
},
3639
"devDependencies": {
3740
"@lavamoat/allow-scripts": "^2.3.1",
3841
"@metamask/create-release-branch": "^1.1.0",

packages/permission-controller/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `@metamask/permission-controller`
22

3-
Mediates access to JSON-RPC methods, used to interact with pieces of the MetaMask stack, via middleware for `json-rpc-engine`.
3+
Mediates access to JSON-RPC methods, used to interact with pieces of the MetaMask stack, via middleware for `@metamask/json-rpc-engine`.
44

55
## Installation
66

packages/permission-controller/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
"@metamask/approval-controller": "^3.5.2",
3333
"@metamask/base-controller": "^3.2.2",
3434
"@metamask/controller-utils": "^5.0.1",
35+
"@metamask/json-rpc-engine": "^7.1.1",
36+
"@metamask/rpc-errors": "^6.0.0",
3537
"@metamask/utils": "^8.1.0",
3638
"@types/deep-freeze-strict": "^1.1.0",
3739
"deep-freeze-strict": "^1.1.1",
38-
"eth-rpc-errors": "^4.0.2",
3940
"immer": "^9.0.6",
40-
"json-rpc-engine": "^6.1.0",
4141
"nanoid": "^3.1.31"
4242
},
4343
"devDependencies": {

packages/permission-controller/src/Permission.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,20 @@ type RestrictedMethodContext = Readonly<{
210210
[key: string]: any;
211211
}>;
212212

213-
export type RestrictedMethodParameters = Json[] | Record<string, Json> | void;
213+
export type RestrictedMethodParameters = Json[] | Record<string, Json>;
214214

215215
/**
216216
* The arguments passed to a restricted method implementation.
217217
*
218218
* @template Params - The JSON-RPC parameters of the restricted method.
219219
*/
220-
export type RestrictedMethodOptions<Params extends RestrictedMethodParameters> =
221-
{
222-
method: TargetName;
223-
params?: Params;
224-
context: RestrictedMethodContext;
225-
};
220+
export type RestrictedMethodOptions<
221+
Params extends RestrictedMethodParameters | null,
222+
> = {
223+
method: TargetName;
224+
params?: Params;
225+
context: RestrictedMethodContext;
226+
};
226227

227228
/**
228229
* A synchronous restricted method implementation.

packages/permission-controller/src/PermissionController.test.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import type {
66
} from '@metamask/approval-controller';
77
import { ControllerMessenger } from '@metamask/base-controller';
88
import { isPlainObject } from '@metamask/controller-utils';
9-
import type { Json } from '@metamask/utils';
9+
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
10+
import type { Json, PendingJsonRpcResponse } from '@metamask/utils';
1011
import { hasProperty } from '@metamask/utils';
1112
import assert from 'assert';
12-
import { JsonRpcEngine } from 'json-rpc-engine';
13-
import type { PendingJsonRpcResponse } from 'json-rpc-engine';
1413

1514
import type {
1615
AsyncRestrictedMethod,
@@ -251,7 +250,7 @@ function getDefaultPermissionSpecifications() {
251250
CaveatTypes.filterArrayResponse,
252251
CaveatTypes.reverseArrayResponse,
253252
],
254-
methodImplementation: (_args: RestrictedMethodOptions<void>) => {
253+
methodImplementation: (_args: RestrictedMethodOptions<Json[]>) => {
255254
return ['a', 'b', 'c'];
256255
},
257256
},
@@ -262,7 +261,9 @@ function getDefaultPermissionSpecifications() {
262261
CaveatTypes.filterObjectResponse,
263262
CaveatTypes.noopCaveat,
264263
],
265-
methodImplementation: (_args: RestrictedMethodOptions<void>) => {
264+
methodImplementation: (
265+
_args: RestrictedMethodOptions<Record<string, Json>>,
266+
) => {
266267
return { a: 'x', b: 'y', c: 'z' };
267268
},
268269
validator: (permission: PermissionConstraint) => {
@@ -292,15 +293,15 @@ function getDefaultPermissionSpecifications() {
292293
permissionType: PermissionType.RestrictedMethod,
293294
targetName: PermissionKeys.wallet_noop,
294295
allowedCaveats: null,
295-
methodImplementation: (_args: RestrictedMethodOptions<void>) => {
296+
methodImplementation: (_args: RestrictedMethodOptions<null>) => {
296297
return null;
297298
},
298299
},
299300
[PermissionKeys.wallet_noopWithPermittedAndFailureSideEffects]: {
300301
permissionType: PermissionType.RestrictedMethod,
301302
targetName: PermissionKeys.wallet_noopWithPermittedAndFailureSideEffects,
302303
allowedCaveats: null,
303-
methodImplementation: (_args: RestrictedMethodOptions<void>) => {
304+
methodImplementation: (_args: RestrictedMethodOptions<null>) => {
304305
return null;
305306
},
306307
sideEffect: {
@@ -312,7 +313,7 @@ function getDefaultPermissionSpecifications() {
312313
permissionType: PermissionType.RestrictedMethod,
313314
targetName: PermissionKeys.wallet_noopWithPermittedAndFailureSideEffects2,
314315
allowedCaveats: null,
315-
methodImplementation: (_args: RestrictedMethodOptions<void>) => {
316+
methodImplementation: (_args: RestrictedMethodOptions<null>) => {
316317
return null;
317318
},
318319
sideEffect: {
@@ -324,7 +325,7 @@ function getDefaultPermissionSpecifications() {
324325
permissionType: PermissionType.RestrictedMethod,
325326
targetName: PermissionKeys.wallet_noopWithPermittedSideEffects,
326327
allowedCaveats: null,
327-
methodImplementation: (_args: RestrictedMethodOptions<void>) => {
328+
methodImplementation: (_args: RestrictedMethodOptions<null>) => {
328329
return null;
329330
},
330331
sideEffect: {
@@ -335,7 +336,7 @@ function getDefaultPermissionSpecifications() {
335336
[PermissionKeys.wallet_noopWithValidator]: {
336337
permissionType: PermissionType.RestrictedMethod,
337338
targetName: PermissionKeys.wallet_noopWithValidator,
338-
methodImplementation: (_args: RestrictedMethodOptions<void>) => {
339+
methodImplementation: (_args: RestrictedMethodOptions<null>) => {
339340
return null;
340341
},
341342
allowedCaveats: [CaveatTypes.noopCaveat, CaveatTypes.filterArrayResponse],
@@ -352,7 +353,7 @@ function getDefaultPermissionSpecifications() {
352353
[PermissionKeys.wallet_noopWithRequiredCaveat]: {
353354
permissionType: PermissionType.RestrictedMethod,
354355
targetName: PermissionKeys.wallet_noopWithRequiredCaveat,
355-
methodImplementation: (_args: RestrictedMethodOptions<void>) => {
356+
methodImplementation: (_args: RestrictedMethodOptions<null>) => {
356357
return null;
357358
},
358359
allowedCaveats: [CaveatTypes.noopCaveat],
@@ -388,7 +389,7 @@ function getDefaultPermissionSpecifications() {
388389
[PermissionKeys.wallet_noopWithFactory]: {
389390
permissionType: PermissionType.RestrictedMethod,
390391
targetName: PermissionKeys.wallet_noopWithFactory,
391-
methodImplementation: (_args: RestrictedMethodOptions<void>) => {
392+
methodImplementation: (_args: RestrictedMethodOptions<null>) => {
392393
return null;
393394
},
394395
allowedCaveats: [CaveatTypes.filterArrayResponse],
@@ -415,7 +416,7 @@ function getDefaultPermissionSpecifications() {
415416
permissionType: PermissionType.RestrictedMethod,
416417
targetName: PermissionKeys.snap_foo,
417418
allowedCaveats: null,
418-
methodImplementation: (_args: RestrictedMethodOptions<void>) => {
419+
methodImplementation: (_args: RestrictedMethodOptions<null>) => {
419420
return null;
420421
},
421422
subjectTypes: [SubjectType.Snap],
@@ -1850,6 +1851,7 @@ describe('PermissionController', () => {
18501851
expect(() =>
18511852
controller.removeCaveat(
18521853
origin,
1854+
// @ts-expect-error - Testing invalid permission name.
18531855
PermissionNames.wallet_noopWithRequiredCaveat,
18541856
CaveatTypes.noopCaveat,
18551857
),
@@ -5270,7 +5272,13 @@ describe('PermissionController', () => {
52705272
};
52715273

52725274
const expectedError = errors.unauthorized({
5273-
data: { origin, method: PermissionNames.wallet_getSecretArray },
5275+
data: {
5276+
origin,
5277+
method: PermissionNames.wallet_getSecretArray,
5278+
cause: null,
5279+
},
5280+
message:
5281+
'Unauthorized to perform action. Try requesting the required permission(s) first. For more information, see: https://docs.metamask.io/guide/rpc-api.html#permissions',
52745282
});
52755283

52765284
const { error }: any = await engine.handle(request);
@@ -5293,6 +5301,11 @@ describe('PermissionController', () => {
52935301
const expectedError = errors.methodNotFound('wallet_foo', { origin });
52945302

52955303
const { error }: any = await engine.handle(request);
5304+
5305+
expect(error.message).toStrictEqual(expectedError.message);
5306+
expect(error.data.cause).toBeNull();
5307+
delete error.message;
5308+
delete error.data.cause;
52965309
expect(error).toMatchObject(expect.objectContaining(expectedError));
52975310
});
52985311

@@ -5334,6 +5347,10 @@ describe('PermissionController', () => {
53345347
);
53355348

53365349
const { error }: any = await engine.handle(request);
5350+
expect(error.message).toStrictEqual(expectedError.message);
5351+
expect(error.data.cause).toBeNull();
5352+
delete error.message;
5353+
delete error.data.cause;
53375354
expect(error).toMatchObject(expect.objectContaining(expectedError));
53385355
});
53395356
});

packages/permission-controller/src/PermissionController.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import {
1818
isPlainObject,
1919
isValidJson,
2020
} from '@metamask/controller-utils';
21+
import { JsonRpcError } from '@metamask/rpc-errors';
2122
import { hasProperty } from '@metamask/utils';
2223
import type { Json, Mutable } from '@metamask/utils';
2324
import deepFreeze from 'deep-freeze-strict';
24-
import { EthereumRpcError } from 'eth-rpc-errors';
2525
import { castDraft } from 'immer';
2626
import type { Draft, Patch } from 'immer';
2727
import { nanoid } from 'nanoid';
@@ -1802,6 +1802,7 @@ export class PermissionController<
18021802
target: string,
18031803
): void {
18041804
if (!isPlainObject(caveat)) {
1805+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
18051806
throw new InvalidCaveatError(caveat, origin, target);
18061807
}
18071808

@@ -2149,7 +2150,7 @@ export class PermissionController<
21492150
try {
21502151
this.validateRequestedPermissions(origin, permissions);
21512152
} catch (error) {
2152-
if (error instanceof EthereumRpcError) {
2153+
if (error instanceof JsonRpcError) {
21532154
// Re-throw as an internal error; we should never receive invalid approved
21542155
// permissions.
21552156
throw internalError(

packages/permission-controller/src/errors.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import { errorCodes, ethErrors, EthereumRpcError } from 'eth-rpc-errors';
1+
import type { DataWithOptionalCause } from '@metamask/rpc-errors';
2+
import {
3+
errorCodes,
4+
providerErrors,
5+
rpcErrors,
6+
JsonRpcError,
7+
} from '@metamask/rpc-errors';
28

39
import type { PermissionType } from './Permission';
410

511
type UnauthorizedArg = {
612
data?: Record<string, unknown>;
13+
message?: string;
714
};
815

916
/**
@@ -13,7 +20,7 @@ type UnauthorizedArg = {
1320
* @returns The built error
1421
*/
1522
export function unauthorized(opts: UnauthorizedArg) {
16-
return ethErrors.provider.unauthorized({
23+
return providerErrors.unauthorized({
1724
message:
1825
'Unauthorized to perform action. Try requesting the required permission(s) first. For more information, see: https://docs.metamask.io/guide/rpc-api.html#permissions',
1926
data: opts.data,
@@ -27,19 +34,19 @@ export function unauthorized(opts: UnauthorizedArg) {
2734
* @param data - Optional data for context.
2835
* @returns The built error
2936
*/
30-
export function methodNotFound(method: string, data?: unknown) {
37+
export function methodNotFound(method: string, data?: DataWithOptionalCause) {
3138
const message = `The method "${method}" does not exist / is not available.`;
3239

33-
const opts: Parameters<typeof ethErrors.rpc.methodNotFound>[0] = { message };
40+
const opts: Parameters<typeof rpcErrors.methodNotFound>[0] = { message };
3441
if (data !== undefined) {
3542
opts.data = data;
3643
}
37-
return ethErrors.rpc.methodNotFound(opts);
44+
return rpcErrors.methodNotFound(opts);
3845
}
3946

4047
type InvalidParamsArg = {
4148
message?: string;
42-
data?: unknown;
49+
data?: DataWithOptionalCause;
4350
};
4451

4552
/**
@@ -49,7 +56,7 @@ type InvalidParamsArg = {
4956
* @returns The built error
5057
*/
5158
export function invalidParams(opts: InvalidParamsArg) {
52-
return ethErrors.rpc.invalidParams({
59+
return rpcErrors.invalidParams({
5360
data: opts.data,
5461
message: opts.message,
5562
});
@@ -63,8 +70,8 @@ export function invalidParams(opts: InvalidParamsArg) {
6370
*/
6471
export function userRejectedRequest<Data extends Record<string, unknown>>(
6572
data?: Data,
66-
): EthereumRpcError<Data> {
67-
return ethErrors.provider.userRejectedRequest({ data });
73+
): JsonRpcError<Data> {
74+
return providerErrors.userRejectedRequest({ data });
6875
}
6976

7077
/**
@@ -77,8 +84,8 @@ export function userRejectedRequest<Data extends Record<string, unknown>>(
7784
export function internalError<Data extends Record<string, unknown>>(
7885
message: string,
7986
data?: Data,
80-
): EthereumRpcError<Data> {
81-
return ethErrors.rpc.internal({ message, data });
87+
): JsonRpcError<Data> {
88+
return rpcErrors.internal({ message, data });
8289
}
8390

8491
export class InvalidSubjectIdentifierError extends Error {
@@ -183,7 +190,9 @@ export class CaveatAlreadyExistsError extends Error {
183190
}
184191
}
185192

186-
export class InvalidCaveatError extends EthereumRpcError<unknown> {
193+
export class InvalidCaveatError extends JsonRpcError<
194+
DataWithOptionalCause | undefined
195+
> {
187196
public override data: { origin: string; target: string };
188197

189198
constructor(receivedCaveat: unknown, origin: string, target: string) {

packages/permission-controller/src/permission-middleware.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import type { Json } from '@metamask/utils';
2-
import { createAsyncMiddleware } from 'json-rpc-engine';
1+
import { createAsyncMiddleware } from '@metamask/json-rpc-engine';
32
import type {
43
// eslint-disable-next-line @typescript-eslint/no-unused-vars
54
JsonRpcEngine,
65
JsonRpcMiddleware,
76
AsyncJsonRpcEngineNextCallback,
7+
} from '@metamask/json-rpc-engine';
8+
import type {
9+
Json,
810
PendingJsonRpcResponse,
911
JsonRpcRequest,
10-
} from 'json-rpc-engine';
12+
} from '@metamask/utils';
1113

1214
import type {
1315
GenericPermissionController,

packages/permission-controller/src/rpc-methods/getPermissions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { JsonRpcEngine } from 'json-rpc-engine';
1+
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
22

33
import { getPermissionsHandler } from './getPermissions';
44

0 commit comments

Comments
 (0)