Skip to content

Commit

Permalink
Optimize: release the device when an error is encountered (#381)
Browse files Browse the repository at this point in the history
* chore: fix lint

* chore: optimize error hint

* chore: release version 1.0.14
  • Loading branch information
ByteZhang1024 authored Nov 19, 2024
1 parent 619f985 commit 50078eb
Show file tree
Hide file tree
Showing 20 changed files with 139 additions and 92 deletions.
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.13",
"version": "1.0.14",
"author": "OneKey",
"description": "End-to-end encrypted workspaces for teams",
"main": "dist/index.js",
Expand Down
1 change: 1 addition & 0 deletions packages/connect-examples/expo-example/locale/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"label__retry_interval_time": "Retry Interval Time",
"label__connection_timeout": "Connection Timeout Time",
"label__init_session": "Init Session",
"label__use_empty_passphrase": "Use Empty Passphrase",
"label__default_parameters": "Default Parameters",
"label__parameters": "Parameters",
"label__enter_parameters_tip": "Enter your parameters here...",
Expand Down
1 change: 1 addition & 0 deletions packages/connect-examples/expo-example/locale/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"label__retry_interval_time": "重试间隔时间",
"label__connection_timeout": "连接超时时间",
"label__init_session": "初始化 Session",
"label__use_empty_passphrase": "使用空 Passphrase",
"label__default_parameters": "预设参数",
"label__parameters": "参数",
"label__enter_parameters_tip": "在此输入您的参数...",
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.13",
"version": "1.0.14",
"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.13",
"@onekeyfe/hd-common-connect-sdk": "^1.0.13",
"@onekeyfe/hd-core": "^1.0.13",
"@onekeyfe/hd-web-sdk": "^1.0.13",
"@onekeyfe/hd-ble-sdk": "^1.0.14",
"@onekeyfe/hd-common-connect-sdk": "^1.0.14",
"@onekeyfe/hd-core": "^1.0.14",
"@onekeyfe/hd-web-sdk": "^1.0.14",
"@onekeyfe/react-native-ble-plx": "3.0.0",
"@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
Expand Up @@ -50,6 +50,11 @@ export default function CommonParamsView() {
value={!!commonParams.initSession}
onToggle={value => handleSetParam('initSession', value)}
/>
<SwitchInput
label={intl.formatMessage({ id: 'label__use_empty_passphrase' })}
value={!!commonParams.useEmptyPassphrase}
onToggle={value => handleSetParam('useEmptyPassphrase', value)}
/>
<SwitchInput
// TODO: i18n
label="detectBootloaderDevice"
Expand Down
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-core",
"version": "1.0.13",
"version": "1.0.14",
"description": "> TODO: description",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
Expand All @@ -25,8 +25,8 @@
"url": "https://github.com/OneKeyHQ/hardware-js-sdk/issues"
},
"dependencies": {
"@onekeyfe/hd-shared": "^1.0.13",
"@onekeyfe/hd-transport": "^1.0.13",
"@onekeyfe/hd-shared": "^1.0.14",
"@onekeyfe/hd-transport": "^1.0.14",
"axios": "^0.27.2",
"bignumber.js": "^9.0.2",
"bytebuffer": "^5.0.1",
Expand Down
62 changes: 29 additions & 33 deletions packages/core/src/api/allnetwork/AllNetworkGetAddress.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import semver from 'semver';
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
import { ERRORS, HardwareError, HardwareErrorCode, serializeError } from '@onekeyfe/hd-shared';

import { get } from 'lodash';
import { UI_REQUEST } from '../../constants/ui-request';
import { serializedPath } from '../helpers/pathUtils';
import { BaseMethod } from '../BaseMethod';
import { validateParams } from '../helpers/paramsValidator';
Expand All @@ -19,8 +18,7 @@ import { createUiMessage, IFRAME } from '../../events';
import { getDeviceFirmwareVersion, getMethodVersionRange } from '../../utils';
import { Device } from '../../device/Device';
import { PROTO } from '../../constants';

import { HardwareError } from '@onekeyfe/hd-shared';
import { UI_REQUEST } from '../../constants/ui-request';

const Mainnet = 'mainnet';

Expand Down Expand Up @@ -307,32 +305,31 @@ export default class AllNetworkGetAddress extends BaseMethod<

method.connector = this.connector;
method.postMessage = this.postMessage;
method.init();
method.setDevice?.(this.device);

let result: AllNetworkAddress;
try {
method.init();
method.setDevice?.(this.device);

const response = await method.run();
result = {
...baseParams,
success: true,
payload: {
...response,
error: response.payload?.error,
errorCode: response.payload?.errorCode,
},
payload: response,
};
} catch (e: any) {
const error = handleSkippableHardwareError(e, this.device, method);

if (error) {
result = {
...baseParams,
success: false,
payload: {
error: error.message,
code: error.errorCode,
connectId: this.payload.connectId,
deviceId: this.payload.deviceId,
params: error.params,
connectId: method.connectId,
deviceId: method.deviceId,
},
};
} else {
Expand All @@ -358,7 +355,7 @@ export default class AllNetworkGetAddress extends BaseMethod<
const response = await this.callMethod(
dependOnMethod.methodName,
dependOnMethod.params,
param,
param
);
dependOnMethodResults.push(response);
}
Expand All @@ -378,7 +375,7 @@ export default class AllNetworkGetAddress extends BaseMethod<

const dependOnPayloads = dependOnMethodResults.reduce(
(acc, cur) => Object.assign(acc, get(cur, 'payload', {})),
{},
{}
);

const result: AllNetworkAddress = {
Expand All @@ -388,7 +385,7 @@ export default class AllNetworkGetAddress extends BaseMethod<
};
responses.push(result);
if (this.payload?.bundle?.length > 1) {
const progress = Math.round(((i + 1) / this.payload?.bundle?.length) * 100);
const progress = Math.round(((i + 1) / this.payload.bundle.length) * 100);
this.postMessage(createUiMessage(UI_REQUEST.DEVICE_PROGRESS, { progress }));
}
this.postPreviousAddressMessage(result);
Expand All @@ -401,14 +398,21 @@ export default class AllNetworkGetAddress extends BaseMethod<
function handleSkippableHardwareError(
e: any,
device: Device,
method: BaseMethod,
method: BaseMethod
): HardwareError | undefined {
let error: HardwareError | undefined = undefined;
let error: HardwareError | undefined;

if (e.message.includes('Failure_UnexpectedMessage')) {
if (e instanceof HardwareError && e.errorCode !== HardwareErrorCode.RuntimeError) {
const { errorCode } = e;
if (errorCode === HardwareErrorCode.CallMethodInvalidParameter) {
error = e;
} else if (errorCode === HardwareErrorCode.CallMethodNeedUpgradeFirmware) {
error = e;
}
} else if (e.message?.includes('Failure_UnexpectedMessage')) {
const versionRange = getMethodVersionRange(
device.features,
type => method.getVersionRange()[type],
type => method.getVersionRange()[type]
);
const currentVersion = getDeviceFirmwareVersion(device.features).join('.');

Expand All @@ -417,23 +421,15 @@ function handleSkippableHardwareError(
semver.valid(versionRange.min) &&
semver.lt(currentVersion, versionRange.min)
) {
error = ERRORS.TypedError(
HardwareErrorCode.CallMethodNeedUpgradeFirmware,
`Device firmware version is too low, please update to ${versionRange.min}`,
{ current: currentVersion, require: versionRange.min },
);
error = ERRORS.createNeedUpgradeFirmwareHardwareError(currentVersion, versionRange.min);
} else {
error = ERRORS.TypedError(HardwareErrorCode.CallMethodNotResponse, e.message);
}
} else if (e.message.includes('Forbidden key path')) {
} else if (
e.message?.toLowerCase()?.includes('forbidden key path') ||
e.message?.toLowerCase()?.includes('invalid path')
) {
error = ERRORS.TypedError(HardwareErrorCode.CallMethodInvalidParameter, e.message);
} else if (e.message.includes('DeviceCheckPassphraseStateError')) {
error = ERRORS.TypedError(HardwareErrorCode.DeviceCheckPassphraseStateError, e.message);
} else if (e instanceof HardwareError) {
const errorCode = e.errorCode;
if (errorCode === HardwareErrorCode.CallMethodInvalidParameter) {
error = e;
}
}

return error;
Expand Down
26 changes: 8 additions & 18 deletions packages/core/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import EventEmitter from 'events';
import { Features, LowlevelTransportSharedPlugin, OneKeyDeviceInfo } from '@onekeyfe/hd-transport';
import {
createDeferred,
createDeprecatedHardwareError,
createNeedUpgradeFirmwareHardwareError,
createNewFirmwareForceUpdateHardwareError,
createNewFirmwareUnReleaseHardwareError,
Deferred,
ERRORS,
HardwareError,
Expand Down Expand Up @@ -175,40 +179,26 @@ export const callAPI = async (message: CoreMessage) => {
(newVersionStatus === 'required' || bleVersionStatus === 'required') &&
method.skipForceUpdateCheck === false
) {
throw ERRORS.TypedError(
HardwareErrorCode.NewFirmwareForceUpdate,
'Device firmware version is too low, please update to the latest version',
{ connectId: method.connectId, deviceId: method.deviceId }
);
throw createNewFirmwareForceUpdateHardwareError(method.connectId, method.deviceId);
}

if (versionRange) {
const currentVersion = getDeviceFirmwareVersion(device.features).join('.');
if (semver.valid(versionRange.min) && semver.lt(currentVersion, versionRange.min)) {
if (newVersionStatus === 'none' || newVersionStatus === 'valid') {
throw ERRORS.TypedError(HardwareErrorCode.NewFirmwareUnRelease);
throw createNewFirmwareUnReleaseHardwareError(currentVersion, versionRange.min);
}

return Promise.reject(
ERRORS.TypedError(
HardwareErrorCode.CallMethodNeedUpgradeFirmware,
`Device firmware version is too low, please update to ${versionRange.min}`,
{ current: currentVersion, require: versionRange.min }
)
createNeedUpgradeFirmwareHardwareError(currentVersion, versionRange.min)
);
}
if (
versionRange.max &&
semver.valid(versionRange.max) &&
semver.gte(currentVersion, versionRange.max)
) {
return Promise.reject(
ERRORS.TypedError(
HardwareErrorCode.CallMethodDeprecated,
`Device firmware version is too high, this method has been deprecated in ${versionRange.max}`,
{ current: currentVersion, deprecated: versionRange.max }
)
);
return Promise.reject(createDeprecatedHardwareError(currentVersion, versionRange.max));
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/device/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,21 @@ export class Device extends EventEmitter {
this.runPromise.reject(e);
}

if (
e instanceof HardwareError &&
(e.errorCode === HardwareErrorCode.DeviceInitializeFailed ||
e.errorCode === HardwareErrorCode.DeviceInterruptedFromOutside ||
e.errorCode === HardwareErrorCode.DeviceInterruptedFromUser ||
e.errorCode === HardwareErrorCode.DeviceCheckPassphraseStateError ||
e.errorCode === HardwareErrorCode.ResponseUnexpectTypeError ||
e.errorCode === HardwareErrorCode.PinInvalid ||
e.errorCode === HardwareErrorCode.PinCancelled ||
e.errorCode === HardwareErrorCode.UnexpectPassphrase)
) {
await this.release();
Log.debug(`error code ${e.errorCode} release device, mainId: ${this.mainId}`);
}

this.runPromise = null;
return;
}
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/types/api/allNetworkGetAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ export type AllNetworkAddress = CommonResponseParams & {
| {
error: string;
code: number;
connectId: string;
deviceId: string;
connectId?: string;
deviceId?: string;
params: any;
};
};

Expand All @@ -107,5 +108,5 @@ export type AllNetworkGetAddressParams = {
export declare function allNetworkGetAddress(
connectId: string,
deviceId: string,
params: CommonParams & AllNetworkGetAddressParams,
params: CommonParams & AllNetworkGetAddressParams
): Response<AllNetworkAddress[]>;
8 changes: 4 additions & 4 deletions packages/hd-ble-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-ble-sdk",
"version": "1.0.13",
"version": "1.0.14",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
"license": "ISC",
Expand All @@ -20,8 +20,8 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@onekeyfe/hd-core": "^1.0.13",
"@onekeyfe/hd-shared": "^1.0.13",
"@onekeyfe/hd-transport-react-native": "^1.0.13"
"@onekeyfe/hd-core": "^1.0.14",
"@onekeyfe/hd-shared": "^1.0.14",
"@onekeyfe/hd-transport-react-native": "^1.0.14"
}
}
12 changes: 6 additions & 6 deletions packages/hd-common-connect-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-common-connect-sdk",
"version": "1.0.13",
"version": "1.0.14",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
"license": "ISC",
Expand All @@ -20,10 +20,10 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@onekeyfe/hd-core": "^1.0.13",
"@onekeyfe/hd-shared": "^1.0.13",
"@onekeyfe/hd-transport-http": "^1.0.13",
"@onekeyfe/hd-transport-lowlevel": "^1.0.13",
"@onekeyfe/hd-transport-webusb": "^1.0.13"
"@onekeyfe/hd-core": "^1.0.14",
"@onekeyfe/hd-shared": "^1.0.14",
"@onekeyfe/hd-transport-http": "^1.0.14",
"@onekeyfe/hd-transport-lowlevel": "^1.0.14",
"@onekeyfe/hd-transport-webusb": "^1.0.14"
}
}
6 changes: 3 additions & 3 deletions packages/hd-transport-http/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-transport-http",
"version": "1.0.13",
"version": "1.0.14",
"description": "hardware http transport",
"author": "OneKey",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
Expand All @@ -24,8 +24,8 @@
"url": "https://github.com/OneKeyHQ/hardware-js-sdk/issues"
},
"dependencies": {
"@onekeyfe/hd-shared": "^1.0.13",
"@onekeyfe/hd-transport": "^1.0.13",
"@onekeyfe/hd-shared": "^1.0.14",
"@onekeyfe/hd-transport": "^1.0.14",
"axios": "^0.27.2"
}
}
6 changes: 3 additions & 3 deletions packages/hd-transport-lowlevel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/hd-transport-lowlevel",
"version": "1.0.13",
"version": "1.0.14",
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
"license": "MIT",
"main": "dist/index.js",
Expand All @@ -19,7 +19,7 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@onekeyfe/hd-shared": "^1.0.13",
"@onekeyfe/hd-transport": "^1.0.13"
"@onekeyfe/hd-shared": "^1.0.14",
"@onekeyfe/hd-transport": "^1.0.14"
}
}
Loading

0 comments on commit 50078eb

Please sign in to comment.