Skip to content

Commit

Permalink
Fix/submit deserializing tx (#122)
Browse files Browse the repository at this point in the history
* fix: added transaction extraction and extradata removal, since this isn't needed by the submit-api

* fix: rewrote extractTransaction function

* fix: added tests for extractTransaction
  • Loading branch information
Kammerlo authored Apr 15, 2024
1 parent 34b0407 commit fb0d9f2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ public TransactionIdentifierResponse constructionSubmitService(
ConstructionSubmitRequest constructionSubmitRequest) {
String signedTransaction = constructionSubmitRequest.getSignedTransaction();
log.info("[constructionSubmit] About to submit signed transaction");
String txHash = cardanoService.submitTransaction(signedTransaction);
String tx = cardanoService.extractTransactionIfNeeded(signedTransaction);
String txHash = cardanoService.submitTransaction(tx);

return new TransactionIdentifierResponse(new TransactionIdentifier(txHash), null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,8 @@ public static ApiException cantDecodeMempoolTransaction() {
public static ApiException sendTransactionError(String error) {
return new ApiException(RosettaErrorType.SEND_TRANSACTION_ERROR.toRosettaError(false, null, error));
}

public static ApiException invalidTransactionError() {
return new ApiException(RosettaErrorType.INVALID_TRANSACTION.toRosettaError(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.openapitools.client.model.Operation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.cardanofoundation.rosetta.common.model.cardano.transaction.UnsignedTransaction;
Expand Down Expand Up @@ -63,4 +62,6 @@ ProcessOperations convertRosettaOperations(NetworkIdentifierType networkIdentifi

String submitTransaction(String signedTransaction);
DepositParameters getDepositParameters();

String extractTransactionIfNeeded(String txWithExtraData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import co.nstant.in.cbor.CborException;
import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.UnicodeString;
import co.nstant.in.cbor.model.UnsignedInteger;
import com.bloxbean.cardano.client.address.ByronAddress;
import com.bloxbean.cardano.client.common.cbor.CborSerializationUtil;
Expand Down Expand Up @@ -531,4 +533,28 @@ public DepositParameters getDepositParameters() {
protocolParametersFromIndexerAndConfig.getPoolDeposit().toString());
}

/**
* Extract raw signed transaction and removes the extradata.
* Transactions build with rosetta contain such data, transaction build with other tools like cardano-cli do not contain this data.
* @param txWithExtraData transaction with extra data
* @return raw signed transaction
*/
@Override
public String extractTransactionIfNeeded(String txWithExtraData) {
byte[] bytes = HexUtil.decodeHexString(txWithExtraData);
Array deserialize = (Array) CborSerializationUtil.deserialize(bytes);
// Unpack transaction if needed
if(deserialize.getDataItems().size() == 1) {
deserialize = (Array) deserialize.getDataItems().getFirst();
}
if(deserialize.getDataItems().isEmpty()) {
throw ExceptionFactory.invalidTransactionError();
}
// unpack transaction
if(deserialize.getDataItems().getFirst().getMajorType().equals(MajorType.UNICODE_STRING)) {
return ((UnicodeString) deserialize.getDataItems().getFirst()).getString();
} else {
return txWithExtraData;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ public enum RosettaErrorType {
TX_HASH_COIN_NOT_MATCH("Transaction hash does not match to given coin identifier", 5015),
ADDRESS_AND_ACCOUNT_ID_NOT_MATCH("Address and account identifier does not match", 5016),
BAD_FORMED_COIN_ERROR("Coin identifier has an invalid format", 5017),
CANT_DECODE_MEMPOOL_TRANSACTION("Cant decode mempool transaction", 5018);
CANT_DECODE_MEMPOOL_TRANSACTION("Cant decode mempool transaction", 5018),
INVALID_TRANSACTION("Can't decode Transaction", 5019);

final String message;
final int code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,19 @@ public void calculateFeeTest() {
assertEquals(0L, l);
}

@Test
public void extractTransactionRosettaTxTest() {
String packedRosettaTransaction = "827901d4383461343030383138323538323037663639306539666234313034383732636136613361353961373636363663633236303737303064323236323839313832643933626564646266393536363138303130313832383235383164363066306233656133363935343261353834383239306237323736343862353833386235646161323535396465626435356466636161316632303161303031653834383038323538316436303564643239333565396262333530383331353062633834326661376661303935623734306335636436363031376532623633313531336532316130353932343937393032316130303032616533643033316130333638313930366131303038313832353832303337363939653262386432333835613031623538393439333638336266326435356635396331646463316637306137303466623936356331613930666633303935383430303935656366376431363731333564653064343831353433306662343037396466383563353865316564633930346363383461323864323361633336393839616631373939653463633662343865383738303131366532383263303365343630353738626337663766353266663663613161616632653138336162643835303166356636a26a6f7065726174696f6e7381a6746f7065726174696f6e5f6964656e746966696572a165696e64657800676163636f756e74a16761646472657373783f616464725f746573743176707761397936376e776534707163347030797939376e6c357a326d7773783965346e717a6c33747676323338637367716b616c6c66616d6f756e74a26863757272656e6379a26673796d626f6c6341444168646563696d616c73066576616c7565692d39353634383832326b636f696e5f6368616e6765a26f636f696e5f6964656e746966696572a16a6964656e7469666965727842376636393065396662343130343837326361366133613539613736363636636332363037373030643232363238393138326439336265646462663935363631383a316b636f696e5f616374696f6e6a636f696e5f7370656e746673746174757360647479706565696e707574767472616e73616374696f6e4d6574616461746148657860";
String rawTx = cardanoService.extractTransactionIfNeeded(packedRosettaTransaction);
String expectedTx = "84a400818258207f690e9fb4104872ca6a3a59a76666cc2607700d226289182d93beddbf95661801018282581d60f0b3ea369542a5848290b727648b5838b5daa2559debd55dfcaa1f201a001e848082581d605dd2935e9bb35083150bc842fa7fa095b740c5cd66017e2b631513e21a05924979021a0002ae3d031a03681906a1008182582037699e2b8d2385a01b589493683bf2d55f59c1ddc1f70a704fb965c1a90ff3095840095ecf7d167135de0d4815430fb4079df85c58e1edc904cc84a28d23ac36989af1799e4cc6b48e8780116e282c03e460578bc7f7f52ff6ca1aaf2e183abd8501f5f6";
assertEquals(expectedTx, rawTx);
}

@Test
public void extractTransactionRawTxTest() {
String rawTx = "84a400818258207f690e9fb4104872ca6a3a59a76666cc2607700d226289182d93beddbf95661801018282581d60f0b3ea369542a5848290b727648b5838b5daa2559debd55dfcaa1f201a001e848082581d605dd2935e9bb35083150bc842fa7fa095b740c5cd66017e2b631513e21a05924979021a0002ae3d031a03681906a1008182582037699e2b8d2385a01b589493683bf2d55f59c1ddc1f70a704fb965c1a90ff3095840095ecf7d167135de0d4815430fb4079df85c58e1edc904cc84a28d23ac36989af1799e4cc6b48e8780116e282c03e460578bc7f7f52ff6ca1aaf2e183abd8501f5f6";
String extractedTx = cardanoService.extractTransactionIfNeeded(rawTx);
assertEquals(rawTx, extractedTx);
}

}

0 comments on commit fb0d9f2

Please sign in to comment.