Skip to content

Commit

Permalink
fix: fix the issue where codesign cannot sign plugin bindings during …
Browse files Browse the repository at this point in the history
…desktop building. (#6666)
  • Loading branch information
huhuanming authored Feb 13, 2025
1 parent d56305a commit 236f4f2
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 53 deletions.
2 changes: 1 addition & 1 deletion apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@onekeyhq/kit": "*",
"@sentry/electron": "5.8.0",
"adm-zip": "^0.5.10",
"electron-check-biometric-auth-changed": "0.0.4",
"electron-check-biometric-auth-changed": "0.0.6",
"electron-context-menu": "^3.5.0",
"electron-is-dev": "^2.0.0",
"electron-log": "5.2.0",
Expand Down
9 changes: 8 additions & 1 deletion apps/desktop/scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path');
const childProcess = require('child_process');
const { build } = require('esbuild');
const glob = require('glob');
const pkg = require('../package.json');

const electronSource = path.join(__dirname, '..', 'src-electron');
Expand All @@ -13,8 +14,14 @@ const gitRevision = childProcess
const isProduction = process.env.NODE_ENV === 'production';

const hrstart = process.hrtime();

// Get all .js files in service directory
const serviceFiles = glob
.sync(path.join(electronSource, 'service', '*.ts'))
.map((name) => name.split('src-electron/').pop());

build({
entryPoints: ['app.ts', 'preload.ts', 'service/windowsHello.ts'].map((f) =>
entryPoints: ['app.ts', 'preload.ts', ...serviceFiles].map((f) =>
path.join(electronSource, f),
),
platform: 'node',
Expand Down
12 changes: 9 additions & 3 deletions apps/desktop/src-electron/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
shell,
systemPreferences,
} from 'electron';
import { checkBiometricAuthChanged } from 'electron-check-biometric-auth-changed';
import contextMenu from 'electron-context-menu';
import isDev from 'electron-is-dev';
import logger from 'electron-log/main';
Expand Down Expand Up @@ -47,6 +46,7 @@ import { resourcesPath, staticPath } from './resoucePath';
import { initSentry } from './sentry';
import {
checkAvailabilityAsync,
checkBiometricAuthChanged,
requestVerificationAsync,
startServices,
} from './service';
Expand Down Expand Up @@ -581,12 +581,18 @@ function createMainWindow() {
event.returnValue = isDev;
});

ipcMain.on(ipcMessageKeys.CHECK_BIOMETRIC_AUTH_CHANGED, (event) => {
ipcMain.on(ipcMessageKeys.CHECK_BIOMETRIC_AUTH_CHANGED, async (event) => {
if (!isMac) {
event.returnValue = false;
return;
}
event.returnValue = checkBiometricAuthChanged();
try {
const result = await checkBiometricAuthChanged();
event.returnValue = result;
} catch (error) {
logger.error('[CHECK_BIOMETRIC_AUTH_CHANGED] Error:', error);
event.returnValue = false;
}
});

ipcMain.on(ipcMessageKeys.TOUCH_ID_CAN_PROMPT, async (event) => {
Expand Down
25 changes: 25 additions & 0 deletions apps/desktop/src-electron/service/checkBiometricAuthChanged.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { checkBiometricAuthChanged } from 'electron-check-biometric-auth-changed';

import { ECheckBiometricAuthChangedEventType } from './enum';

// Child process
process.parentPort.on(
'message',
(e: {
data: { type: ECheckBiometricAuthChangedEventType; params: unknown };
}) => {
switch (e.data.type) {
case ECheckBiometricAuthChangedEventType.CheckBiometricAuthChanged:
{
const result = checkBiometricAuthChanged();
process.parentPort.postMessage({
type: ECheckBiometricAuthChangedEventType.CheckBiometricAuthChanged,
result,
});
}
break;
default:
break;
}
},
);
4 changes: 4 additions & 0 deletions apps/desktop/src-electron/service/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ export enum EWindowHelloEventType {
CheckAvailabilityAsync = 'checkAvailabilityAsync',
RequestVerificationAsync = 'requestVerificationAsync',
}

export enum ECheckBiometricAuthChangedEventType {
CheckBiometricAuthChanged = 'checkBiometricAuthChanged',
}
128 changes: 88 additions & 40 deletions apps/desktop/src-electron/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,51 @@ import path from 'path';
import { utilityProcess } from 'electron/main';
import Logger from 'electron-log/main';

import { EWindowHelloEventType } from './enum';
import {
ECheckBiometricAuthChangedEventType,
EWindowHelloEventType,
} from './enum';

import type { UtilityProcess } from 'electron/main';

let windowsHelloChildProcess: UtilityProcess | null = null;
let windowsHelloCallbacks: {
type: string;
callback: (e: any) => void;
timestamp: number;
}[] = [];
export const startServices = () => {
windowsHelloChildProcess = utilityProcess.fork(
// After build, the directory is 'dist' and WindowsHello file is located in 'dist/service'
path.join(__dirname, './service/windowsHello.js'),
);
windowsHelloChildProcess.on(
enum EServiceName {
WindowsHello = 'windowsHello',
CheckBiometricAuthChanged = 'checkBiometricAuthChanged',
}

const processConfig: Record<
EServiceName,
{
childProcess: UtilityProcess | null;
callbacks: {
type: string;
callback: (e: any) => void;
timestamp: number;
}[];
}
> = {
[EServiceName.WindowsHello]: {
childProcess: null,
callbacks: [],
},
[EServiceName.CheckBiometricAuthChanged]: {
childProcess: null,
callbacks: [],
},
};

const startService = (key: EServiceName) => {
if (!processConfig[key].childProcess) {
processConfig[key].childProcess = utilityProcess.fork(
path.join(__dirname, `./service/${key}.js`),
);
}

processConfig[key].childProcess?.on(
'message',
(e: { type: string; result: boolean }) => {
Logger.info('windowsHelloChildProcess-onMessage', e);
const callbacks = windowsHelloCallbacks.filter(
Logger.info(`${key}ChildProcess-onMessage`, e);
const callbacks = processConfig[key].callbacks.filter(
(callbackItem) => callbackItem.type === e.type,
);
if (callbacks.length) {
Expand All @@ -32,31 +57,47 @@ export const startServices = () => {
callbackItem.callback(e.result);
}
});
windowsHelloCallbacks = windowsHelloCallbacks.filter(
processConfig[key].callbacks = processConfig[key].callbacks.filter(
(callbackItem) => !callbacks.includes(callbackItem),
);
}
},
);
windowsHelloChildProcess.on('exit', (code) => {
Logger.info('windowsHelloChildProcess--onExit', code);
processConfig[key].childProcess?.on('exit', (code) => {
Logger.info(`${key}ChildProcess--onExit`, code);
});
};
export const startServices = () => {
(Object.keys(processConfig) as EServiceName[]).forEach((key) => {
startService(key);
});
};

const postServiceMessage = <T>(
serviceName: EServiceName,
type: string,
params?: any,
): Promise<T> =>
new Promise<T>((resolve) => {
processConfig[serviceName].callbacks.push({
type,
callback: resolve,
timestamp: Date.now(),
});
processConfig[serviceName].childProcess?.postMessage({
type,
params,
});
});

let cacheWindowsHelloSupported: boolean | null = null;
export const checkAvailabilityAsync = async () => {
if (cacheWindowsHelloSupported === null) {
cacheWindowsHelloSupported = await Promise.race<boolean>([
new Promise<boolean>((resolve) => {
windowsHelloCallbacks.push({
type: EWindowHelloEventType.CheckAvailabilityAsync,
callback: resolve,
timestamp: Date.now(),
});
windowsHelloChildProcess?.postMessage({
type: EWindowHelloEventType.CheckAvailabilityAsync,
});
}),
postServiceMessage<boolean>(
EServiceName.WindowsHello,
EWindowHelloEventType.CheckAvailabilityAsync,
),
new Promise((resolve) =>
setTimeout(() => {
cacheWindowsHelloSupported = false;
Expand All @@ -69,17 +110,24 @@ export const checkAvailabilityAsync = async () => {
};

export const requestVerificationAsync = (message: string) =>
new Promise<{
postServiceMessage<{
success: boolean;
error?: string;
}>((resolve) => {
windowsHelloCallbacks.push({
type: EWindowHelloEventType.RequestVerificationAsync,
callback: resolve,
timestamp: Date.now(),
});
windowsHelloChildProcess?.postMessage({
type: EWindowHelloEventType.RequestVerificationAsync,
params: message,
});
});
}>(
EServiceName.WindowsHello,
EWindowHelloEventType.RequestVerificationAsync,
message,
);

export const checkBiometricAuthChanged = async () =>
Promise.race<boolean>([
postServiceMessage<boolean>(
EServiceName.CheckBiometricAuthChanged,
ECheckBiometricAuthChangedEventType.CheckBiometricAuthChanged,
),
new Promise((resolve) =>
setTimeout(() => {
resolve(false);
}, 500),
),
]);
6 changes: 3 additions & 3 deletions apps/desktop/src-electron/service/windowsHello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ function requestVerificationAsync(
// Child process
process.parentPort.on(
'message',
(e: { data: { type: string; params: unknown } }) => {
(e: { data: { type: EWindowHelloEventType; params: unknown } }) => {
switch (e.data.type) {
case 'checkAvailabilityAsync':
case EWindowHelloEventType.CheckAvailabilityAsync:
checkWindowsHelloAvailability((result) => {
process.parentPort.postMessage({
type: EWindowHelloEventType.CheckAvailabilityAsync,
result,
});
});
break;
case 'requestVerificationAsync':
case EWindowHelloEventType.RequestVerificationAsync:
requestVerificationAsync(e.data.params as string, (result) => {
process.parentPort.postMessage({
type: EWindowHelloEventType.RequestVerificationAsync,
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6998,7 +6998,7 @@ __metadata:
cross-env: "npm:^7.0.3"
electron: "npm:27.3.1"
electron-builder: "npm:24.13.3"
electron-check-biometric-auth-changed: "npm:0.0.4"
electron-check-biometric-auth-changed: "npm:0.0.6"
electron-context-menu: "npm:^3.5.0"
electron-is-dev: "npm:^2.0.0"
electron-log: "npm:5.2.0"
Expand Down Expand Up @@ -22028,14 +22028,14 @@ __metadata:
languageName: node
linkType: hard

"electron-check-biometric-auth-changed@npm:0.0.4":
version: 0.0.4
resolution: "electron-check-biometric-auth-changed@npm:0.0.4"
"electron-check-biometric-auth-changed@npm:0.0.6":
version: 0.0.6
resolution: "electron-check-biometric-auth-changed@npm:0.0.6"
dependencies:
bindings: "npm:^1.5.0"
node-addon-api: "npm:^7.1.0"
node-gyp: "npm:latest"
checksum: 10/c42556e77eb000d6fae2085ceaaa325f5c69d95c96ee7adca311f1cd111f816878cc0ac4b7adae8786c5aaaedd2726dcdf41f467f896eb046a68e3475afcd6c7
checksum: 10/daf0bc6d4cddf7c411d4c315910854c974fbc62c9ea7a12bf23bc368c452f0e0bcff5f4b127de9f4145b62c020e2a02d592b781de730926355ef9e1138af6142
languageName: node
linkType: hard

Expand Down

0 comments on commit 236f4f2

Please sign in to comment.