Skip to content

Commit

Permalink
Merge branch '128-fix-cardano-payment-tx-verification' into 'dev'
Browse files Browse the repository at this point in the history
fix CardanoTransaction inputUtxos field on generation

Closes #128

See merge request ergo/rosen-bridge/rosen-chains!159
  • Loading branch information
zargarzadehm committed Dec 16, 2024
2 parents 2b763a1 + 1e18e47 commit 1767994
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-pandas-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-chains/cardano': patch
---

Fix order of the inputUtxos in CardanoTransaction object in generateMultipleTransactions function
7 changes: 6 additions & 1 deletion packages/chains/cardano/lib/CardanoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,17 @@ class CardanoChain extends AbstractUtxoChain<CardanoTx, CardanoUtxo> {
CardanoWasm.hash_transaction(txBody).to_bytes()
).toString('hex');

// sort input utxos
const inputUtxos = structuredClone(bankBoxes);
inputUtxos.sort((a, b) =>
a.txId === b.txId ? a.index - b.index : a.txId < b.txId ? -1 : 1
);
const cardanoTx = new CardanoTransaction(
txId,
eventId,
txBytes,
txType,
bankBoxes.map((box) => JSONBigInt.stringify(box))
inputUtxos.map((box) => JSONBigInt.stringify(box))
);

this.logger.info(
Expand Down
66 changes: 49 additions & 17 deletions packages/chains/cardano/tests/CardanoChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ describe('CardanoChain', () => {
* - call the function
* - check returned value
* @expected
* - PaymentTransaction txType, eventId, network and inputUtxos should be as
* expected
* - PaymentTransaction txType, eventId, network should be as expected
* - PaymentTransaction inputUtxos should be the sorted version of the bankBoxes
* - extracted order of generated transaction should be the same as input
* order
* - transaction fee and ttl should be the same as config fee
Expand Down Expand Up @@ -73,9 +73,19 @@ describe('CardanoChain', () => {
expect(cardanoTx.txType).toEqual(payment1.txType);
expect(cardanoTx.eventId).toEqual(payment1.eventId);
expect(cardanoTx.network).toEqual(payment1.network);
expect(cardanoTx.inputUtxos).toEqual(
bankBoxes.map((utxo) => JsonBI.stringify(utxo))
);

// check inputUtxos
expect(cardanoTx.inputUtxos.length).toEqual(bankBoxes.length);
bankBoxes.forEach((utxo) => {
expect(cardanoTx.inputUtxos.includes(JsonBI.stringify(utxo)));
});
for (let i = 1; i < cardanoTx.inputUtxos.length; i++) {
const previousUtxo = JsonBigInt.parse(cardanoTx.inputUtxos[i - 1]);
const utxo = JsonBigInt.parse(cardanoTx.inputUtxos[i]);
if (utxo.txId === previousUtxo.txId)
expect(utxo.index > previousUtxo.index);
else expect(utxo.txId > previousUtxo.txId);
}

// extracted order of generated transaction should be the same as input order
const extractedOrder = cardanoChain.extractTransactionOrder(cardanoTx);
Expand Down Expand Up @@ -112,8 +122,8 @@ describe('CardanoChain', () => {
* - call the function
* - check returned value
* @expected
* - PaymentTransaction txType, eventId, network and inputUtxos should be as
* expected
* - PaymentTransaction txType, eventId, network should be as expected
* - PaymentTransaction inputUtxos should be the sorted version of the bankBoxes
* - extracted order of generated transaction should be the same as input
* order
* - transaction fee and ttl should be the same as config fee
Expand All @@ -131,9 +141,10 @@ describe('CardanoChain', () => {
// mock getCoveringBoxes, hasLockAddressEnoughAssets
const cardanoChain = TestUtils.generateChainObject(network);
const getCovBoxesSpy = vi.spyOn(cardanoChain as any, 'getCoveringBoxes');
const mockedBoxes = bankBoxes.slice(2);
getCovBoxesSpy.mockResolvedValue({
covered: true,
boxes: bankBoxes.slice(2),
boxes: mockedBoxes,
});
const hasLockAddressEnoughAssetsSpy = vi.spyOn(
cardanoChain,
Expand All @@ -155,9 +166,19 @@ describe('CardanoChain', () => {
expect(cardanoTx.txType).toEqual(payment.txType);
expect(cardanoTx.eventId).toEqual(payment.eventId);
expect(cardanoTx.network).toEqual(payment.network);
expect(cardanoTx.inputUtxos).toEqual(
bankBoxes.slice(2).map((utxo) => JsonBI.stringify(utxo))
);

// check inputUtxos
expect(cardanoTx.inputUtxos.length).toEqual(mockedBoxes.length);
mockedBoxes.forEach((utxo) => {
expect(cardanoTx.inputUtxos.includes(JsonBI.stringify(utxo)));
});
for (let i = 1; i < cardanoTx.inputUtxos.length; i++) {
const previousUtxo = JsonBigInt.parse(cardanoTx.inputUtxos[i - 1]);
const utxo = JsonBigInt.parse(cardanoTx.inputUtxos[i]);
if (utxo.txId === previousUtxo.txId)
expect(utxo.index > previousUtxo.index);
else expect(utxo.txId > previousUtxo.txId);
}

// extracted order of generated transaction should be the same as input order
const extractedOrder = cardanoChain.extractTransactionOrder(cardanoTx);
Expand Down Expand Up @@ -253,8 +274,8 @@ describe('CardanoChain', () => {
* - call the function
* - check returned value
* @expected
* - PaymentTransaction txType, eventId, network and inputUtxos should be as
* expected
* - PaymentTransaction txType, eventId, network should be as expected
* - PaymentTransaction inputUtxos should be the sorted version of the bankBoxes
* - extracted order of generated transaction should be the same as input
* order
* - transaction fee and ttl should be the same as config fee
Expand All @@ -273,9 +294,10 @@ describe('CardanoChain', () => {
const cardanoChain =
TestUtils.generateChainObjectWithMultiDecimalTokenMap(network);
const getCovBoxesSpy = vi.spyOn(cardanoChain as any, 'getCoveringBoxes');
const mockedBoxes = bankBoxes.slice(2);
getCovBoxesSpy.mockResolvedValue({
covered: true,
boxes: bankBoxes.slice(2),
boxes: mockedBoxes,
});
const hasLockAddressEnoughAssetsSpy = vi.spyOn(
cardanoChain,
Expand All @@ -297,9 +319,19 @@ describe('CardanoChain', () => {
expect(cardanoTx.txType).toEqual(payment.txType);
expect(cardanoTx.eventId).toEqual(payment.eventId);
expect(cardanoTx.network).toEqual(payment.network);
expect(cardanoTx.inputUtxos).toEqual(
bankBoxes.slice(2).map((utxo) => JsonBI.stringify(utxo))
);

// check inputUtxos
expect(cardanoTx.inputUtxos.length).toEqual(mockedBoxes.length);
mockedBoxes.forEach((utxo) => {
expect(cardanoTx.inputUtxos.includes(JsonBI.stringify(utxo)));
});
for (let i = 1; i < cardanoTx.inputUtxos.length; i++) {
const previousUtxo = JsonBigInt.parse(cardanoTx.inputUtxos[i - 1]);
const utxo = JsonBigInt.parse(cardanoTx.inputUtxos[i]);
if (utxo.txId === previousUtxo.txId)
expect(utxo.index > previousUtxo.index);
else expect(utxo.txId > previousUtxo.txId);
}

// extracted order of generated transaction should be the same as input order
const extractedOrder = cardanoChain.extractTransactionOrder(cardanoTx);
Expand Down

0 comments on commit 1767994

Please sign in to comment.