@onekeyfe/hd-core 1.2.0-alpha.21 → 1.2.0-alpha.23

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 (192) hide show
  1. package/__tests__/DeviceCommands.test.ts +85 -0
  2. package/__tests__/connectSettings.test.ts +47 -5
  3. package/__tests__/device-identity.test.ts +24 -2
  4. package/__tests__/device-settings.test.ts +2 -2
  5. package/__tests__/device-wallet-session-store.test.ts +152 -7
  6. package/__tests__/firmware-update/check-all-firmware-release.test.ts +86 -0
  7. package/__tests__/firmware-update/firmware-artifact-source.test.ts +119 -0
  8. package/__tests__/firmware-update/firmware-host-binding.test.ts +70 -0
  9. package/__tests__/firmware-update/firmware-update-capabilities.test.ts +13 -0
  10. package/__tests__/firmware-update/firmware-update-plan.test.ts +326 -0
  11. package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +213 -0
  12. package/__tests__/firmware-update/firmware-update-v2-download-before-boot.test.ts +39 -30
  13. package/__tests__/firmware-update/firmware-upload-source.test.ts +70 -0
  14. package/__tests__/logBlockEvent.test.ts +17 -0
  15. package/__tests__/open-wallet-session.test.ts +311 -12
  16. package/__tests__/pro2Wallpaper.test.ts +7 -18
  17. package/__tests__/protocol-v2-unlock-policy.test.ts +45 -0
  18. package/__tests__/protocol-v2.test.ts +685 -177
  19. package/__tests__/public-device-state-api.test.ts +69 -62
  20. package/__tests__/public-pro2-api-boundary.test.ts +8 -34
  21. package/dist/api/BaseMethod.d.ts +2 -1
  22. package/dist/api/BaseMethod.d.ts.map +1 -1
  23. package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
  24. package/dist/api/ClearSessionCache.d.ts.map +1 -1
  25. package/dist/api/FirmwareUpdate.d.ts.map +1 -1
  26. package/dist/api/FirmwareUpdateV2.d.ts +11 -0
  27. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  28. package/dist/api/FirmwareUpdateV3.d.ts +7 -3
  29. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  30. package/dist/api/FirmwareUpdateV4.d.ts +29 -10
  31. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  32. package/dist/api/GetPassphraseState.d.ts.map +1 -1
  33. package/dist/api/OpenWalletSession.d.ts.map +1 -1
  34. package/dist/api/conflux/ConfluxGetAddress.d.ts +1 -0
  35. package/dist/api/conflux/ConfluxGetAddress.d.ts.map +1 -1
  36. package/dist/api/conflux/ConfluxSignMessage.d.ts +1 -0
  37. package/dist/api/conflux/ConfluxSignMessage.d.ts.map +1 -1
  38. package/dist/api/conflux/ConfluxSignMessageCIP23.d.ts +1 -0
  39. package/dist/api/conflux/ConfluxSignMessageCIP23.d.ts.map +1 -1
  40. package/dist/api/conflux/ConfluxSignTransaction.d.ts +1 -0
  41. package/dist/api/conflux/ConfluxSignTransaction.d.ts.map +1 -1
  42. package/dist/api/device/DeviceUnlock.d.ts +1 -1
  43. package/dist/api/device/DeviceUnlock.d.ts.map +1 -1
  44. package/dist/api/device/DeviceUpdateBootloader.d.ts +5 -1
  45. package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
  46. package/dist/api/device/DeviceWipe.d.ts.map +1 -1
  47. package/dist/api/firmware/FirmwareArtifactSource.d.ts +25 -0
  48. package/dist/api/firmware/FirmwareArtifactSource.d.ts.map +1 -0
  49. package/dist/api/firmware/FirmwareHostBinding.d.ts +6 -0
  50. package/dist/api/firmware/FirmwareHostBinding.d.ts.map +1 -0
  51. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +7 -0
  52. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  53. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts +3 -0
  54. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts.map +1 -0
  55. package/dist/api/firmware/FirmwareUpdatePlan.d.ts +23 -0
  56. package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +1 -0
  57. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts +30 -0
  58. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +1 -0
  59. package/dist/api/firmware/getBinary.d.ts +7 -0
  60. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  61. package/dist/api/firmware/updateBootloader.d.ts +2 -0
  62. package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
  63. package/dist/api/firmware/uploadFirmware.d.ts +9 -1
  64. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  65. package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -1
  66. package/dist/api/index.d.ts +0 -17
  67. package/dist/api/index.d.ts.map +1 -1
  68. package/dist/api/nexa/NexaSignTransaction.d.ts +1 -0
  69. package/dist/api/nexa/NexaSignTransaction.d.ts.map +1 -1
  70. package/dist/api/stellar/StellarSignTransaction.d.ts +1 -1
  71. package/dist/core/index.d.ts.map +1 -1
  72. package/dist/data-manager/DataManager.d.ts +1 -0
  73. package/dist/data-manager/DataManager.d.ts.map +1 -1
  74. package/dist/data-manager/connectSettings.d.ts.map +1 -1
  75. package/dist/device/Device.d.ts +3 -2
  76. package/dist/device/Device.d.ts.map +1 -1
  77. package/dist/device/DeviceCommands.d.ts.map +1 -1
  78. package/dist/device/DeviceStateMapper.d.ts.map +1 -1
  79. package/dist/device/DeviceStateProjector.d.ts.map +1 -1
  80. package/dist/events/device.d.ts +1 -0
  81. package/dist/events/device.d.ts.map +1 -1
  82. package/dist/events/logBlockEvent.d.ts.map +1 -1
  83. package/dist/events/ui-request.d.ts +1 -0
  84. package/dist/events/ui-request.d.ts.map +1 -1
  85. package/dist/index.d.ts +273 -230
  86. package/dist/index.d.ts.map +1 -1
  87. package/dist/index.js +3060 -1676
  88. package/dist/inject.d.ts.map +1 -1
  89. package/dist/lowLevelInject.d.ts.map +1 -1
  90. package/dist/protocols/protocol-v2/settingsUnlockPolicy.d.ts.map +1 -1
  91. package/dist/protocols/protocol-v2/unlockPolicy.d.ts +4 -0
  92. package/dist/protocols/protocol-v2/unlockPolicy.d.ts.map +1 -0
  93. package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +1 -1
  94. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  95. package/dist/topLevelInject.d.ts.map +1 -1
  96. package/dist/types/api/checkAllFirmwareRelease.d.ts +3 -0
  97. package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
  98. package/dist/types/api/deviceUnlock.d.ts +2 -2
  99. package/dist/types/api/deviceUnlock.d.ts.map +1 -1
  100. package/dist/types/api/deviceUpdateBootloader.d.ts +6 -0
  101. package/dist/types/api/deviceUpdateBootloader.d.ts.map +1 -1
  102. package/dist/types/api/export.d.ts +4 -1
  103. package/dist/types/api/export.d.ts.map +1 -1
  104. package/dist/types/api/firmwareUpdate.d.ts +69 -0
  105. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  106. package/dist/types/api/firmwareUpdateCapabilities.d.ts +9 -0
  107. package/dist/types/api/firmwareUpdateCapabilities.d.ts.map +1 -0
  108. package/dist/types/api/firmwareUpdatePlan.d.ts +27 -0
  109. package/dist/types/api/firmwareUpdatePlan.d.ts.map +1 -0
  110. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts +42 -0
  111. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts.map +1 -0
  112. package/dist/types/api/getPassphraseState.d.ts.map +1 -1
  113. package/dist/types/api/index.d.ts +10 -19
  114. package/dist/types/api/index.d.ts.map +1 -1
  115. package/dist/types/api/openWalletSession.d.ts +1 -0
  116. package/dist/types/api/openWalletSession.d.ts.map +1 -1
  117. package/dist/types/api/protocolV2.d.ts +3 -76
  118. package/dist/types/api/protocolV2.d.ts.map +1 -1
  119. package/dist/types/api/sessionCache.d.ts +4 -1
  120. package/dist/types/api/sessionCache.d.ts.map +1 -1
  121. package/dist/types/settings.d.ts +13 -0
  122. package/dist/types/settings.d.ts.map +1 -1
  123. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  124. package/dist/utils/deviceInfoUtils.d.ts.map +1 -1
  125. package/dist/utils/patch.d.ts +1 -1
  126. package/dist/utils/patch.d.ts.map +1 -1
  127. package/package.json +4 -4
  128. package/src/api/BaseMethod.ts +8 -3
  129. package/src/api/CheckAllFirmwareRelease.ts +23 -3
  130. package/src/api/ClearSessionCache.ts +4 -0
  131. package/src/api/FirmwareUpdate.ts +8 -1
  132. package/src/api/FirmwareUpdateV2.ts +292 -120
  133. package/src/api/FirmwareUpdateV3.ts +256 -55
  134. package/src/api/FirmwareUpdateV4.ts +821 -331
  135. package/src/api/GetPassphraseState.ts +11 -5
  136. package/src/api/OpenWalletSession.ts +74 -25
  137. package/src/api/UploadPortfolio.ts +1 -1
  138. package/src/api/conflux/ConfluxGetAddress.ts +2 -0
  139. package/src/api/conflux/ConfluxSignMessage.ts +2 -0
  140. package/src/api/conflux/ConfluxSignMessageCIP23.ts +2 -0
  141. package/src/api/conflux/ConfluxSignTransaction.ts +2 -0
  142. package/src/api/device/DeviceChangePin.ts +1 -1
  143. package/src/api/device/DeviceUnlock.ts +1 -2
  144. package/src/api/device/DeviceUpdateBootloader.ts +122 -6
  145. package/src/api/device/DeviceWipe.ts +3 -1
  146. package/src/api/firmware/FirmwareArtifactSource.ts +278 -0
  147. package/src/api/firmware/FirmwareHostBinding.ts +100 -0
  148. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +50 -0
  149. package/src/api/firmware/FirmwareUpdateCapabilities.ts +9 -0
  150. package/src/api/firmware/FirmwareUpdatePlan.ts +621 -0
  151. package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +392 -0
  152. package/src/api/firmware/updateBootloader.ts +19 -4
  153. package/src/api/firmware/uploadFirmware.ts +140 -22
  154. package/src/api/helpers/protocolV2FileWrite.ts +6 -0
  155. package/src/api/index.ts +0 -18
  156. package/src/api/nexa/NexaSignTransaction.ts +2 -0
  157. package/src/api/protocol-v2/DeviceUploadWallpaper.ts +1 -1
  158. package/src/core/index.ts +1 -0
  159. package/src/data/messages/messages-protocol-v2.json +35 -1
  160. package/src/data-manager/DataManager.ts +33 -33
  161. package/src/data-manager/connectSettings.ts +11 -0
  162. package/src/device/Device.ts +80 -67
  163. package/src/device/DeviceCommands.ts +76 -159
  164. package/src/device/DeviceStateMapper.ts +18 -1
  165. package/src/device/DeviceStateProjector.ts +7 -1
  166. package/src/events/device.ts +1 -0
  167. package/src/events/logBlockEvent.ts +1 -14
  168. package/src/events/ui-request.ts +1 -0
  169. package/src/index.ts +5 -0
  170. package/src/inject.ts +22 -25
  171. package/src/lowLevelInject.ts +5 -1
  172. package/src/protocols/protocol-v2/settingsUnlockPolicy.ts +2 -3
  173. package/src/protocols/protocol-v2/unlockPolicy.ts +105 -0
  174. package/src/protocols/protocol-v2/unlockRetry.ts +1 -3
  175. package/src/protocols/protocol-v2/walletSession.ts +86 -9
  176. package/src/topLevelInject.ts +5 -1
  177. package/src/types/api/checkAllFirmwareRelease.ts +3 -0
  178. package/src/types/api/deviceUnlock.ts +2 -5
  179. package/src/types/api/deviceUpdateBootloader.ts +6 -0
  180. package/src/types/api/export.ts +18 -0
  181. package/src/types/api/firmwareUpdate.ts +76 -0
  182. package/src/types/api/firmwareUpdateCapabilities.ts +9 -0
  183. package/src/types/api/firmwareUpdatePlan.ts +36 -0
  184. package/src/types/api/firmwareUpdatePreparedPlan.ts +53 -0
  185. package/src/types/api/getPassphraseState.ts +4 -3
  186. package/src/types/api/index.ts +14 -34
  187. package/src/types/api/openWalletSession.ts +2 -0
  188. package/src/types/api/protocolV2.ts +3 -156
  189. package/src/types/api/sessionCache.ts +9 -4
  190. package/src/types/settings.ts +13 -0
  191. package/src/utils/deviceFeaturesUtils.ts +5 -1
  192. package/src/utils/deviceInfoUtils.ts +3 -16
@@ -9,6 +9,7 @@ import {
9
9
  getDeviceBLEFirmwareVersion,
10
10
  getDeviceBootloaderVersion,
11
11
  getDeviceFirmwareVersion,
12
+ getDeviceUUID,
12
13
  getLogger,
13
14
  } from '../utils';
14
15
  import { getBinary, getSysResourceBinary } from './firmware/getBinary';
@@ -17,10 +18,21 @@ import { FirmwareUpdateBaseMethod } from './firmware/FirmwareUpdateBaseMethod';
17
18
  import { DevicePool } from '../device/DevicePool';
18
19
  import { DEVICE } from '../events';
19
20
  import { buildProtocolV1FeaturesPayload } from '../deviceProfile';
20
-
21
- import type { FirmwareUpdateV3Params } from '../types/api/firmwareUpdate';
21
+ import { openFirmwareByteSource } from './firmware/FirmwareArtifactSource';
22
+ import { resolveFirmwareUpdateHostBinding } from './firmware/FirmwareHostBinding';
23
+ import {
24
+ assertFirmwareUpdatePreparedPlanDeviceIdentity,
25
+ assertFirmwareUpdatePreparedPlanBinding,
26
+ getFirmwareUpdateResourceName,
27
+ } from './firmware/FirmwareUpdatePreparedPlan';
28
+
29
+ import type {
30
+ FirmwareArtifactReference,
31
+ FirmwareUpdateV3Params,
32
+ } from '../types/api/firmwareUpdate';
22
33
  import type { Deferred, EFirmwareType } from '@onekeyfe/hd-shared';
23
34
  import type { TypedResponseMessage } from '../device/DeviceCommands';
35
+ import type { FirmwareByteSource } from './firmware/FirmwareArtifactSource';
24
36
 
25
37
  const Log = getLogger(LoggerNames.Method);
26
38
 
@@ -42,6 +54,8 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
42
54
 
43
55
  private isSwitchFirmware = false;
44
56
 
57
+ private artifactSources: FirmwareByteSource[] = [];
58
+
45
59
  init() {
46
60
  this.allowDeviceMode = [UI_REQUEST.BOOTLOADER, UI_REQUEST.NOT_INITIALIZE];
47
61
  this.requireDeviceMode = [];
@@ -62,8 +76,54 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
62
76
  { name: 'firmwareType', type: 'string' },
63
77
  { name: 'platform', type: 'string' },
64
78
  ]);
79
+ const hostBinding =
80
+ payload.artifacts || payload.preparedPlan
81
+ ? resolveFirmwareUpdateHostBinding(payload.hostBindingGeneration)
82
+ : undefined;
83
+ if (hostBinding) {
84
+ assertFirmwareUpdatePreparedPlanBinding({
85
+ preparedPlan: payload.preparedPlan,
86
+ executor: 'v3',
87
+ platform: payload.platform,
88
+ scopeTargets: ['bootloader', 'firmware', 'resource', 'ble'],
89
+ bindings: [
90
+ ...(payload.artifacts?.bootloader
91
+ ? [
92
+ {
93
+ target: 'bootloader' as const,
94
+ artifact: payload.artifacts.bootloader,
95
+ },
96
+ ]
97
+ : []),
98
+ ...(payload.artifacts?.firmware
99
+ ? [
100
+ {
101
+ target: 'firmware' as const,
102
+ artifact: payload.artifacts.firmware,
103
+ },
104
+ ]
105
+ : []),
106
+ ...(payload.artifacts?.ble
107
+ ? [
108
+ {
109
+ target: 'ble' as const,
110
+ artifact: payload.artifacts.ble,
111
+ },
112
+ ]
113
+ : []),
114
+ ...(payload.artifacts?.resourceEntries ?? []).map(
115
+ (entry: { entryName: string; artifact: FirmwareArtifactReference }) => ({
116
+ target: 'resource' as const,
117
+ entryName: entry.entryName,
118
+ artifact: entry.artifact,
119
+ })
120
+ ),
121
+ ],
122
+ });
123
+ }
65
124
 
66
125
  this.params = {
126
+ preparedPlan: payload.preparedPlan,
67
127
  bleBinary: payload.bleBinary,
68
128
  firmwareBinary: payload.firmwareBinary,
69
129
  forcedUpdateRes: payload.forcedUpdateRes,
@@ -74,6 +134,8 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
74
134
  resourceBinary: payload.resourceBinary,
75
135
  firmwareType: payload.firmwareType,
76
136
  platform: payload.platform,
137
+ artifactReader: hostBinding?.artifactReader ?? payload.artifactReader,
138
+ artifacts: payload.artifacts,
77
139
  };
78
140
  }
79
141
 
@@ -103,39 +165,53 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
103
165
  if (!features) {
104
166
  throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'Device features not available');
105
167
  }
168
+ if (this.params.preparedPlan) {
169
+ assertFirmwareUpdatePreparedPlanDeviceIdentity({
170
+ preparedPlan: this.params.preparedPlan,
171
+ deviceIdentity: getDeviceUUID(features) || undefined,
172
+ });
173
+ }
106
174
 
107
175
  const deviceFirmwareType = device.getCurrentFirmwareType();
108
176
  const firmwareType = this.params.firmwareType ?? deviceFirmwareType;
109
177
  this.isSwitchFirmware = firmwareType !== deviceFirmwareType;
110
178
 
111
179
  let resourceBinary: ArrayBuffer | null = null;
112
- let fwBinaryMap: { fileName: string; binary: ArrayBuffer }[] = [];
113
- let bootloaderBinary: ArrayBuffer | null = null;
180
+ let resourceEntries: Array<{ entryName: string; source: FirmwareByteSource }> = [];
181
+ let fwSources: Array<{ fileName: string; source: FirmwareByteSource }> = [];
182
+ let bootloaderSource: FirmwareByteSource | null = null;
114
183
  try {
115
184
  this.postTipMessage(FirmwareUpdateTipMessage.StartDownloadFirmware);
116
- resourceBinary = await this.prepareResourceBinary(firmwareType);
117
- fwBinaryMap = await this.prepareFirmwareAndBleBinary(firmwareType);
118
- bootloaderBinary = await this.prepareBootloaderBinary(firmwareType);
185
+ const resourceInput = await this.prepareResourceInput(firmwareType);
186
+ resourceBinary = resourceInput.resourceBinary;
187
+ resourceEntries = resourceInput.resourceEntries;
188
+ fwSources = await this.prepareFirmwareAndBleSources(firmwareType);
189
+ bootloaderSource = await this.prepareBootloaderSource(firmwareType);
119
190
  this.postTipMessage(FirmwareUpdateTipMessage.FinishDownloadFirmware);
120
191
  } catch (err) {
192
+ await this.closeArtifactSources();
121
193
  throw ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateDownloadFailed, err.message ?? err);
122
194
  }
123
195
 
124
- if (!bootloaderBinary && fwBinaryMap.length === 0) {
196
+ if (!bootloaderSource && fwSources.length === 0) {
197
+ await this.closeArtifactSources();
125
198
  throw ERRORS.TypedError(
126
199
  HardwareErrorCode.FirmwareUpdateDownloadFailed,
127
200
  'No firmware to update'
128
201
  );
129
202
  }
130
203
 
131
- await this.enterBootloaderMode();
132
-
133
- const updateResult = await this.executeUpdate({
134
- resourceBinary,
135
- fwBinaryMap,
136
- bootloaderBinary,
137
- });
138
- return updateResult;
204
+ try {
205
+ await this.enterBootloaderMode();
206
+ return await this.executeUpdate({
207
+ resourceBinary,
208
+ resourceEntries,
209
+ fwSources,
210
+ bootloaderSource,
211
+ });
212
+ } finally {
213
+ await this.closeArtifactSources();
214
+ }
139
215
  }
140
216
 
141
217
  private validateDeviceAndVersion(deviceType: EDeviceType, bootloaderVersion: string) {
@@ -155,12 +231,81 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
155
231
  }
156
232
  }
157
233
 
158
- private async prepareResourceBinary(firmwareType: EFirmwareType) {
234
+ private async openArtifactSource(
235
+ binary: ArrayBuffer | undefined,
236
+ artifact: FirmwareArtifactReference | undefined
237
+ ) {
238
+ const source = await openFirmwareByteSource({
239
+ binary,
240
+ artifact,
241
+ reader: this.params.artifactReader,
242
+ });
243
+ if (source) {
244
+ this.artifactSources.push(source);
245
+ }
246
+ return source;
247
+ }
248
+
249
+ private async closeArtifactSources() {
250
+ const sources = this.artifactSources.splice(0);
251
+ await Promise.all(sources.map(source => source.close().catch(() => undefined)));
252
+ }
253
+
254
+ private assertSdkDownloadAllowed(artifactName: string) {
255
+ if (
256
+ this.params.artifactReader ||
257
+ DataManager.getSettings('firmwareManifestMode') === 'external-only'
258
+ ) {
259
+ throw ERRORS.TypedError(
260
+ HardwareErrorCode.RuntimeError,
261
+ `${artifactName} must be prepared by the external firmware host`,
262
+ {
263
+ firmwareUpdateCode: 'FirmwareArtifactsNotPrepared',
264
+ artifactName,
265
+ }
266
+ );
267
+ }
268
+ }
269
+
270
+ private async prepareResourceInput(firmwareType: EFirmwareType) {
271
+ const preparedEntries = this.params.artifacts?.resourceEntries;
272
+ if (preparedEntries?.length && this.params.resourceBinary) {
273
+ throw ERRORS.TypedError(
274
+ HardwareErrorCode.RuntimeError,
275
+ 'Firmware resource input must not contain both archive binary and prepared entries'
276
+ );
277
+ }
278
+ if (preparedEntries?.length) {
279
+ const resourceEntries: Array<{ entryName: string; source: FirmwareByteSource }> = [];
280
+ for (const entry of preparedEntries) {
281
+ const resourceName = getFirmwareUpdateResourceName(entry.entryName);
282
+ const source = await this.openArtifactSource(undefined, entry.artifact);
283
+ if (!source) {
284
+ throw ERRORS.TypedError(
285
+ HardwareErrorCode.RuntimeError,
286
+ `Firmware resource entry ${entry.entryName} is not prepared`
287
+ );
288
+ }
289
+ resourceEntries.push({ entryName: resourceName, source });
290
+ }
291
+ return {
292
+ resourceBinary: null,
293
+ resourceEntries,
294
+ };
295
+ }
159
296
  if (this.params.resourceBinary) {
160
- return this.params.resourceBinary;
297
+ return {
298
+ resourceBinary: this.params.resourceBinary,
299
+ resourceEntries: [],
300
+ };
161
301
  }
162
302
  const { features } = this.device;
163
- if (!features) return null;
303
+ if (!features) {
304
+ return {
305
+ resourceBinary: null,
306
+ resourceEntries: [],
307
+ };
308
+ }
164
309
  const resourceUrl = DataManager.getSysResourcesLatestRelease({
165
310
  features,
166
311
  forcedUpdateRes: this.params.forcedUpdateRes,
@@ -168,16 +313,27 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
168
313
  });
169
314
 
170
315
  if (resourceUrl) {
171
- const resource = (await getSysResourceBinary(resourceUrl)).binary;
172
- return resource;
316
+ this.assertSdkDownloadAllowed('resource');
317
+ return {
318
+ resourceBinary: (await getSysResourceBinary(resourceUrl)).binary,
319
+ resourceEntries: [],
320
+ };
173
321
  }
174
322
  Log.warn('No resource url found');
175
- return null;
323
+ return {
324
+ resourceBinary: null,
325
+ resourceEntries: [],
326
+ };
176
327
  }
177
328
 
178
- private async prepareBootloaderBinary(firmwareType: EFirmwareType): Promise<ArrayBuffer | null> {
179
- if (this.params.bootloaderBinary) {
180
- return this.params.bootloaderBinary;
329
+ private async prepareBootloaderSource(
330
+ firmwareType: EFirmwareType
331
+ ): Promise<FirmwareByteSource | null> {
332
+ if (this.params.bootloaderBinary || this.params.artifacts?.bootloader) {
333
+ return this.openArtifactSource(
334
+ this.params.bootloaderBinary,
335
+ this.params.artifacts?.bootloader
336
+ );
181
337
  }
182
338
  const { features } = this.device;
183
339
  if (!features) return null;
@@ -185,23 +341,35 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
185
341
  if (this.params.bootloaderVersion) {
186
342
  const bootResourceUrl = DataManager.getBootloaderResource(features, firmwareType);
187
343
  if (bootResourceUrl) {
344
+ this.assertSdkDownloadAllowed('bootloader');
188
345
  const bootBinary = (await getSysResourceBinary(bootResourceUrl)).binary;
189
- return bootBinary;
346
+ return this.openArtifactSource(bootBinary, undefined);
190
347
  }
191
348
  }
192
349
  return null;
193
350
  }
194
351
 
195
- private async prepareFirmwareAndBleBinary(firmwareType: EFirmwareType) {
196
- const fwBinaryMap: { fileName: string; binary: ArrayBuffer }[] = [];
197
- if (this.params.firmwareBinary) {
198
- fwBinaryMap.push({
352
+ private async prepareFirmwareAndBleSources(firmwareType: EFirmwareType) {
353
+ const fwSources: Array<{ fileName: string; source: FirmwareByteSource }> = [];
354
+ if (this.params.firmwareBinary || this.params.artifacts?.firmware) {
355
+ const source = await this.openArtifactSource(
356
+ this.params.firmwareBinary,
357
+ this.params.artifacts?.firmware
358
+ );
359
+ if (!source) {
360
+ throw ERRORS.TypedError(
361
+ HardwareErrorCode.RuntimeError,
362
+ 'Firmware artifact is not prepared'
363
+ );
364
+ }
365
+ fwSources.push({
199
366
  fileName: 'firmware.bin',
200
- binary: this.params.firmwareBinary,
367
+ source,
201
368
  });
202
369
  } else if (this.params.firmwareVersion) {
203
370
  const { features } = this.device;
204
371
  if (features) {
372
+ this.assertSdkDownloadAllowed('firmware');
205
373
  const firmwareBinary = (
206
374
  await getBinary({
207
375
  features,
@@ -211,44 +379,65 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
211
379
  firmwareType,
212
380
  })
213
381
  ).binary;
214
- fwBinaryMap.push({
382
+ const source = await this.openArtifactSource(firmwareBinary, undefined);
383
+ if (!source) {
384
+ throw ERRORS.TypedError(
385
+ HardwareErrorCode.RuntimeError,
386
+ 'Firmware binary is not prepared'
387
+ );
388
+ }
389
+ fwSources.push({
215
390
  fileName: 'firmware.bin',
216
- binary: firmwareBinary,
391
+ source,
217
392
  });
218
393
  }
219
394
  }
220
395
 
221
- if (this.params.bleBinary) {
222
- fwBinaryMap.push({
396
+ if (this.params.bleBinary || this.params.artifacts?.ble) {
397
+ const source = await this.openArtifactSource(
398
+ this.params.bleBinary,
399
+ this.params.artifacts?.ble
400
+ );
401
+ if (!source) {
402
+ throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'BLE artifact is not prepared');
403
+ }
404
+ fwSources.push({
223
405
  fileName: 'ble-firmware.bin',
224
- binary: this.params.bleBinary,
406
+ source,
225
407
  });
226
408
  } else if (this.params.bleVersion) {
227
409
  const { features } = this.device;
228
410
  if (features) {
411
+ this.assertSdkDownloadAllowed('ble');
229
412
  const bleBinary = await getBinary({
230
413
  features,
231
414
  version: this.params.bleVersion,
232
415
  updateType: 'ble',
233
416
  firmwareType,
234
417
  });
235
- fwBinaryMap.push({
418
+ const source = await this.openArtifactSource(bleBinary.binary, undefined);
419
+ if (!source) {
420
+ throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'BLE binary is not prepared');
421
+ }
422
+ fwSources.push({
236
423
  fileName: 'ble-firmware.bin',
237
- binary: bleBinary.binary,
424
+ source,
238
425
  });
239
426
  }
240
427
  }
241
- return fwBinaryMap;
428
+ return fwSources;
242
429
  }
243
430
 
244
431
  private async executeUpdate({
245
432
  resourceBinary,
246
- fwBinaryMap,
247
- bootloaderBinary,
433
+ resourceEntries,
434
+ fwSources,
435
+ bootloaderSource,
248
436
  }: {
249
437
  resourceBinary: ArrayBuffer | null;
250
- fwBinaryMap: { fileName: string; binary: ArrayBuffer }[];
251
- bootloaderBinary: ArrayBuffer | null;
438
+ resourceEntries: Array<{ entryName: string; source: FirmwareByteSource }>;
439
+ fwSources: Array<{ fileName: string; source: FirmwareByteSource }>;
440
+ bootloaderSource: FirmwareByteSource | null;
252
441
  }) {
253
442
  let totalSize = 0;
254
443
  let processedSize = 0;
@@ -256,11 +445,14 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
256
445
  if (resourceBinary) {
257
446
  totalSize += resourceBinary.byteLength;
258
447
  }
259
- for (const fwbinary of fwBinaryMap) {
260
- totalSize += fwbinary.binary.byteLength;
448
+ for (const entry of resourceEntries) {
449
+ totalSize += entry.source.size;
261
450
  }
262
- if (bootloaderBinary) {
263
- totalSize += bootloaderBinary.byteLength;
451
+ for (const firmware of fwSources) {
452
+ totalSize += firmware.source.size;
453
+ }
454
+ if (bootloaderSource) {
455
+ totalSize += bootloaderSource.size;
264
456
  }
265
457
 
266
458
  this.postTipMessage(FirmwareUpdateTipMessage.StartTransferData);
@@ -283,9 +475,18 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
283
475
  }
284
476
  }
285
477
 
286
- if (bootloaderBinary) {
287
- processedSize = await this.emmcCommonUpdateProcess({
288
- payload: bootloaderBinary,
478
+ for (const entry of resourceEntries) {
479
+ processedSize = await this.emmcCommonUpdateProcessFromSource({
480
+ source: entry.source,
481
+ filePath: `0:res/${entry.entryName}`,
482
+ processedSize,
483
+ totalSize,
484
+ });
485
+ }
486
+
487
+ if (bootloaderSource) {
488
+ processedSize = await this.emmcCommonUpdateProcessFromSource({
489
+ source: bootloaderSource,
289
490
  filePath: `0:boot/bootloader.bin`,
290
491
  processedSize,
291
492
  totalSize,
@@ -294,11 +495,11 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
294
495
 
295
496
  await this.createUpdatesFolderIfNotExists(`0:updates/`);
296
497
 
297
- for (const fwbinary of fwBinaryMap) {
298
- if (fwbinary) {
299
- processedSize = await this.emmcCommonUpdateProcess({
300
- payload: fwbinary.binary,
301
- filePath: `0:updates/${fwbinary.fileName}`,
498
+ for (const firmware of fwSources) {
499
+ if (firmware) {
500
+ processedSize = await this.emmcCommonUpdateProcessFromSource({
501
+ source: firmware.source,
502
+ filePath: `0:updates/${firmware.fileName}`,
302
503
  processedSize,
303
504
  totalSize,
304
505
  });