diff --git a/package-lock.json b/package-lock.json index ef65cb0a9..9f1085707 100644 --- a/package-lock.json +++ b/package-lock.json @@ -67,7 +67,7 @@ "@babel/preset-env": "7.23.2", "@babel/preset-react": "7.18.6", "@babel/preset-typescript": "^7.23.3", - "@playwright/test": "^1.47.2", + "@playwright/test": "^1.48.1", "@redux-devtools/cli": "^4.0.0", "@redux-devtools/remote": "^0.9.3", "@testing-library/dom": "9.3.4", @@ -5000,12 +5000,12 @@ "dev": true }, "node_modules/@playwright/test": { - "version": "1.47.2", - "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.47.2.tgz", - "integrity": "sha512-jTXRsoSPONAs8Za9QEQdyjFn+0ZQFjCiIztAIF6bi1HqhBzG9Ma7g1WotyiGqFSBRZjIEqMdT8RUlbk1QVhzCQ==", + "version": "1.48.1", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.48.1.tgz", + "integrity": "sha512-s9RtWoxkOLmRJdw3oFvhFbs9OJS0BzrLUc8Hf6l2UdCNd1rqeEyD4BhCJkvzeEoD1FsK4mirsWwGerhVmYKtZg==", "dev": true, "dependencies": { - "playwright": "1.47.2" + "playwright": "1.48.1" }, "bin": { "playwright": "cli.js" @@ -23661,12 +23661,12 @@ } }, "node_modules/playwright": { - "version": "1.47.2", - "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.47.2.tgz", - "integrity": "sha512-nx1cLMmQWqmA3UsnjaaokyoUpdVaaDhJhMoxX2qj3McpjnsqFHs516QAKYhqHAgOP+oCFTEOCOAaD1RgD/RQfA==", + "version": "1.48.1", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.48.1.tgz", + "integrity": "sha512-j8CiHW/V6HxmbntOfyB4+T/uk08tBy6ph0MpBXwuoofkSnLmlfdYNNkFTYD6ofzzlSqLA1fwH4vwvVFvJgLN0w==", "dev": true, "dependencies": { - "playwright-core": "1.47.2" + "playwright-core": "1.48.1" }, "bin": { "playwright": "cli.js" @@ -23679,9 +23679,9 @@ } }, "node_modules/playwright-core": { - "version": "1.47.2", - "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.47.2.tgz", - "integrity": "sha512-3JvMfF+9LJfe16l7AbSmU555PaTl2tPyQsVInqm3id16pdDfvZ8TTZ/pyzmkbDrZTQefyzU7AIHlZqQnxpqHVQ==", + "version": "1.48.1", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.48.1.tgz", + "integrity": "sha512-Yw/t4VAFX/bBr1OzwCuOMZkY1Cnb4z/doAFSwf4huqAGWmf9eMNjmK7NiOljCdLmxeRYcGPPmcDgU0zOlzP0YA==", "dev": true, "bin": { "playwright-core": "cli.js" @@ -33225,12 +33225,12 @@ } }, "@playwright/test": { - "version": "1.47.2", - "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.47.2.tgz", - "integrity": "sha512-jTXRsoSPONAs8Za9QEQdyjFn+0ZQFjCiIztAIF6bi1HqhBzG9Ma7g1WotyiGqFSBRZjIEqMdT8RUlbk1QVhzCQ==", + "version": "1.48.1", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.48.1.tgz", + "integrity": "sha512-s9RtWoxkOLmRJdw3oFvhFbs9OJS0BzrLUc8Hf6l2UdCNd1rqeEyD4BhCJkvzeEoD1FsK4mirsWwGerhVmYKtZg==", "dev": true, "requires": { - "playwright": "1.47.2" + "playwright": "1.48.1" } }, "@pnpm/network.ca-file": { @@ -47370,19 +47370,19 @@ } }, "playwright": { - "version": "1.47.2", - "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.47.2.tgz", - "integrity": "sha512-nx1cLMmQWqmA3UsnjaaokyoUpdVaaDhJhMoxX2qj3McpjnsqFHs516QAKYhqHAgOP+oCFTEOCOAaD1RgD/RQfA==", + "version": "1.48.1", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.48.1.tgz", + "integrity": "sha512-j8CiHW/V6HxmbntOfyB4+T/uk08tBy6ph0MpBXwuoofkSnLmlfdYNNkFTYD6ofzzlSqLA1fwH4vwvVFvJgLN0w==", "dev": true, "requires": { "fsevents": "2.3.2", - "playwright-core": "1.47.2" + "playwright-core": "1.48.1" } }, "playwright-core": { - "version": "1.47.2", - "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.47.2.tgz", - "integrity": "sha512-3JvMfF+9LJfe16l7AbSmU555PaTl2tPyQsVInqm3id16pdDfvZ8TTZ/pyzmkbDrZTQefyzU7AIHlZqQnxpqHVQ==", + "version": "1.48.1", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.48.1.tgz", + "integrity": "sha512-Yw/t4VAFX/bBr1OzwCuOMZkY1Cnb4z/doAFSwf4huqAGWmf9eMNjmK7NiOljCdLmxeRYcGPPmcDgU0zOlzP0YA==", "dev": true }, "possible-typed-array-names": { diff --git a/package.json b/package.json index 461c1f2ea..2f2347639 100644 --- a/package.json +++ b/package.json @@ -111,7 +111,7 @@ "@babel/preset-env": "7.23.2", "@babel/preset-react": "7.18.6", "@babel/preset-typescript": "^7.23.3", - "@playwright/test": "^1.47.2", + "@playwright/test": "^1.48.1", "@redux-devtools/cli": "^4.0.0", "@redux-devtools/remote": "^0.9.3", "@testing-library/dom": "9.3.4", diff --git a/src/apps/popup/constants.ts b/src/apps/popup/constants.ts index 431b4d240..b8c95a476 100644 --- a/src/apps/popup/constants.ts +++ b/src/apps/popup/constants.ts @@ -8,12 +8,12 @@ export enum TimeoutDurationSetting { } export const MapTimeoutDurationSettingToValue = { - [TimeoutDurationSetting['1 min']]: 1000 * 60 * 1, - [TimeoutDurationSetting['5 min']]: 1000 * 60 * 5, - [TimeoutDurationSetting['15 min']]: 1000 * 60 * 15, - [TimeoutDurationSetting['30 min']]: 1000 * 60 * 30, - [TimeoutDurationSetting['1 hour']]: 1000 * 60 * 60, - [TimeoutDurationSetting['24 hours']]: 1000 * 60 * 60 * 24 + [TimeoutDurationSetting['1 min']]: 1, + [TimeoutDurationSetting['5 min']]: 5, + [TimeoutDurationSetting['15 min']]: 15, + [TimeoutDurationSetting['30 min']]: 30, + [TimeoutDurationSetting['1 hour']]: 60, + [TimeoutDurationSetting['24 hours']]: 60 * 24 }; export const LOCK_VAULT_TIMEOUT = 1000 * 60 * 5; diff --git a/src/background/index.ts b/src/background/index.ts index b6f6761c8..b0d1019aa 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -3,6 +3,7 @@ import { RootAction, getType } from 'typesafe-actions'; import { Tabs, action, + alarms, browserAction, management, runtime, @@ -274,6 +275,16 @@ tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { } }); +// Dispatch the lockVault action when the 'vaultLock' alarm is triggered +alarms.onAlarm.addListener(async function (alarm) { + const store = await getExistingMainStoreSingletonOrInit(); + + if (alarm.name === 'vaultLock') { + // Dispatch the lockVault action to the main store + store.dispatch(lockVault()); + } +}); + // NOTE: if two events are send at the same time (same function) it must reuse the same store instance runtime.onMessage.addListener( async ( @@ -720,9 +731,6 @@ runtime.onMessage.addListener( ); } } else { - if (action === 'ping') { - return; - } // this is added for not spamming with errors from bringweb3 if ('from' in action) { // @ts-ignore @@ -736,14 +744,6 @@ runtime.onMessage.addListener( } ); -// ping mechanism to keep background script from destroing wallet session when it's unlocked -function ping() { - runtime.sendMessage('ping').catch(() => { - // ping - }); -} -setInterval(ping, 15000); - bringInitBackground({ identifier: process.env.PLATFORM_IDENTIFIER || '', // The identifier key you obtained from Bringweb3 apiEndpoint: process.env.NODE_ENV === 'production' ? 'prod' : 'sandbox' diff --git a/src/background/redux/last-activity-time/selectors.ts b/src/background/redux/last-activity-time/selectors.ts deleted file mode 100644 index 7e49a5314..000000000 --- a/src/background/redux/last-activity-time/selectors.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { RootState } from 'typesafe-actions'; - -export const selectVaultLastActivityTime = (state: RootState): number | null => - state.lastActivityTime; diff --git a/src/background/redux/root-selector.ts b/src/background/redux/root-selector.ts index ddcd68aa4..1629cfe22 100644 --- a/src/background/redux/root-selector.ts +++ b/src/background/redux/root-selector.ts @@ -1,6 +1,5 @@ export * from './active-origin/selectors'; export * from './keys/selectors'; -export * from './last-activity-time/selectors'; export * from './login-retry-count/selectors'; export * from './login-retry-lockout-time/selectors'; export * from './session/selectors'; diff --git a/src/background/redux/sagas/vault-sagas.ts b/src/background/redux/sagas/vault-sagas.ts index 567644534..160d17d1a 100644 --- a/src/background/redux/sagas/vault-sagas.ts +++ b/src/background/redux/sagas/vault-sagas.ts @@ -1,5 +1,6 @@ import { put, select, takeLatest } from 'redux-saga/effects'; import { getType } from 'typesafe-actions'; +import { alarms } from 'webextension-polyfill'; import { getUrlOrigin } from '@src/utils'; @@ -24,7 +25,6 @@ import { Account } from '@libs/types/account'; import { accountInfoReset } from '../account-info/actions'; import { keysUpdated } from '../keys/actions'; import { lastActivityTimeRefreshed } from '../last-activity-time/actions'; -import { selectVaultLastActivityTime } from '../last-activity-time/selectors'; import { loginRetryCountReseted } from '../login-retry-count/actions'; import { encryptionKeyHashCreated, @@ -234,39 +234,35 @@ function* unlockVaultSaga(action: ReturnType) { } /** - * + * This saga function is responsible for managing the vault timeout and locking mechanism. + * It checks if the vault exists and is not locked, retrieves the vault timeout duration setting, + * calculates the timeout duration value based on the setting, and creates an alarm to lock the vault. + * If an error occurs during the execution, it logs the error. */ function* timeoutCounterSaga() { - const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); - try { + // Check if the vault exists and is not locked const vaultDoesExist = yield* sagaSelect(selectVaultCipherDoesExist); const vaultIsLocked = yield* sagaSelect(selectVaultIsLocked); - const vaultLastActivityTime = yield* sagaSelect( - selectVaultLastActivityTime - ); + + // Get the vault timeout duration setting const vaultTimeoutDurationSetting = yield* sagaSelect( selectTimeoutDurationSetting ); + + // Calculate the timeout duration value based on the setting const timeoutDurationValue = MapTimeoutDurationSettingToValue[vaultTimeoutDurationSetting]; - if (vaultDoesExist && !vaultIsLocked && vaultLastActivityTime) { - const currentTime = Date.now(); - const timeoutExpired = - currentTime - vaultLastActivityTime >= timeoutDurationValue; - - if (timeoutExpired) { - yield put(lockVault()); - } else { - yield* sagaCall(delay, timeoutDurationValue); - yield put(lockVault()); - } + // If the vault exists and is not locked, create an alarm to lock the vault + if (vaultDoesExist && !vaultIsLocked) { + alarms.create('vaultLock', { + delayInMinutes: timeoutDurationValue + }); } } catch (err) { - console.error(err); - } finally { - // + // Log any errors that occur during the execution of the saga + console.error(err, 'err'); } } diff --git a/src/manifest.v2.json b/src/manifest.v2.json index bdd108a6c..c50252570 100755 --- a/src/manifest.v2.json +++ b/src/manifest.v2.json @@ -11,6 +11,7 @@ "storage", "tabs", "declarativeNetRequest", + "alarms", "https://image-proxy-cdn.make.services/*", "https://casper-assets.s3.amazonaws.com/*", "https://node.cspr.cloud/*", diff --git a/src/manifest.v2.safari.json b/src/manifest.v2.safari.json index 5923358dd..8cd0492af 100644 --- a/src/manifest.v2.safari.json +++ b/src/manifest.v2.safari.json @@ -12,6 +12,7 @@ "storage", "tabs", "declarativeNetRequest", + "alarms", "https://image-proxy-cdn.make.services/*", "https://api.testnet.casperwallet.io/*", "https://api.mainnet.casperwallet.io/*",