Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Transaction.getChainId when v=27 must return null #2133

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}