@onekeyfe/hd-transport-web-device 1.2.11-alpha.8 → 1.2.11-alpha.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/__tests__/electron-ble-transport.test.ts +63 -579
- package/__tests__/webusb-identity-probe.test.ts +2 -6
- package/__tests__/webusb-protocol-cache.test.ts +31 -0
- package/__tests__/webusb-protocol-v2-timeout.test.ts +3 -3
- package/dist/electron-ble-transport.d.ts +5 -0
- package/dist/electron-ble-transport.d.ts.map +1 -1
- package/dist/index.d.ts +7 -2
- package/dist/index.js +73 -73
- package/dist/webusb.d.ts +2 -2
- package/dist/webusb.d.ts.map +1 -1
- package/package.json +6 -5
- package/src/electron-ble-transport.ts +74 -84
- package/src/webusb.ts +8 -10
|
@@ -34,7 +34,7 @@ import type {
|
|
|
34
34
|
} from '@onekeyfe/hd-transport';
|
|
35
35
|
import type EventEmitter from 'events';
|
|
36
36
|
|
|
37
|
-
const {
|
|
37
|
+
const { parseTransportConfigure, ProtocolV1, check } = transport;
|
|
38
38
|
|
|
39
39
|
declare global {
|
|
40
40
|
interface Window {
|
|
@@ -91,53 +91,6 @@ const BLE_WRITE_DELAY_MS = 5;
|
|
|
91
91
|
const PROTOCOL_PROBE_TIMEOUT_MS = 3000;
|
|
92
92
|
const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
|
|
93
93
|
|
|
94
|
-
const isSilentWindowsPairingNoResponse = (error: unknown): boolean => {
|
|
95
|
-
if (!error || typeof error !== 'object') {
|
|
96
|
-
return false;
|
|
97
|
-
}
|
|
98
|
-
const candidate = error as { errorCode?: unknown; params?: unknown };
|
|
99
|
-
const code = candidate.errorCode;
|
|
100
|
-
if (code !== HardwareErrorCode.BleDeviceBondError && code !== HardwareErrorCode.BleBondInvalid) {
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
const { params } = candidate;
|
|
104
|
-
if (!params || typeof params !== 'object') {
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
107
|
-
const { phase, platform, stage, reason, nativeErrorMessage } = params as Record<string, unknown>;
|
|
108
|
-
if (phase !== 'pairing' || platform !== 'win32' || stage !== 'write') {
|
|
109
|
-
return false;
|
|
110
|
-
}
|
|
111
|
-
if (typeof nativeErrorMessage === 'string' && nativeErrorMessage.length > 0) {
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
return reason === 'pairing_no_response' || reason === 'stale_bond';
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
const shouldRethrowProtocolProbeError = (error: unknown, isLastProbe = false): boolean => {
|
|
118
|
-
if (isProtocolV2LinkDisabledError(error)) {
|
|
119
|
-
return true;
|
|
120
|
-
}
|
|
121
|
-
// A Windows write loop that received no notification without any native error
|
|
122
|
-
// happens when a V2-only device ignores the initial V1 probe; allow fallback
|
|
123
|
-
// to the alternate protocol on the same link. Confirmed pairing errors carry
|
|
124
|
-
// native evidence and remain terminal. If this is the last remaining probe,
|
|
125
|
-
// preserve the silent 715/724 so Core can show re-pair instead of retrying
|
|
126
|
-
// TransportProtocolProbeFailed.
|
|
127
|
-
if (isSilentWindowsPairingNoResponse(error)) {
|
|
128
|
-
return isLastProbe;
|
|
129
|
-
}
|
|
130
|
-
const code = (error as { errorCode?: unknown })?.errorCode;
|
|
131
|
-
// Pairing-recovery and cancel failures are not protocol mismatches; preserve
|
|
132
|
-
// them instead of falling through to another probe or ProbeFailed.
|
|
133
|
-
return (
|
|
134
|
-
code === HardwareErrorCode.BleDeviceBondError ||
|
|
135
|
-
code === HardwareErrorCode.BleDeviceBondedCanceled ||
|
|
136
|
-
code === HardwareErrorCode.BlePeerRemovedPairingInformation ||
|
|
137
|
-
code === HardwareErrorCode.BleBondInvalid
|
|
138
|
-
);
|
|
139
|
-
};
|
|
140
|
-
|
|
141
94
|
/**
|
|
142
95
|
* Desktop Electron BLE transport with automatic Protocol V1/V2 detection.
|
|
143
96
|
*
|
|
@@ -145,9 +98,9 @@ const shouldRethrowProtocolProbeError = (error: unknown, isLastProbe = false): b
|
|
|
145
98
|
* after a Protocol V1 GetFeatures timeout by probing Protocol V2 Ping.
|
|
146
99
|
*/
|
|
147
100
|
export default class ElectronBleTransport {
|
|
148
|
-
private _messages: ReturnType<typeof transport.
|
|
101
|
+
private _messages: ReturnType<typeof transport.parseTransportConfigure> | undefined;
|
|
149
102
|
|
|
150
|
-
private _messagesV2: ReturnType<typeof transport.
|
|
103
|
+
private _messagesV2: ReturnType<typeof transport.parseTransportConfigure> | undefined;
|
|
151
104
|
|
|
152
105
|
private protocolV2SchemaConfiguration: string | undefined;
|
|
153
106
|
|
|
@@ -178,6 +131,10 @@ export default class ElectronBleTransport {
|
|
|
178
131
|
*/
|
|
179
132
|
private acquiringDevices: Set<string> = new Set();
|
|
180
133
|
|
|
134
|
+
private stopped = false;
|
|
135
|
+
|
|
136
|
+
private stopPromise?: Promise<void>;
|
|
137
|
+
|
|
181
138
|
private deviceProtocol: Map<string, ProtocolType> = new Map();
|
|
182
139
|
|
|
183
140
|
private deviceProtocolHints: Map<string, ProtocolType> = new Map();
|
|
@@ -316,6 +273,8 @@ export default class ElectronBleTransport {
|
|
|
316
273
|
}
|
|
317
274
|
|
|
318
275
|
init(logger: any, emitter?: EventEmitter) {
|
|
276
|
+
this.stopped = false;
|
|
277
|
+
this.stopPromise = undefined;
|
|
319
278
|
this.Log = logger;
|
|
320
279
|
this.emitter = emitter;
|
|
321
280
|
|
|
@@ -331,6 +290,43 @@ export default class ElectronBleTransport {
|
|
|
331
290
|
this.Log?.debug('[Electron BLE] Transport initialized');
|
|
332
291
|
}
|
|
333
292
|
|
|
293
|
+
private assertRunning() {
|
|
294
|
+
if (this.stopped) {
|
|
295
|
+
throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'Electron BLE transport is stopped');
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
stop(): Promise<void> {
|
|
300
|
+
this.stopped = true;
|
|
301
|
+
this.hostDisconnectCleanup?.();
|
|
302
|
+
this.hostDisconnectCleanup = undefined;
|
|
303
|
+
|
|
304
|
+
if (!this.stopPromise) {
|
|
305
|
+
this.stopPromise = this.stopResources();
|
|
306
|
+
}
|
|
307
|
+
return this.stopPromise;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
private async stopResources(): Promise<void> {
|
|
311
|
+
const trackedDeviceIds = new Set<string>([
|
|
312
|
+
...this.connectedDevices,
|
|
313
|
+
...this.retainedDevices,
|
|
314
|
+
...this.acquiringDevices,
|
|
315
|
+
...this.notificationCleanups.keys(),
|
|
316
|
+
...this.mtuCleanups.keys(),
|
|
317
|
+
...this.deviceProtocol.keys(),
|
|
318
|
+
...this.v1Buffers.keys(),
|
|
319
|
+
...this.v2Assemblers.keys(),
|
|
320
|
+
]);
|
|
321
|
+
|
|
322
|
+
await Promise.all(Array.from(trackedDeviceIds, deviceId => this.releaseNative(deviceId)));
|
|
323
|
+
this.acquiringDevices.clear();
|
|
324
|
+
this.protocolV2CallCounts.clear();
|
|
325
|
+
await this.protocolV2Links
|
|
326
|
+
.dispose('Electron BLE transport stopped')
|
|
327
|
+
.catch(error => this.Log?.debug('[Electron BLE] link dispose failed:', error));
|
|
328
|
+
}
|
|
329
|
+
|
|
334
330
|
/**
|
|
335
331
|
* One host subscription for the whole transport lifetime. init() can run
|
|
336
332
|
* again after an SDK reset, so drop the previous listener first rather than
|
|
@@ -369,7 +365,7 @@ export default class ElectronBleTransport {
|
|
|
369
365
|
}
|
|
370
366
|
|
|
371
367
|
configure(signedData: any) {
|
|
372
|
-
this._messages =
|
|
368
|
+
this._messages = parseTransportConfigure(signedData);
|
|
373
369
|
this.configured = true;
|
|
374
370
|
}
|
|
375
371
|
|
|
@@ -380,7 +376,7 @@ export default class ElectronBleTransport {
|
|
|
380
376
|
}
|
|
381
377
|
|
|
382
378
|
const isReconfiguration = this.protocolV2SchemaConfiguration !== undefined;
|
|
383
|
-
this._messagesV2 =
|
|
379
|
+
this._messagesV2 = parseTransportConfigure(signedData);
|
|
384
380
|
this.protocolV2SchemaConfiguration = configuration;
|
|
385
381
|
if (isReconfiguration) {
|
|
386
382
|
this.protocolV2Links
|
|
@@ -437,13 +433,16 @@ export default class ElectronBleTransport {
|
|
|
437
433
|
this.runPromiseDeviceId = null;
|
|
438
434
|
}
|
|
439
435
|
|
|
436
|
+
this.assertRunning();
|
|
440
437
|
this.acquiringDevices.add(uuid);
|
|
438
|
+
let connectAttempted = false;
|
|
441
439
|
try {
|
|
442
440
|
if (!window.desktopApi?.nobleBle) {
|
|
443
441
|
throw new Error('Noble BLE API not available');
|
|
444
442
|
}
|
|
445
443
|
|
|
446
444
|
const device = await invokeNobleBle(window.desktopApi.nobleBle.getDevice(uuid));
|
|
445
|
+
this.assertRunning();
|
|
447
446
|
if (!device) {
|
|
448
447
|
throw ERRORS.TypedError(HardwareErrorCode.DeviceNotFound, `Device ${uuid} not found`);
|
|
449
448
|
}
|
|
@@ -467,11 +466,13 @@ export default class ElectronBleTransport {
|
|
|
467
466
|
}
|
|
468
467
|
|
|
469
468
|
try {
|
|
469
|
+
connectAttempted = true;
|
|
470
470
|
await invokeNobleBle(window.desktopApi.nobleBle.connect(uuid));
|
|
471
471
|
this.connectedDevices.add(uuid);
|
|
472
472
|
} catch (error) {
|
|
473
473
|
this.handleBluetoothError(error);
|
|
474
474
|
}
|
|
475
|
+
this.assertRunning();
|
|
475
476
|
|
|
476
477
|
const mtuCleanup = this.createMtuSubscription(uuid);
|
|
477
478
|
if (mtuCleanup) {
|
|
@@ -487,11 +488,13 @@ export default class ElectronBleTransport {
|
|
|
487
488
|
this.handleBluetoothError(error);
|
|
488
489
|
}
|
|
489
490
|
await this.refreshBlePacketCapacity(uuid);
|
|
491
|
+
this.assertRunning();
|
|
490
492
|
|
|
491
493
|
const cleanup = this.createNotificationSubscription(uuid);
|
|
492
494
|
this.notificationCleanups.set(uuid, cleanup);
|
|
493
495
|
|
|
494
496
|
const protocolType = await this.detectProtocol(uuid, expectedProtocol, protocolHint);
|
|
497
|
+
this.assertRunning();
|
|
495
498
|
if (protocolType === 'V2') {
|
|
496
499
|
this.Log?.debug('[Electron BLE] Protocol V2 write configured', {
|
|
497
500
|
writeMode: 'withoutResponse',
|
|
@@ -508,7 +511,7 @@ export default class ElectronBleTransport {
|
|
|
508
511
|
this.Log?.error('[Electron BLE] acquire failed:', error);
|
|
509
512
|
try {
|
|
510
513
|
const nobleBle = window.desktopApi?.nobleBle;
|
|
511
|
-
if (nobleBle) {
|
|
514
|
+
if (nobleBle && !this.stopped && (connectAttempted || this.connectedDevices.has(uuid))) {
|
|
512
515
|
if (this.connectedDevices.has(uuid)) {
|
|
513
516
|
await invokeNobleBle(nobleBle.unsubscribe(uuid)).catch(cleanupError => {
|
|
514
517
|
this.Log?.debug('[Electron BLE] acquire unsubscribe failed:', cleanupError);
|
|
@@ -664,16 +667,13 @@ export default class ElectronBleTransport {
|
|
|
664
667
|
|
|
665
668
|
for (let i = 0; i < probeOrder.length; i += 1) {
|
|
666
669
|
const protocol = probeOrder[i];
|
|
667
|
-
const isLastProbe = i === probeOrder.length - 1;
|
|
668
670
|
if (i > 0) {
|
|
669
671
|
// Keep the physical BLE link while switching probes. Reconnecting here can
|
|
670
672
|
// summon a second OS pairing prompt for a device that is still onboarding.
|
|
671
673
|
await this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
|
|
672
674
|
}
|
|
673
675
|
const detected =
|
|
674
|
-
protocol === 'V1'
|
|
675
|
-
? await this.probeProtocolV1(uuid, isLastProbe)
|
|
676
|
-
: await this.probeProtocolV2(uuid, undefined, isLastProbe);
|
|
676
|
+
protocol === 'V1' ? await this.probeProtocolV1(uuid) : await this.probeProtocolV2(uuid);
|
|
677
677
|
if (detected) {
|
|
678
678
|
this.deviceProtocol.set(uuid, protocol);
|
|
679
679
|
this.Log?.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
@@ -725,7 +725,7 @@ export default class ElectronBleTransport {
|
|
|
725
725
|
}
|
|
726
726
|
}
|
|
727
727
|
|
|
728
|
-
private async probeProtocolV1(uuid: string
|
|
728
|
+
private async probeProtocolV1(uuid: string) {
|
|
729
729
|
if (!this._messages) {
|
|
730
730
|
return false;
|
|
731
731
|
}
|
|
@@ -739,46 +739,36 @@ export default class ElectronBleTransport {
|
|
|
739
739
|
} catch (error) {
|
|
740
740
|
this.clearProbeProtocol(uuid, 'V1');
|
|
741
741
|
this.Log?.debug('[Electron BLE] Protocol V1 GetFeatures probe failed:', error);
|
|
742
|
-
if (
|
|
742
|
+
if (isProtocolV2LinkDisabledError(error)) {
|
|
743
743
|
throw error;
|
|
744
744
|
}
|
|
745
745
|
return false;
|
|
746
746
|
}
|
|
747
747
|
}
|
|
748
748
|
|
|
749
|
-
private async probeProtocolV2(
|
|
750
|
-
uuid: string,
|
|
751
|
-
expectedProtocol?: ProtocolType,
|
|
752
|
-
isLastProbe = false
|
|
753
|
-
) {
|
|
749
|
+
private async probeProtocolV2(uuid: string, expectedProtocol?: ProtocolType) {
|
|
754
750
|
if (!this._messages || !this._messagesV2) {
|
|
755
751
|
return false;
|
|
756
752
|
}
|
|
757
753
|
|
|
758
754
|
this.deviceProtocol.set(uuid, 'V2');
|
|
759
755
|
this.v2Assemblers.get(uuid)?.reset();
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
});
|
|
774
|
-
if (!detected) {
|
|
775
|
-
this.clearProbeProtocol(uuid, 'V2');
|
|
776
|
-
}
|
|
777
|
-
return detected;
|
|
778
|
-
} catch (error) {
|
|
756
|
+
const detected = await probeProtocolV2Helper({
|
|
757
|
+
call: (name, data, options) => this.callProtocolV2(uuid, name, data, options),
|
|
758
|
+
timeoutMs: PROTOCOL_V2_PROBE_TIMEOUT_MS,
|
|
759
|
+
logger: this.Log,
|
|
760
|
+
logPrefix: 'ProtocolV2 BLE',
|
|
761
|
+
onProbeFailed: () => {
|
|
762
|
+
this.v2Assemblers.get(uuid)?.reset();
|
|
763
|
+
this.resetProtocolV2Frames(uuid);
|
|
764
|
+
},
|
|
765
|
+
// A declared V2 protocol needs no fallback; preserve the actual link failure.
|
|
766
|
+
shouldRethrow: error => expectedProtocol === 'V2' || isProtocolV2LinkDisabledError(error),
|
|
767
|
+
});
|
|
768
|
+
if (!detected) {
|
|
779
769
|
this.clearProbeProtocol(uuid, 'V2');
|
|
780
|
-
throw error;
|
|
781
770
|
}
|
|
771
|
+
return detected;
|
|
782
772
|
}
|
|
783
773
|
|
|
784
774
|
private async writeOnce(uuid: string, hexData: string): Promise<void> {
|
package/src/webusb.ts
CHANGED
|
@@ -33,7 +33,7 @@ import type {
|
|
|
33
33
|
TransportCallOptions,
|
|
34
34
|
} from '@onekeyfe/hd-transport';
|
|
35
35
|
|
|
36
|
-
const {
|
|
36
|
+
const { parseTransportConfigure, check, ProtocolV1 } = transport;
|
|
37
37
|
|
|
38
38
|
const CONFIGURATION_ID = 1;
|
|
39
39
|
const INTERFACE_ID = 0;
|
|
@@ -135,10 +135,10 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
135
135
|
|
|
136
136
|
private deviceLogIds = new WeakMap<USBDevice, number>();
|
|
137
137
|
|
|
138
|
-
messages: ReturnType<typeof transport.
|
|
138
|
+
messages: ReturnType<typeof transport.parseTransportConfigure> | undefined;
|
|
139
139
|
|
|
140
140
|
/** Protobuf schema for Protocol V2 transports. */
|
|
141
|
-
messagesV2: ReturnType<typeof transport.
|
|
141
|
+
messagesV2: ReturnType<typeof transport.parseTransportConfigure> | undefined;
|
|
142
142
|
|
|
143
143
|
private protocolV2SchemaSource: string | undefined;
|
|
144
144
|
|
|
@@ -505,7 +505,7 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
505
505
|
* Configure Protocol V1 protobuf schema (legacy chunked 0x3F framing).
|
|
506
506
|
*/
|
|
507
507
|
configure(signedData: any) {
|
|
508
|
-
const messages =
|
|
508
|
+
const messages = parseTransportConfigure(signedData);
|
|
509
509
|
this.configured = true;
|
|
510
510
|
this.messages = messages;
|
|
511
511
|
}
|
|
@@ -521,7 +521,7 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
521
521
|
if (schemaSource === this.protocolV2SchemaSource) return;
|
|
522
522
|
|
|
523
523
|
const hadProtocolV2Schema = this.protocolV2SchemaSource !== undefined;
|
|
524
|
-
this.messagesV2 =
|
|
524
|
+
this.messagesV2 = parseTransportConfigure(signedData);
|
|
525
525
|
this.protocolV2SchemaSource = schemaSource;
|
|
526
526
|
if (!hadProtocolV2Schema) return;
|
|
527
527
|
|
|
@@ -1672,16 +1672,15 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
1672
1672
|
}
|
|
1673
1673
|
|
|
1674
1674
|
try {
|
|
1675
|
-
// Initialize aborts on-device setup; protocol probing must remain read-only.
|
|
1676
1675
|
const response = await this.callProtocolV1(
|
|
1677
1676
|
path,
|
|
1678
|
-
'GetFeatures',
|
|
1677
|
+
this.identityProbe ? 'GetFeatures' : 'Initialize',
|
|
1679
1678
|
{},
|
|
1680
1679
|
{
|
|
1681
1680
|
timeoutMs: PROTOCOL_V1_PROBE_TIMEOUT,
|
|
1682
1681
|
}
|
|
1683
1682
|
);
|
|
1684
|
-
return response.type === 'Features';
|
|
1683
|
+
return !this.identityProbe || response.type === 'Features';
|
|
1685
1684
|
} catch (error) {
|
|
1686
1685
|
if (this.isWebUsbIoHardwareError(error)) throw error;
|
|
1687
1686
|
return false;
|
|
@@ -1829,8 +1828,7 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
1829
1828
|
}
|
|
1830
1829
|
}
|
|
1831
1830
|
decoded.reset();
|
|
1832
|
-
|
|
1833
|
-
return Buffer.from(result as unknown as ArrayBuffer).toString('hex');
|
|
1831
|
+
return decoded.toString('hex');
|
|
1834
1832
|
}
|
|
1835
1833
|
|
|
1836
1834
|
/**
|