@onekeyfe/hd-core 1.2.0-alpha.2 → 1.2.0-alpha.3

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 (44) hide show
  1. package/__tests__/protocol-v2.test.ts +162 -106
  2. package/dist/api/GetPassphraseState.d.ts +4 -4
  3. package/dist/api/GetPassphraseState.d.ts.map +1 -1
  4. package/dist/api/protocol-v2/helpers.d.ts +2 -3
  5. package/dist/api/protocol-v2/helpers.d.ts.map +1 -1
  6. package/dist/constants/index.d.ts +2 -1
  7. package/dist/constants/index.d.ts.map +1 -1
  8. package/dist/core/index.d.ts.map +1 -1
  9. package/dist/data-manager/connectSettings.d.ts.map +1 -1
  10. package/dist/device/Device.d.ts +1 -0
  11. package/dist/device/Device.d.ts.map +1 -1
  12. package/dist/deviceProfile/buildDeviceFeatures.d.ts.map +1 -1
  13. package/dist/deviceProfile/buildDeviceProfile.d.ts.map +1 -1
  14. package/dist/index.d.ts +12 -10
  15. package/dist/index.js +500 -477
  16. package/dist/protocols/protocol-v2/features.d.ts +2 -2
  17. package/dist/protocols/protocol-v2/features.d.ts.map +1 -1
  18. package/dist/types/api/getPassphraseState.d.ts +4 -4
  19. package/dist/types/api/getPassphraseState.d.ts.map +1 -1
  20. package/dist/types/api/protocolV2.d.ts +3 -3
  21. package/dist/types/api/protocolV2.d.ts.map +1 -1
  22. package/dist/types/settings.d.ts +1 -0
  23. package/dist/types/settings.d.ts.map +1 -1
  24. package/dist/utils/deviceFeaturesUtils.d.ts +2 -0
  25. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  26. package/dist/utils/patch.d.ts +1 -1
  27. package/dist/utils/patch.d.ts.map +1 -1
  28. package/package.json +4 -4
  29. package/src/api/GetPassphraseState.ts +7 -4
  30. package/src/api/protocol-v2/DeviceReboot.ts +3 -3
  31. package/src/api/protocol-v2/helpers.ts +1 -26
  32. package/src/constants/index.ts +10 -1
  33. package/src/core/index.ts +2 -0
  34. package/src/data/messages/messages-protocol-v2.json +329 -323
  35. package/src/data-manager/connectSettings.ts +6 -0
  36. package/src/device/Device.ts +12 -1
  37. package/src/device/DevicePool.ts +6 -6
  38. package/src/deviceProfile/buildDeviceFeatures.ts +2 -1
  39. package/src/deviceProfile/buildDeviceProfile.ts +2 -3
  40. package/src/protocols/protocol-v2/features.ts +8 -7
  41. package/src/types/api/getPassphraseState.ts +4 -4
  42. package/src/types/api/protocolV2.ts +2 -3
  43. package/src/types/settings.ts +5 -0
  44. package/src/utils/deviceFeaturesUtils.ts +7 -1
@@ -27,6 +27,8 @@ const initialSettings: ConnectSettings = {
27
27
  env: 'web',
28
28
  lazyLoad: false,
29
29
  timestamp: new Date().getTime(),
30
+ // 临时开关:仅用于本地/测试固件兼容;正式链路默认调用真实 DevGetDeviceInfo。
31
+ protocolV2DeviceInfoMockEnabled: false,
30
32
  };
31
33
 
32
34
  export const getEnv = () => {
@@ -115,6 +117,10 @@ export const parseConnectSettings = (input: Partial<ConnectSettings> = {}) => {
115
117
  settings.fetchConfig = input.fetchConfig;
116
118
  }
117
119
 
120
+ if (typeof input.protocolV2DeviceInfoMockEnabled === 'boolean') {
121
+ settings.protocolV2DeviceInfoMockEnabled = input.protocolV2DeviceInfoMockEnabled;
122
+ }
123
+
118
124
  return settings;
119
125
  };
120
126
 
@@ -261,13 +261,14 @@ export class Device extends EventEmitter {
261
261
  const bleName = this.getCurrentBleName();
262
262
  const label = this.getCurrentLabel();
263
263
  const serialNo = this.getCurrentSerialNo();
264
+ const connectId = this.getConnectId();
264
265
  const deviceId = this.getCurrentDeviceId() || null;
265
266
 
266
267
  const features = this.features;
267
268
 
268
269
  return {
269
270
  /** Android uses Mac address, iOS uses uuid, USB uses uuid */
270
- connectId: DataManager.isBleConnect(env) ? this.mainId || null : serialNo,
271
+ connectId: DataManager.isBleConnect(env) ? this.mainId || null : connectId,
271
272
  /** Hardware ID, will not change at any time */
272
273
  uuid: serialNo,
273
274
  commType: this.originalDescriptor.commType,
@@ -501,6 +502,16 @@ export class Device extends EventEmitter {
501
502
  return this.features ? getDeviceUUID(this.features) : '';
502
503
  }
503
504
 
505
+ getConnectId() {
506
+ const serialNo = this.getCurrentSerialNo();
507
+ if (serialNo) return serialNo;
508
+
509
+ // connectId 是 SDK 内部连接路由 key;Protocol V2 早期固件/mock
510
+ // 可能还没有 serial_no,此时用 transport descriptor 兜底,不改变
511
+ // features.serialNo / deviceId 的业务语义。
512
+ return this.originalDescriptor.path || this.originalDescriptor.id || '';
513
+ }
514
+
504
515
  getCurrentBleName() {
505
516
  return getDeviceBleName(this.features);
506
517
  }
@@ -118,14 +118,14 @@ export class DevicePool extends EventEmitter {
118
118
  for await (const descriptor of descriptorList) {
119
119
  const device = await this._createDevice(descriptor, initOptions);
120
120
 
121
- const uuid = device.getCurrentSerialNo();
122
- if (uuid) {
123
- if (this.devicesCache[uuid]) {
124
- const cache = this.devicesCache[uuid];
121
+ const connectId = device.getConnectId();
122
+ if (connectId) {
123
+ if (this.devicesCache[connectId]) {
124
+ const cache = this.devicesCache[connectId];
125
125
  cache.updateDescriptor(descriptor, true);
126
126
  }
127
- this.devicesCache[uuid] = device;
128
- devices[uuid] = device;
127
+ this.devicesCache[connectId] = device;
128
+ devices[connectId] = device;
129
129
  }
130
130
 
131
131
  deviceList.push(device);
@@ -250,6 +250,7 @@ export const buildProtocolV2FeaturesPayload = (
250
250
  );
251
251
  const boardVersion = firstMeaningfulVersion(getImageVersion(fwBoard), previous?.boardVersion);
252
252
  const bleVersion = firstMeaningfulVersion(getImageVersion(bleApplication), previous?.bleVersion);
253
+ const deviceId = firstValue(info?.hw?.device_id, previous?.deviceId) ?? null;
253
254
  const serialNo = firstValue(info?.hw?.serial_no, previous?.serialNo) ?? '';
254
255
  const label = firstValue(deviceInfo?.status?.label, previous?.label) ?? null;
255
256
  const bleName = firstValue(info?.coprocessor?.bt_adv_name, info?.bt?.adv_name, previous?.bleName);
@@ -268,7 +269,7 @@ export const buildProtocolV2FeaturesPayload = (
268
269
  firmwareType: previous?.firmwareType ?? EFirmwareType.Universal,
269
270
  model: 'pro2',
270
271
  vendor: 'onekey.so',
271
- deviceId: null,
272
+ deviceId,
272
273
  serialNo,
273
274
  label,
274
275
  bleName: bleName ?? null,
@@ -304,6 +304,7 @@ export function buildProfileFromProtocolV2({
304
304
  scope = 'basic',
305
305
  includeRaw = false,
306
306
  }: BuildProtocolV2ProfileParams): DeviceProfile {
307
+ const deviceId = deviceInfo?.hw?.device_id || '';
307
308
  const serialNo = deviceInfo?.hw?.serial_no || '';
308
309
  const label = deviceInfo?.status?.label ?? null;
309
310
  const bleName = deviceInfo?.bt?.adv_name ?? null;
@@ -314,9 +315,7 @@ export function buildProfileFromProtocolV2({
314
315
  sources,
315
316
  deviceType: EDeviceType.Pro2,
316
317
  firmwareType: EFirmwareType.Universal,
317
- // Protocol V2 的 DevGetDeviceInfo 没有 device_id 字段;serialNo 与 deviceId
318
- // 不是等价语义,这里保持空值,避免把稳定硬件序列号误当会随 wipe 轮换的身份。
319
- deviceId: '',
318
+ deviceId,
320
319
  serialNo,
321
320
  label,
322
321
  bleName,
@@ -1,6 +1,6 @@
1
1
  import { DevSEState, DevSeType } from '@onekeyfe/hd-transport';
2
2
 
3
- import type { DeviceGetDeviceInfo, DevSEInfo, ProtocolV2DeviceInfo } from '@onekeyfe/hd-transport';
3
+ import type { DevGetDeviceInfo, DevSEInfo, ProtocolV2DeviceInfo } from '@onekeyfe/hd-transport';
4
4
  import type { DeviceCommands } from '../../device/DeviceCommands';
5
5
 
6
6
  // 单源类型:直接使用 hd-transport 生成的 ProtocolV2DeviceInfo / DevSEInfo /
@@ -121,15 +121,16 @@ export const PROTOCOL_V2_DEVICE_INFO_REQUEST = PROTOCOL_V2_FULL_DEVICE_INFO_REQU
121
121
  export const PROTOCOL_V2_DEVICE_INFO_TIMEOUT_MS = 10 * 1000;
122
122
 
123
123
  /**
124
- * 临时开关(默认开启):当前 Pro2 测试固件 / 早期工程板尚未实现 DevGetDeviceInfo
125
- * 真实调用只会超时失败。开启时跳过 wire 调用,直接返回 mock DeviceInfo;
124
+ * 临时开关(默认关闭):当前正式链路直接调用 DevGetDeviceInfo
125
+ * 仅当 Pro2 测试固件 / 早期工程板尚未实现 DevGetDeviceInfo 时,才显式开启 mock
126
+ * 开启时跳过 wire 调用,直接返回 mock DeviceInfo;
126
127
  * DevGetDeviceInfo 尚未返回的字段保持为空,不再用 transport path 兜底成设备身份。
127
128
  *
128
- * 固件实现 DevGetDeviceInfo 后:把默认值改回 false(或直接删除开关与 mock)。
129
+ * 固件实现 DevGetDeviceInfo 稳定后:删除开关与 mock
129
130
  * 注意:开启期间 FirmwareUpdateV4 的“升级完成版本比对”拿到的也是 mock 版本,
130
131
  * 不能作为升级成功的依据。
131
132
  */
132
- let protocolV2DeviceInfoMockEnabled = true;
133
+ let protocolV2DeviceInfoMockEnabled = false;
133
134
 
134
135
  export const setProtocolV2DeviceInfoMock = (enabled: boolean) => {
135
136
  protocolV2DeviceInfoMockEnabled = enabled;
@@ -163,9 +164,9 @@ export async function requestProtocolV2DeviceInfo({
163
164
  }: {
164
165
  commands: DeviceCommands;
165
166
  timeoutMs?: number;
166
- request?: DeviceGetDeviceInfo;
167
+ request?: DevGetDeviceInfo;
167
168
  }): Promise<ProtocolV2DeviceInfo> {
168
- if (protocolV2DeviceInfoMockEnabled) {
169
+ if (isProtocolV2DeviceInfoMockEnabled()) {
169
170
  return buildMockProtocolV2DeviceInfo();
170
171
  }
171
172
  const { message } = await commands.typedCall('DevGetDeviceInfo', 'DeviceInfo', request, {
@@ -1,10 +1,10 @@
1
1
  import type { CommonParams, Response } from '../params';
2
2
 
3
3
  export type GetPassphraseStatePayload = {
4
- passphrase_state?: string;
5
- session_id?: string;
6
- unlocked_attach_pin?: boolean;
7
- passphrase_protection?: boolean | null;
4
+ passphraseState?: string;
5
+ sessionId?: string;
6
+ unlockedAttachPin?: boolean;
7
+ passphraseProtection?: boolean | null;
8
8
  };
9
9
 
10
10
  export type GetPassphraseStateParams = CommonParams & {
@@ -1,6 +1,5 @@
1
1
  import type { CommonParams, Response } from '../params';
2
2
  import type {
3
- DeviceFirmwareUpdateStatus,
4
3
  DevFirmwareUpdateStatus,
5
4
  FactoryDeviceInfo,
6
5
  OnboardingStatus,
@@ -96,12 +95,12 @@ export declare function deviceGetOnboardingStatus(
96
95
  export declare function deviceFirmwareUpdate(
97
96
  connectId: string,
98
97
  params: CommonParams & DeviceFirmwareUpdateParams
99
- ): Response<Success | DevFirmwareUpdateStatus | DeviceFirmwareUpdateStatus>;
98
+ ): Response<Success | DevFirmwareUpdateStatus>;
100
99
 
101
100
  export declare function deviceGetFirmwareUpdateStatus(
102
101
  connectId: string,
103
102
  params?: CommonParams
104
- ): Response<DevFirmwareUpdateStatus | DeviceFirmwareUpdateStatus>;
103
+ ): Response<DevFirmwareUpdateStatus>;
105
104
 
106
105
  export declare function devReboot(
107
106
  connectId: string,
@@ -34,6 +34,11 @@ export type ConnectSettings = {
34
34
  fetchConfig?: boolean;
35
35
  extension?: string;
36
36
  configFetcher?: (url: string) => Promise<RemoteConfigResponse | null>;
37
+ /**
38
+ * 临时开关:Protocol V2 DevGetDeviceInfo 未稳定前用于 mock 设备信息。
39
+ * 正式 app 可显式设置为 false 以调用真实 DevGetDeviceInfo;固件稳定后删除。
40
+ */
41
+ protocolV2DeviceInfoMockEnabled?: boolean;
37
42
  };
38
43
 
39
44
  export type IVersionArray = [number, number, number];
@@ -93,12 +93,17 @@ export const getPassphraseStateWithRefreshDeviceInfo = async (
93
93
  expectPassphraseState?: string;
94
94
  onlyMainPin?: boolean;
95
95
  allowCreateAttachPin?: boolean;
96
+ initSession?: boolean;
96
97
  }
97
98
  ) => {
98
99
  const { features } = device;
99
100
  const locked = features?.unlocked === false;
100
101
  const deviceType = device.getCurrentDeviceType();
101
102
 
103
+ if (options?.initSession) {
104
+ device.clearInternalState();
105
+ }
106
+
102
107
  const { passphraseState, newSession, unlockedAttachPin } = await getPassphraseState(device, {
103
108
  ...options,
104
109
  });
@@ -128,7 +133,7 @@ export const getPassphraseStateWithRefreshDeviceInfo = async (
128
133
  passphraseState,
129
134
  deviceId,
130
135
  newSession,
131
- device.features?.sessionId
136
+ options?.initSession ? null : device.features?.sessionId
132
137
  );
133
138
 
134
139
  return { passphraseState, newSession, unlockedAttachPin };
@@ -145,6 +150,7 @@ export const getPassphraseState = async (
145
150
  expectPassphraseState?: string;
146
151
  onlyMainPin?: boolean;
147
152
  allowCreateAttachPin?: boolean;
153
+ initSession?: boolean;
148
154
  }
149
155
  ): Promise<{
150
156
  passphraseState: string | undefined;