@onekeyfe/hd-transport-web-device 1.2.11-alpha.10 → 1.2.11-alpha.100
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 +0 -77
- package/__tests__/webusb-protocol-cache.test.ts +0 -31
- package/dist/electron-ble-transport.d.ts +0 -5
- package/dist/electron-ble-transport.d.ts.map +1 -1
- package/dist/index.d.ts +2 -7
- package/dist/index.js +10 -59
- package/dist/webusb.d.ts +2 -2
- package/dist/webusb.d.ts.map +1 -1
- package/package.json +5 -6
- package/src/electron-ble-transport.ts +7 -62
- package/src/webusb.ts +7 -6
|
@@ -1372,81 +1372,4 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
1372
1372
|
await bleTransport.release(device.id);
|
|
1373
1373
|
}
|
|
1374
1374
|
});
|
|
1375
|
-
|
|
1376
|
-
test('stop() unsubscribes host disconnect listener and releases owned links idempotently', async () => {
|
|
1377
|
-
const device = { id: 'stop-cleanup-pro2-id', name: 'OneKey Pro 2' };
|
|
1378
|
-
const nobleBle = createNobleBle(device);
|
|
1379
|
-
const emitter = new EventEmitter();
|
|
1380
|
-
const hostDisconnectCleanup = jest.fn();
|
|
1381
|
-
const notifyCleanup = jest.fn();
|
|
1382
|
-
const mtuCleanup = jest.fn();
|
|
1383
|
-
let disconnectHandler:
|
|
1384
|
-
| ((d: { id: string; name: string; reason?: EBleDisconnectReason }) => void)
|
|
1385
|
-
| undefined;
|
|
1386
|
-
|
|
1387
|
-
nobleBle.onDeviceDisconnected.mockImplementation(handler => {
|
|
1388
|
-
disconnectHandler = handler;
|
|
1389
|
-
return () => {
|
|
1390
|
-
hostDisconnectCleanup();
|
|
1391
|
-
disconnectHandler = undefined;
|
|
1392
|
-
};
|
|
1393
|
-
});
|
|
1394
|
-
echoProtocolV2(nobleBle, device.id);
|
|
1395
|
-
const echoOnNotification = nobleBle.onNotification.getMockImplementation();
|
|
1396
|
-
nobleBle.onNotification.mockImplementation(handler => {
|
|
1397
|
-
echoOnNotification?.(handler);
|
|
1398
|
-
return notifyCleanup;
|
|
1399
|
-
});
|
|
1400
|
-
nobleBle.onMtuChanged.mockImplementation(() => mtuCleanup);
|
|
1401
|
-
|
|
1402
|
-
const transportDisconnect = jest.fn();
|
|
1403
|
-
emitter.on('transport-device-disconnect', transportDisconnect);
|
|
1404
|
-
const bleTransport = configureTransport(nobleBle, emitter);
|
|
1405
|
-
|
|
1406
|
-
await bleTransport.acquire({ uuid: device.id, expectedProtocol: 'V2' });
|
|
1407
|
-
expect(bleTransport.getProtocolType(device.id)).toBe('V2');
|
|
1408
|
-
|
|
1409
|
-
await bleTransport.stop();
|
|
1410
|
-
|
|
1411
|
-
expect(hostDisconnectCleanup).toHaveBeenCalledTimes(1);
|
|
1412
|
-
expect(notifyCleanup).toHaveBeenCalledTimes(1);
|
|
1413
|
-
expect(mtuCleanup).toHaveBeenCalledTimes(1);
|
|
1414
|
-
expect(nobleBle.unsubscribe).toHaveBeenCalledWith(device.id);
|
|
1415
|
-
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1416
|
-
expect(bleTransport.getProtocolType(device.id)).toBeUndefined();
|
|
1417
|
-
|
|
1418
|
-
// Calling stop() again is a safe no-op.
|
|
1419
|
-
await bleTransport.stop();
|
|
1420
|
-
expect(hostDisconnectCleanup).toHaveBeenCalledTimes(1);
|
|
1421
|
-
expect(nobleBle.disconnect).toHaveBeenCalledTimes(1);
|
|
1422
|
-
|
|
1423
|
-
// Late host disconnect events are no longer delivered to the stopped transport.
|
|
1424
|
-
disconnectHandler?.({ ...device, reason: EBleDisconnectReason.DeviceDisconnected });
|
|
1425
|
-
expect(transportDisconnect).not.toHaveBeenCalled();
|
|
1426
|
-
});
|
|
1427
|
-
|
|
1428
|
-
test('stop() during getDevice() does not let retired acquire disconnect a replacement link', async () => {
|
|
1429
|
-
const device = { id: 'lookup-stop-pro2-id', name: 'OneKey Pro 2' };
|
|
1430
|
-
const nobleBle = createNobleBle(device);
|
|
1431
|
-
let resolveGetDevice: ((value: typeof device) => void) | undefined;
|
|
1432
|
-
nobleBle.getDevice.mockImplementation(
|
|
1433
|
-
() =>
|
|
1434
|
-
new Promise(resolve => {
|
|
1435
|
-
resolveGetDevice = resolve;
|
|
1436
|
-
})
|
|
1437
|
-
);
|
|
1438
|
-
const bleTransport = configureTransport(nobleBle);
|
|
1439
|
-
|
|
1440
|
-
const acquiring = bleTransport.acquire({ uuid: device.id, expectedProtocol: 'V2' });
|
|
1441
|
-
const stopping = bleTransport.stop();
|
|
1442
|
-
await stopping;
|
|
1443
|
-
// Clear disconnect count recorded during stopResources() so we verify the late
|
|
1444
|
-
// retired acquire does not issue another disconnect against a replacement link.
|
|
1445
|
-
nobleBle.disconnect.mockClear();
|
|
1446
|
-
|
|
1447
|
-
resolveGetDevice?.(device);
|
|
1448
|
-
await expect(acquiring).rejects.toThrow(/Electron BLE transport is stopped/);
|
|
1449
|
-
expect(nobleBle.connect).not.toHaveBeenCalled();
|
|
1450
|
-
expect(nobleBle.disconnect).not.toHaveBeenCalled();
|
|
1451
|
-
});
|
|
1452
1375
|
});
|
|
@@ -16,37 +16,6 @@ const schema = {
|
|
|
16
16
|
},
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
test('reads a multi-packet V1 response without a global Buffer', async () => {
|
|
20
|
-
const webusb = new WebUsbTransport();
|
|
21
|
-
const messages = transport.parseConfigure(schema);
|
|
22
|
-
const response = { message: 'v1 response '.repeat(9) };
|
|
23
|
-
const expected = transport.ProtocolV1.encodeEnvelope(messages, 'Success', response).toString(
|
|
24
|
-
'hex'
|
|
25
|
-
);
|
|
26
|
-
const packets = transport.ProtocolV1.encodeTransportPackets(messages, 'Success', response);
|
|
27
|
-
expect(packets.length).toBeGreaterThan(1);
|
|
28
|
-
const reader = webusb as unknown as {
|
|
29
|
-
transferInWithRetry: () => Promise<Uint8Array>;
|
|
30
|
-
};
|
|
31
|
-
const transferIn = jest.spyOn(reader, 'transferInWithRetry');
|
|
32
|
-
packets.forEach(packet => {
|
|
33
|
-
transferIn.mockResolvedValueOnce(new Uint8Array(packet.toBuffer()));
|
|
34
|
-
});
|
|
35
|
-
const buffer = globalThis.Buffer;
|
|
36
|
-
try {
|
|
37
|
-
Object.defineProperty(globalThis, 'Buffer', { value: undefined });
|
|
38
|
-
const received = await webusb.receiveData('synthetic-usb');
|
|
39
|
-
expect(received).toBe(expected);
|
|
40
|
-
expect(transport.ProtocolV1.decodeMessage(messages, received)).toMatchObject({
|
|
41
|
-
type: 'Success',
|
|
42
|
-
message: response,
|
|
43
|
-
});
|
|
44
|
-
} finally {
|
|
45
|
-
Object.defineProperty(globalThis, 'Buffer', { value: buffer });
|
|
46
|
-
transferIn.mockRestore();
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
|
|
50
19
|
function buildAcquirableTransport(path = 'pro-webusb') {
|
|
51
20
|
const webusb = new WebUsbTransport() as any;
|
|
52
21
|
webusb.Log = { debug: jest.fn() };
|
|
@@ -28,8 +28,6 @@ export default class ElectronBleTransport {
|
|
|
28
28
|
private retainedDevices;
|
|
29
29
|
private protocolV2CallCounts;
|
|
30
30
|
private acquiringDevices;
|
|
31
|
-
private stopped;
|
|
32
|
-
private stopPromise?;
|
|
33
31
|
private deviceProtocol;
|
|
34
32
|
private deviceProtocolHints;
|
|
35
33
|
private deviceMtus;
|
|
@@ -49,9 +47,6 @@ export default class ElectronBleTransport {
|
|
|
49
47
|
private handleBluetoothError;
|
|
50
48
|
private cleanupDeviceState;
|
|
51
49
|
init(logger: any, emitter?: EventEmitter): void;
|
|
52
|
-
private assertRunning;
|
|
53
|
-
stop(): Promise<void>;
|
|
54
|
-
private stopResources;
|
|
55
50
|
private subscribeHostDisconnects;
|
|
56
51
|
configure(signedData: any): void;
|
|
57
52
|
configureProtocolV2(signedData: any): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"electron-ble-transport.d.ts","sourceRoot":"","sources":["../src/electron-ble-transport.ts"],"names":[],"mappings":";AAyBA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,EAAE,UAAU,EAA4B,MAAM,iCAAiC,CAAC;AAC5F,OAAO,KAAK,EAEV,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;AAkDF,MAAM,CAAC,OAAO,OAAO,oBAAoB;IACvC,OAAO,CAAC,SAAS,
|
|
1
|
+
{"version":3,"file":"electron-ble-transport.d.ts","sourceRoot":"","sources":["../src/electron-ble-transport.ts"],"names":[],"mappings":";AAyBA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,EAAE,UAAU,EAA4B,MAAM,iCAAiC,CAAC;AAC5F,OAAO,KAAK,EAEV,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;CAC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Y,GAAG,SAAS;CAGxD"}
|
package/dist/index.d.ts
CHANGED
|
@@ -22,9 +22,9 @@ declare class WebUsbTransport extends ProtocolV2UsbTransportBase<string> {
|
|
|
22
22
|
private handleOperationSequence;
|
|
23
23
|
private deviceLogSequence;
|
|
24
24
|
private deviceLogIds;
|
|
25
|
-
messages: ReturnType<typeof _onekeyfe_hd_transport__default.
|
|
25
|
+
messages: ReturnType<typeof _onekeyfe_hd_transport__default.parseConfigure> | undefined;
|
|
26
26
|
/** Protobuf schema for Protocol V2 transports. */
|
|
27
|
-
messagesV2: ReturnType<typeof _onekeyfe_hd_transport__default.
|
|
27
|
+
messagesV2: ReturnType<typeof _onekeyfe_hd_transport__default.parseConfigure> | undefined;
|
|
28
28
|
private protocolV2SchemaSource;
|
|
29
29
|
/** Per-path protocol type detected by active wire-level probe. */
|
|
30
30
|
private deviceProtocol;
|
|
@@ -265,8 +265,6 @@ declare class ElectronBleTransport {
|
|
|
265
265
|
* the link; `releaseNative()` needs it to cancel that pending connect.
|
|
266
266
|
*/
|
|
267
267
|
private acquiringDevices;
|
|
268
|
-
private stopped;
|
|
269
|
-
private stopPromise?;
|
|
270
268
|
private deviceProtocol;
|
|
271
269
|
private deviceProtocolHints;
|
|
272
270
|
private deviceMtus;
|
|
@@ -295,9 +293,6 @@ declare class ElectronBleTransport {
|
|
|
295
293
|
private handleBluetoothError;
|
|
296
294
|
private cleanupDeviceState;
|
|
297
295
|
init(logger: any, emitter?: EventEmitter): void;
|
|
298
|
-
private assertRunning;
|
|
299
|
-
stop(): Promise<void>;
|
|
300
|
-
private stopResources;
|
|
301
296
|
/**
|
|
302
297
|
* One host subscription for the whole transport lifetime. init() can run
|
|
303
298
|
* again after an SDK reset, so drop the previous listener first rather than
|
package/dist/index.js
CHANGED
|
@@ -41,7 +41,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
41
41
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
const {
|
|
44
|
+
const { parseConfigure: parseConfigure$1, check: check$1, ProtocolV1: ProtocolV1$1 } = transport__default["default"];
|
|
45
45
|
const CONFIGURATION_ID = 1;
|
|
46
46
|
const INTERFACE_ID = 0;
|
|
47
47
|
const ENDPOINT_ID = 1;
|
|
@@ -320,7 +320,7 @@ class WebUsbTransport extends transport.ProtocolV2UsbTransportBase {
|
|
|
320
320
|
this.probedDeviceObjects.delete(path);
|
|
321
321
|
}
|
|
322
322
|
configure(signedData) {
|
|
323
|
-
const messages =
|
|
323
|
+
const messages = parseConfigure$1(signedData);
|
|
324
324
|
this.configured = true;
|
|
325
325
|
this.messages = messages;
|
|
326
326
|
}
|
|
@@ -332,7 +332,7 @@ class WebUsbTransport extends transport.ProtocolV2UsbTransportBase {
|
|
|
332
332
|
if (schemaSource === this.protocolV2SchemaSource)
|
|
333
333
|
return;
|
|
334
334
|
const hadProtocolV2Schema = this.protocolV2SchemaSource !== undefined;
|
|
335
|
-
this.messagesV2 =
|
|
335
|
+
this.messagesV2 = parseConfigure$1(signedData);
|
|
336
336
|
this.protocolV2SchemaSource = schemaSource;
|
|
337
337
|
if (!hadProtocolV2Schema)
|
|
338
338
|
return;
|
|
@@ -1428,7 +1428,8 @@ class WebUsbTransport extends transport.ProtocolV2UsbTransportBase {
|
|
|
1428
1428
|
}
|
|
1429
1429
|
}
|
|
1430
1430
|
decoded.reset();
|
|
1431
|
-
|
|
1431
|
+
const result = decoded.toBuffer();
|
|
1432
|
+
return Buffer.from(result).toString('hex');
|
|
1432
1433
|
});
|
|
1433
1434
|
}
|
|
1434
1435
|
release(path) {
|
|
@@ -1504,7 +1505,7 @@ function resolveBlePacketCapacity(mtu, maximumPacketCapacity, fallbackPacketCapa
|
|
|
1504
1505
|
return Math.min(maximumPacketCapacity, Math.floor(mtu) - BLE_ATT_HEADER_BYTES);
|
|
1505
1506
|
}
|
|
1506
1507
|
|
|
1507
|
-
const {
|
|
1508
|
+
const { parseConfigure, ProtocolV1, check } = transport__default["default"];
|
|
1508
1509
|
const toBleDescriptor = (device, protocolType) => (Object.assign(Object.assign({ id: device.id, name: device.name, path: device.id, debug: false, commType: 'electron-ble' }, (device.bleColorCode ? { bleColorCode: device.bleColorCode } : {})), (protocolType ? { protocolType } : {})));
|
|
1509
1510
|
const invokeNobleBle = (request) => __awaiter(void 0, void 0, void 0, function* () {
|
|
1510
1511
|
const response = yield request;
|
|
@@ -1534,7 +1535,6 @@ class ElectronBleTransport {
|
|
|
1534
1535
|
this.retainedDevices = new Set();
|
|
1535
1536
|
this.protocolV2CallCounts = new Map();
|
|
1536
1537
|
this.acquiringDevices = new Set();
|
|
1537
|
-
this.stopped = false;
|
|
1538
1538
|
this.deviceProtocol = new Map();
|
|
1539
1539
|
this.deviceProtocolHints = new Map();
|
|
1540
1540
|
this.deviceMtus = new Map();
|
|
@@ -1559,7 +1559,7 @@ class ElectronBleTransport {
|
|
|
1559
1559
|
(_a = this.v2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
|
|
1560
1560
|
this.rejectProtocolV2Frames(uuid, new Error(reason));
|
|
1561
1561
|
(_b = this.Log) === null || _b === void 0 ? void 0 : _b.debug('[Electron BLE] Protocol V2 link invalidated:', uuid, reason);
|
|
1562
|
-
if (
|
|
1562
|
+
if (reason.startsWith('Protocol V2 link-fatal error:')) {
|
|
1563
1563
|
yield this.releaseNative(uuid, { forceNative: true });
|
|
1564
1564
|
}
|
|
1565
1565
|
}),
|
|
@@ -1639,8 +1639,6 @@ class ElectronBleTransport {
|
|
|
1639
1639
|
}
|
|
1640
1640
|
init(logger, emitter) {
|
|
1641
1641
|
var _a, _b;
|
|
1642
|
-
this.stopped = false;
|
|
1643
|
-
this.stopPromise = undefined;
|
|
1644
1642
|
this.Log = logger;
|
|
1645
1643
|
this.emitter = emitter;
|
|
1646
1644
|
if (!((_a = window.desktopApi) === null || _a === void 0 ? void 0 : _a.nobleBle)) {
|
|
@@ -1649,41 +1647,6 @@ class ElectronBleTransport {
|
|
|
1649
1647
|
this.subscribeHostDisconnects();
|
|
1650
1648
|
(_b = this.Log) === null || _b === void 0 ? void 0 : _b.debug('[Electron BLE] Transport initialized');
|
|
1651
1649
|
}
|
|
1652
|
-
assertRunning() {
|
|
1653
|
-
if (this.stopped) {
|
|
1654
|
-
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Electron BLE transport is stopped');
|
|
1655
|
-
}
|
|
1656
|
-
}
|
|
1657
|
-
stop() {
|
|
1658
|
-
var _a;
|
|
1659
|
-
this.stopped = true;
|
|
1660
|
-
(_a = this.hostDisconnectCleanup) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
1661
|
-
this.hostDisconnectCleanup = undefined;
|
|
1662
|
-
if (!this.stopPromise) {
|
|
1663
|
-
this.stopPromise = this.stopResources();
|
|
1664
|
-
}
|
|
1665
|
-
return this.stopPromise;
|
|
1666
|
-
}
|
|
1667
|
-
stopResources() {
|
|
1668
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1669
|
-
const trackedDeviceIds = new Set([
|
|
1670
|
-
...this.connectedDevices,
|
|
1671
|
-
...this.retainedDevices,
|
|
1672
|
-
...this.acquiringDevices,
|
|
1673
|
-
...this.notificationCleanups.keys(),
|
|
1674
|
-
...this.mtuCleanups.keys(),
|
|
1675
|
-
...this.deviceProtocol.keys(),
|
|
1676
|
-
...this.v1Buffers.keys(),
|
|
1677
|
-
...this.v2Assemblers.keys(),
|
|
1678
|
-
]);
|
|
1679
|
-
yield Promise.all(Array.from(trackedDeviceIds, deviceId => this.releaseNative(deviceId)));
|
|
1680
|
-
this.acquiringDevices.clear();
|
|
1681
|
-
this.protocolV2CallCounts.clear();
|
|
1682
|
-
yield this.protocolV2Links
|
|
1683
|
-
.dispose('Electron BLE transport stopped')
|
|
1684
|
-
.catch(error => { var _a; return (_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('[Electron BLE] link dispose failed:', error); });
|
|
1685
|
-
});
|
|
1686
|
-
}
|
|
1687
1650
|
subscribeHostDisconnects() {
|
|
1688
1651
|
var _a, _b, _c;
|
|
1689
1652
|
(_a = this.hostDisconnectCleanup) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
@@ -1702,7 +1665,7 @@ class ElectronBleTransport {
|
|
|
1702
1665
|
});
|
|
1703
1666
|
}
|
|
1704
1667
|
configure(signedData) {
|
|
1705
|
-
this._messages =
|
|
1668
|
+
this._messages = parseConfigure(signedData);
|
|
1706
1669
|
this.configured = true;
|
|
1707
1670
|
}
|
|
1708
1671
|
configureProtocolV2(signedData) {
|
|
@@ -1711,7 +1674,7 @@ class ElectronBleTransport {
|
|
|
1711
1674
|
return;
|
|
1712
1675
|
}
|
|
1713
1676
|
const isReconfiguration = this.protocolV2SchemaConfiguration !== undefined;
|
|
1714
|
-
this._messagesV2 =
|
|
1677
|
+
this._messagesV2 = parseConfigure(signedData);
|
|
1715
1678
|
this.protocolV2SchemaConfiguration = configuration;
|
|
1716
1679
|
if (isReconfiguration) {
|
|
1717
1680
|
this.protocolV2Links
|
|
@@ -1767,15 +1730,12 @@ class ElectronBleTransport {
|
|
|
1767
1730
|
this.runPromise = null;
|
|
1768
1731
|
this.runPromiseDeviceId = null;
|
|
1769
1732
|
}
|
|
1770
|
-
this.assertRunning();
|
|
1771
1733
|
this.acquiringDevices.add(uuid);
|
|
1772
|
-
let connectAttempted = false;
|
|
1773
1734
|
try {
|
|
1774
1735
|
if (!((_a = window.desktopApi) === null || _a === void 0 ? void 0 : _a.nobleBle)) {
|
|
1775
1736
|
throw new Error('Noble BLE API not available');
|
|
1776
1737
|
}
|
|
1777
1738
|
const device = yield invokeNobleBle(window.desktopApi.nobleBle.getDevice(uuid));
|
|
1778
|
-
this.assertRunning();
|
|
1779
1739
|
if (!device) {
|
|
1780
1740
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceNotFound, `Device ${uuid} not found`);
|
|
1781
1741
|
}
|
|
@@ -1792,14 +1752,12 @@ class ElectronBleTransport {
|
|
|
1792
1752
|
this.deviceProtocolHints.set(uuid, protocolHint);
|
|
1793
1753
|
}
|
|
1794
1754
|
try {
|
|
1795
|
-
connectAttempted = true;
|
|
1796
1755
|
yield invokeNobleBle(window.desktopApi.nobleBle.connect(uuid));
|
|
1797
1756
|
this.connectedDevices.add(uuid);
|
|
1798
1757
|
}
|
|
1799
1758
|
catch (error) {
|
|
1800
1759
|
this.handleBluetoothError(error);
|
|
1801
1760
|
}
|
|
1802
|
-
this.assertRunning();
|
|
1803
1761
|
const mtuCleanup = this.createMtuSubscription(uuid);
|
|
1804
1762
|
if (mtuCleanup) {
|
|
1805
1763
|
this.mtuCleanups.set(uuid, mtuCleanup);
|
|
@@ -1813,11 +1771,9 @@ class ElectronBleTransport {
|
|
|
1813
1771
|
this.handleBluetoothError(error);
|
|
1814
1772
|
}
|
|
1815
1773
|
yield this.refreshBlePacketCapacity(uuid);
|
|
1816
|
-
this.assertRunning();
|
|
1817
1774
|
const cleanup = this.createNotificationSubscription(uuid);
|
|
1818
1775
|
this.notificationCleanups.set(uuid, cleanup);
|
|
1819
1776
|
const protocolType = yield this.detectProtocol(uuid, expectedProtocol, protocolHint);
|
|
1820
|
-
this.assertRunning();
|
|
1821
1777
|
if (protocolType === 'V2') {
|
|
1822
1778
|
(_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug('[Electron BLE] Protocol V2 write configured', {
|
|
1823
1779
|
writeMode: 'withoutResponse',
|
|
@@ -1831,7 +1787,7 @@ class ElectronBleTransport {
|
|
|
1831
1787
|
(_e = this.Log) === null || _e === void 0 ? void 0 : _e.error('[Electron BLE] acquire failed:', error);
|
|
1832
1788
|
try {
|
|
1833
1789
|
const nobleBle = (_f = window.desktopApi) === null || _f === void 0 ? void 0 : _f.nobleBle;
|
|
1834
|
-
if (nobleBle
|
|
1790
|
+
if (nobleBle) {
|
|
1835
1791
|
if (this.connectedDevices.has(uuid)) {
|
|
1836
1792
|
yield invokeNobleBle(nobleBle.unsubscribe(uuid)).catch(cleanupError => {
|
|
1837
1793
|
var _a;
|
|
@@ -1961,14 +1917,11 @@ class ElectronBleTransport {
|
|
|
1961
1917
|
}
|
|
1962
1918
|
const probeOrder = protocolHint === 'V2' || this.deviceProtocol.get(uuid) === 'V2' ? ['V2', 'V1'] : ['V1', 'V2'];
|
|
1963
1919
|
for (let i = 0; i < probeOrder.length; i += 1) {
|
|
1964
|
-
this.assertRunning();
|
|
1965
1920
|
const protocol = probeOrder[i];
|
|
1966
1921
|
if (i > 0) {
|
|
1967
1922
|
yield this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
|
|
1968
|
-
this.assertRunning();
|
|
1969
1923
|
}
|
|
1970
1924
|
const detected = protocol === 'V1' ? yield this.probeProtocolV1(uuid) : yield this.probeProtocolV2(uuid);
|
|
1971
|
-
this.assertRunning();
|
|
1972
1925
|
if (detected) {
|
|
1973
1926
|
this.deviceProtocol.set(uuid, protocol);
|
|
1974
1927
|
(_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
@@ -2062,14 +2015,12 @@ class ElectronBleTransport {
|
|
|
2062
2015
|
writeOnce(uuid, hexData) {
|
|
2063
2016
|
var _a;
|
|
2064
2017
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2065
|
-
this.assertRunning();
|
|
2066
2018
|
const nobleBle = (_a = window.desktopApi) === null || _a === void 0 ? void 0 : _a.nobleBle;
|
|
2067
2019
|
if (!nobleBle) {
|
|
2068
2020
|
throw new Error('Noble BLE API not available');
|
|
2069
2021
|
}
|
|
2070
2022
|
try {
|
|
2071
2023
|
yield invokeNobleBle(nobleBle.write(uuid, hexData, { pacingDelayMs: 0 }));
|
|
2072
|
-
this.assertRunning();
|
|
2073
2024
|
}
|
|
2074
2025
|
catch (error) {
|
|
2075
2026
|
this.handleBluetoothError(error);
|
package/dist/webusb.d.ts
CHANGED
|
@@ -18,8 +18,8 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
18
18
|
private handleOperationSequence;
|
|
19
19
|
private deviceLogSequence;
|
|
20
20
|
private deviceLogIds;
|
|
21
|
-
messages: ReturnType<typeof transport.
|
|
22
|
-
messagesV2: ReturnType<typeof transport.
|
|
21
|
+
messages: ReturnType<typeof transport.parseConfigure> | undefined;
|
|
22
|
+
messagesV2: ReturnType<typeof transport.parseConfigure> | undefined;
|
|
23
23
|
private protocolV2SchemaSource;
|
|
24
24
|
private deviceProtocol;
|
|
25
25
|
private confirmedDeviceProtocols;
|
package/dist/webusb.d.ts.map
CHANGED
|
@@ -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;AAahC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EACV,YAAY,EACZ,oBAAoB,EACpB,YAAY,EACZ,qBAAqB,EACrB,iBAAiB,EACjB,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AA0BhC,MAAM,WAAW,UAAW,SAAQ,oBAAoB;IACtD,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,SAAS,CAAC;IAClB,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B;AA2BD,UAAU,oBAAoB;IAC5B,gBAAgB,CAAC,EAAE,YAAY,CAAC;IAChC,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC;AAmCD,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,0BAA0B,CAAC,MAAM,CAAC;IA+EjE,OAAO,CAAC,QAAQ,CAAC,aAAa;IA9E1C,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAmC;IAEvE,OAAO,CAAC,uBAAuB,CAAK;IAEpC,OAAO,CAAC,iBAAiB,CAAK;IAE9B,OAAO,CAAC,YAAY,CAAoC;IAExD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AAM,EAAE,SAAS,CAAC,EAAE,MAAM;IAgD5C,OAAO,CAAC,IAAI,EAAE,MAAM;IA8B1B,SAAS,CAAC,uBAAuB,IAAI,iBAAiB;IAUtD,SAAS,CAAC,sBAAsB;cAIhB,wBAAwB,CACtC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,qBAAqB,GAC9B,OAAO,CAAC,IAAI,CAAC;cAIA,uBAAuB,CACrC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,qBAAqB,GAC9B,OAAO,CAAC,UAAU,CAAC;cASN,4BAA4B,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI1F,SAAS,CAAC,8BAA8B,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAKrE,SAAS,CAAC,+BAA+B,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,KAAK;IAWjF,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;CAGxD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-web-device",
|
|
3
|
-
"version": "1.2.11-alpha.
|
|
3
|
+
"version": "1.2.11-alpha.100",
|
|
4
4
|
"author": "OneKey",
|
|
5
5
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,14 +21,13 @@
|
|
|
21
21
|
"lint:fix": "eslint . --fix"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@onekeyfe/hd-shared": "1.2.11-alpha.
|
|
25
|
-
"@onekeyfe/hd-transport": "1.2.11-alpha.
|
|
26
|
-
"bytebuffer": "^5.0.1"
|
|
24
|
+
"@onekeyfe/hd-shared": "1.2.11-alpha.100",
|
|
25
|
+
"@onekeyfe/hd-transport": "1.2.11-alpha.100"
|
|
27
26
|
},
|
|
28
27
|
"devDependencies": {
|
|
29
|
-
"@onekeyfe/hd-transport-electron": "1.2.11-alpha.
|
|
28
|
+
"@onekeyfe/hd-transport-electron": "1.2.11-alpha.100",
|
|
30
29
|
"@types/w3c-web-usb": "^1.0.6",
|
|
31
30
|
"@types/web-bluetooth": "^0.0.17"
|
|
32
31
|
},
|
|
33
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "5c6c87d1f16d21d9b345e1f74c069ef32912e11a"
|
|
34
33
|
}
|
|
@@ -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 { parseConfigure, ProtocolV1, check } = transport;
|
|
38
38
|
|
|
39
39
|
declare global {
|
|
40
40
|
interface Window {
|
|
@@ -98,9 +98,9 @@ const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
|
|
|
98
98
|
* after a Protocol V1 GetFeatures timeout by probing Protocol V2 Ping.
|
|
99
99
|
*/
|
|
100
100
|
export default class ElectronBleTransport {
|
|
101
|
-
private _messages: ReturnType<typeof transport.
|
|
101
|
+
private _messages: ReturnType<typeof transport.parseConfigure> | undefined;
|
|
102
102
|
|
|
103
|
-
private _messagesV2: ReturnType<typeof transport.
|
|
103
|
+
private _messagesV2: ReturnType<typeof transport.parseConfigure> | undefined;
|
|
104
104
|
|
|
105
105
|
private protocolV2SchemaConfiguration: string | undefined;
|
|
106
106
|
|
|
@@ -131,10 +131,6 @@ export default class ElectronBleTransport {
|
|
|
131
131
|
*/
|
|
132
132
|
private acquiringDevices: Set<string> = new Set();
|
|
133
133
|
|
|
134
|
-
private stopped = false;
|
|
135
|
-
|
|
136
|
-
private stopPromise?: Promise<void>;
|
|
137
|
-
|
|
138
134
|
private deviceProtocol: Map<string, ProtocolType> = new Map();
|
|
139
135
|
|
|
140
136
|
private deviceProtocolHints: Map<string, ProtocolType> = new Map();
|
|
@@ -166,7 +162,7 @@ export default class ElectronBleTransport {
|
|
|
166
162
|
this.v2Assemblers.get(uuid)?.reset();
|
|
167
163
|
this.rejectProtocolV2Frames(uuid, new Error(reason));
|
|
168
164
|
this.Log?.debug('[Electron BLE] Protocol V2 link invalidated:', uuid, reason);
|
|
169
|
-
if (
|
|
165
|
+
if (reason.startsWith('Protocol V2 link-fatal error:')) {
|
|
170
166
|
await this.releaseNative(uuid, { forceNative: true });
|
|
171
167
|
}
|
|
172
168
|
},
|
|
@@ -273,8 +269,6 @@ export default class ElectronBleTransport {
|
|
|
273
269
|
}
|
|
274
270
|
|
|
275
271
|
init(logger: any, emitter?: EventEmitter) {
|
|
276
|
-
this.stopped = false;
|
|
277
|
-
this.stopPromise = undefined;
|
|
278
272
|
this.Log = logger;
|
|
279
273
|
this.emitter = emitter;
|
|
280
274
|
|
|
@@ -290,43 +284,6 @@ export default class ElectronBleTransport {
|
|
|
290
284
|
this.Log?.debug('[Electron BLE] Transport initialized');
|
|
291
285
|
}
|
|
292
286
|
|
|
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
|
-
|
|
330
287
|
/**
|
|
331
288
|
* One host subscription for the whole transport lifetime. init() can run
|
|
332
289
|
* again after an SDK reset, so drop the previous listener first rather than
|
|
@@ -365,7 +322,7 @@ export default class ElectronBleTransport {
|
|
|
365
322
|
}
|
|
366
323
|
|
|
367
324
|
configure(signedData: any) {
|
|
368
|
-
this._messages =
|
|
325
|
+
this._messages = parseConfigure(signedData);
|
|
369
326
|
this.configured = true;
|
|
370
327
|
}
|
|
371
328
|
|
|
@@ -376,7 +333,7 @@ export default class ElectronBleTransport {
|
|
|
376
333
|
}
|
|
377
334
|
|
|
378
335
|
const isReconfiguration = this.protocolV2SchemaConfiguration !== undefined;
|
|
379
|
-
this._messagesV2 =
|
|
336
|
+
this._messagesV2 = parseConfigure(signedData);
|
|
380
337
|
this.protocolV2SchemaConfiguration = configuration;
|
|
381
338
|
if (isReconfiguration) {
|
|
382
339
|
this.protocolV2Links
|
|
@@ -433,16 +390,13 @@ export default class ElectronBleTransport {
|
|
|
433
390
|
this.runPromiseDeviceId = null;
|
|
434
391
|
}
|
|
435
392
|
|
|
436
|
-
this.assertRunning();
|
|
437
393
|
this.acquiringDevices.add(uuid);
|
|
438
|
-
let connectAttempted = false;
|
|
439
394
|
try {
|
|
440
395
|
if (!window.desktopApi?.nobleBle) {
|
|
441
396
|
throw new Error('Noble BLE API not available');
|
|
442
397
|
}
|
|
443
398
|
|
|
444
399
|
const device = await invokeNobleBle(window.desktopApi.nobleBle.getDevice(uuid));
|
|
445
|
-
this.assertRunning();
|
|
446
400
|
if (!device) {
|
|
447
401
|
throw ERRORS.TypedError(HardwareErrorCode.DeviceNotFound, `Device ${uuid} not found`);
|
|
448
402
|
}
|
|
@@ -466,13 +420,11 @@ export default class ElectronBleTransport {
|
|
|
466
420
|
}
|
|
467
421
|
|
|
468
422
|
try {
|
|
469
|
-
connectAttempted = true;
|
|
470
423
|
await invokeNobleBle(window.desktopApi.nobleBle.connect(uuid));
|
|
471
424
|
this.connectedDevices.add(uuid);
|
|
472
425
|
} catch (error) {
|
|
473
426
|
this.handleBluetoothError(error);
|
|
474
427
|
}
|
|
475
|
-
this.assertRunning();
|
|
476
428
|
|
|
477
429
|
const mtuCleanup = this.createMtuSubscription(uuid);
|
|
478
430
|
if (mtuCleanup) {
|
|
@@ -488,13 +440,11 @@ export default class ElectronBleTransport {
|
|
|
488
440
|
this.handleBluetoothError(error);
|
|
489
441
|
}
|
|
490
442
|
await this.refreshBlePacketCapacity(uuid);
|
|
491
|
-
this.assertRunning();
|
|
492
443
|
|
|
493
444
|
const cleanup = this.createNotificationSubscription(uuid);
|
|
494
445
|
this.notificationCleanups.set(uuid, cleanup);
|
|
495
446
|
|
|
496
447
|
const protocolType = await this.detectProtocol(uuid, expectedProtocol, protocolHint);
|
|
497
|
-
this.assertRunning();
|
|
498
448
|
if (protocolType === 'V2') {
|
|
499
449
|
this.Log?.debug('[Electron BLE] Protocol V2 write configured', {
|
|
500
450
|
writeMode: 'withoutResponse',
|
|
@@ -511,7 +461,7 @@ export default class ElectronBleTransport {
|
|
|
511
461
|
this.Log?.error('[Electron BLE] acquire failed:', error);
|
|
512
462
|
try {
|
|
513
463
|
const nobleBle = window.desktopApi?.nobleBle;
|
|
514
|
-
if (nobleBle
|
|
464
|
+
if (nobleBle) {
|
|
515
465
|
if (this.connectedDevices.has(uuid)) {
|
|
516
466
|
await invokeNobleBle(nobleBle.unsubscribe(uuid)).catch(cleanupError => {
|
|
517
467
|
this.Log?.debug('[Electron BLE] acquire unsubscribe failed:', cleanupError);
|
|
@@ -666,17 +616,14 @@ export default class ElectronBleTransport {
|
|
|
666
616
|
protocolHint === 'V2' || this.deviceProtocol.get(uuid) === 'V2' ? ['V2', 'V1'] : ['V1', 'V2'];
|
|
667
617
|
|
|
668
618
|
for (let i = 0; i < probeOrder.length; i += 1) {
|
|
669
|
-
this.assertRunning();
|
|
670
619
|
const protocol = probeOrder[i];
|
|
671
620
|
if (i > 0) {
|
|
672
621
|
// Keep the physical BLE link while switching probes. Reconnecting here can
|
|
673
622
|
// summon a second OS pairing prompt for a device that is still onboarding.
|
|
674
623
|
await this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
|
|
675
|
-
this.assertRunning();
|
|
676
624
|
}
|
|
677
625
|
const detected =
|
|
678
626
|
protocol === 'V1' ? await this.probeProtocolV1(uuid) : await this.probeProtocolV2(uuid);
|
|
679
|
-
this.assertRunning();
|
|
680
627
|
if (detected) {
|
|
681
628
|
this.deviceProtocol.set(uuid, protocol);
|
|
682
629
|
this.Log?.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
@@ -775,7 +722,6 @@ export default class ElectronBleTransport {
|
|
|
775
722
|
}
|
|
776
723
|
|
|
777
724
|
private async writeOnce(uuid: string, hexData: string): Promise<void> {
|
|
778
|
-
this.assertRunning();
|
|
779
725
|
const nobleBle = window.desktopApi?.nobleBle;
|
|
780
726
|
if (!nobleBle) {
|
|
781
727
|
throw new Error('Noble BLE API not available');
|
|
@@ -783,7 +729,6 @@ export default class ElectronBleTransport {
|
|
|
783
729
|
|
|
784
730
|
try {
|
|
785
731
|
await invokeNobleBle(nobleBle.write(uuid, hexData, { pacingDelayMs: 0 }));
|
|
786
|
-
this.assertRunning();
|
|
787
732
|
} catch (error) {
|
|
788
733
|
this.handleBluetoothError(error);
|
|
789
734
|
}
|
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 { parseConfigure, 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.parseConfigure> | undefined;
|
|
139
139
|
|
|
140
140
|
/** Protobuf schema for Protocol V2 transports. */
|
|
141
|
-
messagesV2: ReturnType<typeof transport.
|
|
141
|
+
messagesV2: ReturnType<typeof transport.parseConfigure> | 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 = parseConfigure(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 = parseConfigure(signedData);
|
|
525
525
|
this.protocolV2SchemaSource = schemaSource;
|
|
526
526
|
if (!hadProtocolV2Schema) return;
|
|
527
527
|
|
|
@@ -1828,7 +1828,8 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
1828
1828
|
}
|
|
1829
1829
|
}
|
|
1830
1830
|
decoded.reset();
|
|
1831
|
-
|
|
1831
|
+
const result = decoded.toBuffer();
|
|
1832
|
+
return Buffer.from(result as unknown as ArrayBuffer).toString('hex');
|
|
1832
1833
|
}
|
|
1833
1834
|
|
|
1834
1835
|
/**
|