Skip to content

Commit f2ebeb3

Browse files
authored
Merge branch 'main' into fix/missing-accounts-controller-actions-exports
2 parents 3ccb1bb + 5bc1150 commit f2ebeb3

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

packages/accounts-controller/src/AccountsController.test.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,50 @@ describe('AccountsController', () => {
919919
]);
920920
expect(accountsController.getSelectedAccount().id).toBe(mockAccount.id);
921921
});
922+
923+
it('publishes accountAdded event', async () => {
924+
const messenger = buildMessenger();
925+
const messengerSpy = jest.spyOn(messenger, 'publish');
926+
mockUUID
927+
.mockReturnValueOnce(mockAccount.id) // call to check if its a new account
928+
.mockReturnValueOnce(mockAccount2.id) // call to check if its a new account
929+
.mockReturnValueOnce(mockAccount2.id); // call to add account
930+
931+
setupAccountsController({
932+
initialState: {
933+
internalAccounts: {
934+
accounts: {
935+
[mockAccount.id]: mockAccount,
936+
},
937+
selectedAccount: mockAccount.id,
938+
},
939+
},
940+
messenger,
941+
});
942+
943+
const mockNewKeyringState = {
944+
isUnlocked: true,
945+
keyrings: [
946+
{
947+
type: KeyringTypes.hd,
948+
accounts: [mockAccount.address, mockAccount2.address],
949+
},
950+
],
951+
};
952+
953+
messenger.publish(
954+
'KeyringController:stateChange',
955+
mockNewKeyringState,
956+
[],
957+
);
958+
959+
// First call is 'KeyringController:stateChange'
960+
expect(messengerSpy).toHaveBeenNthCalledWith(
961+
2,
962+
'AccountsController:accountAdded',
963+
setLastSelectedAsAny(mockAccount2),
964+
);
965+
});
922966
});
923967

924968
describe('deleting account', () => {
@@ -1155,6 +1199,50 @@ describe('AccountsController', () => {
11551199
currentTime,
11561200
);
11571201
});
1202+
1203+
it('publishes accountRemoved event', async () => {
1204+
const messenger = buildMessenger();
1205+
const messengerSpy = jest.spyOn(messenger, 'publish');
1206+
mockUUID
1207+
.mockReturnValueOnce(mockAccount.id) // call to check if its a new account
1208+
.mockReturnValueOnce(mockAccount2.id) // call to check if its a new account
1209+
.mockReturnValueOnce(mockAccount2.id); // call to add account
1210+
1211+
setupAccountsController({
1212+
initialState: {
1213+
internalAccounts: {
1214+
accounts: {
1215+
[mockAccount.id]: mockAccount,
1216+
[mockAccount3.id]: mockAccount3,
1217+
},
1218+
selectedAccount: mockAccount.id,
1219+
},
1220+
},
1221+
messenger,
1222+
});
1223+
1224+
const mockNewKeyringState = {
1225+
isUnlocked: true,
1226+
keyrings: [
1227+
{
1228+
type: KeyringTypes.hd,
1229+
accounts: [mockAccount.address, mockAccount2.address],
1230+
},
1231+
],
1232+
};
1233+
messenger.publish(
1234+
'KeyringController:stateChange',
1235+
mockNewKeyringState,
1236+
[],
1237+
);
1238+
1239+
// First call is 'KeyringController:stateChange'
1240+
expect(messengerSpy).toHaveBeenNthCalledWith(
1241+
2,
1242+
'AccountsController:accountRemoved',
1243+
mockAccount3.id,
1244+
);
1245+
});
11581246
});
11591247

11601248
it('handle keyring reinitialization', async () => {

packages/accounts-controller/src/AccountsController.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ import {
4242

4343
const controllerName = 'AccountsController';
4444

45+
export type AccountId = string;
46+
4547
export type AccountsControllerState = {
4648
internalAccounts: {
47-
accounts: Record<string, InternalAccount>;
49+
accounts: Record<AccountId, InternalAccount>;
4850
selectedAccount: string; // id of the selected account
4951
};
5052
};
@@ -137,12 +139,24 @@ export type AccountsControllerSelectedEvmAccountChangeEvent = {
137139
payload: [InternalAccount];
138140
};
139141

142+
export type AccountsControllerAccountAddedEvent = {
143+
type: `${typeof controllerName}:accountAdded`;
144+
payload: [InternalAccount];
145+
};
146+
147+
export type AccountsControllerAccountRemovedEvent = {
148+
type: `${typeof controllerName}:accountRemoved`;
149+
payload: [AccountId];
150+
};
151+
140152
export type AllowedEvents = SnapStateChange | KeyringControllerStateChangeEvent;
141153

142154
export type AccountsControllerEvents =
143155
| AccountsControllerChangeEvent
144156
| AccountsControllerSelectedAccountChangeEvent
145-
| AccountsControllerSelectedEvmAccountChangeEvent;
157+
| AccountsControllerSelectedEvmAccountChangeEvent
158+
| AccountsControllerAccountAddedEvent
159+
| AccountsControllerAccountRemovedEvent;
146160

147161
export type AccountsControllerMessenger = RestrictedControllerMessenger<
148162
typeof controllerName,
@@ -951,7 +965,7 @@ export class AccountsController extends BaseController<
951965
Object.values(accountsState),
952966
);
953967

954-
accountsState[newAccount.id] = {
968+
const newAccountWithUpdatedMetadata = {
955969
...newAccount,
956970
metadata: {
957971
...newAccount.metadata,
@@ -960,6 +974,12 @@ export class AccountsController extends BaseController<
960974
lastSelected: isFirstAccount ? this.#getLastSelectedIndex() : 0,
961975
},
962976
};
977+
accountsState[newAccount.id] = newAccountWithUpdatedMetadata;
978+
979+
this.messagingSystem.publish(
980+
'AccountsController:accountAdded',
981+
newAccountWithUpdatedMetadata,
982+
);
963983

964984
return accountsState;
965985
}
@@ -988,6 +1008,12 @@ export class AccountsController extends BaseController<
9881008
accountId: string,
9891009
): AccountsControllerState['internalAccounts']['accounts'] {
9901010
delete accountsState[accountId];
1011+
1012+
this.messagingSystem.publish(
1013+
'AccountsController:accountRemoved',
1014+
accountId,
1015+
);
1016+
9911017
return accountsState;
9921018
}
9931019

packages/accounts-controller/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export type {
1515
AccountsControllerChangeEvent,
1616
AccountsControllerSelectedAccountChangeEvent,
1717
AccountsControllerSelectedEvmAccountChangeEvent,
18+
AccountsControllerAccountAddedEvent,
19+
AccountsControllerAccountRemovedEvent,
1820
AccountsControllerEvents,
1921
AccountsControllerMessenger,
2022
} from './AccountsController';

0 commit comments

Comments
 (0)