diff --git a/app/scripts/lib/rpc-method-middleware/handlers/eth-accounts.js b/app/scripts/lib/rpc-method-middleware/handlers/eth-accounts.js deleted file mode 100644 index ab603e7de021..000000000000 --- a/app/scripts/lib/rpc-method-middleware/handlers/eth-accounts.js +++ /dev/null @@ -1,33 +0,0 @@ -import { MESSAGE_TYPE } from '../../../../../shared/constants/app'; - -/** - * A wrapper for `eth_accounts` that returns an empty array when permission is denied. - */ - -const requestEthereumAccounts = { - methodNames: [MESSAGE_TYPE.ETH_ACCOUNTS], - implementation: ethAccountsHandler, - hookNames: { - getAccounts: true, - }, -}; -export default requestEthereumAccounts; - -/** - * @typedef {Record} EthAccountsOptions - * @property {Function} getAccounts - Gets the accounts for the requesting - * origin. - */ - -/** - * - * @param {import('json-rpc-engine').JsonRpcRequest} _req - The JSON-RPC request object. - * @param {import('json-rpc-engine').JsonRpcResponse} 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 {EthAccountsOptions} options - The RPC method hooks. - */ -async function ethAccountsHandler(_req, res, _next, end, { getAccounts }) { - res.result = await getAccounts(); - return end(); -} diff --git a/app/scripts/lib/rpc-method-middleware/handlers/eth-accounts.ts b/app/scripts/lib/rpc-method-middleware/handlers/eth-accounts.ts new file mode 100644 index 000000000000..003cbd88281b --- /dev/null +++ b/app/scripts/lib/rpc-method-middleware/handlers/eth-accounts.ts @@ -0,0 +1,59 @@ +import type { + JsonRpcEngineEndCallback, + JsonRpcEngineNextCallback, +} from 'json-rpc-engine'; +import type { + JsonRpcRequest, + JsonRpcParams, + PendingJsonRpcResponse, +} from '@metamask/utils'; +import { MESSAGE_TYPE } from '../../../../../shared/constants/app'; +import { AccountAddress } from '../../../controllers/account-order'; +import { HandlerWrapper } from './types'; + +type EthAccountsHandlerOptions = { + getAccounts: () => Promise; +}; + +type EthAccountsConstraint = { + implementation: ( + _req: JsonRpcRequest, + res: PendingJsonRpcResponse, + _next: JsonRpcEngineNextCallback, + end: JsonRpcEngineEndCallback, + { getAccounts }: EthAccountsHandlerOptions, + ) => Promise; +} & HandlerWrapper; + +/** + * A wrapper for `eth_accounts` that returns an empty array when permission is denied. + */ +const ethAccounts = { + methodNames: [MESSAGE_TYPE.ETH_ACCOUNTS], + implementation: ethAccountsHandler, + hookNames: { + getAccounts: true, + }, +} satisfies EthAccountsConstraint; +export default ethAccounts; + +/** + * + * @param _req - The JSON-RPC request object. + * @param res - The JSON-RPC response object. + * @param _next - The json-rpc-engine 'next' callback. + * @param end - The json-rpc-engine 'end' callback. + * @param options - The RPC method hooks. + * @param options.getAccounts - Gets the accounts for the requesting + * origin. + */ +async function ethAccountsHandler( + _req: JsonRpcRequest, + res: PendingJsonRpcResponse, + _next: JsonRpcEngineNextCallback, + end: JsonRpcEngineEndCallback, + { getAccounts }: EthAccountsHandlerOptions, +): Promise { + res.result = await getAccounts(); + return end(); +} diff --git a/app/scripts/lib/rpc-method-middleware/handlers/types.ts b/app/scripts/lib/rpc-method-middleware/handlers/types.ts new file mode 100644 index 000000000000..5b7a2a7494d4 --- /dev/null +++ b/app/scripts/lib/rpc-method-middleware/handlers/types.ts @@ -0,0 +1,6 @@ +import { MessageType } from '../../../../../shared/constants/app'; + +export type HandlerWrapper = { + methodNames: [MessageType] | MessageType[]; + hookNames: Record; +}; diff --git a/shared/constants/app.ts b/shared/constants/app.ts index 70a393626b58..f3717b7cece9 100644 --- a/shared/constants/app.ts +++ b/shared/constants/app.ts @@ -65,6 +65,8 @@ export const MESSAGE_TYPE = { ///: END:ONLY_INCLUDE_IF } as const; +export type MessageType = (typeof MESSAGE_TYPE)[keyof typeof MESSAGE_TYPE]; + ///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps) export const SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES = { confirmAccountCreation: 'snap_manageAccounts:confirmAccountCreation',