@onekeyfe/hd-core 1.2.0-alpha.84 → 1.2.0-alpha.85

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 (46) hide show
  1. package/__tests__/check-all-firmware-release-protocol-v2.test.ts +24 -18
  2. package/__tests__/firmware-memory-host.test.ts +105 -0
  3. package/__tests__/firmware-update/firmware-update-plan.test.ts +15 -70
  4. package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +53 -1
  5. package/__tests__/protocol-v2-resources.test.ts +0 -38
  6. package/__tests__/protocol-v2.test.ts +207 -318
  7. package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
  8. package/dist/api/FirmwareUpdateV4.d.ts +1 -2
  9. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  10. package/dist/api/firmware/FirmwareMemoryHost.d.ts +27 -0
  11. package/dist/api/firmware/FirmwareMemoryHost.d.ts.map +1 -0
  12. package/dist/api/firmware/FirmwareUpdatePlan.d.ts +3 -4
  13. package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +1 -1
  14. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +1 -1
  15. package/dist/api/firmware/getBinary.d.ts +0 -1
  16. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  17. package/dist/index.d.ts +24 -64
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +406 -367
  20. package/dist/inject.d.ts.map +1 -1
  21. package/dist/protocols/protocol-v2/resources.d.ts +0 -17
  22. package/dist/protocols/protocol-v2/resources.d.ts.map +1 -1
  23. package/dist/types/api/export.d.ts +0 -1
  24. package/dist/types/api/export.d.ts.map +1 -1
  25. package/dist/types/api/firmwareUpdate.d.ts +0 -10
  26. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  27. package/dist/types/api/index.d.ts +0 -2
  28. package/dist/types/api/index.d.ts.map +1 -1
  29. package/dist/types/settings.d.ts +0 -11
  30. package/dist/types/settings.d.ts.map +1 -1
  31. package/package.json +4 -4
  32. package/src/api/CheckAllFirmwareRelease.ts +2 -6
  33. package/src/api/FirmwareUpdateV4.ts +154 -165
  34. package/src/api/firmware/FirmwareMemoryHost.ts +171 -0
  35. package/src/api/firmware/FirmwareUpdatePlan.ts +28 -48
  36. package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +8 -1
  37. package/src/index.ts +6 -1
  38. package/src/inject.ts +0 -2
  39. package/src/protocols/protocol-v2/resources.ts +0 -47
  40. package/src/types/api/export.ts +0 -4
  41. package/src/types/api/firmwareUpdate.ts +0 -15
  42. package/src/types/api/index.ts +0 -2
  43. package/src/types/settings.ts +0 -20
  44. package/dist/types/api/protocolV2ResourceManifest.d.ts +0 -17
  45. package/dist/types/api/protocolV2ResourceManifest.d.ts.map +0 -1
  46. package/src/types/api/protocolV2ResourceManifest.ts +0 -19
@@ -62,14 +62,6 @@ const release: IFirmwareReleaseInfo = {
62
62
  version: [2, 0, 0],
63
63
  },
64
64
  },
65
- resourceBundles: [
66
- {
67
- name: 'images',
68
- url: 'https://example.com/images.okpkg',
69
- devicePath: 'vol0:/bundles/images/images.okpkg',
70
- version: [1, 0, 0],
71
- },
72
- ],
73
65
  };
74
66
 
75
67
  const resourceSource = {
@@ -152,7 +144,6 @@ describe('checkAllFirmwareRelease Protocol V2 support', () => {
152
144
  test('detects same-version package hotfixes by P1 and P2 payload hash', () => {
153
145
  const packageSet: IFirmwareReleaseInfo = {
154
146
  ...release,
155
- resourceBundles: undefined,
156
147
  installOrder: ['applicationP1', 'applicationP2'],
157
148
  components: {
158
149
  applicationP1: {
@@ -203,7 +194,6 @@ describe('checkAllFirmwareRelease Protocol V2 support', () => {
203
194
  const packageSet: IFirmwareReleaseInfo = {
204
195
  ...release,
205
196
  required: false,
206
- resourceBundles: undefined,
207
197
  installOrder: ['applicationP1'],
208
198
  components: {
209
199
  applicationP1: {
@@ -234,7 +224,6 @@ describe('checkAllFirmwareRelease Protocol V2 support', () => {
234
224
  const packageSet: IFirmwareReleaseInfo = {
235
225
  ...release,
236
226
  required: false,
237
- resourceBundles: undefined,
238
227
  installOrder: ['applicationP1'],
239
228
  components: {
240
229
  applicationP1: {
@@ -265,7 +254,6 @@ describe('checkAllFirmwareRelease Protocol V2 support', () => {
265
254
  test('updates both application packages when normal mode only reports P1', () => {
266
255
  const packageSet: IFirmwareReleaseInfo = {
267
256
  ...release,
268
- resourceBundles: undefined,
269
257
  installOrder: ['applicationP1', 'applicationP2'],
270
258
  components: {
271
259
  applicationP1: {
@@ -339,8 +327,14 @@ describe('checkAllFirmwareRelease Protocol V2 support', () => {
339
327
  artifacts: [
340
328
  { artifactId: 'component:boot', target: 'boot' },
341
329
  { artifactId: 'component:app_v1', target: 'app_v1' },
330
+ {
331
+ artifactId: 'resource:archive',
332
+ target: 'resource',
333
+ role: 'resourceBundle',
334
+ container: 'zip',
335
+ },
342
336
  ],
343
- targetsToUpdate: ['boot', 'app_v1'],
337
+ targetsToUpdate: ['boot', 'app_v1', 'resource'],
344
338
  },
345
339
  firmware: {
346
340
  status: 'required',
@@ -391,8 +385,9 @@ describe('checkAllFirmwareRelease Protocol V2 support', () => {
391
385
  { artifactId: 'component:app_v1', target: 'app_v1' },
392
386
  { artifactId: 'component:se02', target: 'se02' },
393
387
  { artifactId: 'component:se03', target: 'se03' },
388
+ { artifactId: 'resource:archive', target: 'resource', container: 'zip' },
394
389
  ],
395
- targetsToUpdate: ['boot', 'app_v1', 'se02', 'se03'],
390
+ targetsToUpdate: ['boot', 'app_v1', 'se02', 'se03', 'resource'],
396
391
  },
397
392
  });
398
393
  });
@@ -449,8 +444,9 @@ describe('checkAllFirmwareRelease Protocol V2 support', () => {
449
444
  artifacts: [
450
445
  { artifactId: 'component:app_v1', target: 'app_v1' },
451
446
  { artifactId: 'component:coprocessor', target: 'coprocessor' },
447
+ { artifactId: 'resource:archive', target: 'resource', container: 'zip' },
452
448
  ],
453
- targetsToUpdate: ['app_v1', 'coprocessor'],
449
+ targetsToUpdate: ['app_v1', 'coprocessor', 'resource'],
454
450
  },
455
451
  });
456
452
  });
@@ -576,7 +572,7 @@ describe('checkAllFirmwareRelease Protocol V2 support', () => {
576
572
  executor: 'v4',
577
573
  deviceModel: 'neo',
578
574
  platform: 'native',
579
- targetsToUpdate: ['se01', 'se02'],
575
+ targetsToUpdate: ['se01', 'se02', 'resource'],
580
576
  },
581
577
  });
582
578
  });
@@ -611,7 +607,6 @@ describe('checkAllFirmwareRelease Protocol V2 support', () => {
611
607
  const packageSet: IFirmwareReleaseInfo = {
612
608
  ...release,
613
609
  required: false,
614
- resourceBundles: undefined,
615
610
  installOrder: ['applicationP1'],
616
611
  components: {
617
612
  applicationP1: {
@@ -730,7 +725,18 @@ describe('checkAllFirmwareRelease Protocol V2 support', () => {
730
725
  resourceStatus: 'unknown',
731
726
  resourcePreparationRequired: true,
732
727
  targetsToUpdate: ['resource'],
733
- firmwareUpdatePlan: undefined,
728
+ firmwareUpdatePlan: {
729
+ executor: 'v4',
730
+ artifacts: [
731
+ {
732
+ artifactId: 'resource:archive',
733
+ target: 'resource',
734
+ role: 'resourceBundle',
735
+ container: 'zip',
736
+ },
737
+ ],
738
+ targetsToUpdate: ['resource'],
739
+ },
734
740
  });
735
741
  });
736
742
 
@@ -0,0 +1,105 @@
1
+ import { prepareFirmwareUpdateV4MemoryHost } from '../src/api/firmware/FirmwareMemoryHost';
2
+
3
+ import type { CoreApi } from '../src/types/api';
4
+ import type { FirmwareUpdatePlan } from '../src/types/api/firmwareUpdatePlan';
5
+
6
+ describe('prepareFirmwareUpdateV4MemoryHost', () => {
7
+ test('registers artifact-backed component and resource readers', async () => {
8
+ const componentBinary = new Uint8Array([1, 2, 3, 4]).buffer;
9
+ const archiveBinary = new Uint8Array([5, 6]).buffer;
10
+ const manifestBinary = new TextEncoder().encode('{"schema":1}').buffer;
11
+ let artifactReader: Parameters<
12
+ CoreApi['registerFirmwareUpdateHostBinding']
13
+ >[0]['artifactReader'];
14
+ const unregisterFirmwareUpdateHostBinding = jest.fn();
15
+ const sdk = {
16
+ prepareFirmwareUpdatePlan: jest.fn(({ plan, artifacts }) => ({
17
+ schemaVersion: 2,
18
+ preparedPlanDigest: 'b'.repeat(64),
19
+ planDigest: plan.planDigest,
20
+ networkPolicy: 'forbid',
21
+ executor: plan.executor,
22
+ deviceIdentity: plan.deviceIdentity,
23
+ deviceModel: plan.deviceModel,
24
+ firmwareType: plan.firmwareType,
25
+ platform: plan.platform,
26
+ leaseRef: 'test-lease',
27
+ targetsToUpdate: plan.targetsToUpdate,
28
+ artifacts: plan.artifacts.map(planArtifact => {
29
+ const input = artifacts.find(item => item.artifactId === planArtifact.artifactId);
30
+ return {
31
+ ...planArtifact,
32
+ artifact: input?.artifact,
33
+ materializedEntries: input?.materializedEntries,
34
+ };
35
+ }),
36
+ })),
37
+ registerFirmwareUpdateHostBinding: jest.fn(binding => {
38
+ artifactReader = binding.artifactReader;
39
+ return 7;
40
+ }),
41
+ unregisterFirmwareUpdateHostBinding,
42
+ } as unknown as Pick<
43
+ CoreApi,
44
+ | 'prepareFirmwareUpdatePlan'
45
+ | 'registerFirmwareUpdateHostBinding'
46
+ | 'unregisterFirmwareUpdateHostBinding'
47
+ >;
48
+ const plan = {
49
+ schemaVersion: 2,
50
+ planDigest: 'a'.repeat(64),
51
+ executor: 'v4',
52
+ deviceIdentity: 'device-id',
53
+ deviceModel: 'pro2',
54
+ firmwareType: 0,
55
+ platform: 'web',
56
+ targetsToUpdate: ['boot', 'resource'],
57
+ artifacts: [
58
+ {
59
+ artifactId: 'component:boot',
60
+ role: 'component',
61
+ target: 'boot',
62
+ url: 'https://example.com/boot.okpkg',
63
+ container: 'raw',
64
+ },
65
+ {
66
+ artifactId: 'resource:archive',
67
+ role: 'resourceBundle',
68
+ target: 'resource',
69
+ url: 'https://example.com/resource.zip',
70
+ container: 'zip',
71
+ logicalName: 'resource-archive',
72
+ },
73
+ ],
74
+ } as FirmwareUpdatePlan;
75
+
76
+ const host = prepareFirmwareUpdateV4MemoryHost({
77
+ sdk,
78
+ plan,
79
+ artifacts: [
80
+ { artifactId: 'component:boot', binary: componentBinary },
81
+ {
82
+ artifactId: 'resource:archive',
83
+ binary: archiveBinary,
84
+ materializedEntries: [{ entryName: 'manifest.json', binary: manifestBinary }],
85
+ },
86
+ ],
87
+ });
88
+
89
+ expect(host.hostBindingGeneration).toBe(7);
90
+ expect(host.componentArtifacts.boot?.size).toBe(componentBinary.byteLength);
91
+ const opened = await artifactReader!.open({
92
+ artifactRef: host.componentArtifacts.boot!.artifactRef,
93
+ });
94
+ const chunk = await artifactReader!.read({
95
+ readerId: opened.readerId,
96
+ offset: 1,
97
+ length: 2,
98
+ });
99
+ expect(Array.from(new Uint8Array(chunk.data))).toEqual([2, 3]);
100
+ await artifactReader!.close({ readerId: opened.readerId });
101
+
102
+ host.release();
103
+ expect(unregisterFirmwareUpdateHostBinding).toHaveBeenCalledWith(7);
104
+ });
105
+ });
@@ -297,12 +297,6 @@ describe('buildFirmwareUpdatePlan', () => {
297
297
  forceTarget: 'firmware' as const,
298
298
  release: {
299
299
  components: {},
300
- resourceBundles: [
301
- {
302
- name: 'images',
303
- url: 'https://firmware.onekey.so/pro2/images.okpkg',
304
- },
305
- ],
306
300
  },
307
301
  },
308
302
  {
@@ -325,41 +319,21 @@ describe('buildFirmwareUpdatePlan', () => {
325
319
  }
326
320
  );
327
321
 
328
- test.each([
329
- {
330
- forceTarget: 'firmware' as const,
331
- expectedArtifactIds: ['component:app_v1'],
332
- expectedTargets: ['app_v1'],
333
- },
334
- {
335
- forceTarget: 'resource' as const,
336
- expectedArtifactIds: ['resourceBundle:images'],
337
- expectedTargets: ['resource'],
338
- },
339
- ])(
340
- 'limits a Pro2 $forceTarget force to the selected artifact role',
341
- ({ forceTarget, expectedArtifactIds, expectedTargets }) => {
342
- const plan = buildFirmwareUpdatePlan(
343
- createProtocolV2ForceInput([forceTarget], {
344
- components: {
345
- applicationP1: {
346
- target: 'APPLICATION_P1',
347
- url: 'https://firmware.onekey.so/pro2/application-p1.bin',
348
- },
322
+ test('limits a Pro2 firmware force to component artifacts', () => {
323
+ const plan = buildFirmwareUpdatePlan(
324
+ createProtocolV2ForceInput(['firmware'], {
325
+ components: {
326
+ applicationP1: {
327
+ target: 'APPLICATION_P1',
328
+ url: 'https://firmware.onekey.so/pro2/application-p1.bin',
349
329
  },
350
- resourceBundles: [
351
- {
352
- name: 'images',
353
- url: 'https://firmware.onekey.so/pro2/images.okpkg',
354
- },
355
- ],
356
- })
357
- );
330
+ },
331
+ })
332
+ );
358
333
 
359
- expect(plan.artifacts.map(artifact => artifact.artifactId)).toEqual(expectedArtifactIds);
360
- expect(plan.targetsToUpdate).toEqual(expectedTargets);
361
- }
362
- );
334
+ expect(plan.artifacts.map(artifact => artifact.artifactId)).toEqual(['component:app_v1']);
335
+ expect(plan.targetsToUpdate).toEqual(['app_v1']);
336
+ });
363
337
 
364
338
  test('rejects a prepared native plan without a stable device identity', () => {
365
339
  const features = createFeatures({
@@ -384,7 +358,7 @@ describe('buildFirmwareUpdatePlan', () => {
384
358
  ).toThrow('requires a stable device identity');
385
359
  });
386
360
 
387
- test('maps the Pro2 component order and resource bundles without downloading them', () => {
361
+ test('maps the Pro2 component order without downloading artifacts', () => {
388
362
  const plan = buildFirmwareUpdatePlan({
389
363
  features: createFeatures({ deviceType: EDeviceType.Pro2 }),
390
364
  firmwareType: EFirmwareType.Universal,
@@ -409,12 +383,6 @@ describe('buildFirmwareUpdatePlan', () => {
409
383
  url: 'https://firmware.onekey.so/pro2/se01.bin',
410
384
  },
411
385
  },
412
- resourceBundles: [
413
- {
414
- name: 'images',
415
- url: 'https://firmware.onekey.so/pro2/images.okpkg',
416
- },
417
- ],
418
386
  },
419
387
  },
420
388
  ble: noUpdate,
@@ -426,9 +394,8 @@ describe('buildFirmwareUpdatePlan', () => {
426
394
  'component:boot',
427
395
  'component:app_v1',
428
396
  'component:se01',
429
- 'resourceBundle:images',
430
397
  ]);
431
- expect(plan.targetsToUpdate).toEqual(['boot', 'app_v1', 'se01', 'resource']);
398
+ expect(plan.targetsToUpdate).toEqual(['boot', 'app_v1', 'se01']);
432
399
  expect(plan.artifacts[1]).toEqual(
433
400
  expect.objectContaining({
434
401
  expectedSize: 1024,
@@ -477,28 +444,6 @@ describe('buildFirmwareUpdatePlan', () => {
477
444
  },
478
445
  expectedError: 'duplicates target app_v1',
479
446
  },
480
- {
481
- label: 'resource bundle name',
482
- release: {
483
- components: {
484
- application: {
485
- target: 'APPLICATION_P1',
486
- url: 'https://firmware.onekey.so/pro2/application.bin',
487
- },
488
- },
489
- resourceBundles: [
490
- {
491
- name: 'images',
492
- url: 'https://firmware.onekey.so/pro2/images.okpkg',
493
- },
494
- {
495
- name: 'images',
496
- url: 'https://firmware.onekey.so/pro2/images-duplicate.okpkg',
497
- },
498
- ],
499
- },
500
- expectedError: 'duplicates name images',
501
- },
502
447
  ])('rejects a duplicate Pro2 $label', ({ release, expectedError }) => {
503
448
  expect(() =>
504
449
  buildFirmwareUpdatePlan({
@@ -1,6 +1,9 @@
1
1
  import { EDeviceType, EFirmwareType } from '@onekeyfe/hd-shared';
2
2
 
3
- import { buildFirmwareUpdatePlan } from '../../src/api/firmware/FirmwareUpdatePlan';
3
+ import {
4
+ buildFirmwareUpdatePlan,
5
+ digestFirmwareUpdateContract,
6
+ } from '../../src/api/firmware/FirmwareUpdatePlan';
4
7
  import {
5
8
  assertFirmwareUpdatePreparedPlanBinding,
6
9
  assertFirmwareUpdatePreparedPlanDeviceIdentity,
@@ -209,6 +212,55 @@ describe('FirmwareUpdatePreparedPlan', () => {
209
212
  ).toThrow('entry name is invalid');
210
213
  });
211
214
 
215
+ test('rejects duplicate materialized entry names during standalone validation', () => {
216
+ const plan = buildPlan();
217
+ const prepared = prepareFirmwareUpdatePlan({
218
+ plan,
219
+ leaseRef: 'fwlease:00000000-0000-4000-8000-000000000001',
220
+ artifacts: [
221
+ {
222
+ artifactId: 'firmware',
223
+ artifact: artifact('a'.repeat(64), 4),
224
+ },
225
+ {
226
+ artifactId: 'resource',
227
+ artifact: artifact('b'.repeat(64), 8),
228
+ materializedEntries: [
229
+ {
230
+ entryName: 'resource/index.bin',
231
+ artifact: artifact('c'.repeat(64), 2),
232
+ },
233
+ ],
234
+ },
235
+ ],
236
+ });
237
+ const resourceArtifact = prepared.artifacts.find(item => item.target === 'resource')!;
238
+ const tamperedWithoutDigest = {
239
+ ...prepared,
240
+ artifacts: prepared.artifacts.map(item =>
241
+ item === resourceArtifact
242
+ ? {
243
+ ...item,
244
+ materializedEntries: [
245
+ ...resourceArtifact.materializedEntries!,
246
+ {
247
+ entryName: 'other/index.bin',
248
+ artifact: artifact('d'.repeat(64), 2),
249
+ },
250
+ ],
251
+ }
252
+ : item
253
+ ),
254
+ };
255
+ const { preparedPlanDigest: _preparedPlanDigest, ...digestInput } = tamperedWithoutDigest;
256
+ const tampered = {
257
+ ...tamperedWithoutDigest,
258
+ preparedPlanDigest: digestFirmwareUpdateContract(digestInput),
259
+ };
260
+
261
+ expect(() => validateFirmwareUpdatePreparedPlan(tampered)).toThrow('duplicate entry names');
262
+ });
263
+
212
264
  test('rejects unknown fields even when a caller presents a plan-shaped object', () => {
213
265
  const plan = buildPlan();
214
266
  expect(() =>
@@ -4,10 +4,8 @@ import { HardwareErrorCode } from '@onekeyfe/hd-shared';
4
4
 
5
5
  import { DataManager } from '../src/data-manager';
6
6
  import {
7
- isProtocolV2ResourceFileValid,
8
7
  parseProtocolV2ResourceManifest,
9
8
  parseProtocolV2Resources,
10
- prepareProtocolV2ResourceFiles,
11
9
  } from '../src/protocols/protocol-v2/resources';
12
10
 
13
11
  import type { ConnectSettings, RemoteConfigResponse } from '../src/types';
@@ -151,42 +149,6 @@ describe('Pro2 resource configuration', () => {
151
149
  ).toThrow('archive_path');
152
150
  });
153
151
 
154
- test('verifies selected manifest files and preserves manifest device paths', () => {
155
- const prepared = prepareProtocolV2ResourceFiles({
156
- manifest: resourceManifest,
157
- files: manifestFiles.map(file => ({
158
- archivePath: file.archive_path,
159
- binary: file.binary,
160
- })),
161
- targetsToUpdate: ['resource'],
162
- });
163
- expect(prepared.map(file => file.devicePath)).toEqual(
164
- manifestFiles.map(file => file.device_path)
165
- );
166
- expect(() =>
167
- prepareProtocolV2ResourceFiles({
168
- manifest: resourceManifest,
169
- files: manifestFiles.map((file, index) => ({
170
- archivePath: file.archive_path,
171
- binary: index === 0 ? new Uint8Array([0]).buffer : file.binary,
172
- })),
173
- targetsToUpdate: ['resource'],
174
- })
175
- ).toThrow('verification failed');
176
- });
177
-
178
- test('verifies both full file size and SHA-256 before transfer', () => {
179
- const bytes = new Uint8Array([1, 2, 3]);
180
- const binary = bytes.buffer;
181
- const identity = { size: bytes.byteLength, fileHash: bytesToHex(sha256(bytes)) };
182
-
183
- expect(isProtocolV2ResourceFileValid(binary, identity)).toBe(true);
184
- expect(isProtocolV2ResourceFileValid(binary, { ...identity, size: 4 })).toBe(false);
185
- expect(isProtocolV2ResourceFileValid(binary, { ...identity, fileHash: '0'.repeat(64) })).toBe(
186
- false
187
- );
188
- });
189
-
190
152
  test('applies a validated pre-release config and exposes its archive source', async () => {
191
153
  const configFetcher = jest.fn().mockResolvedValue(createRemoteConfig());
192
154