Skip to content

Commit

Permalink
Merge pull request #356 from bloxbean/fix_353_main
Browse files Browse the repository at this point in the history
fix: #353 Directly extract tx body bytes from tx bytes
  • Loading branch information
satran004 authored Jan 18, 2024
2 parents 9c29edd + 816d824 commit 12d42ad
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.bloxbean.cardano.client.transaction.util;

import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import com.bloxbean.cardano.client.common.cbor.CborSerializationUtil;
import co.nstant.in.cbor.CborDecoder;
import co.nstant.in.cbor.CborException;
import com.bloxbean.cardano.client.crypto.Blake2bUtil;
import com.bloxbean.cardano.client.exception.CborDeserializationException;
import com.bloxbean.cardano.client.exception.CborRuntimeException;
import com.bloxbean.cardano.client.exception.CborSerializationException;
import com.bloxbean.cardano.client.transaction.spec.Transaction;
import com.bloxbean.cardano.client.transaction.spec.TransactionBody;
import com.bloxbean.cardano.client.util.HexUtil;

import java.io.ByteArrayInputStream;

public class TransactionUtil {

/**
Expand All @@ -36,8 +36,8 @@ public static Transaction createCopy(Transaction transaction) {
*/
public static String getTxHash(Transaction transaction) {
try {
transaction.serialize(); //Just to trigger fill body.setAuxiliaryDataHash(), might be removed later.
return safeGetTxHash(transaction.getBody());
byte[] txBytes = transaction.serialize(); //Just to trigger fill body.setAuxiliaryDataHash(), might be removed later.
return getTxHash(txBytes);
} catch (Exception ex) {
throw new RuntimeException("Get transaction hash failed. ", ex);
}
Expand All @@ -51,19 +51,44 @@ public static String getTxHash(Transaction transaction) {
*/
public static String getTxHash(byte[] transactionBytes) {
try {
Array array = (Array) CborSerializationUtil.deserialize(transactionBytes);
DataItem txBodyDI = array.getDataItems().get(0);
return safeGetTxHash(CborSerializationUtil.serialize(txBodyDI, false));
byte[] txBodyBytes = extractTransactionBodyFromTx(transactionBytes);
return safeGetTxHash(txBodyBytes);
} catch (Exception ex) {
throw new RuntimeException("Get transaction hash failed. ", ex);
}
}

private static String safeGetTxHash(byte[] txBodyBytes) throws Exception {
return HexUtil.encodeHexString(Blake2bUtil.blake2bHash256(txBodyBytes));
/**
* Extract transaction body bytes from transaction bytes.
* @param txBytes transaction bytes
* @return transaction body bytes
*/
public static byte[] extractTransactionBodyFromTx(byte[] txBytes) {
if (txBytes == null || txBytes.length == 0)
throw new IllegalArgumentException("Transaction bytes can't be null or empty");

ByteArrayInputStream bais = new ByteArrayInputStream(txBytes);
CborDecoder decoder = new CborDecoder(bais);

//Extract transaction body
bais.read(); //Skip the first byte as it is a tag
try {
decoder.decodeNext();
} catch (CborException e) {
throw new CborRuntimeException(e);
}

int available = bais.available();
byte[] txBodyRaw = new byte[txBytes.length - available -1]; // -1 for the first byte

//Copy tx body bytes to txBodyRaw
System.arraycopy(txBytes,1,txBodyRaw,0,txBodyRaw.length);

return txBodyRaw;
}

private static String safeGetTxHash(TransactionBody safeTransactionBody) throws Exception {
return safeGetTxHash(CborSerializationUtil.serialize(safeTransactionBody.serialize()));
private static String safeGetTxHash(byte[] txBodyBytes) {
return HexUtil.encodeHexString(Blake2bUtil.blake2bHash256(txBodyBytes));
}

}
Loading

0 comments on commit 12d42ad

Please sign in to comment.