Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
fix: ignore event if account was already removed (#101)
Browse files Browse the repository at this point in the history
* fix: ignore event if account was already removed

* test: add missing unit test (coverage)
  • Loading branch information
danroc authored Sep 20, 2023
1 parent 122fd63 commit d6a6b31
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
10 changes: 4 additions & 6 deletions src/SnapKeyring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,17 @@ describe('SnapKeyring', () => {
]);
});

it('throws when removing an account that does not exist', async () => {
it('returns null when removing an account that does not exist', async () => {
mockCallbacks.removeAccount.mockImplementation(async (address) => {
await keyring.removeAccount(address);
});

await expect(
keyring.handleKeyringSnapMessage(snapId, {
expect(
await keyring.handleKeyringSnapMessage(snapId, {
method: KeyringEvent.AccountDeleted,
params: { id: 'bcda5b5f-098f-4706-919b-ee919402f0dd' },
}),
).rejects.toThrow(
"Account 'bcda5b5f-098f-4706-919b-ee919402f0dd' not found",
);
).toBeNull();
});

it('fails when the method is not supported', async () => {
Expand Down
17 changes: 12 additions & 5 deletions src/SnapKeyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { CaseInsensitiveMap } from './CaseInsensitiveMap';
import { DeferredPromise } from './DeferredPromise';
import type { SnapMessage } from './types';
import { SnapMessageStruct } from './types';
import { strictMask, throwError, toJson, unique } from './util';
import { strictMask, toJson, unique } from './util';

export const SNAP_KEYRING_TYPE = 'Snap Keyring';

Expand Down Expand Up @@ -89,10 +89,17 @@ export class SnapKeyring extends EventEmitter {

case KeyringEvent.AccountDeleted: {
const { id } = params as any;
const account =
this.#getAccountById(id) ??
throwError(`Account '${id as string}' not found`);
await this.#callbacks.removeAccount(account.address);
const account = this.#getAccountById(id);

// We can ignore the case where the account was already removed from
// the keyring.
//
// This happens when the keyring calls the snap to delete an account,
// and the snap responds with an AccountDeleted event.
if (account !== undefined) {
await this.#callbacks.removeAccount(account.address);
}

return null;
}

Expand Down
8 changes: 7 additions & 1 deletion src/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ensureDefined, toJson, unique } from './util';
import { ensureDefined, throwError, toJson, unique } from './util';

describe('unique', () => {
it('returns an empty array when given an empty array', () => {
Expand Down Expand Up @@ -52,3 +52,9 @@ describe('ensureDefined', () => {
expect(() => ensureDefined(undefined)).toThrow('Argument is undefined');
});
});

describe('throwError', () => {
it('throws an error with the given message', () => {
expect(() => throwError('hello')).toThrow('hello');
});
});

0 comments on commit d6a6b31

Please sign in to comment.