mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-30 16:18:07 +01:00
05fae3fa1e
Adds a property, `hookNames`, to each RPC method handler export in `app/scripts/lib/rpc-method-middleware` and a function, `selectHooks`, to select from them. `createMethodMiddleware` receives a giant `opts` object that includes a bunch of different methods from `MetaMaskController` and its subcontrollers. Each method implementation only requires a subset of these methods to do its work. Because they need some kind of name, we call these methods "hooks". With this change, whenever an RPC method is called, `selectHooks` will be called to ensure that each method only receives the hooks that it needs in order to do its job. This implementation is based on [work in `snaps-skunkworks`](https://github.com/MetaMask/snaps-skunkworks/blob/a3e1248/packages/rpc-methods/src/utils.ts#L17-L34) that will be merged in the near future.
45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
import { MESSAGE_TYPE } from '../../../../../shared/constants/app';
|
|
|
|
const watchAsset = {
|
|
methodNames: [MESSAGE_TYPE.WATCH_ASSET, MESSAGE_TYPE.WATCH_ASSET_LEGACY],
|
|
implementation: watchAssetHandler,
|
|
hookNames: {
|
|
handleWatchAssetRequest: true,
|
|
},
|
|
};
|
|
export default watchAsset;
|
|
|
|
/**
|
|
* @typedef {Object} WatchAssetOptions
|
|
* @property {Function} handleWatchAssetRequest - The wallet_watchAsset method implementation.
|
|
*/
|
|
|
|
/**
|
|
* @typedef {Object} WatchAssetParam
|
|
* @property {string} type - The type of the asset to watch.
|
|
* @property {Object} options - Watch options for the asset.
|
|
*/
|
|
|
|
/**
|
|
* @param {import('json-rpc-engine').JsonRpcRequest<WatchAssetParam>} 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 {WatchAssetOptions} options
|
|
*/
|
|
async function watchAssetHandler(
|
|
req,
|
|
res,
|
|
_next,
|
|
end,
|
|
{ handleWatchAssetRequest },
|
|
) {
|
|
try {
|
|
const { options: asset, type } = req.params;
|
|
res.result = await handleWatchAssetRequest(asset, type);
|
|
return end();
|
|
} catch (error) {
|
|
return end(error);
|
|
}
|
|
}
|