Skip to content

Commit b469c1f

Browse files
authored
refactor: fully extract middleware into EIP-5792 Middleware package (#6477)
## Explanation The newly created EIP-5792 Middleware package ([PR #6422](#6422)) currently contains only the hooks required for the existing wallet middleware. To align with the intended abstraction and reduce the monolithic nature of wallet-middleware, we should fully extract the relevant middleware logic from eth-json-rpc-middleware ([GitHub - MetaMask/eth-json-rpc-middleware: Ethereum middleware for composing an Ethereum provider using json-rpc-engine. Intended to replace provider-engine](https://github.com/MetaMask/eth-json-rpc-middleware)) into this new package. This PR moves the identified middleware into @metamask/eip-5792-middleware. <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> * Fixes https://consensyssoftware.atlassian.net/browse/WAPI-691 ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent 5d0e61d commit b469c1f

22 files changed

+975
-37
lines changed

eslint-warning-thresholds.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
"packages/ens-controller/src/EnsController.ts": {
154154
"jsdoc/check-tag-names": 6
155155
},
156-
"packages/eip-5792-middleware/src/methods/processSendCalls.ts": {
156+
"packages/eip-5792-middleware/src/hooks/processSendCalls.ts": {
157157
"@typescript-eslint/no-misused-promises": 1
158158
},
159159
"packages/eth-json-rpc-provider/src/safe-event-emitter-provider.test.ts": {

packages/eip-5792-middleware/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add and export EIP-5792 RPC method handler middlewares and utility types ([#6477](https://github.com/MetaMask/core/pull/6477))
13+
1014
## [1.0.0]
1115

1216
### Added

packages/eip-5792-middleware/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"dependencies": {
5050
"@metamask/eth-json-rpc-middleware": "^17.0.1",
51+
"@metamask/superstruct": "^3.1.0",
5152
"@metamask/transaction-controller": "^60.2.0",
5253
"@metamask/utils": "^11.4.2",
5354
"uuid": "^8.3.2"
@@ -59,6 +60,7 @@
5960
"@types/jest": "^27.4.1",
6061
"deepmerge": "^4.2.2",
6162
"jest": "^27.5.1",
63+
"klona": "^2.0.6",
6264
"ts-jest": "^27.1.4",
6365
"typedoc": "^0.24.8",
6466
"typedoc-plugin-missing-exports": "^2.0.0",

packages/eip-5792-middleware/src/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,12 @@ export enum EIP5792ErrorCode {
1818
UnknownBundleId = 5730,
1919
RejectedUpgrade = 5750,
2020
}
21+
22+
// wallet_getCallStatus
23+
export enum GetCallsStatusCode {
24+
PENDING = 100,
25+
CONFIRMED = 200,
26+
FAILED_OFFCHAIN = 400,
27+
REVERTED = 500,
28+
REVERTED_PARTIAL = 600,
29+
}

packages/eip-5792-middleware/src/methods/getCallsStatus.test.ts renamed to packages/eip-5792-middleware/src/hooks/getCallsStatus.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Messenger } from '@metamask/base-controller';
2-
import { GetCallsStatusCode } from '@metamask/eth-json-rpc-middleware';
32
import { TransactionStatus } from '@metamask/transaction-controller';
43
import type {
54
TransactionControllerGetStateAction,
65
TransactionControllerState,
76
} from '@metamask/transaction-controller';
87

98
import { getCallsStatus } from './getCallsStatus';
9+
import { GetCallsStatusCode } from '../constants';
1010
import type { EIP5792Messenger } from '../types';
1111

1212
const CHAIN_ID_MOCK = '0x123';

packages/eip-5792-middleware/src/methods/getCallsStatus.ts renamed to packages/eip-5792-middleware/src/hooks/getCallsStatus.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { GetCallsStatusResult } from '@metamask/eth-json-rpc-middleware';
2-
import { GetCallsStatusCode } from '@metamask/eth-json-rpc-middleware';
31
import { JsonRpcError } from '@metamask/rpc-errors';
42
import type {
53
Log,
@@ -9,8 +7,8 @@ import type {
97
import { TransactionStatus } from '@metamask/transaction-controller';
108
import type { Hex } from '@metamask/utils';
119

12-
import { EIP5792ErrorCode, VERSION } from '../constants';
13-
import type { EIP5792Messenger } from '../types';
10+
import { EIP5792ErrorCode, GetCallsStatusCode, VERSION } from '../constants';
11+
import type { EIP5792Messenger, GetCallsStatusResult } from '../types';
1412

1513
/**
1614
* Retrieves the status of a transaction batch by its ID.
File renamed without changes.

packages/eip-5792-middleware/src/methods/getCapabilities.ts renamed to packages/eip-5792-middleware/src/hooks/getCapabilities.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { GetCapabilitiesResult } from '@metamask/eth-json-rpc-middleware';
21
import type {
32
IsAtomicBatchSupportedResult,
43
IsAtomicBatchSupportedResultEntry,
@@ -7,7 +6,7 @@ import type {
76
import type { Hex } from '@metamask/utils';
87

98
import { KEYRING_TYPES_SUPPORTING_7702 } from '../constants';
10-
import type { EIP5792Messenger } from '../types';
9+
import type { EIP5792Messenger, GetCapabilitiesResult } from '../types';
1110
import { getAccountKeyringType } from '../utils';
1211

1312
/**

packages/eip-5792-middleware/src/methods/processSendCalls.test.ts renamed to packages/eip-5792-middleware/src/hooks/processSendCalls.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import type {
44
AccountsControllerState,
55
} from '@metamask/accounts-controller';
66
import { Messenger } from '@metamask/base-controller';
7-
import type {
8-
SendCalls,
9-
SendCallsParams,
10-
} from '@metamask/eth-json-rpc-middleware';
117
import { KeyringTypes } from '@metamask/keyring-controller';
128
import type { InternalAccount } from '@metamask/keyring-internal-api';
139
import type {
@@ -19,7 +15,11 @@ import type { TransactionController } from '@metamask/transaction-controller';
1915
import type { JsonRpcRequest } from '@metamask/utils';
2016

2117
import { processSendCalls } from './processSendCalls';
22-
import type { EIP5792Messenger } from '../types';
18+
import type {
19+
SendCallsPayload,
20+
SendCallsParams,
21+
EIP5792Messenger,
22+
} from '../types';
2323

2424
const CHAIN_ID_MOCK = '0x123';
2525
const CHAIN_ID_2_MOCK = '0xabc';
@@ -31,7 +31,7 @@ const FROM_MOCK_SIMPLE = '0x789abc';
3131
const ORIGIN_MOCK = 'test.com';
3232
const DELEGATION_ADDRESS_MOCK = '0x1234567890abcdef1234567890abcdef12345678';
3333

34-
const SEND_CALLS_MOCK: SendCalls = {
34+
const SEND_CALLS_MOCK: SendCallsPayload = {
3535
version: '2.0.0',
3636
calls: [{ to: '0x123' }, { to: '0x456' }],
3737
chainId: CHAIN_ID_MOCK,

packages/eip-5792-middleware/src/methods/processSendCalls.ts renamed to packages/eip-5792-middleware/src/hooks/processSendCalls.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import type {
2-
SendCalls,
3-
SendCallsResult,
4-
} from '@metamask/eth-json-rpc-middleware';
51
import type { KeyringTypes } from '@metamask/keyring-controller';
62
import { JsonRpcError, rpcErrors } from '@metamask/rpc-errors';
73
import type {
@@ -22,7 +18,11 @@ import {
2218
MessageType,
2319
VERSION,
2420
} from '../constants';
25-
import type { EIP5792Messenger } from '../types';
21+
import type {
22+
EIP5792Messenger,
23+
SendCallsPayload,
24+
SendCallsResult,
25+
} from '../types';
2626
import { getAccountKeyringType } from '../utils';
2727

2828
/**
@@ -67,7 +67,7 @@ export type ProcessSendCallsRequest = JsonRpcRequest & {
6767
export async function processSendCalls(
6868
hooks: ProcessSendCallsHooks,
6969
messenger: EIP5792Messenger,
70-
params: SendCalls,
70+
params: SendCallsPayload,
7171
req: ProcessSendCallsRequest,
7272
): Promise<SendCallsResult> {
7373
const {
@@ -159,7 +159,7 @@ async function processSingleTransaction({
159159
networkClientId: string;
160160
origin?: string;
161161
securityAlertId: string;
162-
sendCalls: SendCalls;
162+
sendCalls: SendCallsPayload;
163163
transactions: { params: BatchTransactionParams }[];
164164
validateSecurity: (
165165
securityRequest: ValidateSecurityRequest,
@@ -232,7 +232,7 @@ async function processMultipleTransaction({
232232
messenger: EIP5792Messenger;
233233
networkClientId: string;
234234
origin?: string;
235-
sendCalls: SendCalls;
235+
sendCalls: SendCallsPayload;
236236
securityAlertId: string;
237237
transactions: { params: BatchTransactionParams }[];
238238
validateSecurity: (
@@ -288,7 +288,7 @@ function generateBatchId(): Hex {
288288
* @param sendCalls - The sendCalls request to validate.
289289
* @param dappChainId - The chain ID that the dApp is connected to.
290290
*/
291-
function validateSingleSendCall(sendCalls: SendCalls, dappChainId: Hex) {
291+
function validateSingleSendCall(sendCalls: SendCallsPayload, dappChainId: Hex) {
292292
validateSendCallsVersion(sendCalls);
293293
validateCapabilities(sendCalls);
294294
validateDappChainId(sendCalls, dappChainId);
@@ -304,7 +304,7 @@ function validateSingleSendCall(sendCalls: SendCalls, dappChainId: Hex) {
304304
* @param keyringType - The type of keyring associated with the account.
305305
*/
306306
function validateSendCalls(
307-
sendCalls: SendCalls,
307+
sendCalls: SendCallsPayload,
308308
dappChainId: Hex,
309309
dismissSmartAccountSuggestionEnabled: boolean,
310310
chainBatchSupport: IsAtomicBatchSupportedResultEntry | undefined,
@@ -326,7 +326,7 @@ function validateSendCalls(
326326
* @param sendCalls - The sendCalls request to validate.
327327
* @throws JsonRpcError if the version is not supported.
328328
*/
329-
function validateSendCallsVersion(sendCalls: SendCalls) {
329+
function validateSendCallsVersion(sendCalls: SendCallsPayload) {
330330
const { version } = sendCalls;
331331

332332
if (version !== VERSION) {
@@ -343,7 +343,7 @@ function validateSendCallsVersion(sendCalls: SendCalls) {
343343
* @param dappChainId - The chain ID that the dApp is connected to
344344
* @throws JsonRpcError if the chain IDs don't match
345345
*/
346-
function validateDappChainId(sendCalls: SendCalls, dappChainId: Hex) {
346+
function validateDappChainId(sendCalls: SendCallsPayload, dappChainId: Hex) {
347347
const { chainId: requestChainId } = sendCalls;
348348

349349
if (
@@ -365,7 +365,7 @@ function validateDappChainId(sendCalls: SendCalls, dappChainId: Hex) {
365365
* @throws JsonRpcError if the chain ID doesn't match or EIP-7702 is not supported
366366
*/
367367
function validateSendCallsChainId(
368-
sendCalls: SendCalls,
368+
sendCalls: SendCallsPayload,
369369
dappChainId: Hex,
370370
chainBatchSupport: IsAtomicBatchSupportedResultEntry | undefined,
371371
) {
@@ -384,7 +384,7 @@ function validateSendCallsChainId(
384384
* @param sendCalls - The sendCalls request to validate.
385385
* @throws JsonRpcError if unsupported non-optional capabilities are requested.
386386
*/
387-
function validateCapabilities(sendCalls: SendCalls) {
387+
function validateCapabilities(sendCalls: SendCallsPayload) {
388388
const { calls, capabilities } = sendCalls;
389389

390390
const requiredTopLevelCapabilities = Object.keys(capabilities ?? {}).filter(

0 commit comments

Comments
 (0)