@onekeyfe/hd-transport-electron 1.2.0-alpha.54 → 1.2.0-alpha.56
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{index-5f728113.js → index-c1642d47.js} +1 -1
- package/dist/index.js +1 -1
- package/dist/{noble-ble-handler-d58af248.js → noble-ble-handler-e98d7b00.js} +9 -5
- package/dist/noble-ble-handler.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/__tests__/noble-ble-handler.test.ts +142 -0
- package/src/noble-ble-handler.ts +13 -4
|
@@ -32,7 +32,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
32
32
|
|
|
33
33
|
function initNobleBleSupport(webContents) {
|
|
34
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
-
const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-
|
|
35
|
+
const { setupNobleBleHandlers } = yield Promise.resolve().then(function () { return require('./noble-ble-handler-e98d7b00.js'); });
|
|
36
36
|
setupNobleBleHandlers(webContents);
|
|
37
37
|
});
|
|
38
38
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var index = require('./index-
|
|
3
|
+
var index = require('./index-c1642d47.js');
|
|
4
4
|
var hdShared = require('@onekeyfe/hd-shared');
|
|
5
5
|
var pRetry = require('p-retry');
|
|
6
6
|
|
|
@@ -100,10 +100,10 @@ const ONEKEY_SERVICE_UUID_ALIASES = hdShared.createKnownBleUuidAliases(hdShared.
|
|
|
100
100
|
const ONEKEY_WRITE_UUID_ALIASES = hdShared.createKnownBleUuidAliases(hdShared.ONEKEY_WRITE_CHARACTERISTIC_UUID);
|
|
101
101
|
const ONEKEY_NOTIFY_UUID_ALIASES = hdShared.createKnownBleUuidAliases(hdShared.ONEKEY_NOTIFY_CHARACTERISTIC_UUID);
|
|
102
102
|
const BLUETOOTH_INIT_TIMEOUT = 10000;
|
|
103
|
-
const DEVICE_SCAN_TIMEOUT =
|
|
104
|
-
const FAST_SCAN_TIMEOUT =
|
|
103
|
+
const DEVICE_SCAN_TIMEOUT = 5000;
|
|
104
|
+
const FAST_SCAN_TIMEOUT = 1500;
|
|
105
105
|
const DEVICE_CHECK_INTERVAL = 500;
|
|
106
|
-
const CONNECTION_TIMEOUT =
|
|
106
|
+
const CONNECTION_TIMEOUT = 3000;
|
|
107
107
|
const SERVICE_DISCOVERY_TIMEOUT = 10000;
|
|
108
108
|
const BLE_CLEANUP_TIMEOUT = 250;
|
|
109
109
|
const BLE_PACKET_SIZE = 192;
|
|
@@ -116,10 +116,14 @@ const ABORTABLE_WRITE_ERROR_PATTERNS = [
|
|
|
116
116
|
function isOneKeyPeripheral(peripheral) {
|
|
117
117
|
var _a, _b;
|
|
118
118
|
const serviceUuids = (_a = peripheral.advertisement) === null || _a === void 0 ? void 0 : _a.serviceUuids;
|
|
119
|
+
const localName = (_b = peripheral.advertisement) === null || _b === void 0 ? void 0 : _b.localName;
|
|
120
|
+
if (!(localName === null || localName === void 0 ? void 0 : localName.trim()) || hdShared.isPro2FindMyAdvertisementName(localName)) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
119
123
|
return (hdShared.hasOnekeyCommunicationService(serviceUuids) &&
|
|
120
124
|
hdShared.isOnekeyBluetoothDevice({
|
|
121
125
|
id: peripheral.id,
|
|
122
|
-
localName
|
|
126
|
+
localName,
|
|
123
127
|
serviceUuids,
|
|
124
128
|
}));
|
|
125
129
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"noble-ble-handler.d.ts","sourceRoot":"","sources":["../src/noble-ble-handler.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"noble-ble-handler.d.ts","sourceRoot":"","sources":["../src/noble-ble-handler.ts"],"names":[],"mappings":"AA0BA,OAAO,KAAK,EAAsB,WAAW,EAAE,MAAM,UAAU,CAAC;AAo7ChE,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI,CAyJpE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-electron",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.56",
|
|
4
4
|
"author": "OneKey",
|
|
5
5
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"electron-log": ">=4.0.0"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@onekeyfe/hd-core": "1.2.0-alpha.
|
|
29
|
-
"@onekeyfe/hd-shared": "1.2.0-alpha.
|
|
30
|
-
"@onekeyfe/hd-transport": "1.2.0-alpha.
|
|
28
|
+
"@onekeyfe/hd-core": "1.2.0-alpha.56",
|
|
29
|
+
"@onekeyfe/hd-shared": "1.2.0-alpha.56",
|
|
30
|
+
"@onekeyfe/hd-transport": "1.2.0-alpha.56",
|
|
31
31
|
"@stoprocent/noble": "2.3.16",
|
|
32
32
|
"p-retry": "^4.6.2"
|
|
33
33
|
},
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"electron": "^25.0.0",
|
|
37
37
|
"typescript": "^5.3.3"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "2c472748d67072307c21f8d33bc021cf18fe2ad7"
|
|
40
40
|
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { EOneKeyBleMessageKeys } from '@onekeyfe/hd-shared';
|
|
3
|
+
|
|
4
|
+
import type { WebContents } from 'electron';
|
|
5
|
+
|
|
6
|
+
type IpcHandler = (...args: unknown[]) => Promise<unknown> | unknown;
|
|
7
|
+
|
|
8
|
+
const createPeripheral = (id: string, localName?: string) => ({
|
|
9
|
+
id,
|
|
10
|
+
state: 'disconnected',
|
|
11
|
+
advertisement: {
|
|
12
|
+
localName,
|
|
13
|
+
serviceUuids: ['0001', 'fffd'],
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('Electron Noble BLE device discovery', () => {
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
jest.useRealTimers();
|
|
20
|
+
jest.resetModules();
|
|
21
|
+
jest.clearAllMocks();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('does not enumerate a Pro2 Find My advertisement with the communication service', async () => {
|
|
25
|
+
jest.useFakeTimers({ doNotFake: ['performance'] });
|
|
26
|
+
|
|
27
|
+
const handlers = new Map<string, IpcHandler>();
|
|
28
|
+
const ipcMain = {
|
|
29
|
+
handle: jest.fn((channel: string, handler: IpcHandler) => {
|
|
30
|
+
handlers.set(channel, handler);
|
|
31
|
+
}),
|
|
32
|
+
};
|
|
33
|
+
const noble = new EventEmitter() as EventEmitter & {
|
|
34
|
+
state: string;
|
|
35
|
+
startScanning: jest.Mock;
|
|
36
|
+
stopScanning: jest.Mock;
|
|
37
|
+
};
|
|
38
|
+
let resolveScanStarted = () => undefined;
|
|
39
|
+
const scanStarted = new Promise<void>(resolve => {
|
|
40
|
+
resolveScanStarted = resolve;
|
|
41
|
+
});
|
|
42
|
+
noble.state = 'poweredOn';
|
|
43
|
+
noble.startScanning = jest.fn((_services, _duplicates, callback) => {
|
|
44
|
+
callback?.();
|
|
45
|
+
noble.emit('discover', createPeripheral('find-my-device', 'Pro2 A1B2 - Find My'));
|
|
46
|
+
noble.emit('discover', createPeripheral('onekey-device', 'Pro2 A1B2'));
|
|
47
|
+
resolveScanStarted();
|
|
48
|
+
});
|
|
49
|
+
noble.stopScanning = jest.fn(callback => callback?.());
|
|
50
|
+
|
|
51
|
+
jest.doMock('@stoprocent/noble', () => noble);
|
|
52
|
+
jest.doMock('electron', () => ({ ipcMain }));
|
|
53
|
+
jest.doMock('electron-log', () => ({
|
|
54
|
+
info: jest.fn(),
|
|
55
|
+
debug: jest.fn(),
|
|
56
|
+
error: jest.fn(),
|
|
57
|
+
}));
|
|
58
|
+
|
|
59
|
+
const { setupNobleBleHandlers } = await import('../noble-ble-handler');
|
|
60
|
+
setupNobleBleHandlers({
|
|
61
|
+
on: jest.fn(),
|
|
62
|
+
send: jest.fn(),
|
|
63
|
+
} as unknown as WebContents);
|
|
64
|
+
|
|
65
|
+
const enumerate = handlers.get(EOneKeyBleMessageKeys.NOBLE_BLE_ENUMERATE);
|
|
66
|
+
if (!enumerate) {
|
|
67
|
+
throw new Error('Electron Noble BLE enumerate handler was not registered');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const devicesPromise = Promise.resolve(enumerate());
|
|
71
|
+
await scanStarted;
|
|
72
|
+
jest.advanceTimersByTime(5000);
|
|
73
|
+
|
|
74
|
+
await expect(devicesPromise).resolves.toEqual([
|
|
75
|
+
expect.objectContaining({
|
|
76
|
+
id: 'onekey-device',
|
|
77
|
+
name: 'Pro2 A1B2',
|
|
78
|
+
}),
|
|
79
|
+
]);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('does not enumerate a Find My peripheral first discovered without a name', async () => {
|
|
83
|
+
jest.useFakeTimers({ doNotFake: ['performance'] });
|
|
84
|
+
|
|
85
|
+
const handlers = new Map<string, IpcHandler>();
|
|
86
|
+
const ipcMain = {
|
|
87
|
+
handle: jest.fn((channel: string, handler: IpcHandler) => {
|
|
88
|
+
handlers.set(channel, handler);
|
|
89
|
+
}),
|
|
90
|
+
};
|
|
91
|
+
const noble = new EventEmitter() as EventEmitter & {
|
|
92
|
+
state: string;
|
|
93
|
+
startScanning: jest.Mock;
|
|
94
|
+
stopScanning: jest.Mock;
|
|
95
|
+
};
|
|
96
|
+
let resolveScanStarted = () => undefined;
|
|
97
|
+
const scanStarted = new Promise<void>(resolve => {
|
|
98
|
+
resolveScanStarted = resolve;
|
|
99
|
+
});
|
|
100
|
+
const findMyPeripheral = createPeripheral('find-my-device');
|
|
101
|
+
noble.state = 'poweredOn';
|
|
102
|
+
noble.startScanning = jest.fn((_services, _duplicates, callback) => {
|
|
103
|
+
callback?.();
|
|
104
|
+
noble.emit('discover', findMyPeripheral);
|
|
105
|
+
findMyPeripheral.advertisement.localName = 'Pro2 A1B2 - Find My';
|
|
106
|
+
noble.emit('discover', findMyPeripheral);
|
|
107
|
+
noble.emit('discover', createPeripheral('onekey-device', 'Pro2 A1B2'));
|
|
108
|
+
resolveScanStarted();
|
|
109
|
+
});
|
|
110
|
+
noble.stopScanning = jest.fn(callback => callback?.());
|
|
111
|
+
|
|
112
|
+
jest.doMock('@stoprocent/noble', () => noble);
|
|
113
|
+
jest.doMock('electron', () => ({ ipcMain }));
|
|
114
|
+
jest.doMock('electron-log', () => ({
|
|
115
|
+
info: jest.fn(),
|
|
116
|
+
debug: jest.fn(),
|
|
117
|
+
error: jest.fn(),
|
|
118
|
+
}));
|
|
119
|
+
|
|
120
|
+
const { setupNobleBleHandlers } = await import('../noble-ble-handler');
|
|
121
|
+
setupNobleBleHandlers({
|
|
122
|
+
on: jest.fn(),
|
|
123
|
+
send: jest.fn(),
|
|
124
|
+
} as unknown as WebContents);
|
|
125
|
+
|
|
126
|
+
const enumerate = handlers.get(EOneKeyBleMessageKeys.NOBLE_BLE_ENUMERATE);
|
|
127
|
+
if (!enumerate) {
|
|
128
|
+
throw new Error('Electron Noble BLE enumerate handler was not registered');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const devicesPromise = Promise.resolve(enumerate());
|
|
132
|
+
await scanStarted;
|
|
133
|
+
jest.advanceTimersByTime(5000);
|
|
134
|
+
|
|
135
|
+
await expect(devicesPromise).resolves.toEqual([
|
|
136
|
+
expect.objectContaining({
|
|
137
|
+
id: 'onekey-device',
|
|
138
|
+
name: 'Pro2 A1B2',
|
|
139
|
+
}),
|
|
140
|
+
]);
|
|
141
|
+
});
|
|
142
|
+
});
|
package/src/noble-ble-handler.ts
CHANGED
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
createKnownBleUuidAliases,
|
|
16
16
|
hasOnekeyCommunicationService,
|
|
17
17
|
isOnekeyBluetoothDevice,
|
|
18
|
+
isPro2FindMyAdvertisementName,
|
|
18
19
|
matchesKnownBleUuid,
|
|
19
20
|
wait,
|
|
20
21
|
} from '@onekeyfe/hd-shared';
|
|
@@ -75,10 +76,10 @@ const ONEKEY_NOTIFY_UUID_ALIASES = createKnownBleUuidAliases(ONEKEY_NOTIFY_CHARA
|
|
|
75
76
|
|
|
76
77
|
// Timeout and interval constants
|
|
77
78
|
const BLUETOOTH_INIT_TIMEOUT = 10000; // 10 seconds for Bluetooth initialization
|
|
78
|
-
const DEVICE_SCAN_TIMEOUT =
|
|
79
|
-
const FAST_SCAN_TIMEOUT =
|
|
79
|
+
const DEVICE_SCAN_TIMEOUT = 5000; // 5 seconds for device scanning
|
|
80
|
+
const FAST_SCAN_TIMEOUT = 1500; // 1.5 seconds for targeted scanning
|
|
80
81
|
const DEVICE_CHECK_INTERVAL = 500; // 500ms interval for periodic device checks
|
|
81
|
-
const CONNECTION_TIMEOUT =
|
|
82
|
+
const CONNECTION_TIMEOUT = 3000; // 3 seconds for device connection
|
|
82
83
|
const SERVICE_DISCOVERY_TIMEOUT = 10000; // 10 seconds for service discovery
|
|
83
84
|
const BLE_CLEANUP_TIMEOUT = 250;
|
|
84
85
|
|
|
@@ -93,11 +94,19 @@ const ABORTABLE_WRITE_ERROR_PATTERNS = [
|
|
|
93
94
|
|
|
94
95
|
function isOneKeyPeripheral(peripheral: Peripheral) {
|
|
95
96
|
const serviceUuids = peripheral.advertisement?.serviceUuids;
|
|
97
|
+
const localName = peripheral.advertisement?.localName;
|
|
98
|
+
|
|
99
|
+
// Noble localName is the current advertisement name, so reject the Pro2
|
|
100
|
+
// Find My endpoint before the communication-service fast path accepts it.
|
|
101
|
+
if (!localName?.trim() || isPro2FindMyAdvertisementName(localName)) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
96
105
|
return (
|
|
97
106
|
hasOnekeyCommunicationService(serviceUuids) &&
|
|
98
107
|
isOnekeyBluetoothDevice({
|
|
99
108
|
id: peripheral.id,
|
|
100
|
-
localName
|
|
109
|
+
localName,
|
|
101
110
|
serviceUuids,
|
|
102
111
|
})
|
|
103
112
|
);
|