Skip to content

Commit

Permalink
Merge branch 'x' into new-purchase-flow
Browse files Browse the repository at this point in the history
  • Loading branch information
limichange authored Feb 14, 2025
2 parents f7faccb + 6de17f1 commit 781e2db
Show file tree
Hide file tree
Showing 62 changed files with 1,771 additions and 317 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@
"@onekeyfe/cross-inpage-provider-injected": "2.2.10",
"@onekeyfe/cross-inpage-provider-types": "2.2.10",
"@onekeyfe/extension-bridge-hosted": "2.2.10",
"@onekeyfe/hd-ble-sdk": "1.0.20-alpha.1",
"@onekeyfe/hd-core": "1.0.20-alpha.1",
"@onekeyfe/hd-shared": "1.0.20-alpha.1",
"@onekeyfe/hd-transport": "1.0.20-alpha.1",
"@onekeyfe/hd-web-sdk": "1.0.20-alpha.1",
"@onekeyfe/hd-ble-sdk": "^1.0.20-alpha.2",
"@onekeyfe/hd-core": "^1.0.20-alpha.2",
"@onekeyfe/hd-shared": "^1.0.20-alpha.2",
"@onekeyfe/hd-transport": "^1.0.20-alpha.2",
"@onekeyfe/hd-web-sdk": "^1.0.20-alpha.2",
"@onekeyfe/onekey-cross-webview": "2.2.10",
"@polkadot/extension-inject": "0.46.6",
"@polkadot/types": "11.3.1",
Expand Down
Loading

0 comments on commit 781e2db

Please sign in to comment.