@onekeyfe/hd-core 1.2.0-alpha.87 → 1.2.0-alpha.89

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.
@@ -6,7 +6,6 @@ import type { CoreApi } from '../../types/api';
6
6
  import type {
7
7
  FirmwareArtifactReader,
8
8
  FirmwareArtifactReference,
9
- FirmwareUpdateV4Target,
10
9
  } from '../../types/api/firmwareUpdate';
11
10
  import type { FirmwareUpdatePlan } from '../../types/api/firmwareUpdatePlan';
12
11
 
@@ -24,33 +23,11 @@ export type FirmwareMemoryArtifact = {
24
23
  export type FirmwareUpdateV4MemoryHost = {
25
24
  preparedPlan: ReturnType<CoreApi['prepareFirmwareUpdatePlan']>;
26
25
  hostBindingGeneration: number;
27
- targetsToUpdate: FirmwareUpdateV4Target[];
28
- expectedDeviceId: string;
29
- expectedTargetVersions: Partial<Record<FirmwareUpdateV4Target, string>>;
30
- componentArtifacts: Partial<
31
- Record<Exclude<FirmwareUpdateV4Target, 'resource'>, FirmwareArtifactReference>
32
- >;
33
26
  release: () => void;
34
27
  };
35
28
 
36
29
  let memoryHostSequence = 0;
37
30
 
38
- const FIRMWARE_UPDATE_V4_COMPONENT_TARGETS = new Set<Exclude<FirmwareUpdateV4Target, 'resource'>>([
39
- 'boot',
40
- 'app_v1',
41
- 'app_v2',
42
- 'coprocessor',
43
- 'se01',
44
- 'se02',
45
- 'se03',
46
- 'se04',
47
- ]);
48
-
49
- const isFirmwareUpdateV4ComponentTarget = (
50
- target: string
51
- ): target is Exclude<FirmwareUpdateV4Target, 'resource'> =>
52
- FIRMWARE_UPDATE_V4_COMPONENT_TARGETS.has(target as Exclude<FirmwareUpdateV4Target, 'resource'>);
53
-
54
31
  const createReference = (binary: ArrayBuffer, prefix: string): FirmwareArtifactReference => {
55
32
  const digest = bytesToHex(sha256(new Uint8Array(binary)));
56
33
  return {
@@ -84,14 +61,19 @@ export function prepareFirmwareUpdateV4MemoryHost({
84
61
  const hostId = `${Date.now()}:${memoryHostSequence}`;
85
62
  const binaries = new Map<string, Uint8Array>();
86
63
  const inputs = artifacts.map((input, artifactIndex) => {
87
- const artifact = createReference(input.binary, `${hostId}:artifact:${artifactIndex}`);
88
- binaries.set(artifact.artifactRef, new Uint8Array(input.binary));
64
+ const artifactBinary = new Uint8Array(input.binary).slice();
65
+ const artifact = createReference(
66
+ artifactBinary.buffer as ArrayBuffer,
67
+ `${hostId}:artifact:${artifactIndex}`
68
+ );
69
+ binaries.set(artifact.artifactRef, artifactBinary);
89
70
  const materializedEntries = input.materializedEntries?.map((entry, entryIndex) => {
71
+ const entryBinary = new Uint8Array(entry.binary).slice();
90
72
  const entryArtifact = createReference(
91
- entry.binary,
73
+ entryBinary.buffer as ArrayBuffer,
92
74
  `${hostId}:entry:${artifactIndex}:${entryIndex}`
93
75
  );
94
- binaries.set(entryArtifact.artifactRef, new Uint8Array(entry.binary));
76
+ binaries.set(entryArtifact.artifactRef, entryBinary);
95
77
  return {
96
78
  entryName: entry.entryName,
97
79
  artifact: entryArtifact,
@@ -148,23 +130,9 @@ export function prepareFirmwareUpdateV4MemoryHost({
148
130
  artifactReader,
149
131
  preparedPlanDigest: preparedPlan.preparedPlanDigest,
150
132
  });
151
- const componentArtifacts: FirmwareUpdateV4MemoryHost['componentArtifacts'] = {};
152
- const expectedTargetVersions: FirmwareUpdateV4MemoryHost['expectedTargetVersions'] = {};
153
- for (const artifact of preparedPlan.artifacts) {
154
- if (artifact.role === 'component' && isFirmwareUpdateV4ComponentTarget(artifact.target)) {
155
- componentArtifacts[artifact.target] = artifact.artifact;
156
- }
157
- if (artifact.targetVersion) {
158
- expectedTargetVersions[artifact.target as FirmwareUpdateV4Target] = artifact.targetVersion;
159
- }
160
- }
161
133
  return {
162
134
  preparedPlan,
163
135
  hostBindingGeneration,
164
- targetsToUpdate: [...preparedPlan.targetsToUpdate] as FirmwareUpdateV4Target[],
165
- expectedDeviceId: preparedPlan.deviceIdentity,
166
- expectedTargetVersions,
167
- componentArtifacts,
168
136
  release: () => {
169
137
  sdk.unregisterFirmwareUpdateHostBinding(hostBindingGeneration);
170
138
  readers.clear();
@@ -504,6 +504,13 @@ const buildProtocolV2Artifacts = (
504
504
  );
505
505
  }
506
506
  componentTargetSet.add(target);
507
+ const integrity = asIntegrity({
508
+ size: component.expectedSize,
509
+ sha256: component.fingerprint,
510
+ });
511
+ if (integrity.expectedSize === undefined || integrity.expectedSha256 === undefined) {
512
+ planError(`Protocol V2 component ${key} integrity metadata is invalid`);
513
+ }
507
514
  artifacts.push({
508
515
  artifactId: `component:${target}`,
509
516
  role: 'component',
@@ -511,10 +518,7 @@ const buildProtocolV2Artifacts = (
511
518
  url: assertArtifactUrl(component.url, `Protocol V2 component ${key}`),
512
519
  container: 'raw',
513
520
  logicalName: key,
514
- ...asIntegrity({
515
- size: component.expectedSize,
516
- sha256: component.fingerprint,
517
- }),
521
+ ...integrity,
518
522
  ...(asVersion(component.version) ? { targetVersion: asVersion(component.version) } : {}),
519
523
  });
520
524
  targets.push(target);
@@ -632,6 +636,47 @@ const finalizeFirmwareUpdatePlan = ({
632
636
  });
633
637
  };
634
638
 
639
+ export type ProtocolV2LocalFirmwareUpdatePlanArtifact = {
640
+ artifactId: string;
641
+ target: Exclude<FirmwareUpdateV4Target, 'boot_resources'>;
642
+ container: 'raw' | 'zip';
643
+ logicalName: string;
644
+ expectedSize: number;
645
+ expectedSha256: string;
646
+ targetVersion?: string;
647
+ };
648
+
649
+ export const buildProtocolV2LocalFirmwareUpdatePlan = ({
650
+ features,
651
+ firmwareType,
652
+ platform,
653
+ artifacts,
654
+ }: {
655
+ features: Features;
656
+ firmwareType: EFirmwareType;
657
+ platform: FirmwareUpdatePlatform;
658
+ artifacts: ProtocolV2LocalFirmwareUpdatePlanArtifact[];
659
+ }): FirmwareUpdatePlan => {
660
+ if (artifacts.length === 0) {
661
+ return planError('Protocol V2 local firmware plan has no artifacts');
662
+ }
663
+ const plan = finalizeFirmwareUpdatePlan({
664
+ features,
665
+ firmwareType,
666
+ platform,
667
+ artifacts: artifacts.map(artifact => ({
668
+ ...artifact,
669
+ role: artifact.target === 'resource' ? 'resourceBundle' : 'component',
670
+ url: `https://local-firmware.invalid/${encodeURIComponent(artifact.artifactId)}`,
671
+ })),
672
+ targetsToUpdate: artifacts.map(artifact => artifact.target),
673
+ });
674
+ if (plan.executor !== 'v4') {
675
+ return planError('Protocol V2 local firmware plan requires executor v4');
676
+ }
677
+ return plan;
678
+ };
679
+
635
680
  export const buildProtocolV2FirmwareUpdatePlan = ({
636
681
  features,
637
682
  firmwareType,
@@ -120,6 +120,8 @@ export interface FirmwareUpdateV3Params {
120
120
  */
121
121
  export type FirmwareUpdateV4Target =
122
122
  | 'boot'
123
+ /** @deprecated Use resource with resourceArchiveBinary. */
124
+ | 'boot_resources'
123
125
  | 'app_v1'
124
126
  | 'app_v2'
125
127
  | 'coprocessor'
@@ -154,11 +156,28 @@ export interface FirmwareUpdateV4Params {
154
156
  se02Binary?: ArrayBuffer;
155
157
  se03Binary?: ArrayBuffer;
156
158
  se04Binary?: ArrayBuffer;
159
+ /** Complete Protocol V2 resource ZIP for local development; Core converts it to a local PreparedPlan. */
160
+ resourceArchiveBinary?: ArrayBuffer;
161
+ /** @deprecated Package the complete signed resource set as a ZIP and use resourceArchiveBinary. */
162
+ resourceFiles?: Array<{
163
+ binary: ArrayBuffer;
164
+ devicePath: string;
165
+ size?: number;
166
+ fileHash?: string;
167
+ }>;
157
168
  forcedUpdateRes?: boolean;
158
169
  artifactReader?: FirmwareArtifactReader;
159
170
  componentArtifacts?: Partial<
160
- Record<Exclude<FirmwareUpdateV4Target, 'resource'>, FirmwareArtifactReference>
171
+ Record<
172
+ Exclude<FirmwareUpdateV4Target, 'resource' | 'boot_resources'>,
173
+ FirmwareArtifactReference
174
+ >
161
175
  >;
176
+ /** @deprecated Use a ZIP artifact in preparedPlan instead. */
177
+ resourceBundleArtifacts?: Array<{
178
+ name: string;
179
+ artifact: FirmwareArtifactReference;
180
+ }>;
162
181
  }
163
182
 
164
183
  export declare function registerFirmwareUpdateHostBinding(