Skip to content

Commit

Permalink
Port remaining changes after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Oct 9, 2023
1 parent 2260a7e commit 402b502
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 41 deletions.
23 changes: 11 additions & 12 deletions packages/rpc-methods/src/permitted/invokeKeyring.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import { rpcErrors } from '@metamask/rpc-errors';
import { HandlerType } from '@metamask/snaps-utils';
import { MOCK_SNAP_ID, getSnapObject } from '@metamask/snaps-utils/test-utils';
import type {
Expand All @@ -6,8 +8,6 @@ import type {
JsonRpcFailure,
JsonRpcSuccess,
} from '@metamask/types';
import { ethErrors } from 'eth-rpc-errors';
import { JsonRpcEngine } from 'json-rpc-engine';

import { invokeKeyringHandler } from './invokeKeyring';

Expand Down Expand Up @@ -93,7 +93,7 @@ describe('wallet_invokeKeyring', () => {
hooks.hasPermission.mockImplementation(() => true);
hooks.getSnap.mockImplementation(() => getSnapObject());
hooks.handleSnapRpcRequest.mockImplementation(() => {
throw ethErrors.rpc.invalidRequest({
throw rpcErrors.invalidRequest({
message: 'Failed to start snap.',
});
});
Expand Down Expand Up @@ -124,7 +124,7 @@ describe('wallet_invokeKeyring', () => {
})) as JsonRpcFailure;

expect(response.error).toStrictEqual({
...ethErrors.rpc
...rpcErrors
.invalidRequest({
message: 'Failed to start snap.',
})
Expand All @@ -141,7 +141,7 @@ describe('wallet_invokeKeyring', () => {
hooks.hasPermission.mockImplementation(() => true);
hooks.getSnap.mockImplementation(() => getSnapObject());
hooks.handleSnapRpcRequest.mockImplementation(() => {
throw ethErrors.rpc.invalidRequest({
throw rpcErrors.invalidRequest({
message: 'Failed to start snap.',
});
});
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('wallet_invokeKeyring', () => {
})) as JsonRpcFailure;

expect(response.error).toStrictEqual({
...ethErrors.rpc
...rpcErrors
.invalidRequest({
message:
'The origin "metamask.io" is not allowed to invoke the method "foo".',
Expand All @@ -190,7 +190,7 @@ describe('wallet_invokeKeyring', () => {
hooks.hasPermission.mockImplementation(() => true);
hooks.getSnap.mockImplementation(() => getSnapObject());
hooks.handleSnapRpcRequest.mockImplementation(() => {
throw ethErrors.rpc.invalidRequest({
throw rpcErrors.invalidRequest({
message: 'Failed to start snap.',
});
});
Expand Down Expand Up @@ -221,7 +221,7 @@ describe('wallet_invokeKeyring', () => {
})) as JsonRpcFailure;

expect(response.error).toStrictEqual({
...ethErrors.rpc
...rpcErrors
.invalidRequest({ message: 'The request must have a method.' })
.serialize(),
stack: expect.any(String),
Expand Down Expand Up @@ -260,7 +260,7 @@ describe('wallet_invokeKeyring', () => {
})) as JsonRpcFailure;

expect(response.error).toStrictEqual({
...ethErrors.rpc
...rpcErrors
.invalidRequest({
message: `The snap "${MOCK_SNAP_ID}" is not connected to "metamask.io". Please connect before invoking the snap.`,
})
Expand Down Expand Up @@ -302,7 +302,7 @@ describe('wallet_invokeKeyring', () => {
})) as JsonRpcFailure;

expect(response.error).toStrictEqual({
...ethErrors.rpc
...rpcErrors
.invalidRequest({
message: `The snap "${MOCK_SNAP_ID}" is not installed. Please install it first, before invoking the snap.`,
})
Expand Down Expand Up @@ -334,13 +334,12 @@ describe('wallet_invokeKeyring', () => {
id: 1,
method: 'wallet_invokeKeyring',
params: {
snapId: undefined,
request: [],
},
})) as JsonRpcFailure;

expect(response.error).toStrictEqual({
...ethErrors.rpc
...rpcErrors
.invalidParams({
message: 'Must specify a valid snap ID.',
})
Expand Down
10 changes: 5 additions & 5 deletions packages/rpc-methods/src/permitted/invokeKeyring.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { rpcErrors } from '@metamask/rpc-errors';
import type { Snap } from '@metamask/snaps-utils';
import {
HandlerType,
Expand All @@ -12,7 +13,6 @@ import type {
JsonRpcRequest,
} from '@metamask/types';
import { hasProperty, type Json } from '@metamask/utils';
import { ethErrors } from 'eth-rpc-errors';

import type { MethodHooksObject } from '../utils';
import type { InvokeSnapSugarArgs } from './invokeSnapSugar';
Expand Down Expand Up @@ -95,23 +95,23 @@ async function invokeKeyringImplementation(

if (!origin || !hasPermission(origin, WALLET_SNAP_PERMISSION_KEY)) {
return end(
ethErrors.rpc.invalidRequest({
rpcErrors.invalidRequest({
message: `The snap "${snapId}" is not connected to "${origin}". Please connect before invoking the snap.`,
}),
);
}

if (!getSnap(snapId)) {
return end(
ethErrors.rpc.invalidRequest({
rpcErrors.invalidRequest({
message: `The snap "${snapId}" is not installed. Please install it first, before invoking the snap.`,
}),
);
}

if (!hasProperty(request, 'method') || typeof request.method !== 'string') {
return end(
ethErrors.rpc.invalidRequest({
rpcErrors.invalidRequest({
message: 'The request must have a method.',
}),
);
Expand All @@ -120,7 +120,7 @@ async function invokeKeyringImplementation(
const allowedMethods = getAllowedKeyringMethods(origin);
if (!allowedMethods.includes(request.method)) {
return end(
ethErrors.rpc.invalidRequest({
rpcErrors.invalidRequest({
message: `The origin "${origin}" is not allowed to invoke the method "${request.method}".`,
}),
);
Expand Down
8 changes: 4 additions & 4 deletions packages/snaps-controllers/src/snaps/endowments/keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import type {
ValidPermissionSpecification,
} from '@metamask/permission-controller';
import { PermissionType, SubjectType } from '@metamask/permission-controller';
import { rpcErrors } from '@metamask/rpc-errors';
import type { KeyringOrigins } from '@metamask/snaps-utils';
import { assertIsKeyringOrigins, SnapCaveatType } from '@metamask/snaps-utils';
import type { Json, NonEmptyArray } from '@metamask/utils';
import { assert, hasProperty, isPlainObject } from '@metamask/utils';
import { ethErrors } from 'eth-rpc-errors';

import { SnapEndowments } from './enum';

Expand Down Expand Up @@ -49,7 +49,7 @@ const specificationBuilder: PermissionSpecificationBuilder<
caveats?.length !== 1 ||
caveats[0].type !== SnapCaveatType.KeyringOrigin
) {
throw ethErrors.rpc.invalidParams({
throw rpcErrors.invalidParams({
message: `Expected a single "${SnapCaveatType.KeyringOrigin}" caveat.`,
});
}
Expand All @@ -72,13 +72,13 @@ export const keyringEndowmentBuilder = Object.freeze({
*/
function validateCaveatOrigins(caveat: Caveat<string, any>) {
if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {
throw ethErrors.rpc.invalidParams({
throw rpcErrors.invalidParams({
message: 'Invalid keyring origins: Expected a plain object.',
});
}

const { value } = caveat;
assertIsKeyringOrigins(value, ethErrors.rpc.invalidParams);
assertIsKeyringOrigins(value, rpcErrors.invalidParams);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class BaseSnapExecutor {
assert(
!required || handler !== undefined,
`No ${handlerType} handler exported for snap "${target}`,
ethErrors.rpc.methodNotSupported,
rpcErrors.methodNotSupported,
);

// Certain handlers are not required. If they are not exported, we
Expand All @@ -164,7 +164,7 @@ export class BaseSnapExecutor {
try {
return getSafeJson(result);
} catch (error) {
throw ethErrors.rpc.internal(
throw rpcErrors.internal(
`Received non-JSON-serializable value: ${error.message.replace(
/^Assertion failed: /u,
'',
Expand Down Expand Up @@ -199,7 +199,7 @@ export class BaseSnapExecutor {

private async onCommandRequest(message: JsonRpcRequest) {
if (!isJsonRpcRequest(message)) {
throw ethErrors.rpc.invalidRequest({
throw rpcErrors.invalidRequest({
message: 'Command stream received a non-JSON-RPC request.',
data: message,
});
Expand Down Expand Up @@ -255,7 +255,7 @@ export class BaseSnapExecutor {

protected notify(requestObject: Omit<JsonRpcNotification, 'jsonrpc'>) {
if (!isValidJson(requestObject) || !isObject(requestObject)) {
throw ethErrors.rpc.internal(
throw rpcErrors.internal(
'JSON-RPC notifications must be JSON serializable objects',
);
}
Expand Down Expand Up @@ -373,7 +373,7 @@ export class BaseSnapExecutor {
});
} catch (error) {
this.removeSnap(snapId);
throw ethErrors.rpc.internal({
throw rpcErrors.internal({
message: `Error while running snap '${snapId}': ${getErrorMessage(
error,
)}`,
Expand Down Expand Up @@ -509,7 +509,7 @@ export class BaseSnapExecutor {
): Promise<Result> {
const data = this.snapData.get(snapId);
if (data === undefined) {
throw ethErrors.rpc.internal(
throw rpcErrors.internal(
`Tried to execute in context of unknown snap: "${snapId}".`,
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { StreamProvider } from '@metamask/providers';
import { rpcErrors } from '@metamask/rpc-errors';
import type { SnapsGlobalObject } from '@metamask/rpc-methods';
import type { SnapId } from '@metamask/snaps-utils';
import { logWarning } from '@metamask/snaps-utils';
import { hasProperty } from '@metamask/utils';
import { ethErrors } from 'eth-rpc-errors';

import { rootRealmGlobal } from '../globalObject';
import type { EndowmentFactoryOptions } from './commonEndowmentFactory';
Expand Down Expand Up @@ -102,7 +102,7 @@ export function createEndowments(
} else {
// If we get to this point, we've been passed an endowment that doesn't
// exist in our current environment.
throw ethErrors.rpc.internal(`Unknown endowment: "${endowmentName}".`);
throw rpcErrors.internal(`Unknown endowment: "${endowmentName}".`);
}
return { allEndowments, teardowns };
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethErrors } from 'eth-rpc-errors';
import { rpcErrors } from '@metamask/rpc-errors';

const MINIMUM_INTERVAL = 10;

Expand All @@ -17,7 +17,7 @@ const createInterval = () => {

const _setInterval = (handler: TimerHandler, timeout?: number): unknown => {
if (typeof handler !== 'function') {
throw ethErrors.rpc.invalidInput(
throw rpcErrors.invalidInput(
`The interval handler must be a function. Received: ${typeof handler}.`,
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethErrors } from 'eth-rpc-errors';
import { rpcErrors } from '@metamask/rpc-errors';

const MINIMUM_TIMEOUT = 10;

Expand All @@ -16,7 +16,7 @@ const createTimeout = () => {
const registeredHandles = new Map<unknown, unknown>();
const _setTimeout = (handler: TimerHandler, timeout?: number): unknown => {
if (typeof handler !== 'function') {
throw ethErrors.rpc.internal(
throw rpcErrors.internal(
`The timeout handler must be a function. Received: ${typeof handler}.`,
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethErrors } from 'eth-rpc-errors';
import { rpcErrors } from '@metamask/rpc-errors';

import { rootRealmGlobal } from './globalObject';

Expand Down Expand Up @@ -29,7 +29,7 @@ export function addEventListener(
return rootRealmGlobal.process.on(event, listener);
}

throw ethErrors.rpc.internal('Platform agnostic addEventListener failed.');
throw rpcErrors.internal('Platform agnostic addEventListener failed.');
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/snaps-execution-environments/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function assertSnapOutboundRequest(args: RequestArguments) {
String.prototype.startsWith.call(args.method, 'wallet_') ||
String.prototype.startsWith.call(args.method, 'snap_'),
'The global Snap API only allows RPC methods starting with `wallet_*` and `snap_*`.',
ethErrors.rpc.methodNotSupported,
rpcErrors.methodNotSupported,
);
assert(
!BLOCKED_RPC_METHODS.includes(args.method),
Expand All @@ -147,7 +147,7 @@ export function assertSnapOutboundRequest(args: RequestArguments) {
args,
JsonStruct,
'Provided value is not JSON-RPC compatible',
ethErrors.rpc.invalidParams,
rpcErrors.invalidParams,
);
}

Expand Down Expand Up @@ -178,7 +178,7 @@ export function assertEthereumOutboundRequest(args: RequestArguments) {
args,
JsonStruct,
'Provided value is not JSON-RPC compatible',
ethErrors.rpc.invalidParams,
rpcErrors.invalidParams,
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { rpcErrors } from '@metamask/rpc-errors';
import { ChainIdStruct, HandlerType } from '@metamask/snaps-utils';
import type { Json, JsonRpcSuccess } from '@metamask/utils';
import {
Expand All @@ -7,7 +8,6 @@ import {
JsonRpcSuccessStruct,
JsonStruct,
} from '@metamask/utils';
import { ethErrors } from 'eth-rpc-errors';
import type { Infer } from 'superstruct';
import {
array,
Expand Down Expand Up @@ -133,7 +133,7 @@ export function assertIsOnTransactionRequestArguments(
value,
OnTransactionRequestArgumentsStruct,
'Invalid request params',
ethErrors.rpc.invalidParams,
rpcErrors.invalidParams,
);
}

Expand Down Expand Up @@ -176,7 +176,7 @@ export function assertIsOnNameLookupRequestArguments(
value,
OnNameLookupRequestArgumentsStruct,
'Invalid request params',
ethErrors.rpc.invalidParams,
rpcErrors.invalidParams,
);
}

Expand Down

0 comments on commit 402b502

Please sign in to comment.