EthereumUtil { public static RLPObject rlpDecodeNextItem(ByteBuffer bb) { RLPObject result=null; int objType = detectRLPObjectType(bb); switch (objType) { case EthereumUtil.RLP_OBJECTTYPE_ELEMENT: result=EthereumUtil.decodeRLPElement(bb); break; case EthereumUtil.RLP_OBJECTTYPE_LIST: result=EthereumUtil.decodeRLPList(bb); break; default: LOG.error("Unknown object type"); } 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 decodeRLPElementString() { ByteBuffer wrapper = ByteBuffer.wrap(EthereumUtilTest.TEST_RLP_ELEMENT_STRING); RLPObject elementObject = EthereumUtil.rlpDecodeNextItem(wrapper); RLPElement element = (RLPElement)elementObject; byte[] expectedIndicator = new byte[] {(byte) 0x83}; assertArrayEquals( expectedIndicator, element.getIndicator(),"Indicator is correct"); byte[] expectedRawData = new byte[]{'d','o','g'}; assertArrayEquals( expectedRawData,element.getRawData(),"Raw data is correct"); }
@Test public void decodeRLPListString() { ByteBuffer wrapper = ByteBuffer.wrap(EthereumUtilTest.TEST_RLP_LIST_STRING); RLPObject elementObject = EthereumUtil.rlpDecodeNextItem(wrapper); RLPList list = (RLPList)elementObject; assertEquals(2,list.getRlpList().size(),"List of size 2"); RLPElement firstElement = (RLPElement) list.getRlpList().get(0); byte[] expectedIndicator = new byte[]{(byte) 0x83}; assertArrayEquals(expectedIndicator,firstElement.getIndicator(),"First element indicator is correct"); byte[] expectedRawData=new byte[]{'c','a','t'}; assertArrayEquals(expectedRawData,firstElement.getRawData(),"First element raw data is correct"); RLPElement secondElement = (RLPElement) list.getRlpList().get(1); expectedIndicator=new byte[]{(byte) 0x83}; assertArrayEquals(expectedIndicator,secondElement.getIndicator(),"Second element indicator is correct"); expectedRawData=new byte[]{'d','o','g'}; assertArrayEquals(expectedRawData,secondElement.getRawData(),"Second element raw data is correct"); }
@Test public void decodeRLPEmptyString() { ByteBuffer wrapper = ByteBuffer.wrap(EthereumUtilTest.TEST_RLP_EMPTY_STRING); RLPObject elementObject = EthereumUtil.rlpDecodeNextItem(wrapper); RLPElement element = (RLPElement)elementObject; byte[] expectedIndicator = new byte[] {(byte) 0x80}; assertArrayEquals( expectedIndicator, element.getIndicator(),"Indicator is correct"); assertEquals( 0,element.getRawData().length,"Raw data is zero"); }
@Test public void decodeRLPEmptyList() { ByteBuffer wrapper = ByteBuffer.wrap(EthereumUtilTest.TEST_RLP_EMPTY_LIST); RLPObject elementObject = EthereumUtil.rlpDecodeNextItem(wrapper); RLPList list = (RLPList)elementObject; assertEquals(0,list.getRlpList().size(),"List of size 0"); }
@Test public void decodeRLPElementInt15() { ByteBuffer wrapper = ByteBuffer.wrap(EthereumUtilTest.TEST_RLP_ELEMENT_INT_15); RLPObject elementObject = EthereumUtil.rlpDecodeNextItem(wrapper); RLPElement element = (RLPElement)elementObject; byte[] expectedIndicator = new byte[] {(byte) 0x0f}; assertArrayEquals( expectedIndicator, element.getIndicator(),"Indicator is correct"); assertEquals( 1,element.getRawData().length,"Raw data has length 1"); assertEquals( 15,element.getRawData()[0],"Raw data contains the element 15"); }
@Test public void decodeRLPElementInt1024() { ByteBuffer wrapper = ByteBuffer.wrap(EthereumUtilTest.TEST_RLP_ELEMENT_INT_1024); RLPObject elementObject = EthereumUtil.rlpDecodeNextItem(wrapper); RLPElement element = (RLPElement)elementObject; byte[] expectedIndicator = new byte[] {(byte) 0x82}; assertArrayEquals( expectedIndicator, element.getIndicator(),"Indicator is correct"); assertEquals( 2,element.getRawData().length,"Raw data has length 2"); assertArrayEquals( ByteBuffer.allocate(2).putShort((short) 1024).array(),element.getRawData(),"Raw data contains the element 1024"); }
@Test public void decodeRLPListSet3() { ByteBuffer wrapper = ByteBuffer.wrap(EthereumUtilTest.TEST_RLP_LIST_SET3); RLPObject elementObject = EthereumUtil.rlpDecodeNextItem(wrapper); RLPList list = (RLPList)elementObject; assertEquals(3,list.getRlpList().size(),"List of size 3"); RLPList firstList=(RLPList) list.getRlpList().get(0); assertEquals(0,firstList.getRlpList().size(),"First list is empty"); RLPList secondList=(RLPList) list.getRlpList().get(1); assertEquals(1,secondList.getRlpList().size(),"Second list contains one object"); RLPList secondListOneObject=(RLPList) secondList.getRlpList().get(0); assertEquals(0,secondListOneObject.getRlpList().size(),"Second List First Element is an empty list"); RLPList thirdList = (RLPList) list.getRlpList().get(2); assertEquals(2,thirdList.getRlpList().size(),"Third list contains two objects"); RLPList thirdListFirstObject = (RLPList) thirdList.getRlpList().get(0); assertEquals(0,thirdListFirstObject.getRlpList().size(),"Third list first object is an empyt list"); RLPList thirdListSecondObject = (RLPList) thirdList.getRlpList().get(1); assertEquals(1,thirdListSecondObject.getRlpList().size(),"Third list second object is a list of size 1"); RLPList thirdListSecondObjectListOneObject = (RLPList) thirdListSecondObject.getRlpList().get(0); assertEquals(0,thirdListSecondObjectListOneObject.getRlpList().size(),"Third list second object list first item is an empty list"); }
@Test public void decodeRLPElementLargeString() { ByteBuffer wrapper = ByteBuffer.wrap(EthereumUtilTest.TEST_RLP_LIST_LARGELIST); RLPObject elementObject = EthereumUtil.rlpDecodeNextItem(wrapper); RLPList list = (RLPList)elementObject; assertEquals(1,list.getRlpList().size(),"List contains one object"); RLPElement element = (RLPElement) list.getRlpList().get(0); byte[] expectedIndicator = new byte[] {(byte) 0xb8,(byte)0x4E}; assertArrayEquals( expectedIndicator, element.getIndicator(),"List first object indicator is correct"); byte[] expectedRawData = new byte[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; assertArrayEquals( expectedRawData,element.getRawData(),"List first object raw data is correct"); }