Skip to content

Commit

Permalink
test(wallet): add voting unit tests and e2e tests improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
tomislavhoracek committed Jun 21, 2022
1 parent 1f7895d commit 10c211c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/wallet/test/KeyManagement/InMemoryKeyAgent.test.ts
Expand Up @@ -77,6 +77,18 @@ describe('InMemoryKeyAgent', () => {
expect(typeof signature).toBe('string');
});

test('signVotingMetadata', async () => {
const signature = await keyAgent.signVotingMetadata({
nonce: 1_234_567,
publicStakeKey: '0xf8f734461c7e8c9582bfda6ed821b26fc0e56f36cd35d3de6e7aae2d902abdc5',
rewardAccountKeyHash: '0x4368a2bb9f54cbe0ac6f46e0b15359a76e61b5f221a493b15ba2d99d',
votingPublicKey:
// eslint-disable-next-line max-len
'0x94ee103ffe8943fe124fb5a6d99f91e41248dafa62fa614013071481d431bd66b748ef68a6c5a713173fb5190b51e7b64e2af8646386d56ef6ebf9f753575352'
});
expect(typeof signature).toBe('string');
});

test('signTransaction', async () => {
ownSignatureKeyPaths.mockReturnValueOnce([
{ index: 0, role: 0 },
Expand Down
18 changes: 18 additions & 0 deletions packages/wallet/test/SingleAddressWallet/methods.test.ts
Expand Up @@ -179,6 +179,24 @@ describe('SingleAddressWallet methods', () => {
expect(await txPending).toBe(tx);
expect(await txInFlight).toEqual([tx]);
});

it('initializeVotingRegistrationTx', async () => {
const votingPublicKey = Cardano.Bip32PublicKey(
'0cc46dd6968caa2f3719027844fe7a2f56d6bdcfc1e5d4c0b6e4c04f2920badfa0a4a2405246ca947cfa0c7f93e859d05bcd48b7cf2a224abb59df3b99e57295'
);
const votingProps = {
nonce: 1_234_567,
votingPublicKey
};
const { txInternals, auxiliaryData } = await wallet.initializeVotingRegistrationTx(votingProps);
expect(typeof txInternals.hash).toBe('string');
expect(txInternals.body.outputs).toHaveLength(2); // 1 to own address and 1 change output
expect(txInternals.inputSelection.outputs.size).toBe(1);
expect(txInternals.inputSelection.change.size).toBe(1);
expect(txInternals.inputSelection.inputs.size).toBeGreaterThan(0);
expect(txInternals.inputSelection.fee).toBeGreaterThan(0n);
expect(auxiliaryData.body.blob?.size).toBe(2);
});
});

it('sync() calls wallet provider functions until shutdown()', () => {
Expand Down
27 changes: 26 additions & 1 deletion packages/wallet/test/e2e/SingleAddressWallet/voting.test.ts
@@ -1,3 +1,4 @@
import { Cardano } from '@cardano-sdk/core';
import { KeyManagement, SingleAddressWallet } from '../../../src';
import {
assetProvider,
Expand Down Expand Up @@ -43,13 +44,37 @@ describe('SingleAddressWallet/voting_metadata', () => {
});
const { txInternals, auxiliaryData } = votingTxData;
const outgoingTx = await wallet.finalizeTx(txInternals, auxiliaryData);

await wallet.submitTx(outgoingTx);
const loadedTx = await firstValueFrom(
wallet.transactions.history$.pipe(
map((txs) => txs.find((tx) => tx.id === outgoingTx.id)),
filter(isNotNil)
)
);
expect(loadedTx.auxiliaryData).toEqual(auxiliaryData);
const auxBlobData = auxiliaryData?.body.blob?.get(
BigInt(KeyManagement.util.VotingLabels.DATA)
) as Cardano.MetadatumMap;
const loadedTxAuxBlobData = loadedTx.auxiliaryData?.body.blob?.get(
BigInt(KeyManagement.util.VotingLabels.DATA)
) as Cardano.MetadatumMap;
const auxBlobSignature = auxiliaryData?.body.blob?.get(
BigInt(KeyManagement.util.VotingLabels.SIG)
) as Cardano.MetadatumMap;
const loadedTxAuxBlobSignature = loadedTx.auxiliaryData?.body.blob?.get(
BigInt(KeyManagement.util.VotingLabels.SIG)
) as Cardano.MetadatumMap;
const votingSignature = auxBlobData.get(1n) as Buffer;
const publicStakeKey = auxBlobData.get(2n) as Buffer;
const rewardAccountKeyHash = auxBlobData.get(3n) as Buffer;
const nonce = auxBlobData.get(4n);
const signature = auxBlobSignature.get(1n) as Buffer;
expect(loadedTxAuxBlobData.size).toEqual(4);
expect(loadedTxAuxBlobSignature.size).toEqual(1);
expect(loadedTxAuxBlobData?.get(1n)).toEqual(`0x${votingSignature.toString('hex')}`);
expect(loadedTxAuxBlobData?.get(2n)).toEqual(`0x${publicStakeKey.toString('hex')}`);
expect(loadedTxAuxBlobData?.get(3n)).toEqual(`0x${rewardAccountKeyHash.toString('hex')}`);
expect(loadedTxAuxBlobData?.get(4n)).toEqual(nonce);
expect(loadedTxAuxBlobSignature?.get(1n)).toEqual(`0x${signature.toString('hex')}`);
});
});

0 comments on commit 10c211c

Please sign in to comment.