Skip to content

Refactor: Use new protobuf to compatible with different versions of protocols #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: onekey
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "submodules/firmware"]
path = submodules/firmware
url = https://github.com/OneKeyHQ/firmware.git
[submodule "submodules/onekey-protocol"]
path = submodules/onekey-protocol
url = git@github.com:OneKeyHQ/onekey-protocol.git
2 changes: 1 addition & 1 deletion packages/connect-examples/electron-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hardware-example",
"productName": "HardwareExample",
"executableName": "onekey-hardware-example",
"version": "1.0.26-alpha.3",
"version": "1.1.0-alpha.0",
"author": "OneKey",
"description": "End-to-end encrypted workspaces for teams",
"main": "dist/index.js",
Expand Down
10 changes: 5 additions & 5 deletions packages/connect-examples/expo-example/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "expo-example",
"version": "1.0.26-alpha.3",
"version": "1.1.0-alpha.0",
"scripts": {
"start": "CONNECT_SRC=https://localhost:8087/ yarn expo start --dev-client",
"android": "yarn expo run:android",
Expand All @@ -19,10 +19,10 @@
"@noble/ed25519": "^2.1.0",
"@noble/hashes": "^1.3.3",
"@noble/secp256k1": "^1.7.1",
"@onekeyfe/hd-ble-sdk": "^1.0.26-alpha.3",
"@onekeyfe/hd-common-connect-sdk": "^1.0.26-alpha.3",
"@onekeyfe/hd-core": "^1.0.26-alpha.3",
"@onekeyfe/hd-web-sdk": "^1.0.26-alpha.3",
"@onekeyfe/hd-ble-sdk": "^1.1.0-alpha.0",
"@onekeyfe/hd-common-connect-sdk": "^1.1.0-alpha.0",
"@onekeyfe/hd-core": "^1.1.0-alpha.0",
"@onekeyfe/hd-web-sdk": "^1.1.0-alpha.0",
"@onekeyfe/react-native-ble-utils": "^0.1.3",
"@polkadot/util-crypto": "13.1.1",
"@react-native-async-storage/async-storage": "1.21.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext, useMemo, useState } from 'react';

import type { Features, OnekeyFeatures } from '@onekeyfe/hd-transport';
import type { Features } from '@onekeyfe/hd-transport';
import type { TestCaseDataWithKey } from '../types';

export const TestRunnerContext = createContext<{
Expand All @@ -13,8 +13,8 @@ export const TestRunnerContext = createContext<{
runningDeviceFeatures?: Features;
setRunningDeviceFeatures?: React.Dispatch<React.SetStateAction<Features>>;

runningOneKeyDeviceFeatures?: OnekeyFeatures;
setRunningOneKeyDeviceFeatures?: React.Dispatch<React.SetStateAction<OnekeyFeatures>>;
runningOneKeyDeviceFeatures?: any;
setRunningOneKeyDeviceFeatures?: React.Dispatch<React.SetStateAction<any>>;

Comment on lines +16 to 18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Avoid any; use the unified Features type

The codebase just unified feature shapes, so any throws that gain away. Stick with Features (or Partial<Features> if some fields are optional).

-  runningOneKeyDeviceFeatures?: any;
-  setRunningOneKeyDeviceFeatures?: React.Dispatch<React.SetStateAction<any>>;
+  runningOneKeyDeviceFeatures?: Features;
+  setRunningOneKeyDeviceFeatures?: React.Dispatch<React.SetStateAction<Features>>;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
runningOneKeyDeviceFeatures?: any;
setRunningOneKeyDeviceFeatures?: React.Dispatch<React.SetStateAction<any>>;
runningOneKeyDeviceFeatures?: Features;
setRunningOneKeyDeviceFeatures?: React.Dispatch<React.SetStateAction<Features>>;
🤖 Prompt for AI Agents (early access)
In packages/connect-examples/expo-example/src/components/BaseTestRunner/Context/TestRunnerProvider.tsx around lines 16 to 18, replace the use of 'any' for runningOneKeyDeviceFeatures and setRunningOneKeyDeviceFeatures with the unified 'Features' type or 'Partial<Features>' if some properties are optional. This ensures type safety and consistency with the rest of the codebase.

timestampBeginTest?: number;
setTimestampBeginTest?: React.Dispatch<React.SetStateAction<number>>;
Expand All @@ -37,7 +37,7 @@ export function TestRunnerProvider({ children }: { children: React.ReactNode })
const [runnerTestCaseTitle, setRunnerTestCaseTitle] = useState<string>();
const [runnerDone, setRunnerDone] = useState<boolean>();
const [runningDeviceFeatures, setRunningDeviceFeatures] = useState<Features>();
const [runningOneKeyDeviceFeatures, setRunningOneKeyDeviceFeatures] = useState<OnekeyFeatures>();
const [runningOneKeyDeviceFeatures, setRunningOneKeyDeviceFeatures] = useState<any>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Same here—keep state strongly typed

-  const [runningOneKeyDeviceFeatures, setRunningOneKeyDeviceFeatures] = useState<any>();
+  const [runningOneKeyDeviceFeatures, setRunningOneKeyDeviceFeatures] = useState<Features>();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const [runningOneKeyDeviceFeatures, setRunningOneKeyDeviceFeatures] = useState<any>();
const [runningOneKeyDeviceFeatures, setRunningOneKeyDeviceFeatures] = useState<Features>();
🤖 Prompt for AI Agents (early access)
In packages/connect-examples/expo-example/src/components/BaseTestRunner/Context/TestRunnerProvider.tsx at line 40, the state variable runningOneKeyDeviceFeatures is currently typed as any, which weakens type safety. Define a specific TypeScript type or interface that accurately represents the expected shape of runningOneKeyDeviceFeatures and use it to strongly type the useState hook instead of any.

const [timestampBeginTest, setTimestampBeginTest] = useState<number>();
const [timestampEndTest, setTimestampEndTest] = useState<number>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ export function useRunnerTest<T>(config: RunnerConfig<T>) {

try {
const onekeyFeatures = await SDK.getOnekeyFeatures(connectId);
// @ts-expect-error
setRunningOneKeyDeviceFeatures?.(onekeyFeatures.payload);
} catch (error) {
// ignore
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import packageJson from '../../package.json';
// import packageJson from '../../package.json';

export const CONNECT_SRC =
process.env.CONNECT_SRC || `https://jssdk.onekey.so/${packageJson.version}/`;
// export const CONNECT_SRC =
// process.env.CONNECT_SRC || `https://jssdk.onekey.so/${packageJson.version}/`;
export const CONNECT_SRC = process.env.CONNECT_SRC || `https://jssdk.onekeytest.com/`;
211 changes: 146 additions & 65 deletions packages/connect-examples/expo-example/src/utils/deviceUtils.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
import {
getDeviceBootloaderVersion,
getDeviceFirmwareVersion,
getDeviceType,
getDeviceUUID,
} from '@onekeyfe/hd-core';
import type { Features, OnekeyFeatures } from '@onekeyfe/hd-transport';
getHardwareInfoFromFeatures,
getFirmwareInfoFromFeatures,
getSeInfoFromFeatures,
EDeviceType,
} from '@onekeyfe/hd-shared';
import type { Features } from '@onekeyfe/hd-transport';

export const getReleaseUrl = ({ features }: { features?: Features }) => {
const deviceType = getDeviceType(features)?.toUpperCase() || 'UNKNOWN';
// const { firmwareUrl, bleVersion } = getDeviceBasicInfo(features, onekeyFeatures);
if (!features)
return {
onekey_boot_url: '',
onekey_firmware_url: '',
onekey_ble_url: '',
};
const { firmwareVersion, bootloaderVersion, bleVersion } = getFirmwareInfoFromFeatures(features);
const { deviceType } = getHardwareInfoFromFeatures(features);
// classic 类型(包括classci 1s, mini, classic),不需要更新bootloader
const firmwareVersion = getDeviceFirmwareVersion(features).join('.');
const bootloaderVersion = `${getDeviceBootloaderVersion(features)?.join('.')}`;
switch (deviceType) {
case 'CLASSIC1S':
case EDeviceType.Classic1s:
case EDeviceType.ClassicPure:
return {
onekey_boot_url: '',
onekey_firmware_url: firmwareVersion
? `https://github.com/OneKeyHQ/firmware-classic1s/releases/tag/v${firmwareVersion}`
: '',
onekey_ble_url: features?.ble_ver
? `https://github.com/OneKeyHQ/bluetooth-firmware-classic/releases/tag/v${features?.ble_ver}`
onekey_ble_url: bleVersion
? `https://github.com/OneKeyHQ/bluetooth-firmware-classic/releases/tag/v${bleVersion}`
: '',
};
case 'PRO':
case EDeviceType.Pro:
return {
onekey_boot_url: bootloaderVersion
? `https://github.com/OneKeyHQ/firmware-pro/releases/tag/bootloader-v${bootloaderVersion}`
: '',
onekey_firmware_url: firmwareVersion
? `https://github.com/OneKeyHQ/firmware-pro/releases/tag/v${firmwareVersion}`
: '',
onekey_ble_url: features?.ble_ver
? `https://github.com/OneKeyHQ/bluetooth-firmware-pro/releases/tag/v${features?.ble_ver}`
onekey_ble_url: bleVersion
? `https://github.com/OneKeyHQ/bluetooth-firmware-pro/releases/tag/v${bleVersion}`
: '',
};
default:
Expand All @@ -44,27 +49,31 @@ export const getReleaseUrl = ({ features }: { features?: Features }) => {
}
};

export function getDeviceBasicInfo(
features: Features | undefined,
onekeyFeatures: OnekeyFeatures | undefined
) {
const deviceType = getDeviceType(features)?.toUpperCase() || 'UNKNOWN';
const serialNumber = features && getDeviceUUID(features);

const bleBuildId = onekeyFeatures?.onekey_ble_build_id || features?.onekey_ble_build_id;
const bleVersion = `${features?.ble_ver}-${bleBuildId}`;

const bootloaderBuildId = onekeyFeatures?.onekey_boot_build_id || features?.onekey_boot_build_id;
const bootloaderVersion =
features && `${getDeviceBootloaderVersion(features)?.join('.')}-${bootloaderBuildId}`;

const boardloaderVersion =
features && `${features?.onekey_board_version}-${onekeyFeatures?.onekey_board_build_id}`;

const firmwareBuildId =
onekeyFeatures?.onekey_firmware_build_id || features?.onekey_firmware_build_id;
const firmwareVersion =
features && `${getDeviceFirmwareVersion(features)?.join('.')}-${firmwareBuildId}`;
export function getDeviceBasicInfo(features: Features | undefined) {
if (!features)
return {
deviceType: '',
serialNumber: '',
bleVersion: '',
bootloaderVersion: '',
boardloaderVersion: '',
firmwareVersion: '',
boardloaderBuildId: '',
bootloaderBuildId: '',
firmwareBuildId: '',
bleBuildId: '',
};
const {
Comment on lines +52 to +66
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Return a uniform object when features is undefined

getDeviceBasicInfo() skips bootUrl, firmwareUrl, and bleUrl when features is falsy, yet callers rely on these keys. This forces every caller to add extra null-checks. Return the full shape with empty strings instead.

 return {
   deviceType: '',
   serialNumber: '',
   bleVersion: '',
   bootloaderVersion: '',
   boardloaderVersion: '',
   firmwareVersion: '',
   boardloaderBuildId: '',
   bootloaderBuildId: '',
   firmwareBuildId: '',
   bleBuildId: '',
+  bootUrl: '',
+  firmwareUrl: '',
+  bleUrl: '',
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function getDeviceBasicInfo(features: Features | undefined) {
if (!features)
return {
deviceType: '',
serialNumber: '',
bleVersion: '',
bootloaderVersion: '',
boardloaderVersion: '',
firmwareVersion: '',
boardloaderBuildId: '',
bootloaderBuildId: '',
firmwareBuildId: '',
bleBuildId: '',
};
const {
export function getDeviceBasicInfo(features: Features | undefined) {
if (!features)
return {
deviceType: '',
serialNumber: '',
bleVersion: '',
bootloaderVersion: '',
boardloaderVersion: '',
firmwareVersion: '',
boardloaderBuildId: '',
bootloaderBuildId: '',
firmwareBuildId: '',
bleBuildId: '',
bootUrl: '',
firmwareUrl: '',
bleUrl: '',
};
const {
🤖 Prompt for AI Agents (early access)
In packages/connect-examples/expo-example/src/utils/deviceUtils.ts around lines 50 to 64, the getDeviceBasicInfo function returns an object missing the keys bootUrl, firmwareUrl, and bleUrl when features is undefined. To fix this, update the returned object in the falsy features case to include bootUrl, firmwareUrl, and bleUrl with empty string values, ensuring the returned object always has a uniform shape and callers do not need extra null-checks.

bootloaderVersion,
boardloaderVersion,
firmwareVersion,
firmwareBuildId,
bootloaderBuildId,
boardloaderBuildId,
bleVersion,
bleBuildId,
} = getFirmwareInfoFromFeatures(features);
const { deviceType, serialNumber } = getHardwareInfoFromFeatures(features);

const {
onekey_firmware_url: firmwareUrl,
Expand All @@ -81,49 +90,44 @@ export function getDeviceBasicInfo(
bootloaderVersion,
boardloaderVersion,
firmwareVersion,
bootloaderBuildId,
boardloaderBuildId,
firmwareBuildId,
bleBuildId,
bootUrl,
firmwareUrl,
bleUrl,
};
}

export function getDeviceInfo(
features: Features | undefined,
onekeyFeatures: OnekeyFeatures | undefined
) {
export function getDeviceInfo(features: Features | undefined, onekeyFeatures: any | undefined) {
if (!features) throw new Error('features is undefined');
const _features = {
...features,
...onekeyFeatures,
};
const {
deviceType,
serialNumber,
bleVersion,
bootloaderVersion,
boardloaderVersion,
firmwareVersion,
} = getDeviceBasicInfo(features, onekeyFeatures);

const firmwareHash = onekeyFeatures?.onekey_firmware_hash || features?.onekey_firmware_hash;

const bootloaderHash =
onekeyFeatures?.onekey_boot_hash || features?.onekey_boot_hash || features?.bootloader_hash;

const se01BuildId = onekeyFeatures?.onekey_se01_build_id || features?.onekey_se01_build_id;
const se01Version = `${features?.onekey_se01_version || features?.se_ver}-${se01BuildId}`;
const se01Hash = onekeyFeatures?.onekey_se01_hash;

const se02BuildId = onekeyFeatures?.onekey_se02_build_id;
const se02Version = `${features?.onekey_se02_version}-${se02BuildId}`;
const se02Hash = onekeyFeatures?.onekey_se02_hash;

const se03BuildId = onekeyFeatures?.onekey_se03_build_id;
const se03Version = `${features?.onekey_se03_version}-${se03BuildId}`;
const se03Hash = onekeyFeatures?.onekey_se03_hash;

const se04BuildId = onekeyFeatures?.onekey_se04_build_id;
const se04Version = `${features?.onekey_se04_version}-${se04BuildId}`;
const se04Hash = onekeyFeatures?.onekey_se04_hash;
} = getDeviceBasicInfo(_features);

const boardloaderHash = onekeyFeatures?.onekey_board_hash || features?.onekey_board_hash;
const { firmwareHash, bootloaderHash, boardloaderHash, bleHash } =
getFirmwareInfoFromFeatures(_features);

const bleHash = onekeyFeatures?.onekey_ble_hash || features?.onekey_ble_hash;
const {
se01Version,
se01Hash,
se02Version,
se02Hash,
se03Version,
se03Hash,
se04Version,
se04Hash,
} = getSeInfoFromFeatures(_features);

return {
deviceType,
Expand All @@ -146,3 +150,80 @@ export function getDeviceInfo(
bleHash,
};
}

export function getFeaturesBetweenProtocol(features: Features | undefined) {
if (!features) return {};

const hardwareInfo = getHardwareInfoFromFeatures(features);
const firmwareInfo = getFirmwareInfoFromFeatures(features);
const seInfo = getSeInfoFromFeatures(features);

return {
...features,

// Firmware info
onekey_firmware_version: firmwareInfo.firmwareVersion,
onekey_firmware_hash: firmwareInfo.firmwareHash,
onekey_firmware_build_id: firmwareInfo.firmwareBuildId,

onekey_boot_version: firmwareInfo.bootloaderVersion,
onekey_boot_build_id: firmwareInfo.bootloaderBuildId,
onekey_boot_hash: firmwareInfo.bootloaderHash,
onekey_board_version: firmwareInfo.boardloaderVersion,
onekey_board_build_id: firmwareInfo.boardloaderBuildId,
onekey_board_hash: firmwareInfo.boardloaderHash,
onekey_ble_version: firmwareInfo.bleVersion,
onekey_ble_name: firmwareInfo.bleName,
onekey_ble_build_id: firmwareInfo.bleBuildId,
onekey_ble_hash: firmwareInfo.bleHash,
bleMac: firmwareInfo.bleMac,

// SE info
onekey_se_type: seInfo.seType,
onekey_se01_type: seInfo.se01Type,
onekey_se02_type: seInfo.se02Type,
onekey_se03_type: seInfo.se03Type,
onekey_se04_type: seInfo.se04Type,
onekey_se01_version: seInfo.se01Version,
onekey_se02_version: seInfo.se02Version,
onekey_se03_version: seInfo.se03Version,
onekey_se04_version: seInfo.se04Version,
onekey_se01_hash: seInfo.se01Hash,
onekey_se02_hash: seInfo.se02Hash,
onekey_se03_hash: seInfo.se03Hash,
onekey_se04_hash: seInfo.se04Hash,
onekey_se01_boot_version: seInfo.se01BootVersion,
onekey_se02_boot_version: seInfo.se02BootVersion,
onekey_se03_boot_version: seInfo.se03BootVersion,
onekey_se04_boot_version: seInfo.se04BootVersion,
onekey_se01_boot_hash: seInfo.se01BootHash,
onekey_se02_boot_hash: seInfo.se02BootHash,
onekey_se03_boot_hash: seInfo.se03BootHash,
onekey_se04_boot_hash: seInfo.se04BootHash,
onekey_se01_boot_build_id: seInfo.se01BootBuildId,
onekey_se02_boot_build_id: seInfo.se02BootBuildId,
onekey_se03_boot_build_id: seInfo.se03BootBuildId,
onekey_se04_boot_build_id: seInfo.se04BootBuildId,
onekey_se01_build_id: seInfo.se01BuildId,
onekey_se02_build_id: seInfo.se02BuildId,
onekey_se03_build_id: seInfo.se03BuildId,
onekey_se04_build_id: seInfo.se04BuildId,
onekey_se01_state: seInfo.se01State,
onekey_se02_state: seInfo.se02State,
onekey_se03_state: seInfo.se03State,
onekey_se04_state: seInfo.se04State,

// Hardware info
device_id: hardwareInfo.serialNumber,
label: hardwareInfo.label,
onekey_device_type: hardwareInfo.deviceType,
hardwareVersion: hardwareInfo.hardwareVersion,
hardwareVersionRawAdc: hardwareInfo.hardwareVersionRawAdc,
onekey_serial: hardwareInfo.serialNumber,
onekey_serial_no: hardwareInfo.serialNumber,
serial_no: hardwareInfo.serialNumber,
init_state: hardwareInfo.init_state,
language: hardwareInfo.language,
passphrase_protection: hardwareInfo.passphrase_protection,
};
}
Loading
Loading