@onekeyfe/hd-core 1.1.34-alpha.2 → 1.1.34-alpha.3

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 (613) hide show
  1. package/__tests__/AllNetworkGetAddressBase.tracing.test.ts +75 -0
  2. package/__tests__/DeviceCommands.test.ts +217 -0
  3. package/__tests__/RequestQueue.test.ts +140 -0
  4. package/__tests__/UploadPortfolio.test.ts +46 -0
  5. package/__tests__/connectSettings.test.ts +61 -0
  6. package/__tests__/device-lifecycle-events.test.ts +117 -0
  7. package/__tests__/device-pool-state.test.ts +132 -0
  8. package/__tests__/device-settings.test.ts +286 -0
  9. package/__tests__/device-state-mapper.test.ts +229 -0
  10. package/__tests__/device-utils.test.ts +106 -0
  11. package/__tests__/device-wallet-session-store.test.ts +191 -0
  12. package/__tests__/evmLedgerLegacySafety.test.ts +15 -4
  13. package/__tests__/evmSignTransaction.test.ts +1 -1
  14. package/__tests__/evmSignTypedData.test.ts +1 -1
  15. package/__tests__/filesystemValidation.test.ts +53 -0
  16. package/__tests__/firmware-update/firmware-artifact-source.test.ts +119 -0
  17. package/__tests__/firmware-update/firmware-checkpoint.test.ts +105 -0
  18. package/__tests__/firmware-update/firmware-host-binding.test.ts +73 -0
  19. package/__tests__/firmware-update/firmware-update-capabilities.test.ts +17 -0
  20. package/__tests__/firmware-update/firmware-update-plan.test.ts +358 -0
  21. package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +200 -0
  22. package/__tests__/firmware-update/firmware-update-recovery.test.ts +81 -0
  23. package/__tests__/firmware-update/firmware-update-v2-download-before-boot.test.ts +48 -31
  24. package/__tests__/firmware-update/firmware-upload-source.test.ts +70 -0
  25. package/__tests__/get-device-state.test.ts +403 -0
  26. package/__tests__/logBlockEvent.test.ts +61 -0
  27. package/__tests__/logger.test.ts +34 -0
  28. package/__tests__/open-wallet-session.test.ts +565 -0
  29. package/__tests__/preInitialize.test.ts +33 -0
  30. package/__tests__/pro2Wallpaper.test.ts +39 -0
  31. package/__tests__/protocol-v2-bootloader-mode.test.ts +37 -0
  32. package/__tests__/protocol-v2-firmware-targets.test.ts +19 -0
  33. package/__tests__/protocol-v2.test.ts +6008 -0
  34. package/__tests__/protocolV2FileWrite.test.ts +67 -0
  35. package/__tests__/public-pro2-api-boundary.test.ts +66 -0
  36. package/__tests__/search-devices.test.ts +136 -0
  37. package/dist/api/BaseMethod.d.ts +12 -0
  38. package/dist/api/BaseMethod.d.ts.map +1 -1
  39. package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
  40. package/dist/api/CheckBootloaderRelease.d.ts.map +1 -1
  41. package/dist/api/CheckFirmwareRelease.d.ts.map +1 -1
  42. package/dist/api/ClearSessionCache.d.ts +9 -0
  43. package/dist/api/ClearSessionCache.d.ts.map +1 -0
  44. package/dist/api/DirList.d.ts +10 -0
  45. package/dist/api/DirList.d.ts.map +1 -0
  46. package/dist/api/DirMake.d.ts +9 -0
  47. package/dist/api/DirMake.d.ts.map +1 -0
  48. package/dist/api/DirRemove.d.ts +9 -0
  49. package/dist/api/DirRemove.d.ts.map +1 -0
  50. package/dist/api/FileDelete.d.ts +9 -0
  51. package/dist/api/FileDelete.d.ts.map +1 -0
  52. package/dist/api/FileRead.d.ts +19 -0
  53. package/dist/api/FileRead.d.ts.map +1 -0
  54. package/dist/api/FileWrite.d.ts +25 -0
  55. package/dist/api/FileWrite.d.ts.map +1 -0
  56. package/dist/api/FirmwareUpdate.d.ts.map +1 -1
  57. package/dist/api/FirmwareUpdateV2.d.ts +14 -3
  58. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  59. package/dist/api/FirmwareUpdateV3.d.ts +10 -3
  60. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  61. package/dist/api/FirmwareUpdateV4.d.ts +75 -0
  62. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -0
  63. package/dist/api/GetDeviceState.d.ts +7 -0
  64. package/dist/api/GetDeviceState.d.ts.map +1 -0
  65. package/dist/api/GetFeatures.d.ts +1 -1
  66. package/dist/api/GetFeatures.d.ts.map +1 -1
  67. package/dist/api/GetOnekeyFeatures.d.ts.map +1 -1
  68. package/dist/api/GetPassphraseState.d.ts.map +1 -1
  69. package/dist/api/OpenWalletSession.d.ts +14 -0
  70. package/dist/api/OpenWalletSession.d.ts.map +1 -0
  71. package/dist/api/PathInfo.d.ts +10 -0
  72. package/dist/api/PathInfo.d.ts.map +1 -0
  73. package/dist/api/PromptWebDeviceAccess.d.ts.map +1 -1
  74. package/dist/api/SearchDevices.d.ts +5 -1
  75. package/dist/api/SearchDevices.d.ts.map +1 -1
  76. package/dist/api/UploadPortfolio.d.ts +18 -0
  77. package/dist/api/UploadPortfolio.d.ts.map +1 -0
  78. package/dist/api/alephium/AlephiumGetAddress.d.ts +2 -6
  79. package/dist/api/alephium/AlephiumGetAddress.d.ts.map +1 -1
  80. package/dist/api/alephium/AlephiumSignMessage.d.ts +2 -5
  81. package/dist/api/alephium/AlephiumSignMessage.d.ts.map +1 -1
  82. package/dist/api/alephium/AlephiumSignTransaction.d.ts +5 -8
  83. package/dist/api/alephium/AlephiumSignTransaction.d.ts.map +1 -1
  84. package/dist/api/allnetwork/AllNetworkGetAddressBase.d.ts.map +1 -1
  85. package/dist/api/aptos/AptosGetAddress.d.ts.map +1 -1
  86. package/dist/api/benfen/BenfenGetAddress.d.ts +2 -9
  87. package/dist/api/benfen/BenfenGetAddress.d.ts.map +1 -1
  88. package/dist/api/benfen/BenfenGetPublicKey.d.ts +2 -9
  89. package/dist/api/benfen/BenfenGetPublicKey.d.ts.map +1 -1
  90. package/dist/api/benfen/BenfenSignMessage.d.ts +2 -8
  91. package/dist/api/benfen/BenfenSignMessage.d.ts.map +1 -1
  92. package/dist/api/benfen/BenfenSignTransaction.d.ts +2 -8
  93. package/dist/api/benfen/BenfenSignTransaction.d.ts.map +1 -1
  94. package/dist/api/btc/BTCGetAddress.d.ts +1 -11
  95. package/dist/api/btc/BTCGetAddress.d.ts.map +1 -1
  96. package/dist/api/btc/BTCGetPublicKey.d.ts +1 -11
  97. package/dist/api/btc/BTCGetPublicKey.d.ts.map +1 -1
  98. package/dist/api/btc/BTCSignMessage.d.ts +1 -15
  99. package/dist/api/btc/BTCSignMessage.d.ts.map +1 -1
  100. package/dist/api/btc/BTCSignPsbt.d.ts.map +1 -1
  101. package/dist/api/btc/BTCSignTransaction.d.ts +1 -11
  102. package/dist/api/btc/BTCSignTransaction.d.ts.map +1 -1
  103. package/dist/api/btc/BTCVerifyMessage.d.ts +1 -11
  104. package/dist/api/btc/BTCVerifyMessage.d.ts.map +1 -1
  105. package/dist/api/btc/helpers/versionLimit.d.ts +2 -11
  106. package/dist/api/btc/helpers/versionLimit.d.ts.map +1 -1
  107. package/dist/api/cardano/CardanoSignTransaction.d.ts.map +1 -1
  108. package/dist/api/conflux/ConfluxSignTransaction.d.ts.map +1 -1
  109. package/dist/api/device/DeviceCancel.d.ts.map +1 -1
  110. package/dist/api/device/DeviceChangePin.d.ts.map +1 -1
  111. package/dist/api/device/DeviceFullyUploadResource.d.ts +1 -2
  112. package/dist/api/device/DeviceFullyUploadResource.d.ts.map +1 -1
  113. package/dist/api/device/DeviceLock.d.ts.map +1 -1
  114. package/dist/api/device/DeviceRebootToBoardloader.d.ts +1 -1
  115. package/dist/api/device/DeviceRebootToBoardloader.d.ts.map +1 -1
  116. package/dist/api/device/DeviceRebootToBootloader.d.ts.map +1 -1
  117. package/dist/api/device/DeviceSettings.d.ts.map +1 -1
  118. package/dist/api/device/DeviceSupportFeatures.d.ts.map +1 -1
  119. package/dist/api/device/DeviceUnlock.d.ts +1 -1
  120. package/dist/api/device/DeviceUnlock.d.ts.map +1 -1
  121. package/dist/api/device/DeviceUpdateBootloader.d.ts +7 -1
  122. package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
  123. package/dist/api/device/DeviceUploadResource.d.ts.map +1 -1
  124. package/dist/api/device/DeviceVerify.d.ts.map +1 -1
  125. package/dist/api/device/DeviceWipe.d.ts.map +1 -1
  126. package/dist/api/dynex/DnxGetAddress.d.ts.map +1 -1
  127. package/dist/api/dynex/DnxSignTransaction.d.ts.map +1 -1
  128. package/dist/api/evm/EVMGetPublicKey.d.ts.map +1 -1
  129. package/dist/api/evm/EVMSignMessageEIP712.d.ts +2 -8
  130. package/dist/api/evm/EVMSignMessageEIP712.d.ts.map +1 -1
  131. package/dist/api/evm/EVMSignTypedData.d.ts.map +1 -1
  132. package/dist/api/evm/latest/signTypedData.d.ts +1 -1
  133. package/dist/api/evm/latest/signTypedData.d.ts.map +1 -1
  134. package/dist/api/evm/latest/signTypedHash.d.ts.map +1 -1
  135. package/dist/api/evm/legacyV1/getAddress.d.ts.map +1 -1
  136. package/dist/api/evm/legacyV1/getPublicKey.d.ts.map +1 -1
  137. package/dist/api/evm/legacyV1/signMessage.d.ts.map +1 -1
  138. package/dist/api/evm/legacyV1/signTypedData.d.ts +1 -1
  139. package/dist/api/evm/legacyV1/signTypedData.d.ts.map +1 -1
  140. package/dist/api/evm/legacyV1/signTypedHash.d.ts.map +1 -1
  141. package/dist/api/evm/legacyV1/verifyMessage.d.ts.map +1 -1
  142. package/dist/api/firmware/FirmwareArtifactSource.d.ts +26 -0
  143. package/dist/api/firmware/FirmwareArtifactSource.d.ts.map +1 -0
  144. package/dist/api/firmware/FirmwareCheckpoint.d.ts +15 -0
  145. package/dist/api/firmware/FirmwareCheckpoint.d.ts.map +1 -0
  146. package/dist/api/firmware/FirmwareHostBinding.d.ts +6 -0
  147. package/dist/api/firmware/FirmwareHostBinding.d.ts.map +1 -0
  148. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +10 -1
  149. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  150. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts +3 -0
  151. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts.map +1 -0
  152. package/dist/api/firmware/FirmwareUpdatePlan.d.ts +29 -0
  153. package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +1 -0
  154. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts +25 -0
  155. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +1 -0
  156. package/dist/api/firmware/bootloaderHelper.d.ts.map +1 -1
  157. package/dist/api/firmware/getBinary.d.ts +11 -0
  158. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  159. package/dist/api/firmware/progressThrottle.d.ts +3 -0
  160. package/dist/api/firmware/progressThrottle.d.ts.map +1 -0
  161. package/dist/api/firmware/updateBootloader.d.ts +2 -0
  162. package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
  163. package/dist/api/firmware/uploadFirmware.d.ts +9 -1
  164. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  165. package/dist/api/helpers/batchGetPublickeys.d.ts +3 -0
  166. package/dist/api/helpers/batchGetPublickeys.d.ts.map +1 -1
  167. package/dist/api/helpers/filesystemValidation.d.ts +10 -0
  168. package/dist/api/helpers/filesystemValidation.d.ts.map +1 -0
  169. package/dist/api/helpers/protocolV2FileWrite.d.ts +33 -0
  170. package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -0
  171. package/dist/api/index.d.ts +29 -0
  172. package/dist/api/index.d.ts.map +1 -1
  173. package/dist/api/kaspa/KaspaSignTransaction.d.ts.map +1 -1
  174. package/dist/api/lightning/LnurlAuth.d.ts +4 -0
  175. package/dist/api/lightning/LnurlAuth.d.ts.map +1 -1
  176. package/dist/api/neo/NeoGetAddress.d.ts +2 -8
  177. package/dist/api/neo/NeoGetAddress.d.ts.map +1 -1
  178. package/dist/api/neo/NeoSignTransaction.d.ts +2 -8
  179. package/dist/api/neo/NeoSignTransaction.d.ts.map +1 -1
  180. package/dist/api/nervos/NervosGetAddress.d.ts +2 -9
  181. package/dist/api/nervos/NervosGetAddress.d.ts.map +1 -1
  182. package/dist/api/nervos/NervosSignTransaction.d.ts +9 -12
  183. package/dist/api/nervos/NervosSignTransaction.d.ts.map +1 -1
  184. package/dist/api/nexa/NexaGetAddress.d.ts +2 -8
  185. package/dist/api/nexa/NexaGetAddress.d.ts.map +1 -1
  186. package/dist/api/nexa/NexaSignTransaction.d.ts +2 -9
  187. package/dist/api/nexa/NexaSignTransaction.d.ts.map +1 -1
  188. package/dist/api/polkadot/PolkadotGetAddress.d.ts +3 -0
  189. package/dist/api/polkadot/PolkadotGetAddress.d.ts.map +1 -1
  190. package/dist/api/polkadot/networks.d.ts +3 -0
  191. package/dist/api/polkadot/networks.d.ts.map +1 -1
  192. package/dist/api/protocol-v2/DeviceFactoryInfoGet.d.ts +6 -0
  193. package/dist/api/protocol-v2/DeviceFactoryInfoGet.d.ts.map +1 -0
  194. package/dist/api/protocol-v2/DeviceFactoryInfoSet.d.ts +7 -0
  195. package/dist/api/protocol-v2/DeviceFactoryInfoSet.d.ts.map +1 -0
  196. package/dist/api/protocol-v2/DeviceFirmwareUpdate.d.ts +7 -0
  197. package/dist/api/protocol-v2/DeviceFirmwareUpdate.d.ts.map +1 -0
  198. package/dist/api/protocol-v2/DeviceGetFirmwareUpdateStatus.d.ts +7 -0
  199. package/dist/api/protocol-v2/DeviceGetFirmwareUpdateStatus.d.ts.map +1 -0
  200. package/dist/api/protocol-v2/DeviceGetOnboardingStatus.d.ts +6 -0
  201. package/dist/api/protocol-v2/DeviceGetOnboardingStatus.d.ts.map +1 -0
  202. package/dist/api/protocol-v2/DeviceInfoGet.d.ts +28 -0
  203. package/dist/api/protocol-v2/DeviceInfoGet.d.ts.map +1 -0
  204. package/dist/api/protocol-v2/DeviceReboot.d.ts +7 -0
  205. package/dist/api/protocol-v2/DeviceReboot.d.ts.map +1 -0
  206. package/dist/api/protocol-v2/DeviceSessionOpen.d.ts +28 -0
  207. package/dist/api/protocol-v2/DeviceSessionOpen.d.ts.map +1 -0
  208. package/dist/api/protocol-v2/DeviceSettingsGet.d.ts +6 -0
  209. package/dist/api/protocol-v2/DeviceSettingsGet.d.ts.map +1 -0
  210. package/dist/api/protocol-v2/DeviceSettingsPageShow.d.ts +16 -0
  211. package/dist/api/protocol-v2/DeviceSettingsPageShow.d.ts.map +1 -0
  212. package/dist/api/protocol-v2/DeviceSettingsSet.d.ts +12 -0
  213. package/dist/api/protocol-v2/DeviceSettingsSet.d.ts.map +1 -0
  214. package/dist/api/protocol-v2/DeviceStatusGet.d.ts +6 -0
  215. package/dist/api/protocol-v2/DeviceStatusGet.d.ts.map +1 -0
  216. package/dist/api/protocol-v2/DeviceUploadWallpaper.d.ts +26 -0
  217. package/dist/api/protocol-v2/DeviceUploadWallpaper.d.ts.map +1 -0
  218. package/dist/api/protocol-v2/FilesystemFormat.d.ts +6 -0
  219. package/dist/api/protocol-v2/FilesystemFormat.d.ts.map +1 -0
  220. package/dist/api/protocol-v2/FilesystemPermissionFix.d.ts +6 -0
  221. package/dist/api/protocol-v2/FilesystemPermissionFix.d.ts.map +1 -0
  222. package/dist/api/protocol-v2/Ping.d.ts +8 -0
  223. package/dist/api/protocol-v2/Ping.d.ts.map +1 -0
  224. package/dist/api/protocol-v2/ProtocolInfoRequest.d.ts +6 -0
  225. package/dist/api/protocol-v2/ProtocolInfoRequest.d.ts.map +1 -0
  226. package/dist/api/protocol-v2/helpers.d.ts +42 -0
  227. package/dist/api/protocol-v2/helpers.d.ts.map +1 -0
  228. package/dist/api/scdo/ScdoGetAddress.d.ts +2 -6
  229. package/dist/api/scdo/ScdoGetAddress.d.ts.map +1 -1
  230. package/dist/api/scdo/ScdoSignMessage.d.ts +2 -5
  231. package/dist/api/scdo/ScdoSignMessage.d.ts.map +1 -1
  232. package/dist/api/scdo/ScdoSignTransaction.d.ts +2 -5
  233. package/dist/api/scdo/ScdoSignTransaction.d.ts.map +1 -1
  234. package/dist/api/solana/SolGetAddress.d.ts +1 -0
  235. package/dist/api/solana/SolGetAddress.d.ts.map +1 -1
  236. package/dist/api/solana/SolSignMessage.d.ts +3 -0
  237. package/dist/api/solana/SolSignMessage.d.ts.map +1 -1
  238. package/dist/api/solana/SolSignOffchainMessage.d.ts +3 -0
  239. package/dist/api/solana/SolSignOffchainMessage.d.ts.map +1 -1
  240. package/dist/api/solana/SolSignTransaction.d.ts +6 -0
  241. package/dist/api/solana/SolSignTransaction.d.ts.map +1 -1
  242. package/dist/api/stellar/StellarGetAddress.d.ts +2 -1
  243. package/dist/api/stellar/StellarGetAddress.d.ts.map +1 -1
  244. package/dist/api/stellar/StellarSignTransaction.d.ts +3 -2
  245. package/dist/api/stellar/StellarSignTransaction.d.ts.map +1 -1
  246. package/dist/api/sui/SuiGetAddress.d.ts +3 -0
  247. package/dist/api/sui/SuiGetAddress.d.ts.map +1 -1
  248. package/dist/api/sui/SuiGetPublicKey.d.ts +3 -0
  249. package/dist/api/sui/SuiGetPublicKey.d.ts.map +1 -1
  250. package/dist/api/sui/SuiSignMessage.d.ts +3 -0
  251. package/dist/api/sui/SuiSignMessage.d.ts.map +1 -1
  252. package/dist/api/sui/SuiSignTransaction.d.ts +5 -2
  253. package/dist/api/sui/SuiSignTransaction.d.ts.map +1 -1
  254. package/dist/api/ton/TonGetAddress.d.ts +3 -0
  255. package/dist/api/ton/TonGetAddress.d.ts.map +1 -1
  256. package/dist/api/ton/TonSignData.d.ts +1 -0
  257. package/dist/api/ton/TonSignData.d.ts.map +1 -1
  258. package/dist/api/ton/TonSignMessage.d.ts +3 -0
  259. package/dist/api/ton/TonSignMessage.d.ts.map +1 -1
  260. package/dist/api/ton/TonSignProof.d.ts +3 -0
  261. package/dist/api/ton/TonSignProof.d.ts.map +1 -1
  262. package/dist/api/tron/TronSignMessage.d.ts +3 -0
  263. package/dist/api/tron/TronSignMessage.d.ts.map +1 -1
  264. package/dist/api/tron/TronSignTransaction.d.ts +3 -0
  265. package/dist/api/tron/TronSignTransaction.d.ts.map +1 -1
  266. package/dist/api/utils.d.ts +2 -1
  267. package/dist/api/utils.d.ts.map +1 -1
  268. package/dist/constants/index.d.ts +2 -1
  269. package/dist/constants/index.d.ts.map +1 -1
  270. package/dist/core/RequestQueue.d.ts +19 -1
  271. package/dist/core/RequestQueue.d.ts.map +1 -1
  272. package/dist/core/deviceEventRegistration.d.ts +11 -0
  273. package/dist/core/deviceEventRegistration.d.ts.map +1 -0
  274. package/dist/core/index.d.ts +4 -1
  275. package/dist/core/index.d.ts.map +1 -1
  276. package/dist/data-manager/DataManager.d.ts +9 -5
  277. package/dist/data-manager/DataManager.d.ts.map +1 -1
  278. package/dist/data-manager/MessagesConfig.d.ts +2 -2
  279. package/dist/data-manager/MessagesConfig.d.ts.map +1 -1
  280. package/dist/data-manager/TransportManager.d.ts +5 -4
  281. package/dist/data-manager/TransportManager.d.ts.map +1 -1
  282. package/dist/data-manager/connectSettings.d.ts.map +1 -1
  283. package/dist/device/Device.d.ts +60 -16
  284. package/dist/device/Device.d.ts.map +1 -1
  285. package/dist/device/DeviceCommands.d.ts +16 -14
  286. package/dist/device/DeviceCommands.d.ts.map +1 -1
  287. package/dist/device/DeviceConnector.d.ts +2 -1
  288. package/dist/device/DeviceConnector.d.ts.map +1 -1
  289. package/dist/device/DeviceFeaturesState.d.ts +4 -0
  290. package/dist/device/DeviceFeaturesState.d.ts.map +1 -0
  291. package/dist/device/DevicePool.d.ts +4 -2
  292. package/dist/device/DevicePool.d.ts.map +1 -1
  293. package/dist/device/DeviceStateMapper.d.ts +13 -0
  294. package/dist/device/DeviceStateMapper.d.ts.map +1 -0
  295. package/dist/device/DeviceStateProjector.d.ts +7 -0
  296. package/dist/device/DeviceStateProjector.d.ts.map +1 -0
  297. package/dist/device/DeviceStateStore.d.ts +21 -0
  298. package/dist/device/DeviceStateStore.d.ts.map +1 -0
  299. package/dist/device/DeviceWalletSessionStore.d.ts +15 -0
  300. package/dist/device/DeviceWalletSessionStore.d.ts.map +1 -0
  301. package/dist/device/cloneDeviceState.d.ts +2 -0
  302. package/dist/device/cloneDeviceState.d.ts.map +1 -0
  303. package/dist/device/deviceIdentity.d.ts +5 -0
  304. package/dist/device/deviceIdentity.d.ts.map +1 -0
  305. package/dist/deviceProfile/buildDeviceFeatures.d.ts +12 -0
  306. package/dist/deviceProfile/buildDeviceFeatures.d.ts.map +1 -0
  307. package/dist/deviceProfile/index.d.ts +2 -0
  308. package/dist/deviceProfile/index.d.ts.map +1 -0
  309. package/dist/events/call.d.ts +7 -0
  310. package/dist/events/call.d.ts.map +1 -1
  311. package/dist/events/core.d.ts +2 -2
  312. package/dist/events/core.d.ts.map +1 -1
  313. package/dist/events/device.d.ts +11 -2
  314. package/dist/events/device.d.ts.map +1 -1
  315. package/dist/events/iframe.d.ts +1 -0
  316. package/dist/events/iframe.d.ts.map +1 -1
  317. package/dist/events/logBlockEvent.d.ts +1 -0
  318. package/dist/events/logBlockEvent.d.ts.map +1 -1
  319. package/dist/events/ui-request.d.ts +26 -2
  320. package/dist/events/ui-request.d.ts.map +1 -1
  321. package/dist/index.d.ts +1797 -451
  322. package/dist/index.d.ts.map +1 -1
  323. package/dist/index.js +26905 -5611
  324. package/dist/inject.d.ts +3 -2
  325. package/dist/inject.d.ts.map +1 -1
  326. package/dist/lowLevelInject.d.ts +2 -1
  327. package/dist/lowLevelInject.d.ts.map +1 -1
  328. package/dist/protocols/protocol-v2/deviceSession.d.ts +7 -0
  329. package/dist/protocols/protocol-v2/deviceSession.d.ts.map +1 -0
  330. package/dist/protocols/protocol-v2/features.d.ts +88 -0
  331. package/dist/protocols/protocol-v2/features.d.ts.map +1 -0
  332. package/dist/protocols/protocol-v2/firmware.d.ts +15 -0
  333. package/dist/protocols/protocol-v2/firmware.d.ts.map +1 -0
  334. package/dist/protocols/protocol-v2/index.d.ts +5 -0
  335. package/dist/protocols/protocol-v2/index.d.ts.map +1 -0
  336. package/dist/protocols/protocol-v2/lockedError.d.ts +2 -0
  337. package/dist/protocols/protocol-v2/lockedError.d.ts.map +1 -0
  338. package/dist/protocols/protocol-v2/settingsUnlockPolicy.d.ts +16 -0
  339. package/dist/protocols/protocol-v2/settingsUnlockPolicy.d.ts.map +1 -0
  340. package/dist/protocols/protocol-v2/uiInteraction.d.ts +39 -0
  341. package/dist/protocols/protocol-v2/uiInteraction.d.ts.map +1 -0
  342. package/dist/protocols/protocol-v2/unlockRetry.d.ts +11 -0
  343. package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +1 -0
  344. package/dist/protocols/protocol-v2/walletSession.d.ts +21 -0
  345. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -0
  346. package/dist/topLevelInject.d.ts.map +1 -1
  347. package/dist/types/api/checkAllFirmwareRelease.d.ts +4 -2
  348. package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
  349. package/dist/types/api/deviceSettings.d.ts +9 -0
  350. package/dist/types/api/deviceSettings.d.ts.map +1 -1
  351. package/dist/types/api/deviceUnlock.d.ts +1 -1
  352. package/dist/types/api/deviceUnlock.d.ts.map +1 -1
  353. package/dist/types/api/deviceUpdateBootloader.d.ts +6 -1
  354. package/dist/types/api/deviceUpdateBootloader.d.ts.map +1 -1
  355. package/dist/types/api/export.d.ts +4 -1
  356. package/dist/types/api/export.d.ts.map +1 -1
  357. package/dist/types/api/firmwareUpdate.d.ts +114 -1
  358. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  359. package/dist/types/api/firmwareUpdateCapabilities.d.ts +13 -0
  360. package/dist/types/api/firmwareUpdateCapabilities.d.ts.map +1 -0
  361. package/dist/types/api/firmwareUpdatePlan.d.ts +35 -0
  362. package/dist/types/api/firmwareUpdatePlan.d.ts.map +1 -0
  363. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts +43 -0
  364. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts.map +1 -0
  365. package/dist/types/api/getDeviceState.d.ts +12 -0
  366. package/dist/types/api/getDeviceState.d.ts.map +1 -0
  367. package/dist/types/api/getFeatures.d.ts.map +1 -1
  368. package/dist/types/api/getOnekeyFeatures.d.ts.map +1 -1
  369. package/dist/types/api/getPassphraseState.d.ts +2 -1
  370. package/dist/types/api/getPassphraseState.d.ts.map +1 -1
  371. package/dist/types/api/index.d.ts +48 -2
  372. package/dist/types/api/index.d.ts.map +1 -1
  373. package/dist/types/api/openWalletSession.d.ts +35 -0
  374. package/dist/types/api/openWalletSession.d.ts.map +1 -0
  375. package/dist/types/api/protocolV2.d.ts +113 -0
  376. package/dist/types/api/protocolV2.d.ts.map +1 -0
  377. package/dist/types/api/searchDevices.d.ts +2 -2
  378. package/dist/types/api/searchDevices.d.ts.map +1 -1
  379. package/dist/types/api/sessionCache.d.ts +10 -0
  380. package/dist/types/api/sessionCache.d.ts.map +1 -0
  381. package/dist/types/device.d.ts +207 -5
  382. package/dist/types/device.d.ts.map +1 -1
  383. package/dist/types/params.d.ts +3 -0
  384. package/dist/types/params.d.ts.map +1 -1
  385. package/dist/types/settings.d.ts +33 -0
  386. package/dist/types/settings.d.ts.map +1 -1
  387. package/dist/utils/capabilitieUtils.d.ts.map +1 -1
  388. package/dist/utils/deviceFeaturesCompat.d.ts +13 -0
  389. package/dist/utils/deviceFeaturesCompat.d.ts.map +1 -0
  390. package/dist/utils/deviceFeaturesUtils.d.ts +17 -9
  391. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  392. package/dist/utils/deviceInfoUtils.d.ts +10 -8
  393. package/dist/utils/deviceInfoUtils.d.ts.map +1 -1
  394. package/dist/utils/deviceSettings.d.ts +2 -0
  395. package/dist/utils/deviceSettings.d.ts.map +1 -1
  396. package/dist/utils/deviceVersionUtils.d.ts +6 -5
  397. package/dist/utils/deviceVersionUtils.d.ts.map +1 -1
  398. package/dist/utils/findDefectiveBatchDevice.d.ts +1 -1
  399. package/dist/utils/homescreen.d.ts.map +1 -1
  400. package/dist/utils/index.d.ts +3 -1
  401. package/dist/utils/index.d.ts.map +1 -1
  402. package/dist/utils/patch.d.ts +1 -1
  403. package/dist/utils/patch.d.ts.map +1 -1
  404. package/dist/utils/pro2Wallpaper.d.ts +12 -0
  405. package/dist/utils/pro2Wallpaper.d.ts.map +1 -0
  406. package/dist/utils/tracing.d.ts.map +1 -1
  407. package/dist/utils/versionUtils.d.ts +1 -1
  408. package/package.json +4 -4
  409. package/src/api/BaseMethod.ts +60 -23
  410. package/src/api/CheckAllFirmwareRelease.ts +16 -3
  411. package/src/api/CheckBootloaderRelease.ts +1 -2
  412. package/src/api/CheckFirmwareRelease.ts +1 -2
  413. package/src/api/ClearSessionCache.ts +28 -0
  414. package/src/api/DirList.ts +33 -0
  415. package/src/api/DirMake.ts +23 -0
  416. package/src/api/DirRemove.ts +23 -0
  417. package/src/api/FileDelete.ts +23 -0
  418. package/src/api/FileRead.ts +183 -0
  419. package/src/api/FileWrite.ts +71 -0
  420. package/src/api/FirmwareUpdate.ts +15 -7
  421. package/src/api/FirmwareUpdateV2.ts +354 -148
  422. package/src/api/FirmwareUpdateV3.ts +376 -62
  423. package/src/api/FirmwareUpdateV4.ts +2279 -0
  424. package/src/api/GetDeviceState.ts +55 -0
  425. package/src/api/GetFeatures.ts +12 -4
  426. package/src/api/GetOnekeyFeatures.ts +15 -3
  427. package/src/api/GetPassphraseState.ts +9 -10
  428. package/src/api/OpenWalletSession.ts +211 -0
  429. package/src/api/PathInfo.ts +41 -0
  430. package/src/api/PromptWebDeviceAccess.ts +10 -1
  431. package/src/api/SearchDevices.ts +33 -3
  432. package/src/api/UploadPortfolio.ts +50 -0
  433. package/src/api/alephium/AlephiumGetAddress.ts +6 -2
  434. package/src/api/alephium/AlephiumSignMessage.ts +6 -1
  435. package/src/api/alephium/AlephiumSignTransaction.ts +15 -4
  436. package/src/api/allnetwork/AllNetworkGetAddressBase.ts +25 -21
  437. package/src/api/aptos/AptosGetAddress.ts +2 -3
  438. package/src/api/benfen/BenfenGetAddress.ts +11 -7
  439. package/src/api/benfen/BenfenGetPublicKey.ts +6 -2
  440. package/src/api/benfen/BenfenSignMessage.ts +6 -1
  441. package/src/api/benfen/BenfenSignTransaction.ts +6 -1
  442. package/src/api/btc/BTCGetPublicKey.ts +3 -2
  443. package/src/api/btc/BTCSignPsbt.ts +1 -2
  444. package/src/api/btc/helpers/versionLimit.ts +7 -1
  445. package/src/api/cardano/CardanoSignTransaction.ts +2 -4
  446. package/src/api/conflux/ConfluxSignTransaction.ts +5 -2
  447. package/src/api/device/DeviceCancel.ts +1 -0
  448. package/src/api/device/DeviceChangePin.ts +24 -0
  449. package/src/api/device/DeviceFullyUploadResource.ts +6 -7
  450. package/src/api/device/DeviceLock.ts +2 -3
  451. package/src/api/device/DeviceRebootToBoardloader.ts +11 -1
  452. package/src/api/device/DeviceRebootToBootloader.ts +11 -1
  453. package/src/api/device/DeviceSettings.ts +121 -0
  454. package/src/api/device/DeviceSupportFeatures.ts +2 -13
  455. package/src/api/device/DeviceUnlock.ts +9 -0
  456. package/src/api/device/DeviceUpdateBootloader.ts +175 -11
  457. package/src/api/device/DeviceUploadResource.ts +4 -5
  458. package/src/api/device/DeviceVerify.ts +1 -2
  459. package/src/api/device/DeviceWipe.ts +19 -0
  460. package/src/api/dynex/DnxGetAddress.ts +6 -0
  461. package/src/api/dynex/DnxSignTransaction.ts +6 -0
  462. package/src/api/evm/EVMGetAddress.ts +1 -1
  463. package/src/api/evm/EVMGetPublicKey.ts +3 -4
  464. package/src/api/evm/EVMSignMessage.ts +1 -1
  465. package/src/api/evm/EVMSignMessageEIP712.ts +14 -1
  466. package/src/api/evm/EVMSignTransaction.ts +1 -1
  467. package/src/api/evm/EVMSignTypedData.ts +30 -18
  468. package/src/api/evm/EVMVerifyMessage.ts +1 -1
  469. package/src/api/evm/latest/signTypedHash.ts +2 -4
  470. package/src/api/evm/legacyV1/getAddress.ts +5 -3
  471. package/src/api/evm/legacyV1/getPublicKey.ts +5 -3
  472. package/src/api/evm/legacyV1/signMessage.ts +5 -3
  473. package/src/api/evm/legacyV1/signTypedData.ts +9 -8
  474. package/src/api/evm/legacyV1/signTypedHash.ts +7 -7
  475. package/src/api/evm/legacyV1/verifyMessage.ts +5 -3
  476. package/src/api/firmware/FirmwareArtifactSource.ts +278 -0
  477. package/src/api/firmware/FirmwareCheckpoint.ts +188 -0
  478. package/src/api/firmware/FirmwareHostBinding.ts +108 -0
  479. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +83 -15
  480. package/src/api/firmware/FirmwareUpdateCapabilities.ts +13 -0
  481. package/src/api/firmware/FirmwareUpdatePlan.ts +729 -0
  482. package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +403 -0
  483. package/src/api/firmware/bootloaderHelper.ts +3 -2
  484. package/src/api/firmware/getBinary.ts +1 -1
  485. package/src/api/firmware/progressThrottle.ts +44 -0
  486. package/src/api/firmware/releaseHelper.ts +3 -3
  487. package/src/api/firmware/updateBootloader.ts +19 -4
  488. package/src/api/firmware/uploadFirmware.ts +148 -34
  489. package/src/api/helpers/batchGetPublickeys.ts +51 -6
  490. package/src/api/helpers/filesystemValidation.ts +112 -0
  491. package/src/api/helpers/protocolV2FileWrite.ts +193 -0
  492. package/src/api/index.ts +32 -0
  493. package/src/api/kaspa/KaspaSignTransaction.ts +2 -3
  494. package/src/api/lightning/LnurlAuth.ts +4 -0
  495. package/src/api/neo/NeoGetAddress.ts +6 -1
  496. package/src/api/neo/NeoSignTransaction.ts +6 -1
  497. package/src/api/nervos/NervosGetAddress.ts +6 -2
  498. package/src/api/nervos/NervosSignTransaction.ts +14 -4
  499. package/src/api/nexa/NexaGetAddress.ts +6 -2
  500. package/src/api/nexa/NexaSignTransaction.ts +11 -12
  501. package/src/api/polkadot/networks.ts +9 -0
  502. package/src/api/protocol-v2/DeviceFactoryInfoGet.ts +20 -0
  503. package/src/api/protocol-v2/DeviceFactoryInfoSet.ts +32 -0
  504. package/src/api/protocol-v2/DeviceFirmwareUpdate.ts +61 -0
  505. package/src/api/protocol-v2/DeviceGetFirmwareUpdateStatus.ts +55 -0
  506. package/src/api/protocol-v2/DeviceGetOnboardingStatus.ts +18 -0
  507. package/src/api/protocol-v2/DeviceInfoGet.ts +145 -0
  508. package/src/api/protocol-v2/DeviceReboot.ts +26 -0
  509. package/src/api/protocol-v2/DeviceSessionOpen.ts +102 -0
  510. package/src/api/protocol-v2/DeviceSettingsGet.ts +18 -0
  511. package/src/api/protocol-v2/DeviceSettingsPageShow.ts +70 -0
  512. package/src/api/protocol-v2/DeviceSettingsSet.ts +44 -0
  513. package/src/api/protocol-v2/DeviceStatusGet.ts +16 -0
  514. package/src/api/protocol-v2/DeviceUploadWallpaper.ts +131 -0
  515. package/src/api/protocol-v2/FilesystemFormat.ts +19 -0
  516. package/src/api/protocol-v2/FilesystemPermissionFix.ts +16 -0
  517. package/src/api/protocol-v2/Ping.ts +18 -0
  518. package/src/api/protocol-v2/ProtocolInfoRequest.ts +16 -0
  519. package/src/api/protocol-v2/helpers.ts +203 -0
  520. package/src/api/scdo/ScdoGetAddress.ts +6 -2
  521. package/src/api/scdo/ScdoSignMessage.ts +6 -1
  522. package/src/api/scdo/ScdoSignTransaction.ts +6 -2
  523. package/src/api/solana/SolGetAddress.ts +4 -0
  524. package/src/api/solana/SolSignMessage.ts +3 -0
  525. package/src/api/solana/SolSignOffchainMessage.ts +3 -0
  526. package/src/api/solana/SolSignTransaction.ts +6 -0
  527. package/src/api/stellar/StellarGetAddress.ts +10 -1
  528. package/src/api/stellar/StellarSignTransaction.ts +14 -1
  529. package/src/api/sui/SuiGetAddress.ts +5 -3
  530. package/src/api/sui/SuiGetPublicKey.ts +3 -0
  531. package/src/api/sui/SuiSignMessage.ts +3 -0
  532. package/src/api/sui/SuiSignTransaction.ts +14 -12
  533. package/src/api/ton/TonGetAddress.ts +3 -0
  534. package/src/api/ton/TonSignData.ts +6 -3
  535. package/src/api/ton/TonSignMessage.ts +6 -5
  536. package/src/api/ton/TonSignProof.ts +3 -0
  537. package/src/api/tron/TronSignMessage.ts +4 -1
  538. package/src/api/tron/TronSignTransaction.ts +3 -0
  539. package/src/api/utils.ts +7 -3
  540. package/src/api/xrp/XrpSignTransaction.ts +1 -1
  541. package/src/constants/index.ts +7 -1
  542. package/src/core/RequestQueue.ts +124 -4
  543. package/src/core/deviceEventRegistration.ts +23 -0
  544. package/src/core/index.ts +253 -125
  545. package/src/data/messages/messages-protocol-v2.json +13544 -0
  546. package/src/data-manager/DataManager.ts +61 -49
  547. package/src/data-manager/MessagesConfig.ts +14 -14
  548. package/src/data-manager/TransportManager.ts +38 -12
  549. package/src/data-manager/connectSettings.ts +15 -0
  550. package/src/device/Device.ts +759 -164
  551. package/src/device/DeviceCommands.ts +245 -36
  552. package/src/device/DeviceConnector.ts +29 -4
  553. package/src/device/DeviceFeaturesState.ts +34 -0
  554. package/src/device/DevicePool.ts +69 -21
  555. package/src/device/DeviceStateMapper.ts +348 -0
  556. package/src/device/DeviceStateProjector.ts +95 -0
  557. package/src/device/DeviceStateStore.ts +201 -0
  558. package/src/device/DeviceWalletSessionStore.ts +78 -0
  559. package/src/device/cloneDeviceState.ts +39 -0
  560. package/src/device/deviceIdentity.ts +18 -0
  561. package/src/deviceProfile/buildDeviceFeatures.ts +391 -0
  562. package/src/deviceProfile/index.ts +4 -0
  563. package/src/events/call.ts +6 -0
  564. package/src/events/core.ts +2 -0
  565. package/src/events/device.ts +17 -1
  566. package/src/events/iframe.ts +1 -0
  567. package/src/events/logBlockEvent.ts +30 -0
  568. package/src/events/ui-request.ts +32 -2
  569. package/src/index.ts +9 -0
  570. package/src/inject.ts +75 -3
  571. package/src/lowLevelInject.ts +8 -1
  572. package/src/protocols/protocol-v2/deviceSession.ts +24 -0
  573. package/src/protocols/protocol-v2/features.ts +163 -0
  574. package/src/protocols/protocol-v2/firmware.ts +21 -0
  575. package/src/protocols/protocol-v2/index.ts +18 -0
  576. package/src/protocols/protocol-v2/lockedError.ts +10 -0
  577. package/src/protocols/protocol-v2/settingsUnlockPolicy.ts +61 -0
  578. package/src/protocols/protocol-v2/uiInteraction.ts +210 -0
  579. package/src/protocols/protocol-v2/unlockRetry.ts +76 -0
  580. package/src/protocols/protocol-v2/walletSession.ts +202 -0
  581. package/src/topLevelInject.ts +7 -1
  582. package/src/types/api/checkAllFirmwareRelease.ts +4 -2
  583. package/src/types/api/deviceSettings.ts +9 -0
  584. package/src/types/api/deviceUnlock.ts +1 -1
  585. package/src/types/api/deviceUpdateBootloader.ts +10 -1
  586. package/src/types/api/export.ts +25 -0
  587. package/src/types/api/firmwareUpdate.ts +163 -1
  588. package/src/types/api/firmwareUpdateCapabilities.ts +13 -0
  589. package/src/types/api/firmwareUpdatePlan.ts +52 -0
  590. package/src/types/api/firmwareUpdatePreparedPlan.ts +55 -0
  591. package/src/types/api/getDeviceState.ts +19 -0
  592. package/src/types/api/getFeatures.ts +3 -0
  593. package/src/types/api/getOnekeyFeatures.ts +3 -0
  594. package/src/types/api/getPassphraseState.ts +10 -2
  595. package/src/types/api/index.ts +89 -2
  596. package/src/types/api/openWalletSession.ts +54 -0
  597. package/src/types/api/protocolV2.ts +244 -0
  598. package/src/types/api/searchDevices.ts +2 -2
  599. package/src/types/api/sessionCache.ts +14 -0
  600. package/src/types/device.ts +266 -5
  601. package/src/types/params.ts +11 -0
  602. package/src/types/settings.ts +54 -29
  603. package/src/utils/capabilitieUtils.ts +1 -2
  604. package/src/utils/deviceFeaturesCompat.ts +173 -0
  605. package/src/utils/deviceFeaturesUtils.ts +122 -84
  606. package/src/utils/deviceInfoUtils.ts +38 -87
  607. package/src/utils/deviceSettings.ts +48 -0
  608. package/src/utils/deviceVersionUtils.ts +25 -62
  609. package/src/utils/findDefectiveBatchDevice.ts +8 -8
  610. package/src/utils/homescreen.ts +10 -0
  611. package/src/utils/index.ts +4 -0
  612. package/src/utils/pro2Wallpaper.ts +109 -0
  613. package/src/utils/tracing.ts +3 -1
@@ -1,6 +1,6 @@
1
1
  import EventEmitter from 'events';
2
2
  import semver from 'semver';
3
- import { Enum_Capability } from '@onekeyfe/hd-transport';
3
+ import { DeviceRebootType, Enum_Capability } from '@onekeyfe/hd-transport';
4
4
  import {
5
5
  EDeviceType,
6
6
  ERRORS,
@@ -9,31 +9,56 @@ import {
9
9
  HardwareError,
10
10
  HardwareErrorCode,
11
11
  createDeferred,
12
+ createDeviceNotSupportMethodError,
12
13
  } from '@onekeyfe/hd-shared';
13
14
 
14
15
  import {
15
16
  LoggerNames,
16
17
  getDeviceBLEFirmwareVersion,
17
18
  getDeviceBleName,
19
+ getDeviceBootloaderVersion,
18
20
  getDeviceFirmwareVersion,
19
21
  getDeviceLabel,
22
+ getDeviceSerialNo,
20
23
  getDeviceType,
21
- getDeviceUUID,
24
+ getFirmwareType,
22
25
  getLogger,
23
- getMethodVersionRange,
24
26
  } from '../utils';
25
27
  import {
26
28
  fixFeaturesFirmwareVersion,
27
29
  getPassphraseStateWithRefreshDeviceInfo,
30
+ supportInputPinOnSoftware,
31
+ supportModifyHomescreen,
28
32
  } from '../utils/deviceFeaturesUtils';
29
33
  import { generateInstanceId } from '../utils/tracing';
30
34
  // eslint-disable-next-line import/no-cycle
31
35
  import { DeviceCommands } from './DeviceCommands';
36
+ import { mergeDeviceFeaturesPatch } from './DeviceFeaturesState';
37
+ import {
38
+ mapDeviceSettingsToState,
39
+ mapFeaturesToState,
40
+ mapProtocolV1OnekeyFeaturesToState,
41
+ mapProtocolV2DeviceInfoToState,
42
+ mapProtocolV2DeviceStatusToState,
43
+ } from './DeviceStateMapper';
44
+ import { projectFeatures } from './DeviceStateProjector';
45
+ import { DeviceStateStore, createPublicDeviceState } from './DeviceStateStore';
46
+ import { cloneDeviceState } from './cloneDeviceState';
47
+ import { deviceWalletSessionStore } from './DeviceWalletSessionStore';
32
48
  import {
33
49
  type DeviceFirmwareRange,
50
+ DeviceModelToTypes,
51
+ type DeviceStateEvent,
52
+ type DeviceStatePatch,
53
+ type DeviceStateUpdateSource,
54
+ DeviceTypeToModels,
34
55
  type Device as DeviceTyped,
35
56
  EOneKeyDeviceMode,
36
57
  type Features,
58
+ type IDeviceModel,
59
+ type IDeviceType,
60
+ type IVersionRange,
61
+ type SupportFeatureType,
37
62
  type UnavailableCapabilities,
38
63
  } from '../types';
39
64
  import { DEVICE, UI_REQUEST } from '../events';
@@ -41,16 +66,32 @@ import { DataManager } from '../data-manager';
41
66
  import TransportManager from '../data-manager/TransportManager';
42
67
  import { toHardened } from '../api/helpers/pathUtils';
43
68
  import { existCapability } from '../utils/capabilitieUtils';
69
+ import {
70
+ PROTOCOL_V2_FEATURES_DEVICE_INFO_REQUEST,
71
+ PROTOCOL_V2_FULL_DEVICE_INFO_REQUEST,
72
+ PROTOCOL_V2_VERSIONS_DEVICE_INFO_REQUEST,
73
+ type ProtocolV2RuntimeMode,
74
+ getProtocolV2RuntimeMode,
75
+ requestProtocolV2DeviceInfo,
76
+ requestProtocolV2DeviceStatus,
77
+ } from '../protocols/protocol-v2/features';
78
+ import { buildProtocolV1FeaturesPayload } from '../deviceProfile';
44
79
 
45
80
  import type { PROTO } from '../constants';
81
+ import type { DeviceStateReadOptions } from '../types/api/getDeviceState';
46
82
  import type {
47
83
  DeviceButtonRequestPayload,
48
84
  DeviceFeaturesPayload,
49
85
  PassphraseRequestPayload,
50
86
  } from '../events';
51
87
  import type { PassphrasePromptResponse } from './DeviceCommands';
52
- import type { Deferred } from '@onekeyfe/hd-shared';
53
- import type { OneKeyDeviceInfo as DeviceDescriptor } from '@onekeyfe/hd-transport';
88
+ import type { Deferred, HardwareConnectProtocol } from '@onekeyfe/hd-shared';
89
+ import type {
90
+ OneKeyDeviceInfo as DeviceDescriptor,
91
+ DeviceStatus,
92
+ ProtocolV2DeviceInfo,
93
+ Success,
94
+ } from '@onekeyfe/hd-transport';
54
95
  import type DeviceConnector from './DeviceConnector';
55
96
 
56
97
  export type InitOptions = {
@@ -58,6 +99,10 @@ export type InitOptions = {
58
99
  deviceId?: string;
59
100
  passphraseState?: string;
60
101
  deriveCardano?: boolean;
102
+ connectProtocol?: HardwareConnectProtocol;
103
+ protocolV2DeviceInfoTimeoutMs?: number;
104
+ /** Refresh Protocol V2 runtime state before returning discovery results. */
105
+ refreshRuntimeState?: boolean;
61
106
  };
62
107
 
63
108
  export type RunOptions = {
@@ -74,9 +119,11 @@ const Log = getLogger(LoggerNames.Device);
74
119
 
75
120
  export interface DeviceEvents {
76
121
  [DEVICE.PIN]: [Device, PROTO.PinMatrixRequestType | undefined, (err: any, pin: string) => void];
77
- [DEVICE.PASSPHRASE_ON_DEVICE]: [Device, ((response: any) => void)?];
122
+ [DEVICE.PASSPHRASE_ON_DEVICE]: [Device, PassphraseRequestPayload?];
123
+ [DEVICE.ATTACH_PIN_ON_DEVICE]: [Device, PassphraseRequestPayload?];
78
124
  [DEVICE.BUTTON]: [Device, DeviceButtonRequestPayload];
79
125
  [DEVICE.FEATURES]: [Device, DeviceFeaturesPayload];
126
+ [DEVICE.STATE]: [Device, DeviceStateEvent];
80
127
  [DEVICE.PASSPHRASE]: [
81
128
  Device,
82
129
  PassphraseRequestPayload,
@@ -100,8 +147,6 @@ export interface Device {
100
147
  emit<K extends keyof DeviceEvents>(type: K, ...args: DeviceEvents[K]): boolean;
101
148
  }
102
149
 
103
- const deviceSessionCache: Record<string, string> = {};
104
-
105
150
  /**
106
151
  * Pre-populate the device session cache with a known session ID.
107
152
  *
@@ -119,8 +164,7 @@ export function preloadSessionCache(
119
164
  passphraseState: string,
120
165
  sessionId: string
121
166
  ): void {
122
- const key = `${deviceId}@${passphraseState}`;
123
- deviceSessionCache[key] = sessionId;
167
+ deviceWalletSessionStore.set(deviceId, passphraseState, sessionId);
124
168
  }
125
169
 
126
170
  export class Device extends EventEmitter {
@@ -166,15 +210,51 @@ export class Device extends EventEmitter {
166
210
  */
167
211
  private deviceAcquired = false;
168
212
 
169
- /**
170
- * 设备信息
171
- */
172
- features: Features | undefined = undefined;
213
+ /** Canonical device-state cache; legacy Features is a compatibility projection. */
214
+ private stateStore = new DeviceStateStore();
173
215
 
174
- /**
175
- * 是否需要更新设备信息
176
- */
177
- featuresNeedsReload = false;
216
+ /** Force the next initialization to reload DeviceInfo after reconnect or reboot. */
217
+ private protocolV2StateNeedsReload = false;
218
+
219
+ get state() {
220
+ return this.stateStore.getState();
221
+ }
222
+
223
+ get features(): Features | undefined {
224
+ if (!this.state) return undefined;
225
+ const features = projectFeatures(this.state);
226
+ const sessionCacheDeviceKey =
227
+ this.state.identity.deviceId ||
228
+ (this.isProtocolV2()
229
+ ? this.originalDescriptor.path || this.originalDescriptor.id
230
+ : undefined);
231
+ const sessionId =
232
+ sessionCacheDeviceKey && this.passphraseState
233
+ ? deviceWalletSessionStore.get(sessionCacheDeviceKey, this.passphraseState)
234
+ : undefined;
235
+ return sessionId
236
+ ? {
237
+ ...features,
238
+ sessionId,
239
+ session_id: sessionId,
240
+ passphraseState: this.passphraseState,
241
+ }
242
+ : features;
243
+ }
244
+
245
+ set features(features: Features | undefined) {
246
+ this.stateStore = new DeviceStateStore();
247
+ if (features) {
248
+ this.stateStore.update(mapFeaturesToState(features), 'compatibility');
249
+ const { passphraseState } = features;
250
+ const sessionId = features.sessionId ?? features.session_id;
251
+ const deviceKey = this.getSessionCacheDeviceKey(features.deviceId ?? undefined);
252
+ if (passphraseState && sessionId && deviceKey) {
253
+ this.passphraseState = passphraseState;
254
+ deviceWalletSessionStore.set(deviceKey, passphraseState, sessionId);
255
+ }
256
+ }
257
+ }
178
258
 
179
259
  runPromise?: Deferred<void> | null;
180
260
 
@@ -228,32 +308,41 @@ export class Device extends EventEmitter {
228
308
 
229
309
  // simplified object to pass via postMessage
230
310
  toMessageObject(): DeviceTyped | null {
231
- if (this.isUnacquired() || !this.features) return null;
311
+ if (this.isUnacquired()) return null;
232
312
 
233
313
  const env = DataManager.getSettings('env');
234
- const deviceType = getDeviceType(this.features);
314
+ const deviceType = this.getCurrentDeviceType();
235
315
 
236
- const bleName = getDeviceBleName(this.features);
237
- const label = getDeviceLabel(this.features);
316
+ const bleName = this.getCurrentBleName();
317
+ const label = this.getCurrentLabel();
318
+ const serialNo = this.getCurrentSerialNo();
319
+ const connectId = this.getConnectId();
320
+ const deviceId = this.getCurrentDeviceId() || null;
321
+
322
+ const { features } = this;
323
+ const state = this.state ? createPublicDeviceState(this.state) : undefined;
238
324
 
239
325
  return {
240
- /** Android uses Mac address, iOS uses uuid, USB uses uuid */
241
- connectId: DataManager.isBleConnect(env) ? this.mainId || null : getDeviceUUID(this.features),
242
- /** Hardware ID, will not change at any time */
243
- uuid: getDeviceUUID(this.features),
326
+ /** Android uses a MAC address, while iOS and USB use transport-specific identifiers. */
327
+ connectId: DataManager.isBleConnect(env) ? this.mainId || null : connectId,
328
+ /** Stable physical-device identity. */
329
+ serialNo,
330
+ /** @deprecated Use serialNo instead. */
331
+ uuid: serialNo,
244
332
  commType: this.originalDescriptor.commType,
245
333
  sdkInstanceId: this.sdkInstanceId,
246
334
  instanceId: this.instanceId,
247
335
  createdAt: this.createdAt,
248
336
  deviceType,
249
337
  /** ID for current seeds, will clear after replace a new seed at device */
250
- deviceId: this.features.device_id || null,
338
+ deviceId,
251
339
  path: this.originalDescriptor?.path,
252
340
  bleName,
253
341
  name: bleName || label || `OneKey ${deviceType?.toUpperCase()}`,
254
342
  label: label || 'OneKey',
255
343
  mode: this.getMode(),
256
- features: this.features,
344
+ features,
345
+ state,
257
346
  firmwareVersion: this.getFirmwareVersion(),
258
347
  bleFirmwareVersion: this.getBLEFirmwareVersion(),
259
348
  unavailableCapabilities: this.unavailableCapabilities,
@@ -264,13 +353,13 @@ export class Device extends EventEmitter {
264
353
  * Device connect
265
354
  * @returns {Promise<boolean>}
266
355
  */
267
- connect() {
356
+ connect(connectProtocol?: HardwareConnectProtocol) {
268
357
  const env = DataManager.getSettings('env');
269
358
  // eslint-disable-next-line no-async-promise-executor
270
359
  return new Promise<boolean>(async (resolve, reject) => {
271
360
  if (DataManager.isBleConnect(env)) {
272
361
  try {
273
- await this.acquire();
362
+ await this.acquire(connectProtocol);
274
363
  resolve(true);
275
364
  } catch (error) {
276
365
  reject(error);
@@ -280,7 +369,7 @@ export class Device extends EventEmitter {
280
369
  // 不存在 Session ID 或存在 Session ID 但设备在别处使用,都需要 acquire 获取最新 sessionID
281
370
  if (!this.mainId || (!this.isUsedHere() && this.originalDescriptor)) {
282
371
  try {
283
- await this.acquire();
372
+ await this.acquire(connectProtocol);
284
373
  resolve(true);
285
374
  } catch (error) {
286
375
  reject(error);
@@ -295,29 +384,60 @@ export class Device extends EventEmitter {
295
384
  });
296
385
  }
297
386
 
298
- async acquire() {
387
+ async acquire(
388
+ connectProtocol?: HardwareConnectProtocol,
389
+ options?: { throwOnRunPromiseError?: boolean }
390
+ ) {
299
391
  const env = DataManager.getSettings('env');
300
392
  const mainIdKey = DataManager.isBleConnect(env) ? 'id' : 'session';
393
+ const expectedProtocol = connectProtocol ?? this.originalDescriptor.protocolType;
301
394
  try {
395
+ let acquireResult: unknown;
302
396
  if (DataManager.isBleConnect(env)) {
303
- const res = await this.deviceConnector?.acquire(this.originalDescriptor.id);
304
- this.mainId = (res as unknown as any).uuid ?? '';
397
+ // acquire starts a new BLE session. Any previous runPromise belongs to a dead
398
+ // session, such as an interrupted probe or firmware-update reboot, and would
399
+ // otherwise block the new session. Device cannot distinguish recovery from a
400
+ // regular acquire, so BLE always clears it intentionally.
401
+ acquireResult = await this.deviceConnector?.acquire(
402
+ this.originalDescriptor.id,
403
+ undefined,
404
+ true,
405
+ expectedProtocol
406
+ );
407
+ this.mainId = (acquireResult as any)?.uuid ?? '';
305
408
  Log.debug('Expected uuid:', this.mainId);
306
409
  } else {
307
- this.mainId = await this.deviceConnector?.acquire(
410
+ acquireResult = await this.deviceConnector?.acquire(
308
411
  this.originalDescriptor.path,
309
- this.originalDescriptor.session
412
+ this.originalDescriptor.session,
413
+ undefined,
414
+ expectedProtocol
310
415
  );
416
+ this.mainId = acquireResult as string | undefined;
311
417
  Log.debug('Expected session id:', this.mainId);
312
418
  }
313
419
  this.deviceAcquired = true;
314
420
  this.updateDescriptor({ [mainIdKey]: this.mainId } as unknown as DeviceDescriptor);
421
+
422
+ // Propagate protocol version detected during acquire.
423
+ const detectedProtocol =
424
+ (acquireResult as { protocolType?: HardwareConnectProtocol } | undefined)?.protocolType ??
425
+ TransportManager.transport?.getProtocolType?.(
426
+ DataManager.isBleConnect(env) ? this.originalDescriptor.id : this.originalDescriptor.path
427
+ );
428
+ if (detectedProtocol) {
429
+ this.originalDescriptor.protocolType = detectedProtocol;
430
+ }
431
+
315
432
  if (this.commands) {
316
433
  await this.commands.dispose(false);
317
434
  }
318
435
 
319
436
  this.commands = new DeviceCommands(this, this.mainId ?? '');
320
437
  } catch (error) {
438
+ if (options?.throwOnRunPromiseError) {
439
+ throw error;
440
+ }
321
441
  if (this.runPromise) {
322
442
  this.runPromise.reject(error);
323
443
  } else {
@@ -368,12 +488,10 @@ export class Device extends EventEmitter {
368
488
  }
369
489
 
370
490
  /**
371
- * Pre-initialize: connect + Initialize ahead of the sign. Only runs the
372
- * fallback init when features are missing (gate on `!this.features`, not
373
- * isUsedHere which is always false on BLE); otherwise just records the mark.
491
+ * Pre-initialize: connect + Initialize ahead of the sign.
374
492
  */
375
493
  async preInitialize(initOptions?: InitOptions) {
376
- if (!this.features) {
494
+ if (this.isUnacquired()) {
377
495
  await this.acquire();
378
496
  await this.initialize(initOptions);
379
497
  }
@@ -419,23 +537,174 @@ export class Device extends EventEmitter {
419
537
  return this.commands;
420
538
  }
421
539
 
422
- private generateStateKey(deviceId: string, passphraseState?: string) {
423
- if (passphraseState) {
424
- return `${deviceId}@${passphraseState}`;
540
+ /**
541
+ * Canonical protocol discriminator.
542
+ *
543
+ * descriptor.protocolType is established by active probing; V2 features are mapped
544
+ * from DeviceInfoGet. All protocol branches must use this method instead of reading
545
+ * originalDescriptor.protocolType directly or inferring a protocol from features.
546
+ */
547
+ getProtocol(): 'V1' | 'V2' {
548
+ return this.originalDescriptor.protocolType === 'V2' ? 'V2' : 'V1';
549
+ }
550
+
551
+ isProtocolV2() {
552
+ return this.getProtocol() === 'V2';
553
+ }
554
+
555
+ getCurrentDeviceType() {
556
+ return getDeviceType(this.features);
557
+ }
558
+
559
+ getCurrentDeviceId() {
560
+ return this.features?.deviceId || undefined;
561
+ }
562
+
563
+ getCurrentSerialNo() {
564
+ return getDeviceSerialNo(this.features);
565
+ }
566
+
567
+ getConnectId() {
568
+ const serialNo = this.getCurrentSerialNo();
569
+ if (serialNo) return serialNo;
570
+
571
+ // connectId is an internal routing key. Falling back to the transport descriptor
572
+ // does not change the business meaning of features.serialNo or deviceId.
573
+ return this.originalDescriptor.path || this.originalDescriptor.id || '';
574
+ }
575
+
576
+ getCurrentBleName() {
577
+ return getDeviceBleName(this.features);
578
+ }
579
+
580
+ getCurrentLabel() {
581
+ return getDeviceLabel(this.features);
582
+ }
583
+
584
+ getCurrentPassphraseProtection() {
585
+ return this.features?.passphraseProtection;
586
+ }
587
+
588
+ getCurrentFirmwareType() {
589
+ return getFirmwareType(this.features);
590
+ }
591
+
592
+ getCurrentFirmwareVersionString() {
593
+ return getDeviceFirmwareVersion(this.features)?.join('.');
594
+ }
595
+
596
+ getCurrentBLEFirmwareVersionString() {
597
+ if (!this.features) return undefined;
598
+ return getDeviceBLEFirmwareVersion(this.features).join('.');
599
+ }
600
+
601
+ getCurrentBootloaderVersionString() {
602
+ if (!this.features) return undefined;
603
+ return getDeviceBootloaderVersion(this.features).join('.');
604
+ }
605
+
606
+ getCurrentSafetyChecks() {
607
+ return this.features?.safetyChecks;
608
+ }
609
+
610
+ getCurrentMethodVersionRange(
611
+ getVersionRange: (deviceModel: IDeviceType | IDeviceModel) => IVersionRange | undefined
612
+ ) {
613
+ const deviceType = this.getCurrentDeviceType();
614
+ const versionRange = getVersionRange(deviceType);
615
+ if (versionRange) return versionRange;
616
+
617
+ // Most-specific model first; must match getMethodVersionRange in deviceInfoUtils,
618
+ // otherwise e.g. Classic1s resolves the looser model_mini range before model_classic1s.
619
+ const modelFallbacks: IDeviceModel[] = [
620
+ 'model_classic1s',
621
+ 'model_classic',
622
+ 'model_mini',
623
+ 'model_touch',
624
+ ];
625
+ for (const model of modelFallbacks) {
626
+ if (DeviceTypeToModels[deviceType]?.includes(model)) {
627
+ const fallbackRange = getVersionRange(model);
628
+ if (fallbackRange) return fallbackRange;
629
+ }
630
+ }
631
+ return undefined;
632
+ }
633
+
634
+ supportNewPassphrase(): SupportFeatureType {
635
+ const deviceType = this.getCurrentDeviceType();
636
+ if (
637
+ deviceType === EDeviceType.Touch ||
638
+ deviceType === EDeviceType.Pro ||
639
+ deviceType === EDeviceType.Pro2
640
+ ) {
641
+ return { support: true };
642
+ }
643
+
644
+ const firmwareVersion = this.getCurrentFirmwareVersionString();
645
+ return {
646
+ support: Boolean(firmwareVersion && semver.gte(firmwareVersion, '2.4.0')),
647
+ require: '2.4.0',
648
+ };
649
+ }
650
+
651
+ supportInputPinOnSoftware(): SupportFeatureType {
652
+ if (this.features) return supportInputPinOnSoftware(this.features);
653
+
654
+ const deviceType = this.getCurrentDeviceType();
655
+ if (
656
+ deviceType === EDeviceType.Touch ||
657
+ deviceType === EDeviceType.Pro ||
658
+ deviceType === EDeviceType.Pro2
659
+ ) {
660
+ return { support: false };
425
661
  }
426
- return deviceId;
662
+
663
+ const firmwareVersion = this.getCurrentFirmwareVersionString();
664
+ return {
665
+ support: Boolean(firmwareVersion && semver.gte(firmwareVersion, '2.3.0')),
666
+ require: '2.3.0',
667
+ };
668
+ }
669
+
670
+ supportModifyHomescreen(): SupportFeatureType {
671
+ // Pro2 has an independent 1.x version line and must not use the Touch/Pro 3.4.0
672
+ // threshold. ApplySettings includes homescreen from the first V2 firmware version.
673
+ if (this.isProtocolV2()) {
674
+ return { support: true };
675
+ }
676
+
677
+ if (this.features) return supportModifyHomescreen(this.features);
678
+
679
+ const deviceType = this.getCurrentDeviceType();
680
+ if (DeviceModelToTypes.model_mini.includes(deviceType)) {
681
+ return { support: true };
682
+ }
683
+
684
+ const firmwareVersion = this.getCurrentFirmwareVersionString();
685
+ return {
686
+ support: Boolean(firmwareVersion && semver.gte(firmwareVersion, '3.4.0')),
687
+ };
688
+ }
689
+
690
+ private getSessionCacheDeviceKey(_deviceId?: string) {
691
+ const deviceId = _deviceId || this.getCurrentDeviceId();
692
+ if (deviceId) return deviceId;
693
+ if (this.isProtocolV2()) {
694
+ return this.originalDescriptor.path || this.originalDescriptor.id;
695
+ }
696
+ return undefined;
427
697
  }
428
698
 
429
699
  getInternalState(_deviceId?: string) {
430
- Log.debug('getInternalState session cache: ', deviceSessionCache);
431
700
  Log.debug(
432
701
  'getInternalState session param: ',
433
702
  `device_id: ${_deviceId}`,
434
- `features.device_id: ${this.features?.device_id}`,
435
- `passphraseState: ${this.passphraseState}`
703
+ `features.deviceId: ${this.features?.deviceId}`,
704
+ `hasPassphraseState: ${Boolean(this.passphraseState)}`
436
705
  );
437
706
 
438
- const deviceId = _deviceId || this.features?.device_id;
707
+ const deviceId = this.getSessionCacheDeviceKey(_deviceId);
439
708
  if (!deviceId) return undefined;
440
709
  // Security invariant: no passphraseState → no session lookup.
441
710
  // A previous fallback that scanned `${deviceId}@*` keys could silently
@@ -445,15 +714,14 @@ export class Device extends EventEmitter {
445
714
  // downstream call carries it and hits the primary lookup below.
446
715
  if (!this.passphraseState) return undefined;
447
716
 
448
- const usePassKey = this.generateStateKey(deviceId, this.passphraseState);
449
- return deviceSessionCache[usePassKey];
717
+ return deviceWalletSessionStore.get(deviceId, this.passphraseState);
450
718
  }
451
719
 
452
720
  // attach to pin to fix internal state
453
721
  updateInternalState(
454
722
  enablePassphrase: boolean,
455
723
  passphraseState: string | undefined,
456
- deviceId: string,
724
+ deviceId: string | undefined,
457
725
  sessionId: string | null = null,
458
726
  featuresSessionId: string | null = null
459
727
  ) {
@@ -461,67 +729,73 @@ export class Device extends EventEmitter {
461
729
  'updateInternalState session param: ',
462
730
  `device_id: ${deviceId}`,
463
731
  `enablePassphrase: ${enablePassphrase}`,
464
- `passphraseState: ${passphraseState}`,
465
- `sessionId: ${sessionId}`,
466
- `featuresSessionId: ${featuresSessionId}`
732
+ `hasPassphraseState: ${Boolean(passphraseState)}`,
733
+ `hasSessionId: ${Boolean(sessionId)}`,
734
+ `hasFeaturesSessionId: ${Boolean(featuresSessionId)}`
467
735
  );
468
736
 
469
- if (enablePassphrase) {
470
- // update the sessionId
471
- if (sessionId) {
472
- deviceSessionCache[this.generateStateKey(deviceId, passphraseState)] = sessionId;
473
- } else if (featuresSessionId) {
474
- deviceSessionCache[this.generateStateKey(deviceId, passphraseState)] = featuresSessionId;
475
- }
476
- }
737
+ const cacheDeviceKey = this.getSessionCacheDeviceKey(deviceId);
738
+ if (!cacheDeviceKey) return;
477
739
 
478
- // delete the old sessionId
479
- const oldKey = `${deviceId}`;
480
- if (deviceSessionCache[oldKey]) {
481
- delete deviceSessionCache[oldKey];
740
+ if (enablePassphrase) {
741
+ const walletSessionId =
742
+ sessionId || featuresSessionId || deviceWalletSessionStore.getPending(cacheDeviceKey);
743
+ deviceWalletSessionStore.set(cacheDeviceKey, passphraseState, walletSessionId ?? undefined);
482
744
  }
483
745
 
484
- Log.debug('updateInternalState session cache: ', deviceSessionCache);
746
+ deviceWalletSessionStore.deletePending(cacheDeviceKey);
485
747
  }
486
748
 
487
749
  private setInternalState(state: string, initSession?: boolean) {
488
750
  Log.debug(
489
751
  'setInternalState session param: ',
490
- `state: ${state}`,
752
+ `hasState: ${Boolean(state)}`,
491
753
  `initSession: ${initSession}`,
492
- `device_id: ${this.features?.device_id}`,
493
- `passphraseState: ${this.passphraseState}`
754
+ `deviceId: ${this.features?.deviceId}`,
755
+ `hasPassphraseState: ${Boolean(this.passphraseState)}`
494
756
  );
495
757
 
496
- if (!this.features) return;
497
758
  if (!this.passphraseState && !initSession) return;
498
759
 
499
- const deviceId = this.features?.device_id;
760
+ const deviceId = this.getSessionCacheDeviceKey();
500
761
  if (!deviceId) return;
501
762
 
502
- const key = this.generateStateKey(deviceId, this.passphraseState);
503
-
504
- if (state) {
505
- deviceSessionCache[key] = state;
763
+ if (this.passphraseState) {
764
+ deviceWalletSessionStore.set(deviceId, this.passphraseState, state);
765
+ } else if (initSession) {
766
+ deviceWalletSessionStore.setPending(deviceId, state);
506
767
  }
507
- Log.debug('setInternalState done session cache: ', deviceSessionCache);
508
768
  }
509
769
 
510
770
  clearInternalState(_deviceId?: string) {
511
771
  Log.debug('clearInternalState param: ', _deviceId);
512
772
 
513
- const deviceId = _deviceId || this.features?.device_id;
773
+ const deviceId = this.getSessionCacheDeviceKey(_deviceId);
514
774
  if (!deviceId) return;
515
- const key = `${deviceId}`;
516
- delete deviceSessionCache[key];
775
+ deviceWalletSessionStore.deletePending(deviceId);
517
776
 
518
777
  if (this.passphraseState) {
519
- const usePassKey = this.generateStateKey(deviceId, this.passphraseState);
520
- delete deviceSessionCache[usePassKey];
778
+ deviceWalletSessionStore.delete(deviceId, this.passphraseState);
521
779
  }
522
780
  }
523
781
 
524
782
  async initialize(options?: InitOptions) {
783
+ // Protocol V2 does not support legacy Initialize; use its dedicated flow.
784
+ if (this.isProtocolV2()) {
785
+ this.passphraseState = options?.passphraseState;
786
+ if (this.state && !options?.initSession && !this.protocolV2StateNeedsReload) {
787
+ if (this.features?.bootloaderMode) {
788
+ return;
789
+ }
790
+ // Normal calls reuse the cache. Explicit getDeviceState refreshes, unlock flow,
791
+ // and device events update dynamic state without polling on every SDK call.
792
+ return;
793
+ }
794
+ await this._initializeProtocolV2(options);
795
+ this.protocolV2StateNeedsReload = false;
796
+ return;
797
+ }
798
+
525
799
  // Log.debug('initialize param:', options);
526
800
 
527
801
  this.passphraseState = options?.passphraseState;
@@ -565,26 +839,284 @@ export class Device extends EventEmitter {
565
839
  }
566
840
  }
567
841
 
842
+ /**
843
+ * Device initialization over Protocol V2.
844
+ *
845
+ * Protocol V2 bypasses legacy Initialize/GetFeatures and builds its canonical
846
+ * features state directly from DeviceInfoGet.
847
+ */
848
+ private async _initializeProtocolV2(options?: InitOptions) {
849
+ Log.debug('Initialize device via Protocol V2 features adapter');
850
+
851
+ try {
852
+ // typedCall applies the request timeout. An outer Promise.race would leak its
853
+ // timer and leave the underlying call active after rejection.
854
+ const deviceInfo = await requestProtocolV2DeviceInfo({
855
+ commands: this.commands,
856
+ timeoutMs: options?.protocolV2DeviceInfoTimeoutMs,
857
+ });
858
+ // The default request excludes SE/hash data and therefore uses basic scope.
859
+ // Full version and verification data require getDeviceState({ scope: 'firmware' }).
860
+ const features = await this.probeProtocolV2RuntimeState(
861
+ deviceInfo,
862
+ options?.protocolV2DeviceInfoTimeoutMs
863
+ );
864
+ Log.debug('Protocol V2 features:', features);
865
+ } catch (error) {
866
+ Log.error('Protocol V2 initialization failed:', error);
867
+ throw error;
868
+ }
869
+ }
870
+
568
871
  async getFeatures() {
872
+ if (this.isProtocolV2()) {
873
+ const deviceInfo = await requestProtocolV2DeviceInfo({
874
+ commands: this.commands,
875
+ });
876
+ return this.probeProtocolV2RuntimeState(deviceInfo);
877
+ }
878
+
569
879
  const { message } = await this.commands.typedCall('GetFeatures', 'Features', {});
570
880
  this._updateFeatures(message);
881
+ return this.features;
571
882
  }
572
883
 
573
- _updateFeatures(feat: Features, initSession?: boolean) {
574
- // GetFeatures doesn't return 'session_id'
575
- if (this.features && this.features.session_id && !feat.session_id) {
576
- feat.session_id = this.features.session_id;
884
+ async getDeviceState(params: DeviceStateReadOptions = {}) {
885
+ const refresh = new Set(params.refreshSections ?? []);
886
+ const getProtocolV2DeviceInfoRequest = () => {
887
+ if (refresh.has('verification')) return PROTOCOL_V2_FULL_DEVICE_INFO_REQUEST;
888
+ if (refresh.has('versions')) return PROTOCOL_V2_VERSIONS_DEVICE_INFO_REQUEST;
889
+ return PROTOCOL_V2_FEATURES_DEVICE_INFO_REQUEST;
890
+ };
891
+ let initializedWithDeviceInfo = false;
892
+ let refreshedDeviceInfo: ProtocolV2DeviceInfo | undefined;
893
+
894
+ if (!this.state) {
895
+ if (this.isProtocolV2()) {
896
+ const deviceInfo = await requestProtocolV2DeviceInfo({
897
+ commands: this.commands,
898
+ request: getProtocolV2DeviceInfoRequest(),
899
+ });
900
+ await this.probeProtocolV2RuntimeState(deviceInfo);
901
+ refreshedDeviceInfo = deviceInfo;
902
+ initializedWithDeviceInfo = true;
903
+ } else {
904
+ await this.getFeatures();
905
+ }
906
+ } else if (!this.isProtocolV2() && refresh.size > 0) {
907
+ await this.getFeatures();
908
+ }
909
+
910
+ if (!this.isProtocolV2() && refresh.has('verification')) {
911
+ const { message } = await this.commands.typedCall('OnekeyGetFeatures', 'OnekeyFeatures');
912
+ this.updateState(mapProtocolV1OnekeyFeaturesToState(message), 'device-info');
913
+ }
914
+
915
+ if (this.isProtocolV2()) {
916
+ const refreshDeviceInfo =
917
+ refresh.has('identity') || refresh.has('versions') || refresh.has('verification');
918
+ if (refreshDeviceInfo && !initializedWithDeviceInfo) {
919
+ const deviceInfo = await requestProtocolV2DeviceInfo({
920
+ commands: this.commands,
921
+ request: getProtocolV2DeviceInfoRequest(),
922
+ });
923
+ refreshedDeviceInfo = deviceInfo;
924
+ if (!refresh.has('status')) {
925
+ this.updateState(
926
+ mapProtocolV2DeviceInfoToState(deviceInfo, this.state?.status.mode),
927
+ 'device-info'
928
+ );
929
+ }
930
+ }
931
+
932
+ if (refresh.has('status') && !initializedWithDeviceInfo) {
933
+ const deviceInfo =
934
+ refreshedDeviceInfo ??
935
+ (await requestProtocolV2DeviceInfo({
936
+ commands: this.commands,
937
+ request: getProtocolV2DeviceInfoRequest(),
938
+ }));
939
+ await this.probeProtocolV2RuntimeState(deviceInfo);
940
+ }
941
+
942
+ if (refresh.has('settings') && this.state?.status.mode === 'normal') {
943
+ const { message } = await this.commands.typedCall(
944
+ 'DeviceSettingsGet',
945
+ 'DeviceSettings',
946
+ {}
947
+ );
948
+ this.updateState(mapDeviceSettingsToState(message), 'apply-settings');
949
+ }
950
+ }
951
+
952
+ if (!this.state) {
953
+ throw ERRORS.TypedError(HardwareErrorCode.DeviceInitializeFailed);
577
954
  }
578
- if (this.features && this.features.device_id && feat.session_id) {
579
- this.setInternalState(feat.session_id, initSession);
955
+ return params.includeRaw ? cloneDeviceState(this.state) : createPublicDeviceState(this.state);
956
+ }
957
+
958
+ _updateFeatures(protoFeatures: PROTO.Features | Features, initSession?: boolean) {
959
+ const previousCacheDeviceKey = this.getSessionCacheDeviceKey();
960
+ let feat =
961
+ 'protocol' in protoFeatures
962
+ ? protoFeatures
963
+ : buildProtocolV1FeaturesPayload(protoFeatures, this.features);
964
+
965
+ // GetFeatures doesn't return 'session_id'
966
+ if (this.features?.sessionId && !feat.sessionId) {
967
+ feat.sessionId = this.features.sessionId;
580
968
  }
581
969
  feat.unlocked = feat.unlocked ?? true;
582
970
 
583
971
  feat = fixFeaturesFirmwareVersion(feat);
584
972
 
585
- this.features = feat;
586
- this.featuresNeedsReload = false;
587
- this.emit(DEVICE.FEATURES, this, feat);
973
+ this.updateState(mapFeaturesToState(feat), 'initialize');
974
+ const nextCacheDeviceKey = this.getSessionCacheDeviceKey();
975
+ if (previousCacheDeviceKey && nextCacheDeviceKey) {
976
+ deviceWalletSessionStore.migrateDeviceKey(previousCacheDeviceKey, nextCacheDeviceKey);
977
+ }
978
+ if (feat.deviceId && feat.sessionId) {
979
+ this.setInternalState(feat.sessionId, initSession);
980
+ }
981
+ }
982
+
983
+ updateState(patch: DeviceStatePatch, source: DeviceStateUpdateSource) {
984
+ const result = this.stateStore.update(patch, source);
985
+ if (result.changedKeys.length === 0) return result.state;
986
+
987
+ const event: DeviceStateEvent = {
988
+ connectId: this.getConnectId() ?? null,
989
+ state: createPublicDeviceState(result.state),
990
+ revision: result.revision,
991
+ source,
992
+ changedKeys: result.changedKeys,
993
+ };
994
+ Log.debug('Device state patch committed', {
995
+ source,
996
+ keys: result.changedKeys,
997
+ });
998
+ this.emit(DEVICE.STATE, this, event);
999
+ if (result.state.protocol === 'V1') {
1000
+ this.emit(DEVICE.FEATURES, this, projectFeatures(result.state));
1001
+ }
1002
+ return result.state;
1003
+ }
1004
+
1005
+ updateFeaturesPatch(patch: Partial<Features>, source: DeviceStateUpdateSource) {
1006
+ const currentFeatures = this.features;
1007
+ if (!currentFeatures) return undefined;
1008
+
1009
+ const features = mergeDeviceFeaturesPatch(currentFeatures, patch);
1010
+ if (features === currentFeatures) return currentFeatures;
1011
+
1012
+ const normalized = fixFeaturesFirmwareVersion(features);
1013
+ this.updateState(mapFeaturesToState(normalized), source);
1014
+ return this.features;
1015
+ }
1016
+
1017
+ async probeProtocolV2RuntimeState(deviceInfo: ProtocolV2DeviceInfo, timeoutMs?: number) {
1018
+ let deviceStatus: DeviceStatus;
1019
+ try {
1020
+ deviceStatus = await requestProtocolV2DeviceStatus({
1021
+ commands: this.commands,
1022
+ timeoutMs,
1023
+ });
1024
+ } catch (error) {
1025
+ const runtimeMode = getProtocolV2RuntimeMode({
1026
+ deviceInfo,
1027
+ deviceStatusAvailable: false,
1028
+ });
1029
+ Log.debug('Protocol V2 DeviceStatusGet unavailable; use temporary loader fallback', {
1030
+ runtimeMode,
1031
+ error: error instanceof Error ? error.message : String(error),
1032
+ });
1033
+ return this.updateProtocolV2Features(deviceInfo, null, runtimeMode);
1034
+ }
1035
+ return this.updateProtocolV2Features(deviceInfo, deviceStatus, 'normal');
1036
+ }
1037
+
1038
+ updateProtocolV2Features(
1039
+ deviceInfo?: ProtocolV2DeviceInfo,
1040
+ deviceStatus?: DeviceStatus | null,
1041
+ runtimeMode?: ProtocolV2RuntimeMode
1042
+ ) {
1043
+ const previousCacheDeviceKey = this.getSessionCacheDeviceKey();
1044
+ const resolvedMode =
1045
+ runtimeMode ??
1046
+ (deviceStatus
1047
+ ? getProtocolV2RuntimeMode({ deviceInfo, deviceStatusAvailable: true })
1048
+ : this.state?.status.mode);
1049
+ if (deviceInfo) {
1050
+ this.updateState(mapProtocolV2DeviceInfoToState(deviceInfo, resolvedMode), 'device-info');
1051
+ }
1052
+ if (deviceStatus) {
1053
+ this.updateState(mapProtocolV2DeviceStatusToState(deviceStatus), 'device-status');
1054
+ }
1055
+ const nextCacheDeviceKey = this.getSessionCacheDeviceKey();
1056
+ if (previousCacheDeviceKey && nextCacheDeviceKey) {
1057
+ deviceWalletSessionStore.migrateDeviceKey(previousCacheDeviceKey, nextCacheDeviceKey);
1058
+ }
1059
+ return this.features as Features;
1060
+ }
1061
+
1062
+ updateProtocolV2Status(status: DeviceStatus) {
1063
+ const previousDeviceInfo = this.state?.raw?.protocolV2DeviceInfo;
1064
+ const previousStatus = this.state?.raw?.protocolV2DeviceStatus;
1065
+ return this.updateProtocolV2Features(previousDeviceInfo, {
1066
+ ...previousStatus,
1067
+ ...status,
1068
+ });
1069
+ }
1070
+
1071
+ markTransportDisconnected() {
1072
+ if (!this.isProtocolV2()) return;
1073
+ this.protocolV2StateNeedsReload = true;
1074
+ this.clearPreInitialized();
1075
+ }
1076
+
1077
+ markProtocolV2Reboot(rebootType: DeviceRebootType) {
1078
+ if (!this.isProtocolV2()) return;
1079
+
1080
+ this.protocolV2StateNeedsReload = true;
1081
+ this.clearPreInitialized();
1082
+ let loaderMode: 'bootloader' | 'romloader' | undefined;
1083
+ if (rebootType === DeviceRebootType.Bootloader) {
1084
+ loaderMode = 'bootloader';
1085
+ } else if (rebootType === DeviceRebootType.Romloader) {
1086
+ loaderMode = 'romloader';
1087
+ }
1088
+ if (loaderMode) {
1089
+ this.updateState(
1090
+ {
1091
+ identity: { deviceId: null },
1092
+ status: {
1093
+ mode: loaderMode,
1094
+ initialized: null,
1095
+ unlocked: null,
1096
+ firmwarePresent: null,
1097
+ backupRequired: null,
1098
+ noBackup: null,
1099
+ unfinishedBackup: null,
1100
+ recoveryMode: null,
1101
+ passphraseProtection: null,
1102
+ pinProtection: null,
1103
+ attachToPinEnabled: null,
1104
+ unlockedAttachPin: null,
1105
+ },
1106
+ raw: { protocolV2DeviceStatus: null },
1107
+ },
1108
+ 'transport-reconnect'
1109
+ );
1110
+ return;
1111
+ }
1112
+
1113
+ this.updateState(
1114
+ {
1115
+ status: { mode: 'normal', unlocked: null },
1116
+ raw: { protocolV2DeviceStatus: null },
1117
+ },
1118
+ 'transport-reconnect'
1119
+ );
588
1120
  }
589
1121
 
590
1122
  /**
@@ -605,7 +1137,11 @@ export class Device extends EventEmitter {
605
1137
  }
606
1138
 
607
1139
  if (forceUpdate) {
608
- this.originalDescriptor = descriptor;
1140
+ // Enumerated descriptors may omit protocolType, so preserve an actively probed value.
1141
+ this.originalDescriptor = {
1142
+ ...descriptor,
1143
+ protocolType: descriptor.protocolType ?? this.originalDescriptor.protocolType,
1144
+ };
609
1145
  }
610
1146
  }
611
1147
 
@@ -626,19 +1162,37 @@ export class Device extends EventEmitter {
626
1162
 
627
1163
  options = parseRunOptions(options);
628
1164
 
629
- this.runPromise = createDeferred(this._runInner.bind(this, fn, options));
630
- return this.runPromise.promise;
1165
+ const runPromise = createDeferred<void>();
1166
+ this.runPromise = runPromise;
1167
+ this._runInner(fn, options, runPromise).catch(error => {
1168
+ if (this.runPromise === runPromise) {
1169
+ this.runPromise = null;
1170
+ }
1171
+ runPromise.reject(error);
1172
+ });
1173
+ return runPromise.promise;
631
1174
  }
632
1175
 
633
- async _runInner<T>(fn: (() => Promise<T>) | undefined, options: RunOptions) {
1176
+ async _runInner<T>(
1177
+ fn: (() => Promise<T>) | undefined,
1178
+ options: RunOptions,
1179
+ runPromise: Deferred<void>
1180
+ ) {
1181
+ const clearRunPromise = () => {
1182
+ if (this.runPromise === runPromise) {
1183
+ this.runPromise = null;
1184
+ }
1185
+ };
1186
+
634
1187
  if (!this.isUsedHere() || this.commands.disposed) {
635
1188
  const env = DataManager.getSettings('env');
636
1189
  if (env !== 'react-native') {
637
1190
  try {
638
- await this.acquire();
1191
+ await this.acquire(options.connectProtocol);
639
1192
  } catch (error) {
640
- this.runPromise = null;
641
- return Promise.reject(error);
1193
+ clearRunPromise();
1194
+ runPromise.reject(error);
1195
+ return;
642
1196
  }
643
1197
 
644
1198
  try {
@@ -648,16 +1202,19 @@ export class Device extends EventEmitter {
648
1202
  }
649
1203
  }
650
1204
  } catch (error) {
651
- this.runPromise = null;
1205
+ await this.release();
1206
+ clearRunPromise();
652
1207
  if (error instanceof HardwareError) {
653
- return Promise.reject(error);
1208
+ runPromise.reject(error);
1209
+ return;
654
1210
  }
655
- return Promise.reject(
1211
+ runPromise.reject(
656
1212
  ERRORS.TypedError(
657
1213
  HardwareErrorCode.DeviceInitializeFailed,
658
1214
  `Initialize failed: ${error.message as string}, code: ${error.code as string}`
659
1215
  )
660
1216
  );
1217
+ return;
661
1218
  }
662
1219
  } else if (env === 'react-native') {
663
1220
  // TODO: implement react-native acquire
@@ -676,9 +1233,7 @@ export class Device extends EventEmitter {
676
1233
  try {
677
1234
  await fn();
678
1235
  } catch (e) {
679
- if (this.runPromise) {
680
- this.runPromise.reject(e);
681
- }
1236
+ runPromise.reject(e);
682
1237
 
683
1238
  if (
684
1239
  e instanceof HardwareError &&
@@ -691,7 +1246,7 @@ export class Device extends EventEmitter {
691
1246
  Log.debug(`error code ${e.errorCode} release device, mainId: ${this.mainId}`);
692
1247
  }
693
1248
 
694
- this.runPromise = null;
1249
+ clearRunPromise();
695
1250
  return;
696
1251
  }
697
1252
  }
@@ -705,11 +1260,8 @@ export class Device extends EventEmitter {
705
1260
  Log.debug('release device, mainId: ', this.mainId);
706
1261
  }
707
1262
 
708
- if (this.runPromise) {
709
- this.runPromise.resolve();
710
- }
711
-
712
- this.runPromise = null;
1263
+ runPromise.resolve();
1264
+ clearRunPromise();
713
1265
  }
714
1266
 
715
1267
  async interruptionFromOutside() {
@@ -748,22 +1300,23 @@ export class Device extends EventEmitter {
748
1300
  }
749
1301
 
750
1302
  getMode() {
751
- if (this.features?.bootloader_mode) {
752
- // bootloader mode
753
- return EOneKeyDeviceMode.bootloader;
1303
+ switch (this.state?.status.mode) {
1304
+ case 'bootloader':
1305
+ case 'romloader':
1306
+ return EOneKeyDeviceMode.bootloader;
1307
+ case 'notInitialized':
1308
+ return EOneKeyDeviceMode.notInitialized;
1309
+ case 'backupMode':
1310
+ return EOneKeyDeviceMode.backupMode;
1311
+ case 'normal':
1312
+ return EOneKeyDeviceMode.normal;
1313
+ default:
1314
+ break;
754
1315
  }
755
1316
 
756
- if (!this.features?.initialized) {
757
- // not initialized
758
- return EOneKeyDeviceMode.notInitialized;
759
- }
760
-
761
- if (this.features?.no_backup) {
762
- // backup mode
763
- return EOneKeyDeviceMode.backupMode;
764
- }
765
-
766
- // normal mode
1317
+ if (this.features?.bootloaderMode) return EOneKeyDeviceMode.bootloader;
1318
+ if (this.features?.initialized === false) return EOneKeyDeviceMode.notInitialized;
1319
+ if (this.features?.noBackup) return EOneKeyDeviceMode.backupMode;
767
1320
  return EOneKeyDeviceMode.normal;
768
1321
  }
769
1322
 
@@ -802,7 +1355,7 @@ export class Device extends EventEmitter {
802
1355
  }
803
1356
 
804
1357
  isBootloader() {
805
- return this.features && !!this.features.bootloader_mode;
1358
+ return this.features && !!this.features.bootloaderMode;
806
1359
  }
807
1360
 
808
1361
  isInitialized() {
@@ -810,7 +1363,7 @@ export class Device extends EventEmitter {
810
1363
  }
811
1364
 
812
1365
  isSeedless() {
813
- return this.features && !!this.features.no_backup;
1366
+ return this.features && !!this.features.noBackup;
814
1367
  }
815
1368
 
816
1369
  isUnacquired(): boolean {
@@ -819,9 +1372,9 @@ export class Device extends EventEmitter {
819
1372
 
820
1373
  hasUnexpectedMode(allow: string[], require: string[]) {
821
1374
  // both allow and require cases might generate single unexpected mode
822
- if (this.features) {
1375
+ if (!this.isUnacquired()) {
823
1376
  // allow cases
824
- if (this.isBootloader() && !allow.includes(UI_REQUEST.BOOTLOADER)) {
1377
+ if (this.isBootloader() && !this.isProtocolV2() && !allow.includes(UI_REQUEST.BOOTLOADER)) {
825
1378
  return UI_REQUEST.BOOTLOADER;
826
1379
  }
827
1380
  if (!this.isInitialized() && !allow.includes(UI_REQUEST.NOT_INITIALIZE)) {
@@ -840,27 +1393,30 @@ export class Device extends EventEmitter {
840
1393
  }
841
1394
 
842
1395
  hasUsePassphrase() {
1396
+ const deviceType = this.getCurrentDeviceType();
843
1397
  const isModeT =
844
- getDeviceType(this.features) === EDeviceType.Touch ||
845
- getDeviceType(this.features) === EDeviceType.Pro;
846
- const preCheckTouch = isModeT && this.features?.unlocked === false;
847
-
848
- return this.features && (!!this.features.passphrase_protection || preCheckTouch);
1398
+ deviceType === EDeviceType.Touch ||
1399
+ deviceType === EDeviceType.Pro ||
1400
+ deviceType === EDeviceType.Pro2;
1401
+ const unlocked = this.features?.unlocked;
1402
+ const preCheckTouch = isModeT && unlocked === false;
1403
+ const passphraseProtection = this.getCurrentPassphraseProtection();
1404
+
1405
+ return Boolean(passphraseProtection === true || preCheckTouch);
849
1406
  }
850
1407
 
851
1408
  checkDeviceId(deviceId: string) {
852
- if (this.features) {
853
- return this.features.device_id === deviceId;
854
- }
855
- return false;
1409
+ return this.getCurrentDeviceId() === deviceId;
856
1410
  }
857
1411
 
858
- async lockDevice() {
1412
+ async lockDevice(): Promise<Success> {
859
1413
  const res = await this.commands.typedCall('LockDevice', 'Success', {});
1414
+ this.updateState({ status: { unlocked: false } }, 'lock');
860
1415
  return res.message;
861
1416
  }
862
1417
 
863
1418
  supportUnlockVersionRange(): DeviceFirmwareRange {
1419
+ // This range applies to Protocol V1 Pro devices; Pro2 has a dedicated unlock flow.
864
1420
  return {
865
1421
  pro: {
866
1422
  min: '4.15.0',
@@ -869,9 +1425,28 @@ export class Device extends EventEmitter {
869
1425
  }
870
1426
 
871
1427
  async unlockDevice() {
872
- const firmwareVersion = getDeviceFirmwareVersion(this.features)?.join('.');
873
- const versionRange = getMethodVersionRange(
874
- this.features,
1428
+ if (this.isProtocolV2()) {
1429
+ try {
1430
+ await this.commands.typedCall('DeviceSessionAskPin', 'Success', undefined, {
1431
+ timeoutMs: 120_000,
1432
+ });
1433
+ } catch (error) {
1434
+ const errorText =
1435
+ error instanceof Error
1436
+ ? `${error.name} ${error.message}`
1437
+ : String((error as { message?: unknown } | null)?.message ?? error ?? '');
1438
+ if (errorText.includes('Failure_UnexpectedMessage')) {
1439
+ throw createDeviceNotSupportMethodError('deviceUnlock', this.getCurrentFirmwareType());
1440
+ }
1441
+ throw error;
1442
+ }
1443
+
1444
+ const status = await requestProtocolV2DeviceStatus({ commands: this.commands });
1445
+ return this.updateProtocolV2Status(status);
1446
+ }
1447
+
1448
+ const firmwareVersion = this.getCurrentFirmwareVersionString() ?? '0.0.0';
1449
+ const versionRange = this.getCurrentMethodVersionRange(
875
1450
  type => this.supportUnlockVersionRange()[type]
876
1451
  );
877
1452
 
@@ -879,25 +1454,35 @@ export class Device extends EventEmitter {
879
1454
  this.features,
880
1455
  Enum_Capability.Capability_AttachToPin
881
1456
  );
882
-
883
1457
  const supportUnlock =
884
- supportAttachPinCapability || (versionRange && semver.gte(firmwareVersion, versionRange.min));
1458
+ supportAttachPinCapability ||
1459
+ (versionRange &&
1460
+ semver.valid(firmwareVersion) &&
1461
+ semver.gte(firmwareVersion, versionRange.min));
885
1462
 
886
1463
  if (supportUnlock) {
887
1464
  const res = await this.commands.typedCall('UnLockDevice', 'UnLockDeviceResponse');
888
1465
  if (this.features) {
889
- this.features.unlocked = res.message.unlocked == null ? null : res.message.unlocked;
890
- this.features.unlocked_attach_pin =
891
- res.message.unlocked_attach_pin == null ? undefined : res.message.unlocked_attach_pin;
892
- this.features.passphrase_protection =
893
- res.message.passphrase_protection == null ? null : res.message.passphrase_protection;
1466
+ this.updateState(
1467
+ {
1468
+ status: {
1469
+ unlocked: res.message.unlocked == null ? null : res.message.unlocked,
1470
+ unlockedAttachPin:
1471
+ res.message.unlocked_attach_pin == null ? null : res.message.unlocked_attach_pin,
1472
+ passphraseProtection:
1473
+ res.message.passphrase_protection == null
1474
+ ? null
1475
+ : res.message.passphrase_protection,
1476
+ },
1477
+ },
1478
+ 'unlock'
1479
+ );
894
1480
 
895
1481
  return Promise.resolve(this.features);
896
1482
  }
897
1483
 
898
- const featuresRes = await this.commands.typedCall('GetFeatures', 'Features');
899
- this._updateFeatures(featuresRes.message);
900
- return Promise.resolve(featuresRes.message);
1484
+ const features = await this.getFeatures();
1485
+ return Promise.resolve(features);
901
1486
  }
902
1487
 
903
1488
  const { type } = await this.commands.typedCall('GetAddress', 'Address', {
@@ -911,9 +1496,8 @@ export class Device extends EventEmitter {
911
1496
  if (type === 'CallMethodError') {
912
1497
  throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'unlock device error');
913
1498
  }
914
- const res = await this.commands.typedCall('GetFeatures', 'Features');
915
- this._updateFeatures(res.message);
916
- return Promise.resolve(res.message);
1499
+ const features = await this.getFeatures();
1500
+ return Promise.resolve(features);
917
1501
  }
918
1502
 
919
1503
  async checkPassphraseStateSafety(
@@ -921,30 +1505,41 @@ export class Device extends EventEmitter {
921
1505
  useEmptyPassphrase?: boolean,
922
1506
  skipPassphraseCheck?: boolean
923
1507
  ) {
924
- if (!this.features) return false;
1508
+ if (this.isUnacquired()) return false;
925
1509
 
1510
+ const expectedPassphraseState = useEmptyPassphrase ? undefined : passphraseState;
926
1511
  const { passphraseState: newPassphraseState, unlockedAttachPin } =
927
1512
  await getPassphraseStateWithRefreshDeviceInfo(this, {
928
- expectPassphraseState: passphraseState,
1513
+ expectPassphraseState: expectedPassphraseState,
929
1514
  onlyMainPin: useEmptyPassphrase,
930
1515
  });
931
1516
 
932
- if (skipPassphraseCheck) {
933
- return true;
934
- }
935
-
936
1517
  // Main wallet and unlock Attach Pin, throw safe error
937
1518
  const mainWalletUseAttachPin = unlockedAttachPin && useEmptyPassphrase;
938
1519
  const useErrorAttachPin =
939
- unlockedAttachPin && passphraseState && passphraseState !== newPassphraseState;
1520
+ unlockedAttachPin &&
1521
+ expectedPassphraseState &&
1522
+ expectedPassphraseState !== newPassphraseState;
1523
+ const passphraseStateMismatch =
1524
+ !!expectedPassphraseState && expectedPassphraseState !== newPassphraseState;
940
1525
 
941
1526
  Log.debug('Check passphrase state safety: ', {
942
- passphraseState,
943
- newPassphraseState,
1527
+ hasExpectedPassphraseState: Boolean(expectedPassphraseState),
1528
+ hasNewPassphraseState: Boolean(newPassphraseState),
1529
+ passphraseStateMatches:
1530
+ Boolean(expectedPassphraseState) && expectedPassphraseState === newPassphraseState,
944
1531
  unlockedAttachPin,
945
1532
  useEmptyPassphrase,
946
1533
  });
947
1534
 
1535
+ if (skipPassphraseCheck) {
1536
+ if (passphraseStateMismatch) {
1537
+ this.clearInternalState();
1538
+ return false;
1539
+ }
1540
+ return true;
1541
+ }
1542
+
948
1543
  if (mainWalletUseAttachPin || useErrorAttachPin) {
949
1544
  try {
950
1545
  await this.lockDevice();
@@ -956,7 +1551,7 @@ export class Device extends EventEmitter {
956
1551
  }
957
1552
 
958
1553
  // When exists passphraseState, check passphraseState
959
- if (passphraseState && passphraseState !== newPassphraseState) {
1554
+ if (passphraseStateMismatch) {
960
1555
  this.clearInternalState();
961
1556
  return false;
962
1557
  }