Skip to content

Commit

Permalink
chore(connect): move firmwareRange to AbstractMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
mroz22 committed Nov 7, 2024
1 parent 0dfebc8 commit 7946fa0
Show file tree
Hide file tree
Showing 60 changed files with 576 additions and 290 deletions.
4 changes: 2 additions & 2 deletions packages/connect/src/api/authenticateDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Assert } from '@trezor/schema-utils';

import { AbstractMethod } from '../core/AbstractMethod';
import { UI } from '../events';

Check warning on line 4 in packages/connect/src/api/authenticateDevice.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be no empty line within import group
import { getFirmwareRange } from './common/paramsValidator';

import { deviceAuthenticityConfig } from '../data/deviceAuthenticityConfig';
import { AuthenticateDeviceParams } from '../types/api/authenticateDevice';
import { getRandomChallenge, verifyAuthenticityProof } from './firmware/verifyAuthenticityProof';
Expand All @@ -16,7 +16,7 @@ export default class AuthenticateDevice extends AbstractMethod<
this.allowDeviceMode = [UI.INITIALIZE, UI.SEEDLESS];
this.requiredPermissions = ['management'];
this.useDeviceState = false;
this.firmwareRange = getFirmwareRange(this.name, null, this.firmwareRange);
this.setFirmwareRange(this.name, null);

const { payload } = this;

Expand Down
4 changes: 2 additions & 2 deletions packages/connect/src/api/authorizeCoinjoin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Assert } from '@trezor/schema-utils';

import { AbstractMethod } from '../core/AbstractMethod';

Check warning on line 3 in packages/connect/src/api/authorizeCoinjoin.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be no empty line within import group
import { getFirmwareRange } from './common/paramsValidator';

import { validatePath, getScriptType } from '../utils/pathUtils';
import { getBitcoinNetwork } from '../data/coinInfo';
import { PROTO } from '../constants';
Expand All @@ -18,7 +18,7 @@ export default class AuthorizeCoinjoin extends AbstractMethod<
const address_n = validatePath(payload.path, 3);
const script_type = payload.scriptType || getScriptType(address_n);
const coinInfo = getBitcoinNetwork(payload.coin || address_n);
this.firmwareRange = getFirmwareRange(this.name, coinInfo, this.firmwareRange);
this.setFirmwareRange(this.name, coinInfo);
this.preauthorized = payload.preauthorized;

this.params = {
Expand Down
3 changes: 1 addition & 2 deletions packages/connect/src/api/binance/api/binanceGetAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { Assert } from '@trezor/schema-utils';

import { AbstractMethod, MethodReturnType } from '../../../core/AbstractMethod';
import { getFirmwareRange } from '../../common/paramsValidator';
import { getMiscNetwork } from '../../../data/coinInfo';
import { validatePath, fromHardened, getSerializedPath } from '../../../utils/pathUtils';
import { PROTO, ERRORS } from '../../../constants';
Expand All @@ -23,7 +22,7 @@ export default class BinanceGetAddress extends AbstractMethod<'binanceGetAddress
this.noBackupConfirmationMode = 'always';
this.requiredPermissions = ['read'];
this.requiredDeviceCapabilities = ['Capability_Binance'];
this.firmwareRange = getFirmwareRange(this.name, getMiscNetwork('BNB'), this.firmwareRange);
this.setFirmwareRange(this.name, getMiscNetwork('BNB'));

// create a bundle with only one batch if bundle doesn't exists
this.hasBundle = !!this.payload.bundle;
Expand Down
4 changes: 2 additions & 2 deletions packages/connect/src/api/binance/api/binanceGetPublicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Assert } from '@trezor/schema-utils';

import { AbstractMethod, MethodReturnType } from '../../../core/AbstractMethod';

Check warning on line 5 in packages/connect/src/api/binance/api/binanceGetPublicKey.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be no empty line within import group
import { getFirmwareRange } from '../../common/paramsValidator';

import { getMiscNetwork } from '../../../data/coinInfo';
import { validatePath, fromHardened, getSerializedPath } from '../../../utils/pathUtils';
import { UI, createUiMessage } from '../../../events';
Expand All @@ -19,7 +19,7 @@ export default class BinanceGetPublicKey extends AbstractMethod<
init() {
this.requiredPermissions = ['read'];
this.requiredDeviceCapabilities = ['Capability_Binance'];
this.firmwareRange = getFirmwareRange(this.name, getMiscNetwork('BNB'), this.firmwareRange);
this.setFirmwareRange(this.name, getMiscNetwork('BNB'));

// create a bundle with only one batch if bundle doesn't exists
this.hasBundle = !!this.payload.bundle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { AssertWeak } from '@trezor/schema-utils';

import { AbstractMethod } from '../../../core/AbstractMethod';

Check warning on line 5 in packages/connect/src/api/binance/api/binanceSignTransaction.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be no empty line within import group
import { getFirmwareRange } from '../../common/paramsValidator';

import { getMiscNetwork } from '../../../data/coinInfo';
import { validatePath } from '../../../utils/pathUtils';
import * as helper from '../binanceSignTx';
Expand All @@ -23,7 +23,7 @@ export default class BinanceSignTransaction extends AbstractMethod<
init() {
this.requiredPermissions = ['read', 'write'];
this.requiredDeviceCapabilities = ['Capability_Binance'];
this.firmwareRange = getFirmwareRange(this.name, getMiscNetwork('BNB'), this.firmwareRange);
this.setFirmwareRange(this.name, getMiscNetwork('BNB'));

const { payload } = this;
// validate incoming parameters
Expand Down
4 changes: 2 additions & 2 deletions packages/connect/src/api/cancelCoinjoinAuthorization.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Assert } from '@trezor/schema-utils';

import { AbstractMethod } from '../core/AbstractMethod';

Check warning on line 3 in packages/connect/src/api/cancelCoinjoinAuthorization.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be no empty line within import group
import { getFirmwareRange } from './common/paramsValidator';

import { PROTO } from '../constants';
import { CancelCoinjoinAuthorization as CancelCoinjoinAuthorizationSchema } from '../types/api/cancelCoinjoinAuthorization';

Expand All @@ -14,7 +14,7 @@ export default class CancelCoinjoinAuthorization extends AbstractMethod<

Assert(CancelCoinjoinAuthorizationSchema, payload);

this.firmwareRange = getFirmwareRange(this.name, null, this.firmwareRange);
this.setFirmwareRange(this.name, null);
this.preauthorized =
typeof payload.preauthorized === 'boolean' ? payload.preauthorized : true;
}
Expand Down
7 changes: 1 addition & 6 deletions packages/connect/src/api/cardano/api/cardanoGetAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { Assert } from '@trezor/schema-utils';

import { AbstractMethod, MethodReturnType } from '../../../core/AbstractMethod';
import { getFirmwareRange } from '../../common/paramsValidator';
import { getMiscNetwork } from '../../../data/coinInfo';
import { fromHardened, getSerializedPath } from '../../../utils/pathUtils';
import {
Expand All @@ -29,11 +28,7 @@ export default class CardanoGetAddress extends AbstractMethod<'cardanoGetAddress
this.noBackupConfirmationMode = 'always';
this.requiredPermissions = ['read'];
this.requiredDeviceCapabilities = ['Capability_Cardano'];
this.firmwareRange = getFirmwareRange(
this.name,
getMiscNetwork('Cardano'),
this.firmwareRange,
);
this.setFirmwareRange(this.name, getMiscNetwork('Cardano'));

// create a bundle with only one batch if bundle doesn't exists
this.hasBundle = !!this.payload.bundle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Assert } from '@trezor/schema-utils';

import { PROTO } from '../../../constants';
import { AbstractMethod } from '../../../core/AbstractMethod';

Check warning on line 6 in packages/connect/src/api/cardano/api/cardanoGetNativeScriptHash.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be no empty line within import group
import { getFirmwareRange } from '../../common/paramsValidator';

import { getMiscNetwork } from '../../../data/coinInfo';
import { validatePath } from '../../../utils/pathUtils';
import {
Expand All @@ -19,11 +19,7 @@ export default class CardanoGetNativeScriptHash extends AbstractMethod<
init() {
this.requiredPermissions = ['read'];
this.requiredDeviceCapabilities = ['Capability_Cardano'];
this.firmwareRange = getFirmwareRange(
this.name,
getMiscNetwork('Cardano'),
this.firmwareRange,
);
this.setFirmwareRange(this.name, getMiscNetwork('Cardano'));

const { payload } = this;

Expand Down
8 changes: 2 additions & 6 deletions packages/connect/src/api/cardano/api/cardanoGetPublicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Assert } from '@trezor/schema-utils';

import { PROTO } from '../../../constants';
import { AbstractMethod, MethodReturnType } from '../../../core/AbstractMethod';

Check warning on line 6 in packages/connect/src/api/cardano/api/cardanoGetPublicKey.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be no empty line within import group
import { getFirmwareRange } from '../../common/paramsValidator';

import { getMiscNetwork } from '../../../data/coinInfo';
import { validatePath, fromHardened, getSerializedPath } from '../../../utils/pathUtils';
import { UI, createUiMessage } from '../../../events';
Expand All @@ -21,11 +21,7 @@ export default class CardanoGetPublicKey extends AbstractMethod<'cardanoGetPubli
init() {
this.requiredPermissions = ['read'];
this.requiredDeviceCapabilities = ['Capability_Cardano'];
this.firmwareRange = getFirmwareRange(
this.name,
getMiscNetwork('Cardano'),
this.firmwareRange,
);
this.setFirmwareRange(this.name, getMiscNetwork('Cardano'));

// create a bundle with only one batch if bundle doesn't exists
this.hasBundle = !!this.payload.bundle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { trezorUtils } from '@fivebinaries/coin-selection';
import { AssertWeak, Type } from '@trezor/schema-utils';

import { AbstractMethod } from '../../../core/AbstractMethod';

Check warning on line 9 in packages/connect/src/api/cardano/api/cardanoSignTransaction.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be no empty line within import group
import { getFirmwareRange } from '../../common/paramsValidator';

import { getMiscNetwork } from '../../../data/coinInfo';
import { validatePath } from '../../../utils/pathUtils';
import {
Expand Down Expand Up @@ -79,11 +79,7 @@ export default class CardanoSignTransaction extends AbstractMethod<
init() {
this.requiredPermissions = ['read', 'write'];
this.requiredDeviceCapabilities = ['Capability_Cardano'];
this.firmwareRange = getFirmwareRange(
this.name,
getMiscNetwork('Cardano'),
this.firmwareRange,
);
this.setFirmwareRange(this.name, getMiscNetwork('Cardano'));

const { payload } = this;

Expand Down
4 changes: 2 additions & 2 deletions packages/connect/src/api/cipherKeyValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Assert } from '@trezor/schema-utils';

import { UI, createUiMessage } from '../events';
import { AbstractMethod } from '../core/AbstractMethod';

Check warning on line 6 in packages/connect/src/api/cipherKeyValue.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be no empty line within import group
import { getFirmwareRange } from './common/paramsValidator';

import { validatePath } from '../utils/pathUtils';
import type { PROTO } from '../constants';
import { Bundle } from '../types/params';
Expand All @@ -18,7 +18,7 @@ export default class CipherKeyValue extends AbstractMethod<

init() {
this.requiredPermissions = ['read', 'write'];
this.firmwareRange = getFirmwareRange(this.name, null, this.firmwareRange);
this.setFirmwareRange(this.name, null);

// create a bundle with only one batch if bundle doesn't exists
this.hasBundle = !!this.payload.bundle;
Expand Down
104 changes: 1 addition & 103 deletions packages/connect/src/api/common/paramsValidator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// origin: https://github.com/trezor/connect/blob/develop/src/js/core/methods/helpers/paramsValidator.js
import { versionUtils } from '@trezor/utils';

import { ERRORS } from '../../constants';
import { fromHardened } from '../../utils/pathUtils';
import { config } from '../../data/config';
import type { CoinInfo, FirmwareRange, DeviceModelInternal } from '../../types';
import type { CoinInfo } from '../../types';

type ParamType = 'string' | 'number' | 'array' | 'array-buffer' | 'boolean' | 'uint' | 'object';

Expand Down Expand Up @@ -99,103 +97,3 @@ export const validateCoinPath = (path: number[], coinInfo?: CoinInfo) => {
throw invalidParameter('Parameters "path" and "coin" do not match.');
}
};

export const getFirmwareRange = (
method: string,
coinInfo: CoinInfo | null | undefined,
currentRange: FirmwareRange,
) => {
const range = JSON.parse(JSON.stringify(currentRange)) as FirmwareRange;
const models = Object.keys(range) as DeviceModelInternal[];
// set minimum required firmware from coins.json (coinInfo)
if (coinInfo) {
models.forEach(model => {
const supportVersion = coinInfo.support ? coinInfo.support[model] : false;
if (supportVersion === false) {
range[model].min = '0';
} else if (
range[model].min !== '0' &&
typeof supportVersion === 'string' &&
versionUtils.isNewer(supportVersion, range[model].min)
) {
range[model].min = supportVersion;
}
});
}

const coinType = coinInfo?.type;
const shortcut = coinInfo?.shortcut.toLowerCase();
// find firmware range in config.json
const configRules = config.supportedFirmware
.filter(rule => {
// check if rule applies to requested method
if (rule.methods) {
return rule.methods.includes(method);
}
// check if rule applies to capability
if (rule.capabilities) {
return rule.capabilities.includes(method);
}

// rule doesn't have specified methods
// it may be a global rule for coin or coinType
return true;
})
.filter(rule => {
// REF_TODO: there is no coinType in config. possibly obsolete code?
// probably still useful, we just need to define type for config and not infer it.
// @ts-expect-error
if (rule.coinType) {
// rule for coin type
// @ts-expect-error
return rule.coinType === coinType;
}
if (rule.coin) {
// rule for coin shortcut
// @ts-expect-error
return (typeof rule.coin === 'string' ? [rule.coin] : rule.coin).includes(shortcut);
}

// rule for method
return rule.methods || rule.capabilities;
});

configRules.forEach(rule => {
// override defaults
// NOTE:
// 0 may be confusing. means: no-support for "min" and unlimited support for "max"
if (rule.min) {
models.forEach(model => {
const modelMin = (rule.min as Record<DeviceModelInternal, string | undefined>)[
model
];
if (modelMin) {
if (
modelMin === '0' ||
range[model].min === '0' ||
!versionUtils.isNewerOrEqual(range[model].min, modelMin)
) {
range[model].min = modelMin;
}
}
});
}
if (rule.max) {
models.forEach(model => {
// @ts-expect-error same issue as in coinType above, config needs to be typed not inferred.
const modelMax = rule.max[model];
if (modelMax) {
if (
modelMax === '0' ||
range[model].max === '0' ||
!versionUtils.isNewerOrEqual(range[model].max, modelMax)
) {
range[model].max = modelMax;
}
}
});
}
});

return range;
};
4 changes: 2 additions & 2 deletions packages/connect/src/api/composeTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AbstractMethod } from '../core/AbstractMethod';
import { ERRORS } from '../constants';
import { UI, createUiMessage } from '../events';
import { Discovery } from './common/Discovery';
import { validateParams, getFirmwareRange } from './common/paramsValidator';
import { validateParams } from './common/paramsValidator';
import * as pathUtils from '../utils/pathUtils';
import { resolveAfter } from '../utils/promiseUtils';
import { formatAmount } from '../utils/formatUtils';
Expand Down Expand Up @@ -92,7 +92,7 @@ export default class ComposeTransaction extends AbstractMethod<'composeTransacti
isBackendSupported(coinInfo);

// set required firmware from coinInfo support
this.firmwareRange = getFirmwareRange(this.name, coinInfo, this.firmwareRange);
this.setFirmwareRange(this.name, coinInfo);

// validate each output and transform into @trezor/utxo-lib/compose format
const outputs: ComposeOutput[] = [];
Expand Down
4 changes: 2 additions & 2 deletions packages/connect/src/api/eos/api/eosGetPublicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Assert } from '@trezor/schema-utils';

import { AbstractMethod, MethodReturnType } from '../../../core/AbstractMethod';

Check warning on line 5 in packages/connect/src/api/eos/api/eosGetPublicKey.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be no empty line within import group
import { getFirmwareRange } from '../../common/paramsValidator';

import { getMiscNetwork } from '../../../data/coinInfo';
import { validatePath, fromHardened, getSerializedPath } from '../../../utils/pathUtils';
import { UI, createUiMessage } from '../../../events';
Expand All @@ -19,7 +19,7 @@ export default class EosGetPublicKey extends AbstractMethod<
init() {
this.requiredPermissions = ['read'];
this.requiredDeviceCapabilities = ['Capability_EOS'];
this.firmwareRange = getFirmwareRange(this.name, getMiscNetwork('EOS'), this.firmwareRange);
this.setFirmwareRange(this.name, getMiscNetwork('EOS'));

// create a bundle with only one batch if bundle doesn't exists
this.hasBundle = !!this.payload.bundle;
Expand Down
4 changes: 2 additions & 2 deletions packages/connect/src/api/eos/api/eosSignTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { AssertWeak } from '@trezor/schema-utils';

import { AbstractMethod } from '../../../core/AbstractMethod';
import { getFirmwareRange } from '../../common/paramsValidator';

import { getMiscNetwork } from '../../../data/coinInfo';
import { validatePath } from '../../../utils/pathUtils';
import * as helper from '../eosSignTx';
Expand All @@ -22,7 +22,7 @@ export default class EosSignTransaction extends AbstractMethod<'eosSignTransacti
init() {
this.requiredPermissions = ['read', 'write'];
this.requiredDeviceCapabilities = ['Capability_EOS'];
this.firmwareRange = getFirmwareRange(this.name, getMiscNetwork('EOS'), this.firmwareRange);
this.setFirmwareRange(this.name, getMiscNetwork('EOS'));

const { payload } = this;
// validate incoming parameters
Expand Down
4 changes: 2 additions & 2 deletions packages/connect/src/api/ethereum/api/ethereumGetAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Assert } from '@trezor/schema-utils';

import { AbstractMethod, MethodReturnType } from '../../../core/AbstractMethod';
import { getFirmwareRange } from '../../common/paramsValidator';

import { validatePath, getSerializedPath, getSlip44ByPath } from '../../../utils/pathUtils';
import { getNetworkLabel } from '../../../utils/ethereumUtils';
import { getEthereumNetwork, getUniqueNetworks } from '../../../data/coinInfo';
Expand Down Expand Up @@ -46,7 +46,7 @@ export default class EthereumGetAddress extends AbstractMethod<'ethereumGetAddre
this.params = payload.bundle.map(batch => {
const path = validatePath(batch.path, 3);
const network = getEthereumNetwork(path);
this.firmwareRange = getFirmwareRange(this.name, network, this.firmwareRange);
this.setFirmwareRange(this.name, network);

return {
address_n: path,
Expand Down
Loading

0 comments on commit 7946fa0

Please sign in to comment.