Skip to content

Commit

Permalink
refactor: Typescript conversion of eth-accounts.js (#23504)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiranjanaBinoy authored Sep 23, 2024
1 parent 74c1ee2 commit 1329a39
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 33 deletions.
33 changes: 0 additions & 33 deletions app/scripts/lib/rpc-method-middleware/handlers/eth-accounts.js

This file was deleted.

59 changes: 59 additions & 0 deletions app/scripts/lib/rpc-method-middleware/handlers/eth-accounts.ts
Original file line number Diff line number Diff line change
@@ -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<AccountAddress[]>;
};

type EthAccountsConstraint<Params extends JsonRpcParams = JsonRpcParams> = {
implementation: (
_req: JsonRpcRequest<Params>,
res: PendingJsonRpcResponse<AccountAddress[]>,
_next: JsonRpcEngineNextCallback,
end: JsonRpcEngineEndCallback,
{ getAccounts }: EthAccountsHandlerOptions,
) => Promise<void>;
} & 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<Params extends JsonRpcParams = JsonRpcParams>(
_req: JsonRpcRequest<Params>,
res: PendingJsonRpcResponse<AccountAddress[]>,
_next: JsonRpcEngineNextCallback,
end: JsonRpcEngineEndCallback,
{ getAccounts }: EthAccountsHandlerOptions,
): Promise<void> {
res.result = await getAccounts();
return end();
}
6 changes: 6 additions & 0 deletions app/scripts/lib/rpc-method-middleware/handlers/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MessageType } from '../../../../../shared/constants/app';

export type HandlerWrapper = {
methodNames: [MessageType] | MessageType[];
hookNames: Record<string, boolean>;
};
2 changes: 2 additions & 0 deletions shared/constants/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 1329a39

Please sign in to comment.