Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Typescript conversion of eth-accounts.js #23504

Merged
merged 20 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
NiranjanaBinoy marked this conversation as resolved.
Show resolved Hide resolved
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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, MessageType is resolving to string for me. Investigating...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK IsEqual is evaluating to true, so I'm going to conclude that this is language server flakiness.

type IsEqual = [MessageType, AllMessageTypes] extends [AllMessageTypes, MessageType]
  ? true
  : false;
  
type AllMessageTypes =
  | 'wallet_addEthereumChain'
  | 'eth_accounts'
  | 'eth_decrypt'
  | 'eth_chainId'
  | 'eth_getEncryptionPublicKey'
  | 'eth_getBlockByNumber'
  | 'eth_requestAccounts'
  | 'eth_sendTransaction'
  | 'eth_sendRawTransaction'
  | 'eth_signTransaction'
  | 'eth_signTypedData'
  | 'eth_signTypedData_v1'
  | 'eth_signTypedData_v3'
  | 'eth_signTypedData_v4'
  | 'metamask_getProviderState'
  | 'metamask_logWeb3ShimUsage'
  | 'personal_sign'
  | 'metamask_sendDomainMetadata'
  | 'wallet_switchEthereumChain'
  | 'transaction'
  | 'wallet_requestPermissions'
  | 'wallet_watchAsset'
  | 'metamask_watchAsset'
  | typeof DIALOG_APPROVAL_TYPES.alert
  | typeof DIALOG_APPROVAL_TYPES.confirmation
  | typeof DIALOG_APPROVAL_TYPES.prompt
  | typeof DIALOG_APPROVAL_TYPES.default
  | 'metamaskinstitutional_authenticate'
  | 'metamaskinstitutional_reauthenticate'
  | 'metamaskinstitutional_refresh_token'
  | 'metamaskinstitutional_supported'
  | 'metamaskinstitutional_portfolio'
  | 'metamaskinstitutional_open_swaps'
  | 'metamaskinstitutional_checkIfTokenIsPresent'
  | 'metamaskinstitutional_setAccountAndNetwork'
  | 'metamaskinstitutional_openAddHardwareWallet';


///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
export const SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES = {
confirmAccountCreation: 'snap_manageAccounts:confirmAccountCreation',
Expand Down