1
1
package com .bloxbean .cardano .client .transaction .util ;
2
2
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 ;
6
5
import com .bloxbean .cardano .client .crypto .Blake2bUtil ;
7
6
import com .bloxbean .cardano .client .exception .CborDeserializationException ;
8
7
import com .bloxbean .cardano .client .exception .CborRuntimeException ;
9
8
import com .bloxbean .cardano .client .exception .CborSerializationException ;
10
9
import com .bloxbean .cardano .client .transaction .spec .Transaction ;
11
- import com .bloxbean .cardano .client .transaction .spec .TransactionBody ;
12
10
import com .bloxbean .cardano .client .util .HexUtil ;
13
11
12
+ import java .io .ByteArrayInputStream ;
13
+
14
14
public class TransactionUtil {
15
15
16
16
/**
@@ -36,8 +36,8 @@ public static Transaction createCopy(Transaction transaction) {
36
36
*/
37
37
public static String getTxHash (Transaction transaction ) {
38
38
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 );
41
41
} catch (Exception ex ) {
42
42
throw new RuntimeException ("Get transaction hash failed. " , ex );
43
43
}
@@ -51,19 +51,44 @@ public static String getTxHash(Transaction transaction) {
51
51
*/
52
52
public static String getTxHash (byte [] transactionBytes ) {
53
53
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 );
57
56
} catch (Exception ex ) {
58
57
throw new RuntimeException ("Get transaction hash failed. " , ex );
59
58
}
60
59
}
61
60
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 ;
64
88
}
65
89
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 ));
68
92
}
93
+
69
94
}
0 commit comments