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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (251) hide show
  1. package/__tests__/AllNetworkGetAddressBase.tracing.test.ts +0 -78
  2. package/__tests__/DeviceCommands.test.ts +0 -45
  3. package/__tests__/DeviceEventRegistration.test.ts +0 -6
  4. package/__tests__/check-all-firmware-release-protocol-v2.test.ts +3 -198
  5. package/__tests__/connectSettings.test.ts +47 -5
  6. package/__tests__/device-initialize-timeout.test.ts +56 -0
  7. package/__tests__/device-lifecycle-events.test.ts +3 -105
  8. package/__tests__/firmware-update/check-all-firmware-release.test.ts +175 -0
  9. package/__tests__/firmware-update/firmware-artifact-source.test.ts +119 -0
  10. package/__tests__/firmware-update/firmware-host-binding.test.ts +70 -0
  11. package/__tests__/firmware-update/firmware-preparation-error.test.ts +27 -0
  12. package/__tests__/firmware-update/firmware-update-bootloader-poll.test.ts +200 -0
  13. package/__tests__/firmware-update/firmware-update-capabilities.test.ts +13 -0
  14. package/__tests__/firmware-update/firmware-update-plan-bootloader-identity.test.ts +294 -0
  15. package/__tests__/firmware-update/firmware-update-plan.test.ts +593 -0
  16. package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +213 -0
  17. package/__tests__/firmware-update/firmware-update-v2-download-before-boot.test.ts +59 -31
  18. package/__tests__/firmware-update/firmware-update-v3-reconnect.test.ts +58 -0
  19. package/__tests__/firmware-update/firmware-upload-source.test.ts +70 -0
  20. package/__tests__/get-device-state.test.ts +12 -132
  21. package/__tests__/homescreen.test.ts +1 -41
  22. package/__tests__/logBlockEvent.test.ts +1 -1
  23. package/__tests__/method-protocol-support.test.ts +0 -19
  24. package/__tests__/open-wallet-session.test.ts +33 -446
  25. package/__tests__/protocol-v2-unlock-policy.test.ts +35 -201
  26. package/__tests__/protocol-v2.test.ts +1034 -1378
  27. package/__tests__/protocolV2UiInteraction.test.ts +0 -63
  28. package/__tests__/public-pro2-api-boundary.test.ts +0 -1
  29. package/__tests__/search-devices.test.ts +3 -12
  30. package/dist/api/BaseMethod.d.ts +1 -3
  31. package/dist/api/BaseMethod.d.ts.map +1 -1
  32. package/dist/api/CheckAllFirmwareRelease.d.ts +1 -2
  33. package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
  34. package/dist/api/FirmwareUpdate.d.ts.map +1 -1
  35. package/dist/api/FirmwareUpdateV2.d.ts +11 -0
  36. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  37. package/dist/api/FirmwareUpdateV3.d.ts +7 -3
  38. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  39. package/dist/api/FirmwareUpdateV4.d.ts +25 -11
  40. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  41. package/dist/api/GetPassphraseState.d.ts.map +1 -1
  42. package/dist/api/OpenWalletSession.d.ts.map +1 -1
  43. package/dist/api/SearchDevices.d.ts.map +1 -1
  44. package/dist/api/allnetwork/AllNetworkGetAddressBase.d.ts.map +1 -1
  45. package/dist/api/device/DeviceUnlock.d.ts +2 -2
  46. package/dist/api/device/DeviceUnlock.d.ts.map +1 -1
  47. package/dist/api/device/DeviceUpdateBootloader.d.ts +5 -1
  48. package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
  49. package/dist/api/firmware/FirmwareArtifactSource.d.ts +25 -0
  50. package/dist/api/firmware/FirmwareArtifactSource.d.ts.map +1 -0
  51. package/dist/api/firmware/FirmwareHostBinding.d.ts +6 -0
  52. package/dist/api/firmware/FirmwareHostBinding.d.ts.map +1 -0
  53. package/dist/api/firmware/FirmwarePreparationError.d.ts +3 -0
  54. package/dist/api/firmware/FirmwarePreparationError.d.ts.map +1 -0
  55. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +9 -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 +25 -0
  60. package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +1 -0
  61. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts +32 -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/progressThrottle.d.ts +3 -0
  66. package/dist/api/firmware/progressThrottle.d.ts.map +1 -0
  67. package/dist/api/firmware/updateBootloader.d.ts +2 -0
  68. package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
  69. package/dist/api/firmware/uploadFirmware.d.ts +9 -1
  70. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  71. package/dist/api/helpers/protocolV2FileWrite.d.ts +0 -2
  72. package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -1
  73. package/dist/api/index.d.ts +0 -2
  74. package/dist/api/index.d.ts.map +1 -1
  75. package/dist/api/protocol-v2/ProtocolInfoRequest.d.ts.map +1 -1
  76. package/dist/api/protocol-v2/helpers.d.ts.map +1 -1
  77. package/dist/api/stellar/StellarSignTransaction.d.ts +1 -1
  78. package/dist/core/deviceEventRegistration.d.ts +0 -2
  79. package/dist/core/deviceEventRegistration.d.ts.map +1 -1
  80. package/dist/core/index.d.ts.map +1 -1
  81. package/dist/data-manager/DataManager.d.ts +2 -4
  82. package/dist/data-manager/DataManager.d.ts.map +1 -1
  83. package/dist/data-manager/connectSettings.d.ts.map +1 -1
  84. package/dist/device/Device.d.ts +6 -29
  85. package/dist/device/Device.d.ts.map +1 -1
  86. package/dist/device/DeviceCommands.d.ts +4 -4
  87. package/dist/device/DeviceCommands.d.ts.map +1 -1
  88. package/dist/device/DevicePool.d.ts.map +1 -1
  89. package/dist/events/device.d.ts +0 -4
  90. package/dist/events/device.d.ts.map +1 -1
  91. package/dist/events/logBlockEvent.d.ts.map +1 -1
  92. package/dist/events/ui-promise.d.ts +2 -4
  93. package/dist/events/ui-promise.d.ts.map +1 -1
  94. package/dist/events/ui-request.d.ts +2 -31
  95. package/dist/events/ui-request.d.ts.map +1 -1
  96. package/dist/events/ui-response.d.ts +2 -10
  97. package/dist/events/ui-response.d.ts.map +1 -1
  98. package/dist/index.d.ts +231 -214
  99. package/dist/index.d.ts.map +1 -1
  100. package/dist/index.js +3497 -2559
  101. package/dist/inject.d.ts +1 -6
  102. package/dist/inject.d.ts.map +1 -1
  103. package/dist/lowLevelInject.d.ts.map +1 -1
  104. package/dist/protocols/protocol-v2/features.d.ts.map +1 -1
  105. package/dist/protocols/protocol-v2/lockedError.d.ts +2 -0
  106. package/dist/protocols/protocol-v2/lockedError.d.ts.map +1 -0
  107. package/dist/protocols/protocol-v2/uiInteraction.d.ts +4 -7
  108. package/dist/protocols/protocol-v2/uiInteraction.d.ts.map +1 -1
  109. package/dist/protocols/protocol-v2/unlockPolicy.d.ts +3 -1
  110. package/dist/protocols/protocol-v2/unlockPolicy.d.ts.map +1 -1
  111. package/dist/protocols/protocol-v2/unlockRetry.d.ts +10 -0
  112. package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +1 -0
  113. package/dist/protocols/protocol-v2/walletSession.d.ts +1 -5
  114. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  115. package/dist/topLevelInject.d.ts.map +1 -1
  116. package/dist/types/api/checkAllFirmwareRelease.d.ts +4 -2
  117. package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
  118. package/dist/types/api/deviceUnlock.d.ts +1 -5
  119. package/dist/types/api/deviceUnlock.d.ts.map +1 -1
  120. package/dist/types/api/deviceUpdateBootloader.d.ts +6 -0
  121. package/dist/types/api/deviceUpdateBootloader.d.ts.map +1 -1
  122. package/dist/types/api/export.d.ts +4 -2
  123. package/dist/types/api/export.d.ts.map +1 -1
  124. package/dist/types/api/firmwareUpdate.d.ts +70 -2
  125. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  126. package/dist/types/api/firmwareUpdateCapabilities.d.ts +9 -0
  127. package/dist/types/api/firmwareUpdateCapabilities.d.ts.map +1 -0
  128. package/dist/types/api/firmwareUpdatePlan.d.ts +28 -0
  129. package/dist/types/api/firmwareUpdatePlan.d.ts.map +1 -0
  130. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts +42 -0
  131. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts.map +1 -0
  132. package/dist/types/api/index.d.ts +10 -7
  133. package/dist/types/api/index.d.ts.map +1 -1
  134. package/dist/types/api/protocolV2.d.ts +0 -3
  135. package/dist/types/api/protocolV2.d.ts.map +1 -1
  136. package/dist/types/device.d.ts.map +1 -1
  137. package/dist/types/params.d.ts +0 -1
  138. package/dist/types/params.d.ts.map +1 -1
  139. package/dist/types/settings.d.ts +13 -22
  140. package/dist/types/settings.d.ts.map +1 -1
  141. package/dist/utils/deviceFeaturesCompat.d.ts +1 -0
  142. package/dist/utils/deviceFeaturesCompat.d.ts.map +1 -1
  143. package/dist/utils/deviceFeaturesUtils.d.ts +0 -4
  144. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  145. package/dist/utils/homescreen.d.ts +0 -4
  146. package/dist/utils/homescreen.d.ts.map +1 -1
  147. package/dist/utils/index.d.ts +1 -1
  148. package/dist/utils/index.d.ts.map +1 -1
  149. package/dist/utils/patch.d.ts +1 -1
  150. package/dist/utils/patch.d.ts.map +1 -1
  151. package/dist/utils/pro2Wallpaper.d.ts +0 -10
  152. package/dist/utils/pro2Wallpaper.d.ts.map +1 -1
  153. package/package.json +4 -4
  154. package/src/api/BaseMethod.ts +10 -8
  155. package/src/api/CheckAllFirmwareRelease.ts +57 -54
  156. package/src/api/FirmwareUpdate.ts +15 -7
  157. package/src/api/FirmwareUpdateV2.ts +316 -126
  158. package/src/api/FirmwareUpdateV3.ts +265 -63
  159. package/src/api/FirmwareUpdateV4.ts +965 -566
  160. package/src/api/GetPassphraseState.ts +1 -7
  161. package/src/api/OpenWalletSession.ts +17 -54
  162. package/src/api/SearchDevices.ts +1 -3
  163. package/src/api/allnetwork/AllNetworkGetAddressBase.ts +11 -32
  164. package/src/api/device/DeviceUnlock.ts +3 -8
  165. package/src/api/device/DeviceUpdateBootloader.ts +122 -6
  166. package/src/api/firmware/FirmwareArtifactSource.ts +278 -0
  167. package/src/api/firmware/FirmwareHostBinding.ts +100 -0
  168. package/src/api/firmware/FirmwarePreparationError.ts +12 -0
  169. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +83 -16
  170. package/src/api/firmware/FirmwareUpdateCapabilities.ts +9 -0
  171. package/src/api/firmware/FirmwareUpdatePlan.ts +772 -0
  172. package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +415 -0
  173. package/src/api/firmware/progressThrottle.ts +44 -0
  174. package/src/api/firmware/updateBootloader.ts +19 -4
  175. package/src/api/firmware/uploadFirmware.ts +144 -39
  176. package/src/api/helpers/protocolV2FileWrite.ts +4 -11
  177. package/src/api/index.ts +0 -2
  178. package/src/api/protocol-v2/ProtocolInfoRequest.ts +1 -3
  179. package/src/api/protocol-v2/helpers.ts +0 -1
  180. package/src/core/deviceEventRegistration.ts +0 -4
  181. package/src/core/index.ts +83 -167
  182. package/src/data/messages/messages-protocol-v2.json +0 -25
  183. package/src/data-manager/DataManager.ts +38 -67
  184. package/src/data-manager/connectSettings.ts +11 -0
  185. package/src/device/Device.ts +42 -258
  186. package/src/device/DeviceCommands.ts +1 -7
  187. package/src/device/DevicePool.ts +2 -7
  188. package/src/events/device.ts +0 -4
  189. package/src/events/logBlockEvent.ts +0 -1
  190. package/src/events/ui-promise.ts +2 -4
  191. package/src/events/ui-request.ts +2 -36
  192. package/src/events/ui-response.ts +2 -12
  193. package/src/index.ts +5 -0
  194. package/src/inject.ts +24 -60
  195. package/src/lowLevelInject.ts +8 -7
  196. package/src/protocols/protocol-v2/features.ts +3 -10
  197. package/src/protocols/protocol-v2/lockedError.ts +10 -0
  198. package/src/protocols/protocol-v2/uiInteraction.ts +6 -46
  199. package/src/protocols/protocol-v2/unlockPolicy.ts +105 -1
  200. package/src/protocols/protocol-v2/unlockRetry.ts +109 -0
  201. package/src/protocols/protocol-v2/walletSession.ts +30 -180
  202. package/src/topLevelInject.ts +8 -7
  203. package/src/types/api/checkAllFirmwareRelease.ts +4 -2
  204. package/src/types/api/deviceUnlock.ts +1 -12
  205. package/src/types/api/deviceUpdateBootloader.ts +6 -0
  206. package/src/types/api/export.ts +18 -1
  207. package/src/types/api/firmwareUpdate.ts +76 -3
  208. package/src/types/api/firmwareUpdateCapabilities.ts +9 -0
  209. package/src/types/api/firmwareUpdatePlan.ts +38 -0
  210. package/src/types/api/firmwareUpdatePreparedPlan.ts +53 -0
  211. package/src/types/api/index.ts +14 -13
  212. package/src/types/api/protocolV2.ts +0 -13
  213. package/src/types/device.ts +0 -3
  214. package/src/types/params.ts +1 -7
  215. package/src/types/settings.ts +13 -42
  216. package/src/utils/deviceFeaturesCompat.ts +6 -0
  217. package/src/utils/deviceFeaturesUtils.ts +0 -8
  218. package/src/utils/homescreen.ts +6 -32
  219. package/src/utils/index.ts +1 -6
  220. package/src/utils/pro2Wallpaper.ts +8 -35
  221. package/__tests__/deviceUploadNft.test.ts +0 -293
  222. package/__tests__/pro2Nft.test.ts +0 -108
  223. package/__tests__/protocol-binding.test.ts +0 -89
  224. package/__tests__/protocol-v2-resources.test.ts +0 -284
  225. package/__tests__/protocol-v2-ui-lifecycle.test.ts +0 -91
  226. package/__tests__/uiProgressThrottle.test.ts +0 -74
  227. package/__tests__/uiPromiseRegistry.test.ts +0 -86
  228. package/dist/api/DetectDeviceConnectProtocol.d.ts +0 -7
  229. package/dist/api/DetectDeviceConnectProtocol.d.ts.map +0 -1
  230. package/dist/api/protocol-v2/DeviceUploadNft.d.ts +0 -31
  231. package/dist/api/protocol-v2/DeviceUploadNft.d.ts.map +0 -1
  232. package/dist/core/uiPromiseRegistry.d.ts +0 -4
  233. package/dist/core/uiPromiseRegistry.d.ts.map +0 -1
  234. package/dist/protocols/protocol-v2/resources.d.ts +0 -30
  235. package/dist/protocols/protocol-v2/resources.d.ts.map +0 -1
  236. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts +0 -22
  237. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts.map +0 -1
  238. package/dist/types/api/detectDeviceConnectProtocol.d.ts +0 -4
  239. package/dist/types/api/detectDeviceConnectProtocol.d.ts.map +0 -1
  240. package/dist/utils/pro2Nft.d.ts +0 -31
  241. package/dist/utils/pro2Nft.d.ts.map +0 -1
  242. package/dist/utils/uiProgressThrottle.d.ts +0 -3
  243. package/dist/utils/uiProgressThrottle.d.ts.map +0 -1
  244. package/src/api/DetectDeviceConnectProtocol.ts +0 -18
  245. package/src/api/protocol-v2/DeviceUploadNft.ts +0 -189
  246. package/src/core/uiPromiseRegistry.ts +0 -41
  247. package/src/protocols/protocol-v2/resources.ts +0 -359
  248. package/src/protocols/protocol-v2/unlockPolicyRunner.ts +0 -118
  249. package/src/types/api/detectDeviceConnectProtocol.ts +0 -7
  250. package/src/utils/pro2Nft.ts +0 -142
  251. package/src/utils/uiProgressThrottle.ts +0 -63
package/src/index.ts CHANGED
@@ -19,6 +19,11 @@ export * from './types';
19
19
  export { whitelist, whitelistExtension } from './data/config';
20
20
  export { executeCallback, cleanupCallback };
21
21
  export { preloadSessionCache } from './device/Device';
22
+ export {
23
+ getFirmwareUpdateHostBindingGeneration,
24
+ registerFirmwareUpdateHostBinding,
25
+ unregisterFirmwareUpdateHostBinding,
26
+ } from './api/firmware/FirmwareHostBinding';
22
27
 
23
28
  const HardwareSdk = ({
24
29
  init,
package/src/inject.ts CHANGED
@@ -1,7 +1,17 @@
1
- import type { HardwareConnectProtocol } from '@onekeyfe/hd-shared';
2
- 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
+
3
12
  import type { EventEmitter } from 'events';
4
13
  import type { CallMethod } from './events';
14
+ import type { Unsuccessful } from './types';
5
15
  import type { CoreApi } from './types/api';
6
16
  import type { AllNetworkAddress } from './types/api/allNetworkGetAddress';
7
17
 
@@ -40,50 +50,6 @@ export interface InjectApi {
40
50
  switchTransport: CoreApi['switchTransport'];
41
51
  }
42
52
 
43
- const normalizeConnectId = (connectId: string) => connectId.trim().toLowerCase();
44
-
45
- /**
46
- * Keeps verified protocols isolated per transport endpoint and injects them at
47
- * the single public call boundary, including APIs that do not expose params.
48
- */
49
- export const createProtocolAwareCall = (rawCall: CoreApi['call']) => {
50
- const protocolByConnectId = new Map<string, HardwareConnectProtocol>();
51
-
52
- const setDeviceConnectProtocol: CoreApi['setDeviceConnectProtocol'] = (
53
- connectId,
54
- connectProtocol
55
- ) => {
56
- const normalizedConnectId = normalizeConnectId(connectId);
57
- if (!normalizedConnectId) return;
58
- if (connectProtocol) {
59
- protocolByConnectId.set(normalizedConnectId, connectProtocol);
60
- } else {
61
- protocolByConnectId.delete(normalizedConnectId);
62
- }
63
- };
64
-
65
- const call: CoreApi['call'] = params => {
66
- if (!params || typeof params !== 'object') {
67
- return rawCall(params);
68
- }
69
-
70
- const connectId = typeof params.connectId === 'string' ? params.connectId : undefined;
71
- const boundProtocol = connectId
72
- ? protocolByConnectId.get(normalizeConnectId(connectId))
73
- : undefined;
74
- if (
75
- boundProtocol &&
76
- params.connectProtocol === undefined &&
77
- params.forceProtocolDetection !== true
78
- ) {
79
- return rawCall({ ...params, connectProtocol: boundProtocol });
80
- }
81
- return rawCall(params);
82
- };
83
-
84
- return { call, setDeviceConnectProtocol };
85
- };
86
-
87
53
  export const inject = ({
88
54
  call,
89
55
  cancel,
@@ -94,7 +60,6 @@ export const inject = ({
94
60
  switchTransport,
95
61
  uiResponse,
96
62
  }: InjectApi): CoreApi => {
97
- const protocolAwareCall = createProtocolAwareCall(call);
98
63
  const api: CoreApi = {
99
64
  on: <T extends string, P extends (...args: any[]) => any>(type: T, fn: P) => {
100
65
  eventEmitter.on(type, fn);
@@ -112,11 +77,12 @@ export const inject = ({
112
77
 
113
78
  init,
114
79
 
115
- call: protocolAwareCall.call,
80
+ call,
116
81
 
117
- setDeviceConnectProtocol: protocolAwareCall.setDeviceConnectProtocol,
118
-
119
- dispose,
82
+ dispose: async () => {
83
+ unregisterFirmwareUpdateHostBinding();
84
+ await dispose();
85
+ },
120
86
 
121
87
  uiResponse,
122
88
 
@@ -126,7 +92,7 @@ export const inject = ({
126
92
 
127
93
  switchTransport,
128
94
 
129
- ...createCoreApi(protocolAwareCall.call),
95
+ ...createCoreApi(call),
130
96
  };
131
97
  return api;
132
98
  };
@@ -141,25 +107,24 @@ export const createCoreApi = (
141
107
  | 'removeAllListeners'
142
108
  | 'init'
143
109
  | 'call'
144
- | 'setDeviceConnectProtocol'
145
110
  | 'dispose'
146
111
  | 'uiResponse'
147
112
  | 'cancel'
148
113
  | 'updateSettings'
149
114
  | 'switchTransport'
150
115
  > => ({
116
+ getFirmwareUpdateCapabilities,
117
+ registerFirmwareUpdateHostBinding,
118
+ unregisterFirmwareUpdateHostBinding,
119
+ getFirmwareUpdateHostBindingGeneration,
120
+ prepareFirmwareUpdatePlan,
121
+ validateFirmwareUpdatePreparedPlan,
151
122
  getLogs: () => call({ method: 'getLogs' }),
152
123
  clearSessionCache: params => call({ ...params, method: 'clearSessionCache' }),
153
124
  /**
154
125
  * 搜索设备
155
126
  */
156
127
  searchDevices: params => call({ ...params, method: 'searchDevices' }),
157
- detectDeviceConnectProtocol: connectId =>
158
- call({
159
- connectId,
160
- method: 'detectDeviceConnectProtocol',
161
- forceProtocolDetection: true,
162
- }),
163
128
 
164
129
  /**
165
130
  * 获取设备信息
@@ -217,7 +182,6 @@ export const createCoreApi = (
217
182
  call({ ...params, connectId, method: 'deviceGetOnboardingStatus' }),
218
183
  deviceUploadWallpaper: (connectId, params) =>
219
184
  call({ ...params, connectId, method: 'deviceUploadWallpaper' }),
220
- deviceUploadNft: (connectId, params) => call({ ...params, connectId, method: 'deviceUploadNft' }),
221
185
  uploadPortfolio: (connectId, params) => call({ ...params, connectId, method: 'uploadPortfolio' }),
222
186
  deviceRecovery: (connectId, params) => call({ ...params, connectId, method: 'deviceRecovery' }),
223
187
  deviceReset: (connectId, params) => call({ ...params, connectId, method: 'deviceReset' }),
@@ -1,4 +1,5 @@
1
- import { createCoreApi, createProtocolAwareCall } from './inject';
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';
@@ -33,7 +34,6 @@ export const lowLevelInject = ({
33
34
  switchTransport,
34
35
  addHardwareGlobalEventListener,
35
36
  }: LowLevelInjectApi): LowLevelCoreApi => {
36
- const protocolAwareCall = createProtocolAwareCall(call);
37
37
  const api: LowLevelCoreApi = {
38
38
  addHardwareGlobalEventListener,
39
39
  removeAllListeners: type => {
@@ -42,11 +42,12 @@ export const lowLevelInject = ({
42
42
 
43
43
  init,
44
44
 
45
- call: protocolAwareCall.call,
45
+ call,
46
46
 
47
- setDeviceConnectProtocol: protocolAwareCall.setDeviceConnectProtocol,
48
-
49
- dispose,
47
+ dispose: async () => {
48
+ unregisterFirmwareUpdateHostBinding();
49
+ await dispose();
50
+ },
50
51
 
51
52
  uiResponse,
52
53
 
@@ -58,7 +59,7 @@ export const lowLevelInject = ({
58
59
 
59
60
  emit: () => {},
60
61
 
61
- ...createCoreApi(protocolAwareCall.call),
62
+ ...createCoreApi(call),
62
63
  };
63
64
  return api;
64
65
  };
@@ -157,7 +157,7 @@ export const PROTOCOL_V2_FULL_DEVICE_INFO_REQUEST = {
157
157
  };
158
158
 
159
159
  export const PROTOCOL_V2_DEVICE_INFO_REQUEST = PROTOCOL_V2_FULL_DEVICE_INFO_REQUEST;
160
- export const PROTOCOL_V2_DEVICE_INFO_TIMEOUT_MS = 30 * 1000;
160
+ export const PROTOCOL_V2_DEVICE_INFO_TIMEOUT_MS = 10 * 1000;
161
161
 
162
162
  export async function requestProtocolV2ProtocolInfo({
163
163
  commands,
@@ -168,15 +168,8 @@ export async function requestProtocolV2ProtocolInfo({
168
168
  }): Promise<ProtocolInfo> {
169
169
  const response =
170
170
  timeoutMs === undefined
171
- ? await commands.typedCall('ProtocolInfoRequest', 'ProtocolInfo', {
172
- eventless_wallet_session: true,
173
- })
174
- : await commands.typedCall(
175
- 'ProtocolInfoRequest',
176
- 'ProtocolInfo',
177
- { eventless_wallet_session: true },
178
- { timeoutMs }
179
- );
171
+ ? await commands.typedCall('ProtocolInfoRequest', 'ProtocolInfo', {})
172
+ : await commands.typedCall('ProtocolInfoRequest', 'ProtocolInfo', {}, { timeoutMs });
180
173
  return response.message;
181
174
  }
182
175
 
@@ -0,0 +1,10 @@
1
+ import { HardwareErrorCode } from '@onekeyfe/hd-shared';
2
+
3
+ export function isDeviceLockedError(error: unknown): boolean {
4
+ return (
5
+ typeof error === 'object' &&
6
+ error !== null &&
7
+ 'errorCode' in error &&
8
+ error.errorCode === HardwareErrorCode.DeviceLocked
9
+ );
10
+ }
@@ -1,6 +1,6 @@
1
1
  import { UI_REQUEST, createUiMessage } from '../../events';
2
2
 
3
- import type { CoreMessage, HardwareUiInteractionMeta, ProtocolV2UiCompletion } from '../../events';
3
+ import type { CoreMessage, ProtocolV2UiCompletion } from '../../events';
4
4
  import type { Device } from '../../device/Device';
5
5
  import type { KnownDevice } from '../../types';
6
6
 
@@ -28,15 +28,7 @@ type ProtocolV2InteractionMethod = {
28
28
  protocolV2UiInteraction?: ProtocolV2InteractionDescriptor;
29
29
  };
30
30
 
31
- type InteractionDevice = Pick<Device, 'isProtocolV2' | 'toMessageObject'> &
32
- Partial<
33
- Pick<
34
- Device,
35
- | 'createProtocolV2UiPhaseMetadata'
36
- | 'finishProtocolV2UiInteraction'
37
- | 'hasOpenProtocolV2UiInteraction'
38
- >
39
- >;
31
+ type InteractionDevice = Pick<Device, 'isProtocolV2' | 'toMessageObject'>;
40
32
  type PostMessage = (message: CoreMessage) => void;
41
33
 
42
34
  export const isProtocolV2UiEnabled = (method: { protocolV2UiMode?: 'auto' | 'none' }) =>
@@ -164,17 +156,12 @@ export class ProtocolV2UiInteractionCoordinator {
164
156
  this.methodInteraction = interaction;
165
157
  if (!interaction) return;
166
158
  const { request, ...metadata } = interaction;
167
- const interactionMeta = this.device.createProtocolV2UiPhaseMetadata?.(
168
- request === 'pin' ? 'pin' : 'button',
169
- 'start'
170
- );
171
159
 
172
160
  this.emit(
173
161
  request === 'pin' ? UI_REQUEST.REQUEST_PIN : UI_REQUEST.REQUEST_BUTTON,
174
162
  {
175
163
  device: this.device.toMessageObject() as KnownDevice,
176
164
  ...metadata,
177
- ...(interactionMeta ? { interaction: interactionMeta } : {}),
178
165
  },
179
166
  `method:${request}:${interaction.reason}:${interaction.page ?? ''}:${
180
167
  interaction.operation ?? ''
@@ -182,8 +169,7 @@ export class ProtocolV2UiInteractionCoordinator {
182
169
  );
183
170
  }
184
171
 
185
- enterUnlockInteraction(method?: string): HardwareUiInteractionMeta | undefined {
186
- const interaction = this.device.createProtocolV2UiPhaseMetadata?.('pin', 'start');
172
+ enterUnlockInteraction(method?: string) {
187
173
  this.emit(
188
174
  UI_REQUEST.REQUEST_PIN,
189
175
  {
@@ -192,45 +178,19 @@ export class ProtocolV2UiInteractionCoordinator {
192
178
  reason: 'device-locked',
193
179
  deviceOnly: true,
194
180
  method,
195
- ...(interaction ? { interaction } : {}),
196
181
  },
197
182
  `unlock:${method ?? ''}`
198
183
  );
199
- return interaction;
200
184
  }
201
185
 
202
186
  resumeMethodInteraction() {
203
187
  this.enterMethodInteraction(this.methodInteraction);
204
188
  }
205
189
 
206
- close(
207
- options: {
208
- ensureOperationClose?: boolean;
209
- protocolV2Operation?: boolean;
210
- } = {}
211
- ): boolean {
212
- if (
213
- (!options.protocolV2Operation && !this.device.isProtocolV2()) ||
214
- (!options.ensureOperationClose &&
215
- !this.opened &&
216
- !this.device.hasOpenProtocolV2UiInteraction?.()) ||
217
- this.closed
218
- )
219
- return false;
190
+ close() {
191
+ if (!this.device.isProtocolV2() || !this.opened || this.closed) return;
220
192
  this.closed = true;
221
- const interaction = this.device.finishProtocolV2UiInteraction?.('succeeded', {
222
- ensureMetadata: options.ensureOperationClose,
223
- });
224
- const device = this.device.toMessageObject();
225
- this.postMessage(
226
- interaction
227
- ? createUiMessage(UI_REQUEST.CLOSE_UI_WINDOW, {
228
- ...interaction,
229
- ...(device ? { device } : {}),
230
- })
231
- : createUiMessage(UI_REQUEST.CLOSE_UI_WINDOW)
232
- );
233
- return true;
193
+ this.postMessage(createUiMessage(UI_REQUEST.CLOSE_UI_WINDOW));
234
194
  }
235
195
 
236
196
  private emit(
@@ -1 +1,105 @@
1
- export type UnlockPolicy = 'none' | 'unlock-before-run';
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';
@@ -0,0 +1,109 @@
1
+ import { LoggerNames, getLogger } from '../../utils';
2
+ import { isDeviceLockedError } from './lockedError';
3
+ import { isProtocolV2UiEnabled, resolveProtocolV2UiInteraction } from './uiInteraction';
4
+ import { refreshProtocolV2DeviceStatus, restoreProtocolV2WalletSession } from './walletSession';
5
+
6
+ import type { BaseMethod } from '../../api/BaseMethod';
7
+ import type { Device } from '../../device/Device';
8
+ import type { ProtocolV2UiInteractionCoordinator } from './uiInteraction';
9
+
10
+ const Log = getLogger(LoggerNames.Core);
11
+
12
+ type RunnableMethod = Pick<
13
+ BaseMethod,
14
+ | 'run'
15
+ | 'unlockPolicy'
16
+ | 'protocolV2UiInteraction'
17
+ | 'protocolV2UiMode'
18
+ | 'params'
19
+ | 'payload'
20
+ | 'useDevicePassphraseState'
21
+ > & { name?: string };
22
+ type UiInteractionCoordinator = Pick<
23
+ ProtocolV2UiInteractionCoordinator,
24
+ 'enterMethodInteraction' | 'enterUnlockInteraction' | 'resumeMethodInteraction'
25
+ >;
26
+
27
+ const restoreExpectedWalletSessionAfterUnlock = async (method: RunnableMethod, device: Device) => {
28
+ const expectedPassphraseState = method.payload?.useEmptyPassphrase
29
+ ? undefined
30
+ : method.payload?.passphraseState ?? device.passphraseState;
31
+ if (
32
+ !method.useDevicePassphraseState ||
33
+ typeof expectedPassphraseState !== 'string' ||
34
+ expectedPassphraseState.length === 0
35
+ ) {
36
+ return;
37
+ }
38
+
39
+ await restoreProtocolV2WalletSession(device, expectedPassphraseState);
40
+ Log.debug('Protocol V2 wallet session restored after unlock', { method: method.name });
41
+ };
42
+
43
+ export async function runMethodWithUnlockRetry(
44
+ method: RunnableMethod,
45
+ device: Device,
46
+ uiCoordinator?: UiInteractionCoordinator
47
+ ) {
48
+ const shouldEmitUi = isProtocolV2UiEnabled(method);
49
+ const isProtocolV2 = device.isProtocolV2();
50
+ const requiresFreshStatus =
51
+ isProtocolV2 &&
52
+ method.unlockPolicy === 'unlock-before-run' &&
53
+ !device.isBootloader?.() &&
54
+ !device.isRomloader?.();
55
+
56
+ if (requiresFreshStatus) {
57
+ await refreshProtocolV2DeviceStatus(device);
58
+ }
59
+
60
+ const shouldUnlockBeforeRun =
61
+ isProtocolV2 &&
62
+ method.unlockPolicy !== 'none' &&
63
+ !device.isBootloader?.() &&
64
+ !device.isRomloader?.() &&
65
+ device.features?.unlocked === false;
66
+
67
+ if (shouldUnlockBeforeRun) {
68
+ if (shouldEmitUi) {
69
+ uiCoordinator?.enterUnlockInteraction(method.name);
70
+ }
71
+ await device.unlockDevice();
72
+ Log.debug('Protocol V2 pre-unlock completed', { method: method.name });
73
+ await restoreExpectedWalletSessionAfterUnlock(method, device);
74
+ if (shouldEmitUi) {
75
+ uiCoordinator?.enterMethodInteraction(resolveProtocolV2UiInteraction(method));
76
+ }
77
+ return method.run();
78
+ }
79
+
80
+ if (shouldEmitUi) {
81
+ uiCoordinator?.enterMethodInteraction(resolveProtocolV2UiInteraction(method));
82
+ }
83
+ try {
84
+ return await method.run();
85
+ } catch (error) {
86
+ if (!isProtocolV2 || method.unlockPolicy !== 'retry-on-locked' || !isDeviceLockedError(error)) {
87
+ throw error;
88
+ }
89
+
90
+ Log.debug('Protocol V2 unlock retry triggered', { method: method.name });
91
+ if (shouldEmitUi) {
92
+ uiCoordinator?.enterUnlockInteraction(method.name);
93
+ }
94
+ await device.unlockDevice();
95
+ Log.debug('Protocol V2 unlock completed', { method: method.name });
96
+ await restoreExpectedWalletSessionAfterUnlock(method, device);
97
+ if (shouldEmitUi) {
98
+ uiCoordinator?.resumeMethodInteraction();
99
+ }
100
+ try {
101
+ const response = await method.run();
102
+ Log.debug('Protocol V2 method retry completed', { method: method.name, success: true });
103
+ return response;
104
+ } catch (retryError) {
105
+ Log.debug('Protocol V2 method retry completed', { method: method.name, success: false });
106
+ throw retryError;
107
+ }
108
+ }
109
+ }