@onekeyfe/hd-transport-web-device 1.2.11-alpha.4 → 1.2.11-alpha.5
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 +1 -594
- package/__tests__/webusb-protocol-cache.test.ts +31 -0
- package/dist/electron-ble-transport.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +25 -70
- package/dist/webusb.d.ts +2 -2
- package/dist/webusb.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/electron-ble-transport.ts +23 -83
- package/src/webusb.ts +6 -7
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
import transport, { PROTOCOL_V2_CHANNEL_BLE_UART, bytesToHex } from '@onekeyfe/hd-transport';
|
|
2
|
-
import {
|
|
3
|
-
EBleDisconnectReason,
|
|
4
|
-
EOneKeyBleMessageKeys,
|
|
5
|
-
ERRORS,
|
|
6
|
-
HardwareErrorCode,
|
|
7
|
-
createDeferred,
|
|
8
|
-
} from '@onekeyfe/hd-shared';
|
|
2
|
+
import { EBleDisconnectReason, HardwareErrorCode, createDeferred } from '@onekeyfe/hd-shared';
|
|
9
3
|
import EventEmitter from 'events';
|
|
10
4
|
|
|
11
5
|
import ElectronBleTransport from '../src/electron-ble-transport';
|
|
@@ -843,42 +837,6 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
843
837
|
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
844
838
|
});
|
|
845
839
|
|
|
846
|
-
test('detects a V2-only device on first acquire without a protocol hint', async () => {
|
|
847
|
-
const device = { id: 'v2-only-no-hint-id', name: 'OneKey Pro 2' };
|
|
848
|
-
const nobleBle = createNobleBle(device);
|
|
849
|
-
let notificationHandler: ((deviceId: string, data: string) => void) | undefined;
|
|
850
|
-
let responseSeq = 0;
|
|
851
|
-
nobleBle.onNotification.mockImplementation(handler => {
|
|
852
|
-
notificationHandler = handler;
|
|
853
|
-
return jest.fn();
|
|
854
|
-
});
|
|
855
|
-
nobleBle.write.mockImplementation(() => {
|
|
856
|
-
const writeCount = nobleBle.write.mock.calls.length;
|
|
857
|
-
if (writeCount === 1) {
|
|
858
|
-
return Promise.resolve();
|
|
859
|
-
}
|
|
860
|
-
responseSeq += 1;
|
|
861
|
-
const response = ProtocolV2.encodeFrame(
|
|
862
|
-
schemas,
|
|
863
|
-
'Success',
|
|
864
|
-
{ message: 'ok' },
|
|
865
|
-
{ router: PROTOCOL_V2_CHANNEL_BLE_UART, seq: responseSeq }
|
|
866
|
-
);
|
|
867
|
-
setTimeout(() => notificationHandler?.(device.id, bytesToHex(response)), 0);
|
|
868
|
-
return Promise.resolve();
|
|
869
|
-
});
|
|
870
|
-
const transport = configureTransport(nobleBle);
|
|
871
|
-
|
|
872
|
-
await expect(transport.acquire({ uuid: device.id })).resolves.toMatchObject({
|
|
873
|
-
uuid: device.id,
|
|
874
|
-
protocolType: 'V2',
|
|
875
|
-
});
|
|
876
|
-
expect(nobleBle.write).toHaveBeenCalledTimes(2);
|
|
877
|
-
expect(nobleBle.connect).toHaveBeenCalledTimes(1);
|
|
878
|
-
expect(nobleBle.subscribe).toHaveBeenCalledTimes(1);
|
|
879
|
-
expect(nobleBle.disconnect).not.toHaveBeenCalled();
|
|
880
|
-
});
|
|
881
|
-
|
|
882
840
|
test('keeps a first expected Protocol V2 probe miss retryable', async () => {
|
|
883
841
|
const device = { id: 'first-v2-id', name: 'OneKey Pro 2' };
|
|
884
842
|
const nobleBle = createNobleBle(device);
|
|
@@ -1072,221 +1030,6 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
1072
1030
|
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1073
1031
|
});
|
|
1074
1032
|
|
|
1075
|
-
test.each([
|
|
1076
|
-
HardwareErrorCode.BleDeviceBondError,
|
|
1077
|
-
HardwareErrorCode.BleDeviceBondedCanceled,
|
|
1078
|
-
HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
1079
|
-
HardwareErrorCode.BleBondInvalid,
|
|
1080
|
-
])('preserves pairing-recovery error %s during undeclared Protocol V1 probe', async errorCode => {
|
|
1081
|
-
const device = { id: `undeclared-v1-${errorCode}`, name: 'Unknown BLE Device' };
|
|
1082
|
-
const nobleBle = createNobleBle(device);
|
|
1083
|
-
nobleBle.write.mockRejectedValueOnce({
|
|
1084
|
-
name: 'HardwareError',
|
|
1085
|
-
message: `Pairing failure ${errorCode}`,
|
|
1086
|
-
errorCode,
|
|
1087
|
-
});
|
|
1088
|
-
const bleTransport = configureTransport(nobleBle);
|
|
1089
|
-
const probeProtocolV2 = jest.spyOn(bleTransport as any, 'probeProtocolV2');
|
|
1090
|
-
|
|
1091
|
-
await expect(bleTransport.acquire({ uuid: device.id })).rejects.toMatchObject({ errorCode });
|
|
1092
|
-
|
|
1093
|
-
expect(probeProtocolV2).not.toHaveBeenCalled();
|
|
1094
|
-
expect(nobleBle.write).toHaveBeenCalledTimes(1);
|
|
1095
|
-
expect(nobleBle.unsubscribe).toHaveBeenCalledWith(device.id);
|
|
1096
|
-
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1097
|
-
expect(bleTransport.getProtocolType(device.id)).toBeUndefined();
|
|
1098
|
-
});
|
|
1099
|
-
|
|
1100
|
-
test.each([
|
|
1101
|
-
[
|
|
1102
|
-
'pairing_no_response',
|
|
1103
|
-
HardwareErrorCode.BleDeviceBondError,
|
|
1104
|
-
{ phase: 'pairing', platform: 'win32', stage: 'write', reason: 'pairing_no_response' },
|
|
1105
|
-
],
|
|
1106
|
-
[
|
|
1107
|
-
'silence stale_bond',
|
|
1108
|
-
HardwareErrorCode.BleBondInvalid,
|
|
1109
|
-
{ phase: 'pairing', platform: 'win32', stage: 'write', reason: 'stale_bond' },
|
|
1110
|
-
],
|
|
1111
|
-
] as const)(
|
|
1112
|
-
'falls through to Protocol V2 when undeclared V1 probe hits silent Windows %s',
|
|
1113
|
-
async (_label, errorCode, params) => {
|
|
1114
|
-
const device = { id: `win-silent-${errorCode}`, name: 'Unknown BLE Device' };
|
|
1115
|
-
const nobleBle = createNobleBle(device);
|
|
1116
|
-
let notificationHandler: ((deviceId: string, data: string) => void) | undefined;
|
|
1117
|
-
nobleBle.onNotification.mockImplementation(handler => {
|
|
1118
|
-
notificationHandler = handler;
|
|
1119
|
-
return jest.fn();
|
|
1120
|
-
});
|
|
1121
|
-
const probeResponse = ProtocolV2.encodeFrame(
|
|
1122
|
-
schemas,
|
|
1123
|
-
'Success',
|
|
1124
|
-
{ message: 'ok' },
|
|
1125
|
-
{ router: PROTOCOL_V2_CHANNEL_BLE_UART, seq: 1 }
|
|
1126
|
-
);
|
|
1127
|
-
nobleBle.write
|
|
1128
|
-
.mockRejectedValueOnce({
|
|
1129
|
-
name: 'HardwareError',
|
|
1130
|
-
message: 'Silent pairing miss',
|
|
1131
|
-
errorCode,
|
|
1132
|
-
params,
|
|
1133
|
-
})
|
|
1134
|
-
.mockImplementationOnce(() => {
|
|
1135
|
-
setTimeout(() => notificationHandler?.(device.id, bytesToHex(probeResponse)), 0);
|
|
1136
|
-
return Promise.resolve();
|
|
1137
|
-
});
|
|
1138
|
-
const bleTransport = configureTransport(nobleBle);
|
|
1139
|
-
|
|
1140
|
-
try {
|
|
1141
|
-
await expect(bleTransport.acquire({ uuid: device.id })).resolves.toMatchObject({
|
|
1142
|
-
uuid: device.id,
|
|
1143
|
-
protocolType: 'V2',
|
|
1144
|
-
});
|
|
1145
|
-
expect(nobleBle.write).toHaveBeenCalledTimes(2);
|
|
1146
|
-
expect(nobleBle.disconnect).not.toHaveBeenCalled();
|
|
1147
|
-
} finally {
|
|
1148
|
-
await bleTransport.release(device.id);
|
|
1149
|
-
}
|
|
1150
|
-
}
|
|
1151
|
-
);
|
|
1152
|
-
|
|
1153
|
-
test.each([
|
|
1154
|
-
[
|
|
1155
|
-
'pairing_no_response',
|
|
1156
|
-
HardwareErrorCode.BleDeviceBondError,
|
|
1157
|
-
{ phase: 'pairing', platform: 'win32', stage: 'write', reason: 'pairing_no_response' },
|
|
1158
|
-
],
|
|
1159
|
-
[
|
|
1160
|
-
'silence stale_bond',
|
|
1161
|
-
HardwareErrorCode.BleBondInvalid,
|
|
1162
|
-
{ phase: 'pairing', platform: 'win32', stage: 'write', reason: 'stale_bond' },
|
|
1163
|
-
],
|
|
1164
|
-
] as const)(
|
|
1165
|
-
'preserves silent Windows %s when both undeclared probes fail',
|
|
1166
|
-
async (_label, errorCode, params) => {
|
|
1167
|
-
const device = { id: `win-silent-both-${errorCode}`, name: 'Unknown BLE Device' };
|
|
1168
|
-
const nobleBle = createNobleBle(device);
|
|
1169
|
-
const silentError = {
|
|
1170
|
-
name: 'HardwareError',
|
|
1171
|
-
message: 'Silent pairing miss',
|
|
1172
|
-
errorCode,
|
|
1173
|
-
params,
|
|
1174
|
-
};
|
|
1175
|
-
nobleBle.write.mockRejectedValue(silentError);
|
|
1176
|
-
const bleTransport = configureTransport(nobleBle);
|
|
1177
|
-
|
|
1178
|
-
await expect(bleTransport.acquire({ uuid: device.id })).rejects.toMatchObject({
|
|
1179
|
-
errorCode,
|
|
1180
|
-
params,
|
|
1181
|
-
});
|
|
1182
|
-
|
|
1183
|
-
expect(nobleBle.write).toHaveBeenCalledTimes(2);
|
|
1184
|
-
expect(nobleBle.unsubscribe).toHaveBeenCalledWith(device.id);
|
|
1185
|
-
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1186
|
-
expect(bleTransport.getProtocolType(device.id)).toBeUndefined();
|
|
1187
|
-
}
|
|
1188
|
-
);
|
|
1189
|
-
|
|
1190
|
-
test.each([
|
|
1191
|
-
[
|
|
1192
|
-
HardwareErrorCode.BleDeviceBondError,
|
|
1193
|
-
{
|
|
1194
|
-
phase: 'pairing',
|
|
1195
|
-
platform: 'win32',
|
|
1196
|
-
stage: 'write',
|
|
1197
|
-
reason: 'pairing_failed',
|
|
1198
|
-
nativeErrorMessage: 'Access denied',
|
|
1199
|
-
},
|
|
1200
|
-
],
|
|
1201
|
-
[
|
|
1202
|
-
HardwareErrorCode.BleBondInvalid,
|
|
1203
|
-
{
|
|
1204
|
-
phase: 'pairing',
|
|
1205
|
-
platform: 'win32',
|
|
1206
|
-
stage: 'write',
|
|
1207
|
-
reason: 'stale_bond',
|
|
1208
|
-
nativeErrorMessage: 'status: 3',
|
|
1209
|
-
},
|
|
1210
|
-
],
|
|
1211
|
-
[
|
|
1212
|
-
HardwareErrorCode.BleBondInvalid,
|
|
1213
|
-
{
|
|
1214
|
-
phase: 'pairing',
|
|
1215
|
-
platform: 'win32',
|
|
1216
|
-
stage: 'subscribe',
|
|
1217
|
-
reason: 'stale_bond',
|
|
1218
|
-
},
|
|
1219
|
-
],
|
|
1220
|
-
] as const)(
|
|
1221
|
-
'keeps confirmed Windows pairing failure %s terminal during undeclared V1 probe',
|
|
1222
|
-
async (errorCode, params) => {
|
|
1223
|
-
const device = {
|
|
1224
|
-
id: `win-confirmed-${errorCode}-${params.stage}`,
|
|
1225
|
-
name: 'Unknown BLE Device',
|
|
1226
|
-
};
|
|
1227
|
-
const nobleBle = createNobleBle(device);
|
|
1228
|
-
nobleBle.write.mockRejectedValueOnce({
|
|
1229
|
-
name: 'HardwareError',
|
|
1230
|
-
message: 'Confirmed pairing failure',
|
|
1231
|
-
errorCode,
|
|
1232
|
-
params,
|
|
1233
|
-
});
|
|
1234
|
-
const bleTransport = configureTransport(nobleBle);
|
|
1235
|
-
const probeProtocolV2 = jest.spyOn(bleTransport as any, 'probeProtocolV2');
|
|
1236
|
-
|
|
1237
|
-
await expect(bleTransport.acquire({ uuid: device.id })).rejects.toMatchObject({
|
|
1238
|
-
errorCode,
|
|
1239
|
-
params,
|
|
1240
|
-
});
|
|
1241
|
-
|
|
1242
|
-
expect(probeProtocolV2).not.toHaveBeenCalled();
|
|
1243
|
-
expect(nobleBle.write).toHaveBeenCalledTimes(1);
|
|
1244
|
-
expect(nobleBle.unsubscribe).toHaveBeenCalledWith(device.id);
|
|
1245
|
-
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1246
|
-
}
|
|
1247
|
-
);
|
|
1248
|
-
|
|
1249
|
-
test.each([
|
|
1250
|
-
HardwareErrorCode.BleDeviceBondError,
|
|
1251
|
-
HardwareErrorCode.BleDeviceBondedCanceled,
|
|
1252
|
-
HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
1253
|
-
HardwareErrorCode.BleBondInvalid,
|
|
1254
|
-
])(
|
|
1255
|
-
'preserves pairing-recovery error %s during undeclared Protocol V2 probe after V1 miss',
|
|
1256
|
-
async errorCode => {
|
|
1257
|
-
const device = { id: `undeclared-v2-${errorCode}`, name: 'OneKey Pro 2' };
|
|
1258
|
-
const nobleBle = createNobleBle(device);
|
|
1259
|
-
let notificationHandler: ((deviceId: string, data: string) => void) | undefined;
|
|
1260
|
-
nobleBle.onNotification.mockImplementation(handler => {
|
|
1261
|
-
notificationHandler = handler;
|
|
1262
|
-
return jest.fn();
|
|
1263
|
-
});
|
|
1264
|
-
if (errorCode === HardwareErrorCode.BleDeviceBondedCanceled) {
|
|
1265
|
-
nobleBle.write.mockImplementationOnce(() => {
|
|
1266
|
-
setTimeout(() => notificationHandler?.(device.id, 'PAIRING_REJECTED'), 0);
|
|
1267
|
-
return Promise.resolve();
|
|
1268
|
-
});
|
|
1269
|
-
} else {
|
|
1270
|
-
nobleBle.write.mockRejectedValueOnce({
|
|
1271
|
-
name: 'HardwareError',
|
|
1272
|
-
message: `Pairing failure ${errorCode}`,
|
|
1273
|
-
errorCode,
|
|
1274
|
-
});
|
|
1275
|
-
}
|
|
1276
|
-
const bleTransport = configureTransport(nobleBle);
|
|
1277
|
-
jest
|
|
1278
|
-
.spyOn(bleTransport as any, 'callProtocolV1')
|
|
1279
|
-
.mockRejectedValueOnce(ERRORS.TypedError(HardwareErrorCode.BleTimeoutError));
|
|
1280
|
-
|
|
1281
|
-
await expect(bleTransport.acquire({ uuid: device.id })).rejects.toMatchObject({ errorCode });
|
|
1282
|
-
|
|
1283
|
-
expect(nobleBle.write).toHaveBeenCalledTimes(1);
|
|
1284
|
-
expect(nobleBle.unsubscribe).toHaveBeenCalledWith(device.id);
|
|
1285
|
-
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1286
|
-
expect(bleTransport.getProtocolType(device.id)).toBeUndefined();
|
|
1287
|
-
}
|
|
1288
|
-
);
|
|
1289
|
-
|
|
1290
1033
|
test('does not take a Protocol V2 hint from the BLE name', async () => {
|
|
1291
1034
|
const device = { id: 'named-pro2-id', name: 'OneKey Pro 2' };
|
|
1292
1035
|
const nobleBle = createNobleBle(device);
|
|
@@ -1630,339 +1373,3 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
1630
1373
|
}
|
|
1631
1374
|
});
|
|
1632
1375
|
});
|
|
1633
|
-
|
|
1634
|
-
describe('ElectronBleTransport Windows write-loop protocol detection integration', () => {
|
|
1635
|
-
const originalPlatform = process.platform;
|
|
1636
|
-
|
|
1637
|
-
afterEach(() => {
|
|
1638
|
-
Object.defineProperty(process, 'platform', { value: originalPlatform, configurable: true });
|
|
1639
|
-
delete (global as any).window;
|
|
1640
|
-
jest.useRealTimers();
|
|
1641
|
-
jest.resetModules();
|
|
1642
|
-
jest.clearAllMocks();
|
|
1643
|
-
});
|
|
1644
|
-
|
|
1645
|
-
const setupWindowsIntegration = async (options?: { writeErrorOnV1?: Error }) => {
|
|
1646
|
-
Object.defineProperty(process, 'platform', { value: 'win32', configurable: true });
|
|
1647
|
-
jest.useFakeTimers({ doNotFake: ['performance', 'setImmediate'] });
|
|
1648
|
-
|
|
1649
|
-
const handlers = new Map<string, (...args: unknown[]) => unknown>();
|
|
1650
|
-
const notificationListeners = new Set<(deviceId: string, data: string) => void>();
|
|
1651
|
-
const disconnectListeners = new Set<
|
|
1652
|
-
(device: { id: string; name: string; reason?: EBleDisconnectReason }) => void
|
|
1653
|
-
>();
|
|
1654
|
-
const mtuListeners = new Set<(device: { id: string; mtu: number }) => void>();
|
|
1655
|
-
|
|
1656
|
-
const webContents = {
|
|
1657
|
-
isDestroyed: jest.fn(() => false),
|
|
1658
|
-
on: jest.fn(),
|
|
1659
|
-
send: jest.fn((channel: string, ...args: unknown[]) => {
|
|
1660
|
-
if (channel === EOneKeyBleMessageKeys.NOBLE_BLE_NOTIFICATION) {
|
|
1661
|
-
const [deviceId, data] = args as [string, string];
|
|
1662
|
-
notificationListeners.forEach(listener => listener(deviceId, data));
|
|
1663
|
-
} else if (channel === EOneKeyBleMessageKeys.BLE_DEVICE_DISCONNECTED) {
|
|
1664
|
-
const [payload] = args as [{ id: string; name: string; reason?: EBleDisconnectReason }];
|
|
1665
|
-
disconnectListeners.forEach(listener => listener(payload));
|
|
1666
|
-
} else if (channel === EOneKeyBleMessageKeys.NOBLE_BLE_MTU_CHANGED) {
|
|
1667
|
-
const [payload] = args as [{ id: string; mtu: number }];
|
|
1668
|
-
mtuListeners.forEach(listener => listener(payload));
|
|
1669
|
-
}
|
|
1670
|
-
}),
|
|
1671
|
-
};
|
|
1672
|
-
|
|
1673
|
-
const ipcMain = {
|
|
1674
|
-
handle: jest.fn((channel: string, handler: (...args: unknown[]) => unknown) => {
|
|
1675
|
-
handlers.set(channel, handler);
|
|
1676
|
-
}),
|
|
1677
|
-
removeHandler: jest.fn((channel: string) => {
|
|
1678
|
-
handlers.delete(channel);
|
|
1679
|
-
}),
|
|
1680
|
-
};
|
|
1681
|
-
|
|
1682
|
-
const deviceId = 'win-v2-only-device';
|
|
1683
|
-
let responseSeq = 0;
|
|
1684
|
-
|
|
1685
|
-
const notifyCharacteristic = Object.assign(new EventEmitter(), {
|
|
1686
|
-
uuid: '0003',
|
|
1687
|
-
unsubscribe: jest.fn((callback: (error?: Error) => void) => callback()),
|
|
1688
|
-
subscribe: jest.fn((callback: (error?: Error) => void) => callback()),
|
|
1689
|
-
});
|
|
1690
|
-
|
|
1691
|
-
const writeCharacteristic = Object.assign(new EventEmitter(), {
|
|
1692
|
-
uuid: '0002',
|
|
1693
|
-
write: jest.fn(
|
|
1694
|
-
(buffer: Buffer, _withoutResponse: boolean, callback: (error?: Error) => void) => {
|
|
1695
|
-
if (buffer[0] === 0x5a) {
|
|
1696
|
-
// Emit synchronously so attemptWindowsWriteUntilPaired observes
|
|
1697
|
-
// pairedDevices before it starts another silent retry.
|
|
1698
|
-
notifyCharacteristic.emit('data', Buffer.alloc(0));
|
|
1699
|
-
callback();
|
|
1700
|
-
return;
|
|
1701
|
-
}
|
|
1702
|
-
if (options?.writeErrorOnV1) {
|
|
1703
|
-
callback(options.writeErrorOnV1);
|
|
1704
|
-
return;
|
|
1705
|
-
}
|
|
1706
|
-
// Healthy V2-only device ignores V1 GetFeatures silently without native errors.
|
|
1707
|
-
callback();
|
|
1708
|
-
}
|
|
1709
|
-
),
|
|
1710
|
-
});
|
|
1711
|
-
|
|
1712
|
-
const service = {
|
|
1713
|
-
uuid: '0001',
|
|
1714
|
-
discoverCharacteristics: jest.fn(
|
|
1715
|
-
(
|
|
1716
|
-
_characteristicUuids: string[],
|
|
1717
|
-
callback: (error: Error | null, value: unknown[]) => void
|
|
1718
|
-
) => callback(null, [writeCharacteristic, notifyCharacteristic])
|
|
1719
|
-
),
|
|
1720
|
-
};
|
|
1721
|
-
|
|
1722
|
-
const peripheral = Object.assign(new EventEmitter(), {
|
|
1723
|
-
id: deviceId,
|
|
1724
|
-
state: 'disconnected',
|
|
1725
|
-
advertisement: {
|
|
1726
|
-
localName: 'Unknown BLE Device',
|
|
1727
|
-
serviceUuids: ['0001', 'fffd'],
|
|
1728
|
-
},
|
|
1729
|
-
connect: jest.fn((callback: (error?: Error) => void) => {
|
|
1730
|
-
peripheral.state = 'connected';
|
|
1731
|
-
callback();
|
|
1732
|
-
}),
|
|
1733
|
-
disconnect: jest.fn((callback: () => void) => {
|
|
1734
|
-
peripheral.state = 'disconnected';
|
|
1735
|
-
callback();
|
|
1736
|
-
}),
|
|
1737
|
-
discoverServices: jest.fn(
|
|
1738
|
-
(_serviceUuids: string[], callback: (error: Error | null, value: unknown[]) => void) =>
|
|
1739
|
-
callback(null, [service])
|
|
1740
|
-
),
|
|
1741
|
-
});
|
|
1742
|
-
|
|
1743
|
-
const noble = Object.assign(new EventEmitter(), {
|
|
1744
|
-
state: 'poweredOn',
|
|
1745
|
-
startScanning: jest.fn((_services, _duplicates, callback) => {
|
|
1746
|
-
callback?.();
|
|
1747
|
-
noble.emit('discover', peripheral);
|
|
1748
|
-
}),
|
|
1749
|
-
stopScanning: jest.fn(callback => callback?.()),
|
|
1750
|
-
stop: jest.fn(),
|
|
1751
|
-
});
|
|
1752
|
-
|
|
1753
|
-
jest.resetModules();
|
|
1754
|
-
jest.doMock('@stoprocent/noble', () => noble);
|
|
1755
|
-
jest.doMock(
|
|
1756
|
-
'electron',
|
|
1757
|
-
() => ({
|
|
1758
|
-
ipcMain,
|
|
1759
|
-
webContents: { getAllWebContents: () => [webContents] },
|
|
1760
|
-
}),
|
|
1761
|
-
{ virtual: true }
|
|
1762
|
-
);
|
|
1763
|
-
jest.doMock('electron-log', () => ({
|
|
1764
|
-
info: jest.fn(),
|
|
1765
|
-
debug: jest.fn(),
|
|
1766
|
-
warn: jest.fn(),
|
|
1767
|
-
error: jest.fn(),
|
|
1768
|
-
}));
|
|
1769
|
-
|
|
1770
|
-
const sdk = await import('@onekeyfe/hd-transport-electron/src/noble-ble-handler');
|
|
1771
|
-
const TransportCtor = (await import('../src/electron-ble-transport')).default;
|
|
1772
|
-
sdk.setupNobleBleHandlers(webContents as any);
|
|
1773
|
-
|
|
1774
|
-
const invoke = (channel: string, ...args: unknown[]) => {
|
|
1775
|
-
const handler = handlers.get(channel);
|
|
1776
|
-
if (!handler) {
|
|
1777
|
-
throw new Error(`Missing IPC handler for ${channel}`);
|
|
1778
|
-
}
|
|
1779
|
-
return Promise.resolve(handler(undefined, ...args));
|
|
1780
|
-
};
|
|
1781
|
-
|
|
1782
|
-
const nobleBle = {
|
|
1783
|
-
enumerate: () => invoke(EOneKeyBleMessageKeys.NOBLE_BLE_ENUMERATE),
|
|
1784
|
-
getDevice: (id: string) => invoke(EOneKeyBleMessageKeys.NOBLE_BLE_GET_DEVICE, id),
|
|
1785
|
-
connect: (id: string) => invoke(EOneKeyBleMessageKeys.NOBLE_BLE_CONNECT, id),
|
|
1786
|
-
disconnect: (id: string) => invoke(EOneKeyBleMessageKeys.NOBLE_BLE_DISCONNECT, id),
|
|
1787
|
-
subscribe: (id: string) => invoke(EOneKeyBleMessageKeys.NOBLE_BLE_SUBSCRIBE, id),
|
|
1788
|
-
unsubscribe: (id: string) => invoke(EOneKeyBleMessageKeys.NOBLE_BLE_UNSUBSCRIBE, id),
|
|
1789
|
-
write: async (id: string, hex: string, writeOptions?: unknown) => {
|
|
1790
|
-
const result = await invoke(EOneKeyBleMessageKeys.NOBLE_BLE_WRITE, id, hex, writeOptions);
|
|
1791
|
-
// The native Windows write loop may emit a notification before the
|
|
1792
|
-
// renderer has started readFrame. Deliver the V2 Success after the
|
|
1793
|
-
// write IPC settles, matching the renderer-level V2-only tests.
|
|
1794
|
-
if (hex.startsWith('5a')) {
|
|
1795
|
-
responseSeq += 1;
|
|
1796
|
-
const response = ProtocolV2.encodeFrame(
|
|
1797
|
-
schemas,
|
|
1798
|
-
'Success',
|
|
1799
|
-
{ message: 'ok' },
|
|
1800
|
-
{ router: PROTOCOL_V2_CHANNEL_BLE_UART, seq: responseSeq }
|
|
1801
|
-
);
|
|
1802
|
-
setTimeout(() => {
|
|
1803
|
-
notificationListeners.forEach(listener => listener(id, bytesToHex(response)));
|
|
1804
|
-
}, 0);
|
|
1805
|
-
}
|
|
1806
|
-
return result;
|
|
1807
|
-
},
|
|
1808
|
-
release: (id: string, keepSession?: boolean) =>
|
|
1809
|
-
invoke(EOneKeyBleMessageKeys.NOBLE_BLE_RELEASE, id, keepSession),
|
|
1810
|
-
onNotification: (listener: (id: string, data: string) => void) => {
|
|
1811
|
-
notificationListeners.add(listener);
|
|
1812
|
-
return () => notificationListeners.delete(listener);
|
|
1813
|
-
},
|
|
1814
|
-
onDeviceDisconnected: (
|
|
1815
|
-
listener: (device: { id: string; name: string; reason?: EBleDisconnectReason }) => void
|
|
1816
|
-
) => {
|
|
1817
|
-
disconnectListeners.add(listener);
|
|
1818
|
-
return () => disconnectListeners.delete(listener);
|
|
1819
|
-
},
|
|
1820
|
-
onMtuChanged: (listener: (device: { id: string; mtu: number }) => void) => {
|
|
1821
|
-
mtuListeners.add(listener);
|
|
1822
|
-
return () => mtuListeners.delete(listener);
|
|
1823
|
-
},
|
|
1824
|
-
checkAvailability: () => invoke(EOneKeyBleMessageKeys.BLE_AVAILABILITY_CHECK),
|
|
1825
|
-
};
|
|
1826
|
-
|
|
1827
|
-
(global as any).window = {
|
|
1828
|
-
desktopApi: {
|
|
1829
|
-
nobleBle,
|
|
1830
|
-
},
|
|
1831
|
-
};
|
|
1832
|
-
|
|
1833
|
-
const logger = createLogger();
|
|
1834
|
-
const bleTransport = new TransportCtor();
|
|
1835
|
-
bleTransport.init(logger);
|
|
1836
|
-
bleTransport.configure(protocolV1Schema);
|
|
1837
|
-
bleTransport.configureProtocolV2(protocolV2Schema);
|
|
1838
|
-
|
|
1839
|
-
const driveTimersUntilSettled = async <T>(promise: Promise<T>): Promise<T> => {
|
|
1840
|
-
let settled = false;
|
|
1841
|
-
promise.then(
|
|
1842
|
-
() => {
|
|
1843
|
-
settled = true;
|
|
1844
|
-
},
|
|
1845
|
-
() => {
|
|
1846
|
-
settled = true;
|
|
1847
|
-
}
|
|
1848
|
-
);
|
|
1849
|
-
for (let step = 0; step < 60 && !settled; step += 1) {
|
|
1850
|
-
for (let flush = 0; flush < 5 && !settled; flush += 1) {
|
|
1851
|
-
// eslint-disable-next-line no-await-in-loop
|
|
1852
|
-
await new Promise<void>(resolve => {
|
|
1853
|
-
setImmediate(resolve);
|
|
1854
|
-
});
|
|
1855
|
-
}
|
|
1856
|
-
if (settled) break;
|
|
1857
|
-
jest.advanceTimersByTime(2000);
|
|
1858
|
-
}
|
|
1859
|
-
return promise;
|
|
1860
|
-
};
|
|
1861
|
-
|
|
1862
|
-
return {
|
|
1863
|
-
sdk,
|
|
1864
|
-
logger,
|
|
1865
|
-
webContents,
|
|
1866
|
-
bleTransport,
|
|
1867
|
-
nobleBle,
|
|
1868
|
-
peripheral,
|
|
1869
|
-
writeCharacteristic,
|
|
1870
|
-
driveTimersUntilSettled,
|
|
1871
|
-
};
|
|
1872
|
-
};
|
|
1873
|
-
|
|
1874
|
-
test('detects a V2-only device on Windows through real attemptWindowsWriteUntilPaired V1 silence (first acquire and wasBonded reconnect)', async () => {
|
|
1875
|
-
const {
|
|
1876
|
-
sdk,
|
|
1877
|
-
bleTransport,
|
|
1878
|
-
nobleBle,
|
|
1879
|
-
peripheral,
|
|
1880
|
-
writeCharacteristic,
|
|
1881
|
-
driveTimersUntilSettled,
|
|
1882
|
-
} = await setupWindowsIntegration();
|
|
1883
|
-
|
|
1884
|
-
try {
|
|
1885
|
-
await driveTimersUntilSettled(nobleBle.enumerate());
|
|
1886
|
-
|
|
1887
|
-
// First acquire, no protocol hint: V1 write loop hits 15 silent attempts (715
|
|
1888
|
-
// pairing_no_response), keeps GATT link, and falls through to V2 Ping.
|
|
1889
|
-
await expect(
|
|
1890
|
-
driveTimersUntilSettled(bleTransport.acquire({ uuid: peripheral.id }))
|
|
1891
|
-
).resolves.toMatchObject({
|
|
1892
|
-
uuid: peripheral.id,
|
|
1893
|
-
protocolType: 'V2',
|
|
1894
|
-
});
|
|
1895
|
-
|
|
1896
|
-
const firstAcquireFirstBytes = writeCharacteristic.write.mock.calls.map(([buf]) => buf[0]);
|
|
1897
|
-
expect(firstAcquireFirstBytes.slice(0, 15).every(b => b === 0x3f)).toBe(true);
|
|
1898
|
-
expect(firstAcquireFirstBytes[15]).toBe(0x5a);
|
|
1899
|
-
expect(firstAcquireFirstBytes).toHaveLength(16);
|
|
1900
|
-
// Cold setup force-reconnects once. V1 silence must not abort the GATT link
|
|
1901
|
-
// before the V2 probe runs.
|
|
1902
|
-
expect(peripheral.disconnect).toHaveBeenCalledTimes(1);
|
|
1903
|
-
|
|
1904
|
-
// Disconnect so pairedDevices clears while bondedDevices retains peripheral.id.
|
|
1905
|
-
await bleTransport.disconnect(peripheral.id);
|
|
1906
|
-
writeCharacteristic.write.mockClear();
|
|
1907
|
-
peripheral.disconnect.mockClear();
|
|
1908
|
-
|
|
1909
|
-
// Clear renderer hint so the next acquire probes V1 first again with wasBonded=true.
|
|
1910
|
-
(bleTransport as any).deviceProtocolHints.delete(peripheral.id);
|
|
1911
|
-
|
|
1912
|
-
// wasBonded + silence on V1 produces BleBondInvalid (724) with reason: 'stale_bond'
|
|
1913
|
-
// and no nativeErrorMessage; healthy V2 device still falls through to V2.
|
|
1914
|
-
await expect(
|
|
1915
|
-
driveTimersUntilSettled(bleTransport.acquire({ uuid: peripheral.id }))
|
|
1916
|
-
).resolves.toMatchObject({
|
|
1917
|
-
uuid: peripheral.id,
|
|
1918
|
-
protocolType: 'V2',
|
|
1919
|
-
});
|
|
1920
|
-
|
|
1921
|
-
const secondAcquireFirstBytes = writeCharacteristic.write.mock.calls.map(([buf]) => buf[0]);
|
|
1922
|
-
expect(secondAcquireFirstBytes.slice(0, 15).every(b => b === 0x3f)).toBe(true);
|
|
1923
|
-
expect(secondAcquireFirstBytes[15]).toBe(0x5a);
|
|
1924
|
-
expect(secondAcquireFirstBytes).toHaveLength(16);
|
|
1925
|
-
// After mockClear, only the next cold-setup force-reconnect should run.
|
|
1926
|
-
expect(peripheral.disconnect).toHaveBeenCalledTimes(1);
|
|
1927
|
-
|
|
1928
|
-
await bleTransport.disconnect(peripheral.id);
|
|
1929
|
-
} finally {
|
|
1930
|
-
await sdk.disposeNobleBleSupport();
|
|
1931
|
-
}
|
|
1932
|
-
});
|
|
1933
|
-
|
|
1934
|
-
test('keeps confirmed Windows native status: 3 terminal during undeclared V1 probe', async () => {
|
|
1935
|
-
const {
|
|
1936
|
-
sdk,
|
|
1937
|
-
bleTransport,
|
|
1938
|
-
nobleBle,
|
|
1939
|
-
peripheral,
|
|
1940
|
-
writeCharacteristic,
|
|
1941
|
-
driveTimersUntilSettled,
|
|
1942
|
-
} = await setupWindowsIntegration({
|
|
1943
|
-
writeErrorOnV1: new Error('GATT operation failed, status: 3'),
|
|
1944
|
-
});
|
|
1945
|
-
|
|
1946
|
-
try {
|
|
1947
|
-
await driveTimersUntilSettled(nobleBle.enumerate());
|
|
1948
|
-
|
|
1949
|
-
await expect(
|
|
1950
|
-
driveTimersUntilSettled(bleTransport.acquire({ uuid: peripheral.id }))
|
|
1951
|
-
).rejects.toMatchObject({
|
|
1952
|
-
errorCode: HardwareErrorCode.BleDeviceBondError,
|
|
1953
|
-
params: {
|
|
1954
|
-
phase: 'pairing',
|
|
1955
|
-
platform: 'win32',
|
|
1956
|
-
stage: 'write',
|
|
1957
|
-
reason: 'pairing_failed',
|
|
1958
|
-
nativeErrorMessage: 'GATT operation failed, status: 3',
|
|
1959
|
-
},
|
|
1960
|
-
});
|
|
1961
|
-
|
|
1962
|
-
expect(writeCharacteristic.write).toHaveBeenCalledTimes(1);
|
|
1963
|
-
expect(peripheral.disconnect).toHaveBeenCalled();
|
|
1964
|
-
} finally {
|
|
1965
|
-
await sdk.disposeNobleBleSupport();
|
|
1966
|
-
}
|
|
1967
|
-
});
|
|
1968
|
-
});
|
|
@@ -16,6 +16,37 @@ 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
|
+
|
|
19
50
|
function buildAcquirableTransport(path = 'pro-webusb') {
|
|
20
51
|
const webusb = new WebUsbTransport() as any;
|
|
21
52
|
webusb.Log = { debug: jest.fn() };
|
|
@@ -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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S;YAaT,wBAAwB;IAMtC,OAAO,CAAC,uBAAuB;IAc/B,OAAO,CAAC,qBAAqB;IAU7B,OAAO,CAAC,oBAAoB;IAqB5B,OAAO,CAAC,kBAAkB;IA+B1B,OAAO,CAAC,iCAAiC;IAazC,OAAO,CAAC,4BAA4B;IAkBpC,OAAO,CAAC,uBAAuB;IAS/B,OAAO,CAAC,sBAAsB;IAU9B,OAAO,CAAC,qBAAqB;IAI7B,OAAO,CAAC,sBAAsB;YAShB,mBAAmB;IAiBjC,OAAO,CAAC,4BAA4B;IAqB9B,IAAI,CACR,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,oBAAoB;IAuB1B,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;YAatD,cAAc;YAqFd,cAAc;IA+B5B,OAAO,CAAC,uBAAuB;IAyC/B,OAAO,CAAC,6BAA6B;IA4CrC,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,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.parseTransportConfigure> | 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.parseTransportConfigure> | undefined;
|
|
28
28
|
private protocolV2SchemaSource;
|
|
29
29
|
/** Per-path protocol type detected by active wire-level probe. */
|
|
30
30
|
private deviceProtocol;
|
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 { parseTransportConfigure: parseTransportConfigure$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 = parseTransportConfigure$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 = parseTransportConfigure$1(signedData);
|
|
336
336
|
this.protocolV2SchemaSource = schemaSource;
|
|
337
337
|
if (!hadProtocolV2Schema)
|
|
338
338
|
return;
|
|
@@ -1428,8 +1428,7 @@ class WebUsbTransport extends transport.ProtocolV2UsbTransportBase {
|
|
|
1428
1428
|
}
|
|
1429
1429
|
}
|
|
1430
1430
|
decoded.reset();
|
|
1431
|
-
|
|
1432
|
-
return Buffer.from(result).toString('hex');
|
|
1431
|
+
return decoded.toString('hex');
|
|
1433
1432
|
});
|
|
1434
1433
|
}
|
|
1435
1434
|
release(path) {
|
|
@@ -1505,7 +1504,7 @@ function resolveBlePacketCapacity(mtu, maximumPacketCapacity, fallbackPacketCapa
|
|
|
1505
1504
|
return Math.min(maximumPacketCapacity, Math.floor(mtu) - BLE_ATT_HEADER_BYTES);
|
|
1506
1505
|
}
|
|
1507
1506
|
|
|
1508
|
-
const {
|
|
1507
|
+
const { parseTransportConfigure, ProtocolV1, check } = transport__default["default"];
|
|
1509
1508
|
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 } : {})));
|
|
1510
1509
|
const invokeNobleBle = (request) => __awaiter(void 0, void 0, void 0, function* () {
|
|
1511
1510
|
const response = yield request;
|
|
@@ -1525,41 +1524,6 @@ const BLE_PACKET_SIZE_MAXIMUM = 244;
|
|
|
1525
1524
|
const BLE_WRITE_DELAY_MS = 5;
|
|
1526
1525
|
const PROTOCOL_PROBE_TIMEOUT_MS = 3000;
|
|
1527
1526
|
const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
|
|
1528
|
-
const isSilentWindowsPairingNoResponse = (error) => {
|
|
1529
|
-
if (!error || typeof error !== 'object') {
|
|
1530
|
-
return false;
|
|
1531
|
-
}
|
|
1532
|
-
const candidate = error;
|
|
1533
|
-
const code = candidate.errorCode;
|
|
1534
|
-
if (code !== hdShared.HardwareErrorCode.BleDeviceBondError && code !== hdShared.HardwareErrorCode.BleBondInvalid) {
|
|
1535
|
-
return false;
|
|
1536
|
-
}
|
|
1537
|
-
const { params } = candidate;
|
|
1538
|
-
if (!params || typeof params !== 'object') {
|
|
1539
|
-
return false;
|
|
1540
|
-
}
|
|
1541
|
-
const { phase, platform, stage, reason, nativeErrorMessage } = params;
|
|
1542
|
-
if (phase !== 'pairing' || platform !== 'win32' || stage !== 'write') {
|
|
1543
|
-
return false;
|
|
1544
|
-
}
|
|
1545
|
-
if (typeof nativeErrorMessage === 'string' && nativeErrorMessage.length > 0) {
|
|
1546
|
-
return false;
|
|
1547
|
-
}
|
|
1548
|
-
return reason === 'pairing_no_response' || reason === 'stale_bond';
|
|
1549
|
-
};
|
|
1550
|
-
const shouldRethrowProtocolProbeError = (error, isLastProbe = false) => {
|
|
1551
|
-
if (transport.isProtocolV2LinkDisabledError(error)) {
|
|
1552
|
-
return true;
|
|
1553
|
-
}
|
|
1554
|
-
if (isSilentWindowsPairingNoResponse(error)) {
|
|
1555
|
-
return isLastProbe;
|
|
1556
|
-
}
|
|
1557
|
-
const code = error === null || error === void 0 ? void 0 : error.errorCode;
|
|
1558
|
-
return (code === hdShared.HardwareErrorCode.BleDeviceBondError ||
|
|
1559
|
-
code === hdShared.HardwareErrorCode.BleDeviceBondedCanceled ||
|
|
1560
|
-
code === hdShared.HardwareErrorCode.BlePeerRemovedPairingInformation ||
|
|
1561
|
-
code === hdShared.HardwareErrorCode.BleBondInvalid);
|
|
1562
|
-
};
|
|
1563
1527
|
class ElectronBleTransport {
|
|
1564
1528
|
constructor() {
|
|
1565
1529
|
this.name = 'ElectronBleTransport';
|
|
@@ -1700,7 +1664,7 @@ class ElectronBleTransport {
|
|
|
1700
1664
|
});
|
|
1701
1665
|
}
|
|
1702
1666
|
configure(signedData) {
|
|
1703
|
-
this._messages =
|
|
1667
|
+
this._messages = parseTransportConfigure(signedData);
|
|
1704
1668
|
this.configured = true;
|
|
1705
1669
|
}
|
|
1706
1670
|
configureProtocolV2(signedData) {
|
|
@@ -1709,7 +1673,7 @@ class ElectronBleTransport {
|
|
|
1709
1673
|
return;
|
|
1710
1674
|
}
|
|
1711
1675
|
const isReconfiguration = this.protocolV2SchemaConfiguration !== undefined;
|
|
1712
|
-
this._messagesV2 =
|
|
1676
|
+
this._messagesV2 = parseTransportConfigure(signedData);
|
|
1713
1677
|
this.protocolV2SchemaConfiguration = configuration;
|
|
1714
1678
|
if (isReconfiguration) {
|
|
1715
1679
|
this.protocolV2Links
|
|
@@ -1953,13 +1917,10 @@ class ElectronBleTransport {
|
|
|
1953
1917
|
const probeOrder = protocolHint === 'V2' || this.deviceProtocol.get(uuid) === 'V2' ? ['V2', 'V1'] : ['V1', 'V2'];
|
|
1954
1918
|
for (let i = 0; i < probeOrder.length; i += 1) {
|
|
1955
1919
|
const protocol = probeOrder[i];
|
|
1956
|
-
const isLastProbe = i === probeOrder.length - 1;
|
|
1957
1920
|
if (i > 0) {
|
|
1958
1921
|
yield this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
|
|
1959
1922
|
}
|
|
1960
|
-
const detected = protocol === 'V1'
|
|
1961
|
-
? yield this.probeProtocolV1(uuid, isLastProbe)
|
|
1962
|
-
: yield this.probeProtocolV2(uuid, undefined, isLastProbe);
|
|
1923
|
+
const detected = protocol === 'V1' ? yield this.probeProtocolV1(uuid) : yield this.probeProtocolV2(uuid);
|
|
1963
1924
|
if (detected) {
|
|
1964
1925
|
this.deviceProtocol.set(uuid, protocol);
|
|
1965
1926
|
(_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
@@ -2003,7 +1964,7 @@ class ElectronBleTransport {
|
|
|
2003
1964
|
}
|
|
2004
1965
|
});
|
|
2005
1966
|
}
|
|
2006
|
-
probeProtocolV1(uuid
|
|
1967
|
+
probeProtocolV1(uuid) {
|
|
2007
1968
|
var _a;
|
|
2008
1969
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2009
1970
|
if (!this._messages) {
|
|
@@ -2017,14 +1978,14 @@ class ElectronBleTransport {
|
|
|
2017
1978
|
catch (error) {
|
|
2018
1979
|
this.clearProbeProtocol(uuid, 'V1');
|
|
2019
1980
|
(_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('[Electron BLE] Protocol V1 GetFeatures probe failed:', error);
|
|
2020
|
-
if (
|
|
1981
|
+
if (transport.isProtocolV2LinkDisabledError(error)) {
|
|
2021
1982
|
throw error;
|
|
2022
1983
|
}
|
|
2023
1984
|
return false;
|
|
2024
1985
|
}
|
|
2025
1986
|
});
|
|
2026
1987
|
}
|
|
2027
|
-
probeProtocolV2(uuid, expectedProtocol
|
|
1988
|
+
probeProtocolV2(uuid, expectedProtocol) {
|
|
2028
1989
|
var _a;
|
|
2029
1990
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2030
1991
|
if (!this._messages || !this._messagesV2) {
|
|
@@ -2032,28 +1993,22 @@ class ElectronBleTransport {
|
|
|
2032
1993
|
}
|
|
2033
1994
|
this.deviceProtocol.set(uuid, 'V2');
|
|
2034
1995
|
(_a = this.v2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
if (!detected) {
|
|
2049
|
-
this.clearProbeProtocol(uuid, 'V2');
|
|
2050
|
-
}
|
|
2051
|
-
return detected;
|
|
2052
|
-
}
|
|
2053
|
-
catch (error) {
|
|
1996
|
+
const detected = yield transport.probeProtocolV2({
|
|
1997
|
+
call: (name, data, options) => this.callProtocolV2(uuid, name, data, options),
|
|
1998
|
+
timeoutMs: PROTOCOL_V2_PROBE_TIMEOUT_MS,
|
|
1999
|
+
logger: this.Log,
|
|
2000
|
+
logPrefix: 'ProtocolV2 BLE',
|
|
2001
|
+
onProbeFailed: () => {
|
|
2002
|
+
var _a;
|
|
2003
|
+
(_a = this.v2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
|
|
2004
|
+
this.resetProtocolV2Frames(uuid);
|
|
2005
|
+
},
|
|
2006
|
+
shouldRethrow: error => expectedProtocol === 'V2' || transport.isProtocolV2LinkDisabledError(error),
|
|
2007
|
+
});
|
|
2008
|
+
if (!detected) {
|
|
2054
2009
|
this.clearProbeProtocol(uuid, 'V2');
|
|
2055
|
-
throw error;
|
|
2056
2010
|
}
|
|
2011
|
+
return detected;
|
|
2057
2012
|
});
|
|
2058
2013
|
}
|
|
2059
2014
|
writeOnce(uuid, hexData) {
|
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.parseTransportConfigure> | undefined;
|
|
22
|
+
messagesV2: ReturnType<typeof transport.parseTransportConfigure> | 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;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I,CACR,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,oBAAoB;YA2BlB,cAAc;YAkCd,cAAc;IAkBtB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;IA+C5C,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.5",
|
|
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.11-alpha.
|
|
25
|
-
"@onekeyfe/hd-transport": "1.2.11-alpha.
|
|
24
|
+
"@onekeyfe/hd-shared": "1.2.11-alpha.5",
|
|
25
|
+
"@onekeyfe/hd-transport": "1.2.11-alpha.5"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@onekeyfe/hd-transport-electron": "1.2.11-alpha.
|
|
28
|
+
"@onekeyfe/hd-transport-electron": "1.2.11-alpha.5",
|
|
29
29
|
"@types/w3c-web-usb": "^1.0.6",
|
|
30
30
|
"@types/web-bluetooth": "^0.0.17"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "887771a9cc9f75d8230db39b9aa77adee81c3721"
|
|
33
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 { 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
|
|
|
@@ -369,7 +322,7 @@ export default class ElectronBleTransport {
|
|
|
369
322
|
}
|
|
370
323
|
|
|
371
324
|
configure(signedData: any) {
|
|
372
|
-
this._messages =
|
|
325
|
+
this._messages = parseTransportConfigure(signedData);
|
|
373
326
|
this.configured = true;
|
|
374
327
|
}
|
|
375
328
|
|
|
@@ -380,7 +333,7 @@ export default class ElectronBleTransport {
|
|
|
380
333
|
}
|
|
381
334
|
|
|
382
335
|
const isReconfiguration = this.protocolV2SchemaConfiguration !== undefined;
|
|
383
|
-
this._messagesV2 =
|
|
336
|
+
this._messagesV2 = parseTransportConfigure(signedData);
|
|
384
337
|
this.protocolV2SchemaConfiguration = configuration;
|
|
385
338
|
if (isReconfiguration) {
|
|
386
339
|
this.protocolV2Links
|
|
@@ -664,16 +617,13 @@ export default class ElectronBleTransport {
|
|
|
664
617
|
|
|
665
618
|
for (let i = 0; i < probeOrder.length; i += 1) {
|
|
666
619
|
const protocol = probeOrder[i];
|
|
667
|
-
const isLastProbe = i === probeOrder.length - 1;
|
|
668
620
|
if (i > 0) {
|
|
669
621
|
// Keep the physical BLE link while switching probes. Reconnecting here can
|
|
670
622
|
// summon a second OS pairing prompt for a device that is still onboarding.
|
|
671
623
|
await this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
|
|
672
624
|
}
|
|
673
625
|
const detected =
|
|
674
|
-
protocol === 'V1'
|
|
675
|
-
? await this.probeProtocolV1(uuid, isLastProbe)
|
|
676
|
-
: await this.probeProtocolV2(uuid, undefined, isLastProbe);
|
|
626
|
+
protocol === 'V1' ? await this.probeProtocolV1(uuid) : await this.probeProtocolV2(uuid);
|
|
677
627
|
if (detected) {
|
|
678
628
|
this.deviceProtocol.set(uuid, protocol);
|
|
679
629
|
this.Log?.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
@@ -725,7 +675,7 @@ export default class ElectronBleTransport {
|
|
|
725
675
|
}
|
|
726
676
|
}
|
|
727
677
|
|
|
728
|
-
private async probeProtocolV1(uuid: string
|
|
678
|
+
private async probeProtocolV1(uuid: string) {
|
|
729
679
|
if (!this._messages) {
|
|
730
680
|
return false;
|
|
731
681
|
}
|
|
@@ -739,46 +689,36 @@ export default class ElectronBleTransport {
|
|
|
739
689
|
} catch (error) {
|
|
740
690
|
this.clearProbeProtocol(uuid, 'V1');
|
|
741
691
|
this.Log?.debug('[Electron BLE] Protocol V1 GetFeatures probe failed:', error);
|
|
742
|
-
if (
|
|
692
|
+
if (isProtocolV2LinkDisabledError(error)) {
|
|
743
693
|
throw error;
|
|
744
694
|
}
|
|
745
695
|
return false;
|
|
746
696
|
}
|
|
747
697
|
}
|
|
748
698
|
|
|
749
|
-
private async probeProtocolV2(
|
|
750
|
-
uuid: string,
|
|
751
|
-
expectedProtocol?: ProtocolType,
|
|
752
|
-
isLastProbe = false
|
|
753
|
-
) {
|
|
699
|
+
private async probeProtocolV2(uuid: string, expectedProtocol?: ProtocolType) {
|
|
754
700
|
if (!this._messages || !this._messagesV2) {
|
|
755
701
|
return false;
|
|
756
702
|
}
|
|
757
703
|
|
|
758
704
|
this.deviceProtocol.set(uuid, 'V2');
|
|
759
705
|
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) {
|
|
706
|
+
const detected = await probeProtocolV2Helper({
|
|
707
|
+
call: (name, data, options) => this.callProtocolV2(uuid, name, data, options),
|
|
708
|
+
timeoutMs: PROTOCOL_V2_PROBE_TIMEOUT_MS,
|
|
709
|
+
logger: this.Log,
|
|
710
|
+
logPrefix: 'ProtocolV2 BLE',
|
|
711
|
+
onProbeFailed: () => {
|
|
712
|
+
this.v2Assemblers.get(uuid)?.reset();
|
|
713
|
+
this.resetProtocolV2Frames(uuid);
|
|
714
|
+
},
|
|
715
|
+
// A declared V2 protocol needs no fallback; preserve the actual link failure.
|
|
716
|
+
shouldRethrow: error => expectedProtocol === 'V2' || isProtocolV2LinkDisabledError(error),
|
|
717
|
+
});
|
|
718
|
+
if (!detected) {
|
|
779
719
|
this.clearProbeProtocol(uuid, 'V2');
|
|
780
|
-
throw error;
|
|
781
720
|
}
|
|
721
|
+
return detected;
|
|
782
722
|
}
|
|
783
723
|
|
|
784
724
|
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
|
|
|
@@ -1828,8 +1828,7 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
1828
1828
|
}
|
|
1829
1829
|
}
|
|
1830
1830
|
decoded.reset();
|
|
1831
|
-
|
|
1832
|
-
return Buffer.from(result as unknown as ArrayBuffer).toString('hex');
|
|
1831
|
+
return decoded.toString('hex');
|
|
1833
1832
|
}
|
|
1834
1833
|
|
|
1835
1834
|
/**
|