Permalink
Browse files

Fix bug in RlpString.asBigInteger

  • Loading branch information...
folex committed Jul 26, 2018
1 parent 27506d9 commit 467067f2152a06611c7f46d6753e77b8b2328d1d
@@ -13,11 +13,11 @@ public static RawTransaction decode(String hexTransaction) {
byte[] transaction = Numeric.hexStringToByteArray(hexTransaction);
RlpList rlpList = RlpDecoder.decode(transaction);
RlpList values = (RlpList) rlpList.getValues().get(0);
BigInteger nonce = ((RlpString) values.getValues().get(0)).asBigInteger();
BigInteger gasPrice = ((RlpString) values.getValues().get(1)).asBigInteger();
BigInteger gasLimit = ((RlpString) values.getValues().get(2)).asBigInteger();
BigInteger nonce = ((RlpString) values.getValues().get(0)).asPositiveBigInteger();
BigInteger gasPrice = ((RlpString) values.getValues().get(1)).asPositiveBigInteger();
BigInteger gasLimit = ((RlpString) values.getValues().get(2)).asPositiveBigInteger();
String to = ((RlpString) values.getValues().get(3)).asString();
BigInteger value = ((RlpString) values.getValues().get(4)).asBigInteger();
BigInteger value = ((RlpString) values.getValues().get(4)).asPositiveBigInteger();
String data = ((RlpString) values.getValues().get(5)).asString();
if (values.getValues().size() > 6) {
byte v = ((RlpString) values.getValues().get(6)).getBytes()[0];
@@ -21,11 +21,11 @@ private RlpString(byte[] value) {
return value;
}
public BigInteger asBigInteger() {
public BigInteger asPositiveBigInteger() {
if (value.length == 0) {
return BigInteger.ZERO;
}
return new BigInteger(value);
return new BigInteger(1, value);
}
public String asString() {
@@ -21,6 +21,12 @@
@Test
public void testRLPDecode() {
// big positive number should stay positive after encoding-decoding
// https://github.com/web3j/web3j/issues/562
long value = 3000000000L;
assertThat(RlpString.create(BigInteger.valueOf(value)).asPositiveBigInteger().longValue(),
equalTo(value));
// empty array of binary
assertTrue(RlpDecoder.decode(new byte[]{}).getValues().isEmpty());
@@ -187,6 +193,5 @@ public void testRLPDecode() {
assertThat(((RlpList)
((RlpList) rlpList.getValues().get(0)).getValues().get(1)).getValues().size(),
equalTo(9));
}
}

0 comments on commit 467067f

Please sign in to comment.