@onekeyfe/hd-core 1.2.0-alpha.20 → 1.2.0-alpha.22

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 (226) hide show
  1. package/__tests__/DeviceCommands.test.ts +87 -2
  2. package/__tests__/connectSettings.test.ts +47 -5
  3. package/__tests__/device-identity.test.ts +24 -2
  4. package/__tests__/device-lifecycle-events.test.ts +1 -1
  5. package/__tests__/device-settings.test.ts +111 -6
  6. package/__tests__/device-state-events.test.ts +3 -3
  7. package/__tests__/device-state-mapper.test.ts +15 -3
  8. package/__tests__/device-state-store.test.ts +1 -1
  9. package/__tests__/device-wallet-session-store.test.ts +111 -7
  10. package/__tests__/deviceSettings.test.ts +55 -15
  11. package/__tests__/evmLedgerLegacySafety.test.ts +6 -5
  12. package/__tests__/firmware-update/firmware-artifact-source.test.ts +119 -0
  13. package/__tests__/firmware-update/firmware-host-binding.test.ts +70 -0
  14. package/__tests__/firmware-update/firmware-update-capabilities.test.ts +13 -0
  15. package/__tests__/firmware-update/firmware-update-plan.test.ts +326 -0
  16. package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +200 -0
  17. package/__tests__/firmware-update/firmware-update-v2-download-before-boot.test.ts +39 -30
  18. package/__tests__/firmware-update/firmware-upload-source.test.ts +70 -0
  19. package/__tests__/get-device-state.test.ts +7 -0
  20. package/__tests__/open-wallet-session.test.ts +253 -13
  21. package/__tests__/pro2Wallpaper.test.ts +7 -18
  22. package/__tests__/protocol-v2-unlock-policy.test.ts +45 -0
  23. package/__tests__/protocol-v2.test.ts +439 -282
  24. package/__tests__/public-device-state-api.test.ts +79 -68
  25. package/__tests__/public-pro2-api-boundary.test.ts +22 -29
  26. package/dist/api/BaseMethod.d.ts +2 -4
  27. package/dist/api/BaseMethod.d.ts.map +1 -1
  28. package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
  29. package/dist/api/ClearSessionCache.d.ts.map +1 -1
  30. package/dist/api/FirmwareUpdate.d.ts.map +1 -1
  31. package/dist/api/FirmwareUpdateV2.d.ts +11 -0
  32. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  33. package/dist/api/FirmwareUpdateV3.d.ts +7 -3
  34. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  35. package/dist/api/FirmwareUpdateV4.d.ts +29 -10
  36. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  37. package/dist/api/OpenWalletSession.d.ts.map +1 -1
  38. package/dist/api/UploadPortfolio.d.ts +0 -1
  39. package/dist/api/UploadPortfolio.d.ts.map +1 -1
  40. package/dist/api/conflux/ConfluxGetAddress.d.ts +1 -0
  41. package/dist/api/conflux/ConfluxGetAddress.d.ts.map +1 -1
  42. package/dist/api/conflux/ConfluxSignMessage.d.ts +1 -0
  43. package/dist/api/conflux/ConfluxSignMessage.d.ts.map +1 -1
  44. package/dist/api/conflux/ConfluxSignMessageCIP23.d.ts +1 -0
  45. package/dist/api/conflux/ConfluxSignMessageCIP23.d.ts.map +1 -1
  46. package/dist/api/conflux/ConfluxSignTransaction.d.ts +1 -0
  47. package/dist/api/conflux/ConfluxSignTransaction.d.ts.map +1 -1
  48. package/dist/api/device/DeviceSettings.d.ts.map +1 -1
  49. package/dist/api/device/DeviceUpdateBootloader.d.ts +5 -1
  50. package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
  51. package/dist/api/firmware/FirmwareArtifactSource.d.ts +26 -0
  52. package/dist/api/firmware/FirmwareArtifactSource.d.ts.map +1 -0
  53. package/dist/api/firmware/FirmwareHostBinding.d.ts +6 -0
  54. package/dist/api/firmware/FirmwareHostBinding.d.ts.map +1 -0
  55. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +7 -0
  56. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  57. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts +3 -0
  58. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts.map +1 -0
  59. package/dist/api/firmware/FirmwareUpdatePlan.d.ts +24 -0
  60. package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +1 -0
  61. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts +25 -0
  62. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +1 -0
  63. package/dist/api/firmware/getBinary.d.ts +7 -0
  64. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  65. package/dist/api/firmware/updateBootloader.d.ts +2 -0
  66. package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
  67. package/dist/api/firmware/uploadFirmware.d.ts +9 -1
  68. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  69. package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -1
  70. package/dist/api/index.d.ts +0 -20
  71. package/dist/api/index.d.ts.map +1 -1
  72. package/dist/api/nexa/NexaSignTransaction.d.ts +1 -0
  73. package/dist/api/nexa/NexaSignTransaction.d.ts.map +1 -1
  74. package/dist/api/stellar/StellarSignTransaction.d.ts +1 -1
  75. package/dist/core/RequestQueue.d.ts +1 -19
  76. package/dist/core/RequestQueue.d.ts.map +1 -1
  77. package/dist/core/index.d.ts +0 -1
  78. package/dist/core/index.d.ts.map +1 -1
  79. package/dist/data-manager/DataManager.d.ts +1 -0
  80. package/dist/data-manager/DataManager.d.ts.map +1 -1
  81. package/dist/data-manager/connectSettings.d.ts.map +1 -1
  82. package/dist/device/Device.d.ts +1 -1
  83. package/dist/device/Device.d.ts.map +1 -1
  84. package/dist/device/DeviceCommands.d.ts.map +1 -1
  85. package/dist/device/DeviceFeaturesState.d.ts +0 -1
  86. package/dist/device/DeviceFeaturesState.d.ts.map +1 -1
  87. package/dist/device/DeviceStateMapper.d.ts.map +1 -1
  88. package/dist/device/DeviceStateProjector.d.ts.map +1 -1
  89. package/dist/deviceProfile/buildDeviceFeatures.d.ts.map +1 -1
  90. package/dist/events/call.d.ts +0 -7
  91. package/dist/events/call.d.ts.map +1 -1
  92. package/dist/events/core.d.ts +2 -2
  93. package/dist/events/core.d.ts.map +1 -1
  94. package/dist/events/iframe.d.ts +0 -1
  95. package/dist/events/iframe.d.ts.map +1 -1
  96. package/dist/events/logBlockEvent.d.ts.map +1 -1
  97. package/dist/index.d.ts +320 -277
  98. package/dist/index.d.ts.map +1 -1
  99. package/dist/index.js +3210 -1856
  100. package/dist/inject.d.ts +2 -3
  101. package/dist/inject.d.ts.map +1 -1
  102. package/dist/lowLevelInject.d.ts +1 -2
  103. package/dist/lowLevelInject.d.ts.map +1 -1
  104. package/dist/protocols/protocol-v2/settingsUnlockPolicy.d.ts.map +1 -1
  105. package/dist/protocols/protocol-v2/unlockPolicy.d.ts +4 -0
  106. package/dist/protocols/protocol-v2/unlockPolicy.d.ts.map +1 -0
  107. package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +1 -1
  108. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  109. package/dist/topLevelInject.d.ts.map +1 -1
  110. package/dist/types/api/checkAllFirmwareRelease.d.ts +3 -0
  111. package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
  112. package/dist/types/api/deviceSettings.d.ts +2 -2
  113. package/dist/types/api/deviceSettings.d.ts.map +1 -1
  114. package/dist/types/api/deviceUpdateBootloader.d.ts +6 -0
  115. package/dist/types/api/deviceUpdateBootloader.d.ts.map +1 -1
  116. package/dist/types/api/export.d.ts +4 -1
  117. package/dist/types/api/export.d.ts.map +1 -1
  118. package/dist/types/api/firmwareUpdate.d.ts +69 -0
  119. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  120. package/dist/types/api/firmwareUpdateCapabilities.d.ts +9 -0
  121. package/dist/types/api/firmwareUpdateCapabilities.d.ts.map +1 -0
  122. package/dist/types/api/firmwareUpdatePlan.d.ts +27 -0
  123. package/dist/types/api/firmwareUpdatePlan.d.ts.map +1 -0
  124. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts +42 -0
  125. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts.map +1 -0
  126. package/dist/types/api/index.d.ts +10 -23
  127. package/dist/types/api/index.d.ts.map +1 -1
  128. package/dist/types/api/openWalletSession.d.ts +1 -0
  129. package/dist/types/api/openWalletSession.d.ts.map +1 -1
  130. package/dist/types/api/protocolV2.d.ts +3 -84
  131. package/dist/types/api/protocolV2.d.ts.map +1 -1
  132. package/dist/types/api/sessionCache.d.ts +4 -1
  133. package/dist/types/api/sessionCache.d.ts.map +1 -1
  134. package/dist/types/device.d.ts +4 -4
  135. package/dist/types/device.d.ts.map +1 -1
  136. package/dist/types/settings.d.ts +13 -0
  137. package/dist/types/settings.d.ts.map +1 -1
  138. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  139. package/dist/utils/deviceSettings.d.ts +36 -7
  140. package/dist/utils/deviceSettings.d.ts.map +1 -1
  141. package/dist/utils/index.d.ts +2 -1
  142. package/dist/utils/index.d.ts.map +1 -1
  143. package/dist/utils/patch.d.ts +1 -1
  144. package/dist/utils/patch.d.ts.map +1 -1
  145. package/package.json +4 -4
  146. package/src/api/BaseMethod.ts +11 -12
  147. package/src/api/CheckAllFirmwareRelease.ts +16 -3
  148. package/src/api/ClearSessionCache.ts +4 -0
  149. package/src/api/FirmwareUpdate.ts +8 -1
  150. package/src/api/FirmwareUpdateV2.ts +284 -119
  151. package/src/api/FirmwareUpdateV3.ts +248 -55
  152. package/src/api/FirmwareUpdateV4.ts +816 -328
  153. package/src/api/OpenWalletSession.ts +74 -25
  154. package/src/api/UploadPortfolio.ts +3 -13
  155. package/src/api/conflux/ConfluxGetAddress.ts +2 -0
  156. package/src/api/conflux/ConfluxSignMessage.ts +2 -0
  157. package/src/api/conflux/ConfluxSignMessageCIP23.ts +2 -0
  158. package/src/api/conflux/ConfluxSignTransaction.ts +2 -0
  159. package/src/api/device/DeviceChangePin.ts +1 -1
  160. package/src/api/device/DeviceSettings.ts +66 -5
  161. package/src/api/device/DeviceUpdateBootloader.ts +114 -6
  162. package/src/api/device/DeviceWipe.ts +1 -1
  163. package/src/api/firmware/FirmwareArtifactSource.ts +278 -0
  164. package/src/api/firmware/FirmwareHostBinding.ts +100 -0
  165. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +50 -0
  166. package/src/api/firmware/FirmwareUpdateCapabilities.ts +9 -0
  167. package/src/api/firmware/FirmwareUpdatePlan.ts +621 -0
  168. package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +378 -0
  169. package/src/api/firmware/updateBootloader.ts +19 -4
  170. package/src/api/firmware/uploadFirmware.ts +140 -22
  171. package/src/api/helpers/protocolV2FileWrite.ts +6 -0
  172. package/src/api/index.ts +0 -21
  173. package/src/api/nexa/NexaSignTransaction.ts +2 -0
  174. package/src/api/protocol-v2/DeviceUploadWallpaper.ts +1 -1
  175. package/src/core/RequestQueue.ts +4 -123
  176. package/src/core/index.ts +25 -31
  177. package/src/data/messages/messages-protocol-v2.json +77 -4
  178. package/src/data-manager/DataManager.ts +33 -33
  179. package/src/data-manager/connectSettings.ts +11 -0
  180. package/src/device/Device.ts +51 -61
  181. package/src/device/DeviceCommands.ts +58 -14
  182. package/src/device/DeviceFeaturesState.ts +0 -9
  183. package/src/device/DeviceStateMapper.ts +26 -8
  184. package/src/device/DeviceStateProjector.ts +7 -1
  185. package/src/deviceProfile/buildDeviceFeatures.ts +2 -1
  186. package/src/events/call.ts +0 -6
  187. package/src/events/core.ts +0 -2
  188. package/src/events/iframe.ts +0 -1
  189. package/src/events/logBlockEvent.ts +1 -0
  190. package/src/index.ts +5 -4
  191. package/src/inject.ts +22 -35
  192. package/src/lowLevelInject.ts +5 -4
  193. package/src/protocols/protocol-v2/deviceSession.ts +1 -1
  194. package/src/protocols/protocol-v2/settingsUnlockPolicy.ts +2 -2
  195. package/src/protocols/protocol-v2/unlockPolicy.ts +105 -0
  196. package/src/protocols/protocol-v2/unlockRetry.ts +1 -3
  197. package/src/protocols/protocol-v2/walletSession.ts +68 -12
  198. package/src/topLevelInject.ts +5 -3
  199. package/src/types/api/checkAllFirmwareRelease.ts +3 -0
  200. package/src/types/api/deviceSettings.ts +17 -2
  201. package/src/types/api/deviceUpdateBootloader.ts +6 -0
  202. package/src/types/api/export.ts +18 -0
  203. package/src/types/api/firmwareUpdate.ts +76 -0
  204. package/src/types/api/firmwareUpdateCapabilities.ts +9 -0
  205. package/src/types/api/firmwareUpdatePlan.ts +36 -0
  206. package/src/types/api/firmwareUpdatePreparedPlan.ts +53 -0
  207. package/src/types/api/index.ts +14 -41
  208. package/src/types/api/openWalletSession.ts +2 -0
  209. package/src/types/api/protocolV2.ts +3 -187
  210. package/src/types/api/sessionCache.ts +9 -4
  211. package/src/types/device.ts +5 -3
  212. package/src/types/settings.ts +13 -0
  213. package/src/utils/deviceFeaturesUtils.ts +5 -1
  214. package/src/utils/deviceSettings.ts +130 -56
  215. package/src/utils/index.ts +18 -1
  216. package/__tests__/RequestQueue.test.ts +0 -140
  217. package/__tests__/UploadPortfolio.test.ts +0 -46
  218. package/dist/api/protocol-v2/DeviceSettingsGet.d.ts +0 -6
  219. package/dist/api/protocol-v2/DeviceSettingsGet.d.ts.map +0 -1
  220. package/dist/api/protocol-v2/DeviceSettingsPageShow.d.ts +0 -16
  221. package/dist/api/protocol-v2/DeviceSettingsPageShow.d.ts.map +0 -1
  222. package/dist/api/protocol-v2/DeviceSettingsSet.d.ts +0 -12
  223. package/dist/api/protocol-v2/DeviceSettingsSet.d.ts.map +0 -1
  224. package/src/api/protocol-v2/DeviceSettingsGet.ts +0 -18
  225. package/src/api/protocol-v2/DeviceSettingsPageShow.ts +0 -70
  226. package/src/api/protocol-v2/DeviceSettingsSet.ts +0 -44
@@ -0,0 +1,105 @@
1
+ export type UnlockPolicy = 'none' | 'unlock-before-run' | 'retry-on-locked';
2
+
3
+ /**
4
+ * Protocol V2 methods that are safe to replay after a locked response.
5
+ *
6
+ * Keep this as an exact allowlist. Name-pattern matching can silently make a new
7
+ * state-changing method replayable when it happens to share a suffix.
8
+ */
9
+ export const PROTOCOL_V2_RETRY_ON_LOCKED_METHODS = [
10
+ 'cipherKeyValue',
11
+ 'allNetworkGetAddress',
12
+ 'allNetworkGetAddressByLoop',
13
+ 'btcGetAddress',
14
+ 'btcGetPublicKey',
15
+ 'btcSignMessage',
16
+ 'btcSignPsbt',
17
+ 'btcSignTransaction',
18
+ 'btcVerifyMessage',
19
+ 'confluxGetAddress',
20
+ 'confluxSignMessage',
21
+ 'confluxSignTransaction',
22
+ 'evmGetAddress',
23
+ 'evmGetPublicKey',
24
+ 'evmSignMessage',
25
+ 'evmSignTransaction',
26
+ 'evmSignTypedData',
27
+ 'evmVerifyMessage',
28
+ 'starcoinGetAddress',
29
+ 'starcoinGetPublicKey',
30
+ 'starcoinSignMessage',
31
+ 'starcoinSignTransaction',
32
+ 'starcoinVerifyMessage',
33
+ 'nemGetAddress',
34
+ 'nemSignTransaction',
35
+ 'solGetAddress',
36
+ 'solSignTransaction',
37
+ 'solSignOffchainMessage',
38
+ 'solSignMessage',
39
+ 'stellarGetAddress',
40
+ 'stellarSignTransaction',
41
+ 'tronGetAddress',
42
+ 'tronSignMessage',
43
+ 'tronSignTransaction',
44
+ 'nearGetAddress',
45
+ 'nearSignTransaction',
46
+ 'aptosGetAddress',
47
+ 'aptosGetPublicKey',
48
+ 'aptosSignTransaction',
49
+ 'aptosSignMessage',
50
+ 'aptosSignInMessage',
51
+ 'algoGetAddress',
52
+ 'algoSignTransaction',
53
+ 'cosmosGetAddress',
54
+ 'cosmosGetPublicKey',
55
+ 'cosmosSignTransaction',
56
+ 'xrpGetAddress',
57
+ 'xrpSignTransaction',
58
+ 'suiGetAddress',
59
+ 'suiGetPublicKey',
60
+ 'suiSignMessage',
61
+ 'suiSignTransaction',
62
+ 'cardanoGetAddress',
63
+ 'cardanoGetPublicKey',
64
+ 'cardanoSignTransaction',
65
+ 'cardanoSignMessage',
66
+ 'filecoinGetAddress',
67
+ 'filecoinSignTransaction',
68
+ 'polkadotGetAddress',
69
+ 'polkadotSignTransaction',
70
+ 'kaspaGetAddress',
71
+ 'kaspaSignTransaction',
72
+ 'nexaGetAddress',
73
+ 'nexaSignTransaction',
74
+ 'nostrGetPublicKey',
75
+ 'nostrSignEvent',
76
+ 'nostrEncryptMessage',
77
+ 'nostrDecryptMessage',
78
+ 'nostrSignSchnorr',
79
+ 'lnurlAuth',
80
+ 'nervosGetAddress',
81
+ 'nervosSignTransaction',
82
+ 'dnxGetAddress',
83
+ 'dnxSignTransaction',
84
+ 'tonGetAddress',
85
+ 'tonSignMessage',
86
+ 'tonSignProof',
87
+ 'tonSignData',
88
+ 'scdoGetAddress',
89
+ 'scdoSignTransaction',
90
+ 'scdoSignMessage',
91
+ 'alephiumGetAddress',
92
+ 'alephiumSignTransaction',
93
+ 'alephiumSignMessage',
94
+ 'benfenGetAddress',
95
+ 'benfenGetPublicKey',
96
+ 'benfenSignMessage',
97
+ 'benfenSignTransaction',
98
+ 'neoGetAddress',
99
+ 'neoSignTransaction',
100
+ ] as const;
101
+
102
+ const retryOnLockedMethods = new Set<string>(PROTOCOL_V2_RETRY_ON_LOCKED_METHODS);
103
+
104
+ export const getProtocolV2UnlockPolicy = (methodName: string): UnlockPolicy =>
105
+ retryOnLockedMethods.has(methodName) ? 'retry-on-locked' : 'none';
@@ -25,9 +25,7 @@ export async function runMethodWithUnlockRetry(
25
25
  ) {
26
26
  const shouldEmitUi = isProtocolV2UiEnabled(method);
27
27
  const shouldUnlockBeforeRun =
28
- device.isProtocolV2() &&
29
- method.unlockPolicy === 'retry-on-locked' &&
30
- device.features?.unlocked === false;
28
+ device.isProtocolV2() && method.unlockPolicy !== 'none' && device.features?.unlocked === false;
31
29
 
32
30
  if (shouldUnlockBeforeRun) {
33
31
  if (shouldEmitUi) {
@@ -1,9 +1,10 @@
1
1
  import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
2
2
 
3
+ import { DEVICE } from '../../events';
3
4
  import { assertCompleteDeviceSession } from './deviceSession';
4
5
  import { isDeviceLockedError } from './lockedError';
5
6
 
6
- import type { DeviceSessionGet } from '@onekeyfe/hd-transport';
7
+ import type { DeviceSessionOpen } from '@onekeyfe/hd-transport';
7
8
  import type { Device } from '../../device/Device';
8
9
 
9
10
  export async function requestProtocolV2DeviceStatus(device: Device) {
@@ -16,16 +17,68 @@ export async function refreshProtocolV2DeviceStatus(device: Device) {
16
17
  return device.updateProtocolV2Status(status);
17
18
  }
18
19
 
19
- const getDeviceSession = async (device: Device, request: DeviceSessionGet) => {
20
+ const negotiateEventlessWalletSession = async (device: Device) => {
21
+ await device.commands.typedCall('ProtocolInfoRequest', 'ProtocolInfo', {
22
+ eventless_wallet_session: true,
23
+ });
24
+ };
25
+
26
+ const openDeviceSession = async (device: Device, request: DeviceSessionOpen) => {
20
27
  try {
21
- return await device.commands.typedCall('DeviceSessionGet', 'DeviceSession', request);
28
+ return await device.commands.typedCall('DeviceSessionOpen', 'DeviceSession', request);
22
29
  } catch (error) {
23
30
  if (!isDeviceLockedError(error)) {
24
31
  throw error;
25
32
  }
26
33
  await device.unlockDevice();
27
- return device.commands.typedCall('DeviceSessionGet', 'DeviceSession', request);
34
+ return device.commands.typedCall('DeviceSessionOpen', 'DeviceSession', request);
35
+ }
36
+ };
37
+
38
+ const selectDeviceSession = async (device: Device) => {
39
+ const existsAttachPinUser = device.features?.attachToPinEnabled === true;
40
+ const metadata = {
41
+ source: 'wallet-session-coordinator' as const,
42
+ reason: 'open-wallet' as const,
43
+ };
44
+ const response = await device.commands.promptPassphrase(
45
+ { existsAttachPinUser, ...metadata },
46
+ { cancelDeviceOnReject: false }
47
+ );
48
+ const hostPassphrase =
49
+ typeof response.passphrase === 'string' ? response.passphrase.normalize('NFKD') : undefined;
50
+ const selections = [
51
+ !!hostPassphrase,
52
+ response.passphraseOnDevice === true,
53
+ response.attachPinOnDevice === true,
54
+ ].filter(Boolean).length;
55
+
56
+ if (selections !== 1) {
57
+ throw ERRORS.TypedError(
58
+ HardwareErrorCode.RuntimeError,
59
+ 'Wallet selection must contain exactly one passphrase access mode.'
60
+ );
28
61
  }
62
+
63
+ if (response.attachPinOnDevice) {
64
+ if (!existsAttachPinUser) {
65
+ throw ERRORS.TypedError(
66
+ HardwareErrorCode.RuntimeError,
67
+ 'Attach PIN wallet selection is unavailable on this device.'
68
+ );
69
+ }
70
+ device.emit(DEVICE.ATTACH_PIN_ON_DEVICE, device, metadata);
71
+ return openDeviceSession(device, { select: { attach_pin_on_device: {} } });
72
+ }
73
+
74
+ if (response.passphraseOnDevice) {
75
+ device.emit(DEVICE.PASSPHRASE_ON_DEVICE, device, metadata);
76
+ return openDeviceSession(device, { select: { passphrase_on_device: {} } });
77
+ }
78
+
79
+ return openDeviceSession(device, {
80
+ select: { host_passphrase: { passphrase: hostPassphrase as string } },
81
+ });
29
82
  };
30
83
 
31
84
  export async function getProtocolV2WalletSession(
@@ -40,16 +93,19 @@ export async function getProtocolV2WalletSession(
40
93
  device.clearInternalState();
41
94
  }
42
95
 
43
- const expectedPassphraseState = options?.expectedPassphraseState ?? device.passphraseState;
96
+ await negotiateEventlessWalletSession(device);
97
+
98
+ if (options?.onlyMainPin) {
99
+ device.passphraseState = undefined;
100
+ }
101
+ const expectedPassphraseState = options?.onlyMainPin
102
+ ? undefined
103
+ : options?.expectedPassphraseState ?? device.passphraseState;
44
104
 
45
- // Unlock with PIN first. DeviceSessionGet operates on the wallet selected by
46
- // the device unlock flow; Protocol V2 does not accept a host passphrase here.
47
105
  if (device.features?.unlocked === false) {
48
106
  await device.unlockDevice();
49
107
  }
50
108
 
51
- // A standard wallet needs no DeviceSession. After unlock confirms passphrase is
52
- // disabled, do not create or forward a hidden-wallet passphraseState.
53
109
  if (options?.onlyMainPin || device.getCurrentPassphraseProtection() === false) {
54
110
  return {
55
111
  passphraseState: undefined,
@@ -66,7 +122,7 @@ export async function getProtocolV2WalletSession(
66
122
 
67
123
  if (cachedSessionId && expectedPassphraseState) {
68
124
  try {
69
- response = await getDeviceSession(device, { session_id: cachedSessionId });
125
+ response = await openDeviceSession(device, { resume: { session_id: cachedSessionId } });
70
126
  resumed = true;
71
127
  } catch (error) {
72
128
  device.clearInternalState();
@@ -75,7 +131,7 @@ export async function getProtocolV2WalletSession(
75
131
  }
76
132
 
77
133
  if (!response) {
78
- response = await getDeviceSession(device, {});
134
+ response = await selectDeviceSession(device);
79
135
  }
80
136
 
81
137
  const { message } = response;
@@ -95,7 +151,7 @@ export async function getProtocolV2WalletSession(
95
151
  message.btc_test_address,
96
152
  device.getCurrentDeviceId(),
97
153
  message.session_id,
98
- options?.initSession ? null : device.features?.sessionId ?? null
154
+ options?.initSession ? null : cachedSessionId ?? null
99
155
  );
100
156
 
101
157
  return {
@@ -1,6 +1,7 @@
1
1
  import EventEmitter from 'events';
2
2
 
3
3
  import { createCoreApi } from './inject';
4
+ import { unregisterFirmwareUpdateHostBinding } from './api/firmware/FirmwareHostBinding';
4
5
 
5
6
  import type { ConnectSettings } from './types/settings';
6
7
  import type { CoreApi } from './types/api';
@@ -46,14 +47,15 @@ export const topLevelInject = () => {
46
47
  lowLevelApi?.removeAllListeners(type);
47
48
  },
48
49
 
49
- dispose: () => lowLevelApi?.dispose(),
50
+ dispose: async () => {
51
+ unregisterFirmwareUpdateHostBinding();
52
+ await lowLevelApi?.dispose();
53
+ },
50
54
 
51
55
  uiResponse: response => lowLevelApi?.uiResponse(response),
52
56
 
53
57
  cancel: (connectId?: string) => lowLevelApi?.cancel(connectId),
54
58
 
55
- cancelOperation: (operationId: string) => lowLevelApi?.cancelOperation(operationId),
56
-
57
59
  updateSettings: settings => lowLevelApi?.updateSettings(settings) ?? Promise.resolve(false),
58
60
 
59
61
  switchTransport: (env: ConnectSettings['env']) =>
@@ -2,6 +2,7 @@ import type { IBLEFirmwareReleaseInfo } from '../settings';
2
2
  import type { EFirmwareType } from '@onekeyfe/hd-shared';
3
3
  import type { CommonParams, Response } from '../params';
4
4
  import type { Features, IDeviceBLEFirmwareStatus, IDeviceFirmwareStatus } from '../device';
5
+ import type { FirmwareUpdatePlan } from './firmwareUpdatePlan';
5
6
 
6
7
  export type FirmwareRelease = {
7
8
  shouldUpdate?: boolean;
@@ -20,11 +21,13 @@ export type AllFirmwareRelease = {
20
21
  bootloader?: FirmwareRelease;
21
22
  bridge?: FirmwareRelease;
22
23
  features?: Features;
24
+ firmwareUpdatePlan?: FirmwareUpdatePlan;
23
25
  };
24
26
 
25
27
  export type CheckAllFirmwareReleaseParams = {
26
28
  checkBridgeRelease?: boolean;
27
29
  firmwareType?: EFirmwareType;
30
+ platform?: 'native' | 'desktop' | 'ext' | 'web' | 'web-embed';
28
31
  };
29
32
 
30
33
  export declare function checkAllFirmwareRelease(
@@ -1,28 +1,43 @@
1
- import type { SafetyCheckLevel, Success } from '@onekeyfe/hd-transport';
1
+ import type { Enum_SafetyCheckLevel, Success } from '@onekeyfe/hd-transport';
2
2
  import type { CommonParams, Response } from '../params';
3
3
 
4
4
  export type DeviceSettingsParams = {
5
5
  language?: string;
6
6
  label?: string;
7
7
  usePassphrase?: boolean;
8
+ /** Protocol V1 only. */
8
9
  homescreen?: string;
10
+ /** Protocol V1 only. */
9
11
  passphraseSource?: number;
10
12
  autoLockDelayMs?: number;
13
+ /** Protocol V1 only. */
11
14
  displayRotation?: number;
15
+ /** Protocol V1 only. */
12
16
  passphraseAlwaysOnDevice?: boolean;
13
- safetyChecks?: SafetyCheckLevel;
17
+ /** Protocol V1 only. Uses Enum_SafetyCheckLevel numeric values. */
18
+ safetyChecks?: Enum_SafetyCheckLevel;
19
+ /** Protocol V1 only; the current Protocol V2 schema has no matching field. */
14
20
  experimentalFeatures?: boolean;
15
21
  autoShutdownDelayMs?: number;
22
+ /** Protocol V1 only; opens the legacy device-side brightness flow. */
16
23
  changeBrightness?: boolean;
24
+ /** Protocol V2 only. */
17
25
  brightness?: number;
18
26
  hapticFeedback?: boolean;
19
27
  bluetoothEnabled?: boolean;
28
+ /** Protocol V2 only; changed through an on-device confirmation page. */
20
29
  airgapMode?: boolean;
30
+ /** Protocol V2 only. */
21
31
  animationEnabled?: boolean;
32
+ /** Protocol V2 only. */
22
33
  tapToWake?: boolean;
34
+ /** Protocol V2 only. */
23
35
  deviceNameDisplayEnabled?: boolean;
36
+ /** Protocol V2 only. */
24
37
  fidoEnabled?: boolean;
38
+ /** Protocol V2 only. */
25
39
  usbLockEnabled?: boolean;
40
+ /** Protocol V2 only. */
26
41
  randomKeypad?: boolean;
27
42
  };
28
43
 
@@ -1,9 +1,15 @@
1
1
  import type { Success } from '@onekeyfe/hd-transport';
2
2
  import type { EFirmwareType } from '@onekeyfe/hd-shared';
3
+ import type { FirmwareArtifactReader, FirmwareArtifactReference } from './firmwareUpdate';
4
+ import type { FirmwareUpdatePreparedPlan } from './firmwareUpdatePreparedPlan';
3
5
  import type { Response } from '../params';
4
6
 
5
7
  export type DeviceUpdateBootloaderParams = {
8
+ preparedPlan?: FirmwareUpdatePreparedPlan;
9
+ hostBindingGeneration?: number;
6
10
  binary?: ArrayBuffer;
11
+ artifact?: FirmwareArtifactReference;
12
+ artifactReader?: FirmwareArtifactReader;
7
13
  firmwareType?: EFirmwareType;
8
14
  };
9
15
 
@@ -25,11 +25,29 @@ export type {
25
25
  DeviceUploadResourceResponse,
26
26
  } from './deviceUploadResource';
27
27
  export type {
28
+ FirmwareArtifactReader,
29
+ FirmwareArtifactReference,
30
+ FirmwareUpdateHostBinding,
31
+ FirmwareUpdateArtifactParams,
28
32
  FirmwareUpdateParams,
29
33
  FirmwareUpdateBinaryParams,
30
34
  FirmwareUpdateV3Params,
31
35
  FirmwareUpdateV4Params,
36
+ FirmwareUpdateV4Target,
32
37
  } from './firmwareUpdate';
38
+ export type {
39
+ FirmwareUpdatePlan,
40
+ FirmwareUpdatePlanArtifact,
41
+ FirmwareUpdatePlanArtifactRole,
42
+ FirmwareUpdatePlanTarget,
43
+ } from './firmwareUpdatePlan';
44
+ export type {
45
+ FirmwareUpdatePreparedArtifact,
46
+ FirmwareUpdatePreparedArtifactInput,
47
+ FirmwareUpdatePreparedEntry,
48
+ FirmwareUpdatePreparedPlan,
49
+ } from './firmwareUpdatePreparedPlan';
50
+ export type { FirmwareUpdateCapabilities } from './firmwareUpdateCapabilities';
33
51
  export type { AllFirmwareRelease } from './checkAllFirmwareRelease';
34
52
 
35
53
  export type { AllNetworkAddressParams, AllNetworkGetAddressParams } from './allNetworkGetAddress';
@@ -1,14 +1,53 @@
1
1
  import type { EFirmwareType } from '@onekeyfe/hd-shared';
2
2
  import type { PROTO } from '../../constants';
3
3
  import type { Params, Response } from '../params';
4
+ import type { FirmwareUpdatePreparedPlan } from './firmwareUpdatePreparedPlan';
4
5
 
5
6
  type IUpdateType = 'firmware' | 'ble';
6
7
 
8
+ export interface FirmwareArtifactReference {
9
+ artifactRef: string;
10
+ size: number;
11
+ sha256: string;
12
+ }
13
+
14
+ export interface FirmwareArtifactReader {
15
+ open(input: { artifactRef: string }): Promise<{
16
+ readerId: string;
17
+ size: number;
18
+ }>;
19
+ read(input: { readerId: string; offset: number; length: number }): Promise<{
20
+ data: ArrayBuffer;
21
+ bytesRead: number;
22
+ eof: boolean;
23
+ }>;
24
+ close(input: { readerId: string }): Promise<void>;
25
+ }
26
+
27
+ export interface FirmwareUpdateHostBinding {
28
+ artifactReader: FirmwareArtifactReader;
29
+ }
30
+
7
31
  export interface FirmwareUpdateBinaryParams {
8
32
  binary: ArrayBuffer;
9
33
  updateType: IUpdateType;
10
34
  }
11
35
 
36
+ export interface FirmwareUpdateArtifactParams {
37
+ preparedPlan: FirmwareUpdatePreparedPlan;
38
+ hostBindingGeneration: number;
39
+ artifact: FirmwareArtifactReference;
40
+ resourceArtifact?: FirmwareArtifactReference;
41
+ resourceEntries?: Array<{
42
+ entryName: string;
43
+ artifact: FirmwareArtifactReference;
44
+ }>;
45
+ updateType: IUpdateType;
46
+ forcedUpdateRes?: boolean;
47
+ isUpdateBootloader?: boolean;
48
+ firmwareType?: EFirmwareType;
49
+ }
50
+
12
51
  export interface FirmwareUpdateParams {
13
52
  version?: number[];
14
53
  updateType: IUpdateType;
@@ -37,8 +76,14 @@ export declare function firmwareUpdateV2(
37
76
  connectId: string | undefined,
38
77
  params: Params<FirmwareUpdateBinaryParams & Platform>
39
78
  ): Response<PROTO.Success>;
79
+ export declare function firmwareUpdateV2(
80
+ connectId: string | undefined,
81
+ params: Params<FirmwareUpdateArtifactParams & Platform>
82
+ ): Response<PROTO.Success>;
40
83
 
41
84
  export interface FirmwareUpdateV3Params {
85
+ preparedPlan?: FirmwareUpdatePreparedPlan;
86
+ hostBindingGeneration?: number;
42
87
  bleVersion?: number[];
43
88
  bleBinary?: ArrayBuffer;
44
89
  chunkSize?: number;
@@ -55,6 +100,17 @@ export interface FirmwareUpdateV3Params {
55
100
  firmwareType?: EFirmwareType;
56
101
 
57
102
  platform: IPlatform;
103
+
104
+ artifactReader?: FirmwareArtifactReader;
105
+ artifacts?: {
106
+ ble?: FirmwareArtifactReference;
107
+ firmware?: FirmwareArtifactReference;
108
+ bootloader?: FirmwareArtifactReference;
109
+ resourceEntries?: Array<{
110
+ entryName: string;
111
+ artifact: FirmwareArtifactReference;
112
+ }>;
113
+ };
58
114
  }
59
115
 
60
116
  /**
@@ -73,10 +129,14 @@ export type FirmwareUpdateV4Target =
73
129
  | 'se04';
74
130
 
75
131
  export interface FirmwareUpdateV4Params {
132
+ preparedPlan?: FirmwareUpdatePreparedPlan;
133
+ hostBindingGeneration?: number;
76
134
  platform: IPlatform;
135
+ expectedDeviceId?: string;
77
136
  chunkSize?: number;
78
137
  firmwareType?: EFirmwareType;
79
138
  targetsToUpdate?: FirmwareUpdateV4Target[];
139
+ expectedTargetVersions?: Partial<Record<FirmwareUpdateV4Target, string>>;
80
140
 
81
141
  /** FW_MGMT_TARGET_ROMLOADER = 2; Pro2 cannot install it through firmwareUpdateV4. */
82
142
  romloaderBinary?: ArrayBuffer;
@@ -102,8 +162,24 @@ export interface FirmwareUpdateV4Params {
102
162
  binary: ArrayBuffer;
103
163
  devicePath: string;
104
164
  }>;
165
+ artifactReader?: FirmwareArtifactReader;
166
+ componentArtifacts?: Partial<
167
+ Record<Exclude<FirmwareUpdateV4Target, 'resource'>, FirmwareArtifactReference>
168
+ >;
169
+ resourceBundleArtifacts?: Array<{
170
+ name: string;
171
+ artifact: FirmwareArtifactReference;
172
+ }>;
105
173
  }
106
174
 
175
+ export declare function registerFirmwareUpdateHostBinding(
176
+ binding: FirmwareUpdateHostBinding
177
+ ): number;
178
+
179
+ export declare function unregisterFirmwareUpdateHostBinding(generation?: number): boolean;
180
+
181
+ export declare function getFirmwareUpdateHostBindingGeneration(): number;
182
+
107
183
  export declare function firmwareUpdateV3(
108
184
  connectId: string | undefined,
109
185
  params: Params<FirmwareUpdateV3Params>
@@ -0,0 +1,9 @@
1
+ export interface FirmwareUpdateCapabilities {
2
+ planSchemaVersion: 2;
3
+ preparedPlanSchemaVersion: 2;
4
+ hostBindingProtocolVersion: 2;
5
+ manifestModes: Array<'external-only' | 'sdk-managed'>;
6
+ supportsArtifactReader: true;
7
+ }
8
+
9
+ export declare function getFirmwareUpdateCapabilities(): FirmwareUpdateCapabilities;
@@ -0,0 +1,36 @@
1
+ import type { FirmwareUpdateV4Target } from './firmwareUpdate';
2
+ import type { EFirmwareType } from '@onekeyfe/hd-shared';
3
+
4
+ export type FirmwareUpdatePlanArtifactRole =
5
+ | 'firmware'
6
+ | 'ble'
7
+ | 'bootloader'
8
+ | 'resource'
9
+ | 'component'
10
+ | 'resourceBundle';
11
+
12
+ export type FirmwareUpdatePlanTarget = 'firmware' | 'ble' | 'bootloader' | FirmwareUpdateV4Target;
13
+
14
+ export interface FirmwareUpdatePlanArtifact {
15
+ artifactId: string;
16
+ role: FirmwareUpdatePlanArtifactRole;
17
+ target: FirmwareUpdatePlanTarget;
18
+ url: string;
19
+ container: 'raw' | 'zip';
20
+ logicalName?: string;
21
+ expectedSize?: number;
22
+ expectedSha256?: string;
23
+ targetVersion?: string;
24
+ }
25
+
26
+ export interface FirmwareUpdatePlan {
27
+ schemaVersion: 2;
28
+ planDigest: string;
29
+ executor: 'v2' | 'v3' | 'v4';
30
+ deviceIdentity: string;
31
+ deviceModel: string;
32
+ firmwareType: EFirmwareType;
33
+ platform: 'native' | 'desktop' | 'ext' | 'web' | 'web-embed';
34
+ artifacts: FirmwareUpdatePlanArtifact[];
35
+ targetsToUpdate: FirmwareUpdatePlanTarget[];
36
+ }
@@ -0,0 +1,53 @@
1
+ import type { FirmwareArtifactReference } from './firmwareUpdate';
2
+ import type {
3
+ FirmwareUpdatePlan,
4
+ FirmwareUpdatePlanArtifactRole,
5
+ FirmwareUpdatePlanTarget,
6
+ } from './firmwareUpdatePlan';
7
+
8
+ export interface FirmwareUpdatePreparedEntry {
9
+ entryName: string;
10
+ artifact: FirmwareArtifactReference;
11
+ }
12
+
13
+ export interface FirmwareUpdatePreparedArtifactInput {
14
+ artifactId: string;
15
+ artifact: FirmwareArtifactReference;
16
+ materializedEntries?: FirmwareUpdatePreparedEntry[];
17
+ }
18
+
19
+ export interface FirmwareUpdatePreparedArtifact {
20
+ artifactId: string;
21
+ role: FirmwareUpdatePlanArtifactRole;
22
+ target: FirmwareUpdatePlanTarget;
23
+ container: 'raw' | 'zip';
24
+ logicalName?: string;
25
+ targetVersion?: string;
26
+ artifact: FirmwareArtifactReference;
27
+ materializedEntries?: FirmwareUpdatePreparedEntry[];
28
+ }
29
+
30
+ export interface FirmwareUpdatePreparedPlan {
31
+ schemaVersion: 2;
32
+ preparedPlanDigest: string;
33
+ planDigest: string;
34
+ networkPolicy: 'forbid';
35
+ executor: FirmwareUpdatePlan['executor'];
36
+ deviceIdentity: string;
37
+ deviceModel: string;
38
+ firmwareType: FirmwareUpdatePlan['firmwareType'];
39
+ platform: FirmwareUpdatePlan['platform'];
40
+ leaseRef: string;
41
+ artifacts: FirmwareUpdatePreparedArtifact[];
42
+ targetsToUpdate: FirmwareUpdatePlanTarget[];
43
+ }
44
+
45
+ export declare function prepareFirmwareUpdatePlan(input: {
46
+ plan: FirmwareUpdatePlan;
47
+ leaseRef: string;
48
+ artifacts: FirmwareUpdatePreparedArtifactInput[];
49
+ }): FirmwareUpdatePreparedPlan;
50
+
51
+ export declare function validateFirmwareUpdatePreparedPlan(
52
+ value: unknown
53
+ ): FirmwareUpdatePreparedPlan;