From 771962b6d1c41a3985523524e17961d625e85c3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9lcio=20Franco?= Date: Fri, 6 Dec 2024 18:43:54 +0700 Subject: [PATCH] fix: reset auto-lock timer if the vault service is running (#1685) - Closes https://github.com/FuelLabs/fuels-wallet/issues/1356 - Added E2E to ensure auto-lock behavior. --- .changeset/red-llamas-travel.md | 5 +++ .github/workflows/pr-tests-e2e-crx-lock.yml | 2 +- packages/app/playwright/crx/lock.test.ts | 16 +++++++--- .../CRX/background/services/VaultService.ts | 31 +++++++++++-------- 4 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 .changeset/red-llamas-travel.md diff --git a/.changeset/red-llamas-travel.md b/.changeset/red-llamas-travel.md new file mode 100644 index 000000000..fa683643a --- /dev/null +++ b/.changeset/red-llamas-travel.md @@ -0,0 +1,5 @@ +--- +"fuels-wallet": patch +--- + +Reset auto-lock timer if the Wallet is opened. diff --git a/.github/workflows/pr-tests-e2e-crx-lock.yml b/.github/workflows/pr-tests-e2e-crx-lock.yml index d7bb25d87..b55a602f5 100644 --- a/.github/workflows/pr-tests-e2e-crx-lock.yml +++ b/.github/workflows/pr-tests-e2e-crx-lock.yml @@ -44,7 +44,7 @@ jobs: - name: Run E2E Tests run: xvfb-run --auto-servernum -- pnpm test:e2e:crx-lock - timeout-minutes: 3 + timeout-minutes: 6 env: NODE_ENV: test diff --git a/packages/app/playwright/crx/lock.test.ts b/packages/app/playwright/crx/lock.test.ts index 3e1b397d0..fe442764c 100644 --- a/packages/app/playwright/crx/lock.test.ts +++ b/packages/app/playwright/crx/lock.test.ts @@ -12,10 +12,10 @@ import { WALLET_PASSWORD } from '../mocks'; import { test } from './utils'; // Increase timeout for this test -// The timeout is set for 2 minutes +// The timeout is set for 6 minutes // because some tests like reconnect // can take up to 1 minute before it's reconnected -test.setTimeout(180_000); +test.setTimeout(360_000); test.describe('Lock FuelWallet after inactivity', () => { test('should lock the wallet after 1 minute of inactivity (config in .env file)', async ({ @@ -107,10 +107,18 @@ test.describe('Lock FuelWallet after inactivity', () => { return page; }); - await test.step('Auto lock fuel wallet', async () => { + await test.step('Verify wallet stays unlocked while open', async () => { await getByAriaLabel(popupPage, 'Accounts').click(); await popupPage.waitForTimeout(65_000); - await hasText(popupPage, 'Unlock your wallet to continue'); + await hasText(popupPage, /Assets/i); + }); + + await test.step('Resume auto-lock timer after closing wallet', async () => { + await popupPage.close(); + const page = await context.newPage(); + await page.waitForTimeout(65_000); + await page.goto(`chrome-extension://${extensionId}/popup.html`); + await hasText(page, 'Unlock your wallet to continue'); }); }); }); diff --git a/packages/app/src/systems/CRX/background/services/VaultService.ts b/packages/app/src/systems/CRX/background/services/VaultService.ts index 9e41ff884..e757118e2 100644 --- a/packages/app/src/systems/CRX/background/services/VaultService.ts +++ b/packages/app/src/systems/CRX/background/services/VaultService.ts @@ -31,6 +31,7 @@ export class VaultService extends VaultServer { this.emitLockEvent = this.emitLockEvent.bind(this); this.communicationProtocol = communicationProtocol; + this.autoUnlock(); this.setupListeners(); } @@ -55,25 +56,29 @@ export class VaultService extends VaultServer { this.emitLockEvent(); } - async isLocked(): Promise { - const isWalletLocked = await super.isLocked(); - if (!isWalletLocked) { - const timer = await getTimer(); - if (timer) { - // Reset the timer for wallet auto lock - resetTimer(); - } - } - return isWalletLocked; - } - private startAutoLockTimer() { this.stopAutoLockTimer(); // Clear any existing interval this.autoLockInterval = setInterval(async () => { const timer = await getTimer(); + if (timer === 0) return; - if (timer < Date.now()) { + + // Get all current Wallet PopUp instances that are using VaultService + const origins = Array.from( + this.communicationProtocol.ports.values() + ).filter((p) => p.name === VAULT_SCRIPT_NAME); + + // If we're using the wallet, let's reset the auto lock + const isVaultInUse = origins.length > 0; + if (isVaultInUse) { + await resetTimer(); + return; + } + + // Otherwise, we can check if the autolock time is expired + const isExpired = timer < Date.now(); + if (isExpired) { clearSession(); this.lock(); }