@onekeyfe/hd-transport-web-device 1.2.8 → 1.2.9

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/webusb.d.ts CHANGED
@@ -8,7 +8,16 @@ export interface DeviceInfo extends OneKeyDeviceInfoBase {
8
8
  device: USBDevice;
9
9
  protocolType?: ProtocolType;
10
10
  }
11
+ interface WebUsbConnectOptions {
12
+ expectedProtocol?: ProtocolType;
13
+ forceProtocolDetection?: boolean;
14
+ }
11
15
  export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string> {
16
+ private readonly identityProbe;
17
+ private readonly transportInstanceId;
18
+ private handleOperationSequence;
19
+ private deviceLogSequence;
20
+ private deviceLogIds;
12
21
  messages: ReturnType<typeof transport.parseConfigure> | undefined;
13
22
  messagesV2: ReturnType<typeof transport.parseConfigure> | undefined;
14
23
  private protocolV2SchemaSource;
@@ -17,6 +26,11 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
17
26
  private deviceProtocolHints;
18
27
  private staleProtocolPaths;
19
28
  private acquiredPaths;
29
+ private identityProbeDevices;
30
+ private normalDevices;
31
+ private pendingHandleOperations;
32
+ private stopPromise?;
33
+ private identityProbeCleanupPaths;
20
34
  private probedDeviceObjects;
21
35
  private deviceEndpoints;
22
36
  name: string;
@@ -29,8 +43,19 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
29
43
  configurationId: number;
30
44
  endpointId: number;
31
45
  interfaceId: number;
32
- constructor();
46
+ constructor(identityProbe?: boolean);
33
47
  init(logger: any, emitter?: EventEmitter): void;
48
+ stop(): Promise<void>;
49
+ private stopResources;
50
+ private assertRunning;
51
+ private runHandleOperation;
52
+ private getInterfaceClaimed;
53
+ private getDeviceLogId;
54
+ private getHandleLogState;
55
+ private getDiagnosticError;
56
+ private logLifecycle;
57
+ private logSkippedHandleOperation;
58
+ private runLoggedHandleOperation;
34
59
  emitDeviceDisconnect(path: string, device?: USBDevice): void;
35
60
  markProtocolStale(path: string): void;
36
61
  configure(signedData: any): void;
@@ -44,14 +69,28 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
44
69
  private createProtocolDetectionError;
45
70
  private detectProtocol;
46
71
  findDevice(path: string): Promise<USBDevice>;
47
- connect(path: string, first: boolean): Promise<void>;
72
+ connect(path: string, first: boolean, options?: WebUsbConnectOptions): Promise<undefined>;
73
+ private shouldUseLegacyV1Connection;
48
74
  private discoverEndpoints;
49
- connectToDevice(path: string, first: boolean): Promise<void>;
75
+ connectToDevice(path: string, first: boolean, options?: WebUsbConnectOptions): Promise<undefined>;
76
+ private claimDeviceInterface;
77
+ private assertHandleAvailable;
78
+ private claimNormalHandle;
79
+ private releaseNormalHandles;
80
+ private isHandleOwnershipError;
81
+ private claimIdentityProbeHandle;
82
+ private releaseIdentityProbeHandle;
50
83
  private closeOpenDevice;
84
+ private closeDeviceHandle;
51
85
  private clearEndpointHalt;
52
86
  private assertAcquired;
53
87
  post(session: string, name: string, data: Record<string, unknown>): Promise<void>;
54
88
  private getErrorMessage;
89
+ private getErrorName;
90
+ private isWebUsbIoHardwareError;
91
+ private isDeviceStillEnumerated;
92
+ private throwWebUsbIoError;
93
+ private isWebUsbPermissionDeniedMessage;
55
94
  private isRetryablePacketIoError;
56
95
  private reconnectForPacketIoRetry;
57
96
  private getTransferInData;
@@ -79,4 +118,5 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
79
118
  protected createProtocolV2UsbTimeoutError(name: string, timeoutMs: number): Error;
80
119
  getProtocolType(path: string): ProtocolType | undefined;
81
120
  }
121
+ export {};
82
122
  //# sourceMappingURL=webusb.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"webusb.d.ts","sourceRoot":"","sources":["../src/webusb.ts"],"names":[],"mappings":";;AACA,OAAO,SAAS,EAAE,EAQhB,0BAA0B,EAG3B,MAAM,wBAAwB,CAAC;AAYhC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACv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hd-transport-web-device",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
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.8",
25
- "@onekeyfe/hd-transport": "1.2.8"
24
+ "@onekeyfe/hd-shared": "1.2.9",
25
+ "@onekeyfe/hd-transport": "1.2.9"
26
26
  },
27
27
  "devDependencies": {
28
- "@onekeyfe/hd-transport-electron": "1.2.8",
28
+ "@onekeyfe/hd-transport-electron": "1.2.9",
29
29
  "@types/w3c-web-usb": "^1.0.6",
30
30
  "@types/web-bluetooth": "^0.0.17"
31
31
  },
32
- "gitHead": "9f016278a7f395bf66b8502815dc63191e162fd3"
32
+ "gitHead": "36cc567ae08ffb9190b44efc6430d4199343be9e"
33
33
  }
@@ -18,7 +18,6 @@ import {
18
18
  HardwareErrorCode,
19
19
  HardwareErrorCodeMessage,
20
20
  createDeferred,
21
- isBleStaleBondHardwareError,
22
21
  isHeaderChunk,
23
22
  } from '@onekeyfe/hd-shared';
24
23
 
@@ -27,6 +26,7 @@ import { resolveBlePacketCapacity } from './ble-packet-capacity';
27
26
  import type { Deferred } from '@onekeyfe/hd-shared';
28
27
  import type { DesktopAPI, NobleBleIpcErrorResponse } from '@onekeyfe/hd-transport-electron';
29
28
  import type {
29
+ AcquireInput,
30
30
  OneKeyDeviceInfo,
31
31
  ProtocolType,
32
32
  ProtocolV2CallContext,
@@ -56,7 +56,7 @@ interface PacketProcessResult {
56
56
  }
57
57
 
58
58
  const toBleDescriptor = (
59
- device: { id: string; name: string | null },
59
+ device: { id: string; name: string | null; bleColorCode?: string },
60
60
  protocolType?: ProtocolType
61
61
  ): OneKeyDeviceInfo =>
62
62
  ({
@@ -65,6 +65,7 @@ const toBleDescriptor = (
65
65
  path: device.id,
66
66
  debug: false,
67
67
  commType: 'electron-ble',
68
+ ...(device.bleColorCode ? { bleColorCode: device.bleColorCode } : {}),
68
69
  ...(protocolType ? { protocolType } : {}),
69
70
  } as OneKeyDeviceInfo);
70
71
 
@@ -117,6 +118,10 @@ export default class ElectronBleTransport {
117
118
 
118
119
  private connectedDevices: Set<string> = new Set();
119
120
 
121
+ private retainedDevices: Set<string> = new Set();
122
+
123
+ private protocolV2CallCounts: Map<string, number> = new Map();
124
+
120
125
  /**
121
126
  * Devices whose acquire() is still in flight.
122
127
  *
@@ -203,11 +208,14 @@ export default class ElectronBleTransport {
203
208
  if (error.code === HardwareErrorCode.BlePermissionError) {
204
209
  return ERRORS.TypedError(HardwareErrorCode.BlePermissionError);
205
210
  }
211
+ if (error.code === HardwareErrorCode.BleLocationError) {
212
+ return ERRORS.TypedError(HardwareErrorCode.BleLocationError);
213
+ }
206
214
  }
207
215
  const errorMessage = error.message || String(error);
208
216
  const poweredOffMessage = HardwareErrorCodeMessage[HardwareErrorCode.BlePoweredOff];
209
217
  const unsupportedMessage = HardwareErrorCodeMessage[HardwareErrorCode.BleUnsupported];
210
- const permissionMessage = HardwareErrorCodeMessage[HardwareErrorCode.BlePermissionError];
218
+ const permissionMessage = HardwareErrorCodeMessage[HardwareErrorCode.BleLocationError];
211
219
 
212
220
  if (errorMessage.includes(poweredOffMessage) || errorMessage.includes('poweredOff')) {
213
221
  return ERRORS.TypedError(HardwareErrorCode.BlePoweredOff);
@@ -216,7 +224,7 @@ export default class ElectronBleTransport {
216
224
  return ERRORS.TypedError(HardwareErrorCode.BleUnsupported);
217
225
  }
218
226
  if (errorMessage.includes(permissionMessage) || errorMessage.includes('unauthorized')) {
219
- return ERRORS.TypedError(HardwareErrorCode.BlePermissionError);
227
+ return ERRORS.TypedError(HardwareErrorCode.BleLocationError);
220
228
  }
221
229
  }
222
230
  return error;
@@ -231,6 +239,7 @@ export default class ElectronBleTransport {
231
239
  .invalidateLink(deviceId, 'Electron BLE device state cleaned')
232
240
  .catch(error => this.Log?.debug('[Electron BLE] link cleanup failed:', error));
233
241
  this.connectedDevices.delete(deviceId);
242
+ this.retainedDevices.delete(deviceId);
234
243
  this.deviceProtocol.delete(deviceId);
235
244
  this.deviceMtus.delete(deviceId);
236
245
  this.devicePacketCapacities.delete(deviceId);
@@ -355,13 +364,22 @@ export default class ElectronBleTransport {
355
364
  }
356
365
 
357
366
  async acquire(input: BleAcquireInput) {
358
- const { uuid, forceCleanRunPromise, expectedProtocol } = input;
367
+ const { uuid, forceCleanRunPromise, expectedProtocol, forceProtocolDetection }: AcquireInput =
368
+ input;
359
369
 
360
370
  if (!uuid) {
361
371
  throw ERRORS.TypedError(HardwareErrorCode.BleRequiredUUID);
362
372
  }
363
373
 
364
- if (this.connectedDevices.has(uuid)) {
374
+ const retainedToken =
375
+ this.retainedDevices.has(uuid) &&
376
+ !forceProtocolDetection &&
377
+ (!expectedProtocol || expectedProtocol === 'V2') &&
378
+ this.deviceProtocol.get(uuid) === 'V2'
379
+ ? this.notificationTokens.get(uuid)
380
+ : undefined;
381
+ this.retainedDevices.delete(uuid);
382
+ if (this.connectedDevices.has(uuid) && retainedToken === undefined) {
365
383
  await this.release(uuid);
366
384
  }
367
385
 
@@ -382,6 +400,18 @@ export default class ElectronBleTransport {
382
400
  if (!device) {
383
401
  throw ERRORS.TypedError(HardwareErrorCode.DeviceNotFound, `Device ${uuid} not found`);
384
402
  }
403
+ // A disconnect during the IPC lookup invalidates the retained notification generation.
404
+ if (
405
+ retainedToken !== undefined &&
406
+ this.notificationTokens.get(uuid) === retainedToken &&
407
+ this.connectedDevices.has(uuid) &&
408
+ this.deviceProtocol.get(uuid) === 'V2'
409
+ ) {
410
+ return {
411
+ ...toBleDescriptor({ id: device.id, name: device.name }, 'V2'),
412
+ uuid,
413
+ };
414
+ }
385
415
  const protocolHint = expectedProtocol
386
416
  ? undefined
387
417
  : input.protocolHint ?? this.deviceProtocolHints.get(uuid);
@@ -449,10 +479,18 @@ export default class ElectronBleTransport {
449
479
  }
450
480
  }
451
481
 
452
- async release(id: string, _onclose?: boolean, keepSession?: boolean) {
482
+ async release(id: string, onclose?: boolean, keepSession?: boolean) {
453
483
  try {
454
- await this.protocolV2Links.invalidateLink(id, 'Electron BLE transport released');
455
- await this.releaseLogical(id, keepSession);
484
+ const retainLink =
485
+ keepSession === true &&
486
+ !onclose &&
487
+ this.connectedDevices.has(id) &&
488
+ this.deviceProtocol.get(id) === 'V2' &&
489
+ !this.protocolV2CallCounts.has(id);
490
+ if (!retainLink) {
491
+ await this.protocolV2Links.invalidateLink(id, 'Electron BLE transport released');
492
+ }
493
+ await this.releaseLogical(id, keepSession, retainLink);
456
494
  } catch (error) {
457
495
  this.Log?.error('[Electron BLE] release failed:', error);
458
496
  this.cleanupDeviceState(id);
@@ -497,12 +535,15 @@ export default class ElectronBleTransport {
497
535
  }
498
536
  }
499
537
 
500
- // Logical release: link and subscription stay up for the next call. Renderer
501
- // listeners are still torn down, or fresh ones would double-process packets.
502
- private async releaseLogical(id: string, keepSession?: boolean) {
538
+ // Retained V2 links reuse their renderer listeners; legacy releases remove them.
539
+ private async releaseLogical(id: string, keepSession?: boolean, retainLink = false) {
503
540
  try {
504
541
  if (!this.connectedDevices.has(id)) return;
505
- this.cleanupDeviceState(id);
542
+ if (retainLink) {
543
+ this.retainedDevices.add(id);
544
+ } else {
545
+ this.cleanupDeviceState(id);
546
+ }
506
547
 
507
548
  const release = window.desktopApi?.nobleBle?.release;
508
549
  if (!release) {
@@ -524,17 +565,13 @@ export default class ElectronBleTransport {
524
565
  }
525
566
 
526
567
  private createProtocolMismatchError(expected: ProtocolType) {
527
- return ERRORS.TypedError(
528
- HardwareErrorCode.RuntimeError,
529
- `Device protocol mismatch: expected ${expected}, but device did not respond to expected protocol`
530
- );
568
+ return ERRORS.TypedError(HardwareErrorCode.TransportProtocolProbeFailed, undefined, {
569
+ expectedProtocol: expected,
570
+ });
531
571
  }
532
572
 
533
573
  private createProtocolDetectionError() {
534
- return ERRORS.TypedError(
535
- HardwareErrorCode.BleTimeoutError,
536
- 'Unable to detect BLE protocol: device did not respond to Protocol V1 GetFeatures or Protocol V2 Ping'
537
- );
574
+ return ERRORS.TypedError(HardwareErrorCode.TransportProtocolProbeFailed);
538
575
  }
539
576
 
540
577
  private clearProbeProtocol(uuid: string, protocol: ProtocolType) {
@@ -676,10 +713,7 @@ export default class ElectronBleTransport {
676
713
  this.resetProtocolV2Frames(uuid);
677
714
  },
678
715
  // A declared V2 protocol needs no fallback; preserve the actual link failure.
679
- shouldRethrow: error =>
680
- expectedProtocol === 'V2' ||
681
- isBleStaleBondHardwareError(error) ||
682
- isProtocolV2LinkDisabledError(error),
716
+ shouldRethrow: error => expectedProtocol === 'V2' || isProtocolV2LinkDisabledError(error),
683
717
  });
684
718
  if (!detected) {
685
719
  this.clearProbeProtocol(uuid, 'V2');
@@ -1020,6 +1054,7 @@ export default class ElectronBleTransport {
1020
1054
 
1021
1055
  const callOptions = options;
1022
1056
 
1057
+ this.protocolV2CallCounts.set(uuid, (this.protocolV2CallCounts.get(uuid) ?? 0) + 1);
1023
1058
  try {
1024
1059
  return await this.protocolV2Links.call(
1025
1060
  uuid,
@@ -1031,6 +1066,10 @@ export default class ElectronBleTransport {
1031
1066
  } catch (e) {
1032
1067
  this.Log?.error('[Electron BLE] Protocol V2 call error:', e);
1033
1068
  throw e;
1069
+ } finally {
1070
+ const remaining = (this.protocolV2CallCounts.get(uuid) ?? 1) - 1;
1071
+ if (remaining > 0) this.protocolV2CallCounts.set(uuid, remaining);
1072
+ else this.protocolV2CallCounts.delete(uuid);
1034
1073
  }
1035
1074
  }
1036
1075