1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 11:22:43 +02:00

Add missing permissions middleware hook (#13205)

Adds a missing middleware hook for `wallet_requestPermissions` that we failed to add in #12243. Also adds a runtime check that throws an error if any expected hooks are not provided to `createMethodMiddleware`.

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
This commit is contained in:
Erik Marks 2022-01-04 12:57:18 -08:00 committed by GitHub
parent e213ea77bd
commit 75e8b4f8b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -13,6 +13,12 @@ const handlerMap = allHandlers.reduce((map, handler) => {
return map;
}, new Map());
const expectedHookNames = Array.from(
new Set(
allHandlers.map(({ hookNames }) => Object.keys(hookNames)).flat(),
).values(),
);
/**
* Creates a json-rpc-engine middleware of RPC method implementations.
*
@ -24,6 +30,16 @@ const handlerMap = allHandlers.reduce((map, handler) => {
* @returns {(req: Object, res: Object, next: Function, end: Function) => void}
*/
export default 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)) {

View File

@ -2759,6 +2759,10 @@ export default class MetamaskController extends EventEmitter {
{ origin },
{ eth_accounts: {} },
),
requestPermissionsForOrigin: this.permissionController.requestPermissions.bind(
this.permissionController,
{ origin },
),
// Custom RPC-related
addCustomRpc: async ({