EthereumUtil { public static byte[] getTransactionHash(EthereumTransaction eTrans) { ArrayList<byte[]> rlpTransaction = new ArrayList<>(); rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getNonce())); rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getGasPriceRaw())); rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getGasLimitRaw())); rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getReceiveAddress())); rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getValueRaw())); rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getData())); rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getSig_v())); rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getSig_r())); rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getSig_s())); byte[] transEnc = EthereumUtil.encodeRLPList(rlpTransaction); Keccak.Digest256 digest = new Keccak.Digest256(); digest.update(transEnc,0,transEnc.length); return digest.digest(); }  static RLPObject rlpDecodeNextItem(ByteBuffer bb); static int detectRLPObjectType(ByteBuffer bb); static byte[] encodeRLPElement(byte[] rawData); static long getRLPListSize(ByteBuffer bb); static Long calculateChainId(EthereumTransaction eTrans); static byte[] getTransactionHash(EthereumTransaction eTrans); static byte[] getTransactionHashWithoutSignature(EthereumTransaction eTrans); static byte[] getTransactionHashWithDummySignatureEIP155(EthereumTransaction eTrans); static byte[] getSendAddress(EthereumTransaction eTrans, int chainId); static BigInteger convertVarNumberToBigInteger(RLPElement rpe); static BigInteger convertVarNumberToBigInteger(byte[] rawData); static Long convertVarNumberToLong(RLPElement rpe); static Short convertToByte(RLPElement rpe); static Integer convertToShort(RLPElement rpe); static Long convertToInt(RLPElement rpe); static Long convertToLong(RLPElement rpe); @Deprecated static byte[] convertLongToVarInt(long value); static String convertToString(RLPElement rpe); static String convertToString(RLPElement rpe, String encoding); static byte[] convertHexStringToByteArray(String hexString); static String convertByteArrayToHexString(byte[] byteArray); static byte[] reverseByteArray(byte[] inputByteArray); static final int RLP_OBJECTTYPE_INVALID; static final int RLP_OBJECTTYPE_ELEMENT; static final int RLP_OBJECTTYPE_LIST; static final int CHAIN_ID_INC; static final int LOWER_REAL_V; static final int HASH_SIZE; static final int LONG_SIZE; static final int INT_SIZE; static final int WORD_SIZE; static final byte[] EMPTY_BYTE_ARRAY; }
@Test public void getTransActionHashBlock1346406() throws IOException, EthereumBlockReadException { ClassLoader classLoader = getClass().getClassLoader(); String fileName="eth1346406.bin"; String fileNameBlock=classLoader.getResource("testdata/"+fileName).getFile(); File file = new File(fileNameBlock); boolean direct=false; FileInputStream fin = new FileInputStream(file); EthereumBlockReader ebr = null; try { ebr = new EthereumBlockReader(fin,this.DEFAULT_MAXSIZE_ETHEREUMBLOCK, this.DEFAULT_BUFFERSIZE,direct); EthereumBlock eblock = ebr.readBlock(); List<EthereumTransaction> eTrans = eblock.getEthereumTransactions(); EthereumTransaction trans0 = eTrans.get(0); byte[] expectedHash = new byte[] {(byte)0xe2,(byte)0x7e,(byte)0x92,(byte)0x88,(byte)0xe2,(byte)0x9c,(byte)0xc8,(byte)0xeb,(byte)0x78,(byte)0xf9,(byte)0xf7,(byte)0x68,(byte)0xd8,(byte)0x9b,(byte)0xf1,(byte)0xcd,(byte)0x4b,(byte)0x68,(byte)0xb7,(byte)0x15,(byte)0xa3,(byte)0x8b,(byte)0x95,(byte)0xd4,(byte)0x6d,(byte)0x77,(byte)0x86,(byte)0x18,(byte)0xcb,(byte)0x10,(byte)0x4d,(byte)0x58}; assertArrayEquals(expectedHash,EthereumUtil.getTransactionHash(trans0),"Block 1346406 Transaction 1 hash is correctly calculated"); EthereumTransaction trans1 = eTrans.get(1); expectedHash = new byte[] {(byte)0x7a,(byte)0x23,(byte)0x2a,(byte)0xa2,(byte)0xae,(byte)0x6a,(byte)0x5e,(byte)0x1f,(byte)0x32,(byte)0xca,(byte)0x3a,(byte)0xc9,(byte)0x3f,(byte)0x4f,(byte)0xdb,(byte)0x77,(byte)0x98,(byte)0x3e,(byte)0x93,(byte)0x2b,(byte)0x38,(byte)0x09,(byte)0x93,(byte)0x56,(byte)0x44,(byte)0x42,(byte)0x08,(byte)0xc6,(byte)0x9d,(byte)0x40,(byte)0x86,(byte)0x81}; assertArrayEquals(expectedHash,EthereumUtil.getTransactionHash(trans1),"Block 1346406 Transaction 2 hash is correctly calculated"); EthereumTransaction trans2 = eTrans.get(2); expectedHash = new byte[] {(byte)0x14,(byte)0x33,(byte)0xe3,(byte)0xcb,(byte)0x66,(byte)0x2f,(byte)0x66,(byte)0x8d,(byte)0x87,(byte)0xb8,(byte)0x35,(byte)0x55,(byte)0x34,(byte)0x5a,(byte)0x20,(byte)0xcc,(byte)0xf8,(byte)0x70,(byte)0x6f,(byte)0x25,(byte)0x21,(byte)0x49,(byte)0x18,(byte)0xe2,(byte)0xf8,(byte)0x1f,(byte)0xe3,(byte)0xd2,(byte)0x1c,(byte)0x9d,(byte)0x5b,(byte)0x23}; assertArrayEquals(expectedHash,EthereumUtil.getTransactionHash(trans2),"Block 1346406 Transaction 3 hash is correctly calculated"); EthereumTransaction trans3 = eTrans.get(3); expectedHash = new byte[] {(byte)0x39,(byte)0x22,(byte)0xf7,(byte)0xf6,(byte)0x0a,(byte)0x33,(byte)0xa1,(byte)0x2d,(byte)0x13,(byte)0x9d,(byte)0x67,(byte)0xfa,(byte)0x53,(byte)0x30,(byte)0xdb,(byte)0xfd,(byte)0xba,(byte)0x42,(byte)0xa4,(byte)0xb7,(byte)0x67,(byte)0x29,(byte)0x6e,(byte)0xff,(byte)0x64,(byte)0x15,(byte)0xee,(byte)0xa3,(byte)0x2d,(byte)0x8a,(byte)0x7b,(byte)0x2b}; assertArrayEquals(expectedHash,EthereumUtil.getTransactionHash(trans3),"Block 1346406 Transaction 4 hash is correctly calculated"); EthereumTransaction trans4 = eTrans.get(4); expectedHash = new byte[] {(byte)0xbb,(byte)0x7c,(byte)0xaa,(byte)0x23,(byte)0x38,(byte)0x5a,(byte)0x0f,(byte)0x73,(byte)0x75,(byte)0x3f,(byte)0x9e,(byte)0x28,(byte)0xd8,(byte)0xf0,(byte)0x60,(byte)0x2f,(byte)0xe2,(byte)0xe7,(byte)0x2d,(byte)0x87,(byte)0xe1,(byte)0xe0,(byte)0x95,(byte)0x52,(byte)0x75,(byte)0x28,(byte)0xd1,(byte)0x44,(byte)0x88,(byte)0x5d,(byte)0x6b,(byte)0x51}; assertArrayEquals(expectedHash,EthereumUtil.getTransactionHash(trans4),"Block 1346406 Transaction 5 hash is correctly calculated"); EthereumTransaction trans5 = eTrans.get(5); expectedHash = new byte[] {(byte)0xbc,(byte)0xde,(byte)0x6f,(byte)0x49,(byte)0x84,(byte)0x2c,(byte)0x6d,(byte)0x73,(byte)0x8d,(byte)0x64,(byte)0x32,(byte)0x8f,(byte)0x78,(byte)0x09,(byte)0xb1,(byte)0xd4,(byte)0x9b,(byte)0xf0,(byte)0xff,(byte)0x3f,(byte)0xfa,(byte)0x46,(byte)0x0f,(byte)0xdd,(byte)0xd2,(byte)0x7f,(byte)0xd4,(byte)0x2b,(byte)0x7a,(byte)0x01,(byte)0xfc,(byte)0x9a}; assertArrayEquals(expectedHash,EthereumUtil.getTransactionHash(trans5),"Block 1346406 Transaction 6 hash is correctly calculated"); } finally { if (ebr!=null) { ebr.close(); } } }