Skip to content

Commit

Permalink
fix: reset auto-lock timer if the vault service is running (#1685)
Browse files Browse the repository at this point in the history
- Closes #1356
- Added E2E to ensure auto-lock behavior.
  • Loading branch information
helciofranco authored Dec 6, 2024
1 parent 882f3ea commit 771962b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-llamas-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": patch
---

Reset auto-lock timer if the Wallet is opened.
2 changes: 1 addition & 1 deletion .github/workflows/pr-tests-e2e-crx-lock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 12 additions & 4 deletions packages/app/playwright/crx/lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ({
Expand Down Expand Up @@ -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');
});
});
});
31 changes: 18 additions & 13 deletions packages/app/src/systems/CRX/background/services/VaultService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class VaultService extends VaultServer {
this.emitLockEvent = this.emitLockEvent.bind(this);

this.communicationProtocol = communicationProtocol;

this.autoUnlock();
this.setupListeners();
}
Expand All @@ -55,25 +56,29 @@ export class VaultService extends VaultServer {
this.emitLockEvent();
}

async isLocked(): Promise<boolean> {
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();
}
Expand Down

0 comments on commit 771962b

Please sign in to comment.