Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[discuss] chore(connect): coinAbstractMethod #15304

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/connect/src/api/binance/api/binanceAbstractMethods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Capability } from '@trezor/protobuf/src/messages-schema';

Check warning on line 1 in packages/connect/src/api/binance/api/binanceAbstractMethods.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 BinanceAbstractMethod<
Name extends CallMethodPayload['method'],
Params = undefined,
> extends AbstractMethod<Name, Params> {
requiredDeviceCapabilities = [Capability.Binance];

constructor(message: { id?: number; payload: Payload<Name> }) {
super(message);
this.firmwareRange = getFirmwareRange(this.name, getMiscNetwork('BNB'), this.firmwareRange);
}
}
12 changes: 6 additions & 6 deletions packages/connect/src/api/binance/api/binanceGetAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

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 { validatePath, fromHardened, getSerializedPath } from '../../../utils/pathUtils';
import { PROTO, ERRORS } from '../../../constants';
import { UI, createUiMessage } from '../../../events';
import { Bundle } from '../../../types';
import { GetAddress as GetAddressSchema } from '../../../types/api/getAddress';
import { BinanceAbstractMethod } from './binanceAbstractMethods';

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

export default class BinanceGetAddress extends AbstractMethod<'binanceGetAddress', Params[]> {
export default class BinanceGetAddress extends BinanceAbstractMethod<
'binanceGetAddress',
Params[]
> {
Comment on lines +17 to +20
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I give it a shorter name + remove comments, I am optimistic that this might be a line saving solution :D

hasBundle?: boolean;
progress = 0;

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

// create a bundle with only one batch if bundle doesn't exists
this.hasBundle = !!this.payload.bundle;
Expand Down
9 changes: 3 additions & 6 deletions packages/connect/src/api/binance/api/binanceGetPublicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@

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 { validatePath, fromHardened, getSerializedPath } from '../../../utils/pathUtils';
import { UI, createUiMessage } from '../../../events';
import type { PROTO } from '../../../constants';
import { Bundle, GetPublicKey as GetPublicKeySchema } from '../../../types';
import { BinanceAbstractMethod } from './binanceAbstractMethods';

export default class BinanceGetPublicKey extends AbstractMethod<
export default class BinanceGetPublicKey extends BinanceAbstractMethod<
'binanceGetPublicKey',
PROTO.BinanceGetPublicKey[]
> {
hasBundle?: boolean;

init() {
this.requiredPermissions = ['read'];
this.requiredDeviceCapabilities = ['Capability_Binance'];
this.firmwareRange = getFirmwareRange(this.name, getMiscNetwork('BNB'), 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 @@ -2,28 +2,24 @@

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 '../binanceSignTx';
import { BinanceSignTransaction as BinanceSignTransactionSchema } from '../../../types/api/binance';
import type { BinancePreparedTransaction } from '../../../types/api/binance';
import { BinanceAbstractMethod } from './binanceAbstractMethods';

type Params = {
path: number[];
transaction: BinancePreparedTransaction;
chunkify?: boolean;
};

export default class BinanceSignTransaction extends AbstractMethod<
export default class BinanceSignTransaction extends BinanceAbstractMethod<
'binanceSignTransaction',
Params
> {
init() {
this.requiredPermissions = ['read', 'write'];
this.requiredDeviceCapabilities = ['Capability_Binance'];
this.firmwareRange = getFirmwareRange(this.name, getMiscNetwork('BNB'), this.firmwareRange);

const { payload } = this;
// validate incoming parameters
Expand Down
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;
}
Comment on lines +344 to +346
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if cardanoComposeTransaction, or any method that has useDevice: false should also inherit from a coin specific abstract method, we need to handle cases like this. I am starting to dislike it

const deviceHasAllRequiredCapabilities = (this.requiredDeviceCapabilities || []).every(
capability => this.device.features.capabilities.includes(capability),
);
Expand Down
Loading