@onekeyfe/hd-core 1.2.0-alpha.157 → 1.2.0-alpha.159
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.
- package/__tests__/device-utils.test.ts +2 -0
- package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +1 -4
- package/__tests__/protocol-v2-resources.test.ts +1 -3
- package/__tests__/protocol-v2.test.ts +80 -37
- package/dist/api/FirmwareUpdateV4.d.ts +3 -1
- package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
- package/dist/api/firmware/FirmwareUpdatePlan.d.ts +0 -15
- package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +1 -1
- package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +1 -1
- package/dist/data-manager/TransportManager.d.ts.map +1 -1
- package/dist/index.d.ts +2 -22
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +69 -234
- package/dist/utils/deviceInfoUtils.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/api/FirmwareUpdateV4.ts +76 -185
- package/src/api/firmware/FirmwareUpdatePlan.ts +0 -41
- package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +2 -0
- package/src/data-manager/TransportManager.ts +3 -1
- package/src/index.ts +0 -6
- package/src/types/api/firmwareUpdate.ts +1 -1
- package/src/utils/deviceInfoUtils.ts +3 -1
- package/__tests__/firmware-memory-host.test.ts +0 -126
- package/dist/api/firmware/FirmwareMemoryHost.d.ts +0 -22
- package/dist/api/firmware/FirmwareMemoryHost.d.ts.map +0 -1
- package/src/api/firmware/FirmwareMemoryHost.ts +0 -143
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { sha256 } from '@noble/hashes/sha256';
|
|
2
|
-
import { bytesToHex } from '@noble/hashes/utils';
|
|
3
|
-
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
4
|
-
|
|
5
|
-
import type { CoreApi } from '../../types/api';
|
|
6
|
-
import type {
|
|
7
|
-
FirmwareArtifactReader,
|
|
8
|
-
FirmwareArtifactReference,
|
|
9
|
-
} from '../../types/api/firmwareUpdate';
|
|
10
|
-
import type { FirmwareUpdatePlan } from '../../types/api/firmwareUpdatePlan';
|
|
11
|
-
|
|
12
|
-
export type FirmwareMemoryArtifactEntry = {
|
|
13
|
-
entryName: string;
|
|
14
|
-
binary: ArrayBuffer;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export type FirmwareMemoryArtifact = {
|
|
18
|
-
artifactId: string;
|
|
19
|
-
binary: ArrayBuffer;
|
|
20
|
-
materializedEntries?: FirmwareMemoryArtifactEntry[];
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type FirmwareUpdateV4MemoryHost = {
|
|
24
|
-
preparedPlan: ReturnType<CoreApi['prepareFirmwareUpdatePlan']>;
|
|
25
|
-
hostBindingGeneration: number;
|
|
26
|
-
release: () => void;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
let memoryHostSequence = 0;
|
|
30
|
-
|
|
31
|
-
const createReference = (binary: ArrayBuffer, prefix: string): FirmwareArtifactReference => {
|
|
32
|
-
const digest = bytesToHex(sha256(new Uint8Array(binary)));
|
|
33
|
-
return {
|
|
34
|
-
artifactRef: `fwmem:${prefix}:${digest.slice(0, 32)}`,
|
|
35
|
-
size: binary.byteLength,
|
|
36
|
-
sha256: digest,
|
|
37
|
-
};
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export function prepareFirmwareUpdateV4MemoryHost({
|
|
41
|
-
sdk,
|
|
42
|
-
plan,
|
|
43
|
-
artifacts,
|
|
44
|
-
}: {
|
|
45
|
-
sdk: Pick<
|
|
46
|
-
CoreApi,
|
|
47
|
-
| 'prepareFirmwareUpdatePlan'
|
|
48
|
-
| 'registerFirmwareUpdateHostBinding'
|
|
49
|
-
| 'unregisterFirmwareUpdateHostBinding'
|
|
50
|
-
>;
|
|
51
|
-
plan: FirmwareUpdatePlan;
|
|
52
|
-
artifacts: FirmwareMemoryArtifact[];
|
|
53
|
-
}): FirmwareUpdateV4MemoryHost {
|
|
54
|
-
if (plan.executor !== 'v4') {
|
|
55
|
-
throw ERRORS.TypedError(
|
|
56
|
-
HardwareErrorCode.RuntimeError,
|
|
57
|
-
'Firmware memory host only supports V4 plans'
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
memoryHostSequence += 1;
|
|
61
|
-
const hostId = `${Date.now()}:${memoryHostSequence}`;
|
|
62
|
-
const binaries = new Map<string, Uint8Array>();
|
|
63
|
-
const inputs = artifacts.map((input, artifactIndex) => {
|
|
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);
|
|
70
|
-
const materializedEntries = input.materializedEntries?.map((entry, entryIndex) => {
|
|
71
|
-
const entryArtifact = createReference(
|
|
72
|
-
entry.binary,
|
|
73
|
-
`${hostId}:entry:${artifactIndex}:${entryIndex}`
|
|
74
|
-
);
|
|
75
|
-
// Entry references are compact receipts for bytes that will be re-derived
|
|
76
|
-
// from the verified archive. Retaining another readable copy here doubles
|
|
77
|
-
// resource memory without adding an execution trust boundary.
|
|
78
|
-
return {
|
|
79
|
-
entryName: entry.entryName,
|
|
80
|
-
artifact: entryArtifact,
|
|
81
|
-
};
|
|
82
|
-
});
|
|
83
|
-
return {
|
|
84
|
-
artifactId: input.artifactId,
|
|
85
|
-
artifact,
|
|
86
|
-
...(materializedEntries?.length ? { materializedEntries } : {}),
|
|
87
|
-
};
|
|
88
|
-
});
|
|
89
|
-
const preparedPlan = sdk.prepareFirmwareUpdatePlan({
|
|
90
|
-
plan,
|
|
91
|
-
leaseRef: `fwmemlease:${hostId}`,
|
|
92
|
-
artifacts: inputs,
|
|
93
|
-
});
|
|
94
|
-
const readers = new Map<string, Uint8Array>();
|
|
95
|
-
let readerSequence = 0;
|
|
96
|
-
const artifactReader: FirmwareArtifactReader = {
|
|
97
|
-
open({ artifactRef }) {
|
|
98
|
-
const binary = binaries.get(artifactRef);
|
|
99
|
-
if (!binary) {
|
|
100
|
-
throw ERRORS.TypedError(
|
|
101
|
-
HardwareErrorCode.RuntimeError,
|
|
102
|
-
'Firmware memory artifact is unavailable'
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
readerSequence += 1;
|
|
106
|
-
const readerId = `fwmemreader:${hostId}:${readerSequence}`;
|
|
107
|
-
readers.set(readerId, binary);
|
|
108
|
-
return Promise.resolve({ readerId, size: binary.byteLength });
|
|
109
|
-
},
|
|
110
|
-
read({ readerId, offset, length }) {
|
|
111
|
-
const binary = readers.get(readerId);
|
|
112
|
-
if (!binary || offset < 0 || length <= 0 || offset + length > binary.byteLength) {
|
|
113
|
-
throw ERRORS.TypedError(
|
|
114
|
-
HardwareErrorCode.RuntimeError,
|
|
115
|
-
'Firmware memory artifact read is invalid'
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
const data = binary.slice(offset, offset + length).buffer;
|
|
119
|
-
return Promise.resolve({
|
|
120
|
-
data,
|
|
121
|
-
bytesRead: data.byteLength,
|
|
122
|
-
eof: offset + length === binary.byteLength,
|
|
123
|
-
});
|
|
124
|
-
},
|
|
125
|
-
close({ readerId }) {
|
|
126
|
-
readers.delete(readerId);
|
|
127
|
-
return Promise.resolve();
|
|
128
|
-
},
|
|
129
|
-
};
|
|
130
|
-
const hostBindingGeneration = sdk.registerFirmwareUpdateHostBinding({
|
|
131
|
-
artifactReader,
|
|
132
|
-
preparedPlanDigest: preparedPlan.preparedPlanDigest,
|
|
133
|
-
});
|
|
134
|
-
return {
|
|
135
|
-
preparedPlan,
|
|
136
|
-
hostBindingGeneration,
|
|
137
|
-
release: () => {
|
|
138
|
-
sdk.unregisterFirmwareUpdateHostBinding(hostBindingGeneration);
|
|
139
|
-
readers.clear();
|
|
140
|
-
binaries.clear();
|
|
141
|
-
},
|
|
142
|
-
};
|
|
143
|
-
}
|