Skip to content

Commit 8349907

Browse files
authored
Merge pull request #648 from folex/master
Fix bug in RlpString.asBigInteger
2 parents 27506d9 + 467067f commit 8349907

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

crypto/src/main/java/org/web3j/crypto/TransactionDecoder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public static RawTransaction decode(String hexTransaction) {
1313
byte[] transaction = Numeric.hexStringToByteArray(hexTransaction);
1414
RlpList rlpList = RlpDecoder.decode(transaction);
1515
RlpList values = (RlpList) rlpList.getValues().get(0);
16-
BigInteger nonce = ((RlpString) values.getValues().get(0)).asBigInteger();
17-
BigInteger gasPrice = ((RlpString) values.getValues().get(1)).asBigInteger();
18-
BigInteger gasLimit = ((RlpString) values.getValues().get(2)).asBigInteger();
16+
BigInteger nonce = ((RlpString) values.getValues().get(0)).asPositiveBigInteger();
17+
BigInteger gasPrice = ((RlpString) values.getValues().get(1)).asPositiveBigInteger();
18+
BigInteger gasLimit = ((RlpString) values.getValues().get(2)).asPositiveBigInteger();
1919
String to = ((RlpString) values.getValues().get(3)).asString();
20-
BigInteger value = ((RlpString) values.getValues().get(4)).asBigInteger();
20+
BigInteger value = ((RlpString) values.getValues().get(4)).asPositiveBigInteger();
2121
String data = ((RlpString) values.getValues().get(5)).asString();
2222
if (values.getValues().size() > 6) {
2323
byte v = ((RlpString) values.getValues().get(6)).getBytes()[0];

rlp/src/main/java/org/web3j/rlp/RlpString.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public byte[] getBytes() {
2121
return value;
2222
}
2323

24-
public BigInteger asBigInteger() {
24+
public BigInteger asPositiveBigInteger() {
2525
if (value.length == 0) {
2626
return BigInteger.ZERO;
2727
}
28-
return new BigInteger(value);
28+
return new BigInteger(1, value);
2929
}
3030

3131
public String asString() {

rlp/src/test/java/org/web3j/rlp/RlpDecoderTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public class RlpDecoderTest {
2121
@Test
2222
public void testRLPDecode() {
2323

24+
// big positive number should stay positive after encoding-decoding
25+
// https://github.com/web3j/web3j/issues/562
26+
long value = 3000000000L;
27+
assertThat(RlpString.create(BigInteger.valueOf(value)).asPositiveBigInteger().longValue(),
28+
equalTo(value));
29+
2430
// empty array of binary
2531
assertTrue(RlpDecoder.decode(new byte[]{}).getValues().isEmpty());
2632

@@ -187,6 +193,5 @@ public void testRLPDecode() {
187193
assertThat(((RlpList)
188194
((RlpList) rlpList.getValues().get(0)).getValues().get(1)).getValues().size(),
189195
equalTo(9));
190-
191196
}
192197
}

0 commit comments

Comments
 (0)