@onekeyfe/hd-core 1.2.0-alpha.41 → 1.2.0-alpha.42

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 (200) hide show
  1. package/__tests__/AllNetworkGetAddressBase.tracing.test.ts +78 -0
  2. package/__tests__/DeviceEventRegistration.test.ts +6 -0
  3. package/__tests__/check-all-firmware-release-protocol-v2.test.ts +14 -1
  4. package/__tests__/connectSettings.test.ts +5 -47
  5. package/__tests__/deviceUploadNft.test.ts +187 -0
  6. package/__tests__/firmware-update/firmware-update-v2-download-before-boot.test.ts +31 -59
  7. package/__tests__/get-device-state.test.ts +132 -12
  8. package/__tests__/homescreen.test.ts +41 -1
  9. package/__tests__/logBlockEvent.test.ts +1 -1
  10. package/__tests__/open-wallet-session.test.ts +259 -18
  11. package/__tests__/pro2Nft.test.ts +108 -0
  12. package/__tests__/protocol-v2-resources.test.ts +204 -0
  13. package/__tests__/protocol-v2-ui-lifecycle.test.ts +56 -0
  14. package/__tests__/protocol-v2-unlock-policy.test.ts +86 -35
  15. package/__tests__/protocol-v2.test.ts +526 -955
  16. package/__tests__/public-pro2-api-boundary.test.ts +1 -0
  17. package/__tests__/uiProgressThrottle.test.ts +74 -0
  18. package/dist/api/BaseMethod.d.ts +3 -1
  19. package/dist/api/BaseMethod.d.ts.map +1 -1
  20. package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
  21. package/dist/api/FirmwareUpdateV2.d.ts +0 -11
  22. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  23. package/dist/api/FirmwareUpdateV3.d.ts +3 -7
  24. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  25. package/dist/api/FirmwareUpdateV4.d.ts +6 -25
  26. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  27. package/dist/api/GetPassphraseState.d.ts.map +1 -1
  28. package/dist/api/OpenWalletSession.d.ts.map +1 -1
  29. package/dist/api/allnetwork/AllNetworkGetAddressBase.d.ts.map +1 -1
  30. package/dist/api/device/DeviceUpdateBootloader.d.ts +1 -5
  31. package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
  32. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +0 -8
  33. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  34. package/dist/api/firmware/getBinary.d.ts +0 -7
  35. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  36. package/dist/api/firmware/updateBootloader.d.ts +0 -2
  37. package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
  38. package/dist/api/firmware/uploadFirmware.d.ts +1 -9
  39. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  40. package/dist/api/helpers/protocolV2FileWrite.d.ts +2 -0
  41. package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -1
  42. package/dist/api/index.d.ts +1 -0
  43. package/dist/api/index.d.ts.map +1 -1
  44. package/dist/api/protocol-v2/DeviceUploadNft.d.ts +30 -0
  45. package/dist/api/protocol-v2/DeviceUploadNft.d.ts.map +1 -0
  46. package/dist/api/protocol-v2/ProtocolInfoRequest.d.ts.map +1 -1
  47. package/dist/api/stellar/StellarSignTransaction.d.ts +1 -1
  48. package/dist/core/deviceEventRegistration.d.ts +2 -0
  49. package/dist/core/deviceEventRegistration.d.ts.map +1 -1
  50. package/dist/core/index.d.ts.map +1 -1
  51. package/dist/data-manager/DataManager.d.ts +3 -2
  52. package/dist/data-manager/DataManager.d.ts.map +1 -1
  53. package/dist/data-manager/connectSettings.d.ts.map +1 -1
  54. package/dist/device/Device.d.ts +22 -4
  55. package/dist/device/Device.d.ts.map +1 -1
  56. package/dist/events/device.d.ts +4 -0
  57. package/dist/events/device.d.ts.map +1 -1
  58. package/dist/events/logBlockEvent.d.ts.map +1 -1
  59. package/dist/events/ui-request.d.ts +27 -2
  60. package/dist/events/ui-request.d.ts.map +1 -1
  61. package/dist/index.d.ts +110 -182
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/index.js +1919 -3336
  64. package/dist/inject.d.ts.map +1 -1
  65. package/dist/lowLevelInject.d.ts.map +1 -1
  66. package/dist/protocols/protocol-v2/features.d.ts.map +1 -1
  67. package/dist/protocols/protocol-v2/resources.d.ts +30 -0
  68. package/dist/protocols/protocol-v2/resources.d.ts.map +1 -0
  69. package/dist/protocols/protocol-v2/uiInteraction.d.ts +3 -3
  70. package/dist/protocols/protocol-v2/uiInteraction.d.ts.map +1 -1
  71. package/dist/protocols/protocol-v2/unlockPolicy.d.ts +1 -3
  72. package/dist/protocols/protocol-v2/unlockPolicy.d.ts.map +1 -1
  73. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts +22 -0
  74. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts.map +1 -0
  75. package/dist/protocols/protocol-v2/walletSession.d.ts +4 -1
  76. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  77. package/dist/topLevelInject.d.ts.map +1 -1
  78. package/dist/types/api/checkAllFirmwareRelease.d.ts +1 -4
  79. package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
  80. package/dist/types/api/deviceUpdateBootloader.d.ts +0 -6
  81. package/dist/types/api/deviceUpdateBootloader.d.ts.map +1 -1
  82. package/dist/types/api/export.d.ts +1 -4
  83. package/dist/types/api/export.d.ts.map +1 -1
  84. package/dist/types/api/firmwareUpdate.d.ts +0 -69
  85. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  86. package/dist/types/api/index.d.ts +3 -10
  87. package/dist/types/api/index.d.ts.map +1 -1
  88. package/dist/types/api/protocolV2.d.ts +3 -0
  89. package/dist/types/api/protocolV2.d.ts.map +1 -1
  90. package/dist/types/settings.d.ts +12 -13
  91. package/dist/types/settings.d.ts.map +1 -1
  92. package/dist/utils/deviceFeaturesUtils.d.ts +2 -0
  93. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  94. package/dist/utils/homescreen.d.ts +4 -0
  95. package/dist/utils/homescreen.d.ts.map +1 -1
  96. package/dist/utils/index.d.ts +1 -1
  97. package/dist/utils/index.d.ts.map +1 -1
  98. package/dist/utils/patch.d.ts +1 -1
  99. package/dist/utils/patch.d.ts.map +1 -1
  100. package/dist/utils/pro2Nft.d.ts +29 -0
  101. package/dist/utils/pro2Nft.d.ts.map +1 -0
  102. package/dist/utils/pro2Wallpaper.d.ts +10 -0
  103. package/dist/utils/pro2Wallpaper.d.ts.map +1 -1
  104. package/dist/utils/uiProgressThrottle.d.ts +3 -0
  105. package/dist/utils/uiProgressThrottle.d.ts.map +1 -0
  106. package/package.json +4 -4
  107. package/src/api/BaseMethod.ts +8 -10
  108. package/src/api/CheckAllFirmwareRelease.ts +40 -53
  109. package/src/api/FirmwareUpdateV2.ts +120 -298
  110. package/src/api/FirmwareUpdateV3.ts +57 -263
  111. package/src/api/FirmwareUpdateV4.ts +307 -912
  112. package/src/api/GetPassphraseState.ts +7 -1
  113. package/src/api/OpenWalletSession.ts +54 -17
  114. package/src/api/allnetwork/AllNetworkGetAddressBase.ts +32 -11
  115. package/src/api/device/DeviceUnlock.ts +1 -1
  116. package/src/api/device/DeviceUpdateBootloader.ts +6 -122
  117. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +0 -57
  118. package/src/api/firmware/updateBootloader.ts +4 -19
  119. package/src/api/firmware/uploadFirmware.ts +22 -140
  120. package/src/api/helpers/protocolV2FileWrite.ts +11 -4
  121. package/src/api/index.ts +1 -0
  122. package/src/api/protocol-v2/DeviceUploadNft.ts +166 -0
  123. package/src/api/protocol-v2/ProtocolInfoRequest.ts +3 -1
  124. package/src/core/deviceEventRegistration.ts +4 -0
  125. package/src/core/index.ts +118 -75
  126. package/src/data/messages/messages-protocol-v2.json +53 -0
  127. package/src/data-manager/DataManager.ts +63 -38
  128. package/src/data-manager/connectSettings.ts +0 -11
  129. package/src/device/Device.ts +193 -21
  130. package/src/events/device.ts +4 -0
  131. package/src/events/logBlockEvent.ts +1 -0
  132. package/src/events/ui-request.ts +32 -2
  133. package/src/index.ts +0 -5
  134. package/src/inject.ts +3 -22
  135. package/src/lowLevelInject.ts +1 -5
  136. package/src/protocols/protocol-v2/features.ts +9 -2
  137. package/src/protocols/protocol-v2/resources.ts +233 -0
  138. package/src/protocols/protocol-v2/uiInteraction.ts +31 -5
  139. package/src/protocols/protocol-v2/unlockPolicy.ts +1 -105
  140. package/src/protocols/protocol-v2/unlockPolicyRunner.ts +117 -0
  141. package/src/protocols/protocol-v2/walletSession.ts +93 -18
  142. package/src/topLevelInject.ts +1 -5
  143. package/src/types/api/checkAllFirmwareRelease.ts +1 -4
  144. package/src/types/api/deviceUpdateBootloader.ts +0 -6
  145. package/src/types/api/export.ts +0 -18
  146. package/src/types/api/firmwareUpdate.ts +0 -76
  147. package/src/types/api/index.ts +2 -14
  148. package/src/types/api/protocolV2.ts +13 -0
  149. package/src/types/settings.ts +26 -13
  150. package/src/utils/deviceFeaturesUtils.ts +4 -0
  151. package/src/utils/homescreen.ts +32 -6
  152. package/src/utils/index.ts +6 -1
  153. package/src/utils/pro2Nft.ts +91 -0
  154. package/src/utils/pro2Wallpaper.ts +35 -8
  155. package/src/utils/uiProgressThrottle.ts +63 -0
  156. package/__tests__/firmware-update/check-all-firmware-release.test.ts +0 -175
  157. package/__tests__/firmware-update/firmware-artifact-source.test.ts +0 -119
  158. package/__tests__/firmware-update/firmware-host-binding.test.ts +0 -70
  159. package/__tests__/firmware-update/firmware-preparation-error.test.ts +0 -27
  160. package/__tests__/firmware-update/firmware-update-capabilities.test.ts +0 -13
  161. package/__tests__/firmware-update/firmware-update-plan.test.ts +0 -593
  162. package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +0 -213
  163. package/__tests__/firmware-update/firmware-update-v3-reconnect.test.ts +0 -58
  164. package/__tests__/firmware-update/firmware-upload-source.test.ts +0 -70
  165. package/dist/api/firmware/FirmwareArtifactSource.d.ts +0 -25
  166. package/dist/api/firmware/FirmwareArtifactSource.d.ts.map +0 -1
  167. package/dist/api/firmware/FirmwareHostBinding.d.ts +0 -6
  168. package/dist/api/firmware/FirmwareHostBinding.d.ts.map +0 -1
  169. package/dist/api/firmware/FirmwarePreparationError.d.ts +0 -3
  170. package/dist/api/firmware/FirmwarePreparationError.d.ts.map +0 -1
  171. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts +0 -3
  172. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts.map +0 -1
  173. package/dist/api/firmware/FirmwareUpdatePlan.d.ts +0 -25
  174. package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +0 -1
  175. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts +0 -30
  176. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +0 -1
  177. package/dist/api/firmware/progressThrottle.d.ts +0 -3
  178. package/dist/api/firmware/progressThrottle.d.ts.map +0 -1
  179. package/dist/protocols/protocol-v2/lockedError.d.ts +0 -2
  180. package/dist/protocols/protocol-v2/lockedError.d.ts.map +0 -1
  181. package/dist/protocols/protocol-v2/unlockRetry.d.ts +0 -10
  182. package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +0 -1
  183. package/dist/types/api/firmwareUpdateCapabilities.d.ts +0 -9
  184. package/dist/types/api/firmwareUpdateCapabilities.d.ts.map +0 -1
  185. package/dist/types/api/firmwareUpdatePlan.d.ts +0 -28
  186. package/dist/types/api/firmwareUpdatePlan.d.ts.map +0 -1
  187. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts +0 -42
  188. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts.map +0 -1
  189. package/src/api/firmware/FirmwareArtifactSource.ts +0 -278
  190. package/src/api/firmware/FirmwareHostBinding.ts +0 -100
  191. package/src/api/firmware/FirmwarePreparationError.ts +0 -12
  192. package/src/api/firmware/FirmwareUpdateCapabilities.ts +0 -9
  193. package/src/api/firmware/FirmwareUpdatePlan.ts +0 -745
  194. package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +0 -392
  195. package/src/api/firmware/progressThrottle.ts +0 -44
  196. package/src/protocols/protocol-v2/lockedError.ts +0 -10
  197. package/src/protocols/protocol-v2/unlockRetry.ts +0 -109
  198. package/src/types/api/firmwareUpdateCapabilities.ts +0 -9
  199. package/src/types/api/firmwareUpdatePlan.ts +0 -38
  200. package/src/types/api/firmwareUpdatePreparedPlan.ts +0 -53
@@ -1,278 +0,0 @@
1
- import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
2
-
3
- import type {
4
- FirmwareArtifactReader,
5
- FirmwareArtifactReference,
6
- } from '../../types/api/firmwareUpdate';
7
-
8
- export const MAX_FIRMWARE_ARTIFACT_READ_BYTES = 256 * 1024;
9
-
10
- export interface FirmwareByteSource {
11
- readonly size: number;
12
- readAt(offset: number, length: number): Promise<ArrayBuffer>;
13
- close(): Promise<void>;
14
- }
15
-
16
- const artifactError = (
17
- code:
18
- | 'FirmwareArtifactReceiptMismatch'
19
- | 'FirmwareArtifactReaderInvalid'
20
- | 'FirmwareArtifactReadOutOfBounds'
21
- | 'FirmwareArtifactReadTooLarge',
22
- message: string
23
- ): never => {
24
- throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, message, {
25
- firmwareUpdateCode: code,
26
- });
27
- };
28
-
29
- const assertSafeSize = (value: number, name: string) => {
30
- if (!Number.isSafeInteger(value) || value <= 0) {
31
- artifactError('FirmwareArtifactReceiptMismatch', `${name} must be a positive safe integer`);
32
- }
33
- };
34
-
35
- export const assertFirmwareArtifactReference = (
36
- artifact: FirmwareArtifactReference
37
- ): FirmwareArtifactReference => {
38
- if (
39
- !artifact ||
40
- typeof artifact.artifactRef !== 'string' ||
41
- artifact.artifactRef.length === 0 ||
42
- artifact.artifactRef.length > 160 ||
43
- artifact.artifactRef.includes('/') ||
44
- artifact.artifactRef.includes('\\')
45
- ) {
46
- artifactError(
47
- 'FirmwareArtifactReceiptMismatch',
48
- 'Firmware artifactRef must be an opaque identifier'
49
- );
50
- }
51
- assertSafeSize(artifact.size, 'Firmware artifact size');
52
- if (typeof artifact.sha256 !== 'string' || !/^[a-f0-9]{64}$/i.test(artifact.sha256)) {
53
- artifactError(
54
- 'FirmwareArtifactReceiptMismatch',
55
- 'Firmware artifact sha256 must be a 32-byte hexadecimal digest'
56
- );
57
- }
58
- return artifact;
59
- };
60
-
61
- const assertFirmwareArtifactRead = (size: number, offset: number, length: number) => {
62
- if (
63
- !Number.isSafeInteger(offset) ||
64
- offset < 0 ||
65
- offset >= size ||
66
- !Number.isSafeInteger(length) ||
67
- length <= 0 ||
68
- offset + length > size
69
- ) {
70
- artifactError(
71
- 'FirmwareArtifactReadOutOfBounds',
72
- `Firmware artifact read is outside size ${size}: offset=${offset}, length=${length}`
73
- );
74
- }
75
- if (length > MAX_FIRMWARE_ARTIFACT_READ_BYTES) {
76
- artifactError(
77
- 'FirmwareArtifactReadTooLarge',
78
- `Firmware artifact read length ${length} exceeds ${MAX_FIRMWARE_ARTIFACT_READ_BYTES}`
79
- );
80
- }
81
- };
82
-
83
- const createMemoryFirmwareByteSource = (binary: ArrayBuffer): FirmwareByteSource => {
84
- let bytes: Uint8Array | null = new Uint8Array(binary);
85
- const { byteLength: size } = binary;
86
- assertSafeSize(size, 'Firmware binary size');
87
- return {
88
- size,
89
- readAt: (offset, length) => {
90
- if (!bytes) {
91
- return Promise.reject(
92
- ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'Firmware byte source is closed', {
93
- firmwareUpdateCode: 'FirmwareArtifactReaderInvalid',
94
- })
95
- );
96
- }
97
- assertFirmwareArtifactRead(size, offset, length);
98
- return Promise.resolve(bytes.slice(offset, offset + length).buffer);
99
- },
100
- close: () => {
101
- bytes = null;
102
- return Promise.resolve();
103
- },
104
- };
105
- };
106
-
107
- class ReaderFirmwareByteSource implements FirmwareByteSource {
108
- readonly size: number;
109
-
110
- private closed = false;
111
-
112
- private reading = false;
113
-
114
- private constructor(
115
- private readonly reader: FirmwareArtifactReader,
116
- private readonly readerId: string,
117
- size: number
118
- ) {
119
- this.size = size;
120
- }
121
-
122
- static async open(
123
- artifact: FirmwareArtifactReference,
124
- reader: FirmwareArtifactReader
125
- ): Promise<ReaderFirmwareByteSource> {
126
- const opened = await reader.open({ artifactRef: artifact.artifactRef });
127
- if (
128
- !opened ||
129
- typeof opened.readerId !== 'string' ||
130
- opened.readerId.length === 0 ||
131
- !Number.isSafeInteger(opened.size) ||
132
- opened.size !== artifact.size
133
- ) {
134
- if (opened?.readerId) {
135
- await reader.close({ readerId: opened.readerId }).catch(() => undefined);
136
- }
137
- return artifactError(
138
- 'FirmwareArtifactReceiptMismatch',
139
- `Firmware artifact opened with size ${opened?.size ?? 'unknown'}, expected ${artifact.size}`
140
- );
141
- }
142
- return new ReaderFirmwareByteSource(reader, opened.readerId, opened.size);
143
- }
144
-
145
- async readAt(offset: number, length: number): Promise<ArrayBuffer> {
146
- if (this.closed) {
147
- return artifactError('FirmwareArtifactReaderInvalid', 'Firmware artifact reader is closed');
148
- }
149
- if (this.reading) {
150
- return artifactError(
151
- 'FirmwareArtifactReaderInvalid',
152
- 'Firmware artifact reader only permits one in-flight read'
153
- );
154
- }
155
- assertFirmwareArtifactRead(this.size, offset, length);
156
- this.reading = true;
157
- try {
158
- const result = await this.reader.read({
159
- readerId: this.readerId,
160
- offset,
161
- length,
162
- });
163
- const expectedEof = offset + length === this.size;
164
- if (
165
- !result ||
166
- !(result.data instanceof ArrayBuffer) ||
167
- result.data.byteLength !== length ||
168
- result.bytesRead !== length ||
169
- result.eof !== expectedEof
170
- ) {
171
- return artifactError(
172
- 'FirmwareArtifactReaderInvalid',
173
- 'Firmware artifact reader returned an invalid read result'
174
- );
175
- }
176
- return result.data;
177
- } finally {
178
- this.reading = false;
179
- }
180
- }
181
-
182
- async close(): Promise<void> {
183
- if (this.closed) {
184
- return;
185
- }
186
- this.closed = true;
187
- await this.reader.close({ readerId: this.readerId });
188
- }
189
- }
190
-
191
- export const openFirmwareByteSource = async ({
192
- binary,
193
- artifact,
194
- reader,
195
- }: {
196
- binary?: ArrayBuffer;
197
- artifact?: FirmwareArtifactReference;
198
- reader?: FirmwareArtifactReader;
199
- }): Promise<FirmwareByteSource | null> => {
200
- if (binary && artifact) {
201
- return artifactError(
202
- 'FirmwareArtifactReceiptMismatch',
203
- 'Firmware input must not contain both binary and artifactRef'
204
- );
205
- }
206
- if (binary) {
207
- return createMemoryFirmwareByteSource(binary);
208
- }
209
- if (!artifact) {
210
- return null;
211
- }
212
- assertFirmwareArtifactReference(artifact);
213
- if (!reader) {
214
- return artifactError(
215
- 'FirmwareArtifactReaderInvalid',
216
- 'Firmware artifact reader is required for artifactRef input'
217
- );
218
- }
219
- return ReaderFirmwareByteSource.open(artifact, reader);
220
- };
221
-
222
- export const readFirmwareByteSourceFully = async (
223
- source: FirmwareByteSource
224
- ): Promise<ArrayBuffer> => {
225
- const output = new Uint8Array(source.size);
226
- let offset = 0;
227
- while (offset < source.size) {
228
- const length = Math.min(MAX_FIRMWARE_ARTIFACT_READ_BYTES, source.size - offset);
229
- output.set(new Uint8Array(await source.readAt(offset, length)), offset);
230
- offset += length;
231
- }
232
- return output.buffer;
233
- };
234
-
235
- export const writeFirmwareByteSource = async ({
236
- source,
237
- chunkSize,
238
- write,
239
- }: {
240
- source: FirmwareByteSource;
241
- chunkSize: number;
242
- write: (input: {
243
- data: ArrayBuffer;
244
- sourceOffset: number;
245
- length: number;
246
- first: boolean;
247
- }) => Promise<number>;
248
- }) => {
249
- if (
250
- !Number.isSafeInteger(chunkSize) ||
251
- chunkSize <= 0 ||
252
- chunkSize > MAX_FIRMWARE_ARTIFACT_READ_BYTES
253
- ) {
254
- return artifactError(
255
- 'FirmwareArtifactReadTooLarge',
256
- `Firmware artifact chunk size must be between 1 and ${MAX_FIRMWARE_ARTIFACT_READ_BYTES}`
257
- );
258
- }
259
- let offset = 0;
260
- while (offset < source.size) {
261
- const length = Math.min(chunkSize, source.size - offset);
262
- const data = await source.readAt(offset, length);
263
- const processed = await write({
264
- data,
265
- sourceOffset: offset,
266
- length,
267
- first: offset === 0,
268
- });
269
- if (processed !== length) {
270
- return artifactError(
271
- 'FirmwareArtifactReaderInvalid',
272
- `Firmware artifact writer processed ${processed} bytes, expected ${length}`
273
- );
274
- }
275
- offset += length;
276
- }
277
- return offset;
278
- };
@@ -1,100 +0,0 @@
1
- import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
2
-
3
- import type { FirmwareUpdateHostBinding } from '../../types/api/firmwareUpdate';
4
-
5
- const bindingError = (message: string): never => {
6
- throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, message, {
7
- firmwareUpdateCode: 'FirmwareArtifactReaderInvalid',
8
- });
9
- };
10
-
11
- class FirmwareHostBindingRegistry {
12
- private generation = 0;
13
-
14
- private binding?: FirmwareUpdateHostBinding;
15
-
16
- register(binding: FirmwareUpdateHostBinding): number {
17
- if (
18
- !binding ||
19
- typeof binding.artifactReader?.open !== 'function' ||
20
- typeof binding.artifactReader.read !== 'function' ||
21
- typeof binding.artifactReader.close !== 'function'
22
- ) {
23
- return bindingError('Firmware host binding is invalid');
24
- }
25
- this.generation += 1;
26
- this.binding = binding;
27
- return this.generation;
28
- }
29
-
30
- unregister(generation?: number): boolean {
31
- if (generation !== undefined && generation !== this.generation) {
32
- return false;
33
- }
34
- this.generation += 1;
35
- this.binding = undefined;
36
- return true;
37
- }
38
-
39
- getGeneration(): number {
40
- return this.generation;
41
- }
42
-
43
- resolve(generation: number): FirmwareUpdateHostBinding {
44
- if (
45
- !Number.isSafeInteger(generation) ||
46
- generation <= 0 ||
47
- generation !== this.generation ||
48
- !this.binding
49
- ) {
50
- return bindingError(`Firmware host binding generation ${generation} is stale`);
51
- }
52
- const { binding } = this;
53
- const assertCurrent = () => {
54
- if (generation !== this.generation || binding !== this.binding) {
55
- bindingError(`Firmware host binding generation ${generation} is stale`);
56
- }
57
- };
58
- return {
59
- artifactReader: {
60
- open: async input => {
61
- assertCurrent();
62
- const opened = await binding.artifactReader.open(input);
63
- try {
64
- assertCurrent();
65
- } catch (error) {
66
- if (opened?.readerId) {
67
- await binding.artifactReader
68
- .close({ readerId: opened.readerId })
69
- .catch(() => undefined);
70
- }
71
- throw error;
72
- }
73
- return opened;
74
- },
75
- read: async input => {
76
- assertCurrent();
77
- const result = await binding.artifactReader.read(input);
78
- assertCurrent();
79
- return result;
80
- },
81
- close: input => binding.artifactReader.close(input),
82
- },
83
- };
84
- }
85
- }
86
-
87
- const firmwareHostBindingRegistry = new FirmwareHostBindingRegistry();
88
-
89
- export const registerFirmwareUpdateHostBinding = (binding: FirmwareUpdateHostBinding): number =>
90
- firmwareHostBindingRegistry.register(binding);
91
-
92
- export const unregisterFirmwareUpdateHostBinding = (generation?: number): boolean =>
93
- firmwareHostBindingRegistry.unregister(generation);
94
-
95
- export const getFirmwareUpdateHostBindingGeneration = (): number =>
96
- firmwareHostBindingRegistry.getGeneration();
97
-
98
- export const resolveFirmwareUpdateHostBinding = (
99
- generation: number | undefined
100
- ): FirmwareUpdateHostBinding => firmwareHostBindingRegistry.resolve(generation ?? Number.NaN);
@@ -1,12 +0,0 @@
1
- import { ERRORS, HardwareError, HardwareErrorCode } from '@onekeyfe/hd-shared';
2
-
3
- export const normalizeFirmwarePreparationError = (error: unknown) => {
4
- if (error instanceof HardwareError && error.params?.firmwareUpdateCode) {
5
- return error;
6
- }
7
-
8
- return ERRORS.TypedError(
9
- HardwareErrorCode.FirmwareUpdateDownloadFailed,
10
- error instanceof Error ? error.message : String(error)
11
- );
12
- };
@@ -1,9 +0,0 @@
1
- import type { FirmwareUpdateCapabilities } from '../../types/api/firmwareUpdateCapabilities';
2
-
3
- export const getFirmwareUpdateCapabilities = (): FirmwareUpdateCapabilities => ({
4
- planSchemaVersion: 2,
5
- preparedPlanSchemaVersion: 2,
6
- hostBindingProtocolVersion: 2,
7
- manifestModes: ['external-only', 'sdk-managed'],
8
- supportsArtifactReader: true,
9
- });