@onekeyfe/hd-core 1.2.0-alpha.21 → 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 (175) 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-settings.test.ts +1 -1
  5. package/__tests__/device-wallet-session-store.test.ts +111 -7
  6. package/__tests__/firmware-update/firmware-artifact-source.test.ts +119 -0
  7. package/__tests__/firmware-update/firmware-host-binding.test.ts +70 -0
  8. package/__tests__/firmware-update/firmware-update-capabilities.test.ts +13 -0
  9. package/__tests__/firmware-update/firmware-update-plan.test.ts +326 -0
  10. package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +200 -0
  11. package/__tests__/firmware-update/firmware-update-v2-download-before-boot.test.ts +39 -30
  12. package/__tests__/firmware-update/firmware-upload-source.test.ts +70 -0
  13. package/__tests__/open-wallet-session.test.ts +253 -13
  14. package/__tests__/pro2Wallpaper.test.ts +7 -18
  15. package/__tests__/protocol-v2-unlock-policy.test.ts +45 -0
  16. package/__tests__/protocol-v2.test.ts +415 -145
  17. package/__tests__/public-device-state-api.test.ts +69 -62
  18. package/__tests__/public-pro2-api-boundary.test.ts +8 -34
  19. package/dist/api/BaseMethod.d.ts +2 -1
  20. package/dist/api/BaseMethod.d.ts.map +1 -1
  21. package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
  22. package/dist/api/ClearSessionCache.d.ts.map +1 -1
  23. package/dist/api/FirmwareUpdate.d.ts.map +1 -1
  24. package/dist/api/FirmwareUpdateV2.d.ts +11 -0
  25. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  26. package/dist/api/FirmwareUpdateV3.d.ts +7 -3
  27. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  28. package/dist/api/FirmwareUpdateV4.d.ts +29 -10
  29. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  30. package/dist/api/OpenWalletSession.d.ts.map +1 -1
  31. package/dist/api/conflux/ConfluxGetAddress.d.ts +1 -0
  32. package/dist/api/conflux/ConfluxGetAddress.d.ts.map +1 -1
  33. package/dist/api/conflux/ConfluxSignMessage.d.ts +1 -0
  34. package/dist/api/conflux/ConfluxSignMessage.d.ts.map +1 -1
  35. package/dist/api/conflux/ConfluxSignMessageCIP23.d.ts +1 -0
  36. package/dist/api/conflux/ConfluxSignMessageCIP23.d.ts.map +1 -1
  37. package/dist/api/conflux/ConfluxSignTransaction.d.ts +1 -0
  38. package/dist/api/conflux/ConfluxSignTransaction.d.ts.map +1 -1
  39. package/dist/api/device/DeviceUnlock.d.ts +1 -1
  40. package/dist/api/device/DeviceUnlock.d.ts.map +1 -1
  41. package/dist/api/device/DeviceUpdateBootloader.d.ts +5 -1
  42. package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
  43. package/dist/api/firmware/FirmwareArtifactSource.d.ts +26 -0
  44. package/dist/api/firmware/FirmwareArtifactSource.d.ts.map +1 -0
  45. package/dist/api/firmware/FirmwareHostBinding.d.ts +6 -0
  46. package/dist/api/firmware/FirmwareHostBinding.d.ts.map +1 -0
  47. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +7 -0
  48. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  49. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts +3 -0
  50. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts.map +1 -0
  51. package/dist/api/firmware/FirmwareUpdatePlan.d.ts +24 -0
  52. package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +1 -0
  53. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts +25 -0
  54. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +1 -0
  55. package/dist/api/firmware/getBinary.d.ts +7 -0
  56. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  57. package/dist/api/firmware/updateBootloader.d.ts +2 -0
  58. package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
  59. package/dist/api/firmware/uploadFirmware.d.ts +9 -1
  60. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  61. package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -1
  62. package/dist/api/index.d.ts +0 -17
  63. package/dist/api/index.d.ts.map +1 -1
  64. package/dist/api/nexa/NexaSignTransaction.d.ts +1 -0
  65. package/dist/api/nexa/NexaSignTransaction.d.ts.map +1 -1
  66. package/dist/api/stellar/StellarSignTransaction.d.ts +1 -1
  67. package/dist/data-manager/DataManager.d.ts +1 -0
  68. package/dist/data-manager/DataManager.d.ts.map +1 -1
  69. package/dist/data-manager/connectSettings.d.ts.map +1 -1
  70. package/dist/device/Device.d.ts.map +1 -1
  71. package/dist/device/DeviceCommands.d.ts.map +1 -1
  72. package/dist/device/DeviceStateMapper.d.ts.map +1 -1
  73. package/dist/device/DeviceStateProjector.d.ts.map +1 -1
  74. package/dist/events/logBlockEvent.d.ts.map +1 -1
  75. package/dist/index.d.ts +265 -226
  76. package/dist/index.d.ts.map +1 -1
  77. package/dist/index.js +3045 -1550
  78. package/dist/inject.d.ts.map +1 -1
  79. package/dist/lowLevelInject.d.ts.map +1 -1
  80. package/dist/protocols/protocol-v2/settingsUnlockPolicy.d.ts.map +1 -1
  81. package/dist/protocols/protocol-v2/unlockPolicy.d.ts +4 -0
  82. package/dist/protocols/protocol-v2/unlockPolicy.d.ts.map +1 -0
  83. package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +1 -1
  84. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  85. package/dist/topLevelInject.d.ts.map +1 -1
  86. package/dist/types/api/checkAllFirmwareRelease.d.ts +3 -0
  87. package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
  88. package/dist/types/api/deviceUnlock.d.ts +2 -2
  89. package/dist/types/api/deviceUnlock.d.ts.map +1 -1
  90. package/dist/types/api/deviceUpdateBootloader.d.ts +6 -0
  91. package/dist/types/api/deviceUpdateBootloader.d.ts.map +1 -1
  92. package/dist/types/api/export.d.ts +4 -1
  93. package/dist/types/api/export.d.ts.map +1 -1
  94. package/dist/types/api/firmwareUpdate.d.ts +69 -0
  95. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  96. package/dist/types/api/firmwareUpdateCapabilities.d.ts +9 -0
  97. package/dist/types/api/firmwareUpdateCapabilities.d.ts.map +1 -0
  98. package/dist/types/api/firmwareUpdatePlan.d.ts +27 -0
  99. package/dist/types/api/firmwareUpdatePlan.d.ts.map +1 -0
  100. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts +42 -0
  101. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts.map +1 -0
  102. package/dist/types/api/index.d.ts +10 -19
  103. package/dist/types/api/index.d.ts.map +1 -1
  104. package/dist/types/api/openWalletSession.d.ts +1 -0
  105. package/dist/types/api/openWalletSession.d.ts.map +1 -1
  106. package/dist/types/api/protocolV2.d.ts +3 -76
  107. package/dist/types/api/protocolV2.d.ts.map +1 -1
  108. package/dist/types/api/sessionCache.d.ts +4 -1
  109. package/dist/types/api/sessionCache.d.ts.map +1 -1
  110. package/dist/types/settings.d.ts +13 -0
  111. package/dist/types/settings.d.ts.map +1 -1
  112. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  113. package/dist/utils/patch.d.ts +1 -1
  114. package/dist/utils/patch.d.ts.map +1 -1
  115. package/package.json +4 -4
  116. package/src/api/BaseMethod.ts +8 -3
  117. package/src/api/CheckAllFirmwareRelease.ts +16 -3
  118. package/src/api/ClearSessionCache.ts +4 -0
  119. package/src/api/FirmwareUpdate.ts +8 -1
  120. package/src/api/FirmwareUpdateV2.ts +284 -119
  121. package/src/api/FirmwareUpdateV3.ts +248 -55
  122. package/src/api/FirmwareUpdateV4.ts +816 -328
  123. package/src/api/OpenWalletSession.ts +74 -25
  124. package/src/api/UploadPortfolio.ts +1 -1
  125. package/src/api/conflux/ConfluxGetAddress.ts +2 -0
  126. package/src/api/conflux/ConfluxSignMessage.ts +2 -0
  127. package/src/api/conflux/ConfluxSignMessageCIP23.ts +2 -0
  128. package/src/api/conflux/ConfluxSignTransaction.ts +2 -0
  129. package/src/api/device/DeviceChangePin.ts +1 -1
  130. package/src/api/device/DeviceUnlock.ts +1 -2
  131. package/src/api/device/DeviceUpdateBootloader.ts +114 -6
  132. package/src/api/device/DeviceWipe.ts +1 -1
  133. package/src/api/firmware/FirmwareArtifactSource.ts +278 -0
  134. package/src/api/firmware/FirmwareHostBinding.ts +100 -0
  135. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +50 -0
  136. package/src/api/firmware/FirmwareUpdateCapabilities.ts +9 -0
  137. package/src/api/firmware/FirmwareUpdatePlan.ts +621 -0
  138. package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +378 -0
  139. package/src/api/firmware/updateBootloader.ts +19 -4
  140. package/src/api/firmware/uploadFirmware.ts +140 -22
  141. package/src/api/helpers/protocolV2FileWrite.ts +6 -0
  142. package/src/api/index.ts +0 -18
  143. package/src/api/nexa/NexaSignTransaction.ts +2 -0
  144. package/src/api/protocol-v2/DeviceUploadWallpaper.ts +1 -1
  145. package/src/data/messages/messages-protocol-v2.json +77 -4
  146. package/src/data-manager/DataManager.ts +33 -33
  147. package/src/data-manager/connectSettings.ts +11 -0
  148. package/src/device/Device.ts +50 -60
  149. package/src/device/DeviceCommands.ts +58 -14
  150. package/src/device/DeviceStateMapper.ts +18 -1
  151. package/src/device/DeviceStateProjector.ts +7 -1
  152. package/src/events/logBlockEvent.ts +1 -0
  153. package/src/index.ts +5 -0
  154. package/src/inject.ts +22 -25
  155. package/src/lowLevelInject.ts +5 -1
  156. package/src/protocols/protocol-v2/deviceSession.ts +1 -1
  157. package/src/protocols/protocol-v2/settingsUnlockPolicy.ts +2 -2
  158. package/src/protocols/protocol-v2/unlockPolicy.ts +105 -0
  159. package/src/protocols/protocol-v2/unlockRetry.ts +1 -3
  160. package/src/protocols/protocol-v2/walletSession.ts +68 -12
  161. package/src/topLevelInject.ts +5 -1
  162. package/src/types/api/checkAllFirmwareRelease.ts +3 -0
  163. package/src/types/api/deviceUnlock.ts +2 -5
  164. package/src/types/api/deviceUpdateBootloader.ts +6 -0
  165. package/src/types/api/export.ts +18 -0
  166. package/src/types/api/firmwareUpdate.ts +76 -0
  167. package/src/types/api/firmwareUpdateCapabilities.ts +9 -0
  168. package/src/types/api/firmwareUpdatePlan.ts +36 -0
  169. package/src/types/api/firmwareUpdatePreparedPlan.ts +53 -0
  170. package/src/types/api/index.ts +14 -34
  171. package/src/types/api/openWalletSession.ts +2 -0
  172. package/src/types/api/protocolV2.ts +3 -156
  173. package/src/types/api/sessionCache.ts +9 -4
  174. package/src/types/settings.ts +13 -0
  175. package/src/utils/deviceFeaturesUtils.ts +5 -1
package/src/inject.ts CHANGED
@@ -1,6 +1,17 @@
1
- import type { Unsuccessful } from './types';
1
+ import { getFirmwareUpdateCapabilities } from './api/firmware/FirmwareUpdateCapabilities';
2
+ import {
3
+ prepareFirmwareUpdatePlan,
4
+ validateFirmwareUpdatePreparedPlan,
5
+ } from './api/firmware/FirmwareUpdatePreparedPlan';
6
+ import {
7
+ getFirmwareUpdateHostBindingGeneration,
8
+ registerFirmwareUpdateHostBinding,
9
+ unregisterFirmwareUpdateHostBinding,
10
+ } from './api/firmware/FirmwareHostBinding';
11
+
2
12
  import type { EventEmitter } from 'events';
3
13
  import type { CallMethod } from './events';
14
+ import type { Unsuccessful } from './types';
4
15
  import type { CoreApi } from './types/api';
5
16
  import type { AllNetworkAddress } from './types/api/allNetworkGetAddress';
6
17
 
@@ -68,7 +79,10 @@ export const inject = ({
68
79
 
69
80
  call,
70
81
 
71
- dispose,
82
+ dispose: async () => {
83
+ unregisterFirmwareUpdateHostBinding();
84
+ await dispose();
85
+ },
72
86
 
73
87
  uiResponse,
74
88
 
@@ -99,6 +113,12 @@ export const createCoreApi = (
99
113
  | 'updateSettings'
100
114
  | 'switchTransport'
101
115
  > => ({
116
+ getFirmwareUpdateCapabilities,
117
+ registerFirmwareUpdateHostBinding,
118
+ unregisterFirmwareUpdateHostBinding,
119
+ getFirmwareUpdateHostBindingGeneration,
120
+ prepareFirmwareUpdatePlan,
121
+ validateFirmwareUpdatePreparedPlan,
102
122
  getLogs: () => call({ method: 'getLogs' }),
103
123
  clearSessionCache: params => call({ ...params, method: 'clearSessionCache' }),
104
124
  /**
@@ -160,30 +180,7 @@ export const createCoreApi = (
160
180
  call({ ...params, connectId, method: 'deviceGetOnboardingStatus' }),
161
181
  deviceUploadWallpaper: (connectId, params) =>
162
182
  call({ ...params, connectId, method: 'deviceUploadWallpaper' }),
163
- deviceInfoGet: (connectId, params) => call({ ...params, connectId, method: 'deviceInfoGet' }),
164
- deviceStatusGet: (connectId, params) => call({ ...params, connectId, method: 'deviceStatusGet' }),
165
- protocolInfoRequest: (connectId, params) =>
166
- call({ ...params, connectId, method: 'protocolInfoRequest' }),
167
- ping: (connectId, params) => call({ ...params, connectId, method: 'ping' }),
168
- deviceFirmwareUpdate: (connectId, params) =>
169
- call({ ...params, connectId, method: 'deviceFirmwareUpdate' }),
170
- deviceGetFirmwareUpdateStatus: (connectId, params) =>
171
- call({ ...params, connectId, method: 'deviceGetFirmwareUpdateStatus' }),
172
- deviceFactoryInfoSet: (connectId, params) =>
173
- call({ ...params, connectId, method: 'deviceFactoryInfoSet' }),
174
- deviceFactoryInfoGet: (connectId, params) =>
175
- call({ ...params, connectId, method: 'deviceFactoryInfoGet' }),
176
- filesystemPermissionFix: (connectId, params) =>
177
- call({ ...params, connectId, method: 'filesystemPermissionFix' }),
178
- filesystemFormat: connectId => call({ connectId, method: 'filesystemFormat' }),
179
183
  uploadPortfolio: (connectId, params) => call({ ...params, connectId, method: 'uploadPortfolio' }),
180
- fileRead: (connectId, params) => call({ ...params, connectId, method: 'fileRead' }),
181
- fileWrite: (connectId, params) => call({ ...params, connectId, method: 'fileWrite' }),
182
- fileDelete: (connectId, params) => call({ ...params, connectId, method: 'fileDelete' }),
183
- dirList: (connectId, params) => call({ ...params, connectId, method: 'dirList' }),
184
- dirMake: (connectId, params) => call({ ...params, connectId, method: 'dirMake' }),
185
- dirRemove: (connectId, params) => call({ ...params, connectId, method: 'dirRemove' }),
186
- pathInfo: (connectId, params) => call({ ...params, connectId, method: 'pathInfo' }),
187
184
  deviceRecovery: (connectId, params) => call({ ...params, connectId, method: 'deviceRecovery' }),
188
185
  deviceReset: (connectId, params) => call({ ...params, connectId, method: 'deviceReset' }),
189
186
  deviceSettings: (connectId, params) => call({ ...params, connectId, method: 'deviceSettings' }),
@@ -1,4 +1,5 @@
1
1
  import { createCoreApi } from './inject';
2
+ import { unregisterFirmwareUpdateHostBinding } from './api/firmware/FirmwareHostBinding';
2
3
 
3
4
  import type { EventEmitter } from 'events';
4
5
  import type { CallMethod, CoreMessage } from './events';
@@ -43,7 +44,10 @@ export const lowLevelInject = ({
43
44
 
44
45
  call,
45
46
 
46
- dispose,
47
+ dispose: async () => {
48
+ unregisterFirmwareUpdateHostBinding();
49
+ await dispose();
50
+ },
47
51
 
48
52
  uiResponse,
49
53
 
@@ -18,7 +18,7 @@ export function assertCompleteDeviceSession(
18
18
  ) {
19
19
  throw ERRORS.TypedError(
20
20
  HardwareErrorCode.RuntimeError,
21
- 'DeviceSessionGet returned an incomplete DeviceSession response.'
21
+ 'DeviceSessionOpen returned an incomplete DeviceSession response.'
22
22
  );
23
23
  }
24
24
  }
@@ -12,7 +12,7 @@ const LOCK_FREE_DEVICE_SETTINGS = new Set<keyof DeviceSettings>([
12
12
  export const getProtocolV2SettingsUnlockPolicy = (settings: DeviceSettings): UnlockPolicy =>
13
13
  Object.keys(settings).every(key => LOCK_FREE_DEVICE_SETTINGS.has(key as keyof DeviceSettings))
14
14
  ? 'none'
15
- : 'retry-on-locked';
15
+ : 'unlock-before-run';
16
16
 
17
17
  export type ProtocolV2SettingsOperation =
18
18
  | {
@@ -32,7 +32,7 @@ export const getProtocolV2SettingsBehavior = (
32
32
  } => {
33
33
  if (operation.kind === 'page') {
34
34
  return {
35
- unlockPolicy: 'retry-on-locked',
35
+ unlockPolicy: 'unlock-before-run',
36
36
  uiInteraction: {
37
37
  request: 'button',
38
38
  source: 'method-lifecycle',
@@ -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,7 +47,10 @@ 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
 
@@ -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,7 +1,4 @@
1
- import type { DeviceState } from '../device';
1
+ import type { Features } from '../device';
2
2
  import type { CommonParams, Response } from '../params';
3
3
 
4
- export declare function deviceUnlock(
5
- connectId: string,
6
- params?: CommonParams
7
- ): Response<DeviceState>;
4
+ export declare function deviceUnlock(connectId: string, params?: CommonParams): Response<Features>;
@@ -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
+ }