-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Typescript conversion of eth-accounts.js (#23504)
- Loading branch information
1 parent
74c1ee2
commit 1329a39
Showing
4 changed files
with
67 additions
and
33 deletions.
There are no files selected for viewing
33 changes: 0 additions & 33 deletions
33
app/scripts/lib/rpc-method-middleware/handlers/eth-accounts.js
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
app/scripts/lib/rpc-method-middleware/handlers/eth-accounts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters