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

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