Skip to content

Commit

Permalink
feat: remove node-keytar lib
Browse files Browse the repository at this point in the history
  • Loading branch information
kwoktung committed Oct 7, 2023
1 parent c5afc83 commit 170aa8c
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 93 deletions.
12 changes: 0 additions & 12 deletions packages/desktop/development/build_keytar.sh

This file was deleted.

9 changes: 3 additions & 6 deletions packages/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
"private": true,
"author": "OneKey <[email protected]>",
"scripts": {
"postinstall": "yarn keytar",
"keytar": "bash development/build_keytar.sh",
"lint": "tsc --noEmit && eslint . --ext .ts,.tsx",
"lint:fix": "tsc --noEmit && eslint . --ext .ts,.tsx --fix",
"dev": "yarn keytar && npx concurrently \"yarn build:main\" \"yarn dev:renderer\" \"cross-env LAUNCH_ELECTRON=true node scripts/dev.js\"",
"dev": "npx concurrently \"yarn build:main\" \"yarn dev:renderer\" \"cross-env LAUNCH_ELECTRON=true node scripts/dev.js\"",
"dev:main": "electron --inspect=5858 dist/app.js",
"dev:renderer": "TRANSFORM_REGENERATOR_DISABLED=true BROWSER=none WEB_PORT=3001 expo start --web",
"clean": "rimraf ./build-electron && rimraf ./build && rimraf ./dist && rimraf node_modules && rimraf .expo && rimraf __generated__",
Expand All @@ -21,9 +19,9 @@
"build:electron:winms": "electron-builder build -w --config electron-builder-ms.config.js",
"build:electron:mac": "electron-builder build -m --config electron-builder.config.js",
"build:electron:mas": "electron-builder build -m --config electron-builder-mas.config.js",
"build": "yarn keytar && NODE_ENV=production sh -c \"yarn clean:build && yarn build:renderer && yarn build:main && yarn build:electron --publish never\"",
"build": "NODE_ENV=production sh -c \"yarn clean:build && yarn build:renderer && yarn build:main && yarn build:electron --publish never\"",
"build:mac": "NODE_ENV=production sh -c \"yarn clean:build && yarn build:renderer && yarn build:main && yarn build:electron:mac --publish never\"",
"build:mas": "yarn keytar && NODE_ENV=production sh -c \"yarn clean:build && yarn build:renderer && yarn build:main && yarn build:electron:mas --publish never\"",
"build:mas": "NODE_ENV=production sh -c \"yarn clean:build && yarn build:renderer && yarn build:main && yarn build:electron:mas --publish never\"",
"build:winms": "NODE_ENV=production && DESK_CHANNEL=ms-store sh -c \"yarn clean:build && yarn build:renderer && yarn build:main && yarn build:electron:winms --publish never\"",
"publish:all": "NODE_ENV=production sh -c \"yarn clean:build && yarn build:renderer && yarn build:main && yarn build:electron --publish always\"",
"publish:winms": "NODE_ENV=production && DESK_CHANNEL=ms-store sh -c \"yarn clean:build && yarn build:renderer && yarn build:main && yarn build:electron:winms --publish always\""
Expand All @@ -39,7 +37,6 @@
"electron-store": "^8.1.0",
"electron-updater": "^5.2.1",
"expo": "49.0.6",
"keytar": "^7.9.0",
"node-fetch": "^2.6.7"
},
"devDependencies": {
Expand Down
18 changes: 18 additions & 0 deletions packages/desktop/src-electron/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,24 @@ function createMainWindow() {
}
});

ipcMain.on(
'app/secureSetItemAsync',
(event, { key, value }: { key: string; value: string }) => {
store.setSecureItem(key, value);
event.returnValue = '';
},
);

ipcMain.on('app/secureGetItemAsync', (event, { key }: { key: string }) => {
const value = store.getSecureItem(key);
event.returnValue = value;
});

ipcMain.on('app/secureDelItemAsync', (event, { key }: { key: string }) => {
store.clearSecureItem(key);
event.returnValue = '';
});

ipcMain.on('app/reloadBridgeProcess', (event) => {
logger.debug('reloadBridgeProcess receive');
restartBridge();
Expand Down
30 changes: 27 additions & 3 deletions packages/desktop/src-electron/libs/store.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
import { safeStorage } from 'electron';
import Store from 'electron-store';

const store = new Store();

export type LocalStore = {
getUpdateSettings(): UpdateSettings;
setUpdateSettings(updateSettings: UpdateSettings): void;
clear(): void;
clearUpdateSettings(): void;
};

export type UpdateSettings = {
useTestFeedUrl: boolean;
};

const EncryptedData = 'EncryptedData';

export const getUpdateSettings = (): UpdateSettings =>
store.get('updateSettings', { useTestFeedUrl: false }) as UpdateSettings;

export const setUpdateSettings = (updateSettings: UpdateSettings): void => {
store.set('updateSettings', updateSettings);
};

export const clear = () => {
store.clear();
export const clearUpdateSettings = () => {
store.delete('updateSettings');
};

export const getSecureItem = (key: string) => {
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;
}
};

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);
};

export const clearSecureItem = (key: string) => {
const items = store.get(EncryptedData, {}) as Record<string, string>;
delete items[key];
store.set(EncryptedData, items);
};
29 changes: 3 additions & 26 deletions packages/desktop/src-electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,6 @@ import { ipcRenderer } from 'electron';

import type { UpdateSettings } from './libs/store';

let keytar = {
async setPassword(...args: any[]) {
// noop
console.error('keytar.setPassword() not working.');
},
async getPassword(...args: any[]) {
console.error('keytar.getPassword() not working.');
return Promise.resolve('');
},
async deletePassword(...args: any[]) {
console.error('keytar.deletePassword() not working.');
return Promise.resolve('');
},
};

try {
// eslint-disable-next-line global-require
keytar = require('keytar');
} catch (error: any) {
// Error: dlopen(//app-monorepo/node_modules/keytar/build/Release/keytar.node, 0x0001): tried: '//app-monorepo/node_modules/keytar/build/Release/keytar.node' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))
console.error(error);
}

export type PrefType =
| 'camera'
| 'bluetooth'
Expand Down Expand Up @@ -201,13 +178,13 @@ const desktopApi = {
ipcRenderer.send('app/promptTouchID', msg);
}),
secureSetItemAsync(key: string, value: string) {
return keytar.setPassword('OneKey', key, value);
return ipcRenderer.sendSync('app/secureSetItemAsync', { key, value });
},
secureGetItemAsync(key: string) {
return keytar.getPassword('OneKey', key);
return ipcRenderer.sendSync('app/secureGetItemAsync', { key });
},
secureDelItemAsync(key: string) {
return keytar.deletePassword('OneKey', key);
return ipcRenderer.sendSync('app/secureDelItemAsync', { key });
},
reloadBridgeProcess: () => {
ipcRenderer.send('app/reloadBridgeProcess');
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/src-electron/process/AutoUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const init = ({ mainWindow, store }: Dependencies) => {

ipcMain.on('update/clearSettings', () => {
logger.info('auto-update', 'clear update settings');
store.clear();
store.clearUpdateSettings();
});
};

Expand Down
24 changes: 0 additions & 24 deletions patches/keytar+7.9.0.patch

This file was deleted.

21 changes: 0 additions & 21 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6553,7 +6553,6 @@ __metadata:
esbuild: ^0.15.11
expo: 49.0.6
glob: ^7.2.0
keytar: ^7.9.0
node-fetch: ^2.6.7
rimraf: ^3
languageName: unknown
Expand Down Expand Up @@ -23915,17 +23914,6 @@ __metadata:
languageName: node
linkType: hard

"keytar@npm:^7.9.0":
version: 7.9.0
resolution: "keytar@npm:7.9.0"
dependencies:
node-addon-api: ^4.3.0
node-gyp: latest
prebuild-install: ^7.0.1
checksum: 4dbdd21f69e21a53032cbc949847f57338e42df763c5eec04e1b5d7142a689f95d8c3d74fb3b7dc321b5d678271d8d8d1a0dcaa919673ebc50ef8ce76f354e21
languageName: node
linkType: hard

"keyv@npm:^4.0.0":
version: 4.0.4
resolution: "keyv@npm:4.0.4"
Expand Down Expand Up @@ -26429,15 +26417,6 @@ __metadata:
languageName: node
linkType: hard

"node-addon-api@npm:^4.3.0":
version: 4.3.0
resolution: "node-addon-api@npm:4.3.0"
dependencies:
node-gyp: latest
checksum: 3de396e23cc209f539c704583e8e99c148850226f6e389a641b92e8967953713228109f919765abc1f4355e801e8f41842f96210b8d61c7dcc10a477002dcf00
languageName: node
linkType: hard

"node-addon-api@npm:^5.0.0":
version: 5.1.0
resolution: "node-addon-api@npm:5.1.0"
Expand Down

0 comments on commit 170aa8c

Please sign in to comment.