Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add accountAdded and accountRemoved events #4496

Merged
merged 7 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions packages/accounts-controller/src/AccountsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,50 @@ describe('AccountsController', () => {
]);
expect(accountsController.getSelectedAccount().id).toBe(mockAccount.id);
});

it('publishes accountAdded event', async () => {
const messenger = buildMessenger();
const messengerSpy = jest.spyOn(messenger, 'publish');
mockUUID
.mockReturnValueOnce(mockAccount.id) // call to check if its a new account
.mockReturnValueOnce(mockAccount2.id) // call to check if its a new account
.mockReturnValueOnce(mockAccount2.id); // call to add account

setupAccountsController({
initialState: {
internalAccounts: {
accounts: {
[mockAccount.id]: mockAccount,
},
selectedAccount: mockAccount.id,
},
},
messenger,
});

const mockNewKeyringState = {
isUnlocked: true,
keyrings: [
{
type: KeyringTypes.hd,
accounts: [mockAccount.address, mockAccount2.address],
},
],
};

montelaidev marked this conversation as resolved.
Show resolved Hide resolved
messenger.publish(
'KeyringController:stateChange',
mockNewKeyringState,
[],
);

// First call is 'KeyringController:stateChange'
expect(messengerSpy).toHaveBeenNthCalledWith(
2,
'AccountsController:accountAdded',
setLastSelectedAsAny(mockAccount2),
);
});
});

describe('deleting account', () => {
Expand Down Expand Up @@ -1155,6 +1199,50 @@ describe('AccountsController', () => {
currentTime,
);
});

it('publishes accountRemoved event', async () => {
const messenger = buildMessenger();
const messengerSpy = jest.spyOn(messenger, 'publish');
mockUUID
.mockReturnValueOnce(mockAccount.id) // call to check if its a new account
.mockReturnValueOnce(mockAccount2.id) // call to check if its a new account
.mockReturnValueOnce(mockAccount2.id); // call to add account

setupAccountsController({
initialState: {
internalAccounts: {
accounts: {
[mockAccount.id]: mockAccount,
[mockAccount3.id]: mockAccount3,
},
selectedAccount: mockAccount.id,
},
},
messenger,
});

const mockNewKeyringState = {
isUnlocked: true,
keyrings: [
{
type: KeyringTypes.hd,
accounts: [mockAccount.address, mockAccount2.address],
},
],
};
messenger.publish(
'KeyringController:stateChange',
mockNewKeyringState,
[],
);

// First call is 'KeyringController:stateChange'
expect(messengerSpy).toHaveBeenNthCalledWith(
2,
'AccountsController:accountRemoved',
mockAccount3.id,
);
});
});

it('handle keyring reinitialization', async () => {
Expand Down
32 changes: 29 additions & 3 deletions packages/accounts-controller/src/AccountsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ import {

const controllerName = 'AccountsController';

export type AccountId = string;

export type AccountsControllerState = {
internalAccounts: {
accounts: Record<string, InternalAccount>;
accounts: Record<AccountId, InternalAccount>;
selectedAccount: string; // id of the selected account
};
};
Expand Down Expand Up @@ -137,12 +139,24 @@ export type AccountsControllerSelectedEvmAccountChangeEvent = {
payload: [InternalAccount];
};

export type AccountsControllerAccountAddedEvent = {
type: `${typeof controllerName}:accountAdded`;
payload: [InternalAccount];
};

export type AccountsControllerAccountRemovedEvent = {
type: `${typeof controllerName}:accountRemoved`;
payload: [AccountId];
};

export type AllowedEvents = SnapStateChange | KeyringControllerStateChangeEvent;

export type AccountsControllerEvents =
| AccountsControllerChangeEvent
| AccountsControllerSelectedAccountChangeEvent
| AccountsControllerSelectedEvmAccountChangeEvent;
| AccountsControllerSelectedEvmAccountChangeEvent
| AccountsControllerAccountAddedEvent
| AccountsControllerAccountRemovedEvent;

export type AccountsControllerMessenger = RestrictedControllerMessenger<
typeof controllerName,
Expand Down Expand Up @@ -951,7 +965,7 @@ export class AccountsController extends BaseController<
Object.values(accountsState),
);

accountsState[newAccount.id] = {
const newAccountWithUpdatedMetadata = {
...newAccount,
metadata: {
...newAccount.metadata,
Expand All @@ -960,6 +974,12 @@ export class AccountsController extends BaseController<
lastSelected: isFirstAccount ? this.#getLastSelectedIndex() : 0,
},
};
accountsState[newAccount.id] = newAccountWithUpdatedMetadata;
montelaidev marked this conversation as resolved.
Show resolved Hide resolved

this.messagingSystem.publish(
'AccountsController:accountAdded',
newAccountWithUpdatedMetadata,
);

return accountsState;
}
Expand Down Expand Up @@ -988,6 +1008,12 @@ export class AccountsController extends BaseController<
accountId: string,
): AccountsControllerState['internalAccounts']['accounts'] {
delete accountsState[accountId];

this.messagingSystem.publish(
'AccountsController:accountRemoved',
accountId,
);
montelaidev marked this conversation as resolved.
Show resolved Hide resolved

return accountsState;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/accounts-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export type {
AccountsControllerChangeEvent,
AccountsControllerSelectedAccountChangeEvent,
AccountsControllerSelectedEvmAccountChangeEvent,
AccountsControllerAccountAddedEvent,
AccountsControllerAccountRemovedEvent,
AccountsControllerEvents,
AccountsControllerMessenger,
} from './AccountsController';
Expand Down
Loading