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

Support public key recovery for EIP-155 transactions #1977

Open
wants to merge 2 commits 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
37 changes: 26 additions & 11 deletions crypto/src/main/java/org/web3j/crypto/Sign.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.bouncycastle.math.ec.FixedPointCombMultiplier;
import org.bouncycastle.math.ec.custom.sec.SecP256K1Curve;

import org.web3j.commons.ChainId;
import org.web3j.utils.Numeric;

import static org.bouncycastle.util.BigIntegers.TWO;
Expand Down Expand Up @@ -272,7 +273,7 @@
*/
public static BigInteger signedMessageToKey(byte[] message, SignatureData signatureData)
throws SignatureException {
return signedMessageHashToKey(Hash.sha3(message), signatureData);
return signedMessageHashToKey(Hash.sha3(message), signatureData, ChainId.MAIN_NET);
}

/**
Expand All @@ -288,9 +289,12 @@
*/
public static BigInteger signedPrefixedMessageToKey(byte[] message, SignatureData signatureData)
throws SignatureException {
return signedMessageHashToKey(getEthereumMessageHash(message), signatureData);
return signedPrefixedMessageToKey(message, signatureData, ChainId.MAIN_NET);
}
public static BigInteger signedPrefixedMessageToKey(byte[] message, SignatureData signatureData, ChainId chainId)
throws SignatureException {
return signedMessageHashToKey(getEthereumMessageHash(message), signatureData, chainId);
}

/**
* Given an arbitrary message hash and an Ethereum message signature encoded in bytes, returns
* the public key that was used to sign it. This can then be compared to the expected public key
Expand All @@ -302,27 +306,38 @@
* @throws SignatureException If the public key could not be recovered or if there was a
* signature format error.
*/
@Deprecated
public static BigInteger signedMessageHashToKey(byte[] messageHash, SignatureData signatureData)
throws SignatureException {
return signedPrefixedMessageToKey(messageHash, signatureData, ChainId.MAIN_NET);

Check warning on line 312 in crypto/src/main/java/org/web3j/crypto/Sign.java

View check run for this annotation

Codecov / codecov/patch

crypto/src/main/java/org/web3j/crypto/Sign.java#L312

Added line #L312 was not covered by tests
}

/**
* Given an arbitrary message hash and an Ethereum message signature encoded in bytes, returns
* the public key that was used to sign it. This can then be compared to the expected public key
* to determine if the signature was correct.
*
* @param messageHash The message hash.
* @param signatureData The message signature components
* @param chainId of the network
* @return the public key used to sign the message
* @throws SignatureException If the public key could not be recovered or if there was a
* signature format error.
*/
public static BigInteger signedMessageHashToKey(byte[] messageHash, SignatureData signatureData, ChainId chainId)
throws SignatureException {

byte[] r = signatureData.getR();
byte[] s = signatureData.getS();
verifyPrecondition(r != null && r.length == 32, "r must be 32 bytes");
verifyPrecondition(s != null && s.length == 32, "s must be 32 bytes");

int header = signatureData.getV()[0] & 0xFF;
// The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
// 0x1D = second key with even y, 0x1E = second key with odd y
if (header < 27 || header > 34) {
throw new SignatureException("Header byte out of range: " + header);
}

ECDSASignature sig =
new ECDSASignature(
new BigInteger(1, signatureData.getR()),
new BigInteger(1, signatureData.getS()));

int recId = header - 27;
int recId = Sign.getRecId(signatureData, chainId.getId());
BigInteger key = recoverFromSignature(recId, sig, messageHash);
if (key == null) {
throw new SignatureException("Could not recover public key from signature");
Expand Down
10 changes: 10 additions & 0 deletions crypto/src/test/java/org/web3j/crypto/SignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import org.web3j.commons.ChainId;
import org.web3j.utils.Numeric;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -116,6 +117,15 @@ public void testFromHex() throws SignatureException {
assertEquals(signatureData, (expected));
}

@Test
public void testSignedPrefixedMessageToKeyWithEip155() throws SignatureException {
Sign.SignatureData signatureData = Sign.signPrefixedMessage(TEST_MESSAGE, SampleKeys.KEY_PAIR);
assert(signatureData.getV()[0] == 28);
signatureData.getV()[0] = (byte)(ChainId.GOERLI.getId() * 2 + 36);
BigInteger key = Sign.signedPrefixedMessageToKey(TEST_MESSAGE, signatureData, ChainId.GOERLI);
assertEquals(key, (SampleKeys. PUBLIC_KEY));
}

@ParameterizedTest(name = "testGetRecId(chainId={0}, recId={1}, isEip155={2})")
@MethodSource("recIdArguments")
public void testGetRecId(final long chainId, final long recId, final boolean isEip155) {
Expand Down
4 changes: 3 additions & 1 deletion utils/src/main/java/org/web3j/commons/ChainId.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
package org.web3j.commons;

public enum ChainId {
MAIN_NET(1);
MAIN_NET(1),
GOERLI(5),
SEPOLIA(58008);

Check warning on line 18 in utils/src/main/java/org/web3j/commons/ChainId.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/org/web3j/commons/ChainId.java#L16-L18

Added lines #L16 - L18 were not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sepolia ChainId is 11155111?


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plz add Holesky chainId too 17000

private int id;

Expand Down
Loading