From aa16afa014db63e9424023e06873e5a3857af8a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomislav=20Hora=C4=8Dek?= Date: Thu, 7 Sep 2023 10:28:23 +0200 Subject: [PATCH] test(hardware-trezor): add mapped trezor tx unit test --- .../hardware-trezor/src/TrezorKeyAgent.ts | 5 +- .../hardware-trezor/src/transformers/tx.ts | 31 +-- packages/hardware-trezor/test/testData.ts | 20 ++ .../test/transformers/tx.test.ts | 189 ++++++++++++++++++ 4 files changed, 226 insertions(+), 19 deletions(-) create mode 100644 packages/hardware-trezor/test/transformers/tx.test.ts diff --git a/packages/hardware-trezor/src/TrezorKeyAgent.ts b/packages/hardware-trezor/src/TrezorKeyAgent.ts index 49ed4270f60..655e1fb462d 100644 --- a/packages/hardware-trezor/src/TrezorKeyAgent.ts +++ b/packages/hardware-trezor/src/TrezorKeyAgent.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import * as Crypto from '@cardano-sdk/crypto'; import * as Trezor from '@trezor/connect'; -import { Cardano, NotImplementedError, coreToCml } from '@cardano-sdk/core'; +import { Cardano, NotImplementedError } from '@cardano-sdk/core'; import { CardanoKeyConst, CommunicationType, @@ -163,12 +163,9 @@ export class TrezorKeyAgent extends KeyAgentBase { const scope = new ManagedFreeableScope(); try { await this.isTrezorInitialized; - const cslTxBody = coreToCml.txBody(scope, tx.body); const trezorTxData = await txToTrezor({ - accountIndex: this.accountIndex, cardanoTxBody: tx.body, chainId: this.chainId, - cslTxBody, inputResolver: this.inputResolver, knownAddresses: this.knownAddresses }); diff --git a/packages/hardware-trezor/src/transformers/tx.ts b/packages/hardware-trezor/src/transformers/tx.ts index af97db9003b..1f9fbaa0e5f 100644 --- a/packages/hardware-trezor/src/transformers/tx.ts +++ b/packages/hardware-trezor/src/transformers/tx.ts @@ -1,7 +1,7 @@ import * as Trezor from '@trezor/connect'; import { Cardano } from '@cardano-sdk/core'; +import { GroupedAddress } from '@cardano-sdk/key-management'; import { TrezorTxTransformerContext } from '../types'; -import { util as deprecatedUtil } from '@cardano-sdk/key-management'; import { mapAdditionalWitnessRequests } from './additionalWitnessRequests'; import { mapAuxiliaryData, mapCerts, mapTxIns, mapTxOuts, mapWithdrawals } from './'; import { mapTokenMap } from './assets'; @@ -34,22 +34,23 @@ const trezorTxTransformer = async ( }; }; -/** - * Temporary props type extending the existing TxToTrezorProps - * and adding the core Cardano.TxBody so we can pass it to our - * override implementations. - */ -type TxToTrezorProps = deprecatedUtil.TxToTrezorProps & { - cardanoTxBody: Cardano.TxBody; -}; - /** * Takes a core transaction and context data necessary to transform * it into a trezor.CardanoSignTransaction */ -export const txToTrezor = (props: TxToTrezorProps): Promise> => - trezorTxTransformer(props.cardanoTxBody, { - chainId: props.chainId, - inputResolver: props.inputResolver, - knownAddresses: props.knownAddresses +export const txToTrezor = ({ + cardanoTxBody, + chainId, + inputResolver, + knownAddresses +}: { + chainId: Cardano.ChainId; + inputResolver: Cardano.InputResolver; + knownAddresses: GroupedAddress[]; + cardanoTxBody: Cardano.TxBody; +}): Promise> => + trezorTxTransformer(cardanoTxBody, { + chainId, + inputResolver, + knownAddresses }); diff --git a/packages/hardware-trezor/test/testData.ts b/packages/hardware-trezor/test/testData.ts index e6ad05491c8..628932674d5 100644 --- a/packages/hardware-trezor/test/testData.ts +++ b/packages/hardware-trezor/test/testData.ts @@ -175,3 +175,23 @@ export const poolRegistrationCertificate = { vrf } } as Cardano.PoolRegistrationCertificate; + +export const minValidTxBody: Cardano.TxBody = { + fee: 10n, + inputs: [txIn], + outputs: [txOut] +}; + +export const txBody: Cardano.TxBody = { + auxiliaryDataHash, + certificates: [stakeDelegationCertificate], + fee: 10n, + inputs: [txIn], + mint: mintTokenMap, + outputs: [txOutWithAssets, txOutWithAssetsToOwnedAddress], + validityInterval: { + invalidBefore: Cardano.Slot(100), + invalidHereafter: Cardano.Slot(1000) + }, + withdrawals: [coreWithdrawalWithKeyHashCredential] +}; diff --git a/packages/hardware-trezor/test/transformers/tx.test.ts b/packages/hardware-trezor/test/transformers/tx.test.ts new file mode 100644 index 00000000000..3aa5b22d01b --- /dev/null +++ b/packages/hardware-trezor/test/transformers/tx.test.ts @@ -0,0 +1,189 @@ +import * as Trezor from '@trezor/connect'; +import { CardanoKeyConst, util } from '@cardano-sdk/key-management'; +import { + contextWithKnownAddresses, + contextWithoutKnownAddresses, + knownAddressKeyPath, + knownAddressStakeKeyPath, + minValidTxBody, + txBody, + txIn +} from '../testData'; +import { txToTrezor } from '../../src/transformers/tx'; + +describe('tx', () => { + describe('txToTrezor', () => { + test('can map min valid transaction', async () => { + expect( + await txToTrezor({ + ...contextWithoutKnownAddresses, + cardanoTxBody: minValidTxBody + }) + ).toEqual({ + additionalWitnessRequests: [], + auxiliaryData: undefined, + certificates: [], + fee: '10', + inputs: [ + { + prev_hash: txIn.txId, + prev_index: txIn.index + } + ], + mint: undefined, + networkId: 0, + outputs: [ + { + address: + 'addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp', + amount: '10' + } + ], + protocolMagic: 999, + ttl: undefined, + validityIntervalStart: undefined, + withdrawals: [] + }); + }); + + test('can map transaction', async () => { + expect( + await txToTrezor({ + ...contextWithKnownAddresses, + cardanoTxBody: txBody + }) + ).toEqual({ + additionalWitnessRequests: [ + [2_147_485_500, 2_147_485_463, 2_147_483_648, 1, 0], // payment key path + [2_147_485_500, 2_147_485_463, 2_147_483_648, 2, 0] // reward account key path + ], + auxiliaryData: { + hash: '2ceb364d93225b4a0f004a0975a13eb50c3cc6348474b4fe9121f8dc72ca0cfa' + }, + certificates: [ + { + path: [util.harden(CardanoKeyConst.PURPOSE), util.harden(CardanoKeyConst.COIN_TYPE), util.harden(0), 2, 0], + pool: '153806dbcd134ddee69a8c5204e38ac80448f62342f8c23cfe4b7edf', + type: Trezor.PROTO.CardanoCertificateType.STAKE_DELEGATION + } + ], + fee: '10', + inputs: [ + { + path: knownAddressKeyPath, + prev_hash: txIn.txId, + prev_index: txIn.index + } + ], + mint: [ + { + policyId: '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740', + tokenAmounts: [{ amount: '20', assetNameBytes: '' }] + }, + { + policyId: '659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba82', + tokenAmounts: [{ amount: '-50', assetNameBytes: '54534c41' }] + }, + { + policyId: '7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373', + tokenAmounts: [ + { amount: '40', assetNameBytes: '' }, + { amount: '30', assetNameBytes: '504154415445' } + ] + } + ], + networkId: 0, + outputs: [ + { + address: + 'addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp', + amount: '10', + tokenBundle: [ + { + policyId: '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740', + tokenAmounts: [ + { + amount: '20', + assetNameBytes: '' + } + ] + }, + { + policyId: '659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba82', + tokenAmounts: [ + { + amount: '50', + assetNameBytes: '54534c41' + } + ] + }, + { + policyId: '7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373', + tokenAmounts: [ + { + amount: '40', + assetNameBytes: '' + }, + { + amount: '30', + assetNameBytes: '504154415445' + } + ] + } + ] + }, + { + addressParameters: { + addressType: Trezor.PROTO.CardanoAddressType.BASE, + path: knownAddressKeyPath, + stakingPath: knownAddressStakeKeyPath + }, + amount: '10', + tokenBundle: [ + { + policyId: '2a286ad895d091f2b3d168a6091ad2627d30a72761a5bc36eef00740', + tokenAmounts: [ + { + amount: '20', + assetNameBytes: '' + } + ] + }, + { + policyId: '659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba82', + tokenAmounts: [ + { + amount: '50', + assetNameBytes: '54534c41' + } + ] + }, + { + policyId: '7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373', + tokenAmounts: [ + { + amount: '40', + assetNameBytes: '' + }, + { + amount: '30', + assetNameBytes: '504154415445' + } + ] + } + ] + } + ], + protocolMagic: 999, + ttl: '1000', + validityIntervalStart: '100', + withdrawals: [ + { + amount: '5', + path: [util.harden(CardanoKeyConst.PURPOSE), util.harden(CardanoKeyConst.COIN_TYPE), util.harden(0), 2, 0] + } + ] + }); + }); + }); +});