From 170aa8c309d7d9dca8c5e4b25548f85a3c2813d4 Mon Sep 17 00:00:00 2001 From: kwoktung Date: Thu, 28 Sep 2023 11:46:25 +0800 Subject: [PATCH] feat: remove node-keytar lib --- packages/desktop/development/build_keytar.sh | 12 -------- packages/desktop/package.json | 9 ++---- packages/desktop/src-electron/app.ts | 18 +++++++++++ packages/desktop/src-electron/libs/store.ts | 30 +++++++++++++++++-- packages/desktop/src-electron/preload.ts | 29 ++---------------- .../src-electron/process/AutoUpdate.ts | 2 +- patches/keytar+7.9.0.patch | 24 --------------- yarn.lock | 21 ------------- 8 files changed, 52 insertions(+), 93 deletions(-) delete mode 100644 packages/desktop/development/build_keytar.sh delete mode 100644 patches/keytar+7.9.0.patch diff --git a/packages/desktop/development/build_keytar.sh b/packages/desktop/development/build_keytar.sh deleted file mode 100644 index c26ebcb4466..00000000000 --- a/packages/desktop/development/build_keytar.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash - -set -x - -echo $(pwd) -echo $(uname) -echo $(arch) - -cd ../../node_modules/keytar -npx node-gyp rebuild -file build/Release/keytar.node - diff --git a/packages/desktop/package.json b/packages/desktop/package.json index 3eb74625979..4f077505dda 100644 --- a/packages/desktop/package.json +++ b/packages/desktop/package.json @@ -6,11 +6,9 @@ "private": true, "author": "OneKey ", "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__", @@ -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\"" @@ -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": { diff --git a/packages/desktop/src-electron/app.ts b/packages/desktop/src-electron/app.ts index 51524b03f36..c631ca1d72e 100644 --- a/packages/desktop/src-electron/app.ts +++ b/packages/desktop/src-electron/app.ts @@ -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(); diff --git a/packages/desktop/src-electron/libs/store.ts b/packages/desktop/src-electron/libs/store.ts index 5849f721c4f..a4c1104c385 100644 --- a/packages/desktop/src-electron/libs/store.ts +++ b/packages/desktop/src-electron/libs/store.ts @@ -1,3 +1,4 @@ +import { safeStorage } from 'electron'; import Store from 'electron-store'; const store = new Store(); @@ -5,13 +6,15 @@ 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; @@ -19,6 +22,27 @@ 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; + 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; + items[key] = safeStorage.encryptString(value).toString('hex'); + store.set(EncryptedData, items); +}; + +export const clearSecureItem = (key: string) => { + const items = store.get(EncryptedData, {}) as Record; + delete items[key]; + store.set(EncryptedData, items); }; diff --git a/packages/desktop/src-electron/preload.ts b/packages/desktop/src-electron/preload.ts index 190553a395c..9a1d27eda95 100644 --- a/packages/desktop/src-electron/preload.ts +++ b/packages/desktop/src-electron/preload.ts @@ -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' @@ -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'); diff --git a/packages/desktop/src-electron/process/AutoUpdate.ts b/packages/desktop/src-electron/process/AutoUpdate.ts index 7b4ec773da3..80e906d2a87 100644 --- a/packages/desktop/src-electron/process/AutoUpdate.ts +++ b/packages/desktop/src-electron/process/AutoUpdate.ts @@ -193,7 +193,7 @@ const init = ({ mainWindow, store }: Dependencies) => { ipcMain.on('update/clearSettings', () => { logger.info('auto-update', 'clear update settings'); - store.clear(); + store.clearUpdateSettings(); }); }; diff --git a/patches/keytar+7.9.0.patch b/patches/keytar+7.9.0.patch deleted file mode 100644 index f0f93333945..00000000000 --- a/patches/keytar+7.9.0.patch +++ /dev/null @@ -1,24 +0,0 @@ -diff --git a/node_modules/keytar/binding.gyp b/node_modules/keytar/binding.gyp -index 9be3dcb..df66b96 100644 ---- a/node_modules/keytar/binding.gyp -+++ b/node_modules/keytar/binding.gyp -@@ -7,7 +7,18 @@ - ], - 'cflags!': [ '-fno-exceptions' ], - 'cflags_cc!': [ '-fno-exceptions' ], -- 'xcode_settings': { 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES', -+ 'xcode_settings': { -+ 'OTHER_CFLAGS': [ -+ '-arch x86_64', -+ '-arch arm64' -+ ], -+ 'OTHER_LDFLAGS': [ -+ '-Wl, -bind_at_load', -+ '-framework CoreFoundation -framework CoreServices', -+ '-arch x86_64', -+ '-arch arm64' -+ ], -+ 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES', - 'CLANG_CXX_LIBRARY': 'libc++', - 'MACOSX_DEPLOYMENT_TARGET': '10.7', - }, diff --git a/yarn.lock b/yarn.lock index ddbc10691b3..174f071c62e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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" @@ -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"