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

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 (202) 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 +272 -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 +366 -31
  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 +91 -0
  14. package/__tests__/protocol-v2-unlock-policy.test.ts +196 -35
  15. package/__tests__/protocol-v2.test.ts +568 -957
  16. package/__tests__/protocolV2UiInteraction.test.ts +63 -0
  17. package/__tests__/public-pro2-api-boundary.test.ts +1 -0
  18. package/__tests__/uiProgressThrottle.test.ts +74 -0
  19. package/dist/api/BaseMethod.d.ts +3 -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/FirmwareUpdateV2.d.ts +0 -11
  23. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  24. package/dist/api/FirmwareUpdateV3.d.ts +3 -7
  25. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  26. package/dist/api/FirmwareUpdateV4.d.ts +6 -25
  27. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  28. package/dist/api/GetPassphraseState.d.ts.map +1 -1
  29. package/dist/api/OpenWalletSession.d.ts.map +1 -1
  30. package/dist/api/allnetwork/AllNetworkGetAddressBase.d.ts.map +1 -1
  31. package/dist/api/device/DeviceUpdateBootloader.d.ts +1 -5
  32. package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
  33. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +0 -8
  34. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  35. package/dist/api/firmware/getBinary.d.ts +0 -7
  36. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  37. package/dist/api/firmware/updateBootloader.d.ts +0 -2
  38. package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
  39. package/dist/api/firmware/uploadFirmware.d.ts +1 -9
  40. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  41. package/dist/api/helpers/protocolV2FileWrite.d.ts +2 -0
  42. package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -1
  43. package/dist/api/index.d.ts +1 -0
  44. package/dist/api/index.d.ts.map +1 -1
  45. package/dist/api/protocol-v2/DeviceUploadNft.d.ts +31 -0
  46. package/dist/api/protocol-v2/DeviceUploadNft.d.ts.map +1 -0
  47. package/dist/api/protocol-v2/ProtocolInfoRequest.d.ts.map +1 -1
  48. package/dist/api/stellar/StellarSignTransaction.d.ts +1 -1
  49. package/dist/core/deviceEventRegistration.d.ts +2 -0
  50. package/dist/core/deviceEventRegistration.d.ts.map +1 -1
  51. package/dist/core/index.d.ts.map +1 -1
  52. package/dist/data-manager/DataManager.d.ts +3 -2
  53. package/dist/data-manager/DataManager.d.ts.map +1 -1
  54. package/dist/data-manager/connectSettings.d.ts.map +1 -1
  55. package/dist/device/Device.d.ts +24 -4
  56. package/dist/device/Device.d.ts.map +1 -1
  57. package/dist/device/DeviceCommands.d.ts +4 -4
  58. package/dist/events/device.d.ts +4 -0
  59. package/dist/events/device.d.ts.map +1 -1
  60. package/dist/events/logBlockEvent.d.ts.map +1 -1
  61. package/dist/events/ui-request.d.ts +28 -2
  62. package/dist/events/ui-request.d.ts.map +1 -1
  63. package/dist/index.d.ts +115 -184
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/index.js +2051 -3349
  66. package/dist/inject.d.ts.map +1 -1
  67. package/dist/lowLevelInject.d.ts.map +1 -1
  68. package/dist/protocols/protocol-v2/features.d.ts.map +1 -1
  69. package/dist/protocols/protocol-v2/resources.d.ts +30 -0
  70. package/dist/protocols/protocol-v2/resources.d.ts.map +1 -0
  71. package/dist/protocols/protocol-v2/uiInteraction.d.ts +7 -4
  72. package/dist/protocols/protocol-v2/uiInteraction.d.ts.map +1 -1
  73. package/dist/protocols/protocol-v2/unlockPolicy.d.ts +1 -3
  74. package/dist/protocols/protocol-v2/unlockPolicy.d.ts.map +1 -1
  75. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts +22 -0
  76. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts.map +1 -0
  77. package/dist/protocols/protocol-v2/walletSession.d.ts +4 -1
  78. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  79. package/dist/topLevelInject.d.ts.map +1 -1
  80. package/dist/types/api/checkAllFirmwareRelease.d.ts +1 -4
  81. package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
  82. package/dist/types/api/deviceUpdateBootloader.d.ts +0 -6
  83. package/dist/types/api/deviceUpdateBootloader.d.ts.map +1 -1
  84. package/dist/types/api/export.d.ts +1 -4
  85. package/dist/types/api/export.d.ts.map +1 -1
  86. package/dist/types/api/firmwareUpdate.d.ts +0 -69
  87. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  88. package/dist/types/api/index.d.ts +3 -10
  89. package/dist/types/api/index.d.ts.map +1 -1
  90. package/dist/types/api/protocolV2.d.ts +3 -0
  91. package/dist/types/api/protocolV2.d.ts.map +1 -1
  92. package/dist/types/settings.d.ts +12 -13
  93. package/dist/types/settings.d.ts.map +1 -1
  94. package/dist/utils/deviceFeaturesUtils.d.ts +2 -0
  95. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  96. package/dist/utils/homescreen.d.ts +4 -0
  97. package/dist/utils/homescreen.d.ts.map +1 -1
  98. package/dist/utils/index.d.ts +1 -1
  99. package/dist/utils/index.d.ts.map +1 -1
  100. package/dist/utils/patch.d.ts +1 -1
  101. package/dist/utils/patch.d.ts.map +1 -1
  102. package/dist/utils/pro2Nft.d.ts +31 -0
  103. package/dist/utils/pro2Nft.d.ts.map +1 -0
  104. package/dist/utils/pro2Wallpaper.d.ts +10 -0
  105. package/dist/utils/pro2Wallpaper.d.ts.map +1 -1
  106. package/dist/utils/uiProgressThrottle.d.ts +3 -0
  107. package/dist/utils/uiProgressThrottle.d.ts.map +1 -0
  108. package/package.json +4 -4
  109. package/src/api/BaseMethod.ts +8 -10
  110. package/src/api/CheckAllFirmwareRelease.ts +40 -53
  111. package/src/api/FirmwareUpdateV2.ts +120 -298
  112. package/src/api/FirmwareUpdateV3.ts +57 -263
  113. package/src/api/FirmwareUpdateV4.ts +307 -912
  114. package/src/api/GetPassphraseState.ts +7 -1
  115. package/src/api/OpenWalletSession.ts +54 -17
  116. package/src/api/allnetwork/AllNetworkGetAddressBase.ts +32 -11
  117. package/src/api/device/DeviceUnlock.ts +1 -1
  118. package/src/api/device/DeviceUpdateBootloader.ts +6 -122
  119. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +0 -57
  120. package/src/api/firmware/updateBootloader.ts +4 -19
  121. package/src/api/firmware/uploadFirmware.ts +22 -140
  122. package/src/api/helpers/protocolV2FileWrite.ts +11 -4
  123. package/src/api/index.ts +1 -0
  124. package/src/api/protocol-v2/DeviceUploadNft.ts +189 -0
  125. package/src/api/protocol-v2/ProtocolInfoRequest.ts +3 -1
  126. package/src/core/deviceEventRegistration.ts +4 -0
  127. package/src/core/index.ts +136 -77
  128. package/src/data/messages/messages-protocol-v2.json +68 -0
  129. package/src/data-manager/DataManager.ts +63 -38
  130. package/src/data-manager/connectSettings.ts +0 -11
  131. package/src/device/Device.ts +203 -21
  132. package/src/events/device.ts +4 -0
  133. package/src/events/logBlockEvent.ts +1 -0
  134. package/src/events/ui-request.ts +33 -2
  135. package/src/index.ts +0 -5
  136. package/src/inject.ts +3 -22
  137. package/src/lowLevelInject.ts +1 -5
  138. package/src/protocols/protocol-v2/features.ts +9 -2
  139. package/src/protocols/protocol-v2/resources.ts +233 -0
  140. package/src/protocols/protocol-v2/uiInteraction.ts +46 -6
  141. package/src/protocols/protocol-v2/unlockPolicy.ts +1 -105
  142. package/src/protocols/protocol-v2/unlockPolicyRunner.ts +124 -0
  143. package/src/protocols/protocol-v2/walletSession.ts +154 -30
  144. package/src/topLevelInject.ts +1 -5
  145. package/src/types/api/checkAllFirmwareRelease.ts +1 -4
  146. package/src/types/api/deviceUpdateBootloader.ts +0 -6
  147. package/src/types/api/export.ts +0 -18
  148. package/src/types/api/firmwareUpdate.ts +0 -76
  149. package/src/types/api/index.ts +2 -14
  150. package/src/types/api/protocolV2.ts +13 -0
  151. package/src/types/settings.ts +26 -13
  152. package/src/utils/deviceFeaturesUtils.ts +4 -0
  153. package/src/utils/homescreen.ts +32 -6
  154. package/src/utils/index.ts +6 -1
  155. package/src/utils/pro2Nft.ts +142 -0
  156. package/src/utils/pro2Wallpaper.ts +35 -8
  157. package/src/utils/uiProgressThrottle.ts +63 -0
  158. package/__tests__/firmware-update/check-all-firmware-release.test.ts +0 -175
  159. package/__tests__/firmware-update/firmware-artifact-source.test.ts +0 -119
  160. package/__tests__/firmware-update/firmware-host-binding.test.ts +0 -70
  161. package/__tests__/firmware-update/firmware-preparation-error.test.ts +0 -27
  162. package/__tests__/firmware-update/firmware-update-capabilities.test.ts +0 -13
  163. package/__tests__/firmware-update/firmware-update-plan.test.ts +0 -593
  164. package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +0 -213
  165. package/__tests__/firmware-update/firmware-update-v3-reconnect.test.ts +0 -58
  166. package/__tests__/firmware-update/firmware-upload-source.test.ts +0 -70
  167. package/dist/api/firmware/FirmwareArtifactSource.d.ts +0 -25
  168. package/dist/api/firmware/FirmwareArtifactSource.d.ts.map +0 -1
  169. package/dist/api/firmware/FirmwareHostBinding.d.ts +0 -6
  170. package/dist/api/firmware/FirmwareHostBinding.d.ts.map +0 -1
  171. package/dist/api/firmware/FirmwarePreparationError.d.ts +0 -3
  172. package/dist/api/firmware/FirmwarePreparationError.d.ts.map +0 -1
  173. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts +0 -3
  174. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts.map +0 -1
  175. package/dist/api/firmware/FirmwareUpdatePlan.d.ts +0 -25
  176. package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +0 -1
  177. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts +0 -30
  178. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +0 -1
  179. package/dist/api/firmware/progressThrottle.d.ts +0 -3
  180. package/dist/api/firmware/progressThrottle.d.ts.map +0 -1
  181. package/dist/protocols/protocol-v2/lockedError.d.ts +0 -2
  182. package/dist/protocols/protocol-v2/lockedError.d.ts.map +0 -1
  183. package/dist/protocols/protocol-v2/unlockRetry.d.ts +0 -10
  184. package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +0 -1
  185. package/dist/types/api/firmwareUpdateCapabilities.d.ts +0 -9
  186. package/dist/types/api/firmwareUpdateCapabilities.d.ts.map +0 -1
  187. package/dist/types/api/firmwareUpdatePlan.d.ts +0 -28
  188. package/dist/types/api/firmwareUpdatePlan.d.ts.map +0 -1
  189. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts +0 -42
  190. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts.map +0 -1
  191. package/src/api/firmware/FirmwareArtifactSource.ts +0 -278
  192. package/src/api/firmware/FirmwareHostBinding.ts +0 -100
  193. package/src/api/firmware/FirmwarePreparationError.ts +0 -12
  194. package/src/api/firmware/FirmwareUpdateCapabilities.ts +0 -9
  195. package/src/api/firmware/FirmwareUpdatePlan.ts +0 -745
  196. package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +0 -392
  197. package/src/api/firmware/progressThrottle.ts +0 -44
  198. package/src/protocols/protocol-v2/lockedError.ts +0 -10
  199. package/src/protocols/protocol-v2/unlockRetry.ts +0 -109
  200. package/src/types/api/firmwareUpdateCapabilities.ts +0 -9
  201. package/src/types/api/firmwareUpdatePlan.ts +0 -38
  202. package/src/types/api/firmwareUpdatePreparedPlan.ts +0 -53
@@ -1,392 +0,0 @@
1
- import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
2
-
3
- import { assertFirmwareArtifactReference } from './FirmwareArtifactSource';
4
- import {
5
- FIRMWARE_UPDATE_PLAN_ROLES,
6
- FIRMWARE_UPDATE_PLAN_TARGETS,
7
- assertFirmwareUpdatePlan,
8
- digestFirmwareUpdateContract,
9
- } from './FirmwareUpdatePlan';
10
-
11
- import type {
12
- FirmwareUpdatePreparedArtifact,
13
- FirmwareUpdatePreparedArtifactInput,
14
- FirmwareUpdatePreparedEntry,
15
- FirmwareUpdatePreparedPlan,
16
- } from '../../types/api/firmwareUpdatePreparedPlan';
17
- import type { FirmwareArtifactReference } from '../../types/api/firmwareUpdate';
18
- import type { FirmwareUpdatePlan } from '../../types/api/firmwareUpdatePlan';
19
-
20
- const preparedPlanError = (message: string): never => {
21
- throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, message, {
22
- firmwareUpdateCode: 'FirmwarePreparedPlanInvalid',
23
- });
24
- };
25
-
26
- const digestPreparedPlan = (plan: Omit<FirmwareUpdatePreparedPlan, 'preparedPlanDigest'>): string =>
27
- digestFirmwareUpdateContract(plan);
28
-
29
- const assertOpaqueLeaseRef = (value: unknown): string => {
30
- if (
31
- typeof value !== 'string' ||
32
- value.length === 0 ||
33
- value.length > 160 ||
34
- value.includes('/') ||
35
- value.includes('\\')
36
- ) {
37
- return preparedPlanError('Firmware prepared plan leaseRef must be opaque');
38
- }
39
- return value;
40
- };
41
-
42
- export const getFirmwareUpdateResourceName = (value: unknown): string => {
43
- const normalized = typeof value === 'string' ? value.normalize('NFC') : undefined;
44
- const parts = normalized?.split('/');
45
- const resourceName = parts?.at(-1);
46
- if (
47
- typeof value !== 'string' ||
48
- value.length === 0 ||
49
- value.length > 512 ||
50
- value !== normalized ||
51
- value.startsWith('/') ||
52
- value.includes('\\') ||
53
- value.includes(':') ||
54
- [...value].some(char => {
55
- const code = char.charCodeAt(0);
56
- return code <= 0x1f || code === 0x7f;
57
- }) ||
58
- !parts?.every(
59
- part =>
60
- part.length > 0 &&
61
- part !== '.' &&
62
- part !== '..' &&
63
- !part.endsWith('.') &&
64
- !part.endsWith(' ')
65
- ) ||
66
- !resourceName ||
67
- resourceName.length > 128
68
- ) {
69
- return preparedPlanError('Firmware prepared plan entry name is invalid');
70
- }
71
- return resourceName;
72
- };
73
-
74
- const assertPreparedEntry = (value: unknown): FirmwareUpdatePreparedEntry => {
75
- if (!value || typeof value !== 'object' || Array.isArray(value)) {
76
- return preparedPlanError('Firmware prepared plan entry must be an object');
77
- }
78
- const entry = value as Record<string, unknown>;
79
- if (
80
- Object.keys(entry).length !== 2 ||
81
- !Object.prototype.hasOwnProperty.call(entry, 'entryName') ||
82
- !Object.prototype.hasOwnProperty.call(entry, 'artifact')
83
- ) {
84
- return preparedPlanError('Firmware prepared plan entry fields are invalid');
85
- }
86
- getFirmwareUpdateResourceName(entry.entryName);
87
- assertFirmwareArtifactReference(entry.artifact as FirmwareUpdatePreparedEntry['artifact']);
88
- return value as FirmwareUpdatePreparedEntry;
89
- };
90
-
91
- const assertPreparedArtifacts = ({
92
- plan,
93
- artifacts,
94
- }: {
95
- plan: FirmwareUpdatePlan;
96
- artifacts: readonly FirmwareUpdatePreparedArtifactInput[];
97
- }): FirmwareUpdatePreparedArtifact[] => {
98
- if (artifacts.length !== plan.artifacts.length) {
99
- return preparedPlanError('Firmware prepared plan does not cover every artifact');
100
- }
101
- const inputs = new Map<string, FirmwareUpdatePreparedArtifactInput>();
102
- for (const input of artifacts) {
103
- if (
104
- !input ||
105
- typeof input !== 'object' ||
106
- typeof input.artifactId !== 'string' ||
107
- inputs.has(input.artifactId)
108
- ) {
109
- return preparedPlanError('Firmware prepared plan contains a duplicate artifact receipt');
110
- }
111
- inputs.set(input.artifactId, input);
112
- }
113
- return plan.artifacts.map(planArtifact => {
114
- const input = inputs.get(planArtifact.artifactId);
115
- if (!input) {
116
- return preparedPlanError('Firmware prepared plan artifact receipt is missing');
117
- }
118
- const artifact = assertFirmwareArtifactReference(input.artifact);
119
- if (
120
- (planArtifact.expectedSize !== undefined && artifact.size !== planArtifact.expectedSize) ||
121
- (planArtifact.expectedSha256 !== undefined &&
122
- artifact.sha256.toLowerCase() !== planArtifact.expectedSha256.toLowerCase())
123
- ) {
124
- return preparedPlanError('Firmware prepared plan receipt does not match the update plan');
125
- }
126
- const materializedEntries = input.materializedEntries?.map(value => {
127
- const entry = assertPreparedEntry(value);
128
- return {
129
- entryName: entry.entryName,
130
- artifact: {
131
- ...entry.artifact,
132
- sha256: entry.artifact.sha256.toLowerCase(),
133
- },
134
- };
135
- });
136
- if (
137
- (planArtifact.container === 'raw' && materializedEntries?.length) ||
138
- (planArtifact.container === 'zip' && !materializedEntries?.length)
139
- ) {
140
- return preparedPlanError('Firmware prepared plan materialization is incomplete');
141
- }
142
- if (
143
- materializedEntries &&
144
- new Set(
145
- materializedEntries.map(entry =>
146
- getFirmwareUpdateResourceName(entry.entryName).toLowerCase()
147
- )
148
- ).size !== materializedEntries.length
149
- ) {
150
- return preparedPlanError('Firmware prepared plan contains duplicate entry names');
151
- }
152
- return {
153
- artifactId: planArtifact.artifactId,
154
- role: planArtifact.role,
155
- target: planArtifact.target,
156
- container: planArtifact.container,
157
- ...(planArtifact.logicalName ? { logicalName: planArtifact.logicalName } : {}),
158
- ...(planArtifact.targetVersion ? { targetVersion: planArtifact.targetVersion } : {}),
159
- artifact: {
160
- ...artifact,
161
- sha256: artifact.sha256.toLowerCase(),
162
- },
163
- ...(materializedEntries?.length ? { materializedEntries } : {}),
164
- };
165
- });
166
- };
167
-
168
- export const prepareFirmwareUpdatePlan = ({
169
- plan: value,
170
- leaseRef,
171
- artifacts,
172
- }: {
173
- plan: FirmwareUpdatePlan;
174
- leaseRef: string;
175
- artifacts: FirmwareUpdatePreparedArtifactInput[];
176
- }): FirmwareUpdatePreparedPlan => {
177
- const plan = assertFirmwareUpdatePlan(value);
178
- const planWithoutDigest: Omit<FirmwareUpdatePreparedPlan, 'preparedPlanDigest'> = {
179
- schemaVersion: 2,
180
- planDigest: plan.planDigest,
181
- networkPolicy: 'forbid',
182
- executor: plan.executor,
183
- deviceIdentity: plan.deviceIdentity,
184
- deviceModel: plan.deviceModel,
185
- firmwareType: plan.firmwareType,
186
- platform: plan.platform,
187
- leaseRef: assertOpaqueLeaseRef(leaseRef),
188
- artifacts: assertPreparedArtifacts({ plan, artifacts }),
189
- targetsToUpdate: plan.targetsToUpdate,
190
- };
191
- return {
192
- ...planWithoutDigest,
193
- preparedPlanDigest: digestPreparedPlan(planWithoutDigest),
194
- };
195
- };
196
-
197
- export const validateFirmwareUpdatePreparedPlan = (value: unknown): FirmwareUpdatePreparedPlan => {
198
- if (!value || typeof value !== 'object' || Array.isArray(value)) {
199
- return preparedPlanError('Firmware prepared plan must be an object');
200
- }
201
- const preparedPlan = value as FirmwareUpdatePreparedPlan;
202
- const exactKeys = [
203
- 'schemaVersion',
204
- 'preparedPlanDigest',
205
- 'planDigest',
206
- 'networkPolicy',
207
- 'executor',
208
- 'deviceIdentity',
209
- 'deviceModel',
210
- 'firmwareType',
211
- 'platform',
212
- 'leaseRef',
213
- 'artifacts',
214
- 'targetsToUpdate',
215
- ];
216
- if (
217
- Object.keys(value).length !== exactKeys.length ||
218
- exactKeys.some(key => !Object.prototype.hasOwnProperty.call(value, key)) ||
219
- preparedPlan.schemaVersion !== 2 ||
220
- preparedPlan.networkPolicy !== 'forbid' ||
221
- !/^[a-f0-9]{64}$/u.test(preparedPlan.planDigest) ||
222
- !/^[a-f0-9]{64}$/u.test(preparedPlan.preparedPlanDigest) ||
223
- !Array.isArray(preparedPlan.artifacts) ||
224
- !Array.isArray(preparedPlan.targetsToUpdate) ||
225
- (preparedPlan.executor !== 'v2' &&
226
- preparedPlan.executor !== 'v3' &&
227
- preparedPlan.executor !== 'v4') ||
228
- (preparedPlan.platform !== 'native' &&
229
- preparedPlan.platform !== 'desktop' &&
230
- preparedPlan.platform !== 'ext' &&
231
- preparedPlan.platform !== 'web' &&
232
- preparedPlan.platform !== 'web-embed') ||
233
- (preparedPlan.firmwareType !== 'universal' && preparedPlan.firmwareType !== 'bitcoinonly') ||
234
- typeof preparedPlan.deviceIdentity !== 'string' ||
235
- preparedPlan.deviceIdentity.length === 0 ||
236
- typeof preparedPlan.deviceModel !== 'string' ||
237
- preparedPlan.deviceModel.length === 0
238
- ) {
239
- return preparedPlanError('Firmware prepared plan header is invalid');
240
- }
241
- assertOpaqueLeaseRef(preparedPlan.leaseRef);
242
- const artifactIds = new Set<string>();
243
- const artifactTargets = new Set<string>();
244
- for (const artifact of preparedPlan.artifacts) {
245
- if (!artifact || typeof artifact !== 'object') {
246
- return preparedPlanError('Firmware prepared plan artifact is invalid');
247
- }
248
- const artifactKeys = [
249
- 'artifactId',
250
- 'role',
251
- 'target',
252
- 'container',
253
- 'artifact',
254
- ...(artifact.logicalName !== undefined ? ['logicalName'] : []),
255
- ...(artifact.targetVersion !== undefined ? ['targetVersion'] : []),
256
- ...(artifact.materializedEntries !== undefined ? ['materializedEntries'] : []),
257
- ];
258
- if (
259
- typeof artifact.artifactId !== 'string' ||
260
- artifact.artifactId.length === 0 ||
261
- artifactIds.has(artifact.artifactId) ||
262
- Object.keys(artifact).length !== artifactKeys.length ||
263
- artifactKeys.some(key => !Object.prototype.hasOwnProperty.call(artifact, key)) ||
264
- !FIRMWARE_UPDATE_PLAN_ROLES.has(artifact.role) ||
265
- !FIRMWARE_UPDATE_PLAN_TARGETS.has(artifact.target) ||
266
- (artifact.container !== 'raw' && artifact.container !== 'zip') ||
267
- (artifact.logicalName !== undefined &&
268
- (typeof artifact.logicalName !== 'string' || artifact.logicalName.length === 0)) ||
269
- (artifact.targetVersion !== undefined &&
270
- (typeof artifact.targetVersion !== 'string' || artifact.targetVersion.length === 0)) ||
271
- (artifact.materializedEntries !== undefined &&
272
- !Array.isArray(artifact.materializedEntries)) ||
273
- (artifact.container === 'raw' && Boolean(artifact.materializedEntries?.length)) ||
274
- (artifact.container === 'zip' && !artifact.materializedEntries?.length)
275
- ) {
276
- return preparedPlanError('Firmware prepared plan artifact is invalid');
277
- }
278
- artifactIds.add(artifact.artifactId);
279
- artifactTargets.add(artifact.target);
280
- assertFirmwareArtifactReference(artifact.artifact);
281
- artifact.materializedEntries?.forEach(assertPreparedEntry);
282
- }
283
- if (
284
- preparedPlan.targetsToUpdate.some(target => !FIRMWARE_UPDATE_PLAN_TARGETS.has(target)) ||
285
- new Set(preparedPlan.targetsToUpdate).size !== preparedPlan.targetsToUpdate.length ||
286
- preparedPlan.targetsToUpdate.length !== artifactTargets.size ||
287
- preparedPlan.targetsToUpdate.some(target => !artifactTargets.has(target))
288
- ) {
289
- return preparedPlanError('Firmware prepared plan targets are invalid');
290
- }
291
- const { preparedPlanDigest, ...withoutDigest } = preparedPlan;
292
- if (digestPreparedPlan(withoutDigest) !== preparedPlanDigest) {
293
- return preparedPlanError('Firmware prepared plan digest is invalid');
294
- }
295
- return preparedPlan;
296
- };
297
-
298
- export const assertFirmwareUpdatePreparedPlanDeviceIdentity = ({
299
- preparedPlan: value,
300
- deviceIdentity,
301
- }: {
302
- preparedPlan: unknown;
303
- deviceIdentity: string | undefined;
304
- }): FirmwareUpdatePreparedPlan => {
305
- const preparedPlan = validateFirmwareUpdatePreparedPlan(value);
306
- if (!deviceIdentity || preparedPlan.deviceIdentity !== deviceIdentity) {
307
- throw ERRORS.TypedError(HardwareErrorCode.DeviceCheckDeviceIdError);
308
- }
309
- return preparedPlan;
310
- };
311
-
312
- type FirmwareUpdatePreparedBinding = {
313
- target: FirmwareUpdatePlan['targetsToUpdate'][number];
314
- artifact: FirmwareArtifactReference;
315
- logicalName?: string;
316
- entryName?: string;
317
- };
318
-
319
- const sameArtifactReference = (
320
- first: FirmwareArtifactReference,
321
- second: FirmwareArtifactReference
322
- ) =>
323
- first.artifactRef === second.artifactRef &&
324
- first.size === second.size &&
325
- first.sha256.toLowerCase() === second.sha256.toLowerCase();
326
-
327
- export const assertFirmwareUpdatePreparedPlanBinding = ({
328
- preparedPlan: value,
329
- executor,
330
- platform,
331
- deviceIdentity,
332
- scopeTargets,
333
- bindings,
334
- }: {
335
- preparedPlan: unknown;
336
- executor: FirmwareUpdatePreparedPlan['executor'];
337
- platform?: FirmwareUpdatePreparedPlan['platform'];
338
- deviceIdentity?: string;
339
- scopeTargets: FirmwareUpdatePreparedPlan['targetsToUpdate'];
340
- bindings: FirmwareUpdatePreparedBinding[];
341
- }): FirmwareUpdatePreparedPlan => {
342
- const preparedPlan = validateFirmwareUpdatePreparedPlan(value);
343
- if (
344
- preparedPlan.executor !== executor ||
345
- (platform !== undefined && preparedPlan.platform !== platform) ||
346
- (deviceIdentity !== undefined && preparedPlan.deviceIdentity !== deviceIdentity)
347
- ) {
348
- return preparedPlanError('Firmware prepared plan executor binding is invalid');
349
- }
350
- const scope = new Set(scopeTargets);
351
- const expectedBindings: FirmwareUpdatePreparedBinding[] = [];
352
- for (const artifact of preparedPlan.artifacts) {
353
- if (scope.has(artifact.target)) {
354
- if (artifact.container === 'zip') {
355
- for (const entry of artifact.materializedEntries ?? []) {
356
- expectedBindings.push({
357
- target: artifact.target,
358
- ...(artifact.target === 'resource' && artifact.logicalName
359
- ? { logicalName: artifact.logicalName }
360
- : {}),
361
- entryName: entry.entryName,
362
- artifact: entry.artifact,
363
- });
364
- }
365
- } else {
366
- expectedBindings.push({
367
- target: artifact.target,
368
- ...(artifact.target === 'resource' && artifact.logicalName
369
- ? { logicalName: artifact.logicalName }
370
- : {}),
371
- artifact: artifact.artifact,
372
- });
373
- }
374
- }
375
- }
376
- if (
377
- expectedBindings.length !== bindings.length ||
378
- expectedBindings.some(expected => {
379
- const matching = bindings.filter(
380
- binding =>
381
- binding.target === expected.target &&
382
- binding.logicalName === expected.logicalName &&
383
- binding.entryName === expected.entryName &&
384
- sameArtifactReference(binding.artifact, expected.artifact)
385
- );
386
- return matching.length !== 1;
387
- })
388
- ) {
389
- return preparedPlanError('Firmware prepared plan artifact binding is invalid');
390
- }
391
- return preparedPlan;
392
- };
@@ -1,44 +0,0 @@
1
- import type { IFirmwareUpdateProgressType } from '../../events/ui-request';
2
-
3
- const DEFAULT_FIRMWARE_PROGRESS_INTERVAL_MS = 250;
4
-
5
- type FirmwareProgressThrottleState = {
6
- lastEmittedAt: number;
7
- lastProgress?: number;
8
- };
9
-
10
- export const createFirmwareProgressThrottle = (
11
- intervalMs = DEFAULT_FIRMWARE_PROGRESS_INTERVAL_MS
12
- ) => {
13
- const stateByType = new Map<IFirmwareUpdateProgressType, FirmwareProgressThrottleState>();
14
-
15
- return (progress: number, progressType: IFirmwareUpdateProgressType) => {
16
- if (progressType !== 'transferData') {
17
- return true;
18
- }
19
-
20
- const normalizedProgress = Math.max(0, Math.min(100, Math.floor(progress)));
21
- if (normalizedProgress === 0 || normalizedProgress === 100) {
22
- stateByType.set(progressType, {
23
- lastEmittedAt: Date.now(),
24
- lastProgress: normalizedProgress,
25
- });
26
- return true;
27
- }
28
-
29
- const now = Date.now();
30
- const state = stateByType.get(progressType);
31
- if (
32
- state &&
33
- (state.lastProgress === normalizedProgress || now - state.lastEmittedAt < intervalMs)
34
- ) {
35
- return false;
36
- }
37
-
38
- stateByType.set(progressType, {
39
- lastEmittedAt: now,
40
- lastProgress: normalizedProgress,
41
- });
42
- return true;
43
- };
44
- };
@@ -1,10 +0,0 @@
1
- import { HardwareErrorCode } from '@onekeyfe/hd-shared';
2
-
3
- export function isDeviceLockedError(error: unknown): boolean {
4
- return (
5
- typeof error === 'object' &&
6
- error !== null &&
7
- 'errorCode' in error &&
8
- error.errorCode === HardwareErrorCode.DeviceLocked
9
- );
10
- }
@@ -1,109 +0,0 @@
1
- import { LoggerNames, getLogger } from '../../utils';
2
- import { isDeviceLockedError } from './lockedError';
3
- import { isProtocolV2UiEnabled, resolveProtocolV2UiInteraction } from './uiInteraction';
4
- import { refreshProtocolV2DeviceStatus, restoreProtocolV2WalletSession } from './walletSession';
5
-
6
- import type { BaseMethod } from '../../api/BaseMethod';
7
- import type { Device } from '../../device/Device';
8
- import type { ProtocolV2UiInteractionCoordinator } from './uiInteraction';
9
-
10
- const Log = getLogger(LoggerNames.Core);
11
-
12
- type RunnableMethod = Pick<
13
- BaseMethod,
14
- | 'run'
15
- | 'unlockPolicy'
16
- | 'protocolV2UiInteraction'
17
- | 'protocolV2UiMode'
18
- | 'params'
19
- | 'payload'
20
- | 'useDevicePassphraseState'
21
- > & { name?: string };
22
- type UiInteractionCoordinator = Pick<
23
- ProtocolV2UiInteractionCoordinator,
24
- 'enterMethodInteraction' | 'enterUnlockInteraction' | 'resumeMethodInteraction'
25
- >;
26
-
27
- const restoreExpectedWalletSessionAfterUnlock = async (method: RunnableMethod, device: Device) => {
28
- const expectedPassphraseState = method.payload?.useEmptyPassphrase
29
- ? undefined
30
- : method.payload?.passphraseState ?? device.passphraseState;
31
- if (
32
- !method.useDevicePassphraseState ||
33
- typeof expectedPassphraseState !== 'string' ||
34
- expectedPassphraseState.length === 0
35
- ) {
36
- return;
37
- }
38
-
39
- await restoreProtocolV2WalletSession(device, expectedPassphraseState);
40
- Log.debug('Protocol V2 wallet session restored after unlock', { method: method.name });
41
- };
42
-
43
- export async function runMethodWithUnlockRetry(
44
- method: RunnableMethod,
45
- device: Device,
46
- uiCoordinator?: UiInteractionCoordinator
47
- ) {
48
- const shouldEmitUi = isProtocolV2UiEnabled(method);
49
- const isProtocolV2 = device.isProtocolV2();
50
- const requiresFreshStatus =
51
- isProtocolV2 &&
52
- method.unlockPolicy === 'unlock-before-run' &&
53
- !device.isBootloader?.() &&
54
- !device.isRomloader?.();
55
-
56
- if (requiresFreshStatus) {
57
- await refreshProtocolV2DeviceStatus(device);
58
- }
59
-
60
- const shouldUnlockBeforeRun =
61
- isProtocolV2 &&
62
- method.unlockPolicy !== 'none' &&
63
- !device.isBootloader?.() &&
64
- !device.isRomloader?.() &&
65
- device.features?.unlocked === false;
66
-
67
- if (shouldUnlockBeforeRun) {
68
- if (shouldEmitUi) {
69
- uiCoordinator?.enterUnlockInteraction(method.name);
70
- }
71
- await device.unlockDevice();
72
- Log.debug('Protocol V2 pre-unlock completed', { method: method.name });
73
- await restoreExpectedWalletSessionAfterUnlock(method, device);
74
- if (shouldEmitUi) {
75
- uiCoordinator?.enterMethodInteraction(resolveProtocolV2UiInteraction(method));
76
- }
77
- return method.run();
78
- }
79
-
80
- if (shouldEmitUi) {
81
- uiCoordinator?.enterMethodInteraction(resolveProtocolV2UiInteraction(method));
82
- }
83
- try {
84
- return await method.run();
85
- } catch (error) {
86
- if (!isProtocolV2 || method.unlockPolicy !== 'retry-on-locked' || !isDeviceLockedError(error)) {
87
- throw error;
88
- }
89
-
90
- Log.debug('Protocol V2 unlock retry triggered', { method: method.name });
91
- if (shouldEmitUi) {
92
- uiCoordinator?.enterUnlockInteraction(method.name);
93
- }
94
- await device.unlockDevice();
95
- Log.debug('Protocol V2 unlock completed', { method: method.name });
96
- await restoreExpectedWalletSessionAfterUnlock(method, device);
97
- if (shouldEmitUi) {
98
- uiCoordinator?.resumeMethodInteraction();
99
- }
100
- try {
101
- const response = await method.run();
102
- Log.debug('Protocol V2 method retry completed', { method: method.name, success: true });
103
- return response;
104
- } catch (retryError) {
105
- Log.debug('Protocol V2 method retry completed', { method: method.name, success: false });
106
- throw retryError;
107
- }
108
- }
109
- }
@@ -1,9 +0,0 @@
1
- export interface FirmwareUpdateCapabilities {
2
- planSchemaVersion: 2;
3
- preparedPlanSchemaVersion: 2;
4
- hostBindingProtocolVersion: 2;
5
- manifestModes: Array<'external-only' | 'sdk-managed'>;
6
- supportsArtifactReader: true;
7
- }
8
-
9
- export declare function getFirmwareUpdateCapabilities(): FirmwareUpdateCapabilities;
@@ -1,38 +0,0 @@
1
- import type { FirmwareUpdateV4Target } from './firmwareUpdate';
2
- import type { EFirmwareType } from '@onekeyfe/hd-shared';
3
-
4
- export type FirmwareUpdatePlanArtifactRole =
5
- | 'firmware'
6
- | 'ble'
7
- | 'bootloader'
8
- | 'resource'
9
- | 'component'
10
- | 'resourceBundle';
11
-
12
- export type FirmwareUpdatePlanTarget = 'firmware' | 'ble' | 'bootloader' | FirmwareUpdateV4Target;
13
-
14
- export type FirmwareUpdatePlanForceTarget = 'firmware' | 'ble' | 'bootloader' | 'resource';
15
-
16
- export interface FirmwareUpdatePlanArtifact {
17
- artifactId: string;
18
- role: FirmwareUpdatePlanArtifactRole;
19
- target: FirmwareUpdatePlanTarget;
20
- url: string;
21
- container: 'raw' | 'zip';
22
- logicalName?: string;
23
- expectedSize?: number;
24
- expectedSha256?: string;
25
- targetVersion?: string;
26
- }
27
-
28
- export interface FirmwareUpdatePlan {
29
- schemaVersion: 2;
30
- planDigest: string;
31
- executor: 'v2' | 'v3' | 'v4';
32
- deviceIdentity: string;
33
- deviceModel: string;
34
- firmwareType: EFirmwareType;
35
- platform: 'native' | 'desktop' | 'ext' | 'web' | 'web-embed';
36
- artifacts: FirmwareUpdatePlanArtifact[];
37
- targetsToUpdate: FirmwareUpdatePlanTarget[];
38
- }
@@ -1,53 +0,0 @@
1
- import type { FirmwareArtifactReference } from './firmwareUpdate';
2
- import type {
3
- FirmwareUpdatePlan,
4
- FirmwareUpdatePlanArtifactRole,
5
- FirmwareUpdatePlanTarget,
6
- } from './firmwareUpdatePlan';
7
-
8
- export interface FirmwareUpdatePreparedEntry {
9
- entryName: string;
10
- artifact: FirmwareArtifactReference;
11
- }
12
-
13
- export interface FirmwareUpdatePreparedArtifactInput {
14
- artifactId: string;
15
- artifact: FirmwareArtifactReference;
16
- materializedEntries?: FirmwareUpdatePreparedEntry[];
17
- }
18
-
19
- export interface FirmwareUpdatePreparedArtifact {
20
- artifactId: string;
21
- role: FirmwareUpdatePlanArtifactRole;
22
- target: FirmwareUpdatePlanTarget;
23
- container: 'raw' | 'zip';
24
- logicalName?: string;
25
- targetVersion?: string;
26
- artifact: FirmwareArtifactReference;
27
- materializedEntries?: FirmwareUpdatePreparedEntry[];
28
- }
29
-
30
- export interface FirmwareUpdatePreparedPlan {
31
- schemaVersion: 2;
32
- preparedPlanDigest: string;
33
- planDigest: string;
34
- networkPolicy: 'forbid';
35
- executor: FirmwareUpdatePlan['executor'];
36
- deviceIdentity: string;
37
- deviceModel: string;
38
- firmwareType: FirmwareUpdatePlan['firmwareType'];
39
- platform: FirmwareUpdatePlan['platform'];
40
- leaseRef: string;
41
- artifacts: FirmwareUpdatePreparedArtifact[];
42
- targetsToUpdate: FirmwareUpdatePlanTarget[];
43
- }
44
-
45
- export declare function prepareFirmwareUpdatePlan(input: {
46
- plan: FirmwareUpdatePlan;
47
- leaseRef: string;
48
- artifacts: FirmwareUpdatePreparedArtifactInput[];
49
- }): FirmwareUpdatePreparedPlan;
50
-
51
- export declare function validateFirmwareUpdatePreparedPlan(
52
- value: unknown
53
- ): FirmwareUpdatePreparedPlan;