Skip to content

Commit

Permalink
add alarms for lock timer and remove setTimeout and ping function (#1081
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ost-ptk authored Nov 15, 2024
1 parent 8c47e60 commit 1241dfd
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 67 deletions.
46 changes: 23 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions src/apps/popup/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
22 changes: 11 additions & 11 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RootAction, getType } from 'typesafe-actions';
import {
Tabs,
action,
alarms,
browserAction,
management,
runtime,
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand All @@ -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'
Expand Down
4 changes: 0 additions & 4 deletions src/background/redux/last-activity-time/selectors.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/background/redux/root-selector.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
38 changes: 17 additions & 21 deletions src/background/redux/sagas/vault-sagas.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
Expand Down Expand Up @@ -234,39 +234,35 @@ function* unlockVaultSaga(action: ReturnType<typeof unlockVault>) {
}

/**
*
* 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');
}
}

Expand Down
1 change: 1 addition & 0 deletions src/manifest.v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"storage",
"tabs",
"declarativeNetRequest",
"alarms",
"https://image-proxy-cdn.make.services/*",
"https://casper-assets.s3.amazonaws.com/*",
"https://node.cspr.cloud/*",
Expand Down
1 change: 1 addition & 0 deletions src/manifest.v2.safari.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"storage",
"tabs",
"declarativeNetRequest",
"alarms",
"https://image-proxy-cdn.make.services/*",
"https://api.testnet.casperwallet.io/*",
"https://api.mainnet.casperwallet.io/*",
Expand Down

0 comments on commit 1241dfd

Please sign in to comment.