Skip to content

Commit d967a76

Browse files
legobeatMarioAslautommasinismilingkylansethkfman
authored
fix: replace legacy eth-json-rpc deps (#11952)
## **Description** - This PR is a rebase of #10098, including: * #9925 * #9930 - Bump `@metamask/eth-json-rpc-filters` to `^7.0.0` - Bump `@metamask/json-rpc-engine` to `^10.0.0` - Bump `@metamask/eth-json-rpc-middleware` to `^15.0.0` - Migrate from `json-rpc-middleware-stream` to `@metamask/json-rpc-middleware-stream` - Upgrade `@metamask/providers` from v13 to v16 - Also broken out separately as #12085 - Revert `Internal JSON-RPC error` message change to accomodate for `@metamask/rpc-errors` v7 ## **Related issues** Expected to fix the following issues: - [x] #11163 - [x] #11129 - [ ] #11105 - [ ] #9715 - [ ] #8308 - [x] #7926 - [x] #4621 - [x] #4646 - [ ] #12634 #### Blocked by - [x] #12085 - [x] #12047 - [x] #12024 - [x] #11980 - [x] #12008 - [x] #11978 ## **Manual testing steps** 1. Go to in-app browser 2. Test connect with multiple dapps 3. Perform transaciton on test dapp 1. Go to this page... ## **Screenshots/Recordings** https://github.com/MetaMask/metamask-mobile/assets/46944231/c608d957-6684-40e2-8963-67a11dc610df ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [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-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **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. --------- Co-authored-by: Aslau Mario-Daniel <marioaslau@gmail.com> Co-authored-by: tommasini <tommasini15@gmail.com> Co-authored-by: kylanhurt <kylan.hurt@gmail.com> Co-authored-by: sethkfman <Seth.Kaufman@consensys.net> Co-authored-by: Nicolas MASSART <nicolas.massart@consensys.net> Co-authored-by: Nico MASSART <NicolasMassart@users.noreply.github.com>
1 parent fd13173 commit d967a76

File tree

31 files changed

+120
-147
lines changed

31 files changed

+120
-147
lines changed

app/core/BackgroundBridge/BackgroundBridge.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* eslint-disable import/no-commonjs */
22
import URL from 'url-parse';
3-
import { JsonRpcEngine } from 'json-rpc-engine';
43
import {
54
createSelectedNetworkMiddleware,
65
METAMASK_DOMAIN,
76
} from '@metamask/selected-network-controller';
87
import EthQuery from '@metamask/eth-query';
8+
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
99
import MobilePortStream from '../MobilePortStream';
1010
import { setupMultiplex } from '../../util/streams';
1111
import {
@@ -25,10 +25,10 @@ import snapMethodMiddlewareBuilder from '../Snaps/SnapsMethodMiddleware';
2525
import { SubjectType } from '@metamask/permission-controller';
2626
///: END:ONLY_INCLUDE_IF
2727

28+
import { createEngineStream } from '@metamask/json-rpc-middleware-stream';
2829
const createFilterMiddleware = require('@metamask/eth-json-rpc-filters');
2930
const createSubscriptionManager = require('@metamask/eth-json-rpc-filters/subscriptionManager');
30-
const { providerAsMiddleware } = require('@metamask/eth-json-rpc-middleware');
31-
import { createEngineStream } from '@metamask/json-rpc-middleware-stream';
31+
import { providerAsMiddleware } from '@metamask/eth-json-rpc-middleware';
3232
const pump = require('pump');
3333
// eslint-disable-next-line import/no-nodejs-modules
3434
const EventEmitter = require('events').EventEmitter;
@@ -388,11 +388,7 @@ export class BackgroundBridge extends EventEmitter {
388388

389389
pump(outStream, providerStream, outStream, (err) => {
390390
// handle any middleware cleanup
391-
this.engine._middleware.forEach((mid) => {
392-
if (mid.destroy && typeof mid.destroy === 'function') {
393-
mid.destroy();
394-
}
395-
});
391+
this.engine.destroy();
396392
if (err) Logger.log('Error with provider stream conn', err);
397393
});
398394
}

app/core/Encryptor/pbkdf2.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('pbkdf2', () => {
99
afterEach(() => {
1010
jest.restoreAllMocks();
1111
});
12-
12+
1313
it('uses the native implementation of pbkdf2 with main aes', async () => {
1414
NativeModules.Aes.pbkdf2 = jest
1515
.fn()
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export { createRemoteFeatureFlagController } from './utils';
2-

app/core/Engine/controllers/RemoteFeatureFlagController/types.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { RemoteFeatureFlagControllerMessenger, RemoteFeatureFlagControllerState
22

33
export interface RemoteFeatureFlagInitParamTypes {
44
state?: RemoteFeatureFlagControllerState;
5-
messenger: RemoteFeatureFlagControllerMessenger,
6-
disabled: boolean
5+
messenger: RemoteFeatureFlagControllerMessenger;
6+
disabled: boolean;
77
}
8-

app/core/Engine/controllers/RemoteFeatureFlagController/utils.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ describe('RemoteFeatureFlagController utils', () => {
1616

1717
describe('createRemoteFeatureFlagController', () => {
1818
it('creates controller with initial undefined state', () => {
19-
2019
const controller = createRemoteFeatureFlagController({
2120
state: undefined,
2221
messenger,

app/core/Engine/controllers/RemoteFeatureFlagController/utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export const createRemoteFeatureFlagController = ({
3434
messenger,
3535
disabled,
3636
}: RemoteFeatureFlagInitParamTypes) => {
37-
3837
const remoteFeatureFlagController = new RemoteFeatureFlagController({
3938
messenger,
4039
state,
@@ -58,4 +57,3 @@ export const createRemoteFeatureFlagController = ({
5857
}
5958
return remoteFeatureFlagController;
6059
};
61-

app/core/Ledger/Ledger.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,3 @@ export const unlockLedgerWalletAccount = async (index: number) => {
251251
}
252252
};
253253

254-

app/core/RPCMethods/RPCMethodMiddleware.test.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import {
2-
JsonRpcEngine,
1+
import { JsonRpcEngine, JsonRpcMiddleware } from '@metamask/json-rpc-engine';
2+
import type {
3+
Json,
34
JsonRpcFailure,
4-
JsonRpcMiddleware,
5+
JsonRpcParams,
56
JsonRpcRequest,
67
JsonRpcResponse,
78
JsonRpcSuccess,
8-
} from 'json-rpc-engine';
9+
} from '@metamask/utils';
10+
import { type JsonRpcError, providerErrors, rpcErrors } from '@metamask/rpc-errors';
911
import type { TransactionParams } from '@metamask/transaction-controller';
10-
import { providerErrors, rpcErrors } from '@metamask/rpc-errors';
1112
import Engine from '../Engine';
1213
import { store } from '../../store';
1314
import { getPermittedAccounts } from '../Permissions';
@@ -106,8 +107,8 @@ const jsonrpc = '2.0' as const;
106107
* @throws If the given value is not a valid {@link JsonRpcSuccess} object.
107108
*/
108109
function assertIsJsonRpcSuccess(
109-
response: JsonRpcResponse<unknown>,
110-
): asserts response is JsonRpcSuccess<unknown> {
110+
response: JsonRpcResponse<Json>,
111+
): asserts response is JsonRpcSuccess<Json> {
111112
if ('error' in response) {
112113
throw new Error(`Response failed with error '${JSON.stringify('error')}'`);
113114
} else if (!('result' in response)) {
@@ -208,8 +209,8 @@ async function callMiddleware({
208209
middleware,
209210
request,
210211
}: {
211-
middleware: JsonRpcMiddleware<unknown, unknown>;
212-
request: JsonRpcRequest<unknown>;
212+
middleware: JsonRpcMiddleware<JsonRpcParams, Json>;
213+
request: JsonRpcRequest<JsonRpcParams>;
213214
}) {
214215
const engine = new JsonRpcEngine();
215216
engine.push(middleware);
@@ -415,7 +416,6 @@ describe('getRpcMethodMiddleware', () => {
415416
permissionController.createPermissionMiddleware({
416417
origin: hostMock,
417418
});
418-
// @ts-expect-error JsonRpcId (number | string | void) doesn't match PS middleware's id, which is (string | number | null)
419419
engine.push(permissionMiddleware);
420420
const middleware = getRpcMethodMiddleware(getMinimalOptions());
421421
engine.push(middleware);
@@ -1096,7 +1096,8 @@ describe('getRpcMethodMiddleware', () => {
10961096
it('returns a JSON-RPC error if an error is thrown when adding this transaction', async () => {
10971097
// Omit `from` and `chainId` here to skip validation for simplicity
10981098
// Downcast needed here because `from` is required by this type
1099-
const mockTransactionParameters = {} as TransactionParams;
1099+
const mockTransactionParameters = {} as (TransactionParams &
1100+
JsonRpcParams)[];
11001101
// Transaction fails before returning a result
11011102
mockAddTransaction.mockImplementation(async () => {
11021103
throw new Error('Failed to add transaction');
@@ -1119,12 +1120,17 @@ describe('getRpcMethodMiddleware', () => {
11191120
expect((response as JsonRpcFailure).error.message).toBe(
11201121
expectedError.message,
11211122
);
1123+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1124+
expect(((response as JsonRpcFailure).error as JsonRpcError<any>).data.cause.message).toBe(
1125+
expectedError.message,
1126+
);
11221127
});
11231128

11241129
it('returns a JSON-RPC error if an error is thrown after approval', async () => {
11251130
// Omit `from` and `chainId` here to skip validation for simplicity
11261131
// Downcast needed here because `from` is required by this type
1127-
const mockTransactionParameters = {} as TransactionParams;
1132+
const mockTransactionParameters = {} as (TransactionParams &
1133+
JsonRpcParams)[];
11281134
setupGlobalState({
11291135
addTransactionResult: Promise.reject(
11301136
new Error('Failed to process transaction'),
@@ -1233,7 +1239,7 @@ describe('getRpcMethodMiddleware', () => {
12331239
jsonrpc,
12341240
id: 1,
12351241
method: 'personal_ecRecover',
1236-
params: [undefined, helloWorldSignature],
1242+
params: [undefined, helloWorldSignature] as JsonRpcParams,
12371243
};
12381244
const expectedError = rpcErrors.internal('Missing data parameter');
12391245

app/core/RPCMethods/RPCMethodMiddleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Alert } from 'react-native';
22
import { getVersion } from 'react-native-device-info';
3-
import { createAsyncMiddleware } from 'json-rpc-engine';
3+
import { createAsyncMiddleware } from '@metamask/json-rpc-engine';
44
import { providerErrors, rpcErrors } from '@metamask/rpc-errors';
55
import {
66
EndFlowOptions,

app/core/RPCMethods/createLegacyMethodMiddleware/index.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
JsonRpcEngine,
33
JsonRpcEngineEndCallback,
44
JsonRpcEngineNextCallback,
5-
} from 'json-rpc-engine';
5+
} from '@metamask/json-rpc-engine';
66
import {
77
Json,
88
JsonRpcParams,
@@ -150,6 +150,10 @@ describe('createLegacyMethodMiddleware', () => {
150150
});
151151
assertIsJsonRpcFailure(response);
152152

153+
// Type assertion for the error not having cause object
154+
const errorData = response.error.data as { cause?: Error };
155+
156+
expect(errorData.cause?.message).toBe('test error');
153157
expect(response.error.message).toBe('test error');
154158
});
155159

@@ -166,6 +170,10 @@ describe('createLegacyMethodMiddleware', () => {
166170
});
167171
assertIsJsonRpcFailure(response);
168172

173+
// Type assertion for the error not having cause object
174+
const errorData = response.error.data as { cause?: Error };
175+
176+
expect(errorData.cause?.message).toBe('test error');
169177
expect(response.error.message).toBe('test error');
170178
});
171179

0 commit comments

Comments
 (0)