1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 06:07:06 +01:00
metamask-extension/app/scripts/lib/rpc-method-middleware/handlers/send-metadata.js
Erik Marks fc1ffae406
Add subject types (#13026)
This PR introduces the concept of subject _types_ to be associated with each subject in the `SubjectMetadataController`, and used for control flow in our RPC stack (`setupProviderEngine` and so forth).

We already differentiate between "types" of subjects in various places on an ad hoc basis via boolean flags (e.g. `isInternal` in our RPC stack) or the presence/absence of certain values in the subject's metadata (specifically `metadata.extensionId`). The status quo is manageable if not ideal, but will start to become untenable with the introduction of Snaps in the near future.

Therefore, this PR establishes a `SUBJECT_TYPES` enum and adds the `subjectType` property to the metadata of each subject. A new migration is added to accomplish this. Finally, we specify and `INTERNAL` subject type to distinguish internal from external requests.
2021-12-08 15:37:29 -08:00

58 lines
1.6 KiB
JavaScript

import { ethErrors } from 'eth-rpc-errors';
import { MESSAGE_TYPE } from '../../../../../shared/constants/app';
/**
* This internal method is used by our external provider to send metadata about
* permission subjects so that we can e.g. display a proper name and icon in
* our UI.
*/
const sendMetadata = {
methodNames: [MESSAGE_TYPE.SEND_METADATA],
implementation: sendMetadataHandler,
hookNames: {
addSubjectMetadata: true,
subjectType: true,
},
};
export default sendMetadata;
/**
* @typedef {Record<string, Function>} SendMetadataOptions
* @property {Function} addSubjectMetadata - A function that records subject
* metadata, bound to the requesting origin.
* @property {string} subjectType - The type of the requesting origin / subject.
*/
/**
* @param {import('json-rpc-engine').JsonRpcRequest<unknown>} req - The JSON-RPC request object.
* @param {import('json-rpc-engine').JsonRpcResponse<true>} res - The JSON-RPC response object.
* @param {Function} _next - The json-rpc-engine 'next' callback.
* @param {Function} end - The json-rpc-engine 'end' callback.
* @param {SendMetadataOptions} options
*/
function sendMetadataHandler(
req,
res,
_next,
end,
{ addSubjectMetadata, subjectType },
) {
const { params } = req;
if (params && typeof params === 'object' && !Array.isArray(params)) {
const { icon = null, name = null, ...remainingParams } = params;
addSubjectMetadata({
...remainingParams,
iconUrl: icon,
name,
subjectType,
});
} else {
return end(ethErrors.rpc.invalidParams({ data: params }));
}
res.result = true;
return end();
}