@onekeyfe/hd-core 1.2.0-alpha.48 → 1.2.0-alpha.49

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.
Files changed (55) hide show
  1. package/__tests__/check-all-firmware-release-protocol-v2.test.ts +4 -4
  2. package/__tests__/device-lifecycle-events.test.ts +105 -3
  3. package/__tests__/method-protocol-support.test.ts +19 -0
  4. package/__tests__/protocol-binding.test.ts +89 -0
  5. package/__tests__/protocol-v2.test.ts +274 -6
  6. package/__tests__/search-devices.test.ts +12 -3
  7. package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
  8. package/dist/api/DetectDeviceConnectProtocol.d.ts +7 -0
  9. package/dist/api/DetectDeviceConnectProtocol.d.ts.map +1 -0
  10. package/dist/api/FirmwareUpdate.d.ts.map +1 -1
  11. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  12. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  13. package/dist/api/FirmwareUpdateV4.d.ts +1 -0
  14. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  15. package/dist/api/SearchDevices.d.ts.map +1 -1
  16. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  17. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  18. package/dist/api/index.d.ts +1 -0
  19. package/dist/api/index.d.ts.map +1 -1
  20. package/dist/core/index.d.ts.map +1 -1
  21. package/dist/device/Device.d.ts +5 -1
  22. package/dist/device/Device.d.ts.map +1 -1
  23. package/dist/device/DevicePool.d.ts.map +1 -1
  24. package/dist/index.d.ts +19 -2
  25. package/dist/index.js +238 -105
  26. package/dist/inject.d.ts +6 -1
  27. package/dist/inject.d.ts.map +1 -1
  28. package/dist/lowLevelInject.d.ts.map +1 -1
  29. package/dist/topLevelInject.d.ts.map +1 -1
  30. package/dist/types/api/detectDeviceConnectProtocol.d.ts +4 -0
  31. package/dist/types/api/detectDeviceConnectProtocol.d.ts.map +1 -0
  32. package/dist/types/api/index.d.ts +4 -0
  33. package/dist/types/api/index.d.ts.map +1 -1
  34. package/dist/types/params.d.ts +1 -0
  35. package/dist/types/params.d.ts.map +1 -1
  36. package/package.json +4 -4
  37. package/src/api/CheckAllFirmwareRelease.ts +5 -3
  38. package/src/api/DetectDeviceConnectProtocol.ts +18 -0
  39. package/src/api/FirmwareUpdate.ts +6 -2
  40. package/src/api/FirmwareUpdateV2.ts +5 -2
  41. package/src/api/FirmwareUpdateV3.ts +6 -2
  42. package/src/api/FirmwareUpdateV4.ts +68 -40
  43. package/src/api/SearchDevices.ts +3 -1
  44. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +15 -4
  45. package/src/api/firmware/uploadFirmware.ts +17 -4
  46. package/src/api/index.ts +1 -0
  47. package/src/core/index.ts +11 -2
  48. package/src/device/Device.ts +53 -15
  49. package/src/device/DevicePool.ts +7 -2
  50. package/src/inject.ts +57 -2
  51. package/src/lowLevelInject.ts +6 -3
  52. package/src/topLevelInject.ts +6 -3
  53. package/src/types/api/detectDeviceConnectProtocol.ts +7 -0
  54. package/src/types/api/index.ts +11 -0
  55. package/src/types/params.ts +7 -1
@@ -95,6 +95,7 @@ export type InitOptions = {
95
95
  passphraseState?: string;
96
96
  deriveCardano?: boolean;
97
97
  connectProtocol?: HardwareConnectProtocol;
98
+ forceProtocolDetection?: boolean;
98
99
  protocolV2DeviceInfoTimeoutMs?: number;
99
100
  /** Refresh Protocol V2 runtime state before returning discovery results. */
100
101
  refreshRuntimeState?: boolean;
@@ -377,7 +378,10 @@ export class Device extends EventEmitter {
377
378
  * Device connect
378
379
  * @returns {Promise<boolean>}
379
380
  */
380
- connect(connectProtocol?: HardwareConnectProtocol) {
381
+ connect(
382
+ connectProtocol?: HardwareConnectProtocol,
383
+ options?: { forceProtocolDetection?: boolean }
384
+ ) {
381
385
  const env = DataManager.getSettings('env');
382
386
  // eslint-disable-next-line no-async-promise-executor
383
387
  return new Promise<boolean>(async (resolve, reject) => {
@@ -387,7 +391,7 @@ export class Device extends EventEmitter {
387
391
  return;
388
392
  }
389
393
  try {
390
- await this.acquire(connectProtocol);
394
+ await this.acquire(connectProtocol, options);
391
395
  resolve(true);
392
396
  } catch (error) {
393
397
  reject(error);
@@ -397,7 +401,7 @@ export class Device extends EventEmitter {
397
401
  // 不存在 Session ID 或存在 Session ID 但设备在别处使用,都需要 acquire 获取最新 sessionID
398
402
  if (!this.mainId || (!this.isUsedHere() && this.originalDescriptor)) {
399
403
  try {
400
- await this.acquire(connectProtocol);
404
+ await this.acquire(connectProtocol, options);
401
405
  resolve(true);
402
406
  } catch (error) {
403
407
  reject(error);
@@ -414,11 +418,16 @@ export class Device extends EventEmitter {
414
418
 
415
419
  async acquire(
416
420
  expectedProtocol?: HardwareConnectProtocol,
417
- options?: { throwOnRunPromiseError?: boolean }
421
+ options?: { throwOnRunPromiseError?: boolean; forceProtocolDetection?: boolean }
418
422
  ) {
419
423
  const env = DataManager.getSettings('env');
420
424
  const mainIdKey = DataManager.isBleConnect(env) ? 'id' : 'session';
421
- const protocolHint = expectedProtocol ? undefined : this.originalDescriptor.protocolType;
425
+ const previousProtocol = this.originalDescriptor.protocolType;
426
+ // A protocol stored after a successful probe is authoritative. Only the explicit
427
+ // first-connection/recovery path may bypass it and probe both protocols again.
428
+ const strictProtocol = options?.forceProtocolDetection
429
+ ? undefined
430
+ : expectedProtocol ?? this.originalDescriptor.protocolType;
422
431
  try {
423
432
  let acquireResult: unknown;
424
433
  if (DataManager.isBleConnect(env)) {
@@ -430,8 +439,8 @@ export class Device extends EventEmitter {
430
439
  this.originalDescriptor.id,
431
440
  undefined,
432
441
  true,
433
- expectedProtocol,
434
- protocolHint
442
+ strictProtocol,
443
+ undefined
435
444
  );
436
445
  this.mainId = (acquireResult as any)?.uuid ?? '';
437
446
  Log.debug('Expected uuid:', this.mainId);
@@ -440,24 +449,31 @@ export class Device extends EventEmitter {
440
449
  this.originalDescriptor.path,
441
450
  this.originalDescriptor.session,
442
451
  undefined,
443
- expectedProtocol,
444
- protocolHint
452
+ strictProtocol,
453
+ undefined
445
454
  );
446
455
  this.mainId = acquireResult as string | undefined;
447
456
  Log.debug('Expected session id:', this.mainId);
448
457
  }
449
- this.deviceAcquired = true;
450
- this.updateDescriptor({ [mainIdKey]: this.mainId } as unknown as DeviceDescriptor);
451
-
452
458
  // Propagate protocol version detected during acquire.
453
459
  const detectedProtocol =
454
460
  (acquireResult as { protocolType?: HardwareConnectProtocol } | undefined)?.protocolType ??
455
461
  TransportManager.transport?.getProtocolType?.(
456
462
  DataManager.isBleConnect(env) ? this.originalDescriptor.id : this.originalDescriptor.path
457
463
  );
464
+ if (options?.forceProtocolDetection && !detectedProtocol) {
465
+ throw ERRORS.TypedError(
466
+ HardwareErrorCode.RuntimeError,
467
+ `Active protocol detection returned no protocol for ${
468
+ this.originalDescriptor.path || this.originalDescriptor.id
469
+ }`
470
+ );
471
+ }
458
472
  if (detectedProtocol) {
459
473
  this.originalDescriptor.protocolType = detectedProtocol;
460
474
  }
475
+ this.deviceAcquired = true;
476
+ this.updateDescriptor({ [mainIdKey]: this.mainId } as unknown as DeviceDescriptor);
461
477
 
462
478
  if (this.commands) {
463
479
  await this.commands.dispose(false);
@@ -465,6 +481,22 @@ export class Device extends EventEmitter {
465
481
 
466
482
  this.commands = new DeviceCommands(this, this.mainId ?? '');
467
483
  } catch (error) {
484
+ if (options?.forceProtocolDetection) {
485
+ this.originalDescriptor.protocolType = previousProtocol;
486
+ const failedSession = this.mainId;
487
+ this.deviceAcquired = false;
488
+ if (failedSession) {
489
+ try {
490
+ await this.deviceConnector?.release?.(failedSession, false);
491
+ } catch (releaseError) {
492
+ Log.debug('Failed to release an unsuccessful protocol probe', releaseError);
493
+ }
494
+ }
495
+ if (!DataManager.isBleConnect(env)) {
496
+ this.mainId = null;
497
+ this.updateDescriptor({ session: null } as DeviceDescriptor);
498
+ }
499
+ }
468
500
  if (options?.throwOnRunPromiseError) {
469
501
  throw error;
470
502
  }
@@ -1349,11 +1381,17 @@ export class Device extends EventEmitter {
1349
1381
  }
1350
1382
  };
1351
1383
 
1352
- if (!this.isUsedHere() || this.commands.disposed) {
1353
- const env = DataManager.getSettings('env');
1384
+ const env = DataManager.getSettings('env');
1385
+ if (options.forceProtocolDetection && env !== 'react-native' && this.isUsedHere()) {
1386
+ await this.release();
1387
+ }
1388
+
1389
+ if (options.forceProtocolDetection || !this.isUsedHere() || this.commands.disposed) {
1354
1390
  if (env !== 'react-native') {
1355
1391
  try {
1356
- await this.acquire(options.connectProtocol);
1392
+ await this.acquire(options.connectProtocol, {
1393
+ forceProtocolDetection: options.forceProtocolDetection,
1394
+ });
1357
1395
  } catch (error) {
1358
1396
  clearRunPromise();
1359
1397
  runPromise.reject(error);
@@ -156,7 +156,9 @@ export class DevicePool extends EventEmitter {
156
156
  if (!device) {
157
157
  device = Device.fromDescriptor(descriptor);
158
158
  device.deviceConnector = this.connector;
159
- await device.connect(initOptions?.connectProtocol);
159
+ await device.connect(initOptions?.connectProtocol, {
160
+ forceProtocolDetection: initOptions?.forceProtocolDetection,
161
+ });
160
162
  try {
161
163
  await device.initialize(initOptions);
162
164
  if (initOptions?.refreshRuntimeState && device.isProtocolV2()) {
@@ -181,7 +183,10 @@ export class DevicePool extends EventEmitter {
181
183
  refreshError = error;
182
184
  }
183
185
  },
184
- { connectProtocol: initOptions.connectProtocol }
186
+ {
187
+ connectProtocol: initOptions.connectProtocol,
188
+ forceProtocolDetection: initOptions.forceProtocolDetection,
189
+ }
185
190
  );
186
191
  if (refreshError instanceof Error) throw refreshError;
187
192
  if (refreshError) throw new Error(String(refreshError));
package/src/inject.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { HardwareConnectProtocol } from '@onekeyfe/hd-shared';
1
2
  import type { Unsuccessful } from './types';
2
3
  import type { EventEmitter } from 'events';
3
4
  import type { CallMethod } from './events';
@@ -39,6 +40,50 @@ export interface InjectApi {
39
40
  switchTransport: CoreApi['switchTransport'];
40
41
  }
41
42
 
43
+ const normalizeConnectId = (connectId: string) => connectId.trim().toLowerCase();
44
+
45
+ /**
46
+ * Keeps verified protocols isolated per transport endpoint and injects them at
47
+ * the single public call boundary, including APIs that do not expose params.
48
+ */
49
+ export const createProtocolAwareCall = (rawCall: CoreApi['call']) => {
50
+ const protocolByConnectId = new Map<string, HardwareConnectProtocol>();
51
+
52
+ const setDeviceConnectProtocol: CoreApi['setDeviceConnectProtocol'] = (
53
+ connectId,
54
+ connectProtocol
55
+ ) => {
56
+ const normalizedConnectId = normalizeConnectId(connectId);
57
+ if (!normalizedConnectId) return;
58
+ if (connectProtocol) {
59
+ protocolByConnectId.set(normalizedConnectId, connectProtocol);
60
+ } else {
61
+ protocolByConnectId.delete(normalizedConnectId);
62
+ }
63
+ };
64
+
65
+ const call: CoreApi['call'] = params => {
66
+ if (!params || typeof params !== 'object') {
67
+ return rawCall(params);
68
+ }
69
+
70
+ const connectId = typeof params.connectId === 'string' ? params.connectId : undefined;
71
+ const boundProtocol = connectId
72
+ ? protocolByConnectId.get(normalizeConnectId(connectId))
73
+ : undefined;
74
+ if (
75
+ boundProtocol &&
76
+ params.connectProtocol === undefined &&
77
+ params.forceProtocolDetection !== true
78
+ ) {
79
+ return rawCall({ ...params, connectProtocol: boundProtocol });
80
+ }
81
+ return rawCall(params);
82
+ };
83
+
84
+ return { call, setDeviceConnectProtocol };
85
+ };
86
+
42
87
  export const inject = ({
43
88
  call,
44
89
  cancel,
@@ -49,6 +94,7 @@ export const inject = ({
49
94
  switchTransport,
50
95
  uiResponse,
51
96
  }: InjectApi): CoreApi => {
97
+ const protocolAwareCall = createProtocolAwareCall(call);
52
98
  const api: CoreApi = {
53
99
  on: <T extends string, P extends (...args: any[]) => any>(type: T, fn: P) => {
54
100
  eventEmitter.on(type, fn);
@@ -66,7 +112,9 @@ export const inject = ({
66
112
 
67
113
  init,
68
114
 
69
- call,
115
+ call: protocolAwareCall.call,
116
+
117
+ setDeviceConnectProtocol: protocolAwareCall.setDeviceConnectProtocol,
70
118
 
71
119
  dispose,
72
120
 
@@ -78,7 +126,7 @@ export const inject = ({
78
126
 
79
127
  switchTransport,
80
128
 
81
- ...createCoreApi(call),
129
+ ...createCoreApi(protocolAwareCall.call),
82
130
  };
83
131
  return api;
84
132
  };
@@ -93,6 +141,7 @@ export const createCoreApi = (
93
141
  | 'removeAllListeners'
94
142
  | 'init'
95
143
  | 'call'
144
+ | 'setDeviceConnectProtocol'
96
145
  | 'dispose'
97
146
  | 'uiResponse'
98
147
  | 'cancel'
@@ -105,6 +154,12 @@ export const createCoreApi = (
105
154
  * 搜索设备
106
155
  */
107
156
  searchDevices: params => call({ ...params, method: 'searchDevices' }),
157
+ detectDeviceConnectProtocol: connectId =>
158
+ call({
159
+ connectId,
160
+ method: 'detectDeviceConnectProtocol',
161
+ forceProtocolDetection: true,
162
+ }),
108
163
 
109
164
  /**
110
165
  * 获取设备信息
@@ -1,4 +1,4 @@
1
- import { createCoreApi } from './inject';
1
+ import { createCoreApi, createProtocolAwareCall } from './inject';
2
2
 
3
3
  import type { EventEmitter } from 'events';
4
4
  import type { CallMethod, CoreMessage } from './events';
@@ -33,6 +33,7 @@ export const lowLevelInject = ({
33
33
  switchTransport,
34
34
  addHardwareGlobalEventListener,
35
35
  }: LowLevelInjectApi): LowLevelCoreApi => {
36
+ const protocolAwareCall = createProtocolAwareCall(call);
36
37
  const api: LowLevelCoreApi = {
37
38
  addHardwareGlobalEventListener,
38
39
  removeAllListeners: type => {
@@ -41,7 +42,9 @@ export const lowLevelInject = ({
41
42
 
42
43
  init,
43
44
 
44
- call,
45
+ call: protocolAwareCall.call,
46
+
47
+ setDeviceConnectProtocol: protocolAwareCall.setDeviceConnectProtocol,
45
48
 
46
49
  dispose,
47
50
 
@@ -55,7 +58,7 @@ export const lowLevelInject = ({
55
58
 
56
59
  emit: () => {},
57
60
 
58
- ...createCoreApi(call),
61
+ ...createCoreApi(protocolAwareCall.call),
59
62
  };
60
63
  return api;
61
64
  };
@@ -1,6 +1,6 @@
1
1
  import EventEmitter from 'events';
2
2
 
3
- import { createCoreApi } from './inject';
3
+ import { createCoreApi, createProtocolAwareCall } from './inject';
4
4
 
5
5
  import type { ConnectSettings } from './types/settings';
6
6
  import type { CoreApi } from './types/api';
@@ -19,6 +19,7 @@ export const topLevelInject = () => {
19
19
  if (!lowLevelApi) return Promise.resolve(undefined);
20
20
  return lowLevelApi.call(params);
21
21
  };
22
+ const protocolAwareCall = createProtocolAwareCall(call);
22
23
  const api: CoreApi = {
23
24
  on: <T extends string, P extends (...args: any[]) => any>(type: T, fn: P) => {
24
25
  eventEmitter.on(type, fn);
@@ -37,9 +38,11 @@ export const topLevelInject = () => {
37
38
  return lowLevelApi?.init(settings) ?? Promise.resolve(false);
38
39
  },
39
40
 
40
- call,
41
+ call: protocolAwareCall.call,
41
42
 
42
- ...createCoreApi(call),
43
+ setDeviceConnectProtocol: protocolAwareCall.setDeviceConnectProtocol,
44
+
45
+ ...createCoreApi(protocolAwareCall.call),
43
46
 
44
47
  removeAllListeners: type => {
45
48
  eventEmitter.removeAllListeners(type);
@@ -0,0 +1,7 @@
1
+ import type { HardwareConnectProtocol } from '@onekeyfe/hd-shared';
2
+
3
+ import type { Response } from '../params';
4
+
5
+ export declare function detectDeviceConnectProtocol(
6
+ connectId: string
7
+ ): Response<HardwareConnectProtocol>;
@@ -19,6 +19,7 @@ import type { checkBootloaderRelease } from './checkBootloaderRelease';
19
19
  import type { checkAllFirmwareRelease } from './checkAllFirmwareRelease';
20
20
  import type { checkFirmwareTypeAvailable } from './checkFirmwareTypeAvailable';
21
21
  import type { searchDevices } from './searchDevices';
22
+ import type { detectDeviceConnectProtocol } from './detectDeviceConnectProtocol';
22
23
  import type { getFeatures } from './getFeatures';
23
24
  import type { getDeviceState } from './getDeviceState';
24
25
  import type { getOnekeyFeatures } from './getOnekeyFeatures';
@@ -145,6 +146,7 @@ import type { benfenSignMessage } from './benfenSignMessage';
145
146
  import type { neoGetAddress } from './neoGetAddress';
146
147
  import type { neoSignTransaction } from './neoSignTransaction';
147
148
  import type { ConnectSettings } from '../settings';
149
+ import type { HardwareConnectProtocol } from '@onekeyfe/hd-shared';
148
150
 
149
151
  export * from './export';
150
152
  export type { DeviceStateScope, GetDeviceStateParams } from './getDeviceState';
@@ -169,6 +171,14 @@ export type CoreApi = {
169
171
  removeAllListeners: typeof removeAllListeners;
170
172
  dispose: () => void | Promise<void>;
171
173
  call: (params: any) => Promise<any>;
174
+ /**
175
+ * Bind a protocol that has already been verified for one transport endpoint.
176
+ * Every later SDK call for the same connectId uses it as a strict expectation.
177
+ */
178
+ setDeviceConnectProtocol: (
179
+ connectId: string,
180
+ connectProtocol: HardwareConnectProtocol | undefined
181
+ ) => void;
172
182
  uiResponse: typeof uiResponse;
173
183
  cancel: (connectId?: string) => void;
174
184
  updateSettings: typeof updateSettings;
@@ -196,6 +206,7 @@ export type CoreApi = {
196
206
  * Device function
197
207
  */
198
208
  searchDevices: typeof searchDevices;
209
+ detectDeviceConnectProtocol: typeof detectDeviceConnectProtocol;
199
210
  promptWebDeviceAccess: typeof promptWebDeviceAccess;
200
211
  getFeatures: typeof getFeatures;
201
212
  getDeviceState: typeof getDeviceState;
@@ -61,9 +61,15 @@ export interface CommonParams {
61
61
 
62
62
  /**
63
63
  * Strictly expected transport protocol. The SDK actively verifies this value and
64
- * rejects a mismatch. If omitted, a cached protocol only influences probe order.
64
+ * rejects a mismatch. After a successful probe, the cached protocol is also strict.
65
65
  */
66
66
  connectProtocol?: HardwareConnectProtocol;
67
+
68
+ /**
69
+ * Ignore a previously bound protocol for this call and actively detect the
70
+ * protocol again. Intended for the first verified connection or explicit recovery.
71
+ */
72
+ forceProtocolDetection?: boolean;
67
73
  }
68
74
 
69
75
  export type Params<T> = CommonParams & T & { bundle?: undefined };