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

Cardano Conway certificates #28

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/packages/connect/methods/cardanoSignTransaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const result = await TrezorConnect.cardanoSignTransaction(params);
- `derivationType` — _optional_ `CardanoDerivationType` enum. Determines used derivation type. Default is set to ICARUS_TREZOR=2.
- `includeNetworkId` — _optional_ `Boolean`. Determines whether `networkId` should be explicitly serialized into the transaction body. Default is `false`.
- `chunkify` — _optional_ `boolean` determines if recipient address will be displayed in chunks of 4 characters. Default is set to `false`
- `tagCborSets` - _optional_ `boolean` determines if CBOR arrays intended to be sets will be encoded with tag 258. Default is set to `false`

### CardanoTxSigningMode

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export const paramDescriptions = {
'Determines whether `networkId` should be explicitly serialized into the transaction body. Default is `false`.',
chunkify:
'determines if recipient address will be displayed in chunks of 4 characters. Default is set to `false`',
tagCborSets:
'determines if CBOR arrays intended to be sets will be encoded with tag 258. Default is set to `false`',
};

## Cardano: Sign transaction
Expand Down
347 changes: 347 additions & 0 deletions packages/connect/e2e/__fixtures__/cardanoSignTransaction.ts

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions packages/connect/src/api/cardano/api/cardanoSignTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import { tokenBundleToProto } from '../cardanoTokenBundle';
import { AssertWeak, Type } from '@trezor/schema-utils';

const CardanoSignTransactionFeatures = Object.freeze({
// Minimum firmware (2.6.0) currently supports all features
// FW <2.6.0 is not supported by Connect at all
Conway: ['0', '2.7.1'],
});

export type CardanoSignTransactionParams = {
Expand All @@ -64,6 +65,7 @@ export type CardanoSignTransactionParams = {
additionalWitnessRequests: Path[];
derivationType: PROTO.CardanoDerivationType;
includeNetworkId?: boolean;
tagCborSets?: boolean;
unsignedTx?: { body: string; hash: string };
testnet?: boolean;
chunkify?: boolean;
Expand Down Expand Up @@ -222,6 +224,7 @@ export default class CardanoSignTransaction extends AbstractMethod<
? payload.derivationType
: PROTO.CardanoDerivationType.ICARUS_TREZOR,
includeNetworkId: payload.includeNetworkId,
tagCborSets: payload.tagCborSets,
unsignedTx: 'unsignedTx' in payload ? payload.unsignedTx : undefined,
testnet: 'testnet' in payload ? payload.testnet : undefined,
chunkify: typeof payload.chunkify === 'boolean' ? payload.chunkify : false,
Expand All @@ -246,7 +249,21 @@ export default class CardanoSignTransaction extends AbstractMethod<
}

_ensureFirmwareSupportsParams() {
// Currently, there are no additional features to check for
const { params } = this;

params.certificatesWithPoolOwnersAndRelays.forEach(({ certificate }) => {
if (
certificate.type === PROTO.CardanoCertificateType.STAKE_REGISTRATION_CONWAY ||
certificate.type === PROTO.CardanoCertificateType.STAKE_DEREGISTRATION_CONWAY ||
certificate.type === PROTO.CardanoCertificateType.VOTE_DELEGATION
) {
this._ensureFeatureIsSupported('Conway');
jaskp marked this conversation as resolved.
Show resolved Hide resolved
}
});

if (params.tagCborSets) {
this._ensureFeatureIsSupported('Conway');
}
}

async _sign_tx(): Promise<CardanoSignedTxData> {
Expand Down Expand Up @@ -277,6 +294,7 @@ export default class CardanoSignTransaction extends AbstractMethod<
derivation_type: this.params.derivationType,
include_network_id: this.params.includeNetworkId,
chunkify: this.params.chunkify,
tag_cbor_sets: this.params.tagCborSets,
};

// init
Expand Down
55 changes: 55 additions & 0 deletions packages/connect/src/api/cardano/cardanoCertificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CardanoPoolParameters,
CardanoPoolOwner,
CardanoPoolRelay,
CardanoDRep,
} from '../../types/api/cardano';
import { Assert } from '@trezor/schema-utils';

Expand Down Expand Up @@ -68,6 +69,21 @@ const validatePoolParameters = (poolParameters: CardanoPoolParameters) => {
poolParameters.relays.forEach(validatePoolRelay);
};

const validateDRep = (dRep: CardanoDRep) => {
Assert(CardanoDRep, dRep);
if (dRep.type === PROTO.CardanoDRepType.KEY_HASH && !dRep.keyHash) {
throw ERRORS.TypedError(
'Method_InvalidParameter',
'keyHash must be supplied for KEY_HASH dRep type',
);
} else if (dRep.type === PROTO.CardanoDRepType.SCRIPT_HASH && !dRep.scriptHash) {
throw ERRORS.TypedError(
'Method_InvalidParameter',
'scriptHash must be supplied for SCRIPT_HASH dRep type',
);
}
jaskp marked this conversation as resolved.
Show resolved Hide resolved
};

export type CertificateWithPoolOwnersAndRelays = {
certificate: PROTO.CardanoTxCertificate;
poolOwners: PROTO.CardanoPoolOwner[];
Expand Down Expand Up @@ -118,6 +134,20 @@ const transformPoolParameters = (
};
};

const transformDRep = (dRep: CardanoDRep | undefined): PROTO.CardanoDRep | undefined => {
if (!dRep) {
return undefined;
}

validateDRep(dRep);

return {
type: dRep.type,
key_hash: dRep.keyHash,
script_hash: dRep.scriptHash,
};
};

// transform incoming certificate object to protobuf messages format
export const transformCertificate = (
certificate: CardanoCertificate,
Expand All @@ -142,10 +172,33 @@ export const transformCertificate = (
}
}

if (
certificate.type === PROTO.CardanoCertificateType.STAKE_REGISTRATION_CONWAY ||
certificate.type === PROTO.CardanoCertificateType.STAKE_DEREGISTRATION_CONWAY
) {
if (!certificate.deposit) {
throw ERRORS.TypedError(
'Method_InvalidParameter',
'deposit must be supplied for STAKE_REGISTRATION_CONWAY or STAKE_DEREGISTRATION_CONWAY',
);
}
}

if (certificate.type === PROTO.CardanoCertificateType.VOTE_DELEGATION) {
if (!certificate.dRep) {
throw ERRORS.TypedError(
'Method_InvalidParameter',
'dRep must be supplied for VOTE_DELEGATION',
);
}
}

const { poolParameters, poolOwners, poolRelays } = transformPoolParameters(
certificate.poolParameters,
);

const dRep = transformDRep(certificate.dRep);

return {
certificate: {
type: certificate.type,
Expand All @@ -154,6 +207,8 @@ export const transformCertificate = (
key_hash: certificate.keyHash,
pool: certificate.pool,
pool_parameters: poolParameters,
deposit: certificate.deposit,
drep: dRep,
},
poolOwners,
poolRelays,
Expand Down
1 change: 1 addition & 0 deletions packages/connect/src/api/cardano/cardanoUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const prepareCertificates = (certs: CardanoCertificate[]) => {
convertedCerts.push({
type: cert.type,
});
// TODO conway certificates not supported by coin-selection lib yet
break;
// no default
}
Expand Down
5 changes: 4 additions & 1 deletion packages/connect/src/api/cardano/cardanoWitnesses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export const gatherWitnessPaths = (
if (
certificate.path &&
(certificate.type === PROTO.CardanoCertificateType.STAKE_DELEGATION ||
certificate.type === PROTO.CardanoCertificateType.STAKE_DEREGISTRATION)
certificate.type === PROTO.CardanoCertificateType.STAKE_DEREGISTRATION ||
certificate.type === PROTO.CardanoCertificateType.STAKE_REGISTRATION_CONWAY ||
certificate.type === PROTO.CardanoCertificateType.STAKE_DEREGISTRATION_CONWAY ||
certificate.type === PROTO.CardanoCertificateType.VOTE_DELEGATION)
) {
_insert(certificate.path);
}
Expand Down
1 change: 1 addition & 0 deletions packages/connect/src/types/api/__tests__/cardano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ export const cardanoSignTransaction = async (api: TrezorConnect) => {
networkId: 0,
signingMode: CardanoTxSigningMode.ORDINARY_TRANSACTION,
includeNetworkId: false,
tagCborSets: false,
});

if (sign.success) {
Expand Down
10 changes: 10 additions & 0 deletions packages/connect/src/types/api/cardano/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ export const CardanoPoolParameters = Type.Object({
metadata: Type.Optional(CardanoPoolMetadata),
});

export type CardanoDRep = Static<typeof CardanoDRep>;
export const CardanoDRep = Type.Object({
type: PROTO.EnumCardanoDRepType,
keyHash: Type.Optional(Type.String()),
scriptHash: Type.Optional(Type.String()),
});

export type CardanoCertificate = Static<typeof CardanoCertificate>;
export const CardanoCertificate = Type.Object({
type: PROTO.EnumCardanoCertificateType,
Expand All @@ -173,6 +180,8 @@ export const CardanoCertificate = Type.Object({
poolParameters: Type.Optional(CardanoPoolParameters),
scriptHash: Type.Optional(Type.String()),
keyHash: Type.Optional(Type.String()),
deposit: Type.Optional(Type.String()),
dRep: Type.Optional(CardanoDRep),
});

export type CardanoWithdrawal = Static<typeof CardanoWithdrawal>;
Expand Down Expand Up @@ -253,6 +262,7 @@ export const CardanoSignTransaction = Type.Object({
derivationType: Type.Optional(PROTO.EnumCardanoDerivationType),
includeNetworkId: Type.Optional(Type.Boolean()),
chunkify: Type.Optional(Type.Boolean()),
tagCborSets: Type.Optional(Type.Boolean()),
});

export type CardanoSignTransactionExtended = Static<typeof CardanoSignTransactionExtended>;
Expand Down
1 change: 1 addition & 0 deletions packages/protobuf/scripts/protobuf-patches/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const TYPE_PATCH = {
'CardanoPoolParametersType.cost': UINT_TYPE,
'CardanoPoolParametersType.margin_numerator': UINT_TYPE,
'CardanoPoolParametersType.margin_denominator': UINT_TYPE,
'CardanoTxCertificate.deposit': UINT_TYPE,
'CardanoSignTxInit.ttl': UINT_TYPE,
'CardanoSignTxInit.validity_interval_start': UINT_TYPE,
'CardanoSignTxInit.total_collateral': UINT_TYPE,
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/messages-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ export const CardanoTxCertificate = Type.Object(
pool_parameters: Type.Optional(CardanoPoolParametersType),
script_hash: Type.Optional(Type.String()),
key_hash: Type.Optional(Type.String()),
deposit: Type.Optional(Type.Number()),
deposit: Type.Optional(Type.Uint()),
drep: Type.Optional(CardanoDRep),
},
{ $id: 'CardanoTxCertificate' },
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ export type CardanoTxCertificate = {
pool_parameters?: CardanoPoolParametersType;
script_hash?: string;
key_hash?: string;
deposit?: number;
deposit?: UintType;
drep?: CardanoDRep;
};

Expand Down