Skip to content

Commit

Permalink
fixup! chore(connect): coinAbstractMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
mroz22 committed Nov 12, 2024
1 parent 1f304e5 commit 2f80653
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@ export abstract class BinanceAbstractMethod<
super(message);
this.firmwareRange = getFirmwareRange(this.name, getMiscNetwork('BNB'), this.firmwareRange);
}

// firmwareRangeRequirements could be defined here instead of config.ts
// loading coin-specific protobuf could be done here
// anything else?
}
21 changes: 21 additions & 0 deletions packages/connect/src/api/cardano/api/cardanoAbstractMethods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Capability } from '@trezor/protobuf/src/messages-schema';

Check warning on line 1 in packages/connect/src/api/cardano/api/cardanoAbstractMethods.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be at least one empty line between import groups
import { AbstractMethod, Payload } from '../../../core/AbstractMethod';
import { CallMethodPayload } from '../../../events';
import { getFirmwareRange } from '../../common/paramsValidator';
import { getMiscNetwork } from '../../../data/coinInfo';

export abstract class CardanoAbstractMethod<
Name extends CallMethodPayload['method'],
Params = undefined,
> extends AbstractMethod<Name, Params> {
requiredDeviceCapabilities = [Capability.Cardano];

constructor(message: { id?: number; payload: Payload<Name> }) {
super(message);
this.firmwareRange = getFirmwareRange(
this.name,
getMiscNetwork('Cardano'),
this.firmwareRange,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { trezorUtils, CoinSelectionError } from '@fivebinaries/coin-selection';

import { AbstractMethod } from '../../../core/AbstractMethod';
import { validateParams } from '../../common/paramsValidator';
import { composeTxPlan } from '../cardanoUtils';
import type {
CardanoComposeTransactionParams,
PrecomposedTransactionCardano,
} from '../../../types/api/cardanoComposeTransaction';
import { CardanoAbstractMethod } from './cardanoAbstractMethods';

export default class CardanoComposeTransaction extends AbstractMethod<
export default class CardanoComposeTransaction extends CardanoAbstractMethod<
'cardanoComposeTransaction',
CardanoComposeTransactionParams
> {
Expand Down
16 changes: 6 additions & 10 deletions packages/connect/src/api/cardano/api/cardanoGetAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import { Assert } from '@trezor/schema-utils';

import { AbstractMethod, MethodReturnType } from '../../../core/AbstractMethod';
import { getFirmwareRange } from '../../common/paramsValidator';
import { getMiscNetwork } from '../../../data/coinInfo';
import { MethodReturnType } from '../../../core/AbstractMethod';
import { fromHardened, getSerializedPath } from '../../../utils/pathUtils';
import {
addressParametersFromProto,
Expand All @@ -16,24 +14,22 @@ import { PROTO, ERRORS } from '../../../constants';
import { UI, createUiMessage } from '../../../events';
import { Bundle } from '../../../types';
import { CardanoGetAddress as CardanoGetAddressSchema } from '../../../types/api/cardano';
import { CardanoAbstractMethod } from './cardanoAbstractMethods';

type Params = PROTO.CardanoGetAddress & {
address?: string;
};

export default class CardanoGetAddress extends AbstractMethod<'cardanoGetAddress', Params[]> {
export default class CardanoGetAddress extends CardanoAbstractMethod<
'cardanoGetAddress',
Params[]
> {
hasBundle?: boolean;
progress = 0;

init() {
this.noBackupConfirmationMode = 'always';
this.requiredPermissions = ['read'];
this.requiredDeviceCapabilities = ['Capability_Cardano'];
this.firmwareRange = getFirmwareRange(
this.name,
getMiscNetwork('Cardano'),
this.firmwareRange,
);

// 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,27 +3,20 @@
import { Assert } from '@trezor/schema-utils';

import { PROTO } from '../../../constants';
import { AbstractMethod } from '../../../core/AbstractMethod';
import { getFirmwareRange } from '../../common/paramsValidator';
import { getMiscNetwork } from '../../../data/coinInfo';

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

View workflow job for this annotation

GitHub Actions / Linting and formatting

'getMiscNetwork' is defined but never used. Allowed unused vars must match /^_/u
import { validatePath } from '../../../utils/pathUtils';
import {
CardanoGetNativeScriptHash as CardanoGetNativeScriptHashSchema,
CardanoNativeScript,
} from '../../../types/api/cardano';
import { CardanoAbstractMethod } from './cardanoAbstractMethods';

export default class CardanoGetNativeScriptHash extends AbstractMethod<
export default class CardanoGetNativeScriptHash extends CardanoAbstractMethod<
'cardanoGetNativeScriptHash',
PROTO.CardanoGetNativeScriptHash
> {
init() {
this.requiredPermissions = ['read'];
this.requiredDeviceCapabilities = ['Capability_Cardano'];
this.firmwareRange = getFirmwareRange(
this.name,
getMiscNetwork('Cardano'),
this.firmwareRange,
);

const { payload } = this;

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

import { PROTO } from '../../../constants';
import { AbstractMethod, MethodReturnType } from '../../../core/AbstractMethod';
import { getFirmwareRange } from '../../common/paramsValidator';
import { getMiscNetwork } from '../../../data/coinInfo';
import { MethodReturnType } from '../../../core/AbstractMethod';
import { validatePath, fromHardened, getSerializedPath } from '../../../utils/pathUtils';
import { UI, createUiMessage } from '../../../events';
import { Bundle } from '../../../types';
import { CardanoGetPublicKey as CardanoGetPublicKeySchema } from '../../../types/api/cardano';
import { CardanoAbstractMethod } from './cardanoAbstractMethods';

interface Params extends PROTO.CardanoGetPublicKey {
suppressBackupWarning?: boolean;
}

export default class CardanoGetPublicKey extends AbstractMethod<'cardanoGetPublicKey', Params[]> {
export default class CardanoGetPublicKey extends CardanoAbstractMethod<
'cardanoGetPublicKey',
Params[]
> {
hasBundle?: boolean;

init() {
this.requiredPermissions = ['read'];
this.requiredDeviceCapabilities = ['Capability_Cardano'];
this.firmwareRange = getFirmwareRange(
this.name,
getMiscNetwork('Cardano'),
this.firmwareRange,
);

// create a bundle with only one batch if bundle doesn't exists
this.hasBundle = !!this.payload.bundle;
Expand Down
12 changes: 2 additions & 10 deletions packages/connect/src/api/cardano/api/cardanoSignTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import { trezorUtils } from '@fivebinaries/coin-selection';

import { AssertWeak, Type } 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 {
modifyAuxiliaryDataForBackwardsCompatibility,
Expand Down Expand Up @@ -37,6 +34,7 @@ import {
import { gatherWitnessPaths } from '../cardanoWitnesses';
import type { AssetGroupWithTokens } from '../cardanoTokenBundle';
import { tokenBundleToProto } from '../cardanoTokenBundle';
import { CardanoAbstractMethod } from './cardanoAbstractMethods';

const CardanoSignTransactionFeatures = Object.freeze({
// FW <2.6.0 is not supported by Connect at all
Expand Down Expand Up @@ -72,18 +70,12 @@ export type CardanoSignTransactionParams = {
chunkify?: boolean;
};

export default class CardanoSignTransaction extends AbstractMethod<
export default class CardanoSignTransaction extends CardanoAbstractMethod<
'cardanoSignTransaction',
CardanoSignTransactionParams
> {
init() {
this.requiredPermissions = ['read', 'write'];
this.requiredDeviceCapabilities = ['Capability_Cardano'];
this.firmwareRange = getFirmwareRange(
this.name,
getMiscNetwork('Cardano'),
this.firmwareRange,
);

const { payload } = this;

Expand Down
3 changes: 3 additions & 0 deletions packages/connect/src/core/AbstractMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ export abstract class AbstractMethod<Name extends CallMethodPayload['method'], P
}

checkDeviceCapability() {
if (!this.useDevice) {
return true;
}
const deviceHasAllRequiredCapabilities = (this.requiredDeviceCapabilities || []).every(
capability => this.device.features.capabilities.includes(capability),
);
Expand Down

0 comments on commit 2f80653

Please sign in to comment.