Skip to content

Commit

Permalink
Merge branch '51-add-cardanotransaction-2' into 'dev'
Browse files Browse the repository at this point in the history
add inputUtxos to CardanoTransaction model

Closes #51

See merge request ergo/rosen-bridge/rosen-chains!58
  • Loading branch information
vorujack committed Sep 16, 2023
2 parents 316e238 + 4b19114 commit 6a30888
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
18 changes: 14 additions & 4 deletions packages/chains/cardano/lib/CardanoChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,13 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
CardanoWasm.hash_transaction(txBody).to_bytes()
).toString('hex');

const cardanoTx = new CardanoTransaction(eventId, txBytes, txId, txType);
const cardanoTx = new CardanoTransaction(
eventId,
txBytes,
txId,
txType,
bankBoxes.map((box) => JSONBigInt.stringify(box))
);

this.logger.info(
`Cardano transaction [${txId}] as type [${txType}] generated for event [${eventId}]`
Expand Down Expand Up @@ -328,14 +334,16 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
requiredSign: number
): Promise<PaymentTransaction> => {
const tx = Serializer.deserialize(transaction.txBytes);
const cardanoTx = transaction as CardanoTransaction;
return this.signFunction(hash_transaction(tx.body()).to_bytes()).then(
(signature: string) => {
const signedTx = this.buildSignedTransaction(tx.body(), signature);
return new CardanoTransaction(
transaction.eventId,
cardanoTx.eventId,
Serializer.serialize(signedTx),
transaction.txId,
transaction.txType
cardanoTx.txId,
cardanoTx.txType,
cardanoTx.inputUtxos
);
}
);
Expand All @@ -349,6 +357,8 @@ class CardanoChain extends AbstractUtxoChain<CardanoUtxo> {
getTransactionAssets = async (
transaction: PaymentTransaction
): Promise<TransactionAssetBalance> => {
// TODO: transaction argument is of type CardanoTransaction
// should do a casting and improve extracting input box assets (#52)
const tx = Serializer.deserialize(transaction.txBytes);
const txBody = tx.body();

Expand Down
13 changes: 9 additions & 4 deletions packages/chains/cardano/lib/CardanoTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
import {
PaymentTransaction,
PaymentTransactionJsonModel,
TransactionType,
} from '@rosen-chains/abstract-chain';
import { CARDANO_CHAIN } from './constants';
import { CardanoTransactionJsonModel } from './types';

class CardanoTransaction implements PaymentTransaction {
eventId: string;
network: string;
txBytes: Uint8Array;
txId: string;
txType: TransactionType;
inputUtxos: Array<string>;

constructor(
eventId: string,
txBytes: Uint8Array,
txId: string,
txType: TransactionType
txType: TransactionType,
inputUtxos: Array<string>
) {
this.network = CARDANO_CHAIN;
this.eventId = eventId;
this.txBytes = txBytes;
this.txId = txId;
this.txType = txType;
this.inputUtxos = inputUtxos;
}

/**
* converts json representation of the payment transaction to CardanoTransaction
* @returns CardanoTransaction object
*/
static fromJson = (jsonString: string): CardanoTransaction => {
const obj = JSON.parse(jsonString) as PaymentTransactionJsonModel;
const obj = JSON.parse(jsonString) as CardanoTransactionJsonModel;
return new CardanoTransaction(
obj.eventId,
Buffer.from(obj.txBytes, 'hex'),
obj.txId,
obj.txType as TransactionType
obj.txType as TransactionType,
obj.inputUtxos
);
};

Expand All @@ -50,6 +54,7 @@ class CardanoTransaction implements PaymentTransaction {
txBytes: this.getTxHexString(),
txId: this.txId,
txType: this.txType,
inputUtxos: this.inputUtxos,
});
};

Expand Down
10 changes: 9 additions & 1 deletion packages/chains/cardano/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { ChainConfigs } from '@rosen-chains/abstract-chain';
import {
ChainConfigs,
PaymentTransactionJsonModel,
} from '@rosen-chains/abstract-chain';
import { BigNum } from '@emurgo/cardano-serialization-lib-nodejs';

interface CardanoConfigs extends ChainConfigs {
Expand Down Expand Up @@ -46,6 +49,10 @@ interface UtxoBoxesAssets {
assets: Map<string, BigNum>;
}

interface CardanoTransactionJsonModel extends PaymentTransactionJsonModel {
inputUtxos: Array<string>;
}

export {
CardanoConfigs,
CardanoAsset,
Expand All @@ -54,4 +61,5 @@ export {
CardanoBoxCandidate,
CardanoTx,
UtxoBoxesAssets,
CardanoTransactionJsonModel,
};
2 changes: 1 addition & 1 deletion packages/chains/cardano/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/cardano",
"version": "0.1.16",
"version": "0.1.17",
"description": "this project contains cardano chain for Rosen-bridge",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand Down
10 changes: 8 additions & 2 deletions packages/chains/cardano/tests/CardanoChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('CardanoChain', () => {
* - call the function
* - check returned value
* @expected
* - PaymentTransaction txType, eventId, and network should be as
* - PaymentTransaction txType, eventId, network and inputUtxos should be as
* expected
* - extracted order of generated transaction should be the same as input
* order
Expand Down Expand Up @@ -126,6 +126,9 @@ 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))
);

// extracted order of generated transaction should be the same as input order
const extractedOrder = cardanoChain.extractTransactionOrder(cardanoTx);
Expand Down Expand Up @@ -159,7 +162,7 @@ describe('CardanoChain', () => {
* - call the function
* - check returned value
* @expected
* - PaymentTransaction txType, eventId, and network should be as
* - PaymentTransaction txType, eventId, network and inputUtxos should be as
* expected
* - extracted order of generated transaction should be the same as input
* order
Expand Down Expand Up @@ -202,6 +205,9 @@ 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))
);

// extracted order of generated transaction should be the same as input order
const extractedOrder = cardanoChain.extractTransactionOrder(cardanoTx);
Expand Down
4 changes: 2 additions & 2 deletions packages/networks/cardano-koios/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/cardano-koios-network",
"version": "0.1.16",
"version": "0.1.17",
"description": "cardano koios network package for rosen cardano chain",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",
Expand All @@ -25,7 +25,7 @@
"@rosen-bridge/rosen-extractor": "^0.1.11",
"@rosen-bridge/tokens": "^0.2.0",
"@rosen-chains/abstract-chain": "^0.1.15",
"@rosen-chains/cardano": "^0.1.16",
"@rosen-chains/cardano": "^0.1.17",
"@rosen-clients/cardano-koios": "^1.0.3",
"json-bigint": "^1.0.0"
},
Expand Down

0 comments on commit 6a30888

Please sign in to comment.