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 d2178e1
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 43 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';
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,
);
}
}
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';
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 d2178e1

Please sign in to comment.