Skip to content

Commit

Permalink
feat: update src-electron/libs/store
Browse files Browse the repository at this point in the history
  • Loading branch information
kwoktung committed Oct 18, 2023
1 parent 1871d01 commit a34d8e6
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions packages/desktop/src-electron/libs/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,37 @@ export const clearUpdateSettings = () => {
};

export const getSecureItem = (key: string) => {
const available = safeStorage.isEncryptionAvailable();
if (!available) {
console.error('safeStorage is not available');
return undefined;
}
const item = store.get(EncryptedData, {}) as Record<string, string>;
const value = item[key];
if (value) {
const result = safeStorage.decryptString(Buffer.from(value, 'hex'));
return result;
try {
const result = safeStorage.decryptString(Buffer.from(value, 'hex'));
return result;
} catch (e) {
console.error(`failed to decrypt ${key}`, e);
return undefined;
}
}
};

export const setSecureItem = (key: string, value: string): void => {
const items = store.get(EncryptedData, {}) as Record<string, string>;
items[key] = safeStorage.encryptString(value).toString('hex');
store.set(EncryptedData, items);
const available = safeStorage.isEncryptionAvailable();
if (!available) {
console.error('safeStorage is not available');
return undefined;
}
try {
const items = store.get(EncryptedData, {}) as Record<string, string>;
items[key] = safeStorage.encryptString(value).toString('hex');
store.set(EncryptedData, items);
} catch (e) {
console.error(`failed to encrypt ${key} ${value}`, e);
}
};

export const clearSecureItem = (key: string) => {
Expand Down

0 comments on commit a34d8e6

Please sign in to comment.