Skip to content

Commit

Permalink
Merge branch 'develop' into bump-rate-limit-controller
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed May 13, 2024
2 parents 6d6e814 + cfcbd1e commit 4b4272c
Show file tree
Hide file tree
Showing 86 changed files with 2,811 additions and 1,217 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ jobs:
#!/bin/bash
GH_LABEL=team-mmi
if [ -z "$CIRCLE_PULL_REQUESTS" ]; then
exit 0
fi
echo $CIRCLE_PULL_REQUESTS | sed 's/,/\n/g'
Expand Down
24 changes: 9 additions & 15 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export function createMockFeatureAnnouncementAPIResult(): ContentfulResult {
export function createMockFeatureAnnouncementRaw(): FeatureAnnouncementRawNotification {
return {
type: TRIGGER_TYPES.FEATURES_ANNOUNCEMENT,
createdAt: '2024-04-09T13:24:01.872Z',
createdAt: '2999-04-09T13:24:01.872Z',
data: {
id: 'dont-miss-out-on-airdrops-and-new-nft-mints',
category: 'ANNOUNCEMENT',
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/controllers/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default class PreferencesController {
useNativeCurrencyAsPrimaryCurrency: true,
hideZeroBalanceTokens: false,
petnamesEnabled: true,
redesignedConfirmationsEnabled: false,
redesignedConfirmationsEnabled: true,
featureNotificationsEnabled: false,
},
// ENS decentralized website resolution
Expand Down
5 changes: 2 additions & 3 deletions app/scripts/lib/createRPCMethodTrackingMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ import {

///: BEGIN:ONLY_INCLUDE_IF(blockaid)
import { SIGNING_METHODS } from '../../../shared/constants/transaction';

import { getBlockaidMetricsProps } from '../../../ui/helpers/utils/metrics';
///: END:ONLY_INCLUDE_IF
import {
REDESIGN_APPROVAL_TYPES,
REDESIGN_TRANSACTION_TYPES,
} from '../../../ui/pages/confirmations/utils/confirm';
import { getBlockaidMetricsProps } from '../../../ui/helpers/utils/metrics';
///: END:ONLY_INCLUDE_IF
import { getSnapAndHardwareInfoForMetrics } from './snap-keyring/metrics';

/**
Expand Down
62 changes: 42 additions & 20 deletions app/scripts/lib/rpc-method-middleware/createMethodMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { permissionRpcMethods } from '@metamask/permission-controller';
import { selectHooks } from '@metamask/snaps-rpc-methods';
import { hasProperty } from '@metamask/utils';
import { ethErrors } from 'eth-rpc-errors';
import { flatten } from 'lodash';
import { UNSUPPORTED_RPC_METHODS } from '../../../../shared/constants/network';
import localHandlers from './handlers';

const allHandlers = [...localHandlers, ...permissionRpcMethods.handlers];

const handlerMap = allHandlers.reduce((map, handler) => {
for (const methodName of handler.methodNames) {
map.set(methodName, handler);
map[methodName] = handler;
}
return map;
}, new Map());
}, {});

const expectedHookNames = Array.from(
new Set(
flatten(allHandlers.map(({ hookNames }) => Object.keys(hookNames))),
).values(),
const expectedHookNames = new Set(
allHandlers.flatMap(({ hookNames }) => Object.getOwnPropertyNames(hookNames)),
);

/**
Expand All @@ -26,28 +24,20 @@ const expectedHookNames = Array.from(
* Handlers consume functions that hook into the background, and only depend
* on their signatures, not e.g. controller internals.
*
* @param {Record<string, unknown>} hooks - Required "hooks" into our
* @param {Record<string, (...args: unknown[]) => unknown | Promise<unknown>>} hooks - Required "hooks" into our
* controllers.
* @returns {(req: object, res: object, next: Function, end: Function) => void}
* @returns {import('json-rpc-engine').JsonRpcMiddleware<unknown, unknown>} The method middleware function.
*/
export function createMethodMiddleware(hooks) {
// Fail immediately if we forgot to provide any expected hooks.
const missingHookNames = expectedHookNames.filter(
(hookName) => !Object.hasOwnProperty.call(hooks, hookName),
);
if (missingHookNames.length > 0) {
throw new Error(
`Missing expected hooks:\n\n${missingHookNames.join('\n')}\n`,
);
}
assertExpectedHook(hooks);

return async function methodMiddleware(req, res, next, end) {
// Reject unsupported methods.
if (UNSUPPORTED_RPC_METHODS.has(req.method)) {
return end(ethErrors.rpc.methodNotSupported());
}

const handler = handlerMap.get(req.method);
const handler = handlerMap[req.method];
if (handler) {
const { implementation, hookNames } = handler;
try {
Expand All @@ -63,10 +53,42 @@ export function createMethodMiddleware(hooks) {
if (process.env.METAMASK_DEBUG) {
console.error(error);
}
return end(error);
return end(
error instanceof Error
? error
: ethErrors.rpc.internal({ data: error }),
);
}
}

return next();
};
}

/**
* Asserts that the hooks object only has all expected hooks and no extraneous ones.
*
* @param {Record<string, unknown>} hooks - Required "hooks" into our controllers.
*/
function assertExpectedHook(hooks) {
const missingHookNames = [];
expectedHookNames.forEach((hookName) => {
if (!hasProperty(hooks, hookName)) {
missingHookNames.push(hookName);
}
});
if (missingHookNames.length > 0) {
throw new Error(
`Missing expected hooks:\n\n${missingHookNames.join('\n')}\n`,
);
}

const extraneousHookNames = Object.getOwnPropertyNames(hooks).filter(
(hookName) => !expectedHookNames.has(hookName),
);
if (extraneousHookNames.length > 0) {
throw new Error(
`Received unexpected hooks:\n\n${extraneousHookNames.join('\n')}\n`,
);
}
}

0 comments on commit 4b4272c

Please sign in to comment.