@onekeyfe/hd-transport-web-device 1.2.11-alpha.5 → 1.2.11-alpha.8
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 +594 -1
- package/__tests__/webusb-identity-probe.test.ts +6 -2
- package/__tests__/webusb-protocol-cache.test.ts +0 -31
- package/__tests__/webusb-protocol-v2-timeout.test.ts +3 -3
- package/dist/electron-ble-transport.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +72 -27
- 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 +83 -23
- package/src/webusb.ts +10 -8
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import transport, { PROTOCOL_V2_CHANNEL_BLE_UART, bytesToHex } from '@onekeyfe/hd-transport';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
EBleDisconnectReason,
|
|
4
|
+
EOneKeyBleMessageKeys,
|
|
5
|
+
ERRORS,
|
|
6
|
+
HardwareErrorCode,
|
|
7
|
+
createDeferred,
|
|
8
|
+
} from '@onekeyfe/hd-shared';
|
|
3
9
|
import EventEmitter from 'events';
|
|
4
10
|
|
|
5
11
|
import ElectronBleTransport from '../src/electron-ble-transport';
|
|
@@ -837,6 +843,42 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
837
843
|
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
838
844
|
});
|
|
839
845
|
|
|
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
|
+
|
|
840
882
|
test('keeps a first expected Protocol V2 probe miss retryable', async () => {
|
|
841
883
|
const device = { id: 'first-v2-id', name: 'OneKey Pro 2' };
|
|
842
884
|
const nobleBle = createNobleBle(device);
|
|
@@ -1030,6 +1072,221 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
1030
1072
|
expect(nobleBle.disconnect).toHaveBeenCalledWith(device.id);
|
|
1031
1073
|
});
|
|
1032
1074
|
|
|
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
|
+
|
|
1033
1290
|
test('does not take a Protocol V2 hint from the BLE name', async () => {
|
|
1034
1291
|
const device = { id: 'named-pro2-id', name: 'OneKey Pro 2' };
|
|
1035
1292
|
const nobleBle = createNobleBle(device);
|
|
@@ -1373,3 +1630,339 @@ describe('ElectronBleTransport protocol detection', () => {
|
|
|
1373
1630
|
}
|
|
1374
1631
|
});
|
|
1375
1632
|
});
|
|
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
|
+
});
|
|
@@ -911,7 +911,7 @@ describe('WebUSB identity probe ownership', () => {
|
|
|
911
911
|
);
|
|
912
912
|
|
|
913
913
|
test.each([false, true])(
|
|
914
|
-
'
|
|
914
|
+
'keeps repeated V1 probes read-only during on-device setup (%s)',
|
|
915
915
|
async identityProbe => {
|
|
916
916
|
const probe = new WebUsbTransport(identityProbe);
|
|
917
917
|
const internals = probe as unknown as {
|
|
@@ -929,7 +929,11 @@ describe('WebUSB identity probe ownership', () => {
|
|
|
929
929
|
.spyOn(internals, 'callProtocolV1')
|
|
930
930
|
.mockResolvedValue({ type: 'Features', message: {} });
|
|
931
931
|
await expect(internals.probeProtocolV1('USB_PATH')).resolves.toBe(true);
|
|
932
|
-
expect(
|
|
932
|
+
await expect(internals.probeProtocolV1('USB_PATH')).resolves.toBe(true);
|
|
933
|
+
expect(call).toHaveBeenCalledTimes(2);
|
|
934
|
+
expect(call.mock.calls.map(([, name]) => name)).toEqual(['GetFeatures', 'GetFeatures']);
|
|
935
|
+
call.mockResolvedValue({ type: 'Failure', message: {} });
|
|
936
|
+
await expect(internals.probeProtocolV1('USB_PATH')).resolves.toBe(false);
|
|
933
937
|
}
|
|
934
938
|
);
|
|
935
939
|
});
|
|
@@ -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() };
|
|
@@ -121,17 +121,17 @@ describe('WebUsbTransport Protocol V2 timeout recovery', () => {
|
|
|
121
121
|
expect(webusb.deviceProtocol.has(path)).toBe(false);
|
|
122
122
|
});
|
|
123
123
|
|
|
124
|
-
test('
|
|
124
|
+
test('keeps the legacy WebUSB read-only probe timeout at five seconds', async () => {
|
|
125
125
|
const webusb = new WebUsbTransport() as any;
|
|
126
126
|
const path = 'pro-webusb';
|
|
127
127
|
webusb.messages = {};
|
|
128
|
-
webusb.callProtocolV1 = jest.fn().mockResolvedValue({});
|
|
128
|
+
webusb.callProtocolV1 = jest.fn().mockResolvedValue({ type: 'Features', message: {} });
|
|
129
129
|
|
|
130
130
|
await expect(webusb.probeProtocolV1(path)).resolves.toBe(true);
|
|
131
131
|
|
|
132
132
|
expect(webusb.callProtocolV1).toHaveBeenCalledWith(
|
|
133
133
|
path,
|
|
134
|
-
'
|
|
134
|
+
'GetFeatures',
|
|
135
135
|
{},
|
|
136
136
|
{
|
|
137
137
|
timeoutMs: 5000,
|
|
@@ -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,QA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C,2BAA2B;IAMnC,OAAO,CAAC,4BAA4B;IAIpC,OAAO,CAAC,kBAAkB;YAMZ,cAAc;IA0D5B,OAAO,CAAC,8BAA8B;YAgBxB,iCAAiC;YAwBjC,eAAe;YAqBf,eAAe;YAmCf,SAAS;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.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;
|
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;
|
|
@@ -1315,10 +1315,10 @@ class WebUsbTransport extends transport.ProtocolV2UsbTransportBase {
|
|
|
1315
1315
|
return false;
|
|
1316
1316
|
}
|
|
1317
1317
|
try {
|
|
1318
|
-
const response = yield this.callProtocolV1(path,
|
|
1318
|
+
const response = yield this.callProtocolV1(path, 'GetFeatures', {}, {
|
|
1319
1319
|
timeoutMs: PROTOCOL_V1_PROBE_TIMEOUT,
|
|
1320
1320
|
});
|
|
1321
|
-
return
|
|
1321
|
+
return response.type === 'Features';
|
|
1322
1322
|
}
|
|
1323
1323
|
catch (error) {
|
|
1324
1324
|
if (this.isWebUsbIoHardwareError(error))
|
|
@@ -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;
|
|
@@ -1524,6 +1525,41 @@ const BLE_PACKET_SIZE_MAXIMUM = 244;
|
|
|
1524
1525
|
const BLE_WRITE_DELAY_MS = 5;
|
|
1525
1526
|
const PROTOCOL_PROBE_TIMEOUT_MS = 3000;
|
|
1526
1527
|
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
|
+
};
|
|
1527
1563
|
class ElectronBleTransport {
|
|
1528
1564
|
constructor() {
|
|
1529
1565
|
this.name = 'ElectronBleTransport';
|
|
@@ -1664,7 +1700,7 @@ class ElectronBleTransport {
|
|
|
1664
1700
|
});
|
|
1665
1701
|
}
|
|
1666
1702
|
configure(signedData) {
|
|
1667
|
-
this._messages =
|
|
1703
|
+
this._messages = parseConfigure(signedData);
|
|
1668
1704
|
this.configured = true;
|
|
1669
1705
|
}
|
|
1670
1706
|
configureProtocolV2(signedData) {
|
|
@@ -1673,7 +1709,7 @@ class ElectronBleTransport {
|
|
|
1673
1709
|
return;
|
|
1674
1710
|
}
|
|
1675
1711
|
const isReconfiguration = this.protocolV2SchemaConfiguration !== undefined;
|
|
1676
|
-
this._messagesV2 =
|
|
1712
|
+
this._messagesV2 = parseConfigure(signedData);
|
|
1677
1713
|
this.protocolV2SchemaConfiguration = configuration;
|
|
1678
1714
|
if (isReconfiguration) {
|
|
1679
1715
|
this.protocolV2Links
|
|
@@ -1917,10 +1953,13 @@ class ElectronBleTransport {
|
|
|
1917
1953
|
const probeOrder = protocolHint === 'V2' || this.deviceProtocol.get(uuid) === 'V2' ? ['V2', 'V1'] : ['V1', 'V2'];
|
|
1918
1954
|
for (let i = 0; i < probeOrder.length; i += 1) {
|
|
1919
1955
|
const protocol = probeOrder[i];
|
|
1956
|
+
const isLastProbe = i === probeOrder.length - 1;
|
|
1920
1957
|
if (i > 0) {
|
|
1921
1958
|
yield this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
|
|
1922
1959
|
}
|
|
1923
|
-
const detected = protocol === 'V1'
|
|
1960
|
+
const detected = protocol === 'V1'
|
|
1961
|
+
? yield this.probeProtocolV1(uuid, isLastProbe)
|
|
1962
|
+
: yield this.probeProtocolV2(uuid, undefined, isLastProbe);
|
|
1924
1963
|
if (detected) {
|
|
1925
1964
|
this.deviceProtocol.set(uuid, protocol);
|
|
1926
1965
|
(_c = this.Log) === null || _c === void 0 ? void 0 : _c.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
@@ -1964,7 +2003,7 @@ class ElectronBleTransport {
|
|
|
1964
2003
|
}
|
|
1965
2004
|
});
|
|
1966
2005
|
}
|
|
1967
|
-
probeProtocolV1(uuid) {
|
|
2006
|
+
probeProtocolV1(uuid, isLastProbe = false) {
|
|
1968
2007
|
var _a;
|
|
1969
2008
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1970
2009
|
if (!this._messages) {
|
|
@@ -1978,14 +2017,14 @@ class ElectronBleTransport {
|
|
|
1978
2017
|
catch (error) {
|
|
1979
2018
|
this.clearProbeProtocol(uuid, 'V1');
|
|
1980
2019
|
(_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug('[Electron BLE] Protocol V1 GetFeatures probe failed:', error);
|
|
1981
|
-
if (
|
|
2020
|
+
if (shouldRethrowProtocolProbeError(error, isLastProbe)) {
|
|
1982
2021
|
throw error;
|
|
1983
2022
|
}
|
|
1984
2023
|
return false;
|
|
1985
2024
|
}
|
|
1986
2025
|
});
|
|
1987
2026
|
}
|
|
1988
|
-
probeProtocolV2(uuid, expectedProtocol) {
|
|
2027
|
+
probeProtocolV2(uuid, expectedProtocol, isLastProbe = false) {
|
|
1989
2028
|
var _a;
|
|
1990
2029
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1991
2030
|
if (!this._messages || !this._messagesV2) {
|
|
@@ -1993,22 +2032,28 @@ class ElectronBleTransport {
|
|
|
1993
2032
|
}
|
|
1994
2033
|
this.deviceProtocol.set(uuid, 'V2');
|
|
1995
2034
|
(_a = this.v2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2035
|
+
try {
|
|
2036
|
+
const detected = yield transport.probeProtocolV2({
|
|
2037
|
+
call: (name, data, options) => this.callProtocolV2(uuid, name, data, options),
|
|
2038
|
+
timeoutMs: PROTOCOL_V2_PROBE_TIMEOUT_MS,
|
|
2039
|
+
logger: this.Log,
|
|
2040
|
+
logPrefix: 'ProtocolV2 BLE',
|
|
2041
|
+
onProbeFailed: () => {
|
|
2042
|
+
var _a;
|
|
2043
|
+
(_a = this.v2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
|
|
2044
|
+
this.resetProtocolV2Frames(uuid);
|
|
2045
|
+
},
|
|
2046
|
+
shouldRethrow: error => expectedProtocol === 'V2' || shouldRethrowProtocolProbeError(error, isLastProbe),
|
|
2047
|
+
});
|
|
2048
|
+
if (!detected) {
|
|
2049
|
+
this.clearProbeProtocol(uuid, 'V2');
|
|
2050
|
+
}
|
|
2051
|
+
return detected;
|
|
2052
|
+
}
|
|
2053
|
+
catch (error) {
|
|
2009
2054
|
this.clearProbeProtocol(uuid, 'V2');
|
|
2055
|
+
throw error;
|
|
2010
2056
|
}
|
|
2011
|
-
return detected;
|
|
2012
2057
|
});
|
|
2013
2058
|
}
|
|
2014
2059
|
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.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,MA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M;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.8",
|
|
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.8",
|
|
25
|
+
"@onekeyfe/hd-transport": "1.2.11-alpha.8"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@onekeyfe/hd-transport-electron": "1.2.11-alpha.
|
|
28
|
+
"@onekeyfe/hd-transport-electron": "1.2.11-alpha.8",
|
|
29
29
|
"@types/w3c-web-usb": "^1.0.6",
|
|
30
30
|
"@types/web-bluetooth": "^0.0.17"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "f2f0e4af13efbb09f4b421171142f9144124a1c2"
|
|
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 { parseConfigure, ProtocolV1, check } = transport;
|
|
38
38
|
|
|
39
39
|
declare global {
|
|
40
40
|
interface Window {
|
|
@@ -91,6 +91,53 @@ 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
|
+
|
|
94
141
|
/**
|
|
95
142
|
* Desktop Electron BLE transport with automatic Protocol V1/V2 detection.
|
|
96
143
|
*
|
|
@@ -98,9 +145,9 @@ const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
|
|
|
98
145
|
* after a Protocol V1 GetFeatures timeout by probing Protocol V2 Ping.
|
|
99
146
|
*/
|
|
100
147
|
export default class ElectronBleTransport {
|
|
101
|
-
private _messages: ReturnType<typeof transport.
|
|
148
|
+
private _messages: ReturnType<typeof transport.parseConfigure> | undefined;
|
|
102
149
|
|
|
103
|
-
private _messagesV2: ReturnType<typeof transport.
|
|
150
|
+
private _messagesV2: ReturnType<typeof transport.parseConfigure> | undefined;
|
|
104
151
|
|
|
105
152
|
private protocolV2SchemaConfiguration: string | undefined;
|
|
106
153
|
|
|
@@ -322,7 +369,7 @@ export default class ElectronBleTransport {
|
|
|
322
369
|
}
|
|
323
370
|
|
|
324
371
|
configure(signedData: any) {
|
|
325
|
-
this._messages =
|
|
372
|
+
this._messages = parseConfigure(signedData);
|
|
326
373
|
this.configured = true;
|
|
327
374
|
}
|
|
328
375
|
|
|
@@ -333,7 +380,7 @@ export default class ElectronBleTransport {
|
|
|
333
380
|
}
|
|
334
381
|
|
|
335
382
|
const isReconfiguration = this.protocolV2SchemaConfiguration !== undefined;
|
|
336
|
-
this._messagesV2 =
|
|
383
|
+
this._messagesV2 = parseConfigure(signedData);
|
|
337
384
|
this.protocolV2SchemaConfiguration = configuration;
|
|
338
385
|
if (isReconfiguration) {
|
|
339
386
|
this.protocolV2Links
|
|
@@ -617,13 +664,16 @@ export default class ElectronBleTransport {
|
|
|
617
664
|
|
|
618
665
|
for (let i = 0; i < probeOrder.length; i += 1) {
|
|
619
666
|
const protocol = probeOrder[i];
|
|
667
|
+
const isLastProbe = i === probeOrder.length - 1;
|
|
620
668
|
if (i > 0) {
|
|
621
669
|
// Keep the physical BLE link while switching probes. Reconnecting here can
|
|
622
670
|
// summon a second OS pairing prompt for a device that is still onboarding.
|
|
623
671
|
await this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
|
|
624
672
|
}
|
|
625
673
|
const detected =
|
|
626
|
-
protocol === 'V1'
|
|
674
|
+
protocol === 'V1'
|
|
675
|
+
? await this.probeProtocolV1(uuid, isLastProbe)
|
|
676
|
+
: await this.probeProtocolV2(uuid, undefined, isLastProbe);
|
|
627
677
|
if (detected) {
|
|
628
678
|
this.deviceProtocol.set(uuid, protocol);
|
|
629
679
|
this.Log?.debug(`[Electron BLE] detectProtocol: uuid=${uuid} -> ${protocol}`);
|
|
@@ -675,7 +725,7 @@ export default class ElectronBleTransport {
|
|
|
675
725
|
}
|
|
676
726
|
}
|
|
677
727
|
|
|
678
|
-
private async probeProtocolV1(uuid: string) {
|
|
728
|
+
private async probeProtocolV1(uuid: string, isLastProbe = false) {
|
|
679
729
|
if (!this._messages) {
|
|
680
730
|
return false;
|
|
681
731
|
}
|
|
@@ -689,36 +739,46 @@ export default class ElectronBleTransport {
|
|
|
689
739
|
} catch (error) {
|
|
690
740
|
this.clearProbeProtocol(uuid, 'V1');
|
|
691
741
|
this.Log?.debug('[Electron BLE] Protocol V1 GetFeatures probe failed:', error);
|
|
692
|
-
if (
|
|
742
|
+
if (shouldRethrowProtocolProbeError(error, isLastProbe)) {
|
|
693
743
|
throw error;
|
|
694
744
|
}
|
|
695
745
|
return false;
|
|
696
746
|
}
|
|
697
747
|
}
|
|
698
748
|
|
|
699
|
-
private async probeProtocolV2(
|
|
749
|
+
private async probeProtocolV2(
|
|
750
|
+
uuid: string,
|
|
751
|
+
expectedProtocol?: ProtocolType,
|
|
752
|
+
isLastProbe = false
|
|
753
|
+
) {
|
|
700
754
|
if (!this._messages || !this._messagesV2) {
|
|
701
755
|
return false;
|
|
702
756
|
}
|
|
703
757
|
|
|
704
758
|
this.deviceProtocol.set(uuid, 'V2');
|
|
705
759
|
this.v2Assemblers.get(uuid)?.reset();
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
760
|
+
try {
|
|
761
|
+
const detected = await probeProtocolV2Helper({
|
|
762
|
+
call: (name, data, options) => this.callProtocolV2(uuid, name, data, options),
|
|
763
|
+
timeoutMs: PROTOCOL_V2_PROBE_TIMEOUT_MS,
|
|
764
|
+
logger: this.Log,
|
|
765
|
+
logPrefix: 'ProtocolV2 BLE',
|
|
766
|
+
onProbeFailed: () => {
|
|
767
|
+
this.v2Assemblers.get(uuid)?.reset();
|
|
768
|
+
this.resetProtocolV2Frames(uuid);
|
|
769
|
+
},
|
|
770
|
+
// A declared V2 protocol needs no fallback; preserve the actual link failure.
|
|
771
|
+
shouldRethrow: error =>
|
|
772
|
+
expectedProtocol === 'V2' || shouldRethrowProtocolProbeError(error, isLastProbe),
|
|
773
|
+
});
|
|
774
|
+
if (!detected) {
|
|
775
|
+
this.clearProbeProtocol(uuid, 'V2');
|
|
776
|
+
}
|
|
777
|
+
return detected;
|
|
778
|
+
} catch (error) {
|
|
719
779
|
this.clearProbeProtocol(uuid, 'V2');
|
|
780
|
+
throw error;
|
|
720
781
|
}
|
|
721
|
-
return detected;
|
|
722
782
|
}
|
|
723
783
|
|
|
724
784
|
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 { 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
|
|
|
@@ -1672,15 +1672,16 @@ 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.
|
|
1675
1676
|
const response = await this.callProtocolV1(
|
|
1676
1677
|
path,
|
|
1677
|
-
|
|
1678
|
+
'GetFeatures',
|
|
1678
1679
|
{},
|
|
1679
1680
|
{
|
|
1680
1681
|
timeoutMs: PROTOCOL_V1_PROBE_TIMEOUT,
|
|
1681
1682
|
}
|
|
1682
1683
|
);
|
|
1683
|
-
return
|
|
1684
|
+
return response.type === 'Features';
|
|
1684
1685
|
} catch (error) {
|
|
1685
1686
|
if (this.isWebUsbIoHardwareError(error)) throw error;
|
|
1686
1687
|
return false;
|
|
@@ -1828,7 +1829,8 @@ export default class WebUsbTransport extends ProtocolV2UsbTransportBase<string>
|
|
|
1828
1829
|
}
|
|
1829
1830
|
}
|
|
1830
1831
|
decoded.reset();
|
|
1831
|
-
|
|
1832
|
+
const result = decoded.toBuffer();
|
|
1833
|
+
return Buffer.from(result as unknown as ArrayBuffer).toString('hex');
|
|
1832
1834
|
}
|
|
1833
1835
|
|
|
1834
1836
|
/**
|