Skip to content
Merged
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
5 changes: 5 additions & 0 deletions packages/hardware-trezor/src/TrezorKeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ export class TrezorKeyAgent extends KeyAgentBase {
}

const signedData = result.payload;

if (signedData.hash !== tx.hash) {
throw new errors.HwMappingError('Trezor computed a different transaction id');
}

return new Map<Crypto.Ed25519PublicKeyHex, Crypto.Ed25519SignatureHex>(
await Promise.all(
signedData.witnesses.map(async (witness) => {
Expand Down
9 changes: 7 additions & 2 deletions packages/key-management/src/TrezorKeyAgent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as Crypto from '@cardano-sdk/crypto';
import { AuthenticationError, TransportError } from './errors';
import { AuthenticationError, HwMappingError, TransportError } from './errors';
import { Cardano, NotImplementedError, coreToCml } from '@cardano-sdk/core';
import {
CardanoKeyConst,
Expand Down Expand Up @@ -147,7 +147,7 @@ export class TrezorKeyAgent extends KeyAgentBase {
);
}

async signTransaction({ body }: Cardano.TxBodyWithHash): Promise<Cardano.Signatures> {
async signTransaction({ body, hash }: Cardano.TxBodyWithHash): Promise<Cardano.Signatures> {
const scope = new ManagedFreeableScope();
try {
await this.isTrezorInitialized;
Expand All @@ -167,6 +167,11 @@ export class TrezorKeyAgent extends KeyAgentBase {
}

const signedData = result.payload;

if (signedData.hash !== hash) {
throw new HwMappingError('Trezor computed a different transaction id');
}

return new Map<Crypto.Ed25519PublicKeyHex, Crypto.Ed25519SignatureHex>(
await Promise.all(
signedData.witnesses.map(async (witness) => {
Expand Down
73 changes: 37 additions & 36 deletions packages/wallet/test/hardware/trezor/TrezorKeyAgent.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import * as Crypto from '@cardano-sdk/crypto';
import {
AddressType,
CommunicationType,
GroupedAddress,
SerializableTrezorKeyAgentData,
util
} from '@cardano-sdk/key-management';
import { AddressType, CommunicationType, SerializableTrezorKeyAgentData, util } from '@cardano-sdk/key-management';
import { AssetId, createStubStakePoolProvider, mockProviders as mocks } from '@cardano-sdk/util-dev';
import { CML, Cardano } from '@cardano-sdk/core';
import { InitializeTxProps, InitializeTxResult } from '@cardano-sdk/tx-construction';
import { PersonalWallet, setupWallet } from '../../../src';
import { TrezorKeyAgent } from '@cardano-sdk/hardware-trezor';
import { dummyLogger as logger } from 'ts-log';
import { mockKeyAgentDependencies, stakeKeyDerivationPath } from '../../../../key-management/test/mocks';
import { mockKeyAgentDependencies } from '../../../../key-management/test/mocks';

describe('TrezorKeyAgent', () => {
let keyAgent: TrezorKeyAgent;
Expand All @@ -29,38 +24,28 @@ describe('TrezorKeyAgent', () => {
txSubmitProvider = mocks.mockTxSubmitProvider();
({ keyAgent, wallet } = await setupWallet({
bip32Ed25519: new Crypto.CmlBip32Ed25519(CML),

createKeyAgent: async (dependencies) => {
const trezorKeyAgent = await TrezorKeyAgent.createWithDevice(
createKeyAgent: async (dependencies) =>
await TrezorKeyAgent.createWithDevice(
{
chainId: Cardano.ChainIds.LegacyTestnet,
trezorConfig
},
dependencies
);
const groupedAddress: GroupedAddress = {
accountIndex: 0,
address: mocks.utxo[0][0].address,
index: 0,
networkId: Cardano.NetworkId.Testnet,
rewardAccount: mocks.rewardAccount,
stakeKeyDerivationPath,
type: AddressType.External
};
trezorKeyAgent.deriveAddress = jest.fn().mockResolvedValue(groupedAddress);
trezorKeyAgent.knownAddresses.push(groupedAddress);
return trezorKeyAgent;
},
),
createWallet: async (trezorKeyAgent) => {
const { address, rewardAccount } = await trezorKeyAgent.deriveAddress(
{ index: 0, type: AddressType.External },
0
);
const assetProvider = mocks.mockAssetProvider();
const stakePoolProvider = createStubStakePoolProvider();
const networkInfoProvider = mocks.mockNetworkInfoProvider();
const rewardsProvider = mocks.mockRewardsProvider();
const chainHistoryProvider = mocks.mockChainHistoryProvider();
const utxoProvider = mocks.mockUtxoProvider();
const utxoProvider = mocks.mockUtxoProvider({ address });
const rewardsProvider = mocks.mockRewardsProvider({ rewardAccount });
const chainHistoryProvider = mocks.mockChainHistoryProvider({ rewardAccount });
const asyncKeyAgent = util.createAsyncKeyAgent(trezorKeyAgent);
return new PersonalWallet(
{ name: 'Trezor Wallet' },
{ name: 'HW Wallet' },
{
assetProvider,
chainHistoryProvider,
Expand Down Expand Up @@ -114,7 +99,7 @@ describe('TrezorKeyAgent', () => {
expect(typeof keyAgent.extendedAccountPublicKey).toBe('string');
});

test('sign tx', async () => {
describe('sign transaction', () => {
const outputs = [
{
address: Cardano.PaymentAddress(
Expand All @@ -132,15 +117,31 @@ describe('TrezorKeyAgent', () => {
}
}
];
const props = {
const props: InitializeTxProps = {
outputs: new Set<Cardano.TxOut>(outputs)
};
const txInternals = await wallet.initializeTx(props);
const signatures = await keyAgent.signTransaction({
body: txInternals.body,
hash: txInternals.hash
let txInternals: InitializeTxResult;

beforeAll(async () => {
txInternals = await wallet.initializeTx(props);
});

it('successfully signs a transaction with assets', async () => {
const signatures = await keyAgent.signTransaction({
body: txInternals.body,
hash: txInternals.hash
});
expect(signatures.size).toBe(2);
});

it('throws if signed transaction hash doesnt match hash computed by the wallet', async () => {
await expect(
keyAgent.signTransaction({
body: txInternals.body,
hash: 'non-matching' as unknown as Cardano.TransactionId
})
).rejects.toThrow();
});
expect(signatures.size).toBe(2);
});

describe('serializableData', () => {
Expand Down