Skip to content

Commit eedeb24

Browse files
authored
chore(deps): upgrade from json-rpc-engine to @metamask/json-rpc-engine (#22875)
## **Description** ## **Related issues** - #27784 - MetaMask/eth-json-rpc-middleware#335 - #27917 - #18510 - #15250 - MetaMask/metamask-improvement-proposals#36 ### Blocked by - [x] #24496 ### Follow-up to - #24496 ## **Manual testing steps** ## **Screenshots/Recordings** ### **Before** ### **After** ## **Pre-merge author checklist** - [x] I’ve followed [MetaMask Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [x] I've clearly explained what problem this PR is solving and how it is solved. - [x] I've linked related issues - [x] I've included manual testing steps - [x] I've included screenshots/recordings if applicable - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. - [x] I’ve properly set the pull request status: - [x] In case it's not yet "ready for review", I've set it to "draft". - [x] In case it's "ready for review", I've changed it from "draft" to "non-draft". ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
1 parent 776bc1e commit eedeb24

33 files changed

+535
-242
lines changed

app/scripts/controllers/preferences-controller.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import {
66
AccountsControllerSetSelectedAccountAction,
77
AccountsControllerState,
88
} from '@metamask/accounts-controller';
9-
import { Hex } from '@metamask/utils';
9+
import { Hex, Json } from '@metamask/utils';
1010
import {
1111
BaseController,
1212
ControllerGetStateAction,
1313
ControllerStateChangeEvent,
1414
RestrictedControllerMessenger,
1515
} from '@metamask/base-controller';
16-
import { Json } from 'json-rpc-engine';
1716
import { NetworkControllerGetStateAction } from '@metamask/network-controller';
1817
import {
1918
ETHERSCAN_SUPPORTED_CHAIN_IDS,

app/scripts/lib/createEvmMethodsToNonEvmAccountReqFilterMiddleware.test.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { jsonrpc2 } from '@metamask/utils';
1+
import { jsonrpc2, Json } from '@metamask/utils';
22
import { BtcAccountType, EthAccountType } from '@metamask/keyring-api';
3-
import { Json } from 'json-rpc-engine';
3+
import type { JsonRpcParams, JsonRpcRequest } from '@metamask/utils';
44
import createEvmMethodsToNonEvmAccountReqFilterMiddleware, {
55
EvmMethodsToNonEvmAccountFilterMessenger,
66
} from './createEvmMethodsToNonEvmAccountReqFilterMiddleware';
77

88
describe('createEvmMethodsToNonEvmAccountReqFilterMiddleware', () => {
9-
const getMockRequest = (method: string, params?: Json) => ({
9+
const getMockRequest = (method: string, params: Json) => ({
1010
jsonrpc: jsonrpc2,
1111
id: 1,
1212
method,
@@ -20,185 +20,221 @@ describe('createEvmMethodsToNonEvmAccountReqFilterMiddleware', () => {
2020
{
2121
accountType: BtcAccountType.P2wpkh,
2222
method: 'eth_accounts',
23+
params: undefined,
2324
calledNext: false,
2425
},
2526
{
2627
accountType: BtcAccountType.P2wpkh,
2728
method: 'eth_sendRawTransaction',
29+
params: undefined,
2830
calledNext: false,
2931
},
3032
{
3133
accountType: BtcAccountType.P2wpkh,
3234
method: 'eth_sendTransaction',
35+
params: undefined,
3336
calledNext: false,
3437
},
3538
{
3639
accountType: BtcAccountType.P2wpkh,
3740
method: 'eth_signTypedData',
41+
params: undefined,
3842
calledNext: false,
3943
},
4044
{
4145
accountType: BtcAccountType.P2wpkh,
4246
method: 'eth_signTypedData_v1',
47+
params: undefined,
4348
calledNext: false,
4449
},
4550
{
4651
accountType: BtcAccountType.P2wpkh,
4752
method: 'eth_signTypedData_v3',
53+
params: undefined,
4854
calledNext: false,
4955
},
5056
{
5157
accountType: BtcAccountType.P2wpkh,
5258
method: 'eth_signTypedData_v4',
59+
params: undefined,
5360
calledNext: false,
5461
},
5562
{
5663
accountType: EthAccountType.Eoa,
5764
method: 'eth_accounts',
65+
params: undefined,
5866
calledNext: true,
5967
},
6068
{
6169
accountType: EthAccountType.Eoa,
6270
method: 'eth_sendRawTransaction',
71+
params: undefined,
6372
calledNext: true,
6473
},
6574
{
6675
accountType: EthAccountType.Eoa,
6776
method: 'eth_sendTransaction',
77+
params: undefined,
6878
calledNext: true,
6979
},
7080
{
7181
accountType: EthAccountType.Eoa,
7282
method: 'eth_signTypedData',
83+
params: undefined,
7384
calledNext: true,
7485
},
7586
{
7687
accountType: EthAccountType.Eoa,
7788
method: 'eth_signTypedData_v1',
89+
params: undefined,
7890
calledNext: true,
7991
},
8092
{
8193
accountType: EthAccountType.Eoa,
8294
method: 'eth_signTypedData_v3',
95+
params: undefined,
8396
calledNext: true,
8497
},
8598
{
8699
accountType: EthAccountType.Eoa,
87100
method: 'eth_signTypedData_v4',
101+
params: undefined,
88102
calledNext: true,
89103
},
90104

91105
// EVM requests not associated with an account
92106
{
93107
accountType: BtcAccountType.P2wpkh,
94108
method: 'eth_blockNumber',
109+
params: undefined,
95110
calledNext: true,
96111
},
97112
{
98113
accountType: BtcAccountType.P2wpkh,
99114
method: 'eth_chainId',
115+
params: undefined,
100116
calledNext: true,
101117
},
102118
{
103119
accountType: EthAccountType.Eoa,
104120
method: 'eth_blockNumber',
121+
params: undefined,
105122
calledNext: true,
106123
},
107124
{
108125
accountType: EthAccountType.Eoa,
109126
method: 'eth_chainId',
127+
params: undefined,
110128
calledNext: true,
111129
},
112130

113131
// other requests
114132
{
115133
accountType: BtcAccountType.P2wpkh,
116134
method: 'wallet_getSnaps',
135+
params: undefined,
117136
calledNext: true,
118137
},
119138
{
120139
accountType: BtcAccountType.P2wpkh,
121140
method: 'wallet_invokeSnap',
141+
params: undefined,
122142
calledNext: true,
123143
},
124144
{
125145
accountType: BtcAccountType.P2wpkh,
126146
method: 'wallet_requestSnaps',
147+
params: undefined,
127148
calledNext: true,
128149
},
129150
{
130151
accountType: BtcAccountType.P2wpkh,
131152
method: 'snap_getClientStatus',
153+
params: undefined,
132154
calledNext: true,
133155
},
134156
{
135157
accountType: BtcAccountType.P2wpkh,
136158
method: 'wallet_addEthereumChain',
159+
params: undefined,
137160
calledNext: true,
138161
},
139162
{
140163
accountType: BtcAccountType.P2wpkh,
141164
method: 'wallet_getPermissions',
165+
params: undefined,
142166
calledNext: true,
143167
},
144168
{
145169
accountType: BtcAccountType.P2wpkh,
146170
method: 'wallet_requestPermissions',
171+
params: undefined,
147172
calledNext: true,
148173
},
149174
{
150175
accountType: BtcAccountType.P2wpkh,
151176
method: 'wallet_revokePermissions',
177+
params: undefined,
152178
calledNext: true,
153179
},
154180
{
155181
accountType: BtcAccountType.P2wpkh,
156182
method: 'wallet_switchEthereumChain',
183+
params: undefined,
157184
calledNext: true,
158185
},
159186
{
160187
accountType: EthAccountType.Eoa,
161188
method: 'wallet_getSnaps',
189+
params: undefined,
162190
calledNext: true,
163191
},
164192
{
165193
accountType: EthAccountType.Eoa,
166194
method: 'wallet_invokeSnap',
195+
params: undefined,
167196
calledNext: true,
168197
},
169198
{
170199
accountType: EthAccountType.Eoa,
171200
method: 'wallet_requestSnaps',
201+
params: undefined,
172202
calledNext: true,
173203
},
174204
{
175205
accountType: EthAccountType.Eoa,
176206
method: 'snap_getClientStatus',
207+
params: undefined,
177208
calledNext: true,
178209
},
179210
{
180211
accountType: EthAccountType.Eoa,
181212
method: 'wallet_addEthereumChain',
213+
params: undefined,
182214
calledNext: true,
183215
},
184216
{
185217
accountType: EthAccountType.Eoa,
186218
method: 'wallet_getPermissions',
219+
params: undefined,
187220
calledNext: true,
188221
},
189222
{
190223
accountType: EthAccountType.Eoa,
191224
method: 'wallet_requestPermissions',
225+
params: undefined,
192226
calledNext: true,
193227
},
194228
{
195229
accountType: EthAccountType.Eoa,
196230
method: 'wallet_revokePermissions',
231+
params: undefined,
197232
calledNext: true,
198233
},
199234
{
200235
accountType: EthAccountType.Eoa,
201236
method: 'wallet_switchEthereumChain',
237+
params: undefined,
202238
calledNext: true,
203239
},
204240

@@ -250,7 +286,7 @@ describe('createEvmMethodsToNonEvmAccountReqFilterMiddleware', () => {
250286
}: {
251287
accountType: EthAccountType | BtcAccountType;
252288
method: string;
253-
params?: Json;
289+
params: Json;
254290
calledNext: number;
255291
}) => {
256292
const filterFn = createEvmMethodsToNonEvmAccountReqFilterMiddleware({
@@ -262,7 +298,7 @@ describe('createEvmMethodsToNonEvmAccountReqFilterMiddleware', () => {
262298
const mockEnd = jest.fn();
263299

264300
filterFn(
265-
getMockRequest(method, params),
301+
getMockRequest(method, params) as JsonRpcRequest<JsonRpcParams>,
266302
getMockResponse(),
267303
mockNext,
268304
mockEnd,

app/scripts/lib/createEvmMethodsToNonEvmAccountReqFilterMiddleware.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { isEvmAccountType } from '@metamask/keyring-api';
22
import { RestrictedControllerMessenger } from '@metamask/base-controller';
33
import { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';
4-
import { JsonRpcMiddleware } from 'json-rpc-engine';
4+
import { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
5+
import type { Json, JsonRpcParams } from '@metamask/utils';
56
import { RestrictedEthMethods } from '../../../shared/constants/permissions';
67
import { unrestrictedEthSigningMethods } from '../controllers/permissions';
78

@@ -32,7 +33,7 @@ export default function createEvmMethodsToNonEvmAccountReqFilterMiddleware({
3233
messenger,
3334
}: {
3435
messenger: EvmMethodsToNonEvmAccountFilterMessenger;
35-
}): JsonRpcMiddleware<unknown, void> {
36+
}): JsonRpcMiddleware<JsonRpcParams, Json> {
3637
return function filterEvmRequestToNonEvmAccountsMiddleware(
3738
req,
3839
_res,
@@ -74,7 +75,13 @@ export default function createEvmMethodsToNonEvmAccountReqFilterMiddleware({
7475
// TODO: Convert this to superstruct schema
7576
const isWalletRequestPermission =
7677
req.method === 'wallet_requestPermissions';
77-
if (isWalletRequestPermission && req?.params && Array.isArray(req.params)) {
78+
if (
79+
isWalletRequestPermission &&
80+
req?.params &&
81+
Array.isArray(req.params) &&
82+
req.params.length > 0 &&
83+
req.params[0]
84+
) {
7885
const permissionsMethodRequest = Object.keys(req.params[0]);
7986

8087
const isEvmPermissionRequest = METHODS_TO_CHECK.some((method) =>

app/scripts/lib/createMetamaskMiddleware.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { createScaffoldMiddleware, mergeMiddleware } from 'json-rpc-engine';
1+
import {
2+
createScaffoldMiddleware,
3+
mergeMiddleware,
4+
} from '@metamask/json-rpc-engine';
25
import { createWalletMiddleware } from '@metamask/eth-json-rpc-middleware';
36
import {
47
createPendingNonceMiddleware,

app/scripts/lib/createRPCMethodTrackingMiddleware.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
PRIMARY_TYPES_PERMIT,
1919
} from '../../../shared/constants/signatures';
2020
import { SIGNING_METHODS } from '../../../shared/constants/transaction';
21+
import { getErrorMessage } from '../../../shared/modules/error';
2122
import {
2223
generateSignatureUniqueId,
2324
getBlockaidMetricsProps,
@@ -419,15 +420,20 @@ export default function createRPCMethodTrackingMiddleware({
419420
const location = res.error?.data?.location;
420421

421422
let event;
423+
424+
const errorMessage = getErrorMessage(res.error);
425+
422426
if (res.error?.code === errorCodes.provider.userRejectedRequest) {
423427
event = eventType.REJECTED;
424428
} else if (
425429
res.error?.code === errorCodes.rpc.internal &&
426-
res.error?.message === 'Request rejected by user or snap.'
430+
[errorMessage, res.error.message].includes(
431+
'Request rejected by user or snap.',
432+
)
427433
) {
428434
// The signature was approved in MetaMask but rejected in the snap
429435
event = eventType.REJECTED;
430-
eventProperties.status = res.error.message;
436+
eventProperties.status = errorMessage;
431437
} else {
432438
event = eventType.APPROVED;
433439
}

app/scripts/lib/middleware/pending.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createAsyncMiddleware } from 'json-rpc-engine';
1+
import { createAsyncMiddleware } from '@metamask/json-rpc-engine';
22
import { formatTxMetaForRpcResult } from '../util';
33

44
export function createPendingNonceMiddleware({ getPendingNonce }) {

app/scripts/lib/rpc-method-middleware/createMethodMiddleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function makeMethodMiddlewareMaker(handlers) {
4242
*
4343
* @param {Record<string, (...args: unknown[]) => unknown | Promise<unknown>>} hooks - Required "hooks" into our
4444
* controllers.
45-
* @returns {import('json-rpc-engine').JsonRpcMiddleware<unknown, unknown>} The method middleware function.
45+
* @returns {import('@metamask/json-rpc-engine').JsonRpcMiddleware<unknown, unknown>} The method middleware function.
4646
*/
4747
const makeMethodMiddleware = (hooks) => {
4848
assertExpectedHook(hooks, expectedHookNames);

app/scripts/lib/rpc-method-middleware/createMethodMiddleware.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { JsonRpcEngine } from 'json-rpc-engine';
1+
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
22
import {
33
assertIsJsonRpcFailure,
44
assertIsJsonRpcSuccess,
@@ -140,6 +140,7 @@ describe.each([
140140
assertIsJsonRpcFailure(response);
141141

142142
expect(response.error.message).toBe('test error');
143+
expect(response.error.data.cause.message).toBe('test error');
143144
});
144145

145146
it('should handle errors thrown by the implementation', async () => {
@@ -156,6 +157,7 @@ describe.each([
156157
assertIsJsonRpcFailure(response);
157158

158159
expect(response.error.message).toBe('test error');
160+
expect(response.error.data.cause.message).toBe('test error');
159161
});
160162

161163
it('should handle non-errors thrown by the implementation', async () => {

0 commit comments

Comments
 (0)