Skip to content

Commit d10c144

Browse files
committed
feat: optimize version limit
1 parent 5d8e461 commit d10c144

File tree

13 files changed

+91
-46
lines changed

13 files changed

+91
-46
lines changed

packages/core/src/api/BaseMethod.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import semver from 'semver';
2+
import { ERRORS, HardwareError, HardwareErrorCode } from '@onekeyfe/hd-shared';
13
import { supportInputPinOnSoftware, supportModifyHomescreen } from '../utils/deviceFeaturesUtils';
24
import { createDeviceMessage } from '../events/device';
35
import { UI_REQUEST } from '../constants/ui-request';
@@ -6,7 +8,8 @@ import DeviceConnector from '../device/DeviceConnector';
68
import { DeviceFirmwareRange, KnownDevice } from '../types';
79
import { CoreMessage, createFirmwareMessage, createUiMessage, DEVICE, FIRMWARE } from '../events';
810
import { getBleFirmwareReleaseInfo, getFirmwareReleaseInfo } from './firmware/releaseHelper';
9-
import { getLogger, LoggerNames } from '../utils';
11+
import { getDeviceFirmwareVersion, getLogger, getMethodVersionRange, LoggerNames } from '../utils';
12+
import { DataManager } from '../data-manager';
1013

1114
const Log = getLogger(LoggerNames.Method);
1215

@@ -81,6 +84,12 @@ export abstract class BaseMethod<Params = undefined> {
8184
*/
8285
skipForceUpdateCheck = false;
8386

87+
/**
88+
* 是否需要预先检查版本限制
89+
* @default false
90+
*/
91+
preCheckVersionLimit = false;
92+
8493
// @ts-expect-error: strictPropertyInitialization
8594
postMessage: (message: CoreMessage) => void;
8695

@@ -127,6 +136,46 @@ export abstract class BaseMethod<Params = undefined> {
127136
);
128137
}
129138

139+
handleUnsupportedMethodError(error: HardwareError) {
140+
if (!error.message.includes('Failure_UnexpectedMessage')) {
141+
return undefined;
142+
}
143+
144+
const versionRange = getMethodVersionRange(
145+
this.device.features,
146+
type => this.getVersionRange()[type]
147+
);
148+
149+
if (!versionRange || !this.device.features) {
150+
return ERRORS.TypedError(HardwareErrorCode.UnsupportedMethod);
151+
}
152+
const newVersionStatus = DataManager.getFirmwareStatus(this.device.features);
153+
const currentVersion = getDeviceFirmwareVersion(this.device.features).join('.');
154+
if (semver.valid(versionRange.min) && semver.lt(currentVersion, versionRange.min)) {
155+
if (newVersionStatus === 'none' || newVersionStatus === 'valid') {
156+
throw ERRORS.TypedError(HardwareErrorCode.NewFirmwareUnRelease);
157+
}
158+
159+
return ERRORS.TypedError(
160+
HardwareErrorCode.CallMethodNeedUpgradeFirmware,
161+
`Device firmware version is too low, please update to ${versionRange.min}`,
162+
{ current: currentVersion, require: versionRange.min }
163+
);
164+
}
165+
166+
if (
167+
versionRange.max &&
168+
semver.valid(versionRange.max) &&
169+
semver.gte(currentVersion, versionRange.max)
170+
) {
171+
return ERRORS.TypedError(
172+
HardwareErrorCode.CallMethodDeprecated,
173+
`Device firmware version is too high, this method has been deprecated in ${versionRange.max}`,
174+
{ current: currentVersion, deprecated: versionRange.max }
175+
);
176+
}
177+
}
178+
130179
checkDeviceSupportFeature() {
131180
if (!this.device || !this.device.features) return;
132181
const inputPinOnSoftware = supportInputPinOnSoftware(this.device.features);

packages/core/src/api/btc/BTCGetAddress.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { getBitcoinForkVersionRange } from './helpers/versionLimit';
1010
export default class BTCGetAddress extends BaseMethod<GetAddress[]> {
1111
hasBundle = false;
1212

13+
preCheckVersionLimit = true;
14+
1315
init() {
1416
this.checkDeviceId = true;
1517
this.notAllowDeviceMode = [...this.notAllowDeviceMode, UI_REQUEST.INITIALIZE];

packages/core/src/api/btc/BTCGetPublicKey.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { getBitcoinForkVersionRange } from './helpers/versionLimit';
1111
export default class BTCGetPublicKey extends BaseMethod<GetPublicKey[]> {
1212
hasBundle = false;
1313

14+
preCheckVersionLimit = true;
15+
1416
init() {
1517
this.checkDeviceId = true;
1618
this.notAllowDeviceMode = [...this.notAllowDeviceMode, UI_REQUEST.INITIALIZE];

packages/core/src/api/btc/BTCSignMessage.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { getCoinAndScriptType } from './helpers/btcParamsUtils';
88
import { getBitcoinForkVersionRange } from './helpers/versionLimit';
99

1010
export default class BTCSignMessage extends BaseMethod<SignMessage> {
11+
preCheckVersionLimit = true;
12+
1113
init() {
1214
this.checkDeviceId = true;
1315
this.notAllowDeviceMode = [...this.notAllowDeviceMode, UI_REQUEST.INITIALIZE];

packages/core/src/api/btc/BTCSignPsbt.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { formatAnyHex } from '../helpers/hexUtils';
66
import { getCoinInfo } from './helpers/btcParamsUtils';
77

88
export default class BTCSignPsbt extends BaseMethod<SignPsbt> {
9+
preCheckVersionLimit = true;
10+
911
init() {
1012
this.checkDeviceId = true;
1113
this.notAllowDeviceMode = [...this.notAllowDeviceMode, UI_REQUEST.INITIALIZE];

packages/core/src/api/btc/BTCSignTransaction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type Params = {
2525
coinName: string;
2626
};
2727
export default class BTCSignTransaction extends BaseMethod<Params> {
28+
preCheckVersionLimit = true;
29+
2830
init() {
2931
this.checkDeviceId = true;
3032
this.notAllowDeviceMode = [...this.notAllowDeviceMode, UI_REQUEST.INITIALIZE];

packages/core/src/api/btc/BTCVerifyMessage.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { getCoinInfo } from './helpers/btcParamsUtils';
88
import { getBitcoinForkVersionRange } from './helpers/versionLimit';
99

1010
export default class BTCVerifyMessage extends BaseMethod<VerifyMessage> {
11+
preCheckVersionLimit = true;
12+
1113
init() {
1214
this.checkDeviceId = true;
1315
this.notAllowDeviceMode = [...this.notAllowDeviceMode, UI_REQUEST.INITIALIZE];

packages/core/src/api/evm/EVMSignTransaction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { signTransaction } from './latest/signTransaction';
1010
import { signTransaction as signTransactionLegacyV1 } from './legacyV1/signTransaction';
1111

1212
export default class EVMSignTransaction extends BaseMethod {
13+
preCheckVersionLimit = true;
14+
1315
addressN: number[] = [];
1416

1517
isEIP1559 = false;

packages/core/src/api/polkadot/PolkadotGetAddress.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { getPolkadotVersionRangeWithBundle } from './networks';
99
export default class PolkadotGetAddress extends BaseMethod<HardwarePolkadotGetAddress[]> {
1010
hasBundle = false;
1111

12+
preCheckVersionLimit = true;
13+
1214
init() {
1315
this.checkDeviceId = true;
1416
this.notAllowDeviceMode = [...this.notAllowDeviceMode];

packages/core/src/api/polkadot/PolkadotSignTransaction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { getPolkadotVersionRange } from './networks';
99
export default class PolkadotSignTransaction extends BaseMethod<HardwarePolkadotSignTx> {
1010
hasBundle = false;
1111

12+
preCheckVersionLimit = true;
13+
1214
init() {
1315
this.checkDeviceId = true;
1416
this.notAllowDeviceMode = [...this.notAllowDeviceMode];

0 commit comments

Comments
 (0)