Skip to content

refactor(keyring-snap-bridge)!: remove saveState from callbacks + use events for account{Created,Updated,Deleted} #242

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions packages/keyring-api/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export const AccountCreatedEventStruct = object({
displayAccountNameSuggestion: exactOptional(boolean()),
}),
});
export type AccountCreatedEvent = Infer<typeof AccountCreatedEventStruct>;
export type AccountCreatedEventPayload = AccountCreatedEvent['params'];

export const AccountUpdatedEventStruct = object({
method: literal(`${KeyringEvent.AccountUpdated}`),
Expand All @@ -81,6 +83,8 @@ export const AccountUpdatedEventStruct = object({
account: KeyringAccountStruct,
}),
});
export type AccountUpdatedEvent = Infer<typeof AccountUpdatedEventStruct>;
export type AccountUpdatedEventPayload = AccountUpdatedEvent['params'];

export const AccountDeletedEventStruct = object({
method: literal(`${KeyringEvent.AccountDeleted}`),
Expand All @@ -91,6 +95,8 @@ export const AccountDeletedEventStruct = object({
id: UuidStruct,
}),
});
export type AccountDeletedEvent = Infer<typeof AccountDeletedEventStruct>;
export type AccountDeletedEventPayload = AccountDeletedEvent['params'];

export const RequestApprovedEventStruct = object({
method: literal(`${KeyringEvent.RequestApproved}`),
Expand Down
30 changes: 24 additions & 6 deletions packages/keyring-snap-bridge/src/SnapKeyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ export type KeyringState = {
* These callbacks are used to interact with other components.
*/
export type SnapKeyringCallbacks = {
saveState: () => Promise<void>;

addressExists(address: string): Promise<boolean>;

addAccount(
Expand Down Expand Up @@ -265,8 +263,16 @@ export class SnapKeyring extends EventEmitter {
// event, if anything goes wrong, we will delete the account by
// calling `deleteAccount` on the Snap.
// eslint-disable-next-line no-void
void this.#callbacks
.saveState()
void this.#rePublishAccountEvent(
'SnapKeyring:accountCreated',
// To keep the retro-compatibility, `message.params` might not have a
// valid `scopes`, so we just re-use `account` here instead of the
// one coming from `message.params`.
{
...message.params,
account,
},
)
.then(() => {
// This allows the MetaMask client to be "notified" when then
// Snap keyring has truly persisted its state. From there, we should
Expand Down Expand Up @@ -323,7 +329,16 @@ export class SnapKeyring extends EventEmitter {
}

this.#accounts.set(newAccount.id, { account: newAccount, snapId });
await this.#callbacks.saveState();
await this.#rePublishAccountEvent(
'SnapKeyring:accountUpdated',
// To keep the retro-compatibility, `message.params` might not have a
// valid `scopes`, so we just re-use `newAccount` here instead of the
// one coming from `message.params`.
{
...message.params,
account: newAccount,
},
);
return null;
}

Expand Down Expand Up @@ -360,7 +375,10 @@ export class SnapKeyring extends EventEmitter {
snapId,
async (accepted) => {
if (accepted) {
await this.#callbacks.saveState();
await this.#rePublishAccountEvent(
'SnapKeyring:accountDeleted',
message.params,
);
}
},
);
Expand Down
21 changes: 21 additions & 0 deletions packages/keyring-snap-bridge/src/SnapKeyringMessenger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { RestrictedMessenger } from '@metamask/base-controller';
import type {
AccountCreatedEventPayload,
AccountUpdatedEventPayload,
AccountDeletedEventPayload,
AccountAssetListUpdatedEventPayload,
AccountBalancesUpdatedEventPayload,
AccountTransactionsUpdatedEventPayload,
Expand All @@ -11,6 +14,21 @@ export type SnapKeyringGetAccountsAction = {
handler: () => string[];
};

export type SnapKeyringAccountCreatedEvent = {
type: `SnapKeyring:accountCreated`;
payload: [AccountCreatedEventPayload];
};

export type SnapKeyringAccountUpdatedEvent = {
type: `SnapKeyring:accountUpdated`;
payload: [AccountUpdatedEventPayload];
};

export type SnapKeyringAccountDeletedEvent = {
type: `SnapKeyring:accountDeleted`;
payload: [AccountDeletedEventPayload];
};

export type SnapKeyringAccountBalancesUpdatedEvent = {
type: `SnapKeyring:accountBalancesUpdated`;
payload: [AccountBalancesUpdatedEventPayload];
Expand All @@ -27,6 +45,9 @@ export type SnapKeyringAccountTransactionsUpdatedEvent = {
};

export type SnapKeyringEvents =
| SnapKeyringAccountCreatedEvent
| SnapKeyringAccountUpdatedEvent
| SnapKeyringAccountDeletedEvent
| SnapKeyringAccountAssetListUpdatedEvent
| SnapKeyringAccountBalancesUpdatedEvent
| SnapKeyringAccountTransactionsUpdatedEvent;
Expand Down
Loading