Skip to content

Commit

Permalink
test(wallet): improve hw signing test
Browse files Browse the repository at this point in the history
  • Loading branch information
tomislavhoracek committed May 20, 2022
1 parent 350b193 commit a7cbf49
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
4 changes: 2 additions & 2 deletions packages/wallet/src/KeyManagement/LedgerKeyAgent.ts
Expand Up @@ -9,9 +9,9 @@ import {
SignBlobResult,
SignTransactionOptions
} from './types';
import { DerivationPath, txToLedger } from './util';
import { KeyAgentBase } from './KeyAgentBase';
import { TxInternals } from '../Transaction';
import { txToLedger } from './util';
import LedgerConnection, { GetVersionResponse, HARDENED, utils } from '@cardano-foundation/ledgerjs-hw-app-cardano';
import TransportNodeHid from '@ledgerhq/hw-transport-node-hid-noevents';
import TransportWebHID from '@ledgerhq/hw-transport-webhid';
Expand Down Expand Up @@ -164,7 +164,7 @@ export class LedgerKeyAgent extends KeyAgentBase {
}: GetLedgerXpubProps): Promise<Cardano.Bip32PublicKey> {
try {
const recoveredDeviceConnection = await LedgerKeyAgent.checkDeviceConnection(communicationType, deviceConnection);
const derivationPath = `1852'/1815'/${accountIndex}'`;
const derivationPath = `${DerivationPath.Purpose}'/${DerivationPath.CoinType}'/${accountIndex}'`;
const extendedPublicKey = await recoveredDeviceConnection.getExtendedPublicKey({
path: utils.str_to_path(derivationPath) // BIP32Path
});
Expand Down
4 changes: 2 additions & 2 deletions packages/wallet/src/KeyManagement/TrezorKeyAgent.ts
Expand Up @@ -9,9 +9,9 @@ import {
SignTransactionOptions,
TrezorConfig
} from './types';
import { DerivationPath, txToTrezor } from './util';
import { KeyAgentBase } from './KeyAgentBase';
import { TxInternals } from '../Transaction';
import { txToTrezor } from './util';
import TrezorConnect, { Features } from 'trezor-connect';

export interface TrezorKeyAgentProps extends Omit<SerializableTrezorKeyAgentData, '__typename'> {
Expand Down Expand Up @@ -93,7 +93,7 @@ export class TrezorKeyAgent extends KeyAgentBase {
static async getXpub({ accountIndex }: GetTrezorXpubProps): Promise<Cardano.Bip32PublicKey> {
try {
await TrezorKeyAgent.checkDeviceConnection();
const derivationPath = `m/1852'/1815'/${accountIndex}'`;
const derivationPath = `m/${DerivationPath.Purpose}'/${DerivationPath.CoinType}'/${accountIndex}'`;
const extendedPublicKey = await TrezorConnect.cardanoGetPublicKey({
path: derivationPath,
showOnTrezor: true
Expand Down
6 changes: 5 additions & 1 deletion packages/wallet/src/KeyManagement/util/key.ts
@@ -1,5 +1,6 @@
import { AccountKeyDerivationPath, KeyRole } from '../types';
import { CSL } from '@cardano-sdk/core';
import { DerivationPath } from '.';

export const harden = (num: number): number => 0x80_00_00_00 + num;

Expand All @@ -9,4 +10,7 @@ export const STAKE_KEY_DERIVATION_PATH: AccountKeyDerivationPath = {
};

export const deriveAccountPrivateKey = (rootPrivateKey: CSL.Bip32PrivateKey, accountIndex: number) =>
rootPrivateKey.derive(harden(1852)).derive(harden(1815)).derive(harden(accountIndex));
rootPrivateKey
.derive(harden(DerivationPath.Purpose))
.derive(harden(DerivationPath.CoinType))
.derive(harden(accountIndex));
15 changes: 9 additions & 6 deletions packages/wallet/test/hardware/LedgerKeyAgent.test.ts
Expand Up @@ -58,7 +58,7 @@ describe('LedgerKeyAgent', () => {
expect(typeof keyAgent.extendedAccountPublicKey).toBe('string');
});

test('sign and submit tx', async () => {
test('sign tx', async () => {
const outputs = [
{
address: Cardano.Address(
Expand All @@ -80,11 +80,14 @@ describe('LedgerKeyAgent', () => {
outputs: new Set<Cardano.TxOut>(outputs)
};
const txInternals = await wallet.initializeTx(props);
const tx = await wallet.finalizeTx(txInternals);
expect(tx.body).toBe(txInternals.body);
expect(tx.id).toBe(txInternals.hash);
expect(tx.witness.signatures.size).toBe(1);
await expect(wallet.submitTx(tx)).resolves.not.toThrow();
const signatures = await keyAgent.signTransaction(
{
body: txInternals.body,
hash: txInternals.hash
},
{ inputAddressResolver: wallet.util.resolveInputAddress }
);
expect(signatures.size).toBe(1);
});

describe('establish, check and re-establish device connection', () => {
Expand Down
15 changes: 9 additions & 6 deletions packages/wallet/test/hardware/TrezorKeyAgent.test.ts
Expand Up @@ -62,7 +62,7 @@ describe('TrezorKeyAgent', () => {
expect(typeof keyAgent.extendedAccountPublicKey).toBe('string');
});

test('sign and submit tx', async () => {
test('sign tx', async () => {
const outputs = [
{
address: Cardano.Address(
Expand All @@ -84,11 +84,14 @@ describe('TrezorKeyAgent', () => {
outputs: new Set<Cardano.TxOut>(outputs)
};
const txInternals = await wallet.initializeTx(props);
const tx = await wallet.finalizeTx(txInternals);
expect(tx.body).toBe(txInternals.body);
expect(tx.id).toBe(txInternals.hash);
expect(tx.witness.signatures.size).toBe(1);
await expect(wallet.submitTx(tx)).resolves.not.toThrow();
const signatures = await keyAgent.signTransaction(
{
body: txInternals.body,
hash: txInternals.hash
},
{ inputAddressResolver: wallet.util.resolveInputAddress }
);
expect(signatures.size).toBe(1);
});

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

0 comments on commit a7cbf49

Please sign in to comment.