From 23787d880190076d7900766a6db490b0bde06b7d Mon Sep 17 00:00:00 2001 From: Danica Shen Date: Mon, 23 Sep 2024 19:22:27 +0100 Subject: [PATCH] chore(26921): bump `@metamask/address-book-controller` to 6.0.0 (#27107) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## **Description** Address-book is refactored to baseControllerV2, see more details in [Changelog](https://github.com/MetaMask/core/blob/main/packages/address-book-controller/CHANGELOG.md) [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/27107?quickstart=1) ## **Related issues** Fixes: https://github.com/MetaMask/metamask-extension/issues/26921 ## **Manual testing steps** 1. Go to settings/contacts 2. Add/modify a new contact 3. Send a transaction to the contact ## **Screenshots/Recordings** ### **Before** ### **After** ## **Pre-merge author checklist** - [ ] 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. ## **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: MetaMask Bot --- .../lib/AbstractPetnamesBridge.test.ts | 4 +- .../lib/AddressBookPetnamesBridge.test.ts | 50 +++++++++++-------- app/scripts/lib/AddressBookPetnamesBridge.ts | 49 +++++++++++------- app/scripts/metamask-controller.js | 16 ++++-- lavamoat/browserify/beta/policy.json | 10 +--- lavamoat/browserify/flask/policy.json | 10 +--- lavamoat/browserify/main/policy.json | 10 +--- lavamoat/browserify/mmi/policy.json | 10 +--- package.json | 2 +- yarn.lock | 16 +++--- 10 files changed, 84 insertions(+), 93 deletions(-) diff --git a/app/scripts/lib/AbstractPetnamesBridge.test.ts b/app/scripts/lib/AbstractPetnamesBridge.test.ts index 3347c90f13f5..117705493d06 100644 --- a/app/scripts/lib/AbstractPetnamesBridge.test.ts +++ b/app/scripts/lib/AbstractPetnamesBridge.test.ts @@ -94,9 +94,7 @@ function createNameControllerMock(state: NameControllerState) { function createMessengerMock(): jest.Mocked { return { subscribe: jest.fn(), - // TODO: Replace `any` with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - } as any; + } as unknown as jest.Mocked; } describe('AbstractPetnamesBridge', () => { diff --git a/app/scripts/lib/AddressBookPetnamesBridge.test.ts b/app/scripts/lib/AddressBookPetnamesBridge.test.ts index f95726fba7b2..36b100b3542c 100644 --- a/app/scripts/lib/AddressBookPetnamesBridge.test.ts +++ b/app/scripts/lib/AddressBookPetnamesBridge.test.ts @@ -7,9 +7,14 @@ import { } from '@metamask/name-controller'; import { AddressBookController, - AddressBookState, + AddressBookControllerState, } from '@metamask/address-book-controller'; -import { AddressBookPetnamesBridge } from './AddressBookPetnamesBridge'; +import { + AddressBookPetnamesBridge, + AddressBookPetnamesBridgeActions, + AddressBookPetnamesBridgeEvens, + AddressBookPetnamesBridgeMessenger, +} from './AddressBookPetnamesBridge'; import { PetnamesBridgeMessenger } from './AbstractPetnamesBridge'; const ADDRESS_MOCK = '0xabc'; @@ -18,10 +23,9 @@ const NAME_2_MOCK = 'testName2'; const CHAIN_ID_MOCK = '0x1'; function createAddressBookControllerMock( - state: AddressBookState, + state: AddressBookControllerState, ): jest.Mocked & { - // Override the definition of state. Otherwise state is readonly. - state: AddressBookState; + state: AddressBookControllerState; } { return { state, @@ -37,17 +41,13 @@ function createNameControllerMock( return { state, setName: jest.fn(), - // TODO: Replace `any` with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - } as any; + } as unknown as jest.Mocked; } -function createMessengerMock(): jest.Mocked { +function createMessengerMock(): jest.Mocked { return { subscribe: jest.fn(), - // TODO: Replace `any` with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - } as any; + } as unknown as jest.Mocked; } const EMPTY_NAME_STATE: NameControllerState = { @@ -81,7 +81,7 @@ function createNameState(address: string, name: string): NameControllerState { }; } -const EMPTY_ADDRESS_BOOK_STATE: AddressBookState = { +const EMPTY_ADDRESS_BOOK_STATE: AddressBookControllerState = { addressBook: {}, }; @@ -96,7 +96,7 @@ function createAddressBookState( address: string, name: string, isEns: boolean, -): AddressBookState { +): AddressBookControllerState { return { ...EMPTY_ADDRESS_BOOK_STATE, addressBook: { @@ -114,14 +114,22 @@ function createAddressBookState( } describe('AddressBookPetnamesBridge', () => { - let messenger: jest.Mocked; + let messenger: jest.Mocked< + PetnamesBridgeMessenger< + AddressBookPetnamesBridgeEvens, + AddressBookPetnamesBridgeActions + > + >; beforeEach(() => { jest.resetAllMocks(); - messenger = createMessengerMock(); }); + afterEach(() => { + jest.resetAllMocks(); + }); + describe('NameController', () => { it('adds entry when address book entry added', () => { const addressBookController = createAddressBookControllerMock( @@ -139,8 +147,8 @@ describe('AddressBookPetnamesBridge', () => { NAME_MOCK, true, ); - const listener = addressBookController.subscribe.mock - .calls[0][0] as () => void; + + const listener = messenger.subscribe.mock.calls[1][1] as () => void; listener(); expect(nameController.setName).toHaveBeenCalledTimes(1); @@ -173,8 +181,7 @@ describe('AddressBookPetnamesBridge', () => { NAME_2_MOCK, false, ); - const listener = addressBookController.subscribe.mock - .calls[0][0] as () => void; + const listener = messenger.subscribe.mock.calls[1][1] as () => void; listener(); expect(nameController.setName).toHaveBeenCalledTimes(1); @@ -203,8 +210,7 @@ describe('AddressBookPetnamesBridge', () => { addressBookController.state = EMPTY_ADDRESS_BOOK_STATE; - const listener = addressBookController.subscribe.mock - .calls[0][0] as () => void; + const listener = messenger.subscribe.mock.calls[1][1] as () => void; listener(); expect(nameController.setName).toHaveBeenCalledTimes(1); diff --git a/app/scripts/lib/AddressBookPetnamesBridge.ts b/app/scripts/lib/AddressBookPetnamesBridge.ts index 141814a9ef62..64ab6823c6b0 100644 --- a/app/scripts/lib/AddressBookPetnamesBridge.ts +++ b/app/scripts/lib/AddressBookPetnamesBridge.ts @@ -1,9 +1,14 @@ -import { AddressBookController } from '@metamask/address-book-controller'; +import { + AddressBookController, + AddressBookControllerStateChangeEvent, + AddressBookControllerActions, +} from '@metamask/address-book-controller'; import { NameController, NameType, NameOrigin, } from '@metamask/name-controller'; +import { Hex, isStrictHexString } from '@metamask/utils'; import { AbstractPetnamesBridge, PetnamesBridgeMessenger, @@ -11,7 +16,19 @@ import { PetnameEntry, } from './AbstractPetnamesBridge'; -export class AddressBookPetnamesBridge extends AbstractPetnamesBridge { +export type AddressBookPetnamesBridgeEvens = + AddressBookControllerStateChangeEvent; +export type AddressBookPetnamesBridgeActions = AddressBookControllerActions; + +export type AddressBookPetnamesBridgeMessenger = PetnamesBridgeMessenger< + AddressBookPetnamesBridgeEvens, + AddressBookPetnamesBridgeActions +>; + +export class AddressBookPetnamesBridge extends AbstractPetnamesBridge< + AddressBookPetnamesBridgeEvens, + AddressBookPetnamesBridgeActions +> { #addressBookController: AddressBookController; constructor({ @@ -21,7 +38,7 @@ export class AddressBookPetnamesBridge extends AbstractPetnamesBridge { }: { addressBookController: AddressBookController; nameController: NameController; - messenger: PetnamesBridgeMessenger; + messenger: AddressBookPetnamesBridgeMessenger; }) { super({ isTwoWay: true, nameController, messenger }); @@ -35,14 +52,14 @@ export class AddressBookPetnamesBridge extends AbstractPetnamesBridge { const entries: PetnameEntry[] = []; const { state } = this.#addressBookController; for (const chainId of Object.keys(state.addressBook)) { - // TODO: Replace `any` with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const chainEntries = state.addressBook[chainId as any]; + if (!isStrictHexString(chainId)) { + continue; + } + + const chainEntries = state.addressBook[chainId]; for (const address of Object.keys(chainEntries)) { - // TODO: Replace `any` with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const entry = state.addressBook[chainId as any][address]; + const entry = state.addressBook[chainId][address]; const normalizedChainId = chainId.toLowerCase(); const { name, isEns } = entry; @@ -68,18 +85,12 @@ export class AddressBookPetnamesBridge extends AbstractPetnamesBridge { */ protected updateSourceEntry(type: ChangeType, entry: PetnameEntry): void { if (type === ChangeType.DELETED) { - // TODO: Replace `any` with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - this.#addressBookController.delete(entry.variation as any, entry.value); + this.#addressBookController.delete(entry.variation as Hex, entry.value); } else { this.#addressBookController.set( entry.value, - // TODO: Replace `any` with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - entry.name as any, - // TODO: Replace `any` with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - entry.variation as any, + entry.name, + entry.variation as Hex | undefined, ); } } @@ -88,6 +99,6 @@ export class AddressBookPetnamesBridge extends AbstractPetnamesBridge { * @override */ onSourceChange(listener: () => void): void { - this.#addressBookController.subscribe(listener); + this.messenger.subscribe('AddressBookController:stateChange', listener); } } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 1be3edfb6b9a..6289608c5291 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1746,10 +1746,17 @@ export default class MetamaskController extends EventEmitter { ), }); - this.addressBookController = new AddressBookController( - undefined, - initState.AddressBookController, - ); + const addressBookControllerMessenger = + this.controllerMessenger.getRestricted({ + name: 'AddressBookController', + allowedActions: [], + allowedEvents: [], + }); + + this.addressBookController = new AddressBookController({ + messenger: addressBookControllerMessenger, + state: initState.AddressBookController, + }); this.alertController = new AlertController({ initState: initState.AlertController, @@ -2153,6 +2160,7 @@ export default class MetamaskController extends EventEmitter { allowedEvents: [ 'NameController:stateChange', 'AccountsController:stateChange', + 'AddressBookController:stateChange', ], allowedActions: ['AccountsController:listAccounts'], }); diff --git a/lavamoat/browserify/beta/policy.json b/lavamoat/browserify/beta/policy.json index 86253b3fa63c..6fe02c83f1ad 100644 --- a/lavamoat/browserify/beta/policy.json +++ b/lavamoat/browserify/beta/policy.json @@ -639,18 +639,10 @@ }, "@metamask/address-book-controller": { "packages": { - "@metamask/address-book-controller>@metamask/base-controller": true, + "@metamask/base-controller": true, "@metamask/controller-utils": true } }, - "@metamask/address-book-controller>@metamask/base-controller": { - "globals": { - "setTimeout": true - }, - "packages": { - "immer": true - } - }, "@metamask/announcement-controller": { "packages": { "@metamask/announcement-controller>@metamask/base-controller": true diff --git a/lavamoat/browserify/flask/policy.json b/lavamoat/browserify/flask/policy.json index 86253b3fa63c..6fe02c83f1ad 100644 --- a/lavamoat/browserify/flask/policy.json +++ b/lavamoat/browserify/flask/policy.json @@ -639,18 +639,10 @@ }, "@metamask/address-book-controller": { "packages": { - "@metamask/address-book-controller>@metamask/base-controller": true, + "@metamask/base-controller": true, "@metamask/controller-utils": true } }, - "@metamask/address-book-controller>@metamask/base-controller": { - "globals": { - "setTimeout": true - }, - "packages": { - "immer": true - } - }, "@metamask/announcement-controller": { "packages": { "@metamask/announcement-controller>@metamask/base-controller": true diff --git a/lavamoat/browserify/main/policy.json b/lavamoat/browserify/main/policy.json index 86253b3fa63c..6fe02c83f1ad 100644 --- a/lavamoat/browserify/main/policy.json +++ b/lavamoat/browserify/main/policy.json @@ -639,18 +639,10 @@ }, "@metamask/address-book-controller": { "packages": { - "@metamask/address-book-controller>@metamask/base-controller": true, + "@metamask/base-controller": true, "@metamask/controller-utils": true } }, - "@metamask/address-book-controller>@metamask/base-controller": { - "globals": { - "setTimeout": true - }, - "packages": { - "immer": true - } - }, "@metamask/announcement-controller": { "packages": { "@metamask/announcement-controller>@metamask/base-controller": true diff --git a/lavamoat/browserify/mmi/policy.json b/lavamoat/browserify/mmi/policy.json index 032906bee80b..3cd466b03042 100644 --- a/lavamoat/browserify/mmi/policy.json +++ b/lavamoat/browserify/mmi/policy.json @@ -731,18 +731,10 @@ }, "@metamask/address-book-controller": { "packages": { - "@metamask/address-book-controller>@metamask/base-controller": true, + "@metamask/base-controller": true, "@metamask/controller-utils": true } }, - "@metamask/address-book-controller>@metamask/base-controller": { - "globals": { - "setTimeout": true - }, - "packages": { - "immer": true - } - }, "@metamask/announcement-controller": { "packages": { "@metamask/announcement-controller>@metamask/base-controller": true diff --git a/package.json b/package.json index 587ab7b5abde..7fa321fb7829 100644 --- a/package.json +++ b/package.json @@ -298,7 +298,7 @@ "@metamask/abi-utils": "^2.0.2", "@metamask/account-watcher": "^4.1.0", "@metamask/accounts-controller": "^18.2.1", - "@metamask/address-book-controller": "^5.0.0", + "@metamask/address-book-controller": "^6.0.0", "@metamask/announcement-controller": "^7.0.0", "@metamask/approval-controller": "^7.0.0", "@metamask/assets-controllers": "^37.0.0", diff --git a/yarn.lock b/yarn.lock index 56b12ef4ccb9..e6329f1b9a03 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4822,14 +4822,14 @@ __metadata: languageName: node linkType: hard -"@metamask/address-book-controller@npm:^5.0.0": - version: 5.0.0 - resolution: "@metamask/address-book-controller@npm:5.0.0" +"@metamask/address-book-controller@npm:^6.0.0": + version: 6.0.0 + resolution: "@metamask/address-book-controller@npm:6.0.0" dependencies: - "@metamask/base-controller": "npm:^6.0.0" - "@metamask/controller-utils": "npm:^11.0.0" - "@metamask/utils": "npm:^8.3.0" - checksum: 10/f4f3ec2c569457243cc4771c2d146a91227114431ecf8bb0d74e42b2e93fcc4d10545c32f865c918c9f1586379016d3ea2d7f2bc056a6b10b6cfd985f2b3c432 + "@metamask/base-controller": "npm:^7.0.0" + "@metamask/controller-utils": "npm:^11.2.0" + "@metamask/utils": "npm:^9.1.0" + checksum: 10/e699b97fc798a363390d673b766737dc10f82a1eefcf119823d8f4370846d6708b3b2eadd177c8b2cb826afa7e38b51cf978a0d55cd51e92e7a9ffe83f5f0803 languageName: node linkType: hard @@ -26080,7 +26080,7 @@ __metadata: "@metamask/abi-utils": "npm:^2.0.2" "@metamask/account-watcher": "npm:^4.1.0" "@metamask/accounts-controller": "npm:^18.2.1" - "@metamask/address-book-controller": "npm:^5.0.0" + "@metamask/address-book-controller": "npm:^6.0.0" "@metamask/announcement-controller": "npm:^7.0.0" "@metamask/api-specs": "npm:^0.9.3" "@metamask/approval-controller": "npm:^7.0.0"