Skip to content

Commit

Permalink
fix Transaction.getChainId when v=27 must return null
Browse files Browse the repository at this point in the history
  • Loading branch information
jpnovais committed Dec 20, 2024
1 parent d952334 commit 8774b65
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
15 changes: 15 additions & 0 deletions core/src/test/java/org/web3j/protocol/core/ResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,21 @@ void testTransactionChainId() {
Transaction transaction = new Transaction();
transaction.setV(0x25);
assertEquals(transaction.getChainId(), (1L));

Transaction transaction2 = new Transaction();
transaction2.setV(0x24);
assertEquals(transaction2.getChainId(), (0L));
}

@Test
void testTransactionWithoutChainId() {
Transaction transaction1 = new Transaction();
transaction1.setV(0x1b); // 27
assertEquals(transaction1.getChainId(), null);

Transaction transaction2 = new Transaction();
transaction2.setV(0x1c); // 28
assertEquals(transaction2.getChainId(), null);
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions crypto/src/main/java/org/web3j/crypto/TransactionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public static String generateTransactionHashHexEncoded(
* @param v recovery identifier
* @return Chain id
*/
public static long deriveChainId(long v) {
public static Long deriveChainId(long v) {
if (v == LOWER_REAL_V || v == (LOWER_REAL_V + 1)) {
return 0L;
return null;
}
return (v - CHAIN_ID_INC) / 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.web3j.crypto.TransactionUtils.generateTransactionHashHexEncoded;

public class TransactionUtilsTest {
Expand Down Expand Up @@ -60,10 +61,10 @@ void deriveChainIdWhenLegacySignature() {
long v1 = 27;
long v2 = 28;

long chainId_1 = TransactionUtils.deriveChainId(v1);
long chainId_2 = TransactionUtils.deriveChainId(v2);
Long chainId_1 = TransactionUtils.deriveChainId(v1);
Long chainId_2 = TransactionUtils.deriveChainId(v2);

assertEquals(0, chainId_1);
assertEquals(0, chainId_2);
assertNull(chainId_1);
assertNull(chainId_2);
}
}

0 comments on commit 8774b65

Please sign in to comment.