@onekeyfe/hd-transport-web-device 1.2.2-alpha.15 → 1.2.2-alpha.17

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.
@@ -553,14 +553,19 @@ describe('ElectronBleTransport protocol detection', () => {
553
553
  test('fails Protocol V2 acquire immediately when subscribe reports insufficient encryption', async () => {
554
554
  const device = { id: 'stale-bond-pro2-id', name: 'OneKey Pro 2' };
555
555
  const nobleBle = createNobleBle(device);
556
- nobleBle.subscribe.mockRejectedValue(new Error('Encryption is insufficient'));
556
+ nobleBle.subscribe.mockRejectedValue({
557
+ name: 'HardwareError',
558
+ message: 'Bluetooth pairing information is no longer valid',
559
+ errorCode: HardwareErrorCode.BleBondInvalid,
560
+ params: { nativeErrorMessage: 'Encryption is insufficient' },
561
+ });
557
562
  const transport = configureTransport(nobleBle);
558
563
  const probe = jest.spyOn(transport as any, 'probeProtocolV2');
559
564
 
560
565
  await expect(
561
566
  transport.acquire({ uuid: device.id, expectedProtocol: 'V2' })
562
567
  ).rejects.toMatchObject({
563
- errorCode: HardwareErrorCode.BleDeviceBondError,
568
+ errorCode: HardwareErrorCode.BleBondInvalid,
564
569
  });
565
570
 
566
571
  expect(probe).not.toHaveBeenCalled();
@@ -568,6 +573,56 @@ describe('ElectronBleTransport protocol detection', () => {
568
573
  expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
569
574
  });
570
575
 
576
+ test('rehydrates a structured stale-bond error before the protocol is known', async () => {
577
+ const device = { id: 'reset-unknown-protocol-id', name: 'OneKey Pro 2' };
578
+ const nobleBle = createNobleBle(device);
579
+ nobleBle.connect.mockRejectedValue({
580
+ name: 'HardwareError',
581
+ message: 'Bluetooth pairing information is no longer valid',
582
+ errorCode: HardwareErrorCode.BleBondInvalid,
583
+ params: { nativeErrorMessage: 'CBErrorDomain:14 native message' },
584
+ });
585
+ const transport = configureTransport(nobleBle);
586
+
587
+ await expect(transport.acquire({ uuid: device.id })).rejects.toMatchObject({
588
+ name: 'HardwareError',
589
+ errorCode: HardwareErrorCode.BleBondInvalid,
590
+ params: { nativeErrorMessage: 'CBErrorDomain:14 native message' },
591
+ });
592
+ });
593
+
594
+ test('does not infer hardware errors from native error text in the renderer', async () => {
595
+ const device = { id: 'reset-pro2-localized-id', name: 'OneKey Pro 2' };
596
+ const nobleBle = createNobleBle(device);
597
+ nobleBle.connect.mockRejectedValue(new Error('CBATTErrorDomain:14 localized native message'));
598
+ const transport = configureTransport(nobleBle);
599
+
600
+ try {
601
+ await transport.acquire({ uuid: device.id, expectedProtocol: 'V2' });
602
+ throw new Error('Expected acquire to fail');
603
+ } catch (error) {
604
+ expect(error).toBeInstanceOf(Error);
605
+ expect((error as Error).message).toBe('CBATTErrorDomain:14 localized native message');
606
+ expect((error as { errorCode?: unknown }).errorCode).toBeUndefined();
607
+ }
608
+ });
609
+
610
+ test('does not classify a generic macOS connection failure as a stale bond', async () => {
611
+ const device = { id: 'offline-pro2-macos-id', name: 'OneKey Pro 2' };
612
+ const nobleBle = createNobleBle(device);
613
+ nobleBle.connect.mockRejectedValue(new Error('connection failed'));
614
+ const transport = configureTransport(nobleBle);
615
+
616
+ try {
617
+ await transport.acquire({ uuid: device.id, expectedProtocol: 'V2' });
618
+ throw new Error('Expected acquire to fail');
619
+ } catch (error) {
620
+ expect(error).toBeInstanceOf(Error);
621
+ expect((error as Error).message).toBe('connection failed');
622
+ expect((error as { errorCode?: unknown }).errorCode).toBeUndefined();
623
+ }
624
+ });
625
+
571
626
  test('keeps stale-bond subscribe mapping out of Protocol V1 acquire', async () => {
572
627
  const device = { id: 'classic-v1-id', name: 'OneKey Classic' };
573
628
  const nobleBle = createNobleBle(device);
@@ -41,7 +41,7 @@ export default class ElectronBleTransport {
41
41
  private hostDisconnectCleanup?;
42
42
  private notificationTokens;
43
43
  private nextNotificationToken;
44
- private toStaleBondError;
44
+ private normalizeBluetoothError;
45
45
  private handleBluetoothError;
46
46
  private cleanupDeviceState;
47
47
  init(logger: any, emitter?: EventEmitter): void;
@@ -1 +1 @@
1
- {"version":3,"file":"electron-ble-transport.d.ts","sourceRoot":"","sources":["../src/electron-ble-transport.ts"],"names":[],"mappings":";AA2BA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAClE,OAAO,KAAK,EACV,gBAAgB,EAChB,YAAY,EAEZ,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AAIvC,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,UAAU,CAAC,EAAE,UAAU,CAAC;KACzB;CACF;AAED,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,gBAAgB,CAAC,EAAE,YAAY,CAAC;IAChC,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,CAAC;AAiCF,MAAM,CAAC,OAAO,OAAO,oBAAoB;IACvC,OAAO,CAAC,SAAS,CAA0D;IAE3E,OAAO,CAAC,WAAW,CAA0D;IAE7E,OAAO,CAAC,6BAA6B,CAAqB;IAE1D,IAAI,SAA0B;IAE9B,UAAU,UAAS;IAEnB,UAAU,EAAE,QAAQ,CAAC,UAAU,GAAG,MAAM,CAAC,GAAG,IAAI,CAAQ;IAExD,OAAO,CAAC,kBAAkB,CAAuB;IAEjD,GAAG,CAAC,EAAE,GAAG,CAAC;IAEV,OAAO,CAAC,EAAE,YAAY,CAAC;IAEvB,OAAO,CAAC,gBAAgB,CAA0B;IAElD,OAAO,CAAC,cAAc,CAAwC;IAE9D,OAAO,CAAC,mBAAmB,CAAwC;IAGnE,OAAO,CAAC,mBAAmB,CAAqB;IAEhD,OAAO,CAAC,UAAU,CAAkC;IAEpD,OAAO,CAAC,sBAAsB,CAAkC;IAEhE,OAAO,CAAC,SAAS,CA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Tokens;
244
244
  private nextNotificationToken;
245
- private toStaleBondError;
245
+ private normalizeBluetoothError;
246
246
  private handleBluetoothError;
247
247
  private cleanupDeviceState;
248
248
  init(logger: any, emitter?: EventEmitter): void;
package/dist/index.js CHANGED
@@ -871,39 +871,20 @@ class ElectronBleTransport {
871
871
  this.notificationTokens = new Map();
872
872
  this.nextNotificationToken = 1;
873
873
  }
874
- toStaleBondError(error) {
875
- var _a;
876
- if (hdShared.isBleStaleBondHardwareError(error)) {
877
- return error;
878
- }
879
- const errorMessage = error && typeof error === 'object' && 'message' in error
880
- ? String((_a = error.message) !== null && _a !== void 0 ? _a : '')
881
- : String(error !== null && error !== void 0 ? error : '');
882
- if (!hdShared.isBleStaleBondErrorText(errorMessage)) {
883
- return null;
884
- }
885
- const normalizedErrorMessage = errorMessage.toLowerCase();
886
- return hdShared.ERRORS.TypedError(normalizedErrorMessage.includes('peer removed pairing information')
887
- ? hdShared.HardwareErrorCode.BlePeerRemovedPairingInformation
888
- : hdShared.HardwareErrorCode.BleDeviceBondError, errorMessage);
889
- }
890
- handleBluetoothError(error, mapProtocolV2StaleBond = false) {
891
- if (mapProtocolV2StaleBond) {
892
- const staleBondError = this.toStaleBondError(error);
893
- if (staleBondError) {
894
- throw staleBondError;
895
- }
896
- }
874
+ normalizeBluetoothError(error) {
897
875
  if (error && typeof error === 'object') {
876
+ if (typeof error.errorCode === 'number') {
877
+ return hdShared.ERRORS.TypedError(error.errorCode, typeof error.message === 'string' ? error.message : undefined, error.params);
878
+ }
898
879
  if ('code' in error) {
899
880
  if (error.code === hdShared.HardwareErrorCode.BlePoweredOff) {
900
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePoweredOff);
881
+ return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePoweredOff);
901
882
  }
902
883
  if (error.code === hdShared.HardwareErrorCode.BleUnsupported) {
903
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleUnsupported);
884
+ return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleUnsupported);
904
885
  }
905
886
  if (error.code === hdShared.HardwareErrorCode.BlePermissionError) {
906
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError);
887
+ return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError);
907
888
  }
908
889
  }
909
890
  const errorMessage = error.message || String(error);
@@ -911,16 +892,19 @@ class ElectronBleTransport {
911
892
  const unsupportedMessage = hdShared.HardwareErrorCodeMessage[hdShared.HardwareErrorCode.BleUnsupported];
912
893
  const permissionMessage = hdShared.HardwareErrorCodeMessage[hdShared.HardwareErrorCode.BlePermissionError];
913
894
  if (errorMessage.includes(poweredOffMessage) || errorMessage.includes('poweredOff')) {
914
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePoweredOff);
895
+ return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePoweredOff);
915
896
  }
916
897
  if (errorMessage.includes(unsupportedMessage) || errorMessage.includes('unsupported')) {
917
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleUnsupported);
898
+ return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleUnsupported);
918
899
  }
919
900
  if (errorMessage.includes(permissionMessage) || errorMessage.includes('unauthorized')) {
920
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError);
901
+ return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError);
921
902
  }
922
903
  }
923
- throw error;
904
+ return error;
905
+ }
906
+ handleBluetoothError(error) {
907
+ throw this.normalizeBluetoothError(error);
924
908
  }
925
909
  cleanupDeviceState(deviceId) {
926
910
  this.protocolV2Links
@@ -1024,9 +1008,6 @@ class ElectronBleTransport {
1024
1008
  var _a, _b, _c, _d, _e, _f, _g;
1025
1009
  return __awaiter(this, void 0, void 0, function* () {
1026
1010
  const { uuid, forceCleanRunPromise, expectedProtocol } = input;
1027
- const shouldMapProtocolV2StaleBond = expectedProtocol
1028
- ? expectedProtocol === 'V2'
1029
- : this.confirmedProtocolV2.has(uuid);
1030
1011
  if (!uuid) {
1031
1012
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleRequiredUUID);
1032
1013
  }
@@ -1058,7 +1039,7 @@ class ElectronBleTransport {
1058
1039
  this.connectedDevices.add(uuid);
1059
1040
  }
1060
1041
  catch (error) {
1061
- this.handleBluetoothError(error, shouldMapProtocolV2StaleBond);
1042
+ this.handleBluetoothError(error);
1062
1043
  }
1063
1044
  const mtuCleanup = this.createMtuSubscription(uuid);
1064
1045
  if (mtuCleanup) {
@@ -1070,7 +1051,7 @@ class ElectronBleTransport {
1070
1051
  yield window.desktopApi.nobleBle.subscribe(uuid);
1071
1052
  }
1072
1053
  catch (error) {
1073
- this.handleBluetoothError(error, shouldMapProtocolV2StaleBond);
1054
+ this.handleBluetoothError(error);
1074
1055
  }
1075
1056
  yield this.refreshBlePacketCapacity(uuid);
1076
1057
  const cleanup = this.createNotificationSubscription(uuid);
@@ -1097,7 +1078,7 @@ class ElectronBleTransport {
1097
1078
  (_g = this.Log) === null || _g === void 0 ? void 0 : _g.debug('[Electron BLE] acquire cleanup failed:', cleanupError);
1098
1079
  }
1099
1080
  this.cleanupDeviceState(uuid);
1100
- throw error;
1081
+ this.handleBluetoothError(error);
1101
1082
  }
1102
1083
  });
1103
1084
  }
@@ -1301,11 +1282,7 @@ class ElectronBleTransport {
1301
1282
  yield nobleBle.write(uuid, hexData, { pacingDelayMs: 0 });
1302
1283
  }
1303
1284
  catch (error) {
1304
- const staleBondError = this.toStaleBondError(error);
1305
- if (staleBondError) {
1306
- throw staleBondError;
1307
- }
1308
- throw error;
1285
+ this.handleBluetoothError(error);
1309
1286
  }
1310
1287
  });
1311
1288
  }
@@ -1568,7 +1545,7 @@ class ElectronBleTransport {
1568
1545
  yield this.releaseNative(uuid);
1569
1546
  }
1570
1547
  }
1571
- throw e;
1548
+ throw this.normalizeBluetoothError(e);
1572
1549
  }
1573
1550
  finally {
1574
1551
  if (timeout)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-web-device",
3
- "version": "1.2.2-alpha.15",
3
+ "version": "1.2.2-alpha.17",
4
4
  "author": "OneKey",
5
5
  "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
6
6
  "license": "MIT",
@@ -21,13 +21,13 @@
21
21
  "lint:fix": "eslint . --fix"
22
22
  },
23
23
  "dependencies": {
24
- "@onekeyfe/hd-shared": "1.2.2-alpha.15",
25
- "@onekeyfe/hd-transport": "1.2.2-alpha.15"
24
+ "@onekeyfe/hd-shared": "1.2.2-alpha.17",
25
+ "@onekeyfe/hd-transport": "1.2.2-alpha.17"
26
26
  },
27
27
  "devDependencies": {
28
- "@onekeyfe/hd-transport-electron": "1.2.2-alpha.15",
28
+ "@onekeyfe/hd-transport-electron": "1.2.2-alpha.17",
29
29
  "@types/w3c-web-usb": "^1.0.6",
30
30
  "@types/web-bluetooth": "^0.0.17"
31
31
  },
32
- "gitHead": "ea66eff5817385ee2144d8e037c385342e44e21f"
32
+ "gitHead": "b6c48b842d1c325dcd5ce42c03a07ee4377002dd"
33
33
  }
@@ -18,7 +18,6 @@ import {
18
18
  HardwareErrorCode,
19
19
  HardwareErrorCodeMessage,
20
20
  createDeferred,
21
- isBleStaleBondErrorText,
22
21
  isBleStaleBondHardwareError,
23
22
  isHeaderChunk,
24
23
  } from '@onekeyfe/hd-shared';
@@ -163,43 +162,24 @@ export default class ElectronBleTransport {
163
162
 
164
163
  private nextNotificationToken = 1;
165
164
 
166
- private toStaleBondError(error: unknown): Error | null {
167
- if (isBleStaleBondHardwareError(error)) {
168
- return error as Error;
169
- }
170
- const errorMessage =
171
- error && typeof error === 'object' && 'message' in error
172
- ? String((error as { message?: unknown }).message ?? '')
173
- : String(error ?? '');
174
- if (!isBleStaleBondErrorText(errorMessage)) {
175
- return null;
176
- }
177
- const normalizedErrorMessage = errorMessage.toLowerCase();
178
- return ERRORS.TypedError(
179
- normalizedErrorMessage.includes('peer removed pairing information')
180
- ? HardwareErrorCode.BlePeerRemovedPairingInformation
181
- : HardwareErrorCode.BleDeviceBondError,
182
- errorMessage
183
- );
184
- }
185
-
186
- private handleBluetoothError(error: any, mapProtocolV2StaleBond = false): never {
187
- if (mapProtocolV2StaleBond) {
188
- const staleBondError = this.toStaleBondError(error);
189
- if (staleBondError) {
190
- throw staleBondError;
191
- }
192
- }
165
+ private normalizeBluetoothError(error: any): any {
193
166
  if (error && typeof error === 'object') {
167
+ if (typeof error.errorCode === 'number') {
168
+ return ERRORS.TypedError(
169
+ error.errorCode,
170
+ typeof error.message === 'string' ? error.message : undefined,
171
+ error.params
172
+ );
173
+ }
194
174
  if ('code' in error) {
195
175
  if (error.code === HardwareErrorCode.BlePoweredOff) {
196
- throw ERRORS.TypedError(HardwareErrorCode.BlePoweredOff);
176
+ return ERRORS.TypedError(HardwareErrorCode.BlePoweredOff);
197
177
  }
198
178
  if (error.code === HardwareErrorCode.BleUnsupported) {
199
- throw ERRORS.TypedError(HardwareErrorCode.BleUnsupported);
179
+ return ERRORS.TypedError(HardwareErrorCode.BleUnsupported);
200
180
  }
201
181
  if (error.code === HardwareErrorCode.BlePermissionError) {
202
- throw ERRORS.TypedError(HardwareErrorCode.BlePermissionError);
182
+ return ERRORS.TypedError(HardwareErrorCode.BlePermissionError);
203
183
  }
204
184
  }
205
185
  const errorMessage = error.message || String(error);
@@ -208,16 +188,20 @@ export default class ElectronBleTransport {
208
188
  const permissionMessage = HardwareErrorCodeMessage[HardwareErrorCode.BlePermissionError];
209
189
 
210
190
  if (errorMessage.includes(poweredOffMessage) || errorMessage.includes('poweredOff')) {
211
- throw ERRORS.TypedError(HardwareErrorCode.BlePoweredOff);
191
+ return ERRORS.TypedError(HardwareErrorCode.BlePoweredOff);
212
192
  }
213
193
  if (errorMessage.includes(unsupportedMessage) || errorMessage.includes('unsupported')) {
214
- throw ERRORS.TypedError(HardwareErrorCode.BleUnsupported);
194
+ return ERRORS.TypedError(HardwareErrorCode.BleUnsupported);
215
195
  }
216
196
  if (errorMessage.includes(permissionMessage) || errorMessage.includes('unauthorized')) {
217
- throw ERRORS.TypedError(HardwareErrorCode.BlePermissionError);
197
+ return ERRORS.TypedError(HardwareErrorCode.BlePermissionError);
218
198
  }
219
199
  }
220
- throw error;
200
+ return error;
201
+ }
202
+
203
+ private handleBluetoothError(error: any): never {
204
+ throw this.normalizeBluetoothError(error);
221
205
  }
222
206
 
223
207
  private cleanupDeviceState(deviceId: string): void {
@@ -350,9 +334,6 @@ export default class ElectronBleTransport {
350
334
 
351
335
  async acquire(input: BleAcquireInput) {
352
336
  const { uuid, forceCleanRunPromise, expectedProtocol } = input;
353
- const shouldMapProtocolV2StaleBond = expectedProtocol
354
- ? expectedProtocol === 'V2'
355
- : this.confirmedProtocolV2.has(uuid);
356
337
 
357
338
  if (!uuid) {
358
339
  throw ERRORS.TypedError(HardwareErrorCode.BleRequiredUUID);
@@ -389,7 +370,7 @@ export default class ElectronBleTransport {
389
370
  await window.desktopApi.nobleBle.connect(uuid);
390
371
  this.connectedDevices.add(uuid);
391
372
  } catch (error) {
392
- this.handleBluetoothError(error, shouldMapProtocolV2StaleBond);
373
+ this.handleBluetoothError(error);
393
374
  }
394
375
 
395
376
  const mtuCleanup = this.createMtuSubscription(uuid);
@@ -403,7 +384,7 @@ export default class ElectronBleTransport {
403
384
  try {
404
385
  await window.desktopApi.nobleBle.subscribe(uuid);
405
386
  } catch (error) {
406
- this.handleBluetoothError(error, shouldMapProtocolV2StaleBond);
387
+ this.handleBluetoothError(error);
407
388
  }
408
389
  await this.refreshBlePacketCapacity(uuid);
409
390
 
@@ -434,7 +415,7 @@ export default class ElectronBleTransport {
434
415
  this.Log?.debug('[Electron BLE] acquire cleanup failed:', cleanupError);
435
416
  }
436
417
  this.cleanupDeviceState(uuid);
437
- throw error;
418
+ this.handleBluetoothError(error);
438
419
  }
439
420
  }
440
421
 
@@ -673,11 +654,7 @@ export default class ElectronBleTransport {
673
654
  try {
674
655
  await nobleBle.write(uuid, hexData, { pacingDelayMs: 0 });
675
656
  } catch (error) {
676
- const staleBondError = this.toStaleBondError(error);
677
- if (staleBondError) {
678
- throw staleBondError;
679
- }
680
- throw error;
657
+ this.handleBluetoothError(error);
681
658
  }
682
659
  }
683
660
 
@@ -978,7 +955,7 @@ export default class ElectronBleTransport {
978
955
  await this.releaseNative(uuid);
979
956
  }
980
957
  }
981
- throw e;
958
+ throw this.normalizeBluetoothError(e);
982
959
  } finally {
983
960
  if (timeout) clearTimeout(timeout);
984
961
  if (this.runPromise === runPromise) {