import type { Keyring as KeyringV2 } from '@metamask/keyring-api/v2'; import type { EthKeyring } from '@metamask/keyring-internal-api'; import type { Hex, Json } from '@metamask/utils'; import { describe, expect, it, vi } from 'vitest'; import type { KeyringEntry } from './KeyringController.js'; import { VaultTransaction } from './VaultTransaction.js'; type FakeKeyringOptions = { destroy?: () => Promise; }; const buildFakeKeyring = ({ destroy, }: FakeKeyringOptions = {}): EthKeyring => ({ type: 'Fake Keyring', ...(destroy ? { destroy } : {}), serialize: async (): Promise => ({}), deserialize: async (): Promise => undefined, getAccounts: async (): Promise => [], addAccounts: async (): Promise => [], }); const buildFakeKeyringV2 = ({ destroy, }: FakeKeyringOptions = {}): KeyringV2 => ({ type: 'hd', capabilities: { scopes: ['eip155:1'] }, ...(destroy ? { destroy } : {}), serialize: async (): Promise => ({}), deserialize: async (): Promise => undefined, getAccounts: async () => [], getAccount: async () => { throw new Error('Not implemented'); }, createAccounts: async () => [], deleteAccount: async (): Promise => undefined, submitRequest: async (): Promise => ({}), }); const buildEntry = ( keyring: EthKeyring, keyringV2?: KeyringV2, ): KeyringEntry => ({ keyring, ...(keyringV2 ? { keyringV2 } : {}), metadata: { id: 'keyring-id', name: 'Keyring' }, }); describe('VaultTransaction', () => { describe('scope', () => { it('exposes the wallet scope', () => { const transaction = new VaultTransaction({ type: 'wallet' }); expect(transaction.scope).toStrictEqual({ type: 'wallet' }); }); it('exposes the keyring scope with its entry and created flag', () => { const entry = buildEntry(buildFakeKeyring()); const transaction = new VaultTransaction({ type: 'keyring', entry, created: false, }); expect(transaction.scope).toStrictEqual({ type: 'keyring', entry, created: false, }); }); }); describe('destroy', () => { it('destroys the keyring and its V2 instance', async () => { const keyringDestroy = vi.fn(async () => undefined); const keyringV2Destroy = vi.fn(async () => undefined); const entry = buildEntry( buildFakeKeyring({ destroy: keyringDestroy }), buildFakeKeyringV2({ destroy: keyringV2Destroy }), ); const transaction = new VaultTransaction({ type: 'wallet' }); await transaction.destroy(entry); expect(keyringDestroy).toHaveBeenCalledTimes(1); expect(keyringV2Destroy).toHaveBeenCalledTimes(1); }); it('marks the entry as destroyed even without a destroy method', async () => { const keyring = buildFakeKeyring(); const entry = buildEntry(keyring); const transaction = new VaultTransaction({ type: 'wallet' }); expect(await transaction.destroy(entry)).toBeUndefined(); expect(transaction.isDestroyed(entry)).toBe(true); }); it('marks the entry as destroyed', async () => { const destroy = vi.fn(async () => undefined); const entry = buildEntry(buildFakeKeyring({ destroy })); const transaction = new VaultTransaction({ type: 'wallet' }); await transaction.destroy(entry); expect(destroy).toHaveBeenCalledTimes(1); expect(transaction.isDestroyed(entry)).toBe(true); }); it('attempts V2 destruction before propagating a V1 failure', async () => { const consoleErrorSpy = vi .spyOn(console, 'error') .mockImplementation(() => undefined); const error = new Error('V1 destroy failed'); const keyringDestroy = vi.fn(async () => { throw error; }); const keyringV2Destroy = vi.fn(async () => undefined); const entry = buildEntry( buildFakeKeyring({ destroy: keyringDestroy }), buildFakeKeyringV2({ destroy: keyringV2Destroy }), ); const transaction = new VaultTransaction({ type: 'wallet' }); await expect(transaction.destroy(entry)).rejects.toBe(error); expect(consoleErrorSpy).toHaveBeenCalledExactlyOnceWith(error); expect(keyringDestroy).toHaveBeenCalledTimes(1); expect(keyringV2Destroy).toHaveBeenCalledTimes(1); expect(keyringDestroy.mock.invocationCallOrder[0]).toBeLessThan( keyringV2Destroy.mock.invocationCallOrder[0], ); expect(transaction.isDestroyed(entry)).toBe(true); }); it('logs both failures and throws the first when both destructors fail', async () => { const consoleErrorSpy = vi .spyOn(console, 'error') .mockImplementation(() => undefined); const firstError = new Error('V1 destroy failed'); const secondError = new Error('V2 destroy failed'); const entry = buildEntry( buildFakeKeyring({ destroy: async () => { throw firstError; }, }), buildFakeKeyringV2({ destroy: async () => { throw secondError; }, }), ); const transaction = new VaultTransaction({ type: 'wallet' }); await expect(transaction.destroy(entry)).rejects.toBe(firstError); expect(consoleErrorSpy).toHaveBeenCalledTimes(2); expect(consoleErrorSpy).toHaveBeenNthCalledWith(1, firstError); expect(consoleErrorSpy).toHaveBeenNthCalledWith(2, secondError); expect(transaction.isDestroyed(entry)).toBe(true); }); it('attempts V2 destruction when a V1 failure is ignored', async () => { const consoleErrorSpy = vi .spyOn(console, 'error') .mockImplementation(() => undefined); const error = new Error('V1 destroy failed'); const keyringV2Destroy = vi.fn(async () => undefined); const entry = buildEntry( buildFakeKeyring({ destroy: async () => { throw error; }, }), buildFakeKeyringV2({ destroy: keyringV2Destroy }), ); const transaction = new VaultTransaction({ type: 'wallet' }); expect( await transaction.destroy(entry, { ignoreErrors: true }), ).toBeUndefined(); expect(keyringV2Destroy).toHaveBeenCalledTimes(1); expect(consoleErrorSpy).toHaveBeenCalledExactlyOnceWith(error); expect(transaction.isDestroyed(entry)).toBe(true); }); it('logs both failures when destruction errors are ignored', async () => { const consoleErrorSpy = vi .spyOn(console, 'error') .mockImplementation(() => undefined); const firstError = new Error('V1 destroy failed'); const secondError = new Error('V2 destroy failed'); const entry = buildEntry( buildFakeKeyring({ destroy: async () => { throw firstError; }, }), buildFakeKeyringV2({ destroy: async () => { throw secondError; }, }), ); const transaction = new VaultTransaction({ type: 'wallet' }); expect( await transaction.destroy(entry, { ignoreErrors: true }), ).toBeUndefined(); expect(consoleErrorSpy).toHaveBeenCalledTimes(2); expect(consoleErrorSpy).toHaveBeenNthCalledWith(1, firstError); expect(consoleErrorSpy).toHaveBeenNthCalledWith(2, secondError); expect(transaction.isDestroyed(entry)).toBe(true); }); it('marks the entry as destroyed even when destruction throws', async () => { const entry = buildEntry( buildFakeKeyring({ destroy: async () => { throw new Error('destroy failed'); }, }), ); const transaction = new VaultTransaction({ type: 'wallet' }); await expect(transaction.destroy(entry)).rejects.toThrow( 'destroy failed', ); expect(transaction.isDestroyed(entry)).toBe(true); }); it('marks the entry as destroyed when the V1 destruction succeeds but the V2 one throws', async () => { const keyringDestroy = vi.fn(async () => undefined); const entry = buildEntry( buildFakeKeyring({ destroy: keyringDestroy }), buildFakeKeyringV2({ destroy: async () => { throw new Error('destroy failed'); }, }), ); const transaction = new VaultTransaction({ type: 'wallet' }); await expect(transaction.destroy(entry)).rejects.toThrow( 'destroy failed', ); expect(keyringDestroy).toHaveBeenCalledTimes(1); expect(transaction.isDestroyed(entry)).toBe(true); }); it('propagates destruction errors', async () => { const destroy = vi.fn(async () => { throw new Error('destroy failed'); }); const entry = buildEntry(buildFakeKeyring({ destroy })); const transaction = new VaultTransaction({ type: 'wallet' }); await expect(transaction.destroy(entry)).rejects.toThrow( 'destroy failed', ); expect(destroy).toHaveBeenCalledTimes(1); }); it('logs instead of throwing when errors are ignored', async () => { const consoleErrorSpy = vi .spyOn(console, 'error') .mockImplementation(() => undefined); const destroy = vi.fn(async () => { throw new Error('destroy failed'); }); const entry = buildEntry(buildFakeKeyring({ destroy })); const transaction = new VaultTransaction({ type: 'wallet' }); expect( await transaction.destroy(entry, { ignoreErrors: true }), ).toBeUndefined(); expect(destroy).toHaveBeenCalledTimes(1); expect(consoleErrorSpy).toHaveBeenCalledTimes(1); expect(consoleErrorSpy).toHaveBeenCalledWith(expect.any(Error)); }); it('marks the entry as destroyed when destruction throws and errors are ignored', async () => { vi.spyOn(console, 'error').mockImplementation(() => undefined); const entry = buildEntry( buildFakeKeyring({ destroy: async () => { throw new Error('destroy failed'); }, }), ); const transaction = new VaultTransaction({ type: 'wallet' }); await transaction.destroy(entry, { ignoreErrors: true }); expect(transaction.isDestroyed(entry)).toBe(true); }); it('does not mark entries it did not destroy', async () => { const destroyedEntry = buildEntry(buildFakeKeyring()); const untouchedEntry = buildEntry(buildFakeKeyring()); const transaction = new VaultTransaction({ type: 'wallet' }); await transaction.destroy(destroyedEntry); expect(transaction.isDestroyed(untouchedEntry)).toBe(false); }); }); describe('isDestroyed', () => { it('returns false for untouched entries', () => { const entry = buildEntry(buildFakeKeyring()); const transaction = new VaultTransaction({ type: 'wallet' }); expect(transaction.isDestroyed(entry)).toBe(false); }); it('tracks destroyed entries per transaction instance', async () => { const entry = buildEntry(buildFakeKeyring()); const firstTransaction = new VaultTransaction({ type: 'wallet' }); const secondTransaction = new VaultTransaction({ type: 'wallet' }); await firstTransaction.destroy(entry); expect(firstTransaction.isDestroyed(entry)).toBe(true); expect(secondTransaction.isDestroyed(entry)).toBe(false); }); }); });