Skip to content

Commit

Permalink
@metamask/controllers@15.0.0 (#11975)
Browse files Browse the repository at this point in the history
Adds the latest version of `@metamask/controllers`, and updates our usage of the `ApprovalController`, which has been migrated to `BaseControllerV2`. Of [the new `controllers` release](https://github.com/MetaMask/controllers/releases/tag/v15.0.0), only the `ApprovalController` migration should be breaking.

This is the first time we use events on the `ControllerMessenger` to update the badge, so I turned the messenger into a property on the main `MetaMaskController` in order to subscribe to events on it in `background.js`. I confirmed that the badge does indeed update during local QA.

As it turns out, [MetaMask/core#571](MetaMask/core#571) was breaking for a single unit test case, which is now handled during setup and teardown for the related test suite (`metamask-controller.test.js`).
  • Loading branch information
rekmarks authored Aug 31, 2021
1 parent 4b4303c commit 8a8ce3a
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 17 deletions.
6 changes: 5 additions & 1 deletion app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,16 @@ function setupController(initState, initLangCode) {
METAMASK_CONTROLLER_EVENTS.UPDATE_BADGE,
updateBadge,
);
controller.approvalController.subscribe(updateBadge);
controller.appStateController.on(
METAMASK_CONTROLLER_EVENTS.UPDATE_BADGE,
updateBadge,
);

controller.controllerMessenger.subscribe(
METAMASK_CONTROLLER_EVENTS.APPROVAL_STATE_CHANGE,
updateBadge,
);

/**
* Updates the Web Extension's "badge" number, on the little fox in the toolbar.
* The number reflects the current number of pending transactions or message signatures needing user approval.
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/controllers/permissions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class PermissionsController {
approved.permissions,
accounts,
);
this.approvals.resolve(id, approved.permissions);
this.approvals.accept(id, approved.permissions);
}
} catch (err) {
// if finalization fails, reject the request
Expand Down
19 changes: 12 additions & 7 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export const METAMASK_CONTROLLER_EVENTS = {
// Fired after state changes that impact the extension badge (unapproved msg count)
// The process of updating the badge happens in app/scripts/background.js.
UPDATE_BADGE: 'updateBadge',
// TODO: Add this and similar enums to @metamask/controllers and export them
APPROVAL_STATE_CHANGE: 'ApprovalController:stateChange',
};

/**
Expand Down Expand Up @@ -115,12 +117,12 @@ export default class MetamaskController extends EventEmitter {
this.getRequestAccountTabIds = opts.getRequestAccountTabIds;
this.getOpenMetamaskTabsIds = opts.getOpenMetamaskTabsIds;

const controllerMessenger = new ControllerMessenger();
this.controllerMessenger = new ControllerMessenger();

// observable state store
this.store = new ComposableObservableStore({
state: initState,
controllerMessenger,
controllerMessenger: this.controllerMessenger,
});

// external connections by origin
Expand All @@ -140,6 +142,9 @@ export default class MetamaskController extends EventEmitter {
// controller initialization order matters

this.approvalController = new ApprovalController({
messenger: this.controllerMessenger.getRestricted({
name: 'ApprovalController',
}),
showApprovalRequest: opts.showUserConfirmation,
});

Expand Down Expand Up @@ -178,7 +183,7 @@ export default class MetamaskController extends EventEmitter {
initState: initState.MetaMetricsController,
});

const gasFeeMessenger = controllerMessenger.getRestricted({
const gasFeeMessenger = this.controllerMessenger.getRestricted({
name: 'GasFeeController',
});

Expand Down Expand Up @@ -219,7 +224,7 @@ export default class MetamaskController extends EventEmitter {
preferencesStore: this.preferencesController.store,
});

const currencyRateMessenger = controllerMessenger.getRestricted({
const currencyRateMessenger = this.controllerMessenger.getRestricted({
name: 'CurrencyRateController',
});
this.currencyRateController = new CurrencyRateController({
Expand All @@ -228,7 +233,7 @@ export default class MetamaskController extends EventEmitter {
state: initState.CurrencyController,
});

const tokenListMessenger = controllerMessenger.getRestricted({
const tokenListMessenger = this.controllerMessenger.getRestricted({
name: 'TokenListController',
});
this.tokenListController = new TokenListController({
Expand Down Expand Up @@ -578,7 +583,7 @@ export default class MetamaskController extends EventEmitter {
GasFeeController: this.gasFeeController,
TokenListController: this.tokenListController,
},
controllerMessenger,
controllerMessenger: this.controllerMessenger,
});
this.memStore.subscribe(this.sendUpdate.bind(this));

Expand Down Expand Up @@ -1106,7 +1111,7 @@ export default class MetamaskController extends EventEmitter {

// approval controller
resolvePendingApproval: nodeify(
approvalController.resolve,
approvalController.accept,
approvalController,
),
rejectPendingApproval: nodeify(
Expand Down
2 changes: 2 additions & 0 deletions app/scripts/metamask-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ describe('MetaMaskController', function () {
const noop = () => undefined;

before(async function () {
globalThis.AbortController = window.AbortController;
await ganacheServer.start();
});

Expand Down Expand Up @@ -157,6 +158,7 @@ describe('MetaMaskController', function () {

after(async function () {
await ganacheServer.quit();
delete globalThis.AbortController;
});

describe('#getAccounts', function () {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"@fortawesome/fontawesome-free": "^5.13.0",
"@material-ui/core": "^4.11.0",
"@metamask/contract-metadata": "^1.28.0",
"@metamask/controllers": "^14.0.2",
"@metamask/controllers": "^15.0.0",
"@metamask/eth-ledger-bridge-keyring": "^0.6.0",
"@metamask/eth-token-tracker": "^3.0.1",
"@metamask/etherscan-link": "^2.1.0",
Expand Down
3 changes: 2 additions & 1 deletion test/mocks/permission-controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethErrors, errorCodes } from 'eth-rpc-errors';
import deepFreeze from 'deep-freeze-strict';

import { ApprovalController } from '@metamask/controllers';
import { ApprovalController, ControllerMessenger } from '@metamask/controllers';

import _getRestrictedMethods from '../../app/scripts/controllers/permissions/restrictedMethods';

Expand Down Expand Up @@ -70,6 +70,7 @@ const getRestrictedMethods = (permController) => {
export function getPermControllerOpts() {
return {
approvals: new ApprovalController({
messenger: new ControllerMessenger(),
showApprovalRequest: noop,
}),
getKeyringAccounts: async () => [...keyringAccounts],
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2725,19 +2725,19 @@
semver "^7.3.5"
yargs "^17.0.1"

"@metamask/contract-metadata@^1.19.0", "@metamask/contract-metadata@^1.28.0":
"@metamask/contract-metadata@^1.19.0", "@metamask/contract-metadata@^1.28.0", "@metamask/contract-metadata@^1.29.0":
version "1.29.0"
resolved "https://registry.yarnpkg.com/@metamask/contract-metadata/-/contract-metadata-1.29.0.tgz#4ca86a2f03d4dad4350d09216a7fe92f9dd21c8e"
integrity sha512-wxsC0ZCyhPKqThvmsL8+2zVWGWPqofSo8HNtOuOnQM9oGbXX9294imJ3T+A/Lov8fkX4jAWZOeNV0uR80zkNtA==

"@metamask/controllers@^14.0.2":
version "14.2.0"
resolved "https://registry.yarnpkg.com/@metamask/controllers/-/controllers-14.2.0.tgz#bed161ac3523fd525be79b0e557b132e2e2526e4"
integrity sha512-1Is1OJByDJo5GOKt02TiCCGhYlclcAT2IeoiqhSL6fTUT4Qc+2xwgjx42XeE7bzKIDwXpxafpHViCkO3d6IIdA==
"@metamask/controllers@^15.0.0":
version "15.0.0"
resolved "https://registry.yarnpkg.com/@metamask/controllers/-/controllers-15.0.0.tgz#b7e816e12e02debaf32f7bab5f8d612cbd7a5170"
integrity sha512-vYVwDVctxdmBRBYDzPfpab3GoVtePykaMKfOdgD+OT8Cz8tlDrEIRc5+DZhr6HembWg8LkNfw9Gh5lKfAFSGLg==
dependencies:
"@ethereumjs/common" "^2.3.1"
"@ethereumjs/tx" "^3.2.1"
"@metamask/contract-metadata" "^1.28.0"
"@metamask/contract-metadata" "^1.29.0"
"@types/uuid" "^8.3.0"
async-mutex "^0.2.6"
babel-runtime "^6.26.0"
Expand Down

0 comments on commit 8a8ce3a

Please sign in to comment.