@onekeyfe/hd-core 1.2.0-alpha.3 → 1.2.0-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.
Files changed (117) hide show
  1. package/__tests__/preInitialize.test.ts +33 -0
  2. package/__tests__/protocol-v2.test.ts +1443 -480
  3. package/dist/api/FileRead.d.ts.map +1 -1
  4. package/dist/api/FileWrite.d.ts.map +1 -1
  5. package/dist/api/FirmwareUpdateV4.d.ts +22 -2
  6. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  7. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +1 -0
  8. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  9. package/dist/api/firmware/getBinary.d.ts +4 -0
  10. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  11. package/dist/api/firmware/progressThrottle.d.ts +3 -0
  12. package/dist/api/firmware/progressThrottle.d.ts.map +1 -0
  13. package/dist/api/index.d.ts +5 -10
  14. package/dist/api/index.d.ts.map +1 -1
  15. package/dist/api/protocol-v2/DeviceFactoryInfoGet.d.ts +6 -0
  16. package/dist/api/protocol-v2/DeviceFactoryInfoGet.d.ts.map +1 -0
  17. package/dist/api/protocol-v2/DeviceFactoryInfoSet.d.ts +7 -0
  18. package/dist/api/protocol-v2/DeviceFactoryInfoSet.d.ts.map +1 -0
  19. package/dist/api/protocol-v2/DeviceFirmwareUpdate.d.ts +1 -1
  20. package/dist/api/protocol-v2/DeviceFirmwareUpdate.d.ts.map +1 -1
  21. package/dist/api/protocol-v2/DeviceGetFirmwareUpdateStatus.d.ts +3 -2
  22. package/dist/api/protocol-v2/DeviceGetFirmwareUpdateStatus.d.ts.map +1 -1
  23. package/dist/api/protocol-v2/{DeviceGetDeviceInfo.d.ts → DeviceInfoGet.d.ts} +10 -10
  24. package/dist/api/protocol-v2/DeviceInfoGet.d.ts.map +1 -0
  25. package/dist/api/protocol-v2/{FilesystemFixPermission.d.ts → FilesystemPermissionFix.d.ts} +2 -2
  26. package/dist/api/protocol-v2/FilesystemPermissionFix.d.ts.map +1 -0
  27. package/dist/api/protocol-v2/ProtocolInfoRequest.d.ts +6 -0
  28. package/dist/api/protocol-v2/ProtocolInfoRequest.d.ts.map +1 -0
  29. package/dist/api/protocol-v2/helpers.d.ts +27 -18
  30. package/dist/api/protocol-v2/helpers.d.ts.map +1 -1
  31. package/dist/api/ton/TonSignData.d.ts +1 -5
  32. package/dist/api/ton/TonSignData.d.ts.map +1 -1
  33. package/dist/core/index.d.ts.map +1 -1
  34. package/dist/data-manager/DataManager.d.ts +1 -1
  35. package/dist/data-manager/DataManager.d.ts.map +1 -1
  36. package/dist/data-manager/connectSettings.d.ts.map +1 -1
  37. package/dist/device/Device.d.ts +4 -1
  38. package/dist/device/Device.d.ts.map +1 -1
  39. package/dist/deviceProfile/buildDeviceFeatures.d.ts.map +1 -1
  40. package/dist/deviceProfile/buildDeviceProfile.d.ts.map +1 -1
  41. package/dist/index.d.ts +82 -54
  42. package/dist/index.js +1615 -1537
  43. package/dist/inject.d.ts.map +1 -1
  44. package/dist/protocols/protocol-v2/features.d.ts +12 -14
  45. package/dist/protocols/protocol-v2/features.d.ts.map +1 -1
  46. package/dist/protocols/protocol-v2/firmware.d.ts +11 -9
  47. package/dist/protocols/protocol-v2/firmware.d.ts.map +1 -1
  48. package/dist/types/api/firmwareUpdate.d.ts +1 -6
  49. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  50. package/dist/types/api/getDeviceInfo.d.ts +2 -0
  51. package/dist/types/api/getDeviceInfo.d.ts.map +1 -1
  52. package/dist/types/api/index.d.ts +6 -11
  53. package/dist/types/api/index.d.ts.map +1 -1
  54. package/dist/types/api/protocolV2.d.ts +12 -17
  55. package/dist/types/api/protocolV2.d.ts.map +1 -1
  56. package/dist/types/device.d.ts +1 -0
  57. package/dist/types/device.d.ts.map +1 -1
  58. package/dist/types/params.d.ts +1 -0
  59. package/dist/types/params.d.ts.map +1 -1
  60. package/dist/types/settings.d.ts +29 -2
  61. package/dist/types/settings.d.ts.map +1 -1
  62. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  63. package/dist/utils/patch.d.ts +1 -1
  64. package/dist/utils/patch.d.ts.map +1 -1
  65. package/package.json +4 -4
  66. package/src/api/FileRead.ts +14 -1
  67. package/src/api/FileWrite.ts +14 -1
  68. package/src/api/FirmwareUpdateV4.ts +975 -201
  69. package/src/api/GetOnekeyFeatures.ts +1 -1
  70. package/src/api/device/DeviceRebootToBoardloader.ts +4 -4
  71. package/src/api/device/DeviceRebootToBootloader.ts +4 -4
  72. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +18 -4
  73. package/src/api/firmware/progressThrottle.ts +44 -0
  74. package/src/api/index.ts +5 -10
  75. package/src/api/protocol-v2/{FactoryGetDeviceInfo.ts → DeviceFactoryInfoGet.ts} +3 -3
  76. package/src/api/protocol-v2/DeviceFactoryInfoSet.ts +32 -0
  77. package/src/api/protocol-v2/DeviceFirmwareUpdate.ts +3 -5
  78. package/src/api/protocol-v2/DeviceGetFirmwareUpdateStatus.ts +39 -5
  79. package/src/api/protocol-v2/{DeviceGetDeviceInfo.ts → DeviceInfoGet.ts} +52 -22
  80. package/src/api/protocol-v2/DeviceReboot.ts +1 -1
  81. package/src/api/protocol-v2/{FilesystemFixPermission.ts → FilesystemPermissionFix.ts} +2 -2
  82. package/src/api/protocol-v2/{GetProtoVersion.ts → ProtocolInfoRequest.ts} +2 -2
  83. package/src/api/protocol-v2/helpers.ts +64 -43
  84. package/src/api/ton/TonSignData.ts +1 -5
  85. package/src/core/index.ts +19 -5
  86. package/src/data/messages/messages-protocol-v2.json +485 -1069
  87. package/src/data-manager/DataManager.ts +7 -0
  88. package/src/data-manager/connectSettings.ts +0 -6
  89. package/src/device/Device.ts +25 -14
  90. package/src/deviceProfile/buildDeviceFeatures.ts +60 -63
  91. package/src/deviceProfile/buildDeviceProfile.ts +77 -58
  92. package/src/inject.ts +10 -16
  93. package/src/protocols/protocol-v2/features.ts +26 -63
  94. package/src/protocols/protocol-v2/firmware.ts +12 -36
  95. package/src/types/api/firmwareUpdate.ts +10 -20
  96. package/src/types/api/getDeviceInfo.ts +2 -0
  97. package/src/types/api/index.ts +10 -20
  98. package/src/types/api/protocolV2.ts +25 -43
  99. package/src/types/device.ts +1 -0
  100. package/src/types/params.ts +4 -0
  101. package/src/types/settings.ts +44 -35
  102. package/src/utils/deviceFeaturesUtils.ts +45 -10
  103. package/dist/api/protocol-v2/DevReboot.d.ts +0 -7
  104. package/dist/api/protocol-v2/DevReboot.d.ts.map +0 -1
  105. package/dist/api/protocol-v2/DeviceGetDeviceInfo.d.ts.map +0 -1
  106. package/dist/api/protocol-v2/DeviceGetOnboardingStatus.d.ts +0 -6
  107. package/dist/api/protocol-v2/DeviceGetOnboardingStatus.d.ts.map +0 -1
  108. package/dist/api/protocol-v2/FactoryDeviceInfoSettings.d.ts +0 -7
  109. package/dist/api/protocol-v2/FactoryDeviceInfoSettings.d.ts.map +0 -1
  110. package/dist/api/protocol-v2/FactoryGetDeviceInfo.d.ts +0 -6
  111. package/dist/api/protocol-v2/FactoryGetDeviceInfo.d.ts.map +0 -1
  112. package/dist/api/protocol-v2/FilesystemFixPermission.d.ts.map +0 -1
  113. package/dist/api/protocol-v2/GetProtoVersion.d.ts +0 -6
  114. package/dist/api/protocol-v2/GetProtoVersion.d.ts.map +0 -1
  115. package/src/api/protocol-v2/DevReboot.ts +0 -24
  116. package/src/api/protocol-v2/DeviceGetOnboardingStatus.ts +0 -16
  117. package/src/api/protocol-v2/FactoryDeviceInfoSettings.ts +0 -29
@@ -1,18 +1,16 @@
1
- import { DevRebootType } from '@onekeyfe/hd-transport';
1
+ import { DeviceRebootType } from '@onekeyfe/hd-transport';
2
2
 
3
3
  import { invalidParameter, validateNonEmptyString } from '../helpers/filesystemValidation';
4
+ import { ProtocolV2FirmwareTargetType } from '../../protocols/protocol-v2/firmware';
4
5
 
5
6
  import type {
6
- DevFirmwareTarget,
7
- DevFirmwareTargetType,
7
+ DeviceFirmwareTarget,
8
+ DeviceFirmwareUpdateRecordFields,
9
+ DeviceFirmwareTargetType,
8
10
  TransportCallOptions,
9
11
  } from '@onekeyfe/hd-transport';
10
12
 
11
- export type RebootTypeInput =
12
- | DevRebootType
13
- | keyof typeof DevRebootType
14
- | string
15
- | number;
13
+ export type RebootTypeInput = DeviceRebootType | keyof typeof DeviceRebootType | string | number;
16
14
 
17
15
  export type DeviceRebootParams = {
18
16
  rebootType?: RebootTypeInput;
@@ -20,48 +18,58 @@ export type DeviceRebootParams = {
20
18
  };
21
19
 
22
20
  export type DeviceFirmwareTargetInput =
23
- | DevFirmwareTarget
21
+ | DeviceFirmwareTarget
24
22
  | {
25
- targetId?: DevFirmwareTargetType | string | number;
26
- target_id?: DevFirmwareTargetType | string | number;
23
+ targetId?: DeviceFirmwareTargetType | string | number;
24
+ target_id?: DeviceFirmwareTargetType | string | number;
27
25
  path: string;
28
26
  };
29
27
 
30
28
  export type DeviceFirmwareUpdateParams = {
31
29
  targets?: DeviceFirmwareTargetInput[];
32
- targetId?: DevFirmwareTargetType | string | number;
33
- target_id?: DevFirmwareTargetType | string | number;
30
+ targetId?: DeviceFirmwareTargetType | string | number;
31
+ target_id?: DeviceFirmwareTargetType | string | number;
34
32
  path?: string;
35
33
  };
36
34
 
37
- export type FactoryDeviceInfoSettingsParams = {
38
- serial_no?: string;
39
- serialNo?: string;
40
- cpu_info?: string;
41
- cpuInfo?: string;
42
- pre_firmware?: string;
43
- preFirmware?: string;
35
+ export type DeviceFirmwareUpdateStatusGetParams = {
36
+ fields?: DeviceFirmwareUpdateRecordFields;
44
37
  };
45
38
 
46
- const DEV_REBOOT_TYPES: Record<string, DevRebootType> = {
47
- Normal: DevRebootType.Normal,
48
- normal: DevRebootType.Normal,
49
- Romloader: DevRebootType.Boardloader,
50
- romloader: DevRebootType.Boardloader,
51
- Boardloader: DevRebootType.Boardloader,
52
- boardloader: DevRebootType.Boardloader,
53
- Bootloader: DevRebootType.Bootloader,
54
- bootloader: DevRebootType.Bootloader,
39
+ export type DeviceFactoryInfoSetParams = {
40
+ version?: number;
41
+ serial_number?: string;
42
+ burn_in_completed?: boolean;
43
+ factory_test_completed?: boolean;
44
+ manufacture_time?: {
45
+ year: number;
46
+ month: number;
47
+ day: number;
48
+ hour: number;
49
+ minute: number;
50
+ second: number;
51
+ };
52
+ };
53
+
54
+ const DEVICE_REBOOT_TYPES: Record<string, DeviceRebootType> = {
55
+ Normal: DeviceRebootType.Normal,
56
+ normal: DeviceRebootType.Normal,
57
+ Romloader: DeviceRebootType.Romloader,
58
+ romloader: DeviceRebootType.Romloader,
59
+ Boardloader: DeviceRebootType.Romloader,
60
+ boardloader: DeviceRebootType.Romloader,
61
+ Bootloader: DeviceRebootType.Bootloader,
62
+ bootloader: DeviceRebootType.Bootloader,
55
63
  };
56
64
 
57
65
  export const PROTOCOL_V2_FIRMWARE_UPDATE_OPTIONS: TransportCallOptions = {
58
- intermediateTypes: ['DevFirmwareInstallProgress'],
66
+ intermediateTypes: ['DeviceFirmwareUpdateStatus'],
59
67
  };
60
68
 
61
69
  export const PROTOCOL_V2_FIRMWARE_UPDATE_RESPONSE_TYPES: (
62
70
  | 'Success'
63
- | 'DevFirmwareUpdateStatus'
64
- )[] = ['Success', 'DevFirmwareUpdateStatus'];
71
+ | 'DeviceFirmwareUpdateStatus'
72
+ )[] = ['Success', 'DeviceFirmwareUpdateStatus'];
65
73
 
66
74
  export const getProtocolV2UnknownErrorText = (error: unknown) => {
67
75
  if (!error) {
@@ -120,31 +128,42 @@ export const isProtocolV2DeviceDisconnectedError = (error: unknown) => {
120
128
  );
121
129
  };
122
130
 
123
- export function normalizeRebootType(value: RebootTypeInput | undefined): DevRebootType {
131
+ export function normalizeRebootType(value: RebootTypeInput | undefined): DeviceRebootType {
124
132
  if (typeof value === 'number') return value;
125
133
  if (typeof value === 'string') {
126
134
  const numeric = Number(value);
127
135
  if (Number.isFinite(numeric)) return numeric;
128
- if (value in DEV_REBOOT_TYPES) return DEV_REBOOT_TYPES[value];
136
+ if (value in DEVICE_REBOOT_TYPES) return DEVICE_REBOOT_TYPES[value];
129
137
  }
130
- return DevRebootType.Normal;
138
+ return DeviceRebootType.Normal;
131
139
  }
132
140
 
133
- // 当前 firmware-pro2 子模块的 DevFirmwareTargetType 合法值。
134
- const VALID_FIRMWARE_TARGET_IDS = new Set<number>([0, 1, 2, 3, 4, 5, 6, 10]);
141
+ // 当前 firmware-pro2 子模块的 DeviceFirmwareTargetType 合法值从生成 enum 派生,
142
+ // 避免协议枚举增减时这里继续保留过期手写编号。
143
+ const VALID_FIRMWARE_TARGET_IDS = new Set<number>(
144
+ Object.values(ProtocolV2FirmwareTargetType).filter(value => typeof value === 'number') as number[]
145
+ );
146
+ const FIRMWARE_TARGET_ID_BY_NAME = new Map<string, DeviceFirmwareTargetType>(
147
+ Object.entries(ProtocolV2FirmwareTargetType).flatMap(([key, value]) =>
148
+ VALID_FIRMWARE_TARGET_IDS.has(value)
149
+ ? [[key, value as DeviceFirmwareTargetType]]
150
+ : []
151
+ )
152
+ );
135
153
 
136
154
  function normalizeTargetId(
137
- value: DevFirmwareTargetType | string | number | undefined,
155
+ value: DeviceFirmwareTargetType | string | number | undefined,
138
156
  name: string
139
- ): DevFirmwareTargetType {
157
+ ): DeviceFirmwareTargetType {
140
158
  if (value === undefined || value === null) {
141
159
  throw invalidParameter(`Missing required parameter: ${name}`);
142
160
  }
143
- const numeric = typeof value === 'number' ? value : Number(value);
144
- // 校验值域:仅接受 DevFirmwareTargetType 中定义的 target id,
161
+ const named = typeof value === 'string' ? FIRMWARE_TARGET_ID_BY_NAME.get(value) : undefined;
162
+ const numeric = named ?? (typeof value === 'number' ? value : Number(value));
163
+ // 校验值域:仅接受 DeviceFirmwareTargetType 中定义的 target id,
145
164
  // 不再放行任意非负整数。
146
165
  if (Number.isSafeInteger(numeric) && VALID_FIRMWARE_TARGET_IDS.has(numeric)) {
147
- return numeric as DevFirmwareTargetType;
166
+ return numeric as DeviceFirmwareTargetType;
148
167
  }
149
168
  throw invalidParameter(
150
169
  `Parameter [${name}] must be a valid firmware target id (one of ${[
@@ -153,7 +172,9 @@ function normalizeTargetId(
153
172
  );
154
173
  }
155
174
 
156
- export function normalizeFirmwareTargets(params: DeviceFirmwareUpdateParams): DevFirmwareTarget[] {
175
+ export function normalizeFirmwareTargets(
176
+ params: DeviceFirmwareUpdateParams
177
+ ): DeviceFirmwareTarget[] {
157
178
  const targets =
158
179
  params.targets ??
159
180
  (params.path
@@ -51,11 +51,7 @@ export default class TonSignData extends BaseMethod<HardwareTonSignData> {
51
51
  }
52
52
 
53
53
  getVersionRange() {
54
- return {
55
- pro2: {
56
- min: '0.0.0',
57
- },
58
- };
54
+ return {};
59
55
  }
60
56
 
61
57
  async run() {
package/src/core/index.ts CHANGED
@@ -56,7 +56,6 @@ import TransportManager from '../data-manager/TransportManager';
56
56
  import DeviceConnector from '../device/DeviceConnector';
57
57
  import RequestQueue from './RequestQueue';
58
58
  import { getSynchronize } from '../utils/getSynchronize';
59
- import { setProtocolV2DeviceInfoMock } from '../protocols/protocol-v2/features';
60
59
 
61
60
  import type { ConnectSettings, Features, KnownDevice } from '../types';
62
61
  import type { CoreMessage, IFrameCallMessage, UiPromise, UiPromiseResponse } from '../events';
@@ -96,6 +95,7 @@ const parseInitOptions = (method?: BaseMethod): InitOptions => ({
96
95
  deviceId: method?.payload.deviceId,
97
96
  deriveCardano: method && hasDeriveCardano(method),
98
97
  connectProtocol: method?.payload.connectProtocol,
98
+ protocolV2DeviceInfoTimeoutMs: method?.payload.protocolV2DeviceInfoTimeoutMs,
99
99
  });
100
100
 
101
101
  let _core: Core;
@@ -223,9 +223,15 @@ const handlePreWarmSignal = async (
223
223
  message: CoreMessage,
224
224
  method: BaseMethod
225
225
  ): Promise<any> => {
226
+ const createAckResponse = () => {
227
+ completeMethodRequestContext(method);
228
+ method.dispose();
229
+ return createResponseMessage(method.responseID, true, true);
230
+ };
231
+
226
232
  // no connectId: can't target a device safely, skip pre-warm (ack only)
227
233
  if (!method.connectId) {
228
- return createResponseMessage(method.responseID, true, true);
234
+ return createAckResponse();
229
235
  }
230
236
 
231
237
  const key = method.getPreWarmKey();
@@ -238,12 +244,12 @@ const handlePreWarmSignal = async (
238
244
  } catch {
239
245
  // pre-warm is best-effort; ignore its failure for the coalesced caller
240
246
  }
241
- return createResponseMessage(method.responseID, true, true);
247
+ return createAckResponse();
242
248
  }
243
249
 
244
250
  const doneAt = preWarmDoneAt.get(key);
245
251
  if (typeof doneAt === 'number' && Date.now() - doneAt <= method.preWarmTtl) {
246
- return createResponseMessage(method.responseID, true, true);
252
+ return createAckResponse();
247
253
  }
248
254
 
249
255
  const run = onCallDevice(context, message, method);
@@ -732,6 +738,15 @@ function initDevice(method: BaseMethod) {
732
738
 
733
739
  if (method.connectId) {
734
740
  device = _deviceList.getDevice(method.connectId);
741
+ if (!device && method.name === 'firmwareUpdateV4' && allDevices.length === 1) {
742
+ const [singleDevice] = allDevices;
743
+ if (singleDevice.isBootloader()) {
744
+ Log.debug(
745
+ 'firmwareUpdateV4 uses the only bootloader device when connectId changed after reboot'
746
+ );
747
+ device = singleDevice;
748
+ }
749
+ }
735
750
  } else if (allDevices.length === 1) {
736
751
  [device] = allDevices;
737
752
  } else if (allDevices.length > 1) {
@@ -1439,7 +1454,6 @@ export const init = async (
1439
1454
  try {
1440
1455
  try {
1441
1456
  await DataManager.load(settings);
1442
- setProtocolV2DeviceInfoMock(settings.protocolV2DeviceInfoMockEnabled ?? false);
1443
1457
  initTransport(Transport, plugin);
1444
1458
  } catch {
1445
1459
  Log.error('DataManager.load error');