@onekeyfe/hardware-cli 1.2.0-alpha.11 → 1.2.0-alpha.13

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.
@@ -16,7 +16,6 @@ const CONNECTION_TIMEOUT = 8000;
16
16
  const SERVICE_DISCOVERY_TIMEOUT = 10000;
17
17
  const BLE_CLEANUP_TIMEOUT = 100;
18
18
  const BLE_PACKET_SIZE = 192;
19
- const BLE_WRITE_DELAY = 5;
20
19
  const BLE_ENCRYPTION_ERROR_PATTERNS = [/encryption is insufficient/i, /insufficient encryption/i];
21
20
  let noble = null;
22
21
  let nobleReadyPromise = null;
@@ -288,9 +287,9 @@ function subscribeNotifications(deviceId, generation, notifyCharacteristic) {
288
287
  throw hd_shared_1.ERRORS.TypedError(hd_shared_1.HardwareErrorCode.BleCharacteristicNotifyChangeFailure);
289
288
  });
290
289
  }
291
- function writeCharacteristic(characteristic, buffer) {
290
+ function writeCharacteristic(characteristic, buffer, withoutResponse) {
292
291
  return new Promise((resolve, reject) => {
293
- characteristic.write(buffer, true, (error) => {
292
+ characteristic.write(buffer, withoutResponse, (error) => {
294
293
  if (error) {
295
294
  reject(error);
296
295
  return;
@@ -359,10 +358,7 @@ function createNobleBlePlugin() {
359
358
  const buffer = Buffer.from(data, 'hex');
360
359
  for (let offset = 0; offset < buffer.length; offset += BLE_PACKET_SIZE) {
361
360
  const chunk = buffer.subarray(offset, Math.min(offset + BLE_PACKET_SIZE, buffer.length));
362
- await writeCharacteristic(characteristics.write, chunk);
363
- if (offset + BLE_PACKET_SIZE < buffer.length) {
364
- await (0, hd_shared_1.wait)(BLE_WRITE_DELAY);
365
- }
361
+ await writeCharacteristic(characteristics.write, chunk, true);
366
362
  }
367
363
  },
368
364
  async receive(uuid) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hardware-cli",
3
- "version": "1.2.0-alpha.11",
3
+ "version": "1.2.0-alpha.13",
4
4
  "description": "OneKey hardware wallet CLI for testing device communication",
5
5
  "author": "OneKey",
6
6
  "license": "Apache-2.0",
@@ -30,12 +30,12 @@
30
30
  "test": "jest"
31
31
  },
32
32
  "dependencies": {
33
- "@onekeyfe/hd-common-connect-sdk": "1.2.0-alpha.11",
34
- "@onekeyfe/hd-core": "1.2.0-alpha.11",
35
- "@onekeyfe/hd-shared": "1.2.0-alpha.11",
36
- "@onekeyfe/hd-transport-usb": "1.2.0-alpha.11",
33
+ "@onekeyfe/hd-common-connect-sdk": "1.2.0-alpha.13",
34
+ "@onekeyfe/hd-core": "1.2.0-alpha.13",
35
+ "@onekeyfe/hd-shared": "1.2.0-alpha.13",
36
+ "@onekeyfe/hd-transport-usb": "1.2.0-alpha.13",
37
37
  "@stoprocent/noble": "2.3.16",
38
38
  "commander": "^12.0.0"
39
39
  },
40
- "gitHead": "05d3421ec00e07e99f6665e6436a612503e32dfc"
40
+ "gitHead": "a907a7ec140123107f6188b37550a7143f1e0d91"
41
41
  }
@@ -39,6 +39,7 @@ const createPeripheral = (id: string) => {
39
39
  connect: jest.fn(callback => callback()),
40
40
  disconnect: jest.fn(callback => callback()),
41
41
  },
42
+ write,
42
43
  notify,
43
44
  };
44
45
  };
@@ -110,4 +111,64 @@ describe('Noble BLE plugin notification routing', () => {
110
111
 
111
112
  expect(result).toBe('completed');
112
113
  });
114
+
115
+ test('uses withoutResponse for normal and high-volume writes', async () => {
116
+ const device = createPeripheral('device-a');
117
+ const noble = new EventEmitter() as EventEmitter & {
118
+ state: string;
119
+ startScanning: jest.Mock;
120
+ stopScanning: jest.Mock;
121
+ };
122
+ noble.state = 'poweredOn';
123
+ noble.startScanning = jest.fn((_services, _duplicates, callback) => {
124
+ callback?.();
125
+ noble.emit('discover', device.peripheral);
126
+ });
127
+ noble.stopScanning = jest.fn(callback => callback?.());
128
+ jest.doMock('@stoprocent/noble', () => noble);
129
+
130
+ const { createNobleBlePlugin } = await import('../transports/nobleBlePlugin');
131
+ const plugin = createNobleBlePlugin();
132
+ await plugin.init();
133
+ await plugin.connect('device-a');
134
+
135
+ await plugin.send('device-a', 'aa');
136
+ await plugin.send('device-a', 'bb');
137
+
138
+ expect(device.write.write.mock.calls.map(([, withoutResponse]) => withoutResponse)).toEqual([
139
+ true,
140
+ true,
141
+ ]);
142
+ });
143
+
144
+ test('does not add a fixed delay between 192-byte writes', async () => {
145
+ const device = createPeripheral('device-a');
146
+ const noble = new EventEmitter() as EventEmitter & {
147
+ state: string;
148
+ startScanning: jest.Mock;
149
+ stopScanning: jest.Mock;
150
+ };
151
+ const wait = jest.fn(() => Promise.resolve());
152
+ noble.state = 'poweredOn';
153
+ noble.startScanning = jest.fn((_services, _duplicates, callback) => {
154
+ callback?.();
155
+ noble.emit('discover', device.peripheral);
156
+ });
157
+ noble.stopScanning = jest.fn(callback => callback?.());
158
+ jest.doMock('@stoprocent/noble', () => noble);
159
+ jest.doMock('@onekeyfe/hd-shared', () => ({
160
+ ...jest.requireActual('@onekeyfe/hd-shared'),
161
+ wait,
162
+ }));
163
+
164
+ const { createNobleBlePlugin } = await import('../transports/nobleBlePlugin');
165
+ const plugin = createNobleBlePlugin();
166
+ await plugin.init();
167
+ await plugin.connect('device-a');
168
+
169
+ await plugin.send('device-a', 'aa'.repeat(193));
170
+
171
+ expect(device.write.write).toHaveBeenCalledTimes(2);
172
+ expect(wait).not.toHaveBeenCalled();
173
+ });
113
174
  });
@@ -3,7 +3,6 @@ import {
3
3
  HardwareErrorCode,
4
4
  ONEKEY_SERVICE_UUID,
5
5
  isOnekeyDevice,
6
- wait,
7
6
  } from '@onekeyfe/hd-shared';
8
7
 
9
8
  import type { LowLevelDevice, LowlevelTransportSharedPlugin } from '@onekeyfe/hd-transport';
@@ -54,7 +53,6 @@ const CONNECTION_TIMEOUT = 8_000;
54
53
  const SERVICE_DISCOVERY_TIMEOUT = 10_000;
55
54
  const BLE_CLEANUP_TIMEOUT = 100;
56
55
  const BLE_PACKET_SIZE = 192;
57
- const BLE_WRITE_DELAY = 5;
58
56
  const BLE_ENCRYPTION_ERROR_PATTERNS = [/encryption is insufficient/i, /insufficient encryption/i];
59
57
 
60
58
  let noble: NobleModule | null = null;
@@ -379,9 +377,13 @@ function subscribeNotifications(
379
377
  });
380
378
  }
381
379
 
382
- function writeCharacteristic(characteristic: Characteristic, buffer: Buffer) {
380
+ function writeCharacteristic(
381
+ characteristic: Characteristic,
382
+ buffer: Buffer,
383
+ withoutResponse: boolean
384
+ ) {
383
385
  return new Promise<void>((resolve, reject) => {
384
- characteristic.write(buffer, true, (error?: Error) => {
386
+ characteristic.write(buffer, withoutResponse, (error?: Error) => {
385
387
  if (error) {
386
388
  reject(error);
387
389
  return;
@@ -463,10 +465,7 @@ export function createNobleBlePlugin(): LowlevelTransportSharedPlugin {
463
465
  const buffer = Buffer.from(data, 'hex');
464
466
  for (let offset = 0; offset < buffer.length; offset += BLE_PACKET_SIZE) {
465
467
  const chunk = buffer.subarray(offset, Math.min(offset + BLE_PACKET_SIZE, buffer.length));
466
- await writeCharacteristic(characteristics.write, chunk);
467
- if (offset + BLE_PACKET_SIZE < buffer.length) {
468
- await wait(BLE_WRITE_DELAY);
469
- }
468
+ await writeCharacteristic(characteristics.write, chunk, true);
470
469
  }
471
470
  },
472
471