Skip to content

Commit

Permalink
test: fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
greatertomi committed May 30, 2024
1 parent c21d193 commit 8e238da
Show file tree
Hide file tree
Showing 45 changed files with 451 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { validateFuzzyOptions, withTextFilter } from '../../../src/StakePool/Typ
describe('TypeormStakePoolProvider utils', () => {
describe('validateFuzzyOptions', () => {
it('throws if value is not a valid JSON encoded string', () =>
expect(() => validateFuzzyOptions('test')).toThrow('Unexpected token e in JSON at position 1'));
expect(() => validateFuzzyOptions('test')).toThrow(/Unexpected token/));
it('throws if value is not an object', () =>
expect(() => validateFuzzyOptions('"test"')).toThrow('must be an object'));
it('throws without threshold', () =>
Expand Down
6 changes: 3 additions & 3 deletions packages/hardware-ledger/src/transformers/certificates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const getCredentialType = (knownAddress: GroupedAddress | undefined) => {
const stakeCredentialMapper = (credential: Cardano.Credential, context: LedgerTxTransformerContext) => {
const knownAddress = getKnownAddress(credential, context);
const credentialType = getCredentialType(knownAddress);
const path = util.stakeKeyPathFromGroupedAddress({ address: knownAddress });
const path = util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: context.purpose });

return credentialMapper(credential, credentialType, path);
};
Expand Down Expand Up @@ -160,7 +160,7 @@ const getPoolOperatorKeyPath = (
context: LedgerTxTransformerContext
): Ledger.BIP32Path | null => {
const knownAddress = context?.knownAddresses.find((address) => address.rewardAccount === operator);
return util.stakeKeyPathFromGroupedAddress({ address: knownAddress });
return util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: context.purpose });
};

export const poolRegistrationCertificate: Transform<
Expand Down Expand Up @@ -270,7 +270,7 @@ const poolRetirementCertificate: Transform<
)
);

const poolKeyPath = util.stakeKeyPathFromGroupedAddress({ address: knownAddress });
const poolKeyPath = util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: context?.purpose });

if (!poolKeyPath) throw new InvalidArgumentError('certificate', 'Missing key matching pool retirement certificate.');

Expand Down
6 changes: 4 additions & 2 deletions packages/hardware-ledger/test/LedgerKeyAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Crypto from '@cardano-sdk/crypto';
import * as Ledger from '@cardano-foundation/ledgerjs-hw-app-cardano';
import { Ada, InvalidDataReason } from '@cardano-foundation/ledgerjs-hw-app-cardano';
import { Cardano } from '@cardano-sdk/core';
import { CardanoKeyConst, CommunicationType, util } from '@cardano-sdk/key-management';
import { CardanoKeyConst, CommunicationType, KeyPurpose, util } from '@cardano-sdk/key-management';
import { LedgerKeyAgent } from '../src';
import { dummyLogger } from 'ts-log';
import { poolId, poolParameters, pureAdaTxOut, stakeKeyHash, txIn, txOutWithDatum } from './testData';
Expand Down Expand Up @@ -366,6 +366,7 @@ describe('LedgerKeyAgent', () => {
const txId = '0000000000000000000000000000000000000000000000000000000000000000' as unknown as Cardano.TransactionId;
const noAddressesOptions = {
knownAddresses: [],
purpose: KeyPurpose.STANDARD,
txInKeyPathMap: {}
};

Expand All @@ -379,7 +380,8 @@ describe('LedgerKeyAgent', () => {
communicationType: CommunicationType.Node,
extendedAccountPublicKey: Crypto.Bip32PublicKeyHex(
'00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
)
),
purpose: KeyPurpose.STANDARD
},
{
bip32Ed25519: new Crypto.SodiumBip32Ed25519(),
Expand Down
63 changes: 48 additions & 15 deletions packages/hardware-ledger/test/transformers/certificates.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable sonarjs/no-duplicate-string */
import * as Ledger from '@cardano-foundation/ledgerjs-hw-app-cardano';
import {
AccountKeyDerivationPath,
AddressType,
CardanoKeyConst,
GroupedAddress,
KeyPurpose,
KeyRole,
TxInId,
TxInKeyPathMap,
Expand All @@ -22,23 +22,26 @@ import {
} from '../testData';
import { Cardano } from '@cardano-sdk/core';
import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
import { LedgerTxTransformerContext } from '../../src';
import { getKnownAddress, mapCerts } from '../../src/transformers/certificates';
import { LedgerTxTransformerContext, getKnownAddress, mapCerts } from '../../src';

export const stakeKeyPath = {
index: 0,
purpose: KeyPurpose.STANDARD,
role: KeyRole.Stake
};
const createGroupedAddress = (
address: Cardano.PaymentAddress,
rewardAccount: Cardano.RewardAccount,
type: AddressType,
index: number,
stakeKeyDerivationPath: AccountKeyDerivationPath
): GroupedAddress =>

const createGroupedAddress = ({
address,
rewardAccount,
stakeKeyDerivationPath,
index,
purpose,
type
}: Omit<GroupedAddress, 'networkId' | 'accountIndex'>): GroupedAddress =>
({
address,
index,
purpose,
rewardAccount,
stakeKeyDerivationPath,
type
Expand All @@ -64,6 +67,7 @@ export const createTxInKeyPathMapMock = (knownAddresses: GroupedAddress[]): TxIn
const txInId: TxInId = `MockTxIn_${index}` as TxInId; // Mock TxInId creation
result[txInId] = {
index: address.index,
purpose: address.purpose,
role: KeyRole.Internal
};
}
Expand All @@ -78,13 +82,42 @@ const mockContext: LedgerTxTransformerContext = {
handleResolutions: [],

knownAddresses: [
createGroupedAddress(address1, ownRewardAccount, AddressType.External, 0, stakeKeyPath),
createGroupedAddress(address2, ownRewardAccount, AddressType.External, 1, stakeKeyPath)
createGroupedAddress({
address: address1,
index: 0,
purpose: KeyPurpose.STANDARD,
rewardAccount: ownRewardAccount,
stakeKeyDerivationPath: stakeKeyPath,
type: AddressType.External
}),
createGroupedAddress({
address: address2,
index: 1,
purpose: KeyPurpose.STANDARD,
rewardAccount: ownRewardAccount,
stakeKeyDerivationPath: stakeKeyPath,
type: AddressType.External
})
],
purpose: KeyPurpose.STANDARD,
sender: undefined,
txInKeyPathMap: createTxInKeyPathMapMock([
createGroupedAddress(address1, ownRewardAccount, AddressType.External, 0, stakeKeyPath),
createGroupedAddress(address2, ownRewardAccount, AddressType.External, 1, stakeKeyPath)
createGroupedAddress({
address: address1,
index: 0,
purpose: KeyPurpose.STANDARD,
rewardAccount: ownRewardAccount,
stakeKeyDerivationPath: stakeKeyPath,
type: AddressType.External
}),
createGroupedAddress({
address: address2,
index: 1,
purpose: KeyPurpose.STANDARD,
rewardAccount: ownRewardAccount,
stakeKeyDerivationPath: stakeKeyPath,
type: AddressType.External
})
])
};

Expand Down Expand Up @@ -140,7 +173,7 @@ describe('certificates', () => {
params: {
stakeCredential: {
keyPath: [
util.harden(CardanoKeyConst.PURPOSE),
util.harden(KeyPurpose.STANDARD),
util.harden(CardanoKeyConst.COIN_TYPE),
util.harden(0),
KeyRole.Stake,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CONTEXT_WITH_KNOWN_ADDRESSES, txIn } from '../testData';
import { CardanoKeyConst, TxInId, util } from '@cardano-sdk/key-management';
import { CardanoKeyConst, KeyPurpose, TxInId, util } from '@cardano-sdk/key-management';
import { mapCollateralTxIns } from '../../src/transformers';

describe('collateralInputs', () => {
Expand All @@ -12,6 +12,7 @@ describe('collateralInputs', () => {
it('can map a a set of collateral inputs', async () => {
const keyPath = {
index: 0,
purpose: KeyPurpose.STANDARD,
role: 1
};
const txIns = mapCollateralTxIns([txIn, txIn, txIn], {
Expand Down
8 changes: 4 additions & 4 deletions packages/hardware-ledger/test/transformers/txIn.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { CONTEXT_WITHOUT_KNOWN_ADDRESSES, CONTEXT_WITH_KNOWN_ADDRESSES, txIn } from '../testData';
import { CardanoKeyConst, TxInId, util } from '@cardano-sdk/key-management';
import { mapTxIns, toTxIn } from '../../src/transformers';
import { CardanoKeyConst, KeyPurpose, TxInId, util } from '@cardano-sdk/key-management';
import { mapTxIns, toTxIn } from '../../src';

describe('txIn', () => {
const paymentKeyPath = { index: 0, role: 1 };
const paymentKeyPath = { index: 0, purpose: KeyPurpose.STANDARD, role: 1 };

describe('mapTxIns', () => {
it('can map a a set of TxIns', async () => {
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('txIn', () => {
expect(ledgerTxIn).toEqual({
outputIndex: txIn.index,
path: [
util.harden(CardanoKeyConst.PURPOSE),
util.harden(KeyPurpose.STANDARD),
util.harden(CardanoKeyConst.COIN_TYPE),
util.harden(CONTEXT_WITH_KNOWN_ADDRESSES.accountIndex),
paymentKeyPath.role,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export const mapAdditionalWitnessRequests: Transform<
const additionalWitnessPaths: Trezor.DerivationPath[] = [...paymentKeyPaths];

if (context?.knownAddresses?.length) {
const stakeKeyPath = util.stakeKeyPathFromGroupedAddress({ address: context.knownAddresses[0] });
const stakeKeyPath = util.stakeKeyPathFromGroupedAddress({
address: context.knownAddresses[0],
purpose: context.purpose
});
if (stakeKeyPath) additionalWitnessPaths.push(stakeKeyPath);
}
return additionalWitnessPaths;
Expand Down
15 changes: 9 additions & 6 deletions packages/hardware-trezor/src/transformers/certificates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Crypto from '@cardano-sdk/crypto';
import * as Trezor from '@trezor/connect';
import { BIP32Path } from '@cardano-sdk/crypto';
import { Cardano } from '@cardano-sdk/core';
import { GroupedAddress, util } from '@cardano-sdk/key-management';
import { GroupedAddress, KeyPurpose, util } from '@cardano-sdk/key-management';
import {
InvalidArgumentError,
Transform,
Expand All @@ -19,7 +19,8 @@ type CertCredentialsType = {

const getCertCredentials = (
stakeKeyHash: Crypto.Ed25519KeyHashHex,
knownAddresses: GroupedAddress[] | undefined
knownAddresses: GroupedAddress[] | undefined,
purpose: KeyPurpose | undefined
): CertCredentialsType => {
const knownAddress = knownAddresses?.find((address) =>
areStringsEqualInConstantTime(Cardano.RewardAccount.toHash(address.rewardAccount), stakeKeyHash)
Expand All @@ -30,7 +31,7 @@ const getCertCredentials = (
!!rewardAddress &&
areNumbersEqualInConstantTime(rewardAddress?.getPaymentCredential().type, Cardano.CredentialType.KeyHash)
) {
const path = util.stakeKeyPathFromGroupedAddress({ address: knownAddress });
const path = util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose });
return path ? { path } : { keyHash: stakeKeyHash };
}
return {
Expand All @@ -48,7 +49,7 @@ const getPoolOperatorKeyPath = (
context: TrezorTxTransformerContext
): BIP32Path | null => {
const knownAddress = context?.knownAddresses.find((address) => address.rewardAccount === operator);
return util.stakeKeyPathFromGroupedAddress({ address: knownAddress });
return util.stakeKeyPathFromGroupedAddress({ address: knownAddress, purpose: context.purpose });
};

export const getStakeAddressCertificate: Transform<
Expand All @@ -58,7 +59,8 @@ export const getStakeAddressCertificate: Transform<
> = (certificate, context) => {
const credentials = getCertCredentials(
certificate.stakeCredential.hash as unknown as Crypto.Ed25519KeyHashHex,
context?.knownAddresses
context?.knownAddresses,
context?.purpose
);
const certificateType =
certificate.__typename === Cardano.CertificateType.StakeRegistration
Expand All @@ -82,7 +84,8 @@ export const getStakeDelegationCertificate: Transform<
const poolIdKeyHash = Cardano.PoolId.toKeyHash(certificate.poolId);
const credentials = getCertCredentials(
certificate.stakeCredential.hash as unknown as Crypto.Ed25519KeyHashHex,
context?.knownAddresses
context?.knownAddresses,
context?.purpose
);
return {
keyHash: credentials.keyHash,
Expand Down
10 changes: 7 additions & 3 deletions packages/hardware-trezor/src/transformers/keyPaths.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BIP32Path } from '@cardano-sdk/crypto';
import { Cardano } from '@cardano-sdk/core';
import { GroupedAddress, TxInId, util } from '@cardano-sdk/key-management';
import { GroupedAddress, KeyPurpose, TxInId, util } from '@cardano-sdk/key-management';
import { TrezorTxTransformerContext } from '../types';

/** Uses the given Trezor input resolver to resolve the payment key path for known addresses for given input transaction. */
Expand All @@ -17,10 +17,14 @@ export const resolvePaymentKeyPathForTxIn = (

export const resolveStakeKeyPath = (
rewardAddress: Cardano.RewardAddress,
knownAddresses: GroupedAddress[]
knownAddresses: GroupedAddress[],
purpose: KeyPurpose
): BIP32Path | null => {
const knownAddress = knownAddresses.find(
({ rewardAccount }) => rewardAccount === rewardAddress.toAddress().toBech32()
);
return util.stakeKeyPathFromGroupedAddress({ address: knownAddress });
return util.stakeKeyPathFromGroupedAddress({
address: knownAddress,
purpose
});
};
4 changes: 3 additions & 1 deletion packages/hardware-trezor/src/transformers/withdrawals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const toTrezorWithdrawal: Transform<Cardano.Withdrawal, Trezor.CardanoWit
* - The script hash, blake2b-224 hash digests of serialized monetary scripts.
*/
if (areNumbersEqualInConstantTime(rewardAddress.getPaymentCredential().type, Cardano.CredentialType.KeyHash)) {
const keyPath = context?.knownAddresses ? resolveStakeKeyPath(rewardAddress, context.knownAddresses) : null;
const keyPath = context?.knownAddresses
? resolveStakeKeyPath(rewardAddress, context.knownAddresses, context.purpose)
: null;
trezorWithdrawal = keyPath
? {
amount: withdrawal.quantity.toString(),
Expand Down
6 changes: 5 additions & 1 deletion packages/hardware-trezor/test/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ export const knownAddress: GroupedAddress = {
type: AddressType.Internal
};

export const knownAddressPaymentKeyPath = { index: knownAddress.index, role: Number(knownAddress.type) };
export const knownAddressPaymentKeyPath = {
index: knownAddress.index,
purpose: knownAddress.purpose,
role: Number(knownAddress.type)
};

export const knownAddressWithoutStakingPath: GroupedAddress = {
accountIndex: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TxInId } from '@cardano-sdk/key-management';
import { KeyPurpose, TxInId } from '@cardano-sdk/key-management';
import { contextWithKnownAddresses, txIn } from '../testData';
import { mapAdditionalWitnessRequests } from '../../src/transformers/additionalWitnessRequests';

describe('additionalWitnessRequests', () => {
it('should include payment key paths and reward account key path from given inputs', async () => {
const paymentKeyPath = { index: 0, role: 1 };
const paymentKeyPath = { index: 0, purpose: KeyPurpose.STANDARD, role: 1 };
const result = mapAdditionalWitnessRequests([txIn], {
...contextWithKnownAddresses,
txInKeyPathMap: { [TxInId(txIn)]: paymentKeyPath }
Expand Down
6 changes: 3 additions & 3 deletions packages/hardware-trezor/test/transformers/keyPaths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ describe('key-paths', () => {
});
describe('resolveStakeKeyPath', () => {
it('returns the stake key path for a known address', async () => {
expect(resolveStakeKeyPath(rewardAddress, contextWithKnownAddresses.knownAddresses)).toEqual(
knownAddressStakeKeyPath
);
expect(
resolveStakeKeyPath(rewardAddress, contextWithKnownAddresses.knownAddresses, contextWithKnownAddresses.purpose)
).toEqual(knownAddressStakeKeyPath);
});
});
});
4 changes: 2 additions & 2 deletions packages/hardware-trezor/test/transformers/tx.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Trezor from '@trezor/connect';
import { CardanoKeyConst, TxInId, util } from '@cardano-sdk/key-management';
import { CardanoKeyConst, KeyPurpose, TxInId, util } from '@cardano-sdk/key-management';
import {
babbageTxBodyWithScripts,
contextWithKnownAddresses,
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('tx', () => {
},
certificates: [
{
path: [util.harden(CardanoKeyConst.PURPOSE), util.harden(CardanoKeyConst.COIN_TYPE), util.harden(0), 2, 0],
path: [util.harden(KeyPurpose.STANDARD), util.harden(CardanoKeyConst.COIN_TYPE), util.harden(0), 2, 0],
pool: '153806dbcd134ddee69a8c5204e38ac80448f62342f8c23cfe4b7edf',
type: Trezor.PROTO.CardanoCertificateType.STAKE_DELEGATION
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressType, GroupedAddress, KeyRole } from '@cardano-sdk/key-management';
import { AddressType, GroupedAddress, KeyPurpose, KeyRole } from '@cardano-sdk/key-management';
import { Cardano } from '@cardano-sdk/core';
import { InvalidStateError } from '@cardano-sdk/util';
import { StaticChangeAddressResolver } from '../../src';
Expand All @@ -10,8 +10,9 @@ export const knownAddresses = () =>
address: 'testAddress' as Cardano.PaymentAddress,
index: 0,
networkId: Cardano.NetworkId.Testnet,
purpose: KeyPurpose.STANDARD,
rewardAccount: '' as Cardano.RewardAccount,
stakeKeyDerivationPath: { index: 0, role: KeyRole.Stake },
stakeKeyDerivationPath: { index: 0, purpose: KeyPurpose.STANDARD, role: KeyRole.Stake },
type: AddressType.External
}
]);
Expand Down
Loading

0 comments on commit 8e238da

Please sign in to comment.