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

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 (256) hide show
  1. package/__tests__/AllNetworkGetAddressBase.tracing.test.ts +75 -0
  2. package/__tests__/DeviceCommands.test.ts +143 -7
  3. package/__tests__/RequestQueue.test.ts +106 -0
  4. package/__tests__/UploadPortfolio.test.ts +46 -0
  5. package/__tests__/core-dispose.test.ts +34 -0
  6. package/__tests__/core-error-output.test.ts +39 -0
  7. package/__tests__/device-lifecycle-events.test.ts +117 -0
  8. package/__tests__/device-pool-state.test.ts +20 -1
  9. package/__tests__/device-settings.test.ts +184 -6
  10. package/__tests__/device-state-events.test.ts +1 -2
  11. package/__tests__/device-state-mapper.test.ts +33 -26
  12. package/__tests__/device-state-store.test.ts +1 -34
  13. package/__tests__/device-utils.test.ts +106 -0
  14. package/__tests__/device-wallet-session-store.test.ts +65 -29
  15. package/__tests__/deviceSettings.test.ts +21 -12
  16. package/__tests__/evmLedgerLegacySafety.test.ts +2 -2
  17. package/__tests__/evmSignTypedData.test.ts +80 -0
  18. package/__tests__/filesystemValidation.test.ts +53 -0
  19. package/__tests__/firmware-update/firmware-update-v2-download-before-boot.test.ts +402 -0
  20. package/__tests__/get-device-state.test.ts +77 -41
  21. package/__tests__/logBlockEvent.test.ts +24 -11
  22. package/__tests__/networkUtils.test.ts +386 -0
  23. package/__tests__/open-wallet-session-error-response.test.ts +43 -0
  24. package/__tests__/open-wallet-session.test.ts +559 -0
  25. package/__tests__/protocol-v2.test.ts +756 -452
  26. package/__tests__/public-device-state-api.test.ts +113 -13
  27. package/__tests__/public-pro2-api-boundary.test.ts +91 -0
  28. package/__tests__/search-devices.test.ts +43 -0
  29. package/dist/api/BaseMethod.d.ts +3 -0
  30. package/dist/api/BaseMethod.d.ts.map +1 -1
  31. package/dist/api/CheckBootloaderRelease.d.ts.map +1 -1
  32. package/dist/api/CheckFirmwareRelease.d.ts.map +1 -1
  33. package/dist/api/ClearSessionCache.d.ts.map +1 -1
  34. package/dist/api/DirList.d.ts.map +1 -1
  35. package/dist/api/FileRead.d.ts.map +1 -1
  36. package/dist/api/FirmwareUpdateV2.d.ts +2 -2
  37. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  38. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  39. package/dist/api/FirmwareUpdateV4.d.ts +3 -2
  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 +14 -0
  43. package/dist/api/OpenWalletSession.d.ts.map +1 -0
  44. package/dist/api/PathInfo.d.ts.map +1 -1
  45. package/dist/api/PromptWebDeviceAccess.d.ts.map +1 -1
  46. package/dist/api/SearchDevices.d.ts +3 -0
  47. package/dist/api/SearchDevices.d.ts.map +1 -1
  48. package/dist/api/UploadPortfolio.d.ts.map +1 -1
  49. package/dist/api/allnetwork/AllNetworkGetAddressBase.d.ts.map +1 -1
  50. package/dist/api/device/DeviceChangePin.d.ts.map +1 -1
  51. package/dist/api/device/DeviceFullyUploadResource.d.ts +1 -2
  52. package/dist/api/device/DeviceFullyUploadResource.d.ts.map +1 -1
  53. package/dist/api/device/DeviceSettings.d.ts.map +1 -1
  54. package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
  55. package/dist/api/device/DeviceWipe.d.ts.map +1 -1
  56. package/dist/api/evm/EVMSignTypedData.d.ts.map +1 -1
  57. package/dist/api/firmware/getBinary.d.ts +7 -3
  58. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  59. package/dist/api/helpers/filesystemValidation.d.ts +3 -0
  60. package/dist/api/helpers/filesystemValidation.d.ts.map +1 -1
  61. package/dist/api/helpers/paramsValidator.d.ts +1 -0
  62. package/dist/api/helpers/paramsValidator.d.ts.map +1 -1
  63. package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -1
  64. package/dist/api/index.d.ts +7 -11
  65. package/dist/api/index.d.ts.map +1 -1
  66. package/dist/api/protocol-v2/DeviceGetFirmwareUpdateStatus.d.ts.map +1 -1
  67. package/dist/api/protocol-v2/DeviceSettingsPageShow.d.ts.map +1 -1
  68. package/dist/api/protocol-v2/DeviceSettingsSet.d.ts.map +1 -1
  69. package/dist/api/utils.d.ts.map +1 -1
  70. package/dist/core/RequestQueue.d.ts +17 -1
  71. package/dist/core/RequestQueue.d.ts.map +1 -1
  72. package/dist/core/index.d.ts +3 -1
  73. package/dist/core/index.d.ts.map +1 -1
  74. package/dist/device/Device.d.ts +6 -5
  75. package/dist/device/Device.d.ts.map +1 -1
  76. package/dist/device/DeviceCommands.d.ts +5 -5
  77. package/dist/device/DeviceCommands.d.ts.map +1 -1
  78. package/dist/device/DevicePool.d.ts +1 -0
  79. package/dist/device/DevicePool.d.ts.map +1 -1
  80. package/dist/device/DeviceStateMapper.d.ts +2 -2
  81. package/dist/device/DeviceStateMapper.d.ts.map +1 -1
  82. package/dist/device/DeviceStateProjector.d.ts +1 -2
  83. package/dist/device/DeviceStateProjector.d.ts.map +1 -1
  84. package/dist/device/DeviceStateStore.d.ts +1 -3
  85. package/dist/device/DeviceStateStore.d.ts.map +1 -1
  86. package/dist/device/DeviceWalletSessionStore.d.ts +6 -1
  87. package/dist/device/DeviceWalletSessionStore.d.ts.map +1 -1
  88. package/dist/device/cloneDeviceState.d.ts +2 -0
  89. package/dist/device/cloneDeviceState.d.ts.map +1 -0
  90. package/dist/deviceProfile/buildDeviceFeatures.d.ts +3 -1
  91. package/dist/deviceProfile/buildDeviceFeatures.d.ts.map +1 -1
  92. package/dist/events/logBlockEvent.d.ts.map +1 -1
  93. package/dist/index.d.ts +608 -75
  94. package/dist/index.js +2287 -1706
  95. package/dist/inject.d.ts.map +1 -1
  96. package/dist/protocols/protocol-v2/deviceSession.d.ts +7 -0
  97. package/dist/protocols/protocol-v2/deviceSession.d.ts.map +1 -0
  98. package/dist/protocols/protocol-v2/features.d.ts +11 -4
  99. package/dist/protocols/protocol-v2/features.d.ts.map +1 -1
  100. package/dist/protocols/protocol-v2/index.d.ts +2 -2
  101. package/dist/protocols/protocol-v2/index.d.ts.map +1 -1
  102. package/dist/protocols/protocol-v2/settingsUnlockPolicy.d.ts +16 -0
  103. package/dist/protocols/protocol-v2/settingsUnlockPolicy.d.ts.map +1 -0
  104. package/dist/protocols/protocol-v2/uiInteraction.d.ts.map +1 -1
  105. package/dist/protocols/protocol-v2/walletSession.d.ts +8 -3
  106. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  107. package/dist/types/api/checkAllFirmwareRelease.d.ts +1 -2
  108. package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
  109. package/dist/types/api/deviceSettings.d.ts +1 -0
  110. package/dist/types/api/deviceSettings.d.ts.map +1 -1
  111. package/dist/types/api/deviceUnlock.d.ts +1 -1
  112. package/dist/types/api/deviceUnlock.d.ts.map +1 -1
  113. package/dist/types/api/getFeatures.d.ts.map +1 -1
  114. package/dist/types/api/getOnekeyFeatures.d.ts.map +1 -1
  115. package/dist/types/api/getPassphraseState.d.ts.map +1 -1
  116. package/dist/types/api/index.d.ts +13 -15
  117. package/dist/types/api/index.d.ts.map +1 -1
  118. package/dist/types/api/openWalletSession.d.ts +41 -0
  119. package/dist/types/api/openWalletSession.d.ts.map +1 -0
  120. package/dist/types/api/protocolV2.d.ts +6 -11
  121. package/dist/types/api/protocolV2.d.ts.map +1 -1
  122. package/dist/types/device.d.ts +2 -9
  123. package/dist/types/device.d.ts.map +1 -1
  124. package/dist/utils/assets.d.ts +2 -1
  125. package/dist/utils/assets.d.ts.map +1 -1
  126. package/dist/utils/deviceFeaturesCompat.d.ts +13 -0
  127. package/dist/utils/deviceFeaturesCompat.d.ts.map +1 -0
  128. package/dist/utils/deviceFeaturesUtils.d.ts +11 -1
  129. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  130. package/dist/utils/deviceInfoUtils.d.ts +9 -8
  131. package/dist/utils/deviceInfoUtils.d.ts.map +1 -1
  132. package/dist/utils/deviceSettings.d.ts +2 -0
  133. package/dist/utils/deviceSettings.d.ts.map +1 -1
  134. package/dist/utils/deviceVersionUtils.d.ts +6 -5
  135. package/dist/utils/deviceVersionUtils.d.ts.map +1 -1
  136. package/dist/utils/index.d.ts +2 -2
  137. package/dist/utils/index.d.ts.map +1 -1
  138. package/dist/utils/networkUtils.d.ts +9 -1
  139. package/dist/utils/networkUtils.d.ts.map +1 -1
  140. package/dist/utils/patch.d.ts +1 -1
  141. package/dist/utils/patch.d.ts.map +1 -1
  142. package/dist/utils/pro2Wallpaper.d.ts.map +1 -1
  143. package/package.json +4 -4
  144. package/src/api/BaseMethod.ts +17 -10
  145. package/src/api/CheckBootloaderRelease.ts +1 -2
  146. package/src/api/CheckFirmwareRelease.ts +1 -2
  147. package/src/api/ClearSessionCache.ts +0 -4
  148. package/src/api/DirList.ts +5 -3
  149. package/src/api/DirMake.ts +3 -3
  150. package/src/api/DirRemove.ts +3 -3
  151. package/src/api/FileDelete.ts +3 -3
  152. package/src/api/FileRead.ts +9 -6
  153. package/src/api/FileWrite.ts +3 -3
  154. package/src/api/FirmwareUpdate.ts +4 -4
  155. package/src/api/FirmwareUpdateV2.ts +133 -45
  156. package/src/api/FirmwareUpdateV3.ts +3 -5
  157. package/src/api/FirmwareUpdateV4.ts +192 -154
  158. package/src/api/GetPassphraseState.ts +10 -16
  159. package/src/api/OpenWalletSession.ts +221 -0
  160. package/src/api/PathInfo.ts +5 -3
  161. package/src/api/PromptWebDeviceAccess.ts +2 -3
  162. package/src/api/SearchDevices.ts +6 -2
  163. package/src/api/UploadPortfolio.ts +12 -3
  164. package/src/api/alephium/AlephiumSignTransaction.ts +2 -2
  165. package/src/api/allnetwork/AllNetworkGetAddressBase.ts +4 -8
  166. package/src/api/benfen/BenfenGetAddress.ts +1 -1
  167. package/src/api/btc/BTCGetPublicKey.ts +1 -1
  168. package/src/api/device/DeviceChangePin.ts +1 -0
  169. package/src/api/device/DeviceFullyUploadResource.ts +4 -5
  170. package/src/api/device/DeviceSettings.ts +93 -0
  171. package/src/api/device/DeviceUpdateBootloader.ts +1 -2
  172. package/src/api/device/DeviceWipe.ts +1 -0
  173. package/src/api/evm/EVMSignTypedData.ts +3 -5
  174. package/src/api/evm/legacyV1/getAddress.ts +2 -2
  175. package/src/api/evm/legacyV1/getPublicKey.ts +2 -2
  176. package/src/api/evm/legacyV1/signMessage.ts +2 -2
  177. package/src/api/evm/legacyV1/signTypedData.ts +2 -2
  178. package/src/api/evm/legacyV1/signTypedHash.ts +2 -2
  179. package/src/api/evm/legacyV1/verifyMessage.ts +2 -2
  180. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +3 -3
  181. package/src/api/firmware/getBinary.ts +8 -3
  182. package/src/api/helpers/batchGetPublickeys.ts +2 -2
  183. package/src/api/helpers/filesystemValidation.ts +61 -0
  184. package/src/api/helpers/paramsValidator.ts +1 -1
  185. package/src/api/helpers/protocolV2FileWrite.ts +3 -2
  186. package/src/api/index.ts +9 -12
  187. package/src/api/nervos/NervosSignTransaction.ts +2 -2
  188. package/src/api/nexa/NexaSignTransaction.ts +1 -1
  189. package/src/api/protocol-v2/DeviceFactoryInfoGet.ts +1 -1
  190. package/src/api/protocol-v2/DeviceFactoryInfoSet.ts +1 -1
  191. package/src/api/protocol-v2/DeviceFirmwareUpdate.ts +4 -4
  192. package/src/api/protocol-v2/DeviceGetFirmwareUpdateStatus.ts +6 -5
  193. package/src/api/protocol-v2/DeviceInfoGet.ts +4 -4
  194. package/src/api/protocol-v2/DeviceReboot.ts +1 -1
  195. package/src/api/protocol-v2/DeviceSettingsPageShow.ts +11 -8
  196. package/src/api/protocol-v2/DeviceSettingsSet.ts +4 -11
  197. package/src/api/protocol-v2/FilesystemFormat.ts +1 -1
  198. package/src/api/protocol-v2/FilesystemPermissionFix.ts +1 -1
  199. package/src/api/protocol-v2/Ping.ts +1 -1
  200. package/src/api/protocol-v2/ProtocolInfoRequest.ts +1 -1
  201. package/src/api/utils.ts +1 -3
  202. package/src/core/RequestQueue.ts +107 -6
  203. package/src/core/index.ts +91 -46
  204. package/src/data/messages/messages-protocol-v2.json +25 -79
  205. package/src/data-manager/DataManager.ts +6 -6
  206. package/src/device/Device.ts +186 -102
  207. package/src/device/DeviceCommands.ts +51 -21
  208. package/src/device/DeviceFeaturesState.ts +4 -4
  209. package/src/device/DevicePool.ts +17 -3
  210. package/src/device/DeviceStateMapper.ts +13 -29
  211. package/src/device/DeviceStateProjector.ts +11 -8
  212. package/src/device/DeviceStateStore.ts +11 -52
  213. package/src/device/DeviceWalletSessionStore.ts +31 -4
  214. package/src/device/cloneDeviceState.ts +39 -0
  215. package/src/device/deviceIdentity.ts +2 -2
  216. package/src/deviceProfile/buildDeviceFeatures.ts +29 -88
  217. package/src/events/logBlockEvent.ts +8 -1
  218. package/src/inject.ts +14 -24
  219. package/src/protocols/protocol-v2/deviceSession.ts +24 -0
  220. package/src/protocols/protocol-v2/features.ts +37 -49
  221. package/src/protocols/protocol-v2/firmware.ts +1 -1
  222. package/src/protocols/protocol-v2/index.ts +2 -3
  223. package/src/protocols/protocol-v2/settingsUnlockPolicy.ts +61 -0
  224. package/src/protocols/protocol-v2/uiInteraction.ts +4 -3
  225. package/src/protocols/protocol-v2/walletSession.ts +57 -146
  226. package/src/types/api/checkAllFirmwareRelease.ts +1 -2
  227. package/src/types/api/deviceSettings.ts +1 -0
  228. package/src/types/api/deviceUnlock.ts +1 -1
  229. package/src/types/api/firmwareUpdate.ts +5 -5
  230. package/src/types/api/getDeviceState.ts +1 -1
  231. package/src/types/api/getFeatures.ts +3 -0
  232. package/src/types/api/getOnekeyFeatures.ts +3 -0
  233. package/src/types/api/getPassphraseState.ts +6 -0
  234. package/src/types/api/index.ts +16 -23
  235. package/src/types/api/openWalletSession.ts +64 -0
  236. package/src/types/api/protocolV2.ts +36 -18
  237. package/src/types/device.ts +20 -14
  238. package/src/types/params.ts +1 -1
  239. package/src/types/settings.ts +8 -8
  240. package/src/utils/assets.ts +4 -1
  241. package/src/utils/deviceFeaturesCompat.ts +173 -0
  242. package/src/utils/deviceFeaturesUtils.ts +9 -8
  243. package/src/utils/deviceInfoUtils.ts +26 -64
  244. package/src/utils/deviceSettings.ts +18 -11
  245. package/src/utils/deviceVersionUtils.ts +25 -44
  246. package/src/utils/findDefectiveBatchDevice.ts +3 -3
  247. package/src/utils/index.ts +2 -5
  248. package/src/utils/networkUtils.ts +270 -14
  249. package/src/utils/pro2Wallpaper.ts +11 -13
  250. package/src/utils/tracing.ts +2 -2
  251. package/dist/api/internal.d.ts +0 -4
  252. package/dist/api/internal.d.ts.map +0 -1
  253. package/dist/api/protocol-v2/DeviceSessionOpen.d.ts +0 -28
  254. package/dist/api/protocol-v2/DeviceSessionOpen.d.ts.map +0 -1
  255. package/src/api/internal.ts +0 -9
  256. package/src/api/protocol-v2/DeviceSessionOpen.ts +0 -100
@@ -1,25 +1,281 @@
1
- import axios from 'axios';
1
+ import axios, { type AxiosResponse } from 'axios';
2
2
 
3
- export const httpRequest = async (url: string, type = 'text') => {
3
+ export type HttpRequestOptions = {
4
+ /** Axios adapter-defined request timeout retained for existing opt-in callers. */
5
+ timeoutMs?: number;
6
+ /** Deadline for DNS, connection, TLS, redirects, and the first response-body progress. */
7
+ connectTimeoutMs?: number;
8
+ /** Maximum idle interval between response-body progress events. */
9
+ readTimeoutMs?: number;
10
+ /** Wall-clock deadline across all attempts and retry backoff. */
11
+ overallTimeoutMs?: number;
12
+ maxRetries?: number;
13
+ retryDelayMs?: number;
14
+ };
15
+
16
+ const MAX_RETRY_COUNT = 3;
17
+ const REQUEST_PHASE_TIMEOUT_CODES = {
18
+ connect: 'ECONNECTTIMEDOUT',
19
+ read: 'EREADTIMEDOUT',
20
+ } as const;
21
+ const RETRYABLE_HTTP_STATUSES = new Set([500, 502, 503, 504]);
22
+ const RETRYABLE_NETWORK_ERROR_CODES = new Set([
23
+ REQUEST_PHASE_TIMEOUT_CODES.connect,
24
+ REQUEST_PHASE_TIMEOUT_CODES.read,
25
+ 'ECONNABORTED',
26
+ 'ECONNREFUSED',
27
+ 'ECONNRESET',
28
+ 'EHOSTUNREACH',
29
+ 'ENETDOWN',
30
+ 'ENETUNREACH',
31
+ 'ENOTFOUND',
32
+ 'EAI_AGAIN',
33
+ 'ERR_NETWORK',
34
+ 'ETIMEDOUT',
35
+ ]);
36
+
37
+ const createAbortError = () => {
38
+ const error = new Error('httpRequest aborted');
39
+ error.name = 'AbortError';
40
+ return error;
41
+ };
42
+
43
+ const createOverallTimeoutError = (url: string, timeoutMs: number) => {
44
+ const error = new Error(`httpRequest overall timeout: ${url} ${timeoutMs}ms`);
45
+ error.name = 'HttpRequestOverallTimeoutError';
46
+ return error;
47
+ };
48
+
49
+ const createRequestPhaseTimeoutError = (
50
+ url: string,
51
+ phase: keyof typeof REQUEST_PHASE_TIMEOUT_CODES,
52
+ timeoutMs: number
53
+ ) => {
54
+ const error = new Error(`httpRequest ${phase} timeout: ${url} ${timeoutMs}ms`);
55
+ error.name = 'HttpRequestPhaseTimeoutError';
56
+ return Object.assign(error, {
57
+ code: REQUEST_PHASE_TIMEOUT_CODES[phase],
58
+ });
59
+ };
60
+
61
+ const waitForRetry = async (delayMs: number, attempt: number, signal?: AbortSignal) => {
62
+ const backoffMs = delayMs * 2 ** attempt;
63
+ if (backoffMs <= 0) {
64
+ return;
65
+ }
66
+ await new Promise<void>((resolve, reject) => {
67
+ const timeoutTimer = setTimeout(() => {
68
+ signal?.removeEventListener('abort', handleAbort);
69
+ resolve();
70
+ }, backoffMs);
71
+ const handleAbort = () => {
72
+ clearTimeout(timeoutTimer);
73
+ reject(createAbortError());
74
+ };
75
+ if (signal?.aborted) {
76
+ handleAbort();
77
+ return;
78
+ }
79
+ signal?.addEventListener('abort', handleAbort, { once: true });
80
+ });
81
+ };
82
+
83
+ const isRetryableRequestError = (error: unknown) => {
84
+ if (axios.isCancel(error)) {
85
+ return false;
86
+ }
87
+ if (
88
+ typeof error === 'object' &&
89
+ error !== null &&
90
+ 'code' in error &&
91
+ typeof error.code === 'string' &&
92
+ RETRYABLE_NETWORK_ERROR_CODES.has(error.code)
93
+ ) {
94
+ return true;
95
+ }
96
+ if (!axios.isAxiosError(error)) {
97
+ return false;
98
+ }
99
+ const status = error.response?.status;
100
+ if (status !== undefined) {
101
+ return RETRYABLE_HTTP_STATUSES.has(Number(status));
102
+ }
103
+ return false;
104
+ };
105
+
106
+ export const httpRequest = async <T = unknown>(
107
+ url: string,
108
+ type = 'text',
109
+ options: HttpRequestOptions = {}
110
+ ): Promise<T> => {
4
111
  const headers: any = {};
5
112
  if (url.indexOf('ngrok-free.app') > -1) {
6
113
  headers['ngrok-skip-browser-warning'] = true;
7
114
  }
8
- const response = await axios.request({
9
- url,
10
- withCredentials: false,
11
- responseType: type === 'binary' ? 'arraybuffer' : 'json',
12
- headers,
13
- });
14
115
 
15
- if (+response.status === 200) {
16
- if (type === 'json') {
17
- return response.data;
116
+ const timeoutMs =
117
+ Number.isSafeInteger(options.timeoutMs) && Number(options.timeoutMs) > 0
118
+ ? Number(options.timeoutMs)
119
+ : undefined;
120
+ const connectTimeoutMs =
121
+ Number.isSafeInteger(options.connectTimeoutMs) && Number(options.connectTimeoutMs) > 0
122
+ ? Number(options.connectTimeoutMs)
123
+ : undefined;
124
+ const readTimeoutMs =
125
+ Number.isSafeInteger(options.readTimeoutMs) && Number(options.readTimeoutMs) > 0
126
+ ? Number(options.readTimeoutMs)
127
+ : undefined;
128
+ const overallTimeoutMs =
129
+ Number.isSafeInteger(options.overallTimeoutMs) && Number(options.overallTimeoutMs) > 0
130
+ ? Number(options.overallTimeoutMs)
131
+ : undefined;
132
+ const maxRetries =
133
+ Number.isSafeInteger(options.maxRetries) && Number(options.maxRetries) > 0
134
+ ? Math.min(Number(options.maxRetries), MAX_RETRY_COUNT)
135
+ : 0;
136
+ const retryDelayMs =
137
+ Number.isSafeInteger(options.retryDelayMs) && Number(options.retryDelayMs) > 0
138
+ ? Number(options.retryDelayMs)
139
+ : 0;
140
+ const overallTimeoutError = overallTimeoutMs
141
+ ? createOverallTimeoutError(url, overallTimeoutMs)
142
+ : undefined;
143
+ const overallDeadlineAt = overallTimeoutMs ? Date.now() + overallTimeoutMs : undefined;
144
+ const assertWithinOverallDeadline = () => {
145
+ if (overallDeadlineAt !== undefined && overallTimeoutError && Date.now() >= overallDeadlineAt) {
146
+ throw overallTimeoutError;
147
+ }
148
+ };
149
+
150
+ const request = async (attempt: number, signal?: AbortSignal): Promise<T> => {
151
+ assertWithinOverallDeadline();
152
+ if (signal?.aborted) {
153
+ throw createAbortError();
154
+ }
155
+
156
+ let response: AxiosResponse<T> | undefined;
157
+ let requestError: unknown;
158
+ let requestFailed = false;
159
+ let phaseTimeoutError: ReturnType<typeof createRequestPhaseTimeoutError> | undefined;
160
+ let phaseTimeoutTimer: ReturnType<typeof setTimeout> | undefined;
161
+ const hasPhaseDeadline = Boolean(connectTimeoutMs || readTimeoutMs);
162
+ const attemptController = signal || hasPhaseDeadline ? new AbortController() : undefined;
163
+ let attemptSettled = false;
164
+ let lastProgressLoaded = 0;
165
+ const handleParentAbort = () => {
166
+ attemptController?.abort();
167
+ };
168
+ const clearPhaseTimeout = () => {
169
+ if (phaseTimeoutTimer) {
170
+ clearTimeout(phaseTimeoutTimer);
171
+ phaseTimeoutTimer = undefined;
172
+ }
173
+ };
174
+ const armPhaseTimeout = (
175
+ phase: keyof typeof REQUEST_PHASE_TIMEOUT_CODES,
176
+ phaseTimeoutMs: number | undefined
177
+ ) => {
178
+ clearPhaseTimeout();
179
+ if (!phaseTimeoutMs || !attemptController) {
180
+ return;
181
+ }
182
+ phaseTimeoutTimer = setTimeout(() => {
183
+ phaseTimeoutError = createRequestPhaseTimeoutError(url, phase, phaseTimeoutMs);
184
+ attemptController.abort();
185
+ }, phaseTimeoutMs);
186
+ };
187
+
188
+ if (signal) {
189
+ signal.addEventListener('abort', handleParentAbort, { once: true });
190
+ }
191
+ armPhaseTimeout('connect', connectTimeoutMs);
192
+
193
+ try {
194
+ response = await axios.request<T>({
195
+ url,
196
+ withCredentials: false,
197
+ responseType: type === 'binary' ? 'arraybuffer' : 'json',
198
+ headers,
199
+ ...(timeoutMs ? { timeout: timeoutMs } : {}),
200
+ ...(attemptController ? { signal: attemptController.signal } : {}),
201
+ ...(hasPhaseDeadline
202
+ ? {
203
+ adapter: ['xhr', 'http'],
204
+ onDownloadProgress: (progressEvent: { loaded: number }) => {
205
+ if (
206
+ attemptSettled ||
207
+ attemptController?.signal.aborted ||
208
+ !Number.isFinite(progressEvent.loaded) ||
209
+ progressEvent.loaded <= lastProgressLoaded
210
+ ) {
211
+ return;
212
+ }
213
+ lastProgressLoaded = progressEvent.loaded;
214
+ armPhaseTimeout('read', readTimeoutMs);
215
+ },
216
+ }
217
+ : {}),
218
+ });
219
+ if (phaseTimeoutError) {
220
+ requestFailed = true;
221
+ requestError = phaseTimeoutError;
222
+ response = undefined;
223
+ }
224
+ } catch (error) {
225
+ requestFailed = true;
226
+ requestError = phaseTimeoutError ?? (signal?.aborted ? createAbortError() : error);
227
+ } finally {
228
+ attemptSettled = true;
229
+ clearPhaseTimeout();
230
+ signal?.removeEventListener('abort', handleParentAbort);
231
+ }
232
+
233
+ assertWithinOverallDeadline();
234
+
235
+ if (requestFailed) {
236
+ if (attempt >= maxRetries || !isRetryableRequestError(requestError)) {
237
+ throw requestError;
238
+ }
239
+ await waitForRetry(retryDelayMs, attempt, signal);
240
+ assertWithinOverallDeadline();
241
+ return request(attempt + 1, signal);
18
242
  }
19
- if (type === 'binary') {
243
+
244
+ if (!response) {
245
+ throw new Error(`httpRequest completed without a response: ${url}`);
246
+ }
247
+
248
+ if (+response.status === 200) {
20
249
  return response.data;
21
250
  }
22
- return response.data;
251
+
252
+ if (RETRYABLE_HTTP_STATUSES.has(Number(response.status)) && attempt < maxRetries) {
253
+ await waitForRetry(retryDelayMs, attempt, signal);
254
+ assertWithinOverallDeadline();
255
+ return request(attempt + 1, signal);
256
+ }
257
+
258
+ throw new Error(`httpRequest error: ${url} ${response.statusText}`);
259
+ };
260
+
261
+ if (!overallTimeoutMs) {
262
+ return request(0);
263
+ }
264
+
265
+ const controller = new AbortController();
266
+ const requestPromise = request(0, controller.signal);
267
+ let timeoutTimer: ReturnType<typeof setTimeout> | undefined;
268
+ try {
269
+ return await new Promise<T>((resolve, reject) => {
270
+ timeoutTimer = setTimeout(() => {
271
+ reject(overallTimeoutError);
272
+ controller.abort();
273
+ }, overallTimeoutMs);
274
+ requestPromise.then(resolve, reject);
275
+ });
276
+ } finally {
277
+ if (timeoutTimer) {
278
+ clearTimeout(timeoutTimer);
279
+ }
23
280
  }
24
- throw new Error(`httpRequest error: ${url} ${response.statusText}`);
25
281
  };
@@ -1,5 +1,8 @@
1
1
  import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
2
2
 
3
+ // RGB565 pixel packing and dithering index calculation require bitwise operations.
4
+ /* eslint-disable no-bitwise */
5
+
3
6
  export const PRO2_WALLPAPER_WIDTH = 604;
4
7
  export const PRO2_WALLPAPER_HEIGHT = 1024;
5
8
 
@@ -9,19 +12,16 @@ const COLOR_FORMAT_RGB565 = 0x12;
9
12
  const COLOR_FORMAT_RGB565A8 = 0x14;
10
13
 
11
14
  const RED_THRESHOLD = [
12
- 1, 7, 3, 5, 0, 8, 2, 6, 7, 1, 5, 3, 8, 0, 6, 2, 3, 5, 0, 8, 2, 6, 1, 7, 5, 3, 8,
13
- 0, 6, 2, 7, 1, 0, 8, 2, 6, 1, 7, 3, 5, 8, 0, 6, 2, 7, 1, 5, 3, 2, 6, 1, 7, 3, 5, 0,
14
- 8, 6, 2, 7, 1, 5, 3, 8, 0,
15
+ 1, 7, 3, 5, 0, 8, 2, 6, 7, 1, 5, 3, 8, 0, 6, 2, 3, 5, 0, 8, 2, 6, 1, 7, 5, 3, 8, 0, 6, 2, 7, 1, 0,
16
+ 8, 2, 6, 1, 7, 3, 5, 8, 0, 6, 2, 7, 1, 5, 3, 2, 6, 1, 7, 3, 5, 0, 8, 6, 2, 7, 1, 5, 3, 8, 0,
15
17
  ];
16
18
  const GREEN_THRESHOLD = [
17
- 1, 3, 2, 2, 3, 1, 2, 2, 2, 2, 0, 4, 2, 2, 4, 0, 3, 1, 2, 2, 1, 3, 2, 2, 2, 2, 4,
18
- 0, 2, 2, 0, 4, 1, 3, 2, 2, 3, 1, 2, 2, 2, 2, 0, 4, 2, 2, 4, 0, 3, 1, 2, 2, 1, 3, 2,
19
- 2, 2, 2, 4, 0, 2, 2, 0, 4,
19
+ 1, 3, 2, 2, 3, 1, 2, 2, 2, 2, 0, 4, 2, 2, 4, 0, 3, 1, 2, 2, 1, 3, 2, 2, 2, 2, 4, 0, 2, 2, 0, 4, 1,
20
+ 3, 2, 2, 3, 1, 2, 2, 2, 2, 0, 4, 2, 2, 4, 0, 3, 1, 2, 2, 1, 3, 2, 2, 2, 2, 4, 0, 2, 2, 0, 4,
20
21
  ];
21
22
  const BLUE_THRESHOLD = [
22
- 5, 3, 8, 0, 6, 2, 7, 1, 3, 5, 0, 8, 2, 6, 1, 7, 8, 0, 6, 2, 7, 1, 5, 3, 0, 8, 2,
23
- 6, 1, 7, 3, 5, 6, 2, 7, 1, 5, 3, 8, 0, 2, 6, 1, 7, 3, 5, 0, 8, 7, 1, 5, 3, 8, 0, 6,
24
- 2, 1, 7, 3, 5, 0, 8, 2, 6,
23
+ 5, 3, 8, 0, 6, 2, 7, 1, 3, 5, 0, 8, 2, 6, 1, 7, 8, 0, 6, 2, 7, 1, 5, 3, 0, 8, 2, 6, 1, 7, 3, 5, 6,
24
+ 2, 7, 1, 5, 3, 8, 0, 2, 6, 1, 7, 3, 5, 0, 8, 7, 1, 5, 3, 8, 0, 6, 2, 1, 7, 3, 5, 0, 8, 2, 6,
25
25
  ];
26
26
 
27
27
  function invalidParameter(message: string): Error {
@@ -86,10 +86,8 @@ export function encodePro2Wallpaper(options: {
86
86
  const sourceOffset = (y * width + x) * 4;
87
87
  const thresholdIndex = ((y & 7) << 3) + (x & 7);
88
88
  let red = Math.min(rgba[sourceOffset] + RED_THRESHOLD[thresholdIndex], 0xff) & 0xf8;
89
- let green =
90
- Math.min(rgba[sourceOffset + 1] + GREEN_THRESHOLD[thresholdIndex], 0xff) & 0xfc;
91
- let blue =
92
- Math.min(rgba[sourceOffset + 2] + BLUE_THRESHOLD[thresholdIndex], 0xff) & 0xf8;
89
+ let green = Math.min(rgba[sourceOffset + 1] + GREEN_THRESHOLD[thresholdIndex], 0xff) & 0xfc;
90
+ let blue = Math.min(rgba[sourceOffset + 2] + BLUE_THRESHOLD[thresholdIndex], 0xff) & 0xf8;
93
91
  if (!hasTransparency) {
94
92
  const alpha = rgba[sourceOffset + 3];
95
93
  red = (red * alpha) >> 8;
@@ -164,8 +164,8 @@ export function completeRequestContext(responseID: number, error?: Error): void
164
164
  context.status = error ? 'error' : 'success';
165
165
  if (error) {
166
166
  context.error = error.message;
167
- // 方法错误已经通过标准 API 响应返回给调用方。请求追踪属于调试信息,
168
- // 不应再以 console.error 级别制造第二份面向用户的错误输出。
167
+ // Method errors already reach callers through the standard API response.
168
+ // Request tracing is diagnostic and must not emit a second user-facing error.
169
169
  Log.debug(
170
170
  `[RequestContext] [completeRequestContext] Error: ${formatRequestContext(context)}`
171
171
  );
@@ -1,4 +0,0 @@
1
- export { default as deviceInfoGet } from './protocol-v2/DeviceInfoGet';
2
- export { default as deviceStatusGet } from './protocol-v2/DeviceStatusGet';
3
- export { default as deviceSettingsGet } from './protocol-v2/DeviceSettingsGet';
4
- //# sourceMappingURL=internal.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../../src/api/internal.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,6BAA6B,CAAC;AACvE,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAC3E,OAAO,EAAE,OAAO,IAAI,iBAAiB,EAAE,MAAM,iCAAiC,CAAC"}
@@ -1,28 +0,0 @@
1
- import { BaseMethod } from '../BaseMethod';
2
- import type { DeviceAttachPinOnDevice, DeviceHostPassphrase, DevicePassphraseOnDevice, DeviceSessionResume } from '@onekeyfe/hd-transport';
3
- type HiddenWalletSelection = {
4
- host_passphrase: DeviceHostPassphrase;
5
- passphrase_on_device?: never;
6
- attach_pin_on_device?: never;
7
- } | {
8
- host_passphrase?: never;
9
- passphrase_on_device: DevicePassphraseOnDevice;
10
- attach_pin_on_device?: never;
11
- } | {
12
- host_passphrase?: never;
13
- passphrase_on_device?: never;
14
- attach_pin_on_device: DeviceAttachPinOnDevice;
15
- };
16
- export type DeviceSessionOpenParams = {
17
- resume: DeviceSessionResume;
18
- select?: never;
19
- } | {
20
- resume?: never;
21
- select: HiddenWalletSelection;
22
- };
23
- export default class DeviceSessionOpen extends BaseMethod<DeviceSessionOpenParams> {
24
- init(): void;
25
- run(): Promise<import("@onekeyfe/hd-transport").DeviceSession>;
26
- }
27
- export {};
28
- //# sourceMappingURL=DeviceSessionOpen.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"DeviceSessionOpen.d.ts","sourceRoot":"","sources":["../../../src/api/protocol-v2/DeviceSessionOpen.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAG3C,OAAO,KAAK,EACV,uBAAuB,EACvB,oBAAoB,EACpB,wBAAwB,EACxB,mBAAmB,EACpB,MAAM,wBAAwB,CAAC;AAEhC,KAAK,qBAAqB,GACtB;IACE,eAAe,EAAE,oBAAoB,CAAC;IACtC,oBAAoB,CAAC,EAAE,KAAK,CAAC;IAC7B,oBAAoB,CAAC,EAAE,KAAK,CAAC;CAC9B,GACD;IACE,eAAe,CAAC,EAAE,KAAK,CAAC;IACxB,oBAAoB,EAAE,wBAAwB,CAAC;IAC/C,oBAAoB,CAAC,EAAE,KAAK,CAAC;CAC9B,GACD;IACE,eAAe,CAAC,EAAE,KAAK,CAAC;IACxB,oBAAoB,CAAC,EAAE,KAAK,CAAC;IAC7B,oBAAoB,EAAE,uBAAuB,CAAC;CAC/C,CAAC;AAEN,MAAM,MAAM,uBAAuB,GAC/B;IAAE,MAAM,EAAE,mBAAmB,CAAC;IAAC,MAAM,CAAC,EAAE,KAAK,CAAA;CAAE,GAC/C;IAAE,MAAM,CAAC,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,qBAAqB,CAAA;CAAE,CAAC;AAsDtD,MAAM,CAAC,OAAO,OAAO,iBAAkB,SAAQ,UAAU,CAAC,uBAAuB,CAAC;IAChF,IAAI;IAOE,GAAG;CAQV"}
@@ -1,9 +0,0 @@
1
- /**
2
- * SDK 内部原生命令注册表。
3
- *
4
- * 这些方法允许开发工具通过低层 `call` 调试 Protocol V2,但不会进入
5
- * `CoreApi` 或公开的便捷方法集合;业务接入仍应使用统一 DeviceState API。
6
- */
7
- export { default as deviceInfoGet } from './protocol-v2/DeviceInfoGet';
8
- export { default as deviceStatusGet } from './protocol-v2/DeviceStatusGet';
9
- export { default as deviceSettingsGet } from './protocol-v2/DeviceSettingsGet';
@@ -1,100 +0,0 @@
1
- import { BaseMethod } from '../BaseMethod';
2
- import { invalidParameter } from '../helpers/filesystemValidation';
3
-
4
- import type {
5
- DeviceAttachPinOnDevice,
6
- DeviceHostPassphrase,
7
- DevicePassphraseOnDevice,
8
- DeviceSessionResume,
9
- } from '@onekeyfe/hd-transport';
10
-
11
- type HiddenWalletSelection =
12
- | {
13
- host_passphrase: DeviceHostPassphrase;
14
- passphrase_on_device?: never;
15
- attach_pin_on_device?: never;
16
- }
17
- | {
18
- host_passphrase?: never;
19
- passphrase_on_device: DevicePassphraseOnDevice;
20
- attach_pin_on_device?: never;
21
- }
22
- | {
23
- host_passphrase?: never;
24
- passphrase_on_device?: never;
25
- attach_pin_on_device: DeviceAttachPinOnDevice;
26
- };
27
-
28
- export type DeviceSessionOpenParams =
29
- | { resume: DeviceSessionResume; select?: never }
30
- | { resume?: never; select: HiddenWalletSelection };
31
-
32
- const isRecord = (value: unknown): value is Record<string, unknown> =>
33
- !!value && typeof value === 'object' && !Array.isArray(value);
34
-
35
- const normalizeParams = (payload: Record<string, unknown>): DeviceSessionOpenParams => {
36
- const hasResume = payload.resume !== undefined;
37
- const hasSelect = payload.select !== undefined;
38
- if (hasResume === hasSelect) {
39
- throw invalidParameter('DeviceSessionOpen requires exactly one of [resume] or [select].');
40
- }
41
-
42
- if (hasResume) {
43
- if (!isRecord(payload.resume) || typeof payload.resume.session_id !== 'string') {
44
- throw invalidParameter('Parameter [resume.session_id] must be a non-empty string.');
45
- }
46
- const sessionId = payload.resume.session_id.trim();
47
- if (!sessionId) {
48
- throw invalidParameter('Parameter [resume.session_id] must be a non-empty string.');
49
- }
50
- return { resume: { session_id: sessionId } };
51
- }
52
-
53
- if (!isRecord(payload.select)) {
54
- throw invalidParameter('Parameter [select] must be a hidden-wallet selection object.');
55
- }
56
- const { select } = payload;
57
- const accessKeys = ['host_passphrase', 'passphrase_on_device', 'attach_pin_on_device'] as const;
58
- const selected = accessKeys.filter(key => select[key] !== undefined);
59
- const unknown = Object.keys(select).filter(
60
- key => !accessKeys.includes(key as (typeof accessKeys)[number])
61
- );
62
- if (unknown.length > 0 || selected.length !== 1) {
63
- throw invalidParameter('Parameter [select] requires exactly one hidden-wallet access mode.');
64
- }
65
-
66
- if (selected[0] === 'host_passphrase') {
67
- const host = select.host_passphrase;
68
- if (!isRecord(host) || typeof host.passphrase !== 'string') {
69
- throw invalidParameter('Parameter [select.host_passphrase.passphrase] is required.');
70
- }
71
- const passphrase = host.passphrase.normalize('NFKD');
72
- if (!passphrase || passphrase.includes('\0') || passphrase.length > 50) {
73
- throw invalidParameter('Parameter [select.host_passphrase.passphrase] is invalid.');
74
- }
75
- return { select: { host_passphrase: { passphrase } } };
76
- }
77
-
78
- if (selected[0] === 'passphrase_on_device') {
79
- return { select: { passphrase_on_device: {} } };
80
- }
81
- return { select: { attach_pin_on_device: {} } };
82
- };
83
-
84
- export default class DeviceSessionOpen extends BaseMethod<DeviceSessionOpenParams> {
85
- init() {
86
- this.requireProtocolV2 = true;
87
- this.useDevicePassphraseState = false;
88
- this.skipForceUpdateCheck = true;
89
- this.params = normalizeParams(this.payload as unknown as Record<string, unknown>);
90
- }
91
-
92
- async run() {
93
- const { message } = await this.device.commands.typedCall(
94
- 'DeviceSessionOpen',
95
- 'DeviceSession',
96
- this.params
97
- );
98
- return message;
99
- }
100
- }