@onekeyfe/hd-core 1.1.26 → 1.1.27-alpha.31

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 (186) hide show
  1. package/__tests__/evmSignTransaction.test.ts +1 -1
  2. package/__tests__/evmSignTypedData.test.ts +1 -1
  3. package/__tests__/protocol-v2.test.ts +1096 -0
  4. package/dist/api/BaseMethod.d.ts +1 -3
  5. package/dist/api/BaseMethod.d.ts.map +1 -1
  6. package/dist/api/DirList.d.ts +10 -0
  7. package/dist/api/DirList.d.ts.map +1 -0
  8. package/dist/api/DirMake.d.ts +9 -0
  9. package/dist/api/DirMake.d.ts.map +1 -0
  10. package/dist/api/DirRemove.d.ts +9 -0
  11. package/dist/api/DirRemove.d.ts.map +1 -0
  12. package/dist/api/FileDelete.d.ts +9 -0
  13. package/dist/api/FileDelete.d.ts.map +1 -0
  14. package/dist/api/FileRead.d.ts +19 -0
  15. package/dist/api/FileRead.d.ts.map +1 -0
  16. package/dist/api/FileWrite.d.ts +23 -0
  17. package/dist/api/FileWrite.d.ts.map +1 -0
  18. package/dist/api/FirmwareUpdateV3.d.ts +1 -0
  19. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  20. package/dist/api/FirmwareUpdateV4.d.ts +32 -0
  21. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -0
  22. package/dist/api/GetOnekeyFeatures.d.ts.map +1 -1
  23. package/dist/api/GetPassphraseState.d.ts +6 -1
  24. package/dist/api/GetPassphraseState.d.ts.map +1 -1
  25. package/dist/api/PathInfo.d.ts +9 -0
  26. package/dist/api/PathInfo.d.ts.map +1 -0
  27. package/dist/api/SearchDevices.d.ts +2 -1
  28. package/dist/api/SearchDevices.d.ts.map +1 -1
  29. package/dist/api/allnetwork/AllNetworkGetAddressBase.d.ts.map +1 -1
  30. package/dist/api/conflux/ConfluxSignTransaction.d.ts.map +1 -1
  31. package/dist/api/device/DeviceRebootToBoardloader.d.ts +1 -1
  32. package/dist/api/device/DeviceRebootToBoardloader.d.ts.map +1 -1
  33. package/dist/api/device/DeviceRebootToBootloader.d.ts.map +1 -1
  34. package/dist/api/dynex/DnxGetAddress.d.ts.map +1 -1
  35. package/dist/api/dynex/DnxSignTransaction.d.ts.map +1 -1
  36. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +10 -2
  37. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  38. package/dist/api/helpers/batchGetPublickeys.d.ts.map +1 -1
  39. package/dist/api/index.d.ts +26 -0
  40. package/dist/api/index.d.ts.map +1 -1
  41. package/dist/api/protocol-v2/DeviceFirmwareUpdate.d.ts +7 -0
  42. package/dist/api/protocol-v2/DeviceFirmwareUpdate.d.ts.map +1 -0
  43. package/dist/api/protocol-v2/DeviceGetDeviceInfo.d.ts +7 -0
  44. package/dist/api/protocol-v2/DeviceGetDeviceInfo.d.ts.map +1 -0
  45. package/dist/api/protocol-v2/DeviceGetFirmwareUpdateStatus.d.ts +6 -0
  46. package/dist/api/protocol-v2/DeviceGetFirmwareUpdateStatus.d.ts.map +1 -0
  47. package/dist/api/protocol-v2/DeviceGetOnboardingStatus.d.ts +6 -0
  48. package/dist/api/protocol-v2/DeviceGetOnboardingStatus.d.ts.map +1 -0
  49. package/dist/api/protocol-v2/DeviceReboot.d.ts +7 -0
  50. package/dist/api/protocol-v2/DeviceReboot.d.ts.map +1 -0
  51. package/dist/api/protocol-v2/FactoryDeviceInfoSettings.d.ts +7 -0
  52. package/dist/api/protocol-v2/FactoryDeviceInfoSettings.d.ts.map +1 -0
  53. package/dist/api/protocol-v2/FactoryGetDeviceInfo.d.ts +6 -0
  54. package/dist/api/protocol-v2/FactoryGetDeviceInfo.d.ts.map +1 -0
  55. package/dist/api/protocol-v2/FilesystemFixPermission.d.ts +6 -0
  56. package/dist/api/protocol-v2/FilesystemFixPermission.d.ts.map +1 -0
  57. package/dist/api/protocol-v2/FilesystemFormat.d.ts +6 -0
  58. package/dist/api/protocol-v2/FilesystemFormat.d.ts.map +1 -0
  59. package/dist/api/protocol-v2/GetProtoVersion.d.ts +6 -0
  60. package/dist/api/protocol-v2/GetProtoVersion.d.ts.map +1 -0
  61. package/dist/api/protocol-v2/Ping.d.ts +8 -0
  62. package/dist/api/protocol-v2/Ping.d.ts.map +1 -0
  63. package/dist/api/protocol-v2/helpers.d.ts +49 -0
  64. package/dist/api/protocol-v2/helpers.d.ts.map +1 -0
  65. package/dist/core/index.d.ts.map +1 -1
  66. package/dist/data-manager/DataManager.d.ts +7 -4
  67. package/dist/data-manager/DataManager.d.ts.map +1 -1
  68. package/dist/data-manager/MessagesConfig.d.ts +2 -2
  69. package/dist/data-manager/MessagesConfig.d.ts.map +1 -1
  70. package/dist/data-manager/TransportManager.d.ts +5 -4
  71. package/dist/data-manager/TransportManager.d.ts.map +1 -1
  72. package/dist/device/Device.d.ts +5 -3
  73. package/dist/device/Device.d.ts.map +1 -1
  74. package/dist/device/DeviceCommands.d.ts +8 -8
  75. package/dist/device/DeviceCommands.d.ts.map +1 -1
  76. package/dist/device/DeviceConnector.d.ts +2 -1
  77. package/dist/device/DeviceConnector.d.ts.map +1 -1
  78. package/dist/events/ui-request.d.ts +8 -0
  79. package/dist/events/ui-request.d.ts.map +1 -1
  80. package/dist/index.d.ts +198 -23
  81. package/dist/index.js +15726 -806
  82. package/dist/inject.d.ts.map +1 -1
  83. package/dist/protocols/protocol-v2/features.d.ts +57 -0
  84. package/dist/protocols/protocol-v2/features.d.ts.map +1 -0
  85. package/dist/protocols/protocol-v2/firmware.d.ts +12 -0
  86. package/dist/protocols/protocol-v2/firmware.d.ts.map +1 -0
  87. package/dist/protocols/protocol-v2/index.d.ts +3 -0
  88. package/dist/protocols/protocol-v2/index.d.ts.map +1 -0
  89. package/dist/types/api/export.d.ts +1 -1
  90. package/dist/types/api/export.d.ts.map +1 -1
  91. package/dist/types/api/firmwareUpdate.d.ts +7 -0
  92. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  93. package/dist/types/api/getPassphraseState.d.ts +7 -1
  94. package/dist/types/api/getPassphraseState.d.ts.map +1 -1
  95. package/dist/types/api/index.d.ts +29 -1
  96. package/dist/types/api/index.d.ts.map +1 -1
  97. package/dist/types/api/protocolV2.d.ts +123 -0
  98. package/dist/types/api/protocolV2.d.ts.map +1 -0
  99. package/dist/types/api/searchDevices.d.ts +2 -2
  100. package/dist/types/api/searchDevices.d.ts.map +1 -1
  101. package/dist/types/device.d.ts +1 -1
  102. package/dist/types/device.d.ts.map +1 -1
  103. package/dist/types/params.d.ts +2 -0
  104. package/dist/types/params.d.ts.map +1 -1
  105. package/dist/types/settings.d.ts +1 -1
  106. package/dist/types/settings.d.ts.map +1 -1
  107. package/dist/utils/deviceFeaturesUtils.d.ts +3 -3
  108. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  109. package/dist/utils/deviceInfoUtils.d.ts +1 -0
  110. package/dist/utils/deviceInfoUtils.d.ts.map +1 -1
  111. package/dist/utils/index.d.ts +1 -1
  112. package/dist/utils/index.d.ts.map +1 -1
  113. package/dist/utils/patch.d.ts +1 -1
  114. package/dist/utils/patch.d.ts.map +1 -1
  115. package/dist/utils/versionUtils.d.ts +1 -1
  116. package/package.json +4 -4
  117. package/src/api/BaseMethod.ts +12 -60
  118. package/src/api/DirList.ts +25 -0
  119. package/src/api/DirMake.ts +20 -0
  120. package/src/api/DirRemove.ts +20 -0
  121. package/src/api/FileDelete.ts +20 -0
  122. package/src/api/FileRead.ts +158 -0
  123. package/src/api/FileWrite.ts +191 -0
  124. package/src/api/FirmwareUpdateV3.ts +21 -4
  125. package/src/api/FirmwareUpdateV4.ts +810 -0
  126. package/src/api/GetOnekeyFeatures.ts +75 -3
  127. package/src/api/GetPassphraseState.ts +14 -2
  128. package/src/api/PathInfo.ts +24 -0
  129. package/src/api/SearchDevices.ts +7 -2
  130. package/src/api/allnetwork/AllNetworkGetAddressBase.ts +25 -9
  131. package/src/api/conflux/ConfluxSignTransaction.ts +5 -2
  132. package/src/api/device/DeviceRebootToBoardloader.ts +10 -1
  133. package/src/api/device/DeviceRebootToBootloader.ts +10 -1
  134. package/src/api/dynex/DnxGetAddress.ts +7 -0
  135. package/src/api/dynex/DnxSignTransaction.ts +7 -0
  136. package/src/api/evm/EVMGetAddress.ts +1 -1
  137. package/src/api/evm/EVMGetPublicKey.ts +1 -1
  138. package/src/api/evm/EVMSignMessage.ts +1 -1
  139. package/src/api/evm/EVMSignTransaction.ts +1 -1
  140. package/src/api/evm/EVMSignTypedData.ts +6 -6
  141. package/src/api/evm/EVMVerifyMessage.ts +1 -1
  142. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +27 -4
  143. package/src/api/helpers/batchGetPublickeys.ts +4 -2
  144. package/src/api/index.ts +28 -0
  145. package/src/api/protocol-v2/DeviceFirmwareUpdate.ts +33 -0
  146. package/src/api/protocol-v2/DeviceGetDeviceInfo.ts +35 -0
  147. package/src/api/protocol-v2/DeviceGetFirmwareUpdateStatus.ts +18 -0
  148. package/src/api/protocol-v2/DeviceGetOnboardingStatus.ts +18 -0
  149. package/src/api/protocol-v2/DeviceReboot.ts +22 -0
  150. package/src/api/protocol-v2/FactoryDeviceInfoSettings.ts +27 -0
  151. package/src/api/protocol-v2/FactoryGetDeviceInfo.ts +18 -0
  152. package/src/api/protocol-v2/FilesystemFixPermission.ts +14 -0
  153. package/src/api/protocol-v2/FilesystemFormat.ts +14 -0
  154. package/src/api/protocol-v2/GetProtoVersion.ts +14 -0
  155. package/src/api/protocol-v2/Ping.ts +16 -0
  156. package/src/api/protocol-v2/helpers.ts +138 -0
  157. package/src/api/tron/TronSignMessage.ts +1 -1
  158. package/src/api/xrp/XrpSignTransaction.ts +1 -1
  159. package/src/core/index.ts +31 -5
  160. package/src/data/messages/messages-pro2.json +13106 -0
  161. package/src/data-manager/DataManager.ts +12 -7
  162. package/src/data-manager/MessagesConfig.ts +14 -14
  163. package/src/data-manager/TransportManager.ts +38 -12
  164. package/src/device/Device.ts +73 -9
  165. package/src/device/DeviceCommands.ts +162 -26
  166. package/src/device/DeviceConnector.ts +29 -4
  167. package/src/device/DevicePool.ts +1 -1
  168. package/src/events/ui-request.ts +8 -0
  169. package/src/inject.ts +42 -1
  170. package/src/protocols/protocol-v2/features.ts +267 -0
  171. package/src/protocols/protocol-v2/firmware.ts +26 -0
  172. package/src/protocols/protocol-v2/index.ts +2 -0
  173. package/src/types/api/export.ts +1 -0
  174. package/src/types/api/firmwareUpdate.ts +12 -0
  175. package/src/types/api/getPassphraseState.ts +10 -1
  176. package/src/types/api/index.ts +64 -1
  177. package/src/types/api/protocolV2.ts +221 -0
  178. package/src/types/api/searchDevices.ts +2 -2
  179. package/src/types/device.ts +3 -1
  180. package/src/types/params.ts +7 -0
  181. package/src/types/settings.ts +1 -1
  182. package/src/utils/deviceFeaturesUtils.ts +33 -15
  183. package/src/utils/deviceInfoUtils.ts +14 -5
  184. package/src/utils/index.ts +1 -0
  185. package/__tests__/DeviceCommands.test.ts +0 -99
  186. package/__tests__/evmLedgerLegacySafety.test.ts +0 -261
@@ -4,6 +4,7 @@ import { EDeviceType, EFirmwareType } from '@onekeyfe/hd-shared';
4
4
 
5
5
  import MessagesJSON from '../data/messages/messages.json';
6
6
  import MessagesLegacyV1JSON from '../data/messages/messages_legacy_v1.json';
7
+ import MessagesPro2JSON from '../data/messages/messages-pro2.json';
7
8
  import {
8
9
  LoggerNames,
9
10
  getDeviceBLEFirmwareVersion,
@@ -40,7 +41,8 @@ export const FIRMWARE_FIELDS = [
40
41
 
41
42
  export type IFirmwareField = (typeof FIRMWARE_FIELDS)[number];
42
43
 
43
- export type MessageVersion = 'latest' | 'v1';
44
+ export type ProtocolV1MessageSchema = 'protocolV1Current' | 'protocolV1Legacy';
45
+ export type ProtobufMessageSchema = ProtocolV1MessageSchema | 'protocolV2';
44
46
 
45
47
  const FIRMWARE_FIELD_TYPE_MAP: Readonly<Record<IFirmwareField, EFirmwareType>> = {
46
48
  firmware: EFirmwareType.Universal,
@@ -62,7 +64,9 @@ function getFirmwareTypeFromField(firmwareField: IFirmwareField): EFirmwareType
62
64
  }
63
65
 
64
66
  export default class DataManager {
65
- static deviceMap: DeviceTypeMap = {
67
+ static deviceMap: DeviceTypeMap & {
68
+ [k: string]: DeviceTypeMap[keyof DeviceTypeMap] | undefined;
69
+ } = {
66
70
  [EDeviceType.Classic]: {
67
71
  firmware: [],
68
72
  ble: [],
@@ -93,9 +97,10 @@ export default class DataManager {
93
97
 
94
98
  static settings: ConnectSettings;
95
99
 
96
- static messages: { [version in MessageVersion]: JSON } = {
97
- latest: MessagesJSON as unknown as JSON,
98
- v1: MessagesLegacyV1JSON as unknown as JSON,
100
+ static messages: { [schema in ProtobufMessageSchema]: JSON } = {
101
+ protocolV1Current: MessagesJSON as unknown as JSON,
102
+ protocolV1Legacy: MessagesLegacyV1JSON as unknown as JSON,
103
+ protocolV2: MessagesPro2JSON as unknown as JSON,
99
104
  };
100
105
 
101
106
  static lastCheckTimestamp = 0;
@@ -472,8 +477,8 @@ export default class DataManager {
472
477
  }
473
478
  }
474
479
 
475
- static getProtobufMessages(messageVersion: MessageVersion = 'latest'): JSON {
476
- return this.messages[messageVersion];
480
+ static getProtobufMessages(schema: ProtobufMessageSchema = 'protocolV1Current'): JSON {
481
+ return this.messages[schema];
477
482
  }
478
483
 
479
484
  static getSettings(key?: undefined): ConnectSettings;
@@ -1,28 +1,28 @@
1
1
  import type { IDeviceModel, IDeviceType } from '../types';
2
- import type { MessageVersion } from './DataManager';
2
+ import type { ProtocolV1MessageSchema } from './DataManager';
3
3
 
4
4
  type DeviceVersionConfig = {
5
5
  [deviceType in IDeviceType | IDeviceModel]?: {
6
6
  minVersion: string;
7
- messageVersion: MessageVersion;
7
+ protocolV1MessageSchema: ProtocolV1MessageSchema;
8
8
  }[];
9
9
  };
10
10
 
11
11
  export const PROTOBUF_MESSAGE_CONFIG: DeviceVersionConfig = {
12
12
  model_mini: [
13
- // Classic1s starts from 3.5.0, so use latest by default
14
- // Only use v1 for specific old versions (< 3.3.0)
15
- { minVersion: '3.3.0', messageVersion: 'latest' },
16
- { minVersion: '0.0.1', messageVersion: 'v1' },
17
- // Fallback to latest for unknown versions (0.0.0) to prevent device type detection issues
18
- { minVersion: '0.0.0', messageVersion: 'latest' },
13
+ // Classic1s starts from 3.5.0, so use the current Protocol V1 schema by default.
14
+ // Only use the legacy Protocol V1 schema for specific old versions (< 3.3.0).
15
+ { minVersion: '3.3.0', protocolV1MessageSchema: 'protocolV1Current' },
16
+ { minVersion: '0.0.1', protocolV1MessageSchema: 'protocolV1Legacy' },
17
+ // Fallback to current Protocol V1 schema for unknown versions (0.0.0).
18
+ { minVersion: '0.0.0', protocolV1MessageSchema: 'protocolV1Current' },
19
19
  ],
20
20
  model_touch: [
21
- // Use latest by default for Touch/Pro
22
- // Only use v1 for specific old versions (< 4.5.0)
23
- { minVersion: '4.5.0', messageVersion: 'latest' },
24
- { minVersion: '0.0.1', messageVersion: 'v1' },
25
- // Fallback to latest for unknown versions (0.0.0)
26
- { minVersion: '0.0.0', messageVersion: 'latest' },
21
+ // Use the current Protocol V1 schema by default for Touch/Pro.
22
+ // Only use the legacy Protocol V1 schema for specific old versions (< 4.5.0).
23
+ { minVersion: '4.5.0', protocolV1MessageSchema: 'protocolV1Current' },
24
+ { minVersion: '0.0.1', protocolV1MessageSchema: 'protocolV1Legacy' },
25
+ // Fallback to current Protocol V1 schema for unknown versions (0.0.0).
26
+ { minVersion: '0.0.0', protocolV1MessageSchema: 'protocolV1Current' },
27
27
  ],
28
28
  };
@@ -4,9 +4,9 @@ import DataManager from './DataManager';
4
4
  import { LoggerNames, getLogger } from '../utils';
5
5
  // eslint-disable-next-line import/no-cycle
6
6
  import { DevicePool } from '../device/DevicePool';
7
- import { getSupportMessageVersion } from '../utils/deviceFeaturesUtils';
7
+ import { getSupportProtocolV1MessageSchema } from '../utils/deviceFeaturesUtils';
8
8
 
9
- import type { MessageVersion } from './DataManager';
9
+ import type { ProtocolV1MessageSchema } from './DataManager';
10
10
  import type { LowlevelTransportSharedPlugin, Transport } from '@onekeyfe/hd-transport';
11
11
  import type { Features } from '../types';
12
12
 
@@ -17,6 +17,7 @@ const LowLevelLogger = getLogger(LoggerNames.HdTransportLowLevel);
17
17
  const NodeUsbLogger = getLogger(LoggerNames.HdTransportNodeUsb);
18
18
  const WebBleLogger = getLogger(LoggerNames.HdWebBleTransport);
19
19
  const WebUsbLogger = getLogger(LoggerNames.HdTransportWebUsb);
20
+ const REACT_NATIVE_BLE_SCAN_TIMEOUT_MS = 8000;
20
21
 
21
22
  /**
22
23
  * transport 在同一个环境中只会存在一个
@@ -32,7 +33,7 @@ export default class TransportManager {
32
33
 
33
34
  static reactNativeInit = false;
34
35
 
35
- static messageVersion: MessageVersion = 'latest';
36
+ static protocolV1MessageSchema: ProtocolV1MessageSchema = 'protocolV1Current';
36
37
 
37
38
  static plugin: LowlevelTransportSharedPlugin | null = null;
38
39
 
@@ -40,7 +41,7 @@ export default class TransportManager {
40
41
  Log.debug('transport manager load');
41
42
  this.defaultMessages = DataManager.getProtobufMessages();
42
43
  this.currentMessages = this.defaultMessages;
43
- this.messageVersion = 'latest';
44
+ this.protocolV1MessageSchema = 'protocolV1Current';
44
45
  }
45
46
 
46
47
  static async configure() {
@@ -74,6 +75,9 @@ export default class TransportManager {
74
75
  }
75
76
  Log.debug('Configuring transports');
76
77
  await this.transport.configure(JSON.stringify(this.defaultMessages));
78
+ this.currentMessages = this.defaultMessages;
79
+ this.protocolV1MessageSchema = 'protocolV1Current';
80
+ await this.configureProtocolV2Messages();
77
81
  Log.debug('Configuring transports done');
78
82
  } catch (error) {
79
83
  Log.debug('Initializing transports error: ', error);
@@ -83,20 +87,31 @@ export default class TransportManager {
83
87
  }
84
88
  }
85
89
 
86
- static async reconfigure(features?: Features | undefined) {
87
- Log.debug(`Begin reconfiguring transports`);
88
- const { messageVersion, messages } = getSupportMessageVersion(features);
90
+ /**
91
+ * Re-load the transport's main protobuf schema based on a device's reported features.
92
+ *
93
+ * This handles protobuf schema compatibility within Protocol V1 (e.g. Touch's legacy
94
+ * vs current Protocol V1 schema). It is NOT used to switch between Protocol V1 and Protocol V2 —
95
+ * the transport already holds both schemas after initial configure(), and routes per
96
+ * device by `getProtocolType()`.
97
+ */
98
+ static async reconfigure(features?: Features) {
99
+ if (!features) {
100
+ return;
101
+ }
102
+
103
+ const { protocolV1MessageSchema, messages } = getSupportProtocolV1MessageSchema(features);
89
104
 
90
105
  if (this.currentMessages === messages || !messages) {
91
106
  return;
92
107
  }
93
108
 
94
- Log.debug(`Reconfiguring transports version:${messageVersion}`);
109
+ Log.debug(`Reconfiguring transports Protocol V1 schema:${protocolV1MessageSchema}`);
95
110
 
96
111
  try {
97
112
  await this.transport.configure(JSON.stringify(messages));
98
113
  this.currentMessages = messages;
99
- this.messageVersion = messageVersion;
114
+ this.protocolV1MessageSchema = protocolV1MessageSchema;
100
115
  } catch (error) {
101
116
  throw ERRORS.TypedError(
102
117
  HardwareErrorCode.TransportInvalidProtobuf,
@@ -109,7 +124,9 @@ export default class TransportManager {
109
124
  const env = DataManager.getSettings('env');
110
125
  if (env === 'react-native') {
111
126
  /** Actually initializes the ReactNativeTransport */
112
- this.transport = new TransportConstructor({ scanTimeout: 3000 }) as unknown as Transport;
127
+ this.transport = new TransportConstructor({
128
+ scanTimeout: REACT_NATIVE_BLE_SCAN_TIMEOUT_MS,
129
+ }) as unknown as Transport;
113
130
  } else {
114
131
  /** Actually initializes the HttpTransport */
115
132
  this.transport = new TransportConstructor() as unknown as Transport;
@@ -130,6 +147,15 @@ export default class TransportManager {
130
147
  return this.transport;
131
148
  }
132
149
 
150
+ private static async configureProtocolV2Messages() {
151
+ const protocolV2Messages = DataManager.getProtobufMessages('protocolV2');
152
+ const { configureProtocolV2 } = this.transport;
153
+ if (protocolV2Messages && typeof configureProtocolV2 === 'function') {
154
+ await configureProtocolV2.call(this.transport, JSON.stringify(protocolV2Messages));
155
+ Log.debug('Protocol V2 messages configured');
156
+ }
157
+ }
158
+
133
159
  static getDefaultMessages() {
134
160
  return this.defaultMessages;
135
161
  }
@@ -138,7 +164,7 @@ export default class TransportManager {
138
164
  return this.currentMessages;
139
165
  }
140
166
 
141
- static getMessageVersion() {
142
- return this.messageVersion;
167
+ static getProtocolV1MessageSchema() {
168
+ return this.protocolV1MessageSchema;
143
169
  }
144
170
  }
@@ -41,6 +41,7 @@ import { DataManager } from '../data-manager';
41
41
  import TransportManager from '../data-manager/TransportManager';
42
42
  import { toHardened } from '../api/helpers/pathUtils';
43
43
  import { existCapability } from '../utils/capabilitieUtils';
44
+ import { getProtocolV2Features } from '../protocols/protocol-v2';
44
45
 
45
46
  import type { PROTO } from '../constants';
46
47
  import type {
@@ -49,7 +50,7 @@ import type {
49
50
  PassphraseRequestPayload,
50
51
  } from '../events';
51
52
  import type { PassphrasePromptResponse } from './DeviceCommands';
52
- import type { Deferred } from '@onekeyfe/hd-shared';
53
+ import type { Deferred, HardwareConnectProtocol } from '@onekeyfe/hd-shared';
53
54
  import type { OneKeyDeviceInfo as DeviceDescriptor } from '@onekeyfe/hd-transport';
54
55
  import type DeviceConnector from './DeviceConnector';
55
56
 
@@ -58,6 +59,7 @@ export type InitOptions = {
58
59
  deviceId?: string;
59
60
  passphraseState?: string;
60
61
  deriveCardano?: boolean;
62
+ connectProtocol?: HardwareConnectProtocol;
61
63
  };
62
64
 
63
65
  export type RunOptions = {
@@ -252,13 +254,13 @@ export class Device extends EventEmitter {
252
254
  * Device connect
253
255
  * @returns {Promise<boolean>}
254
256
  */
255
- connect() {
257
+ connect(connectProtocol?: HardwareConnectProtocol) {
256
258
  const env = DataManager.getSettings('env');
257
259
  // eslint-disable-next-line no-async-promise-executor
258
260
  return new Promise<boolean>(async (resolve, reject) => {
259
261
  if (DataManager.isBleConnect(env)) {
260
262
  try {
261
- await this.acquire();
263
+ await this.acquire(connectProtocol);
262
264
  resolve(true);
263
265
  } catch (error) {
264
266
  reject(error);
@@ -268,7 +270,7 @@ export class Device extends EventEmitter {
268
270
  // 不存在 Session ID 或存在 Session ID 但设备在别处使用,都需要 acquire 获取最新 sessionID
269
271
  if (!this.mainId || (!this.isUsedHere() && this.originalDescriptor)) {
270
272
  try {
271
- await this.acquire();
273
+ await this.acquire(connectProtocol);
272
274
  resolve(true);
273
275
  } catch (error) {
274
276
  reject(error);
@@ -283,23 +285,40 @@ export class Device extends EventEmitter {
283
285
  });
284
286
  }
285
287
 
286
- async acquire() {
288
+ async acquire(connectProtocol?: HardwareConnectProtocol) {
287
289
  const env = DataManager.getSettings('env');
288
290
  const mainIdKey = DataManager.isBleConnect(env) ? 'id' : 'session';
291
+ const expectedProtocol = connectProtocol ?? this.originalDescriptor.protocolType;
289
292
  try {
290
293
  if (DataManager.isBleConnect(env)) {
291
- const res = await this.deviceConnector?.acquire(this.originalDescriptor.id);
294
+ const res = await this.deviceConnector?.acquire(
295
+ this.originalDescriptor.id,
296
+ undefined,
297
+ true,
298
+ expectedProtocol
299
+ );
292
300
  this.mainId = (res as unknown as any).uuid ?? '';
293
301
  Log.debug('Expected uuid:', this.mainId);
294
302
  } else {
295
303
  this.mainId = await this.deviceConnector?.acquire(
296
304
  this.originalDescriptor.path,
297
- this.originalDescriptor.session
305
+ this.originalDescriptor.session,
306
+ undefined,
307
+ expectedProtocol
298
308
  );
299
309
  Log.debug('Expected session id:', this.mainId);
300
310
  }
301
311
  this.deviceAcquired = true;
302
312
  this.updateDescriptor({ [mainIdKey]: this.mainId } as unknown as DeviceDescriptor);
313
+
314
+ // Propagate protocol version detected during acquire.
315
+ const detectedProtocol = TransportManager.transport?.getProtocolType?.(
316
+ DataManager.isBleConnect(env) ? this.originalDescriptor.id : this.originalDescriptor.path
317
+ );
318
+ if (detectedProtocol) {
319
+ this.originalDescriptor.protocolType = detectedProtocol;
320
+ }
321
+
303
322
  if (this.commands) {
304
323
  await this.commands.dispose(false);
305
324
  }
@@ -462,6 +481,17 @@ export class Device extends EventEmitter {
462
481
  }
463
482
 
464
483
  async initialize(options?: InitOptions) {
484
+ // Protocol V2 不支持传统 Initialize,直接使用协议专用初始化流程。
485
+ if (this.originalDescriptor.protocolType === 'V2') {
486
+ this.passphraseState = options?.passphraseState;
487
+ if (this.features && !this.featuresNeedsReload && !options?.initSession) {
488
+ Log.debug('Skip Protocol V2 feature adapter; cached features are available');
489
+ return;
490
+ }
491
+ await this._initializeProtocolV2();
492
+ return;
493
+ }
494
+
465
495
  // Log.debug('initialize param:', options);
466
496
 
467
497
  this.passphraseState = options?.passphraseState;
@@ -510,6 +540,36 @@ export class Device extends EventEmitter {
510
540
  }
511
541
  }
512
542
 
543
+ /**
544
+ * Device initialization over Protocol V2.
545
+ *
546
+ * Protocol V2 不走传统 Initialize/GetFeatures,当前仅通过 Ping 验证链路,
547
+ * 然后使用 descriptor 生成临时 Features 视图。
548
+ */
549
+ private async _initializeProtocolV2() {
550
+ Log.debug('Initialize device via Protocol V2 feature adapter');
551
+
552
+ try {
553
+ const features = await Promise.race([
554
+ getProtocolV2Features({
555
+ commands: this.commands,
556
+ descriptor: this.originalDescriptor,
557
+ timeoutMs: 10 * 1000,
558
+ }),
559
+ new Promise<never>((_, reject) => {
560
+ setTimeout(() => {
561
+ reject(ERRORS.TypedError(HardwareErrorCode.DeviceInitializeFailed));
562
+ }, 10 * 1000);
563
+ }),
564
+ ]);
565
+ Log.debug('Protocol V2 normalized features:', features);
566
+ this._updateFeatures(features);
567
+ } catch (error) {
568
+ Log.error('Protocol V2 initialization failed:', error);
569
+ throw error;
570
+ }
571
+ }
572
+
513
573
  async getFeatures() {
514
574
  const { message } = await this.commands.typedCall('GetFeatures', 'Features', {});
515
575
  this._updateFeatures(message);
@@ -580,7 +640,7 @@ export class Device extends EventEmitter {
580
640
  const env = DataManager.getSettings('env');
581
641
  if (env !== 'react-native') {
582
642
  try {
583
- await this.acquire();
643
+ await this.acquire(options.connectProtocol);
584
644
  } catch (error) {
585
645
  this.runPromise = null;
586
646
  return Promise.reject(error);
@@ -822,9 +882,13 @@ export class Device extends EventEmitter {
822
882
  this.features,
823
883
  Enum_Capability.Capability_AttachToPin
824
884
  );
885
+ const isPro2 = getDeviceType(this.features) === EDeviceType.Pro2;
825
886
 
826
887
  const supportUnlock =
827
- supportAttachPinCapability || (versionRange && semver.gte(firmwareVersion, versionRange.min));
888
+ supportAttachPinCapability ||
889
+ // Pro2 V2 暂未从 features 暴露 capabilities,先直连该方法用于固件联调。
890
+ isPro2 ||
891
+ (versionRange && semver.gte(firmwareVersion, versionRange.min));
828
892
 
829
893
  if (supportUnlock) {
830
894
  const res = await this.commands.typedCall('UnLockDevice', 'UnLockDeviceResponse');