Skip to content

Commit aa71ae0

Browse files
committed
fix: adjust auto lock features
1 parent 9cc0b74 commit aa71ae0

File tree

7 files changed

+86
-18
lines changed

7 files changed

+86
-18
lines changed

packages/adena-extension/src/App/use-app.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect } from 'react';
22
import { useLocation } from 'react-router-dom';
33

4+
import { ADENA_WALLET_EXTENSION_ID } from '@common/constants/storage.constant';
45
import { useAccountName } from '@hooks/use-account-name';
56
import { useWalletContext } from '@hooks/use-context';
67
import { useCurrentAccount } from '@hooks/use-current-account';
@@ -19,6 +20,10 @@ const useApp = (): void => {
1920
const { pathname, key } = useLocation();
2021
const { scrollMove } = useScrollHistory();
2122

23+
useEffect(() => {
24+
chrome?.runtime?.connect({ name: ADENA_WALLET_EXTENSION_ID });
25+
}, []);
26+
2227
useEffect(() => {
2328
checkNetworkState();
2429
}, [pathname]);

packages/adena-extension/src/background.ts

+44-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { AlarmKey } from '@common/constants/alarm-key.constant';
1+
import { AlarmKey, SCHEDULE_ALARMS } from '@common/constants/alarm-key.constant';
2+
import { ADENA_WALLET_EXTENSION_ID } from '@common/constants/storage.constant';
23
import { MemoryProvider } from '@common/provider/memory/memory-provider';
34
import { ChromeLocalStorage } from '@common/storage';
45
import { CommandHandler } from '@inject/message/command-handler';
@@ -9,6 +10,8 @@ import { MessageHandler } from './inject/message';
910
const inMemoryProvider = new MemoryProvider();
1011
inMemoryProvider.init();
1112

13+
initAlarms();
14+
1215
function existsWallet(): Promise<boolean> {
1316
const storage = new ChromeLocalStorage();
1417
return storage
@@ -53,33 +56,51 @@ chrome.action.onClicked.addListener(async () => {
5356
});
5457

5558
chrome.runtime.onConnect.addListener(async (port) => {
59+
if (port.name !== ADENA_WALLET_EXTENSION_ID) {
60+
return;
61+
}
62+
5663
inMemoryProvider.addConnection();
57-
await chrome.alarms.clear(AlarmKey.EXPIRED_PASSWORD);
64+
inMemoryProvider.updateExpiredTimeBy(null);
5865

5966
port.onDisconnect.addListener(async () => {
6067
inMemoryProvider.removeConnection();
6168

62-
if (inMemoryProvider.isActive()) {
63-
await chrome.alarms.clear(AlarmKey.EXPIRED_PASSWORD);
64-
} else {
65-
await chrome.alarms.create(AlarmKey.EXPIRED_PASSWORD, {
66-
delayInMinutes: inMemoryProvider.getExpiredPasswordDurationMinutes(),
67-
});
69+
if (!inMemoryProvider.isActive()) {
70+
const expiredTime = new Date().getTime() + inMemoryProvider.getExpiredPasswordDurationTime();
71+
inMemoryProvider.updateExpiredTimeBy(expiredTime);
72+
73+
console.info('Password Expired time:', new Date(expiredTime));
6874
}
6975
});
7076
});
7177

7278
chrome.alarms.onAlarm.addListener(async (alarm) => {
73-
if (alarm.name === AlarmKey.EXPIRED_PASSWORD) {
74-
await chrome.alarms.clear(AlarmKey.EXPIRED_PASSWORD);
79+
try {
80+
const currentTime = new Date().getTime();
81+
chrome.storage.local.set({ SESSION: currentTime });
7582

76-
if (inMemoryProvider.isActive()) {
77-
return;
78-
}
83+
switch (alarm?.name) {
84+
case AlarmKey.EXPIRED_PASSWORD:
85+
if (!inMemoryProvider.isExpired(currentTime)) {
86+
return;
87+
}
88+
89+
await chrome.storage.session.clear();
90+
await clearInMemoryKey(inMemoryProvider);
91+
92+
inMemoryProvider.updateExpiredTimeBy(null);
93+
console.info('Password Expired');
7994

80-
await chrome.storage.session.clear();
81-
await clearInMemoryKey(inMemoryProvider);
95+
break;
96+
default:
97+
break;
98+
}
99+
} catch (error) {
100+
console.error(error);
82101
}
102+
103+
return true;
83104
});
84105

85106
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
@@ -97,3 +118,11 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
97118

98119
return MessageHandler.createHandler(inMemoryProvider, message, sender, sendResponse);
99120
});
121+
122+
function initAlarms(): void {
123+
SCHEDULE_ALARMS.map((alarm) => {
124+
chrome.alarms.create(alarm.key, {
125+
periodInMinutes: alarm.periodInMinutes,
126+
});
127+
});
128+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
export enum AlarmKey {
22
EXPIRED_PASSWORD = 'EXPIRED_PASSWORD',
3+
WAKE_ALARM = 'WAKE_ALARM',
4+
WAKE_ALARM_DELAY_20S = 'WAKE_ALARM_DELAY_20S',
5+
WAKE_ALARM_DELAY_40S = 'WAKE_ALARM_DELAY_40S',
36
}
7+
8+
export const SCHEDULE_ALARMS: { key: string; periodInMinutes: number }[] = [
9+
{ key: AlarmKey.EXPIRED_PASSWORD, periodInMinutes: 1 },
10+
{ key: AlarmKey.WAKE_ALARM, periodInMinutes: 1 },
11+
{ key: AlarmKey.WAKE_ALARM_DELAY_20S, periodInMinutes: 1 },
12+
{ key: AlarmKey.WAKE_ALARM_DELAY_40S, periodInMinutes: 1 },
13+
];

packages/adena-extension/src/common/constants/storage.constant.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const ADENA_WALLET_ID = 'adena-wallet';
2+
export const ADENA_WALLET_EXTENSION_ID = 'adena-wallet-extension';
13
export const WALLET_EXPORT_TYPE_STORAGE_KEY = 'WALLET_EXPORT_TYPE' as const;
24
export const WALLET_EXPORT_ACCOUNT_ID = 'WALLET_EXPORT_ACCOUNT_ID' as const;
35
export const QUESTIONNAIRE_EXPIRATION_MIN = 30 * 24 * 60;

packages/adena-extension/src/common/provider/memory/memory-provider.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export class MemoryProvider {
44
private memory: Map<string, any> = new Map();
55
private activeConnections = 0;
66
private expiredPasswordDuration = EXPIRED_PASSWORD_DURATION_MIN;
7+
private expiredTime: number | null = null;
78

89
public get = <T = any>(key: string): T | null => {
910
if (!this.memory.get(key)) {
@@ -33,11 +34,27 @@ export class MemoryProvider {
3334
return this.activeConnections > 0;
3435
}
3536

36-
public getExpiredPasswordDurationMinutes(): number {
37-
return this.expiredPasswordDuration;
37+
public getExpiredPasswordDurationTime(): number {
38+
return this.expiredPasswordDuration * 60 * 1000;
3839
}
3940

4041
public setExpiredPasswordDurationMinutes(duration: number): void {
4142
this.expiredPasswordDuration = duration;
4243
}
44+
45+
public isExpired(time: number): boolean {
46+
if (this.isActive()) {
47+
return false;
48+
}
49+
50+
if (!this.expiredTime) {
51+
return false;
52+
}
53+
54+
return this.expiredTime < time;
55+
}
56+
57+
public updateExpiredTimeBy(time: number | null): void {
58+
this.expiredTime = time;
59+
}
4360
}

packages/adena-extension/src/content.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { CommandMessageData } from '@inject/message/command-message';
55

66
const sendMessage = (event: MessageEvent): void => {
77
const message = event.data;
8+
89
chrome.runtime.sendMessage(message, (response) => {
910
Promise.resolve(response).then((result) => {
1011
event.source?.postMessage(result, {

packages/adena-extension/src/inject/message/commands/encrypt.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const KEY_LENGTH = 256; // AES-256 key length
66
const IV_LENGTH = 12; // GCM nonce length (12 bytes is recommended)
77

88
export async function getInMemoryKey(memoryProvider: MemoryProvider): Promise<CryptoKey | null> {
9-
const key = memoryProvider.get(MEMORY_KEY) || null;
9+
const key = memoryProvider?.get(MEMORY_KEY) || null;
1010
if (!key) {
1111
const generated = await generateInMemoryKey();
1212
memoryProvider.set(MEMORY_KEY, generated);
@@ -49,6 +49,10 @@ export const decryptPassword = async (
4949
iv: string,
5050
encryptedPassword: string,
5151
): Promise<string> => {
52+
if (!key || !iv || !encryptedPassword) {
53+
return '';
54+
}
55+
5256
const encryptedData = Buffer.from(encryptedPassword, 'base64');
5357
const ivBytes = Buffer.from(iv, 'base64');
5458
const dec = new TextDecoder();

0 commit comments

Comments
 (0)