mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-29 07:16:36 +01:00
95c37e1ba3
* feat: add yaml feature management Add yaml feature file per build type. Also add method to parse yaml and set enabled features env to true. The build process will then replace any process.env[feature] that exists on the config by its value * chore: add example for desktop * Added initial draft of build features * [TMP] Sync between computers * Is able to succesfully build stable extension with snaps feature * Removing var context from builds.yml * Add asssets to builds.yml * Minor bug fixes and removing debug logs * [WIP] Test changes * Removed TODOs * Fix regession bug Also * remove debug logs * merge Variables.set and Variables.setMany with an overload * Fix build, lint and a bunch of issues * Update LavaMoat policies * Re-add desktop build type * Fix some tests * Fix desktop build * Define some env variables used by MV3 * Fix lint * Fix remove-fenced-code tests * Fix README typo * Move new code * Fix missing asset copy * Move Jest env setup * Fix path for test after rebase * Fix code fences * Fix fencing and LavaMoat policies * Fix MMI code-fencing after rebase * Fix MMI code fencing after merge * Fix more MMI code fencing --------- Co-authored-by: cryptotavares <joao.tavares@consensys.net> Co-authored-by: Frederik Bolding <frederik.bolding@gmail.com> Co-authored-by: Brad Decker <bhdecker84@gmail.com>
111 lines
3.2 KiB
JavaScript
111 lines
3.2 KiB
JavaScript
///: BEGIN:ONLY_INCLUDE_IN(snaps)
|
|
import { handlers as permittedSnapMethods } from '@metamask/rpc-methods/dist/permitted';
|
|
///: END:ONLY_INCLUDE_IN
|
|
import { permissionRpcMethods } from '@metamask/permission-controller';
|
|
import { selectHooks } from '@metamask/rpc-methods/dist/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);
|
|
}
|
|
return map;
|
|
}, new Map());
|
|
|
|
const expectedHookNames = Array.from(
|
|
new Set(
|
|
flatten(allHandlers.map(({ hookNames }) => Object.keys(hookNames))),
|
|
).values(),
|
|
);
|
|
|
|
/**
|
|
* Creates a json-rpc-engine middleware of RPC method implementations.
|
|
*
|
|
* 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
|
|
* controllers.
|
|
* @returns {(req: object, res: object, next: Function, end: Function) => void}
|
|
*/
|
|
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`,
|
|
);
|
|
}
|
|
|
|
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);
|
|
if (handler) {
|
|
const { implementation, hookNames } = handler;
|
|
try {
|
|
// Implementations may or may not be async, so we must await them.
|
|
return await implementation(
|
|
req,
|
|
res,
|
|
next,
|
|
end,
|
|
selectHooks(hooks, hookNames),
|
|
);
|
|
} catch (error) {
|
|
console.error(error);
|
|
return end(error);
|
|
}
|
|
}
|
|
|
|
return next();
|
|
};
|
|
}
|
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(snaps)
|
|
const snapHandlerMap = permittedSnapMethods.reduce((map, handler) => {
|
|
for (const methodName of handler.methodNames) {
|
|
map.set(methodName, handler);
|
|
}
|
|
return map;
|
|
}, new Map());
|
|
|
|
export function createSnapMethodMiddleware(isSnap, hooks) {
|
|
return async function methodMiddleware(req, res, next, end) {
|
|
const handler = snapHandlerMap.get(req.method);
|
|
if (handler) {
|
|
if (/^snap_/iu.test(req.method) && !isSnap) {
|
|
return end(ethErrors.rpc.methodNotFound());
|
|
}
|
|
|
|
const { implementation, hookNames } = handler;
|
|
try {
|
|
// Implementations may or may not be async, so we must await them.
|
|
return await implementation(
|
|
req,
|
|
res,
|
|
next,
|
|
end,
|
|
selectHooks(hooks, hookNames),
|
|
);
|
|
} catch (error) {
|
|
console.error(error);
|
|
return end(error);
|
|
}
|
|
}
|
|
|
|
return next();
|
|
};
|
|
}
|
|
///: END:ONLY_INCLUDE_IN
|