Skip to content

Commit

Permalink
chore(26921): bump @metamask/address-book-controller to 6.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DDDDDanica committed Sep 13, 2024
1 parent ca6fd53 commit fea045f
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 82 deletions.
4 changes: 1 addition & 3 deletions app/scripts/lib/AbstractPetnamesBridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ function createNameControllerMock(state: NameControllerState) {
function createMessengerMock(): jest.Mocked<PetnamesBridgeMessenger> {
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<PetnamesBridgeMessenger>;
}

describe('AbstractPetnamesBridge', () => {
Expand Down
50 changes: 28 additions & 22 deletions app/scripts/lib/AddressBookPetnamesBridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -18,10 +23,9 @@ const NAME_2_MOCK = 'testName2';
const CHAIN_ID_MOCK = '0x1';

function createAddressBookControllerMock(
state: AddressBookState,
state: AddressBookControllerState,
): jest.Mocked<AddressBookController> & {
// Override the definition of state. Otherwise state is readonly.
state: AddressBookState;
state: AddressBookControllerState;
} {
return {
state,
Expand All @@ -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<NameController>;
}

function createMessengerMock(): jest.Mocked<PetnamesBridgeMessenger> {
function createMessengerMock(): jest.Mocked<AddressBookPetnamesBridgeMessenger> {
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<AddressBookPetnamesBridgeMessenger>;
}

const EMPTY_NAME_STATE: NameControllerState = {
Expand Down Expand Up @@ -81,7 +81,7 @@ function createNameState(address: string, name: string): NameControllerState {
};
}

const EMPTY_ADDRESS_BOOK_STATE: AddressBookState = {
const EMPTY_ADDRESS_BOOK_STATE: AddressBookControllerState = {
addressBook: {},
};

Expand All @@ -96,7 +96,7 @@ function createAddressBookState(
address: string,
name: string,
isEns: boolean,
): AddressBookState {
): AddressBookControllerState {
return {
...EMPTY_ADDRESS_BOOK_STATE,
addressBook: {
Expand All @@ -114,14 +114,22 @@ function createAddressBookState(
}

describe('AddressBookPetnamesBridge', () => {
let messenger: jest.Mocked<PetnamesBridgeMessenger>;
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(
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 22 additions & 7 deletions app/scripts/lib/AddressBookPetnamesBridge.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
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 type { Hex } from '@metamask/utils';
import {
AbstractPetnamesBridge,
PetnamesBridgeMessenger,
ChangeType,
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,
AddressBookPetnamesBridgeMessenger
> {
#addressBookController: AddressBookController;

constructor({
Expand All @@ -21,7 +38,7 @@ export class AddressBookPetnamesBridge extends AbstractPetnamesBridge {
}: {
addressBookController: AddressBookController;
nameController: NameController;
messenger: PetnamesBridgeMessenger<never, never>;
messenger: AddressBookPetnamesBridgeMessenger;
}) {
super({ isTwoWay: true, nameController, messenger });

Expand All @@ -35,9 +52,7 @@ 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];
const chainEntries = state.addressBook[chainId as Hex];

for (const address of Object.keys(chainEntries)) {
// TODO: Replace `any` with type
Expand Down Expand Up @@ -88,6 +103,6 @@ export class AddressBookPetnamesBridge extends AbstractPetnamesBridge {
* @override
*/
onSourceChange(listener: () => void): void {
this.#addressBookController.subscribe(listener);
this.messenger.subscribe('AddressBookController:stateChange', listener);
}
}
16 changes: 12 additions & 4 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1683,10 +1683,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,
Expand Down Expand Up @@ -2091,6 +2098,7 @@ export default class MetamaskController extends EventEmitter {
allowedEvents: [
'NameController:stateChange',
'AccountsController:stateChange',
'AddressBookController:stateChange',
],
allowedActions: ['AccountsController:listAccounts'],
});
Expand Down
10 changes: 1 addition & 9 deletions lavamoat/browserify/beta/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -784,18 +784,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
Expand Down
10 changes: 1 addition & 9 deletions lavamoat/browserify/flask/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -784,18 +784,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
Expand Down
10 changes: 1 addition & 9 deletions lavamoat/browserify/main/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -784,18 +784,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
Expand Down
10 changes: 1 addition & 9 deletions lavamoat/browserify/mmi/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -876,18 +876,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
Expand Down
10 changes: 9 additions & 1 deletion lavamoat/build-system/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -2117,7 +2117,8 @@
"chokidar>normalize-path": true,
"chokidar>readdirp": true,
"del>is-glob": true,
"eslint>glob-parent": true
"eslint>glob-parent": true,
"tsx>fsevents": true
}
},
"chokidar>anymatch": {
Expand Down Expand Up @@ -8893,6 +8894,13 @@
"typescript": true
}
},
"tsx>fsevents": {
"globals": {
"console.assert": true,
"process.platform": true
},
"native": true
},
"typescript": {
"builtin": {
"buffer.Buffer": true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
"@metamask/abi-utils": "^2.0.2",
"@metamask/account-watcher": "^4.1.0",
"@metamask/accounts-controller": "^18.2.0",
"@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": "^36.0.0",
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4765,14 +4765,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

Expand Down Expand Up @@ -25970,7 +25970,7 @@ __metadata:
"@metamask/abi-utils": "npm:^2.0.2"
"@metamask/account-watcher": "npm:^4.1.0"
"@metamask/accounts-controller": "npm:^18.2.0"
"@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"
Expand Down

0 comments on commit fea045f

Please sign in to comment.