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

[cherry pick] Fix bugs related to queued requests #28197

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { jsonrpc2, Json } from '@metamask/utils';
import { BtcAccountType, EthAccountType } from '@metamask/keyring-api';
import type { JsonRpcParams, JsonRpcRequest } from '@metamask/utils';
import createEvmMethodsToNonEvmAccountReqFilterMiddleware, {
EvmMethodsToNonEvmAccountFilterMessenger,
} from './createEvmMethodsToNonEvmAccountReqFilterMiddleware';

describe('createEvmMethodsToNonEvmAccountReqFilterMiddleware', () => {
const getMockRequest = (method: string, params: Json) => ({
const getMockRequest = (method: string, params: Record<string, Json>) => ({
jsonrpc: jsonrpc2,
id: 1,
method,
Expand Down Expand Up @@ -286,7 +285,7 @@ describe('createEvmMethodsToNonEvmAccountReqFilterMiddleware', () => {
}: {
accountType: EthAccountType | BtcAccountType;
method: string;
params: Json;
params: Record<string, Json>;
calledNext: number;
}) => {
const filterFn = createEvmMethodsToNonEvmAccountReqFilterMiddleware({
Expand All @@ -298,7 +297,7 @@ describe('createEvmMethodsToNonEvmAccountReqFilterMiddleware', () => {
const mockEnd = jest.fn();

filterFn(
getMockRequest(method, params) as JsonRpcRequest<JsonRpcParams>,
getMockRequest(method, params),
getMockResponse(),
mockNext,
mockEnd,
Expand Down
46 changes: 28 additions & 18 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ import {
NotificationServicesController,
} from '@metamask/notification-services-controller';
import { isProduction } from '../../shared/modules/environment';
import { methodsRequiringNetworkSwitch } from '../../shared/constants/methods-tags';
import {
methodsRequiringNetworkSwitch,
methodsThatCanSwitchNetworkWithoutApproval,
methodsThatShouldBeEnqueued,
} from '../../shared/constants/methods-tags';

///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
import { toChecksumHexAddress } from '../../shared/modules/hexstring-utils';
Expand Down Expand Up @@ -486,22 +490,6 @@ export default class MetamaskController extends EventEmitter {
this.approvalController.clear(providerErrors.userRejectedRequest());
};

this.queuedRequestController = new QueuedRequestController({
messenger: this.controllerMessenger.getRestricted({
name: 'QueuedRequestController',
allowedActions: [
'NetworkController:getState',
'NetworkController:setActiveNetwork',
'SelectedNetworkController:getNetworkClientIdForDomain',
],
allowedEvents: ['SelectedNetworkController:stateChange'],
}),
shouldRequestSwitchNetwork: ({ method }) =>
methodsRequiringNetworkSwitch.includes(method),
clearPendingConfirmations,
showApprovalRequest: opts.showUserConfirmation,
});

this.approvalController = new ApprovalController({
messenger: this.controllerMessenger.getRestricted({
name: 'ApprovalController',
Expand All @@ -517,6 +505,28 @@ export default class MetamaskController extends EventEmitter {
],
});

this.queuedRequestController = new QueuedRequestController({
messenger: this.controllerMessenger.getRestricted({
name: 'QueuedRequestController',
allowedActions: [
'NetworkController:getState',
'NetworkController:setActiveNetwork',
'SelectedNetworkController:getNetworkClientIdForDomain',
],
allowedEvents: ['SelectedNetworkController:stateChange'],
}),
shouldRequestSwitchNetwork: ({ method }) =>
methodsRequiringNetworkSwitch.includes(method),
canRequestSwitchNetworkWithoutApproval: ({ method }) =>
methodsThatCanSwitchNetworkWithoutApproval.includes(method),
clearPendingConfirmations,
showApprovalRequest: () => {
if (this.approvalController.getTotalApprovalCount() > 0) {
opts.showUserConfirmation();
}
},
});

///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
this.mmiConfigurationController = new MmiConfigurationController({
initState: initState.MmiConfigurationController,
Expand Down Expand Up @@ -5642,7 +5652,7 @@ export default class MetamaskController extends EventEmitter {
this.preferencesController,
),
shouldEnqueueRequest: (request) => {
return methodsRequiringNetworkSwitch.includes(request.method);
return methodsThatShouldBeEnqueued.includes(request.method);
},
});
engine.push(requestQueueMiddleware);
Expand Down
112 changes: 53 additions & 59 deletions lavamoat/browserify/beta/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -787,15 +787,30 @@
},
"packages": {
"@ethereumjs/tx>@ethereumjs/util": true,
"@metamask/controller-utils>@metamask/utils": true,
"@metamask/controller-utils>@spruceid/siwe-parser": true,
"@metamask/ethjs>@metamask/ethjs-unit": true,
"@metamask/utils": true,
"bn.js": true,
"browserify>buffer": true,
"eslint>fast-deep-equal": true,
"eth-ens-namehash": true
}
},
"@metamask/controller-utils>@metamask/utils": {
"globals": {
"TextDecoder": true,
"TextEncoder": true
},
"packages": {
"@metamask/utils>@metamask/superstruct": true,
"@metamask/utils>@scure/base": true,
"@metamask/utils>pony-cause": true,
"@noble/hashes": true,
"browserify>buffer": true,
"nock>debug": true,
"semver": true
}
},
"@metamask/controller-utils>@spruceid/siwe-parser": {
"globals": {
"console.error": true,
Expand Down Expand Up @@ -1354,9 +1369,24 @@
},
"@metamask/json-rpc-engine": {
"packages": {
"@metamask/json-rpc-engine>@metamask/utils": true,
"@metamask/rpc-errors": true,
"@metamask/safe-event-emitter": true,
"@metamask/utils": true
"@metamask/safe-event-emitter": true
}
},
"@metamask/json-rpc-engine>@metamask/utils": {
"globals": {
"TextDecoder": true,
"TextEncoder": true
},
"packages": {
"@metamask/utils>@metamask/superstruct": true,
"@metamask/utils>@scure/base": true,
"@metamask/utils>pony-cause": true,
"@noble/hashes": true,
"browserify>buffer": true,
"nock>debug": true,
"semver": true
}
},
"@metamask/keyring-api": {
Expand Down Expand Up @@ -2197,64 +2227,13 @@
},
"@metamask/queued-request-controller": {
"packages": {
"@metamask/queued-request-controller>@metamask/base-controller": true,
"@metamask/queued-request-controller>@metamask/json-rpc-engine": true,
"@metamask/queued-request-controller>@metamask/rpc-errors": true,
"@metamask/base-controller": true,
"@metamask/json-rpc-engine": true,
"@metamask/queued-request-controller>@metamask/utils": true,
"@metamask/rpc-errors": true,
"@metamask/selected-network-controller": true
}
},
"@metamask/queued-request-controller>@metamask/base-controller": {
"globals": {
"setTimeout": true
},
"packages": {
"immer": true
}
},
"@metamask/queued-request-controller>@metamask/json-rpc-engine": {
"packages": {
"@metamask/queued-request-controller>@metamask/json-rpc-engine>@metamask/utils": true,
"@metamask/queued-request-controller>@metamask/rpc-errors": true,
"@metamask/safe-event-emitter": true
}
},
"@metamask/queued-request-controller>@metamask/json-rpc-engine>@metamask/utils": {
"globals": {
"TextDecoder": true,
"TextEncoder": true
},
"packages": {
"@metamask/utils>@metamask/superstruct": true,
"@metamask/utils>@scure/base": true,
"@metamask/utils>pony-cause": true,
"@noble/hashes": true,
"browserify>buffer": true,
"nock>debug": true,
"semver": true
}
},
"@metamask/queued-request-controller>@metamask/rpc-errors": {
"packages": {
"@metamask/queued-request-controller>@metamask/rpc-errors>@metamask/utils": true,
"@metamask/rpc-errors>fast-safe-stringify": true
}
},
"@metamask/queued-request-controller>@metamask/rpc-errors>@metamask/utils": {
"globals": {
"TextDecoder": true,
"TextEncoder": true
},
"packages": {
"@metamask/utils>@metamask/superstruct": true,
"@metamask/utils>@scure/base": true,
"@metamask/utils>pony-cause": true,
"@noble/hashes": true,
"browserify>buffer": true,
"nock>debug": true,
"semver": true
}
},
"@metamask/queued-request-controller>@metamask/utils": {
"globals": {
"TextDecoder": true,
Expand Down Expand Up @@ -2326,8 +2305,23 @@
},
"@metamask/rpc-errors": {
"packages": {
"@metamask/rpc-errors>fast-safe-stringify": true,
"@metamask/utils": true
"@metamask/rpc-errors>@metamask/utils": true,
"@metamask/rpc-errors>fast-safe-stringify": true
}
},
"@metamask/rpc-errors>@metamask/utils": {
"globals": {
"TextDecoder": true,
"TextEncoder": true
},
"packages": {
"@metamask/utils>@metamask/superstruct": true,
"@metamask/utils>@scure/base": true,
"@metamask/utils>pony-cause": true,
"@noble/hashes": true,
"browserify>buffer": true,
"nock>debug": true,
"semver": true
}
},
"@metamask/rpc-methods-flask>nanoid": {
Expand Down
Loading