EthereumUtil { public static byte[] getSendAddress(EthereumTransaction eTrans, int chainId) { X9ECParameters params = SECNamedCurves.getByName("secp256k1"); ECDomainParameters CURVE=new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH()); byte[] transactionHash; if ((eTrans.getSig_v()[0]==chainId*2+EthereumUtil.CHAIN_ID_INC) || (eTrans.getSig_v()[0]==chainId*2+EthereumUtil.CHAIN_ID_INC+1)) { transactionHash = EthereumUtil.getTransactionHashWithDummySignatureEIP155(eTrans); } else { transactionHash = EthereumUtil.getTransactionHashWithoutSignature(eTrans); } BigInteger bR = new BigInteger(1,eTrans.getSig_r()); BigInteger bS = new BigInteger(1,eTrans.getSig_s()); byte v =(byte) (eTrans.getSig_v()[0]); if (!((v == EthereumUtil.LOWER_REAL_V) || (v== (LOWER_REAL_V+1)))) { byte vReal = EthereumUtil.LOWER_REAL_V; if (((int)v%2 == 0)) { v = (byte) (vReal+0x01); } else { v = vReal; } } if (v < 27 || v > 34) { LOG.error("Header out of Range: "+v); throw new RuntimeException("Header out of range "+v); } if (v>=31) { v -=4; } int receiverId = v - 27; BigInteger n = CURVE.getN(); BigInteger i = BigInteger.valueOf((long) receiverId / 2); BigInteger x = bR.add(i.multiply(n)); ECCurve.Fp curve = (ECCurve.Fp) CURVE.getCurve(); BigInteger prime = curve.getQ(); if (x.compareTo(prime) >= 0) { return null; } X9IntegerConverter x9 = new X9IntegerConverter(); byte[] compEnc = x9.integerToBytes(x, 1 + x9.getByteLength(CURVE.getCurve())); boolean yBit=(receiverId & 1) == 1; compEnc[0] = (byte)(yBit ? 0x03 : 0x02); ECPoint R = CURVE.getCurve().decodePoint(compEnc); if (!R.multiply(n).isInfinity()) { return null; } BigInteger e = new BigInteger(1,transactionHash); BigInteger eInv = BigInteger.ZERO.subtract(e).mod(n); BigInteger rInv = bR.modInverse(n); BigInteger srInv = rInv.multiply(bS).mod(n); BigInteger eInvrInv = rInv.multiply(eInv).mod(n); ECPoint.Fp q = (ECPoint.Fp) ECAlgorithms.sumOfTwoMultiplies(CURVE.getG(), eInvrInv, R, srInv); byte[] pubKey=q.getEncoded(false); Keccak.Digest256 digest256 = new Keccak.Digest256(); digest256.update(pubKey,1,pubKey.length-1); byte[] kcck = digest256.digest(); return Arrays.copyOfRange(kcck,12,kcck.length); }  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 getTransActionSendAddressBlock1346406() 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[] expectedSentAddress = new byte[] {(byte)0x39,(byte)0x42,(byte)0x4b,(byte)0xd2,(byte)0x8a,(byte)0x22,(byte)0x23,(byte)0xda,(byte)0x3e,(byte)0x14,(byte)0xbf,(byte)0x79,(byte)0x3c,(byte)0xf7,(byte)0xf8,(byte)0x20,(byte)0x8e,(byte)0xe9,(byte)0x98,(byte)0x0a}; assertArrayEquals(expectedSentAddress,EthereumUtil.getSendAddress(trans0,EthereumUtilTest.CHAIN_ID),"Block 1346406 Transaction 1 send address is correctly calculated"); EthereumTransaction trans1 = eTrans.get(1); expectedSentAddress = new byte[] {(byte)0x4b,(byte)0xb9,(byte)0x60,(byte)0x91,(byte)0xee,(byte)0x9d,(byte)0x80,(byte)0x2e,(byte)0xd0,(byte)0x39,(byte)0xc4,(byte)0xd1,(byte)0xa5,(byte)0xf6,(byte)0x21,(byte)0x6f,(byte)0x90,(byte)0xf8,(byte)0x1b,(byte)0x01}; assertArrayEquals(expectedSentAddress,EthereumUtil.getSendAddress(trans1,EthereumUtilTest.CHAIN_ID),"Block 1346406 Transaction 2 send address is correctly calculated"); EthereumTransaction trans2 = eTrans.get(2); expectedSentAddress = new byte[] {(byte)0x63,(byte)0xa9,(byte)0x97,(byte)0x5b,(byte)0xa3,(byte)0x1b,(byte)0x0b,(byte)0x96,(byte)0x26,(byte)0xb3,(byte)0x43,(byte)0x00,(byte)0xf7,(byte)0xf6,(byte)0x27,(byte)0x14,(byte)0x7d,(byte)0xf1,(byte)0xf5,(byte)0x26}; assertArrayEquals(expectedSentAddress,EthereumUtil.getSendAddress(trans2,EthereumUtilTest.CHAIN_ID),"Block 1346406 Transaction 3 send address is correctly calculated"); EthereumTransaction trans3 = eTrans.get(3); expectedSentAddress = new byte[] {(byte)0x63,(byte)0xa9,(byte)0x97,(byte)0x5b,(byte)0xa3,(byte)0x1b,(byte)0x0b,(byte)0x96,(byte)0x26,(byte)0xb3,(byte)0x43,(byte)0x00,(byte)0xf7,(byte)0xf6,(byte)0x27,(byte)0x14,(byte)0x7d,(byte)0xf1,(byte)0xf5,(byte)0x26}; assertArrayEquals(expectedSentAddress,EthereumUtil.getSendAddress(trans3,EthereumUtilTest.CHAIN_ID),"Block 1346406 Transaction 4 send address is correctly calculated"); EthereumTransaction trans4 = eTrans.get(4); expectedSentAddress = new byte[] {(byte)0x63,(byte)0xa9,(byte)0x97,(byte)0x5b,(byte)0xa3,(byte)0x1b,(byte)0x0b,(byte)0x96,(byte)0x26,(byte)0xb3,(byte)0x43,(byte)0x00,(byte)0xf7,(byte)0xf6,(byte)0x27,(byte)0x14,(byte)0x7d,(byte)0xf1,(byte)0xf5,(byte)0x26}; assertArrayEquals(expectedSentAddress,EthereumUtil.getSendAddress(trans4,EthereumUtilTest.CHAIN_ID),"Block 1346406 Transaction 5 send address is correctly calculated"); EthereumTransaction trans5 = eTrans.get(5); expectedSentAddress = new byte[] {(byte)0x63,(byte)0xa9,(byte)0x97,(byte)0x5b,(byte)0xa3,(byte)0x1b,(byte)0x0b,(byte)0x96,(byte)0x26,(byte)0xb3,(byte)0x43,(byte)0x00,(byte)0xf7,(byte)0xf6,(byte)0x27,(byte)0x14,(byte)0x7d,(byte)0xf1,(byte)0xf5,(byte)0x26}; assertArrayEquals(expectedSentAddress,EthereumUtil.getSendAddress(trans5,EthereumUtilTest.CHAIN_ID),"Block 1346406 Transaction 6 send address is correctly calculated"); } finally { if (ebr!=null) { ebr.close(); } } }