Skip to content

Commit 12d42ad

Browse files
authored
Merge pull request #356 from bloxbean/fix_353_main
fix: #353 Directly extract tx body bytes from tx bytes
2 parents 9c29edd + 816d824 commit 12d42ad

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

transaction-spec/src/main/java/com/bloxbean/cardano/client/transaction/util/TransactionUtil.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.bloxbean.cardano.client.transaction.util;
22

3-
import co.nstant.in.cbor.model.Array;
4-
import co.nstant.in.cbor.model.DataItem;
5-
import com.bloxbean.cardano.client.common.cbor.CborSerializationUtil;
3+
import co.nstant.in.cbor.CborDecoder;
4+
import co.nstant.in.cbor.CborException;
65
import com.bloxbean.cardano.client.crypto.Blake2bUtil;
76
import com.bloxbean.cardano.client.exception.CborDeserializationException;
87
import com.bloxbean.cardano.client.exception.CborRuntimeException;
98
import com.bloxbean.cardano.client.exception.CborSerializationException;
109
import com.bloxbean.cardano.client.transaction.spec.Transaction;
11-
import com.bloxbean.cardano.client.transaction.spec.TransactionBody;
1210
import com.bloxbean.cardano.client.util.HexUtil;
1311

12+
import java.io.ByteArrayInputStream;
13+
1414
public class TransactionUtil {
1515

1616
/**
@@ -36,8 +36,8 @@ public static Transaction createCopy(Transaction transaction) {
3636
*/
3737
public static String getTxHash(Transaction transaction) {
3838
try {
39-
transaction.serialize(); //Just to trigger fill body.setAuxiliaryDataHash(), might be removed later.
40-
return safeGetTxHash(transaction.getBody());
39+
byte[] txBytes = transaction.serialize(); //Just to trigger fill body.setAuxiliaryDataHash(), might be removed later.
40+
return getTxHash(txBytes);
4141
} catch (Exception ex) {
4242
throw new RuntimeException("Get transaction hash failed. ", ex);
4343
}
@@ -51,19 +51,44 @@ public static String getTxHash(Transaction transaction) {
5151
*/
5252
public static String getTxHash(byte[] transactionBytes) {
5353
try {
54-
Array array = (Array) CborSerializationUtil.deserialize(transactionBytes);
55-
DataItem txBodyDI = array.getDataItems().get(0);
56-
return safeGetTxHash(CborSerializationUtil.serialize(txBodyDI, false));
54+
byte[] txBodyBytes = extractTransactionBodyFromTx(transactionBytes);
55+
return safeGetTxHash(txBodyBytes);
5756
} catch (Exception ex) {
5857
throw new RuntimeException("Get transaction hash failed. ", ex);
5958
}
6059
}
6160

62-
private static String safeGetTxHash(byte[] txBodyBytes) throws Exception {
63-
return HexUtil.encodeHexString(Blake2bUtil.blake2bHash256(txBodyBytes));
61+
/**
62+
* Extract transaction body bytes from transaction bytes.
63+
* @param txBytes transaction bytes
64+
* @return transaction body bytes
65+
*/
66+
public static byte[] extractTransactionBodyFromTx(byte[] txBytes) {
67+
if (txBytes == null || txBytes.length == 0)
68+
throw new IllegalArgumentException("Transaction bytes can't be null or empty");
69+
70+
ByteArrayInputStream bais = new ByteArrayInputStream(txBytes);
71+
CborDecoder decoder = new CborDecoder(bais);
72+
73+
//Extract transaction body
74+
bais.read(); //Skip the first byte as it is a tag
75+
try {
76+
decoder.decodeNext();
77+
} catch (CborException e) {
78+
throw new CborRuntimeException(e);
79+
}
80+
81+
int available = bais.available();
82+
byte[] txBodyRaw = new byte[txBytes.length - available -1]; // -1 for the first byte
83+
84+
//Copy tx body bytes to txBodyRaw
85+
System.arraycopy(txBytes,1,txBodyRaw,0,txBodyRaw.length);
86+
87+
return txBodyRaw;
6488
}
6589

66-
private static String safeGetTxHash(TransactionBody safeTransactionBody) throws Exception {
67-
return safeGetTxHash(CborSerializationUtil.serialize(safeTransactionBody.serialize()));
90+
private static String safeGetTxHash(byte[] txBodyBytes) {
91+
return HexUtil.encodeHexString(Blake2bUtil.blake2bHash256(txBodyBytes));
6892
}
93+
6994
}

0 commit comments

Comments
 (0)