@onekeyfe/hd-transport-electron 1.2.0-alpha.54 → 1.2.0-alpha.55

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.
@@ -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-d58af248.js'); });
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require('./index-5f728113.js');
5
+ var index = require('./index-c1642d47.js');
6
6
 
7
7
 
8
8
 
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var index = require('./index-5f728113.js');
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 = 8000;
104
- const FAST_SCAN_TIMEOUT = 8000;
103
+ const DEVICE_SCAN_TIMEOUT = 5000;
104
+ const FAST_SCAN_TIMEOUT = 1500;
105
105
  const DEVICE_CHECK_INTERVAL = 500;
106
- const CONNECTION_TIMEOUT = 8000;
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: (_b = peripheral.advertisement) === null || _b === void 0 ? void 0 : _b.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":"AAyBA,OAAO,KAAK,EAAsB,WAAW,EAAE,MAAM,UAAU,CAAC;AA46ChE,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI,CAyJpE"}
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.54",
3
+ "version": "1.2.0-alpha.55",
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.54",
29
- "@onekeyfe/hd-shared": "1.2.0-alpha.54",
30
- "@onekeyfe/hd-transport": "1.2.0-alpha.54",
28
+ "@onekeyfe/hd-core": "1.2.0-alpha.55",
29
+ "@onekeyfe/hd-shared": "1.2.0-alpha.55",
30
+ "@onekeyfe/hd-transport": "1.2.0-alpha.55",
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": "a382a4719286e0df04fe706ed13099020eb8e13f"
39
+ "gitHead": "0a99a180062ec32fac0c5e14be3366fbfb7bc01a"
40
40
  }
@@ -0,0 +1,143 @@
1
+ import { EventEmitter } from 'events';
2
+
3
+ import { EOneKeyBleMessageKeys } from '@onekeyfe/hd-shared';
4
+
5
+ import type { WebContents } from 'electron';
6
+
7
+ type IpcHandler = (...args: unknown[]) => Promise<unknown> | unknown;
8
+
9
+ const createPeripheral = (id: string, localName?: string) => ({
10
+ id,
11
+ state: 'disconnected',
12
+ advertisement: {
13
+ localName,
14
+ serviceUuids: ['0001', 'fffd'],
15
+ },
16
+ });
17
+
18
+ describe('Electron Noble BLE device discovery', () => {
19
+ afterEach(() => {
20
+ jest.useRealTimers();
21
+ jest.resetModules();
22
+ jest.clearAllMocks();
23
+ });
24
+
25
+ test('does not enumerate a Pro2 Find My advertisement with the communication service', async () => {
26
+ jest.useFakeTimers({ doNotFake: ['performance'] });
27
+
28
+ const handlers = new Map<string, IpcHandler>();
29
+ const ipcMain = {
30
+ handle: jest.fn((channel: string, handler: IpcHandler) => {
31
+ handlers.set(channel, handler);
32
+ }),
33
+ };
34
+ const noble = new EventEmitter() as EventEmitter & {
35
+ state: string;
36
+ startScanning: jest.Mock;
37
+ stopScanning: jest.Mock;
38
+ };
39
+ let resolveScanStarted = () => undefined;
40
+ const scanStarted = new Promise<void>(resolve => {
41
+ resolveScanStarted = resolve;
42
+ });
43
+ noble.state = 'poweredOn';
44
+ noble.startScanning = jest.fn((_services, _duplicates, callback) => {
45
+ callback?.();
46
+ noble.emit('discover', createPeripheral('find-my-device', 'Pro2 A1B2 - Find My'));
47
+ noble.emit('discover', createPeripheral('onekey-device', 'Pro2 A1B2'));
48
+ resolveScanStarted();
49
+ });
50
+ noble.stopScanning = jest.fn(callback => callback?.());
51
+
52
+ jest.doMock('@stoprocent/noble', () => noble);
53
+ jest.doMock('electron', () => ({ ipcMain }));
54
+ jest.doMock('electron-log', () => ({
55
+ info: jest.fn(),
56
+ debug: jest.fn(),
57
+ error: jest.fn(),
58
+ }));
59
+
60
+ const { setupNobleBleHandlers } = await import('../noble-ble-handler');
61
+ setupNobleBleHandlers({
62
+ on: jest.fn(),
63
+ send: jest.fn(),
64
+ } as unknown as WebContents);
65
+
66
+ const enumerate = handlers.get(EOneKeyBleMessageKeys.NOBLE_BLE_ENUMERATE);
67
+ if (!enumerate) {
68
+ throw new Error('Electron Noble BLE enumerate handler was not registered');
69
+ }
70
+
71
+ const devicesPromise = Promise.resolve(enumerate());
72
+ await scanStarted;
73
+ jest.advanceTimersByTime(5000);
74
+
75
+ await expect(devicesPromise).resolves.toEqual([
76
+ expect.objectContaining({
77
+ id: 'onekey-device',
78
+ name: 'Pro2 A1B2',
79
+ }),
80
+ ]);
81
+ });
82
+
83
+ test('does not enumerate a Find My peripheral first discovered without a name', async () => {
84
+ jest.useFakeTimers({ doNotFake: ['performance'] });
85
+
86
+ const handlers = new Map<string, IpcHandler>();
87
+ const ipcMain = {
88
+ handle: jest.fn((channel: string, handler: IpcHandler) => {
89
+ handlers.set(channel, handler);
90
+ }),
91
+ };
92
+ const noble = new EventEmitter() as EventEmitter & {
93
+ state: string;
94
+ startScanning: jest.Mock;
95
+ stopScanning: jest.Mock;
96
+ };
97
+ let resolveScanStarted = () => undefined;
98
+ const scanStarted = new Promise<void>(resolve => {
99
+ resolveScanStarted = resolve;
100
+ });
101
+ const findMyPeripheral = createPeripheral('find-my-device');
102
+ noble.state = 'poweredOn';
103
+ noble.startScanning = jest.fn((_services, _duplicates, callback) => {
104
+ callback?.();
105
+ noble.emit('discover', findMyPeripheral);
106
+ findMyPeripheral.advertisement.localName = 'Pro2 A1B2 - Find My';
107
+ noble.emit('discover', findMyPeripheral);
108
+ noble.emit('discover', createPeripheral('onekey-device', 'Pro2 A1B2'));
109
+ resolveScanStarted();
110
+ });
111
+ noble.stopScanning = jest.fn(callback => callback?.());
112
+
113
+ jest.doMock('@stoprocent/noble', () => noble);
114
+ jest.doMock('electron', () => ({ ipcMain }));
115
+ jest.doMock('electron-log', () => ({
116
+ info: jest.fn(),
117
+ debug: jest.fn(),
118
+ error: jest.fn(),
119
+ }));
120
+
121
+ const { setupNobleBleHandlers } = await import('../noble-ble-handler');
122
+ setupNobleBleHandlers({
123
+ on: jest.fn(),
124
+ send: jest.fn(),
125
+ } as unknown as WebContents);
126
+
127
+ const enumerate = handlers.get(EOneKeyBleMessageKeys.NOBLE_BLE_ENUMERATE);
128
+ if (!enumerate) {
129
+ throw new Error('Electron Noble BLE enumerate handler was not registered');
130
+ }
131
+
132
+ const devicesPromise = Promise.resolve(enumerate());
133
+ await scanStarted;
134
+ jest.advanceTimersByTime(5000);
135
+
136
+ await expect(devicesPromise).resolves.toEqual([
137
+ expect.objectContaining({
138
+ id: 'onekey-device',
139
+ name: 'Pro2 A1B2',
140
+ }),
141
+ ]);
142
+ });
143
+ });
@@ -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 = 8000; // 8 seconds for device scanning (Pro2 has longer advertising interval)
79
- const FAST_SCAN_TIMEOUT = 8000; // 8 seconds for targeted scanning (Pro2 has longer advertising interval)
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 = 8000; // 8 seconds for device connection (BLE reconnect after release can be slow)
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: peripheral.advertisement?.localName,
109
+ localName,
101
110
  serviceUuids,
102
111
  })
103
112
  );