Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json rpc engine and utils #1

Closed
wants to merge 12 commits into from
Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
diff --git a/dist/types/restricted/getLocale.d.ts b/dist/types/restricted/getLocale.d.ts
index 2941d2733042664c341776c7bc840ba0813994ca..0188bbd4de0cd013159a36b736ad9baf94c18c92 100644
--- a/dist/types/restricted/getLocale.d.ts
+++ b/dist/types/restricted/getLocale.d.ts
@@ -1,6 +1,6 @@
import type { PermissionSpecificationBuilder, ValidPermissionSpecification, RestrictedMethodOptions } from '@metamask/permission-controller';
import { PermissionType } from '@metamask/permission-controller';
-import type { NonEmptyArray } from '@metamask/utils';
+import type { Json, NonEmptyArray } from '@metamask/utils';
import type { MethodHooksObject } from '../utils';
declare const methodName = "snap_getLocale";
export declare type GetLocaleMethodHooks = {
@@ -43,5 +43,5 @@ export declare const getLocaleBuilder: Readonly<{
* @param hooks.getLocale - A function that returns the user selected locale.
* @returns The user selected locale.
*/
-export declare function getImplementation({ getLocale }: GetLocaleMethodHooks): (_args: RestrictedMethodOptions<void>) => Promise<string>;
+export declare function getImplementation({ getLocale }: GetLocaleMethodHooks): (_args: RestrictedMethodOptions<Record<string, Json> | Json[]>) => Promise<string>;
export {};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
"simple-git-hooks": {
"pre-push": "yarn lint"
},
"resolutions": {
"@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",
"@metamask/rpc-methods@^1.0.1": "patch:@metamask/rpc-methods@npm%3A0.38.1-flask.1#./.yarn/patches/@metamask-rpc-methods-npm-0.38.1-flask.1-081e1eb5b3.patch"
},
"devDependencies": {
"@lavamoat/allow-scripts": "^2.3.1",
"@metamask/create-release-branch": "^1.1.0",
Expand All @@ -40,7 +44,7 @@
"@metamask/eslint-config-nodejs": "^12.0.0",
"@metamask/eslint-config-typescript": "^12.0.0",
"@metamask/eth-json-rpc-provider": "^1.0.0",
"@metamask/utils": "^6.2.0",
"@metamask/utils": "^8.1.0",
"@types/node": "^16.18.24",
"@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7",
Expand Down
2 changes: 1 addition & 1 deletion packages/accounts-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@metamask/eth-snap-keyring": "^0.2.2",
"@metamask/keyring-api": "^0.2.5",
"@metamask/snaps-utils": "^1.0.1",
"@metamask/utils": "^6.2.0",
"@metamask/utils": "^8.1.0",
"deepmerge": "^4.2.2",
"eth-rpc-errors": "^4.0.2",
"ethereumjs-util": "^7.0.10",
Expand Down
2 changes: 1 addition & 1 deletion packages/address-book-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"dependencies": {
"@metamask/base-controller": "^3.2.1",
"@metamask/controller-utils": "^5.0.0",
"@metamask/utils": "^6.2.0"
"@metamask/utils": "^8.1.0"
},
"devDependencies": {
"@metamask/auto-changelog": "^3.1.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/approval-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
},
"dependencies": {
"@metamask/base-controller": "^3.2.1",
"@metamask/utils": "^6.2.0",
"eth-rpc-errors": "^4.0.2",
"@metamask/rpc-errors": "^6.0.0",
"@metamask/utils": "^8.1.0",
"immer": "^9.0.6",
"nanoid": "^3.1.31"
},
Expand Down
16 changes: 8 additions & 8 deletions packages/approval-controller/src/ApprovalController.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable jest/expect-expect */

import { ControllerMessenger } from '@metamask/base-controller';
import { errorCodes, EthereumRpcError } from 'eth-rpc-errors';
import { errorCodes, JsonRpcError } from '@metamask/rpc-errors';

import type {
ApprovalControllerActions,
Expand Down Expand Up @@ -462,7 +462,7 @@

describe('get', () => {
it('gets entry', () => {
approvalController.add({

Check warning on line 465 in packages/approval-controller/src/ApprovalController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
id: 'foo',
origin: 'bar.baz',
type: 'myType',
Expand All @@ -481,7 +481,7 @@
});

it('returns undefined for non-existing entry', () => {
approvalController.add({ id: 'foo', origin: 'bar.baz', type: 'type' });

Check warning on line 484 in packages/approval-controller/src/ApprovalController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

expect(approvalController.get('fizz')).toBeUndefined();

Expand All @@ -495,7 +495,7 @@
let addWithCatch: (args: any) => void;

beforeEach(() => {
addWithCatch = (args: any) =>

Check warning on line 498 in packages/approval-controller/src/ApprovalController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Promise-returning function provided to variable where a void return was expected
approvalController.add(args).catch(() => undefined);
});

Expand Down Expand Up @@ -630,19 +630,19 @@
const addWithCatch = (args: any) =>
approvalController.add(args).catch(() => undefined);

addWithCatch({ id: '1', origin: 'origin1', type: 'type0' });

Check warning on line 633 in packages/approval-controller/src/ApprovalController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
expect(approvalController.getTotalApprovalCount()).toBe(1);

addWithCatch({ id: '2', origin: 'origin1', type: 'type1' });

Check warning on line 636 in packages/approval-controller/src/ApprovalController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
expect(approvalController.getTotalApprovalCount()).toBe(2);

addWithCatch({ id: '3', origin: 'origin2', type: 'type1' });

Check warning on line 639 in packages/approval-controller/src/ApprovalController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
expect(approvalController.getTotalApprovalCount()).toBe(3);

approvalController.reject('2', new Error('foo'));
expect(approvalController.getTotalApprovalCount()).toBe(2);

approvalController.clear(new EthereumRpcError(1, 'clear'));
approvalController.clear(new JsonRpcError(1, 'clear'));
expect(approvalController.getTotalApprovalCount()).toBe(0);
});

Expand All @@ -657,16 +657,16 @@
const addWithCatch = (args: any) =>
approvalController.add(args).catch(() => undefined);

addWithCatch({ id: '1', origin: 'origin1', type: 'type0' });

Check warning on line 660 in packages/approval-controller/src/ApprovalController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
expect(approvalController.getTotalApprovalCount()).toBe(1);

addWithCatch({ id: '2', origin: 'origin1', type: 'type0' });

Check warning on line 663 in packages/approval-controller/src/ApprovalController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
expect(approvalController.getTotalApprovalCount()).toBe(2);

approvalController.reject('2', new Error('foo'));
expect(approvalController.getTotalApprovalCount()).toBe(1);

approvalController.clear(new EthereumRpcError(1, 'clear'));
approvalController.clear(new JsonRpcError(1, 'clear'));
expect(approvalController.getTotalApprovalCount()).toBe(0);
});
});
Expand Down Expand Up @@ -699,7 +699,7 @@
});

it('returns true for existing entry by id', () => {
approvalController.add({ id: 'foo', origin: 'bar.baz', type: TYPE });

Check warning on line 702 in packages/approval-controller/src/ApprovalController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

expect(approvalController.has({ id: 'foo' })).toBe(true);
});
Expand Down Expand Up @@ -1025,7 +1025,7 @@
describe('clear', () => {
it('does nothing if state is already empty', () => {
expect(() =>
approvalController.clear(new EthereumRpcError(1, 'clear')),
approvalController.clear(new JsonRpcError(1, 'clear')),
).not.toThrow();
});

Expand All @@ -1040,7 +1040,7 @@
.add({ id: 'foo3', origin: 'fizz.buzz', type: 'myType' })
.catch((_error) => undefined);

approvalController.clear(new EthereumRpcError(1, 'clear'));
approvalController.clear(new JsonRpcError(1, 'clear'));

expect(
approvalController.state[PENDING_APPROVALS_STORE_KEY],
Expand All @@ -1055,16 +1055,16 @@
type: 'myType',
});

approvalController.clear(new EthereumRpcError(1000, 'foo'));
approvalController.clear(new JsonRpcError(1000, 'foo'));
await expect(rejectPromise).rejects.toThrow(
new EthereumRpcError(1000, 'foo'),
new JsonRpcError(1000, 'foo'),
);
});

it('does not clear approval flows', async () => {
approvalController.startFlow();

approvalController.clear(new EthereumRpcError(1, 'clear'));
approvalController.clear(new JsonRpcError(1, 'clear'));

expect(approvalController.state[APPROVAL_FLOWS_STORE_KEY]).toHaveLength(
1,
Expand Down
14 changes: 7 additions & 7 deletions packages/approval-controller/src/ApprovalController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { RestrictedControllerMessenger } from '@metamask/base-controller';
import { BaseControllerV2 } from '@metamask/base-controller';
import type { JsonRpcError, DataWithOptionalCause } from '@metamask/rpc-errors';
import { rpcErrors } from '@metamask/rpc-errors';
import type { Json, OptionalField } from '@metamask/utils';
import type { EthereumRpcError } from 'eth-rpc-errors';
import { ethErrors } from 'eth-rpc-errors';
import type { Patch } from 'immer';
import { nanoid } from 'nanoid';

Expand Down Expand Up @@ -256,7 +256,7 @@ export type GetApprovalsState = {

export type ClearApprovalRequests = {
type: `${typeof controllerName}:clearRequests`;
handler: (error: EthereumRpcError<unknown>) => void;
handler: (error: JsonRpcError<DataWithOptionalCause>) => void;
};

export type AddApprovalRequest = {
Expand Down Expand Up @@ -719,10 +719,10 @@ export class ApprovalController extends BaseControllerV2<
/**
* Rejects and deletes all approval requests.
*
* @param rejectionError - The EthereumRpcError to reject the approval
* @param rejectionError - The JsonRpcError to reject the approval
* requests with.
*/
clear(rejectionError: EthereumRpcError<unknown>): void {
clear(rejectionError: JsonRpcError<DataWithOptionalCause>): void {
for (const id of this.#approvals.keys()) {
this.reject(id, rejectionError);
}
Expand Down Expand Up @@ -878,7 +878,7 @@ export class ApprovalController extends BaseControllerV2<
!this.#typesExcludedFromRateLimiting.includes(type) &&
this.has({ origin, type })
) {
throw ethErrors.rpc.resourceUnavailable(
throw rpcErrors.resourceUnavailable(
getAlreadyPendingMessage(origin, type),
);
}
Expand Down Expand Up @@ -937,7 +937,7 @@ export class ApprovalController extends BaseControllerV2<
}

if (errorMessage) {
throw ethErrors.rpc.internal(errorMessage);
throw rpcErrors.internal(errorMessage);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/assets-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"@metamask/metamask-eth-abis": "3.0.0",
"@metamask/network-controller": "^13.0.0",
"@metamask/preferences-controller": "^4.4.1",
"@metamask/rpc-errors": "^5.1.1",
"@metamask/utils": "^6.2.0",
"@metamask/rpc-errors": "^6.0.0",
"@metamask/utils": "^8.1.0",
"@types/uuid": "^8.3.0",
"async-mutex": "^0.2.6",
"ethereumjs-util": "^7.0.10",
Expand Down
2 changes: 1 addition & 1 deletion packages/base-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@metamask/utils": "^6.2.0",
"@metamask/utils": "^8.1.0",
"immer": "^9.0.6"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions packages/controller-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
},
"dependencies": {
"@metamask/eth-query": "^3.0.1",
"@metamask/utils": "^6.2.0",
"@metamask/utils": "^8.1.0",
"@spruceid/siwe-parser": "1.1.3",
"eth-ens-namehash": "^2.0.8",
"eth-rpc-errors": "^4.0.2",
"ethereumjs-util": "^7.0.10",
"ethjs-unit": "^0.1.6",
"fast-deep-equal": "^3.1.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/ens-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@metamask/base-controller": "^3.2.1",
"@metamask/controller-utils": "^5.0.0",
"@metamask/network-controller": "^13.0.0",
"@metamask/utils": "^6.2.0",
"@metamask/utils": "^8.1.0",
"ethereum-ens-network-map": "^1.0.2",
"punycode": "^2.1.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/gas-fee-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@metamask/controller-utils": "^5.0.0",
"@metamask/eth-query": "^3.0.1",
"@metamask/network-controller": "^13.0.0",
"@metamask/utils": "^6.2.0",
"@metamask/utils": "^8.1.0",
"@types/uuid": "^8.3.0",
"ethereumjs-util": "^7.0.10",
"ethjs-unit": "^0.1.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@metamask/eth-keyring-controller": "^13.0.1",
"@metamask/message-manager": "^7.3.3",
"@metamask/preferences-controller": "^4.4.1",
"@metamask/utils": "^6.2.0",
"@metamask/utils": "^8.1.0",
"async-mutex": "^0.2.6",
"ethereumjs-util": "^7.0.10",
"ethereumjs-wallet": "^1.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/message-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@metamask/base-controller": "^3.2.1",
"@metamask/controller-utils": "^5.0.0",
"@metamask/eth-sig-util": "^7.0.0",
"@metamask/utils": "^6.2.0",
"@metamask/utils": "^8.1.0",
"@types/uuid": "^8.3.0",
"ethereumjs-util": "^7.0.10",
"jsonschema": "^1.2.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/name-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"dependencies": {
"@metamask/base-controller": "^3.2.1",
"@metamask/utils": "^6.2.0",
"@metamask/utils": "^8.1.0",
"async-mutex": "^0.2.6",
"immer": "^9.0.6"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/network-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@metamask/eth-json-rpc-provider": "^1.0.0",
"@metamask/eth-query": "^3.0.1",
"@metamask/swappable-obj-proxy": "^2.1.0",
"@metamask/utils": "^6.2.0",
"@metamask/utils": "^8.1.0",
"async-mutex": "^0.2.6",
"eth-block-tracker": "^7.0.1",
"eth-rpc-errors": "^4.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/notification-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"dependencies": {
"@metamask/base-controller": "^3.2.1",
"@metamask/utils": "^6.2.0",
"@metamask/utils": "^8.1.0",
"immer": "^9.0.6",
"nanoid": "^3.1.31"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/permission-controller/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `@metamask/permission-controller`

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

## Installation

Expand Down
6 changes: 3 additions & 3 deletions packages/permission-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
"@metamask/approval-controller": "^3.5.1",
"@metamask/base-controller": "^3.2.1",
"@metamask/controller-utils": "^5.0.0",
"@metamask/utils": "^6.2.0",
"@metamask/json-rpc-engine": "^7.1.1",
"@metamask/rpc-errors": "^6.0.0",
"@metamask/utils": "^8.1.0",
"@types/deep-freeze-strict": "^1.1.0",
"deep-freeze-strict": "^1.1.1",
"eth-rpc-errors": "^4.0.2",
"immer": "^9.0.6",
"json-rpc-engine": "^6.1.0",
"nanoid": "^3.1.31"
},
"devDependencies": {
Expand Down
15 changes: 8 additions & 7 deletions packages/permission-controller/src/Permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,20 @@ type RestrictedMethodContext = Readonly<{
[key: string]: any;
}>;

export type RestrictedMethodParameters = Json[] | Record<string, Json> | void;
export type RestrictedMethodParameters = Json[] | Record<string, Json>;

/**
* The arguments passed to a restricted method implementation.
*
* @template Params - The JSON-RPC parameters of the restricted method.
*/
export type RestrictedMethodOptions<Params extends RestrictedMethodParameters> =
{
method: TargetName;
params?: Params;
context: RestrictedMethodContext;
};
export type RestrictedMethodOptions<
Params extends RestrictedMethodParameters | null,
> = {
method: TargetName;
params?: Params;
context: RestrictedMethodContext;
};

/**
* A synchronous restricted method implementation.
Expand Down
Loading
Loading