EthereumUtil { public static Long calculateChainId(EthereumTransaction eTrans) { Long result=null; long rawResult=EthereumUtil.convertVarNumberToLong(new RLPElement(new byte[0],eTrans.getSig_v())); if (!((rawResult == EthereumUtil.LOWER_REAL_V) || (rawResult== (LOWER_REAL_V+1)))) { result = (rawResult-EthereumUtil.CHAIN_ID_INC)/2; } return result; }  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 calculateChainIdBlock1346406() 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); assertNull(EthereumUtil.calculateChainId(trans0),"Block 1346406 Transaction 1 is Ethereum MainNet"); EthereumTransaction trans1 = eTrans.get(1); assertNull(EthereumUtil.calculateChainId(trans1),"Block 1346406 Transaction 2 is Ethereum MainNet"); EthereumTransaction trans2 = eTrans.get(2); assertNull(EthereumUtil.calculateChainId(trans2),"Block 1346406 Transaction 3 is Ethereum MainNet"); EthereumTransaction trans3 = eTrans.get(3); assertNull(EthereumUtil.calculateChainId(trans3),"Block 1346406 Transaction 4 is Ethereum MainNet"); EthereumTransaction trans4 = eTrans.get(4); assertNull(EthereumUtil.calculateChainId(trans4),"Block 1346406 Transaction 5 is Ethereum MainNet"); EthereumTransaction trans5 = eTrans.get(5); assertNull(EthereumUtil.calculateChainId(trans5),"Block 1346406 Transaction 6 is Ethereum MainNet"); } finally { if (ebr!=null) { ebr.close(); } } }