@onekeyfe/hd-core 1.2.0-alpha.64 → 1.2.0-alpha.66

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 (314) hide show
  1. package/__tests__/AllNetworkGetAddressBase.tracing.test.ts +313 -0
  2. package/__tests__/DeviceCommands.test.ts +59 -13
  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__/core-initialization.test.ts +23 -0
  7. package/__tests__/device-connector-protocol.test.ts +81 -0
  8. package/__tests__/device-lifecycle-events.test.ts +105 -3
  9. package/__tests__/device-pool-state.test.ts +22 -0
  10. package/__tests__/device-settings.test.ts +39 -18
  11. package/__tests__/device-state-contract.test.ts +1 -0
  12. package/__tests__/device-state-mapper.test.ts +13 -1
  13. package/__tests__/device-utils.test.ts +48 -1
  14. package/__tests__/deviceSettings.test.ts +11 -1
  15. package/__tests__/deviceUploadNft.test.ts +316 -0
  16. package/__tests__/firmware-update/firmware-update-v2-download-before-boot.test.ts +31 -59
  17. package/__tests__/get-device-state.test.ts +132 -12
  18. package/__tests__/homescreen.test.ts +41 -1
  19. package/__tests__/kaspa-use-tweak-pro2.test.ts +2 -2
  20. package/__tests__/kaspaSignTransaction.test.ts +3 -26
  21. package/__tests__/logBlockEvent.test.ts +1 -1
  22. package/__tests__/method-protocol-support.test.ts +19 -0
  23. package/__tests__/open-wallet-session.test.ts +568 -37
  24. package/__tests__/pro2Nft.test.ts +108 -0
  25. package/__tests__/protocol-binding.test.ts +89 -0
  26. package/__tests__/protocol-v2-resources.test.ts +340 -0
  27. package/__tests__/protocol-v2-ui-lifecycle.test.ts +91 -0
  28. package/__tests__/protocol-v2-unlock-policy.test.ts +300 -37
  29. package/__tests__/protocol-v2.test.ts +1506 -1097
  30. package/__tests__/protocolV2FileWrite.test.ts +152 -0
  31. package/__tests__/protocolV2UiInteraction.test.ts +63 -0
  32. package/__tests__/public-pro2-api-boundary.test.ts +1 -0
  33. package/__tests__/search-devices.test.ts +12 -3
  34. package/__tests__/tron-sign-message-init.test.ts +2 -2
  35. package/__tests__/uiProgressThrottle.test.ts +74 -0
  36. package/__tests__/uiPromiseRegistry.test.ts +115 -0
  37. package/dist/api/BaseMethod.d.ts +3 -1
  38. package/dist/api/BaseMethod.d.ts.map +1 -1
  39. package/dist/api/CheckAllFirmwareRelease.d.ts +2 -1
  40. package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
  41. package/dist/api/DetectDeviceConnectProtocol.d.ts +7 -0
  42. package/dist/api/DetectDeviceConnectProtocol.d.ts.map +1 -0
  43. package/dist/api/FirmwareUpdate.d.ts.map +1 -1
  44. package/dist/api/FirmwareUpdateV2.d.ts +0 -11
  45. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  46. package/dist/api/FirmwareUpdateV3.d.ts +3 -7
  47. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  48. package/dist/api/FirmwareUpdateV4.d.ts +13 -27
  49. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  50. package/dist/api/GetPassphraseState.d.ts.map +1 -1
  51. package/dist/api/OpenWalletSession.d.ts.map +1 -1
  52. package/dist/api/SearchDevices.d.ts.map +1 -1
  53. package/dist/api/allnetwork/AllNetworkGetAddressBase.d.ts.map +1 -1
  54. package/dist/api/device/DeviceSettings.d.ts.map +1 -1
  55. package/dist/api/device/DeviceUnlock.d.ts +2 -2
  56. package/dist/api/device/DeviceUnlock.d.ts.map +1 -1
  57. package/dist/api/device/DeviceUpdateBootloader.d.ts +1 -5
  58. package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
  59. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +4 -11
  60. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  61. package/dist/api/firmware/getBinary.d.ts +0 -7
  62. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  63. package/dist/api/firmware/updateBootloader.d.ts +0 -2
  64. package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
  65. package/dist/api/firmware/uploadFirmware.d.ts +1 -9
  66. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  67. package/dist/api/helpers/protocolV2FileWrite.d.ts +9 -0
  68. package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -1
  69. package/dist/api/index.d.ts +2 -0
  70. package/dist/api/index.d.ts.map +1 -1
  71. package/dist/api/kaspa/KaspaGetAddress.d.ts +1 -1
  72. package/dist/api/kaspa/KaspaSignTransaction.d.ts +1 -1
  73. package/dist/api/kaspa/KaspaSignTransaction.d.ts.map +1 -1
  74. package/dist/api/polkadot/PolkadotGetAddress.d.ts +1 -1
  75. package/dist/api/polkadot/networks.d.ts +1 -1
  76. package/dist/api/protocol-v2/DeviceUploadNft.d.ts +31 -0
  77. package/dist/api/protocol-v2/DeviceUploadNft.d.ts.map +1 -0
  78. package/dist/api/protocol-v2/ProtocolInfoRequest.d.ts.map +1 -1
  79. package/dist/api/protocol-v2/helpers.d.ts.map +1 -1
  80. package/dist/api/solana/SolSignMessage.d.ts +1 -1
  81. package/dist/api/solana/SolSignOffchainMessage.d.ts +1 -1
  82. package/dist/api/solana/SolSignTransaction.d.ts +2 -2
  83. package/dist/api/stellar/StellarSignTransaction.d.ts +1 -1
  84. package/dist/api/sui/SuiGetAddress.d.ts +1 -1
  85. package/dist/api/sui/SuiGetPublicKey.d.ts +1 -1
  86. package/dist/api/sui/SuiSignMessage.d.ts +1 -1
  87. package/dist/api/sui/SuiSignTransaction.d.ts +1 -1
  88. package/dist/api/ton/TonGetAddress.d.ts +1 -1
  89. package/dist/api/ton/TonSignMessage.d.ts +1 -1
  90. package/dist/api/ton/TonSignProof.d.ts +1 -1
  91. package/dist/api/tron/TronSignMessage.d.ts +2 -2
  92. package/dist/api/tron/TronSignTransaction.d.ts +1 -1
  93. package/dist/core/deviceEventRegistration.d.ts +2 -0
  94. package/dist/core/deviceEventRegistration.d.ts.map +1 -1
  95. package/dist/core/index.d.ts.map +1 -1
  96. package/dist/core/uiPromiseRegistry.d.ts +6 -0
  97. package/dist/core/uiPromiseRegistry.d.ts.map +1 -0
  98. package/dist/data-manager/DataManager.d.ts +9 -4
  99. package/dist/data-manager/DataManager.d.ts.map +1 -1
  100. package/dist/data-manager/TransportManager.d.ts.map +1 -1
  101. package/dist/data-manager/connectSettings.d.ts.map +1 -1
  102. package/dist/device/Device.d.ts +29 -6
  103. package/dist/device/Device.d.ts.map +1 -1
  104. package/dist/device/DeviceCommands.d.ts +4 -4
  105. package/dist/device/DeviceCommands.d.ts.map +1 -1
  106. package/dist/device/DeviceConnector.d.ts +2 -1
  107. package/dist/device/DeviceConnector.d.ts.map +1 -1
  108. package/dist/device/DevicePool.d.ts.map +1 -1
  109. package/dist/deviceProfile/protocolV2DeviceIdentity.d.ts.map +1 -1
  110. package/dist/events/device.d.ts +4 -0
  111. package/dist/events/device.d.ts.map +1 -1
  112. package/dist/events/logBlockEvent.d.ts.map +1 -1
  113. package/dist/events/ui-promise.d.ts +4 -2
  114. package/dist/events/ui-promise.d.ts.map +1 -1
  115. package/dist/events/ui-request.d.ts +31 -2
  116. package/dist/events/ui-request.d.ts.map +1 -1
  117. package/dist/events/ui-response.d.ts +10 -2
  118. package/dist/events/ui-response.d.ts.map +1 -1
  119. package/dist/index.d.ts +243 -248
  120. package/dist/index.d.ts.map +1 -1
  121. package/dist/index.js +3051 -3774
  122. package/dist/inject.d.ts +6 -1
  123. package/dist/inject.d.ts.map +1 -1
  124. package/dist/lowLevelInject.d.ts.map +1 -1
  125. package/dist/protocols/protocol-v2/features.d.ts +0 -4
  126. package/dist/protocols/protocol-v2/features.d.ts.map +1 -1
  127. package/dist/protocols/protocol-v2/firmware.d.ts.map +1 -1
  128. package/dist/protocols/protocol-v2/resources.d.ts +30 -0
  129. package/dist/protocols/protocol-v2/resources.d.ts.map +1 -0
  130. package/dist/protocols/protocol-v2/uiInteraction.d.ts +7 -4
  131. package/dist/protocols/protocol-v2/uiInteraction.d.ts.map +1 -1
  132. package/dist/protocols/protocol-v2/unlockPolicy.d.ts +1 -3
  133. package/dist/protocols/protocol-v2/unlockPolicy.d.ts.map +1 -1
  134. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts +22 -0
  135. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts.map +1 -0
  136. package/dist/protocols/protocol-v2/walletSession.d.ts +6 -1
  137. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  138. package/dist/topLevelInject.d.ts.map +1 -1
  139. package/dist/types/api/checkAllFirmwareRelease.d.ts +3 -5
  140. package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
  141. package/dist/types/api/detectDeviceConnectProtocol.d.ts +4 -0
  142. package/dist/types/api/detectDeviceConnectProtocol.d.ts.map +1 -0
  143. package/dist/types/api/deviceUnlock.d.ts +5 -1
  144. package/dist/types/api/deviceUnlock.d.ts.map +1 -1
  145. package/dist/types/api/deviceUpdateBootloader.d.ts +0 -6
  146. package/dist/types/api/deviceUpdateBootloader.d.ts.map +1 -1
  147. package/dist/types/api/export.d.ts +2 -4
  148. package/dist/types/api/export.d.ts.map +1 -1
  149. package/dist/types/api/firmwareUpdate.d.ts +4 -71
  150. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  151. package/dist/types/api/index.d.ts +7 -10
  152. package/dist/types/api/index.d.ts.map +1 -1
  153. package/dist/types/api/protocolV2.d.ts +3 -0
  154. package/dist/types/api/protocolV2.d.ts.map +1 -1
  155. package/dist/types/device.d.ts +2 -2
  156. package/dist/types/device.d.ts.map +1 -1
  157. package/dist/types/params.d.ts +1 -0
  158. package/dist/types/params.d.ts.map +1 -1
  159. package/dist/types/settings.d.ts +38 -21
  160. package/dist/types/settings.d.ts.map +1 -1
  161. package/dist/utils/deviceFeaturesCompat.d.ts +0 -1
  162. package/dist/utils/deviceFeaturesCompat.d.ts.map +1 -1
  163. package/dist/utils/deviceFeaturesUtils.d.ts +4 -0
  164. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  165. package/dist/utils/deviceInfoUtils.d.ts.map +1 -1
  166. package/dist/utils/deviceSettings.d.ts.map +1 -1
  167. package/dist/utils/homescreen.d.ts +4 -0
  168. package/dist/utils/homescreen.d.ts.map +1 -1
  169. package/dist/utils/index.d.ts +1 -1
  170. package/dist/utils/index.d.ts.map +1 -1
  171. package/dist/utils/patch.d.ts +1 -1
  172. package/dist/utils/patch.d.ts.map +1 -1
  173. package/dist/utils/pro2Nft.d.ts +31 -0
  174. package/dist/utils/pro2Nft.d.ts.map +1 -0
  175. package/dist/utils/pro2Wallpaper.d.ts +10 -0
  176. package/dist/utils/pro2Wallpaper.d.ts.map +1 -1
  177. package/dist/utils/uiProgressThrottle.d.ts +3 -0
  178. package/dist/utils/uiProgressThrottle.d.ts.map +1 -0
  179. package/package.json +4 -4
  180. package/src/api/BaseMethod.ts +8 -10
  181. package/src/api/CheckAllFirmwareRelease.ts +59 -60
  182. package/src/api/DetectDeviceConnectProtocol.ts +18 -0
  183. package/src/api/FirmwareUpdate.ts +7 -15
  184. package/src/api/FirmwareUpdateV2.ts +126 -316
  185. package/src/api/FirmwareUpdateV3.ts +63 -265
  186. package/src/api/FirmwareUpdateV4.ts +597 -1005
  187. package/src/api/GetPassphraseState.ts +7 -1
  188. package/src/api/OpenWalletSession.ts +71 -17
  189. package/src/api/SearchDevices.ts +3 -1
  190. package/src/api/allnetwork/AllNetworkGetAddress.ts +21 -21
  191. package/src/api/allnetwork/AllNetworkGetAddressBase.ts +36 -11
  192. package/src/api/device/DeviceSettings.ts +5 -4
  193. package/src/api/device/DeviceUnlock.ts +8 -3
  194. package/src/api/device/DeviceUpdateBootloader.ts +6 -125
  195. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +27 -83
  196. package/src/api/firmware/updateBootloader.ts +4 -19
  197. package/src/api/firmware/uploadFirmware.ts +39 -144
  198. package/src/api/helpers/protocolV2FileWrite.ts +202 -63
  199. package/src/api/index.ts +2 -0
  200. package/src/api/kaspa/KaspaGetAddress.ts +1 -1
  201. package/src/api/kaspa/KaspaSignTransaction.ts +2 -13
  202. package/src/api/polkadot/networks.ts +3 -3
  203. package/src/api/protocol-v2/DeviceUploadNft.ts +197 -0
  204. package/src/api/protocol-v2/ProtocolInfoRequest.ts +3 -1
  205. package/src/api/protocol-v2/helpers.ts +1 -1
  206. package/src/api/solana/SolSignMessage.ts +1 -1
  207. package/src/api/solana/SolSignOffchainMessage.ts +1 -1
  208. package/src/api/solana/SolSignTransaction.ts +2 -2
  209. package/src/api/sui/SuiGetAddress.ts +1 -1
  210. package/src/api/sui/SuiGetPublicKey.ts +1 -1
  211. package/src/api/sui/SuiSignMessage.ts +1 -1
  212. package/src/api/sui/SuiSignTransaction.ts +1 -1
  213. package/src/api/ton/TonGetAddress.ts +1 -1
  214. package/src/api/ton/TonSignMessage.ts +1 -1
  215. package/src/api/ton/TonSignProof.ts +1 -1
  216. package/src/api/tron/TronSignMessage.ts +2 -2
  217. package/src/api/tron/TronSignTransaction.ts +1 -1
  218. package/src/core/deviceEventRegistration.ts +4 -0
  219. package/src/core/index.ts +213 -111
  220. package/src/core/uiPromiseRegistry.ts +63 -0
  221. package/src/data/messages/messages-protocol-v2.json +25 -0
  222. package/src/data-manager/DataManager.ts +113 -41
  223. package/src/data-manager/TransportManager.ts +6 -0
  224. package/src/data-manager/connectSettings.ts +0 -11
  225. package/src/device/Device.ts +274 -49
  226. package/src/device/DeviceCommands.ts +31 -4
  227. package/src/device/DeviceConnector.ts +33 -13
  228. package/src/device/DevicePool.ts +21 -7
  229. package/src/deviceProfile/buildDeviceFeatures.ts +2 -2
  230. package/src/deviceProfile/protocolV2DeviceIdentity.ts +3 -0
  231. package/src/events/device.ts +4 -0
  232. package/src/events/logBlockEvent.ts +1 -0
  233. package/src/events/ui-promise.ts +4 -2
  234. package/src/events/ui-request.ts +36 -2
  235. package/src/events/ui-response.ts +12 -2
  236. package/src/index.ts +0 -5
  237. package/src/inject.ts +60 -24
  238. package/src/lowLevelInject.ts +7 -8
  239. package/src/protocols/protocol-v2/features.ts +10 -7
  240. package/src/protocols/protocol-v2/firmware.ts +2 -0
  241. package/src/protocols/protocol-v2/resources.ts +390 -0
  242. package/src/protocols/protocol-v2/uiInteraction.ts +46 -6
  243. package/src/protocols/protocol-v2/unlockPolicy.ts +1 -105
  244. package/src/protocols/protocol-v2/unlockPolicyRunner.ts +131 -0
  245. package/src/protocols/protocol-v2/walletSession.ts +214 -36
  246. package/src/topLevelInject.ts +7 -8
  247. package/src/types/api/checkAllFirmwareRelease.ts +3 -5
  248. package/src/types/api/detectDeviceConnectProtocol.ts +6 -0
  249. package/src/types/api/deviceUnlock.ts +12 -1
  250. package/src/types/api/deviceUpdateBootloader.ts +0 -6
  251. package/src/types/api/export.ts +1 -18
  252. package/src/types/api/firmwareUpdate.ts +6 -79
  253. package/src/types/api/index.ts +13 -14
  254. package/src/types/api/protocolV2.ts +13 -0
  255. package/src/types/device.ts +15 -3
  256. package/src/types/params.ts +7 -1
  257. package/src/types/settings.ts +60 -22
  258. package/src/utils/deviceFeaturesCompat.ts +5 -6
  259. package/src/utils/deviceFeaturesUtils.ts +14 -3
  260. package/src/utils/deviceInfoUtils.ts +2 -0
  261. package/src/utils/deviceSettings.ts +3 -0
  262. package/src/utils/homescreen.ts +32 -6
  263. package/src/utils/index.ts +6 -1
  264. package/src/utils/pro2Nft.ts +142 -0
  265. package/src/utils/pro2Wallpaper.ts +35 -8
  266. package/src/utils/uiProgressThrottle.ts +63 -0
  267. package/__tests__/device-initialize-timeout.test.ts +0 -56
  268. package/__tests__/firmware-update/check-all-firmware-release.test.ts +0 -175
  269. package/__tests__/firmware-update/firmware-artifact-source.test.ts +0 -119
  270. package/__tests__/firmware-update/firmware-host-binding.test.ts +0 -70
  271. package/__tests__/firmware-update/firmware-preparation-error.test.ts +0 -27
  272. package/__tests__/firmware-update/firmware-update-bootloader-poll.test.ts +0 -200
  273. package/__tests__/firmware-update/firmware-update-capabilities.test.ts +0 -13
  274. package/__tests__/firmware-update/firmware-update-plan-bootloader-identity.test.ts +0 -394
  275. package/__tests__/firmware-update/firmware-update-plan.test.ts +0 -593
  276. package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +0 -213
  277. package/__tests__/firmware-update/firmware-update-v3-reconnect.test.ts +0 -58
  278. package/__tests__/firmware-update/firmware-upload-source.test.ts +0 -70
  279. package/dist/api/firmware/FirmwareArtifactSource.d.ts +0 -25
  280. package/dist/api/firmware/FirmwareArtifactSource.d.ts.map +0 -1
  281. package/dist/api/firmware/FirmwareHostBinding.d.ts +0 -6
  282. package/dist/api/firmware/FirmwareHostBinding.d.ts.map +0 -1
  283. package/dist/api/firmware/FirmwarePreparationError.d.ts +0 -3
  284. package/dist/api/firmware/FirmwarePreparationError.d.ts.map +0 -1
  285. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts +0 -3
  286. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts.map +0 -1
  287. package/dist/api/firmware/FirmwareUpdatePlan.d.ts +0 -25
  288. package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +0 -1
  289. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts +0 -32
  290. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +0 -1
  291. package/dist/api/firmware/progressThrottle.d.ts +0 -3
  292. package/dist/api/firmware/progressThrottle.d.ts.map +0 -1
  293. package/dist/protocols/protocol-v2/lockedError.d.ts +0 -2
  294. package/dist/protocols/protocol-v2/lockedError.d.ts.map +0 -1
  295. package/dist/protocols/protocol-v2/unlockRetry.d.ts +0 -10
  296. package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +0 -1
  297. package/dist/types/api/firmwareUpdateCapabilities.d.ts +0 -9
  298. package/dist/types/api/firmwareUpdateCapabilities.d.ts.map +0 -1
  299. package/dist/types/api/firmwareUpdatePlan.d.ts +0 -28
  300. package/dist/types/api/firmwareUpdatePlan.d.ts.map +0 -1
  301. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts +0 -42
  302. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts.map +0 -1
  303. package/src/api/firmware/FirmwareArtifactSource.ts +0 -278
  304. package/src/api/firmware/FirmwareHostBinding.ts +0 -100
  305. package/src/api/firmware/FirmwarePreparationError.ts +0 -12
  306. package/src/api/firmware/FirmwareUpdateCapabilities.ts +0 -9
  307. package/src/api/firmware/FirmwareUpdatePlan.ts +0 -772
  308. package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +0 -449
  309. package/src/api/firmware/progressThrottle.ts +0 -44
  310. package/src/protocols/protocol-v2/lockedError.ts +0 -10
  311. package/src/protocols/protocol-v2/unlockRetry.ts +0 -109
  312. package/src/types/api/firmwareUpdateCapabilities.ts +0 -9
  313. package/src/types/api/firmwareUpdatePlan.ts +0 -38
  314. package/src/types/api/firmwareUpdatePreparedPlan.ts +0 -53
@@ -14,46 +14,18 @@ import { BaseMethod } from './BaseMethod';
14
14
  import { validateParams } from './helpers/paramsValidator';
15
15
  import { DevicePool } from '../device/DevicePool';
16
16
  import { getBinary, getInfo, getSysResourceBinary } from './firmware/getBinary';
17
- import { normalizeFirmwarePreparationError } from './firmware/FirmwarePreparationError';
18
- import {
19
- updateResources,
20
- updateResourcesFromSources,
21
- uploadFirmwareFromSource,
22
- } from './firmware/uploadFirmware';
23
- import { BOOTLOADER_POLL_INITIALIZE_TIMEOUT_MS } from './firmware/FirmwareUpdateBaseMethod';
24
- import { LoggerNames, getDeviceType, getDeviceUUID, getLogger, wait } from '../utils';
25
- import { resolveDeviceBootloaderMode } from '../utils/deviceFeaturesCompat';
17
+ import { updateResources, uploadFirmware } from './firmware/uploadFirmware';
18
+ import { LoggerNames, getLogger, wait } from '../utils';
26
19
  import { FirmwareUpdateTipMessage, createUiMessage } from '../events/ui-request';
27
20
  import { DeviceModelToTypes } from '../types';
28
21
  import { DataManager } from '../data-manager';
29
22
  import { DEVICE } from '../events';
30
- import { type FirmwareByteSource, openFirmwareByteSource } from './firmware/FirmwareArtifactSource';
31
- import { resolveFirmwareUpdateHostBinding } from './firmware/FirmwareHostBinding';
32
- import {
33
- assertFirmwareUpdatePreparedPlanBinding,
34
- assertFirmwareUpdatePreparedPlanDeviceIdentity,
35
- getFirmwareUpdateResourceName,
36
- } from './firmware/FirmwareUpdatePreparedPlan';
37
23
 
38
24
  import type { Features, KnownDevice } from '../types';
39
25
  import type { FirmwareBinary } from './firmware/getBinary';
40
- import type {
41
- FirmwareArtifactReader,
42
- FirmwareArtifactReference,
43
- } from '../types/api/firmwareUpdate';
44
- import type { FirmwareUpdatePreparedPlan } from '../types/api/firmwareUpdatePreparedPlan';
45
26
 
46
27
  type Params = {
47
28
  binary?: ArrayBuffer;
48
- artifact?: FirmwareArtifactReference;
49
- resourceArtifact?: FirmwareArtifactReference;
50
- resourceEntries?: Array<{
51
- entryName: string;
52
- artifact: FirmwareArtifactReference;
53
- }>;
54
- artifactReader?: FirmwareArtifactReader;
55
- preparedPlan?: FirmwareUpdatePreparedPlan;
56
- platform?: FirmwareUpdatePreparedPlan['platform'];
57
29
  version?: number[];
58
30
  updateType: 'firmware' | 'ble';
59
31
  forcedUpdateRes?: boolean;
@@ -134,13 +106,6 @@ const normalizeFirmwareBinary = (binary: unknown): FirmwareBinary | undefined =>
134
106
  return normalized.buffer;
135
107
  };
136
108
 
137
- const toArrayBuffer = (binary: FirmwareBinary): ArrayBuffer => {
138
- if (binary instanceof ArrayBuffer) {
139
- return binary;
140
- }
141
- return new Uint8Array(binary.buffer, binary.byteOffset, binary.byteLength).slice().buffer;
142
- };
143
-
144
109
  export default class FirmwareUpdateV2 extends BaseMethod<Params> {
145
110
  checkPromise: Deferred<any> | null = null;
146
111
 
@@ -187,38 +152,6 @@ export default class FirmwareUpdateV2 extends BaseMethod<Params> {
187
152
  binary: payload.binary,
188
153
  };
189
154
  }
190
-
191
- if ('artifact' in payload) {
192
- const hostBinding = resolveFirmwareUpdateHostBinding(payload.hostBindingGeneration);
193
- const target = payload.isUpdateBootloader ? 'bootloader' : payload.updateType;
194
- const resourceBindings = [
195
- ...(payload.resourceArtifact
196
- ? [{ target: 'resource' as const, artifact: payload.resourceArtifact }]
197
- : []),
198
- ...(payload.resourceEntries ?? []).map(
199
- (entry: { entryName: string; artifact: FirmwareArtifactReference }) => ({
200
- target: 'resource' as const,
201
- entryName: entry.entryName,
202
- artifact: entry.artifact,
203
- })
204
- ),
205
- ];
206
- assertFirmwareUpdatePreparedPlanBinding({
207
- preparedPlan: payload.preparedPlan,
208
- executor: 'v2',
209
- platform: payload.platform,
210
- scopeTargets: [target, ...(target === 'firmware' ? (['resource'] as const) : [])],
211
- bindings: [{ target, artifact: payload.artifact }, ...resourceBindings],
212
- });
213
- this.params = {
214
- ...this.params,
215
- preparedPlan: payload.preparedPlan,
216
- artifact: payload.artifact,
217
- resourceArtifact: payload.resourceArtifact,
218
- resourceEntries: payload.resourceEntries,
219
- artifactReader: hostBinding.artifactReader,
220
- };
221
- }
222
155
  }
223
156
 
224
157
  postTipMessage = (message: string) => {
@@ -254,10 +187,6 @@ export default class FirmwareUpdateV2 extends BaseMethod<Params> {
254
187
  this.checkPromise = createDeferred();
255
188
  const env = DataManager.getSettings('env');
256
189
  const isBleReconnect = connectId && DataManager.isBleConnect(env);
257
- // Tracks the in-flight BLE probe so interval ticks never overlap: a superseded
258
- // Initialize left pending on the V1 transport is exactly what later times out
259
- // and used to tear down the connection mid-firmware-upload.
260
- let bleProbeInFlight = false;
261
190
 
262
191
  Log.log('FirmwareUpdateV2 [checkDeviceToBootloader] isBleReconnect: ', isBleReconnect);
263
192
 
@@ -305,18 +234,14 @@ export default class FirmwareUpdateV2 extends BaseMethod<Params> {
305
234
  }
306
235
 
307
236
  if (isBleReconnect) {
308
- if (bleProbeInFlight) return;
309
- bleProbeInFlight = true;
310
237
  try {
311
238
  await this.device.deviceConnector?.acquire(
312
239
  this.device.originalDescriptor.id,
313
240
  null,
314
- true
241
+ true,
242
+ this.payload.connectProtocol ?? this.device.originalDescriptor.protocolType
315
243
  );
316
- // Bound each probe so a request the rebooting device never received
317
- // frees the slot for the next tick instead of hanging into the
318
- // 30s reboot budget.
319
- await this.device.initialize({ timeoutMs: BOOTLOADER_POLL_INITIALIZE_TIMEOUT_MS });
244
+ await this.device.initialize();
320
245
  if (this.device.isBootloader()) {
321
246
  clearInterval(intervalTimer);
322
247
  this.checkPromise?.resolve(true);
@@ -324,8 +249,6 @@ export default class FirmwareUpdateV2 extends BaseMethod<Params> {
324
249
  } catch (e) {
325
250
  // ignore error because of device is not connected
326
251
  Log.log('catch Bluetooth error when device is restarting: ', e);
327
- } finally {
328
- bleProbeInFlight = false;
329
252
  }
330
253
  } else {
331
254
  await this._checkDeviceInBootloaderMode(connectId, intervalTimer, timeoutTimer);
@@ -350,7 +273,9 @@ export default class FirmwareUpdateV2 extends BaseMethod<Params> {
350
273
  ) {
351
274
  const deviceDiff = await this.device.deviceConnector?.enumerate();
352
275
  const devicesDescriptor = deviceDiff?.descriptors ?? [];
353
- const { deviceList } = await DevicePool.getDevices(devicesDescriptor, connectId);
276
+ const { deviceList } = await DevicePool.getDevices(devicesDescriptor, connectId, {
277
+ connectProtocol: this.payload.connectProtocol ?? this.device.originalDescriptor.protocolType,
278
+ });
354
279
 
355
280
  if (deviceList.length === 1 && deviceList[0]?.isBootloader()) {
356
281
  // should update current device from cache
@@ -419,266 +344,151 @@ export default class FirmwareUpdateV2 extends BaseMethod<Params> {
419
344
  const { features, commands } = device;
420
345
  const deviceType = device.getCurrentDeviceType();
421
346
 
422
- // Protocol V2 (Pro2) uses DeviceFirmwareUpdate and must not enter this legacy flow.
423
- if (device.isProtocolV2()) {
424
- throw ERRORS.TypedError(
425
- HardwareErrorCode.RuntimeError,
426
- 'Protocol V2 firmware update must use firmwareUpdateV4'
427
- );
428
- }
429
- if (params.preparedPlan) {
430
- assertFirmwareUpdatePreparedPlanDeviceIdentity({
431
- preparedPlan: params.preparedPlan,
432
- deviceIdentity: getDeviceUUID(features) || undefined,
433
- bootloaderMode: resolveDeviceBootloaderMode(features),
434
- deviceModel: String(getDeviceType(features)),
435
- });
436
- }
437
-
438
347
  const deviceFirmwareType = device.getCurrentFirmwareType();
439
348
  const firmwareType = params.firmwareType ?? deviceFirmwareType;
440
349
 
441
350
  this.checkVersionForCopyTouchResource(features, firmwareType);
442
351
 
443
- let preparedSource: FirmwareByteSource | undefined;
444
- try {
445
- const acquireFirmwareSource = async (): Promise<FirmwareByteSource> => {
446
- try {
447
- if (preparedSource) {
448
- return preparedSource;
449
- }
352
+ let preparedBinary: FirmwareBinary | undefined;
353
+ const acquireFirmwareBinary = async (): Promise<FirmwareBinary> => {
354
+ try {
355
+ if (preparedBinary) {
356
+ return preparedBinary;
357
+ }
450
358
 
451
- if (params.binary !== undefined) {
452
- const binary = normalizeFirmwareBinary(params.binary);
453
- if (!binary) {
454
- throw new Error('firmware binary is empty or invalid');
455
- }
456
- const source = await openFirmwareByteSource({
457
- binary: toArrayBuffer(binary),
458
- });
459
- if (!source) {
460
- throw new Error('firmware binary is empty or invalid');
461
- }
462
- preparedSource = source;
463
- return source;
359
+ if (params.binary !== undefined) {
360
+ preparedBinary = normalizeFirmwareBinary(params.binary);
361
+ if (!preparedBinary) {
362
+ throw new Error('firmware binary is empty or invalid');
464
363
  }
364
+ return preparedBinary;
365
+ }
465
366
 
466
- if (params.artifact) {
467
- const source = await openFirmwareByteSource({
468
- artifact: params.artifact,
469
- reader: params.artifactReader,
470
- });
471
- if (!source) {
472
- throw new Error('firmware artifact is not prepared');
473
- }
474
- preparedSource = source;
475
- return source;
476
- }
367
+ if (!device.features) {
368
+ throw ERRORS.TypedError(
369
+ HardwareErrorCode.RuntimeError,
370
+ 'no features found for this device'
371
+ );
372
+ }
477
373
 
478
- if (!device.features) {
479
- throw ERRORS.TypedError(
480
- HardwareErrorCode.RuntimeError,
481
- 'no features found for this device'
482
- );
483
- }
374
+ this.postTipMessage('DownloadFirmware');
375
+ const firmware = await getBinary({
376
+ features: device.features,
377
+ version: params.version,
378
+ updateType: params.updateType,
379
+ isUpdateBootloader: params.isUpdateBootloader,
380
+ firmwareType,
381
+ requestOptions: FIRMWARE_DOWNLOAD_REQUEST_OPTIONS,
382
+ });
383
+ preparedBinary = normalizeFirmwareBinary(firmware.binary);
384
+ if (!preparedBinary) {
385
+ throw new Error('downloaded firmware binary is empty or invalid');
386
+ }
387
+ this.postTipMessage('DownloadFirmwareSuccess');
388
+ return preparedBinary;
389
+ } catch (err) {
390
+ throw ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateDownloadFailed, err.message ?? err);
391
+ }
392
+ };
484
393
 
485
- if (
486
- params.artifactReader ||
487
- DataManager.getSettings('firmwareManifestMode') === 'external-only'
488
- ) {
489
- throw ERRORS.TypedError(
490
- HardwareErrorCode.RuntimeError,
491
- 'Firmware must be prepared by the external firmware host',
492
- {
493
- firmwareUpdateCode: 'FirmwareArtifactsNotPrepared',
494
- artifactName: 'firmware',
495
- }
496
- );
497
- }
394
+ if (!device.isBootloader() && features) {
395
+ const serialNo = device.getCurrentSerialNo();
396
+ // should go to bootloader mode manually
397
+ if (this.isEnteredManuallyBoot()) {
398
+ return Promise.reject(ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateManuallyEnterBoot));
399
+ }
498
400
 
499
- this.postTipMessage('DownloadFirmware');
500
- const firmware = await getBinary({
501
- features: device.features,
502
- version: params.version,
503
- updateType: params.updateType,
504
- isUpdateBootloader: params.isUpdateBootloader,
505
- firmwareType,
506
- requestOptions: FIRMWARE_DOWNLOAD_REQUEST_OPTIONS,
507
- });
508
- const binary = normalizeFirmwareBinary(firmware.binary);
509
- if (!binary) {
510
- throw new Error('downloaded firmware binary is empty or invalid');
511
- }
512
- const source = await openFirmwareByteSource({
513
- binary: toArrayBuffer(binary),
514
- });
515
- if (!source) {
516
- throw new Error('downloaded firmware binary is empty or invalid');
401
+ // check & upgrade firmware resource
402
+ if (this.isSupportResourceUpdate(params.updateType)) {
403
+ this.postTipMessage('CheckLatestUiResource');
404
+ const resourceUrl = DataManager.getSysResourcesLatestRelease({
405
+ features,
406
+ forcedUpdateRes: params.forcedUpdateRes,
407
+ firmwareType,
408
+ });
409
+ if (resourceUrl) {
410
+ this.postTipMessage('DownloadLatestUiResource');
411
+ const resource = await getSysResourceBinary(resourceUrl);
412
+ this.postTipMessage('DownloadLatestUiResourceSuccess');
413
+ if (resource) {
414
+ await updateResources(
415
+ this.device.getCommands().typedCall.bind(this.device.getCommands()),
416
+ this.postMessage,
417
+ device,
418
+ resource.binary
419
+ );
517
420
  }
518
- preparedSource = source;
519
- this.postTipMessage('DownloadFirmwareSuccess');
520
- return source;
521
- } catch (err) {
522
- throw normalizeFirmwarePreparationError(err);
523
421
  }
524
- };
422
+ }
525
423
 
526
- if (!device.isBootloader() && features) {
527
- const serialNo = device.getCurrentSerialNo();
528
- // should go to bootloader mode manually
529
- if (this.isEnteredManuallyBoot()) {
530
- throw ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateManuallyEnterBoot);
424
+ // check if the device commands has been disposed
425
+ this.device?.commands?.checkDisposed();
426
+
427
+ // A failed firmware download must leave the device in normal mode.
428
+ await acquireFirmwareBinary();
429
+
430
+ // The request may outlive the current transport command instance.
431
+ this.device?.commands?.checkDisposed();
432
+
433
+ // auto go to bootloader mode
434
+ try {
435
+ this.postTipMessage('AutoRebootToBootloader');
436
+ const bootRes = await commands.typedCall('DeviceBackToBoot', 'Success');
437
+ // @ts-expect-error
438
+ if (bootRes.type === 'CallMethodError') {
439
+ throw ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateAutoEnterBootFailure);
531
440
  }
441
+ this.postTipMessage('GoToBootloaderSuccess');
442
+ this.checkDeviceToBootloader(this.payload.connectId);
532
443
 
533
- // All network-backed input must be ready before the first device mutation.
534
- await acquireFirmwareSource();
535
-
536
- // check & upgrade firmware resource
537
- if (this.isSupportResourceUpdate(params.updateType)) {
538
- this.postTipMessage('CheckLatestUiResource');
539
- const resourceUrl = DataManager.getSysResourcesLatestRelease({
540
- features,
541
- forcedUpdateRes: params.forcedUpdateRes,
542
- firmwareType,
543
- });
544
- if (resourceUrl) {
545
- this.postTipMessage('DownloadLatestUiResource');
546
- if (params.resourceEntries?.length) {
547
- const sources: Array<{
548
- entryName: string;
549
- source: FirmwareByteSource;
550
- }> = [];
551
- try {
552
- for (const entry of params.resourceEntries) {
553
- const resourceName = getFirmwareUpdateResourceName(entry.entryName);
554
- const source = await openFirmwareByteSource({
555
- artifact: entry.artifact,
556
- reader: params.artifactReader,
557
- });
558
- if (!source) {
559
- throw new Error('Firmware resource entry is not prepared');
560
- }
561
- sources.push({ entryName: resourceName, source });
562
- }
563
- await updateResourcesFromSources(
564
- this.device.getCommands().typedCall.bind(this.device.getCommands()),
565
- this.postMessage,
566
- device,
567
- sources.map(entry => ({
568
- entryName: entry.entryName,
569
- source: entry.source,
570
- }))
571
- );
572
- } finally {
573
- await Promise.all(
574
- sources.map(entry => entry.source.close().catch(() => undefined))
575
- );
576
- }
577
- this.postTipMessage('DownloadLatestUiResourceSuccess');
578
- } else {
579
- let resourceBinary: ArrayBuffer | Buffer;
580
- if (params.resourceArtifact) {
581
- throw ERRORS.TypedError(
582
- HardwareErrorCode.RuntimeError,
583
- 'Firmware resource archive must be materialized into prepared entries',
584
- {
585
- firmwareUpdateCode: 'FirmwareArtifactsNotPrepared',
586
- artifactName: 'resource',
587
- }
588
- );
589
- } else {
590
- if (
591
- params.artifactReader ||
592
- DataManager.getSettings('firmwareManifestMode') === 'external-only'
593
- ) {
594
- throw ERRORS.TypedError(
595
- HardwareErrorCode.RuntimeError,
596
- 'Firmware resource must be prepared by the external firmware host',
597
- {
598
- firmwareUpdateCode: 'FirmwareArtifactsNotPrepared',
599
- artifactName: 'resource',
600
- }
601
- );
602
- }
603
- resourceBinary = (await getSysResourceBinary(resourceUrl)).binary;
604
- }
605
- this.postTipMessage('DownloadLatestUiResourceSuccess');
606
- await updateResources(
607
- this.device.getCommands().typedCall.bind(this.device.getCommands()),
608
- this.postMessage,
609
- device,
610
- resourceBinary
611
- );
612
- }
613
- }
444
+ // force clean classic device cache so that the device can initialize again
445
+ if (DeviceModelToTypes.model_classic.includes(deviceType)) {
446
+ DevicePool.clearDeviceCache(serialNo);
614
447
  }
448
+ delete DevicePool.devicesCache[''];
449
+ await this.checkPromise?.promise;
450
+ this.checkPromise = null;
615
451
 
616
- // The request may outlive the current transport command instance.
452
+ // check if the device commands has been disposed
617
453
  this.device?.commands?.checkDisposed();
618
454
 
619
- // auto go to bootloader mode
620
- try {
621
- this.postTipMessage('AutoRebootToBootloader');
622
- const bootRes = await commands.typedCall('DeviceBackToBoot', 'Success');
623
- // @ts-expect-error
624
- if (bootRes.type === 'CallMethodError') {
625
- throw ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateAutoEnterBootFailure);
626
- }
627
- this.postTipMessage('GoToBootloaderSuccess');
628
- this.checkDeviceToBootloader(this.payload.connectId);
629
-
630
- // force clean classic device cache so that the device can initialize again
631
- if (DeviceModelToTypes.model_classic.includes(deviceType)) {
632
- DevicePool.clearDeviceCache(serialNo);
633
- }
634
- delete DevicePool.devicesCache[''];
635
- await this.checkPromise?.promise;
636
- this.checkPromise = null;
637
-
638
- // check if the device commands has been disposed
639
- this.device?.commands?.checkDisposed();
640
-
641
- /**
642
- * Touch 1 with bootloader v2.5.0 issue: BLE chip need more time for looking up name, here change the delay time to 3000ms after rebooting.
643
- */
644
- const isTouch = DeviceModelToTypes.model_touch.includes(deviceType);
645
- await wait(isTouch ? 3000 : 1500);
646
- } catch (e) {
647
- if (e instanceof HardwareError) {
648
- return Promise.reject(e);
649
- }
650
- console.log('auto go to bootloader mode failed: ', e);
651
- return Promise.reject(
652
- ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateAutoEnterBootFailure)
653
- );
455
+ /**
456
+ * Touch 1 with bootloader v2.5.0 issue: BLE chip need more time for looking up name, here change the delay time to 3000ms after rebooting.
457
+ */
458
+ const isTouch = DeviceModelToTypes.model_touch.includes(deviceType);
459
+ await wait(isTouch ? 3000 : 1500);
460
+ } catch (e) {
461
+ if (e instanceof HardwareError) {
462
+ return Promise.reject(e);
654
463
  }
464
+ console.log('auto go to bootloader mode failed: ', e);
465
+ return Promise.reject(
466
+ ERRORS.TypedError(HardwareErrorCode.FirmwareUpdateAutoEnterBootFailure)
467
+ );
655
468
  }
469
+ }
656
470
 
657
- // Devices already in bootloader mode still acquire through the same helper.
658
- const source = await acquireFirmwareSource();
659
-
660
- // check if the device commands has been disposed
661
- this.device?.commands?.checkDisposed();
471
+ // Devices already in bootloader mode still acquire through the same helper.
472
+ const binary = await acquireFirmwareBinary();
662
473
 
663
- await this.device.acquire();
474
+ // check if the device commands has been disposed
475
+ this.device?.commands?.checkDisposed();
664
476
 
665
- const response = await uploadFirmwareFromSource(
666
- params.updateType,
667
- this.device.getCommands().typedCall.bind(this.device.getCommands()),
668
- this.postMessage,
669
- device,
670
- source,
671
- true,
672
- params.isUpdateBootloader
673
- );
477
+ await this.device.acquire();
674
478
 
675
- if (this.connectId) {
676
- DevicePool.clearDeviceCache(this.connectId);
677
- }
479
+ const response = await uploadFirmware(
480
+ params.updateType,
481
+ this.device.getCommands().typedCall.bind(this.device.getCommands()),
482
+ this.postMessage,
483
+ device,
484
+ { payload: binary, rebootOnSuccess: true },
485
+ params.isUpdateBootloader
486
+ );
678
487
 
679
- return response;
680
- } finally {
681
- await preparedSource?.close().catch(() => undefined);
488
+ if (this.connectId) {
489
+ DevicePool.clearDeviceCache(this.connectId);
682
490
  }
491
+
492
+ return response;
683
493
  }
684
494
  }