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

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 (141) hide show
  1. package/__tests__/device-features-state.test.ts +78 -0
  2. package/__tests__/device-identity.test.ts +30 -0
  3. package/__tests__/device-pool-state.test.ts +113 -0
  4. package/__tests__/device-settings.test.ts +108 -0
  5. package/__tests__/device-state-contract.test.ts +27 -0
  6. package/__tests__/device-state-events.test.ts +163 -0
  7. package/__tests__/device-state-mapper.test.ts +222 -0
  8. package/__tests__/device-state-projector.test.ts +94 -0
  9. package/__tests__/device-state-store.test.ts +115 -0
  10. package/__tests__/device-wallet-session-store.test.ts +46 -0
  11. package/__tests__/deviceFeaturesUtils.test.ts +13 -1
  12. package/__tests__/deviceSettings.test.ts +54 -0
  13. package/__tests__/get-device-state.test.ts +367 -0
  14. package/__tests__/logBlockEvent.test.ts +13 -0
  15. package/__tests__/protocol-v2.test.ts +358 -413
  16. package/__tests__/public-device-state-api.test.ts +153 -0
  17. package/__tests__/refresh-device-state.test.ts +101 -0
  18. package/__tests__/request-context-logging.test.ts +16 -0
  19. package/__tests__/search-devices.test.ts +93 -0
  20. package/dist/api/ClearSessionCache.d.ts.map +1 -1
  21. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  22. package/dist/api/GetDeviceState.d.ts +7 -0
  23. package/dist/api/GetDeviceState.d.ts.map +1 -0
  24. package/dist/api/GetFeatures.d.ts +1 -1
  25. package/dist/api/GetFeatures.d.ts.map +1 -1
  26. package/dist/api/GetOnekeyFeatures.d.ts.map +1 -1
  27. package/dist/api/GetPassphraseState.d.ts.map +1 -1
  28. package/dist/api/SearchDevices.d.ts.map +1 -1
  29. package/dist/api/device/DeviceRebootToBoardloader.d.ts.map +1 -1
  30. package/dist/api/device/DeviceRebootToBootloader.d.ts.map +1 -1
  31. package/dist/api/device/DeviceSettings.d.ts.map +1 -1
  32. package/dist/api/index.d.ts +1 -4
  33. package/dist/api/index.d.ts.map +1 -1
  34. package/dist/api/internal.d.ts +4 -0
  35. package/dist/api/internal.d.ts.map +1 -0
  36. package/dist/api/protocol-v2/DeviceInfoGet.d.ts +0 -1
  37. package/dist/api/protocol-v2/DeviceInfoGet.d.ts.map +1 -1
  38. package/dist/api/protocol-v2/DeviceReboot.d.ts.map +1 -1
  39. package/dist/api/protocol-v2/DeviceSettingsGet.d.ts.map +1 -1
  40. package/dist/api/protocol-v2/DeviceSettingsSet.d.ts.map +1 -1
  41. package/dist/api/protocol-v2/DeviceStatusGet.d.ts.map +1 -1
  42. package/dist/api/utils.d.ts +2 -1
  43. package/dist/api/utils.d.ts.map +1 -1
  44. package/dist/constants/index.d.ts +1 -1
  45. package/dist/constants/index.d.ts.map +1 -1
  46. package/dist/core/index.d.ts.map +1 -1
  47. package/dist/device/Device.d.ts +22 -3
  48. package/dist/device/Device.d.ts.map +1 -1
  49. package/dist/device/DeviceFeaturesState.d.ts +4 -0
  50. package/dist/device/DeviceFeaturesState.d.ts.map +1 -0
  51. package/dist/device/DevicePool.d.ts +1 -0
  52. package/dist/device/DevicePool.d.ts.map +1 -1
  53. package/dist/device/DeviceStateMapper.d.ts +13 -0
  54. package/dist/device/DeviceStateMapper.d.ts.map +1 -0
  55. package/dist/device/DeviceStateProjector.d.ts +8 -0
  56. package/dist/device/DeviceStateProjector.d.ts.map +1 -0
  57. package/dist/device/DeviceStateStore.d.ts +23 -0
  58. package/dist/device/DeviceStateStore.d.ts.map +1 -0
  59. package/dist/device/deviceIdentity.d.ts +5 -0
  60. package/dist/device/deviceIdentity.d.ts.map +1 -0
  61. package/dist/deviceProfile/buildDeviceFeatures.d.ts.map +1 -1
  62. package/dist/deviceProfile/index.d.ts +0 -1
  63. package/dist/deviceProfile/index.d.ts.map +1 -1
  64. package/dist/events/device.d.ts +7 -2
  65. package/dist/events/device.d.ts.map +1 -1
  66. package/dist/index.d.ts +688 -639
  67. package/dist/index.js +1274 -942
  68. package/dist/inject.d.ts.map +1 -1
  69. package/dist/protocols/protocol-v2/features.d.ts +1 -0
  70. package/dist/protocols/protocol-v2/features.d.ts.map +1 -1
  71. package/dist/protocols/protocol-v2/index.d.ts +1 -1
  72. package/dist/protocols/protocol-v2/index.d.ts.map +1 -1
  73. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  74. package/dist/types/api/deviceSettings.d.ts +8 -0
  75. package/dist/types/api/deviceSettings.d.ts.map +1 -1
  76. package/dist/types/api/getDeviceState.d.ts +12 -0
  77. package/dist/types/api/getDeviceState.d.ts.map +1 -0
  78. package/dist/types/api/index.d.ts +4 -7
  79. package/dist/types/api/index.d.ts.map +1 -1
  80. package/dist/types/api/protocolV2.d.ts +1 -6
  81. package/dist/types/api/protocolV2.d.ts.map +1 -1
  82. package/dist/types/device.d.ts +121 -1
  83. package/dist/types/device.d.ts.map +1 -1
  84. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  85. package/dist/utils/deviceSettings.d.ts.map +1 -1
  86. package/dist/utils/patch.d.ts +1 -1
  87. package/dist/utils/tracing.d.ts.map +1 -1
  88. package/package.json +4 -4
  89. package/src/api/ClearSessionCache.ts +4 -0
  90. package/src/api/FirmwareUpdateV4.ts +15 -7
  91. package/src/api/GetDeviceState.ts +55 -0
  92. package/src/api/GetFeatures.ts +8 -3
  93. package/src/api/GetOnekeyFeatures.ts +3 -72
  94. package/src/api/GetPassphraseState.ts +3 -1
  95. package/src/api/SearchDevices.ts +25 -4
  96. package/src/api/device/DeviceRebootToBoardloader.ts +1 -0
  97. package/src/api/device/DeviceRebootToBootloader.ts +1 -0
  98. package/src/api/device/DeviceSettings.ts +28 -0
  99. package/src/api/index.ts +1 -4
  100. package/src/api/internal.ts +9 -0
  101. package/src/api/protocol-v2/DeviceInfoGet.ts +3 -5
  102. package/src/api/protocol-v2/DeviceReboot.ts +3 -1
  103. package/src/api/protocol-v2/DeviceSettingsGet.ts +3 -1
  104. package/src/api/protocol-v2/DeviceSettingsSet.ts +2 -0
  105. package/src/api/protocol-v2/DeviceStatusGet.ts +1 -0
  106. package/src/api/utils.ts +9 -3
  107. package/src/constants/index.ts +1 -4
  108. package/src/core/index.ts +7 -1
  109. package/src/data/messages/messages-protocol-v2.json +22 -13
  110. package/src/device/Device.ts +256 -78
  111. package/src/device/DeviceFeaturesState.ts +34 -0
  112. package/src/device/DevicePool.ts +35 -9
  113. package/src/device/DeviceStateMapper.ts +364 -0
  114. package/src/device/DeviceStateProjector.ts +92 -0
  115. package/src/device/DeviceStateStore.ts +242 -0
  116. package/src/device/deviceIdentity.ts +18 -0
  117. package/src/deviceProfile/buildDeviceFeatures.ts +166 -109
  118. package/src/deviceProfile/index.ts +0 -1
  119. package/src/events/device.ts +13 -1
  120. package/src/events/logBlockEvent.ts +1 -1
  121. package/src/inject.ts +4 -5
  122. package/src/protocols/protocol-v2/features.ts +18 -9
  123. package/src/protocols/protocol-v2/index.ts +1 -0
  124. package/src/protocols/protocol-v2/walletSession.ts +5 -7
  125. package/src/types/api/deviceSettings.ts +8 -0
  126. package/src/types/api/getDeviceState.ts +19 -0
  127. package/src/types/api/index.ts +3 -20
  128. package/src/types/api/protocolV2.ts +0 -24
  129. package/src/types/device.ts +148 -1
  130. package/src/utils/deviceFeaturesUtils.ts +5 -1
  131. package/src/utils/deviceSettings.ts +41 -0
  132. package/src/utils/tracing.ts +3 -1
  133. package/dist/api/GetDeviceInfo.d.ts +0 -9
  134. package/dist/api/GetDeviceInfo.d.ts.map +0 -1
  135. package/dist/deviceProfile/buildDeviceProfile.d.ts +0 -22
  136. package/dist/deviceProfile/buildDeviceProfile.d.ts.map +0 -1
  137. package/dist/types/api/getDeviceInfo.d.ts +0 -88
  138. package/dist/types/api/getDeviceInfo.d.ts.map +0 -1
  139. package/src/api/GetDeviceInfo.ts +0 -152
  140. package/src/deviceProfile/buildDeviceProfile.ts +0 -370
  141. package/src/types/api/getDeviceInfo.ts +0 -106
@@ -1,152 +0,0 @@
1
- import semver from 'semver';
2
- import { EDeviceType, ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
3
-
4
- import { UI_REQUEST } from '../constants/ui-request';
5
- import {
6
- PROTOCOL_V2_DEVICE_INFO_REQUEST,
7
- PROTOCOL_V2_FEATURES_DEVICE_INFO_REQUEST,
8
- PROTOCOL_V2_VERSIONS_DEVICE_INFO_REQUEST,
9
- } from '../protocols/protocol-v2';
10
- import {
11
- requestProtocolV2DeviceInfo,
12
- requestProtocolV2DeviceStatus,
13
- } from '../protocols/protocol-v2/features';
14
- import { buildProfileFromProtocolV1, buildProfileFromProtocolV2 } from '../deviceProfile';
15
- import { getDeviceType } from '../utils';
16
- import { fixVersion } from '../utils/deviceFeaturesUtils';
17
- import { BaseMethod } from './BaseMethod';
18
-
19
- import type {
20
- DeviceInfoScope,
21
- DeviceInfoSource,
22
- GetDeviceInfoParams,
23
- } from '../types/api/getDeviceInfo';
24
- import type { Features, OnekeyFeatures } from '../types';
25
-
26
- const DEVICE_INFO_SCOPES: readonly DeviceInfoScope[] = ['basic', 'versions', 'verify', 'full'];
27
-
28
- function isDeviceInfoScope(scope: unknown): scope is DeviceInfoScope {
29
- return typeof scope === 'string' && DEVICE_INFO_SCOPES.includes(scope as DeviceInfoScope);
30
- }
31
-
32
- function normalizeScope(scope: unknown): GetDeviceInfoParams['scope'] {
33
- if (scope === undefined || scope === null) return 'basic';
34
- if (isDeviceInfoScope(scope)) {
35
- return scope;
36
- }
37
- throw ERRORS.TypedError(
38
- HardwareErrorCode.CallMethodInvalidParameter,
39
- `Invalid getDeviceInfo scope: ${String(scope)}`
40
- );
41
- }
42
-
43
- function resolveProtocolV2DeviceInfoRequest(params: GetDeviceInfoParams) {
44
- if (params.scope === 'verify' || params.scope === 'full') {
45
- return PROTOCOL_V2_DEVICE_INFO_REQUEST;
46
- }
47
- if (params.scope === 'versions') {
48
- return PROTOCOL_V2_VERSIONS_DEVICE_INFO_REQUEST;
49
- }
50
- return PROTOCOL_V2_FEATURES_DEVICE_INFO_REQUEST;
51
- }
52
-
53
- function shouldReadOnekeyFeatures(params: GetDeviceInfoParams) {
54
- return (
55
- params.includeRaw === true ||
56
- params.scope === 'versions' ||
57
- params.scope === 'verify' ||
58
- params.scope === 'full'
59
- );
60
- }
61
-
62
- function supportOnekeyFeatures(features?: Features) {
63
- if (!features || features.bootloaderMode) return false;
64
-
65
- const deviceType = getDeviceType(features);
66
- return ![
67
- EDeviceType.Unknown,
68
- EDeviceType.Classic1s,
69
- EDeviceType.ClassicPure,
70
- EDeviceType.Pro2,
71
- ].includes(deviceType);
72
- }
73
-
74
- function normalizeOnekeyFeatures(message: OnekeyFeatures) {
75
- if (message.onekey_firmware_version && !semver.valid(message.onekey_firmware_version)) {
76
- message.onekey_firmware_version = fixVersion(message.onekey_firmware_version);
77
- }
78
- return message;
79
- }
80
-
81
- export default class GetDeviceInfo extends BaseMethod<GetDeviceInfoParams> {
82
- init() {
83
- this.allowDeviceMode = [
84
- ...this.allowDeviceMode,
85
- UI_REQUEST.NOT_INITIALIZE,
86
- UI_REQUEST.BOOTLOADER,
87
- ];
88
- this.useDevicePassphraseState = false;
89
- this.skipForceUpdateCheck = true;
90
- this.params = {
91
- scope: normalizeScope(this.payload.scope),
92
- refresh: this.payload.refresh,
93
- includeRaw: this.payload.includeRaw,
94
- };
95
- }
96
-
97
- async run() {
98
- if (this.device.isProtocolV2()) {
99
- return this.runProtocolV2();
100
- }
101
- return this.runProtocolV1();
102
- }
103
-
104
- private async runProtocolV2() {
105
- const sources: DeviceInfoSource[] = ['deviceInfo'];
106
- const protocolV2DeviceInfo = await requestProtocolV2DeviceInfo({
107
- commands: this.device.commands,
108
- request: resolveProtocolV2DeviceInfoRequest(this.params),
109
- });
110
- const protocolV2DeviceStatus = await requestProtocolV2DeviceStatus({
111
- commands: this.device.commands,
112
- }).catch(() => undefined);
113
- const profile = buildProfileFromProtocolV2({
114
- deviceInfo: protocolV2DeviceInfo,
115
- deviceStatus: protocolV2DeviceStatus,
116
- sources,
117
- scope: this.params.scope,
118
- includeRaw: this.params.includeRaw,
119
- });
120
- this.device.updateProtocolV2Features(protocolV2DeviceInfo, protocolV2DeviceStatus);
121
- return profile;
122
- }
123
-
124
- private async runProtocolV1() {
125
- if (this.params.refresh === true) {
126
- await this.device.getFeatures();
127
- }
128
-
129
- const sources: DeviceInfoSource[] = ['features'];
130
- const { features } = this.device;
131
- let protocolV1OneKeyFeatures: OnekeyFeatures | undefined;
132
-
133
- if (shouldReadOnekeyFeatures(this.params) && supportOnekeyFeatures(features)) {
134
- const { message } = await this.device.commands.typedCall(
135
- 'OnekeyGetFeatures',
136
- 'OnekeyFeatures'
137
- );
138
- protocolV1OneKeyFeatures = normalizeOnekeyFeatures(message);
139
- sources.push('protocolV1OneKeyFeatures');
140
- }
141
-
142
- const profile = buildProfileFromProtocolV1({
143
- protocol: 'V1',
144
- features,
145
- protocolV1OneKeyFeatures,
146
- sources,
147
- scope: this.params.scope,
148
- includeRaw: this.params.includeRaw,
149
- });
150
- return profile;
151
- }
152
- }
@@ -1,370 +0,0 @@
1
- import { EDeviceType, EFirmwareType } from '@onekeyfe/hd-shared';
2
-
3
- import {
4
- getDeviceBLEFirmwareVersion,
5
- getDeviceBoardloaderVersion,
6
- getDeviceBootloaderVersion,
7
- getDeviceFirmwareVersion,
8
- } from '../utils/deviceVersionUtils';
9
- import {
10
- getDeviceBleName,
11
- getDeviceLabel,
12
- getDeviceType,
13
- getDeviceUUID,
14
- getFirmwareType,
15
- } from '../utils/deviceInfoUtils';
16
- import {
17
- isProtocolV2BootloaderDeviceInfo,
18
- isProtocolV2RomloaderDeviceInfo,
19
- } from '../protocols/protocol-v2/features';
20
-
21
- import type {
22
- DeviceInfoProtocol,
23
- DeviceInfoSource,
24
- DeviceInfoStatus,
25
- DeviceProfile,
26
- DeviceProfileRaw,
27
- DeviceProfileVerify,
28
- DeviceProfileVersions,
29
- GetDeviceInfoParams,
30
- } from '../types/api/getDeviceInfo';
31
- import type { Features, OnekeyFeatures } from '../types';
32
- import type {
33
- DeviceFirmwareImageInfo,
34
- DeviceStatus,
35
- ProtocolV2DeviceInfo,
36
- } from '@onekeyfe/hd-transport';
37
-
38
- type BuildProtocolV1ProfileParams = {
39
- protocol?: DeviceInfoProtocol;
40
- features?: Features;
41
- protocolV1OneKeyFeatures?: OnekeyFeatures;
42
- sources?: DeviceInfoSource[];
43
- scope?: GetDeviceInfoParams['scope'];
44
- includeRaw?: boolean;
45
- };
46
-
47
- type BuildProtocolV2ProfileParams = {
48
- deviceInfo?: ProtocolV2DeviceInfo;
49
- deviceStatus?: DeviceStatus;
50
- sources?: DeviceInfoSource[];
51
- scope?: GetDeviceInfoParams['scope'];
52
- includeRaw?: boolean;
53
- };
54
-
55
- const isMeaningfulVersion = (version?: string | null) => Boolean(version && version !== '0.0.0');
56
-
57
- const firstMeaningfulVersion = (...versions: Array<string | null | undefined>) =>
58
- versions.find(isMeaningfulVersion) ?? null;
59
-
60
- const versionArrayToString = (version?: Array<number | string | null> | null) => {
61
- if (!version || version.length === 0) return null;
62
- const value = version.join('.');
63
- return isMeaningfulVersion(value) ? value : null;
64
- };
65
-
66
- const bytesToHex = (value: unknown): string | undefined => {
67
- if (!value) return undefined;
68
- if (typeof value === 'string') return value;
69
- if (value instanceof Uint8Array) {
70
- return Array.from(value)
71
- .map(byte => byte.toString(16).padStart(2, '0'))
72
- .join('');
73
- }
74
- if (Array.isArray(value)) {
75
- return value.map(byte => Number(byte).toString(16).padStart(2, '0')).join('');
76
- }
77
- return undefined;
78
- };
79
-
80
- const getImageVersion = (image?: DeviceFirmwareImageInfo | null) => image?.version ?? null;
81
-
82
- const getImageBuildId = (image?: DeviceFirmwareImageInfo | null) => image?.build_id ?? undefined;
83
-
84
- const getImageHash = (image?: DeviceFirmwareImageInfo | null) => bytesToHex(image?.hash);
85
-
86
- const shouldIncludeVerify = (scope?: GetDeviceInfoParams['scope']) =>
87
- scope === 'verify' || scope === 'full';
88
-
89
- const getDeviceMode = (features?: Features): DeviceInfoStatus['mode'] => {
90
- if (!features) return 'unknown';
91
- if (features.bootloaderMode === true) return 'bootloader';
92
- if (features.initialized === false) return 'notInitialized';
93
- if (features.initialized === true) return 'normal';
94
- return 'unknown';
95
- };
96
-
97
- const getProtocolV2Mode = (
98
- deviceInfo?: ProtocolV2DeviceInfo,
99
- deviceStatus?: DeviceStatus
100
- ): DeviceInfoStatus['mode'] => {
101
- if (isProtocolV2RomloaderDeviceInfo(deviceInfo, deviceStatus)) return 'romloader';
102
- if (isProtocolV2BootloaderDeviceInfo(deviceInfo, deviceStatus)) return 'bootloader';
103
- const initialized = deviceStatus?.init_states;
104
- if (initialized === false) return 'notInitialized';
105
- if (initialized === true) return 'normal';
106
- return 'unknown';
107
- };
108
-
109
- const normalizeV1Versions = (
110
- features?: Features,
111
- protocolV1OneKeyFeatures?: OnekeyFeatures
112
- ): DeviceProfileVersions => ({
113
- firmware: firstMeaningfulVersion(
114
- protocolV1OneKeyFeatures?.onekey_firmware_version,
115
- versionArrayToString(getDeviceFirmwareVersion(features))
116
- ),
117
- bootloader: firstMeaningfulVersion(
118
- protocolV1OneKeyFeatures?.onekey_boot_version,
119
- versionArrayToString(getDeviceBootloaderVersion(features))
120
- ),
121
- board: firstMeaningfulVersion(
122
- protocolV1OneKeyFeatures?.onekey_board_version,
123
- versionArrayToString(features ? getDeviceBoardloaderVersion(features) : undefined)
124
- ),
125
- ble: firstMeaningfulVersion(
126
- protocolV1OneKeyFeatures?.onekey_ble_version,
127
- features?.bleVersion,
128
- versionArrayToString(features ? getDeviceBLEFirmwareVersion(features) : undefined)
129
- ),
130
- se01: firstMeaningfulVersion(
131
- protocolV1OneKeyFeatures?.onekey_se01_version,
132
- features?.se01Version
133
- ),
134
- se02: firstMeaningfulVersion(
135
- protocolV1OneKeyFeatures?.onekey_se02_version,
136
- features?.se02Version
137
- ),
138
- se03: firstMeaningfulVersion(
139
- protocolV1OneKeyFeatures?.onekey_se03_version,
140
- features?.se03Version
141
- ),
142
- se04: firstMeaningfulVersion(
143
- protocolV1OneKeyFeatures?.onekey_se04_version,
144
- features?.se04Version
145
- ),
146
- se01Boot: firstMeaningfulVersion(
147
- protocolV1OneKeyFeatures?.onekey_se01_boot_version,
148
- features?.se01BootVersion
149
- ),
150
- se02Boot: firstMeaningfulVersion(
151
- protocolV1OneKeyFeatures?.onekey_se02_boot_version,
152
- features?.se02BootVersion
153
- ),
154
- se03Boot: firstMeaningfulVersion(
155
- protocolV1OneKeyFeatures?.onekey_se03_boot_version,
156
- features?.se03BootVersion
157
- ),
158
- se04Boot: firstMeaningfulVersion(
159
- protocolV1OneKeyFeatures?.onekey_se04_boot_version,
160
- features?.se04BootVersion
161
- ),
162
- });
163
-
164
- const normalizeV2Versions = (deviceInfo?: ProtocolV2DeviceInfo): DeviceProfileVersions => {
165
- const info = deviceInfo;
166
- return {
167
- firmware: firstMeaningfulVersion(getImageVersion(info?.fw?.application)),
168
- bootloader: firstMeaningfulVersion(getImageVersion(info?.fw?.bootloader)),
169
- board: firstMeaningfulVersion(getImageVersion(info?.fw?.romloader)),
170
- ble: firstMeaningfulVersion(getImageVersion(info?.coprocessor?.application)),
171
- se01: firstMeaningfulVersion(getImageVersion(info?.se1?.application)),
172
- se02: firstMeaningfulVersion(getImageVersion(info?.se2?.application)),
173
- se03: firstMeaningfulVersion(getImageVersion(info?.se3?.application)),
174
- se04: firstMeaningfulVersion(getImageVersion(info?.se4?.application)),
175
- se01Boot: firstMeaningfulVersion(getImageVersion(info?.se1?.bootloader)),
176
- se02Boot: firstMeaningfulVersion(getImageVersion(info?.se2?.bootloader)),
177
- se03Boot: firstMeaningfulVersion(getImageVersion(info?.se3?.bootloader)),
178
- se04Boot: firstMeaningfulVersion(getImageVersion(info?.se4?.bootloader)),
179
- };
180
- };
181
-
182
- // V2 状态由 normalizeV2Status 处理,这里只服务 buildProfileFromProtocolV1 的 V1 路径。
183
- const normalizeV1Status = (features?: Features): DeviceInfoStatus => ({
184
- mode: getDeviceMode(features),
185
- initialized: features?.initialized ?? null,
186
- bootloaderMode: features?.bootloaderMode ?? null,
187
- unlocked: features?.unlocked ?? null,
188
- passphraseProtection: features?.passphraseProtection ?? null,
189
- attachToPinEnabled: features?.attachToPinEnabled ?? null,
190
- unlockedAttachPin: features?.unlockedAttachPin ?? null,
191
- backupRequired: features?.backupRequired ?? null,
192
- noBackup: features?.noBackup ?? null,
193
- language: features?.language ?? null,
194
- bleEnabled: features?.bleEnabled ?? null,
195
- });
196
-
197
- const normalizeV2Status = (
198
- deviceInfo?: ProtocolV2DeviceInfo,
199
- deviceStatus?: DeviceStatus
200
- ): DeviceInfoStatus => {
201
- const status = deviceStatus;
202
- const bootloaderMode = isProtocolV2BootloaderDeviceInfo(deviceInfo, deviceStatus);
203
- return {
204
- mode: getProtocolV2Mode(deviceInfo, deviceStatus),
205
- initialized: status?.init_states ?? null,
206
- bootloaderMode,
207
- unlocked: status?.unlocked ?? null,
208
- passphraseProtection: status?.passphrase_enabled ?? null,
209
- attachToPinEnabled: status?.attach_to_pin_enabled ?? null,
210
- unlockedAttachPin: status?.unlocked_by_attach_to_pin ?? null,
211
- backupRequired: status?.backup_required ?? null,
212
- noBackup: null,
213
- language: null,
214
- bleEnabled: null,
215
- };
216
- };
217
-
218
- const normalizeV1Verify = (
219
- features?: Features,
220
- protocolV1OneKeyFeatures?: OnekeyFeatures
221
- ): DeviceProfileVerify => ({
222
- firmwareBuildId:
223
- protocolV1OneKeyFeatures?.onekey_firmware_build_id ?? features?.verify?.firmwareBuildId,
224
- firmwareHash: protocolV1OneKeyFeatures?.onekey_firmware_hash ?? features?.verify?.firmwareHash,
225
- bootloaderBuildId:
226
- protocolV1OneKeyFeatures?.onekey_boot_build_id ?? features?.verify?.bootloaderBuildId,
227
- bootloaderHash: protocolV1OneKeyFeatures?.onekey_boot_hash ?? features?.verify?.bootloaderHash,
228
- boardBuildId: protocolV1OneKeyFeatures?.onekey_board_build_id ?? features?.verify?.boardBuildId,
229
- boardHash: protocolV1OneKeyFeatures?.onekey_board_hash ?? features?.verify?.boardHash,
230
- bleBuildId: protocolV1OneKeyFeatures?.onekey_ble_build_id ?? features?.verify?.bleBuildId,
231
- bleHash: protocolV1OneKeyFeatures?.onekey_ble_hash ?? features?.verify?.bleHash,
232
- se01BuildId: protocolV1OneKeyFeatures?.onekey_se01_build_id ?? features?.verify?.se01BuildId,
233
- se01Hash: protocolV1OneKeyFeatures?.onekey_se01_hash ?? features?.verify?.se01Hash,
234
- se02BuildId: protocolV1OneKeyFeatures?.onekey_se02_build_id ?? features?.verify?.se02BuildId,
235
- se02Hash: protocolV1OneKeyFeatures?.onekey_se02_hash ?? features?.verify?.se02Hash,
236
- se03BuildId: protocolV1OneKeyFeatures?.onekey_se03_build_id ?? features?.verify?.se03BuildId,
237
- se03Hash: protocolV1OneKeyFeatures?.onekey_se03_hash ?? features?.verify?.se03Hash,
238
- se04BuildId: protocolV1OneKeyFeatures?.onekey_se04_build_id ?? features?.verify?.se04BuildId,
239
- se04Hash: protocolV1OneKeyFeatures?.onekey_se04_hash ?? features?.verify?.se04Hash,
240
- se01BootBuildId:
241
- protocolV1OneKeyFeatures?.onekey_se01_boot_build_id ?? features?.verify?.se01BootBuildId,
242
- se01BootHash: protocolV1OneKeyFeatures?.onekey_se01_boot_hash ?? features?.verify?.se01BootHash,
243
- se02BootBuildId:
244
- protocolV1OneKeyFeatures?.onekey_se02_boot_build_id ?? features?.verify?.se02BootBuildId,
245
- se02BootHash: protocolV1OneKeyFeatures?.onekey_se02_boot_hash ?? features?.verify?.se02BootHash,
246
- se03BootBuildId:
247
- protocolV1OneKeyFeatures?.onekey_se03_boot_build_id ?? features?.verify?.se03BootBuildId,
248
- se03BootHash: protocolV1OneKeyFeatures?.onekey_se03_boot_hash ?? features?.verify?.se03BootHash,
249
- se04BootBuildId:
250
- protocolV1OneKeyFeatures?.onekey_se04_boot_build_id ?? features?.verify?.se04BootBuildId,
251
- se04BootHash: protocolV1OneKeyFeatures?.onekey_se04_boot_hash ?? features?.verify?.se04BootHash,
252
- });
253
-
254
- const normalizeV2Verify = (deviceInfo?: ProtocolV2DeviceInfo): DeviceProfileVerify => {
255
- const info = deviceInfo;
256
- return {
257
- firmwareBuildId: getImageBuildId(info?.fw?.application),
258
- firmwareHash: getImageHash(info?.fw?.application),
259
- bootloaderBuildId: getImageBuildId(info?.fw?.bootloader),
260
- bootloaderHash: getImageHash(info?.fw?.bootloader),
261
- boardBuildId: getImageBuildId(info?.fw?.romloader),
262
- boardHash: getImageHash(info?.fw?.romloader),
263
- bleBuildId: getImageBuildId(info?.coprocessor?.application),
264
- bleHash: getImageHash(info?.coprocessor?.application),
265
- se01BuildId: getImageBuildId(info?.se1?.application),
266
- se01Hash: getImageHash(info?.se1?.application),
267
- se02BuildId: getImageBuildId(info?.se2?.application),
268
- se02Hash: getImageHash(info?.se2?.application),
269
- se03BuildId: getImageBuildId(info?.se3?.application),
270
- se03Hash: getImageHash(info?.se3?.application),
271
- se04BuildId: getImageBuildId(info?.se4?.application),
272
- se04Hash: getImageHash(info?.se4?.application),
273
- se01BootBuildId: getImageBuildId(info?.se1?.bootloader),
274
- se01BootHash: getImageHash(info?.se1?.bootloader),
275
- se02BootBuildId: getImageBuildId(info?.se2?.bootloader),
276
- se02BootHash: getImageHash(info?.se2?.bootloader),
277
- se03BootBuildId: getImageBuildId(info?.se3?.bootloader),
278
- se03BootHash: getImageHash(info?.se3?.bootloader),
279
- se04BootBuildId: getImageBuildId(info?.se4?.bootloader),
280
- se04BootHash: getImageHash(info?.se4?.bootloader),
281
- };
282
- };
283
-
284
- const normalizeRaw = ({
285
- features,
286
- protocolV1OneKeyFeatures,
287
- protocolV2DeviceInfo,
288
- protocolV2DeviceStatus,
289
- }: {
290
- features?: Features;
291
- protocolV1OneKeyFeatures?: OnekeyFeatures;
292
- protocolV2DeviceInfo?: ProtocolV2DeviceInfo;
293
- protocolV2DeviceStatus?: DeviceStatus;
294
- }): DeviceProfileRaw => ({
295
- ...(features ? { features } : {}),
296
- ...(protocolV1OneKeyFeatures ? { protocolV1OneKeyFeatures } : {}),
297
- ...(protocolV2DeviceInfo ? { protocolV2DeviceInfo } : {}),
298
- ...(protocolV2DeviceStatus ? { protocolV2DeviceStatus } : {}),
299
- });
300
-
301
- export function buildProfileFromProtocolV1({
302
- protocol = 'V1',
303
- features,
304
- protocolV1OneKeyFeatures,
305
- sources = ['features'],
306
- scope = 'basic',
307
- includeRaw = false,
308
- }: BuildProtocolV1ProfileParams): DeviceProfile {
309
- const sourceFeatures = features;
310
- const verify = normalizeV1Verify(sourceFeatures, protocolV1OneKeyFeatures);
311
-
312
- return {
313
- protocol,
314
- sources,
315
- deviceType: getDeviceType(sourceFeatures),
316
- firmwareType: getFirmwareType(sourceFeatures),
317
- deviceId: sourceFeatures?.deviceId || (sourceFeatures ? getDeviceUUID(sourceFeatures) : ''),
318
- serialNo: sourceFeatures ? getDeviceUUID(sourceFeatures) : '',
319
- label: getDeviceLabel(sourceFeatures),
320
- bleName: getDeviceBleName(sourceFeatures),
321
- status: normalizeV1Status(sourceFeatures),
322
- versions: normalizeV1Versions(sourceFeatures, protocolV1OneKeyFeatures),
323
- ...(shouldIncludeVerify(scope) ? { verify } : {}),
324
- ...(includeRaw
325
- ? {
326
- raw: normalizeRaw({
327
- features,
328
- protocolV1OneKeyFeatures,
329
- }),
330
- }
331
- : {}),
332
- };
333
- }
334
-
335
- export function buildProfileFromProtocolV2({
336
- deviceInfo,
337
- deviceStatus,
338
- sources = ['deviceInfo'],
339
- scope = 'basic',
340
- includeRaw = false,
341
- }: BuildProtocolV2ProfileParams): DeviceProfile {
342
- const info = deviceInfo;
343
- const deviceId = deviceStatus?.device_id || '';
344
- const serialNo = deviceInfo?.hw?.serial_no || '';
345
- const label = null;
346
- const bleName = info?.coprocessor?.bt_adv_name ?? null;
347
- const verify = normalizeV2Verify(deviceInfo);
348
-
349
- return {
350
- protocol: 'V2',
351
- sources,
352
- deviceType: EDeviceType.Pro2,
353
- firmwareType: EFirmwareType.Universal,
354
- deviceId,
355
- serialNo,
356
- label,
357
- bleName,
358
- status: normalizeV2Status(deviceInfo, deviceStatus),
359
- versions: normalizeV2Versions(deviceInfo),
360
- ...(shouldIncludeVerify(scope) ? { verify } : {}),
361
- ...(includeRaw
362
- ? {
363
- raw: normalizeRaw({
364
- protocolV2DeviceInfo: deviceInfo,
365
- protocolV2DeviceStatus: deviceStatus,
366
- }),
367
- }
368
- : {}),
369
- };
370
- }
@@ -1,106 +0,0 @@
1
- import type { CommonParams, Response } from '../params';
2
- import type { Features, IDeviceType, OnekeyFeatures } from '../device';
3
- import type { EFirmwareType } from '@onekeyfe/hd-shared';
4
- import type {
5
- DeviceStatus,
6
- ProtocolType,
7
- ProtocolV2DeviceInfo,
8
- } from '@onekeyfe/hd-transport';
9
-
10
- // 协议类型单源:从 hd-transport 的 ProtocolType 派生,只额外允许 'unknown'。
11
- export type DeviceInfoProtocol = ProtocolType | 'unknown';
12
-
13
- export type DeviceInfoSource = 'features' | 'protocolV1OneKeyFeatures' | 'deviceInfo';
14
-
15
- export type DeviceInfoScope = 'basic' | 'versions' | 'verify' | 'full';
16
-
17
- export type GetDeviceInfoParams = {
18
- scope?: DeviceInfoScope;
19
- refresh?: boolean;
20
- includeRaw?: boolean;
21
- };
22
-
23
- export type DeviceInfoMode = 'normal' | 'bootloader' | 'romloader' | 'notInitialized' | 'unknown';
24
-
25
- export type DeviceInfoStatus = {
26
- mode: DeviceInfoMode;
27
- initialized: boolean | null;
28
- bootloaderMode: boolean | null;
29
- unlocked: boolean | null;
30
- passphraseProtection: boolean | null;
31
- attachToPinEnabled?: boolean | null;
32
- unlockedAttachPin?: boolean | null;
33
- backupRequired: boolean | null;
34
- noBackup: boolean | null;
35
- language: string | null;
36
- bleEnabled: boolean | null;
37
- };
38
-
39
- export type DeviceProfileVersions = {
40
- firmware: string | null;
41
- bootloader: string | null;
42
- board: string | null;
43
- ble: string | null;
44
- se01?: string | null;
45
- se02?: string | null;
46
- se03?: string | null;
47
- se04?: string | null;
48
- se01Boot?: string | null;
49
- se02Boot?: string | null;
50
- se03Boot?: string | null;
51
- se04Boot?: string | null;
52
- };
53
-
54
- export type DeviceProfileVerify = {
55
- firmwareBuildId?: string;
56
- firmwareHash?: string;
57
- bootloaderBuildId?: string;
58
- bootloaderHash?: string;
59
- boardBuildId?: string;
60
- boardHash?: string;
61
- bleBuildId?: string;
62
- bleHash?: string;
63
- se01BuildId?: string;
64
- se01Hash?: string;
65
- se02BuildId?: string;
66
- se02Hash?: string;
67
- se03BuildId?: string;
68
- se03Hash?: string;
69
- se04BuildId?: string;
70
- se04Hash?: string;
71
- se01BootBuildId?: string;
72
- se01BootHash?: string;
73
- se02BootBuildId?: string;
74
- se02BootHash?: string;
75
- se03BootBuildId?: string;
76
- se03BootHash?: string;
77
- se04BootBuildId?: string;
78
- se04BootHash?: string;
79
- };
80
-
81
- export type DeviceProfileRaw = {
82
- features?: Features;
83
- protocolV1OneKeyFeatures?: OnekeyFeatures;
84
- protocolV2DeviceInfo?: ProtocolV2DeviceInfo;
85
- protocolV2DeviceStatus?: DeviceStatus;
86
- };
87
-
88
- export type DeviceProfile = {
89
- protocol: DeviceInfoProtocol;
90
- sources: DeviceInfoSource[];
91
- deviceType: IDeviceType;
92
- firmwareType: EFirmwareType;
93
- deviceId: string;
94
- serialNo: string;
95
- label: string | null;
96
- bleName: string | null;
97
- status: DeviceInfoStatus;
98
- versions: DeviceProfileVersions;
99
- verify?: DeviceProfileVerify;
100
- raw?: DeviceProfileRaw;
101
- };
102
-
103
- export declare function getDeviceInfo(
104
- connectId?: string,
105
- params?: CommonParams & GetDeviceInfoParams
106
- ): Response<DeviceProfile>;