@onekeyfe/hd-core 1.2.0-alpha.52 → 1.2.0-alpha.53

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 (251) hide show
  1. package/__tests__/AllNetworkGetAddressBase.tracing.test.ts +78 -0
  2. package/__tests__/DeviceCommands.test.ts +45 -0
  3. package/__tests__/DeviceEventRegistration.test.ts +6 -0
  4. package/__tests__/check-all-firmware-release-protocol-v2.test.ts +198 -3
  5. package/__tests__/connectSettings.test.ts +5 -47
  6. package/__tests__/device-lifecycle-events.test.ts +105 -3
  7. package/__tests__/deviceUploadNft.test.ts +293 -0
  8. package/__tests__/firmware-update/firmware-update-v2-download-before-boot.test.ts +31 -59
  9. package/__tests__/get-device-state.test.ts +132 -12
  10. package/__tests__/homescreen.test.ts +41 -1
  11. package/__tests__/logBlockEvent.test.ts +1 -1
  12. package/__tests__/method-protocol-support.test.ts +19 -0
  13. package/__tests__/open-wallet-session.test.ts +446 -33
  14. package/__tests__/pro2Nft.test.ts +108 -0
  15. package/__tests__/protocol-binding.test.ts +89 -0
  16. package/__tests__/protocol-v2-resources.test.ts +284 -0
  17. package/__tests__/protocol-v2-ui-lifecycle.test.ts +91 -0
  18. package/__tests__/protocol-v2-unlock-policy.test.ts +201 -35
  19. package/__tests__/protocol-v2.test.ts +1349 -1005
  20. package/__tests__/protocolV2UiInteraction.test.ts +63 -0
  21. package/__tests__/public-pro2-api-boundary.test.ts +1 -0
  22. package/__tests__/search-devices.test.ts +12 -3
  23. package/__tests__/uiProgressThrottle.test.ts +74 -0
  24. package/__tests__/uiPromiseRegistry.test.ts +86 -0
  25. package/dist/api/BaseMethod.d.ts +3 -1
  26. package/dist/api/BaseMethod.d.ts.map +1 -1
  27. package/dist/api/CheckAllFirmwareRelease.d.ts +2 -1
  28. package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
  29. package/dist/api/DetectDeviceConnectProtocol.d.ts +7 -0
  30. package/dist/api/DetectDeviceConnectProtocol.d.ts.map +1 -0
  31. package/dist/api/FirmwareUpdate.d.ts.map +1 -1
  32. package/dist/api/FirmwareUpdateV2.d.ts +0 -11
  33. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  34. package/dist/api/FirmwareUpdateV3.d.ts +3 -7
  35. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  36. package/dist/api/FirmwareUpdateV4.d.ts +11 -25
  37. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  38. package/dist/api/GetPassphraseState.d.ts.map +1 -1
  39. package/dist/api/OpenWalletSession.d.ts.map +1 -1
  40. package/dist/api/SearchDevices.d.ts.map +1 -1
  41. package/dist/api/allnetwork/AllNetworkGetAddressBase.d.ts.map +1 -1
  42. package/dist/api/device/DeviceUnlock.d.ts +2 -2
  43. package/dist/api/device/DeviceUnlock.d.ts.map +1 -1
  44. package/dist/api/device/DeviceUpdateBootloader.d.ts +1 -5
  45. package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
  46. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +0 -9
  47. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  48. package/dist/api/firmware/getBinary.d.ts +0 -7
  49. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  50. package/dist/api/firmware/updateBootloader.d.ts +0 -2
  51. package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
  52. package/dist/api/firmware/uploadFirmware.d.ts +1 -9
  53. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  54. package/dist/api/helpers/protocolV2FileWrite.d.ts +2 -0
  55. package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -1
  56. package/dist/api/index.d.ts +2 -0
  57. package/dist/api/index.d.ts.map +1 -1
  58. package/dist/api/protocol-v2/DeviceUploadNft.d.ts +31 -0
  59. package/dist/api/protocol-v2/DeviceUploadNft.d.ts.map +1 -0
  60. package/dist/api/protocol-v2/ProtocolInfoRequest.d.ts.map +1 -1
  61. package/dist/api/protocol-v2/helpers.d.ts.map +1 -1
  62. package/dist/api/stellar/StellarSignTransaction.d.ts +1 -1
  63. package/dist/core/deviceEventRegistration.d.ts +2 -0
  64. package/dist/core/deviceEventRegistration.d.ts.map +1 -1
  65. package/dist/core/index.d.ts.map +1 -1
  66. package/dist/core/uiPromiseRegistry.d.ts +4 -0
  67. package/dist/core/uiPromiseRegistry.d.ts.map +1 -0
  68. package/dist/data-manager/DataManager.d.ts +4 -2
  69. package/dist/data-manager/DataManager.d.ts.map +1 -1
  70. package/dist/data-manager/connectSettings.d.ts.map +1 -1
  71. package/dist/device/Device.d.ts +29 -6
  72. package/dist/device/Device.d.ts.map +1 -1
  73. package/dist/device/DeviceCommands.d.ts +4 -4
  74. package/dist/device/DeviceCommands.d.ts.map +1 -1
  75. package/dist/device/DevicePool.d.ts.map +1 -1
  76. package/dist/events/device.d.ts +4 -0
  77. package/dist/events/device.d.ts.map +1 -1
  78. package/dist/events/logBlockEvent.d.ts.map +1 -1
  79. package/dist/events/ui-promise.d.ts +4 -2
  80. package/dist/events/ui-promise.d.ts.map +1 -1
  81. package/dist/events/ui-request.d.ts +31 -2
  82. package/dist/events/ui-request.d.ts.map +1 -1
  83. package/dist/events/ui-response.d.ts +10 -2
  84. package/dist/events/ui-response.d.ts.map +1 -1
  85. package/dist/index.d.ts +214 -231
  86. package/dist/index.d.ts.map +1 -1
  87. package/dist/index.js +2559 -3497
  88. package/dist/inject.d.ts +6 -1
  89. package/dist/inject.d.ts.map +1 -1
  90. package/dist/lowLevelInject.d.ts.map +1 -1
  91. package/dist/protocols/protocol-v2/features.d.ts.map +1 -1
  92. package/dist/protocols/protocol-v2/resources.d.ts +30 -0
  93. package/dist/protocols/protocol-v2/resources.d.ts.map +1 -0
  94. package/dist/protocols/protocol-v2/uiInteraction.d.ts +7 -4
  95. package/dist/protocols/protocol-v2/uiInteraction.d.ts.map +1 -1
  96. package/dist/protocols/protocol-v2/unlockPolicy.d.ts +1 -3
  97. package/dist/protocols/protocol-v2/unlockPolicy.d.ts.map +1 -1
  98. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts +22 -0
  99. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts.map +1 -0
  100. package/dist/protocols/protocol-v2/walletSession.d.ts +5 -1
  101. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  102. package/dist/topLevelInject.d.ts.map +1 -1
  103. package/dist/types/api/checkAllFirmwareRelease.d.ts +2 -4
  104. package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
  105. package/dist/types/api/detectDeviceConnectProtocol.d.ts +4 -0
  106. package/dist/types/api/detectDeviceConnectProtocol.d.ts.map +1 -0
  107. package/dist/types/api/deviceUnlock.d.ts +5 -1
  108. package/dist/types/api/deviceUnlock.d.ts.map +1 -1
  109. package/dist/types/api/deviceUpdateBootloader.d.ts +0 -6
  110. package/dist/types/api/deviceUpdateBootloader.d.ts.map +1 -1
  111. package/dist/types/api/export.d.ts +2 -4
  112. package/dist/types/api/export.d.ts.map +1 -1
  113. package/dist/types/api/firmwareUpdate.d.ts +2 -70
  114. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  115. package/dist/types/api/index.d.ts +7 -10
  116. package/dist/types/api/index.d.ts.map +1 -1
  117. package/dist/types/api/protocolV2.d.ts +3 -0
  118. package/dist/types/api/protocolV2.d.ts.map +1 -1
  119. package/dist/types/device.d.ts.map +1 -1
  120. package/dist/types/params.d.ts +1 -0
  121. package/dist/types/params.d.ts.map +1 -1
  122. package/dist/types/settings.d.ts +22 -13
  123. package/dist/types/settings.d.ts.map +1 -1
  124. package/dist/utils/deviceFeaturesCompat.d.ts +0 -1
  125. package/dist/utils/deviceFeaturesCompat.d.ts.map +1 -1
  126. package/dist/utils/deviceFeaturesUtils.d.ts +4 -0
  127. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  128. package/dist/utils/homescreen.d.ts +4 -0
  129. package/dist/utils/homescreen.d.ts.map +1 -1
  130. package/dist/utils/index.d.ts +1 -1
  131. package/dist/utils/index.d.ts.map +1 -1
  132. package/dist/utils/patch.d.ts +1 -1
  133. package/dist/utils/patch.d.ts.map +1 -1
  134. package/dist/utils/pro2Nft.d.ts +31 -0
  135. package/dist/utils/pro2Nft.d.ts.map +1 -0
  136. package/dist/utils/pro2Wallpaper.d.ts +10 -0
  137. package/dist/utils/pro2Wallpaper.d.ts.map +1 -1
  138. package/dist/utils/uiProgressThrottle.d.ts +3 -0
  139. package/dist/utils/uiProgressThrottle.d.ts.map +1 -0
  140. package/package.json +4 -4
  141. package/src/api/BaseMethod.ts +8 -10
  142. package/src/api/CheckAllFirmwareRelease.ts +54 -57
  143. package/src/api/DetectDeviceConnectProtocol.ts +18 -0
  144. package/src/api/FirmwareUpdate.ts +7 -15
  145. package/src/api/FirmwareUpdateV2.ts +126 -316
  146. package/src/api/FirmwareUpdateV3.ts +63 -265
  147. package/src/api/FirmwareUpdateV4.ts +566 -965
  148. package/src/api/GetPassphraseState.ts +7 -1
  149. package/src/api/OpenWalletSession.ts +54 -17
  150. package/src/api/SearchDevices.ts +3 -1
  151. package/src/api/allnetwork/AllNetworkGetAddressBase.ts +32 -11
  152. package/src/api/device/DeviceUnlock.ts +8 -3
  153. package/src/api/device/DeviceUpdateBootloader.ts +6 -122
  154. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +16 -83
  155. package/src/api/firmware/updateBootloader.ts +4 -19
  156. package/src/api/firmware/uploadFirmware.ts +39 -144
  157. package/src/api/helpers/protocolV2FileWrite.ts +11 -4
  158. package/src/api/index.ts +2 -0
  159. package/src/api/protocol-v2/DeviceUploadNft.ts +189 -0
  160. package/src/api/protocol-v2/ProtocolInfoRequest.ts +3 -1
  161. package/src/api/protocol-v2/helpers.ts +1 -0
  162. package/src/core/deviceEventRegistration.ts +4 -0
  163. package/src/core/index.ts +167 -83
  164. package/src/core/uiPromiseRegistry.ts +41 -0
  165. package/src/data/messages/messages-protocol-v2.json +25 -0
  166. package/src/data-manager/DataManager.ts +67 -38
  167. package/src/data-manager/connectSettings.ts +0 -11
  168. package/src/device/Device.ts +258 -42
  169. package/src/device/DeviceCommands.ts +7 -1
  170. package/src/device/DevicePool.ts +7 -2
  171. package/src/events/device.ts +4 -0
  172. package/src/events/logBlockEvent.ts +1 -0
  173. package/src/events/ui-promise.ts +4 -2
  174. package/src/events/ui-request.ts +36 -2
  175. package/src/events/ui-response.ts +12 -2
  176. package/src/index.ts +0 -5
  177. package/src/inject.ts +60 -24
  178. package/src/lowLevelInject.ts +7 -8
  179. package/src/protocols/protocol-v2/features.ts +10 -3
  180. package/src/protocols/protocol-v2/resources.ts +359 -0
  181. package/src/protocols/protocol-v2/uiInteraction.ts +46 -6
  182. package/src/protocols/protocol-v2/unlockPolicy.ts +1 -105
  183. package/src/protocols/protocol-v2/unlockPolicyRunner.ts +118 -0
  184. package/src/protocols/protocol-v2/walletSession.ts +180 -30
  185. package/src/topLevelInject.ts +7 -8
  186. package/src/types/api/checkAllFirmwareRelease.ts +2 -4
  187. package/src/types/api/detectDeviceConnectProtocol.ts +7 -0
  188. package/src/types/api/deviceUnlock.ts +12 -1
  189. package/src/types/api/deviceUpdateBootloader.ts +0 -6
  190. package/src/types/api/export.ts +1 -18
  191. package/src/types/api/firmwareUpdate.ts +3 -76
  192. package/src/types/api/index.ts +13 -14
  193. package/src/types/api/protocolV2.ts +13 -0
  194. package/src/types/device.ts +3 -0
  195. package/src/types/params.ts +7 -1
  196. package/src/types/settings.ts +42 -13
  197. package/src/utils/deviceFeaturesCompat.ts +0 -6
  198. package/src/utils/deviceFeaturesUtils.ts +8 -0
  199. package/src/utils/homescreen.ts +32 -6
  200. package/src/utils/index.ts +6 -1
  201. package/src/utils/pro2Nft.ts +142 -0
  202. package/src/utils/pro2Wallpaper.ts +35 -8
  203. package/src/utils/uiProgressThrottle.ts +63 -0
  204. package/__tests__/device-initialize-timeout.test.ts +0 -56
  205. package/__tests__/firmware-update/check-all-firmware-release.test.ts +0 -175
  206. package/__tests__/firmware-update/firmware-artifact-source.test.ts +0 -119
  207. package/__tests__/firmware-update/firmware-host-binding.test.ts +0 -70
  208. package/__tests__/firmware-update/firmware-preparation-error.test.ts +0 -27
  209. package/__tests__/firmware-update/firmware-update-bootloader-poll.test.ts +0 -200
  210. package/__tests__/firmware-update/firmware-update-capabilities.test.ts +0 -13
  211. package/__tests__/firmware-update/firmware-update-plan-bootloader-identity.test.ts +0 -294
  212. package/__tests__/firmware-update/firmware-update-plan.test.ts +0 -593
  213. package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +0 -213
  214. package/__tests__/firmware-update/firmware-update-v3-reconnect.test.ts +0 -58
  215. package/__tests__/firmware-update/firmware-upload-source.test.ts +0 -70
  216. package/dist/api/firmware/FirmwareArtifactSource.d.ts +0 -25
  217. package/dist/api/firmware/FirmwareArtifactSource.d.ts.map +0 -1
  218. package/dist/api/firmware/FirmwareHostBinding.d.ts +0 -6
  219. package/dist/api/firmware/FirmwareHostBinding.d.ts.map +0 -1
  220. package/dist/api/firmware/FirmwarePreparationError.d.ts +0 -3
  221. package/dist/api/firmware/FirmwarePreparationError.d.ts.map +0 -1
  222. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts +0 -3
  223. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts.map +0 -1
  224. package/dist/api/firmware/FirmwareUpdatePlan.d.ts +0 -25
  225. package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +0 -1
  226. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts +0 -32
  227. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +0 -1
  228. package/dist/api/firmware/progressThrottle.d.ts +0 -3
  229. package/dist/api/firmware/progressThrottle.d.ts.map +0 -1
  230. package/dist/protocols/protocol-v2/lockedError.d.ts +0 -2
  231. package/dist/protocols/protocol-v2/lockedError.d.ts.map +0 -1
  232. package/dist/protocols/protocol-v2/unlockRetry.d.ts +0 -10
  233. package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +0 -1
  234. package/dist/types/api/firmwareUpdateCapabilities.d.ts +0 -9
  235. package/dist/types/api/firmwareUpdateCapabilities.d.ts.map +0 -1
  236. package/dist/types/api/firmwareUpdatePlan.d.ts +0 -28
  237. package/dist/types/api/firmwareUpdatePlan.d.ts.map +0 -1
  238. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts +0 -42
  239. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts.map +0 -1
  240. package/src/api/firmware/FirmwareArtifactSource.ts +0 -278
  241. package/src/api/firmware/FirmwareHostBinding.ts +0 -100
  242. package/src/api/firmware/FirmwarePreparationError.ts +0 -12
  243. package/src/api/firmware/FirmwareUpdateCapabilities.ts +0 -9
  244. package/src/api/firmware/FirmwareUpdatePlan.ts +0 -772
  245. package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +0 -415
  246. package/src/api/firmware/progressThrottle.ts +0 -44
  247. package/src/protocols/protocol-v2/lockedError.ts +0 -10
  248. package/src/protocols/protocol-v2/unlockRetry.ts +0 -109
  249. package/src/types/api/firmwareUpdateCapabilities.ts +0 -9
  250. package/src/types/api/firmwareUpdatePlan.ts +0 -38
  251. package/src/types/api/firmwareUpdatePreparedPlan.ts +0 -53
@@ -1,415 +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: reportedIdentity,
301
- bootloaderMode,
302
- deviceModel,
303
- }: {
304
- preparedPlan: unknown;
305
- deviceIdentity: string | undefined;
306
- /** Live device mode; only bootloader mode may accept a degraded-identity plan. */
307
- bootloaderMode?: boolean;
308
- /** Live device model; a degraded plan must target the same model. */
309
- deviceModel?: string;
310
- }): FirmwareUpdatePreparedPlan => {
311
- const preparedPlan = validateFirmwareUpdatePreparedPlan(value);
312
- // 'unavailable' is the reserved degraded sentinel — a device-reported serial equal
313
- // to it must never satisfy the exact-match branch below.
314
- const deviceIdentity = reportedIdentity === 'unavailable' ? undefined : reportedIdentity;
315
- // Bootloader recovery: classic-family devices report no serial in bootloader mode,
316
- // so a degraded plan is only acceptable when the live device is equally
317
- // identity-less AND targets the same device model (identity equality implies the
318
- // model match in the strict branch; the degraded branch must substitute it).
319
- // A device that DOES report an identity must match the plan exactly.
320
- if (
321
- preparedPlan.deviceIdentity === 'unavailable' &&
322
- !deviceIdentity &&
323
- bootloaderMode === true &&
324
- deviceModel !== undefined &&
325
- preparedPlan.deviceModel === deviceModel
326
- ) {
327
- return preparedPlan;
328
- }
329
- if (!deviceIdentity || preparedPlan.deviceIdentity !== deviceIdentity) {
330
- throw ERRORS.TypedError(HardwareErrorCode.DeviceCheckDeviceIdError);
331
- }
332
- return preparedPlan;
333
- };
334
-
335
- type FirmwareUpdatePreparedBinding = {
336
- target: FirmwareUpdatePlan['targetsToUpdate'][number];
337
- artifact: FirmwareArtifactReference;
338
- logicalName?: string;
339
- entryName?: string;
340
- };
341
-
342
- const sameArtifactReference = (
343
- first: FirmwareArtifactReference,
344
- second: FirmwareArtifactReference
345
- ) =>
346
- first.artifactRef === second.artifactRef &&
347
- first.size === second.size &&
348
- first.sha256.toLowerCase() === second.sha256.toLowerCase();
349
-
350
- export const assertFirmwareUpdatePreparedPlanBinding = ({
351
- preparedPlan: value,
352
- executor,
353
- platform,
354
- deviceIdentity,
355
- scopeTargets,
356
- bindings,
357
- }: {
358
- preparedPlan: unknown;
359
- executor: FirmwareUpdatePreparedPlan['executor'];
360
- platform?: FirmwareUpdatePreparedPlan['platform'];
361
- deviceIdentity?: string;
362
- scopeTargets: FirmwareUpdatePreparedPlan['targetsToUpdate'];
363
- bindings: FirmwareUpdatePreparedBinding[];
364
- }): FirmwareUpdatePreparedPlan => {
365
- const preparedPlan = validateFirmwareUpdatePreparedPlan(value);
366
- if (
367
- preparedPlan.executor !== executor ||
368
- (platform !== undefined && preparedPlan.platform !== platform) ||
369
- (deviceIdentity !== undefined && preparedPlan.deviceIdentity !== deviceIdentity)
370
- ) {
371
- return preparedPlanError('Firmware prepared plan executor binding is invalid');
372
- }
373
- const scope = new Set(scopeTargets);
374
- const expectedBindings: FirmwareUpdatePreparedBinding[] = [];
375
- for (const artifact of preparedPlan.artifacts) {
376
- if (scope.has(artifact.target)) {
377
- if (artifact.container === 'zip') {
378
- for (const entry of artifact.materializedEntries ?? []) {
379
- expectedBindings.push({
380
- target: artifact.target,
381
- ...(artifact.target === 'resource' && artifact.logicalName
382
- ? { logicalName: artifact.logicalName }
383
- : {}),
384
- entryName: entry.entryName,
385
- artifact: entry.artifact,
386
- });
387
- }
388
- } else {
389
- expectedBindings.push({
390
- target: artifact.target,
391
- ...(artifact.target === 'resource' && artifact.logicalName
392
- ? { logicalName: artifact.logicalName }
393
- : {}),
394
- artifact: artifact.artifact,
395
- });
396
- }
397
- }
398
- }
399
- if (
400
- expectedBindings.length !== bindings.length ||
401
- expectedBindings.some(expected => {
402
- const matching = bindings.filter(
403
- binding =>
404
- binding.target === expected.target &&
405
- binding.logicalName === expected.logicalName &&
406
- binding.entryName === expected.entryName &&
407
- sameArtifactReference(binding.artifact, expected.artifact)
408
- );
409
- return matching.length !== 1;
410
- })
411
- ) {
412
- return preparedPlanError('Firmware prepared plan artifact binding is invalid');
413
- }
414
- return preparedPlan;
415
- };
@@ -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;