Skip to content

Commit

Permalink
feat: Bump QueuedRequestController from ^2.0.0 to ^7.0.0  (#28090)
Browse files Browse the repository at this point in the history
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

Bumps `@metamask/queued-request-controller` to fix queueing issue with
Chain Permission `wallet_switchEthereumChain` and
`wallet_addEthereumChain` when switching to a previously permitted chain
and with `wallet_addEthereumChain` not being enqueued when it still
should be.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/28090?quickstart=1)

Related: MetaMask/core#4846
Fixes: #28101
Fixes: #27977

The easiest way to test this would be a combination of using the test
dapp and the following request to switch chains
```
await window.ethereum.request({
 "method": "wallet_switchEthereumChain",
 "params": [
  {
    chainId: "0x1"
  }
],
});
```

The behaviors you should see include:
**One dapp:**
* On a dapp permissioned for chain A and B, on chain A, queue up several
send transactions, then use wallet_switchEthereumChain to switch to
chain B. The send transactions should NOT get cleared immediately after
requesting the chain switch. Chain switch should NOT happen until the
previous approvals are approved/rejected.
* On a dapp permissioned for chain A and B, on chain A, queue up one
send transaction, then use wallet_switchEthereumChain to switch to chain
B, then queue up several more send transactions. Reject/approve the
first transaction. Afterwards, you should see chain B as the active
chain for the dapp, and all subsequent approvals cleared/rejected
automatically.
* On a dapp permissioned for ONLY chain A, on chain A, queue up one send
transaction, then use wallet_switchEthereumChain to switch to chain B,
then queue up several more send transactions. Reject/approve the first
transaction. Afterwards, you should an approval prompt for adding chain
B. If you approve it, the dapp should then be on chain B, with all
subsequent approvals cleared/rejected. If you disapprove it, you should
be prompted with the subsequent approvals.
* On a dapp permissioned for ONLY chain A, on chain A,
wallet_switchEthereumChain to switch to chain B, then queue up several
more send transactions. Reject/approve the first transaction.
Afterwards, you should an approval prompt for adding chain B. If you
approve it, the dapp should then be on chain B, with all subsequent
approvals cleared/rejected. If you disapprove it, you should be prompted
with the subsequent approvals.

**Two dapps:**
* On a dapp permissioned for chain A, on chain A, queue up several send
transactions, On a separate dapp permissioned for chain A and B, on
chain A, use wallet_switchEthereumChain to switch to chain B. The send
transactions should NOT get cleared immediately after requesting the
chain switch. Chain switch should NOT happen until the previous
approvals are approved/rejected.
* On a dapp permissioned for chain A and B, on chain A, queue up one
send transaction. On a separate dapp permissioned for chain A and B, on
chain A, use wallet_switchEthereumChain to switch to chain B. Then on
the first dapp queue up several more send transactions. Reject/approve
the first transaction. Afterwards, you should see chain B as the active
chain for the second dapp, and then you should still be prompted with
the subsequent approvals for the first dapp.
* One one dapp, start a wallet_addEthereumChain for a chain that does
not exist in the wallet and leave the approval alone. On a different
dapp, do the same thing. Only the request from the first dapp should be
accessible (i.e. no scrubbing between both of them). After rejecting the
first request, the second request should then appear (which will look
exactly the same of course). Wallet should not lock up if you repeat
this and accept either of the requests

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

<!-- [screenshots/recordings] -->

https://github.com/user-attachments/assets/2634119f-67db-4866-8520-9320a9400b1d

https://github.com/user-attachments/assets/c78c13ab-ea4f-4420-bccc-70959786e8db

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] 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.

- [ ] 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: MetaMask Bot <[email protected]>
  • Loading branch information
2 people authored and Gudahtt committed Oct 30, 2024
1 parent 43e43b3 commit 66403c1
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 280 deletions.
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
76 changes: 20 additions & 56 deletions lavamoat/browserify/beta/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1354,9 +1354,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 +2212,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
76 changes: 20 additions & 56 deletions lavamoat/browserify/flask/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1354,9 +1354,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 +2212,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
76 changes: 20 additions & 56 deletions lavamoat/browserify/main/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1354,9 +1354,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 +2212,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
Loading

0 comments on commit 66403c1

Please sign in to comment.