I'm trying to understand how to sign a message in Java and recover the public key in a Solidity smart contract however my public keys don't seem to match.

Here's my Java code to generate the signature:

    String testString = "testing signature method";
    Sign.SignatureData signatureData = Sign.signMessage(testString.getBytes(), credentials.getEcKeyPair());

    System.out.println("R: "+Numeric.toHexString(signatureData.getR()));
    System.out.println("S: "+Numeric.toHexString(signatureData.getS()));
    System.out.println("V: "+signatureData.getV());

And here's my smart contract verify function:

function verify(bytes32 testStringBytes, uint8 v, bytes32 r, bytes32 s) constant returns (address) {

bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixedValue = keccak256(prefix, testStringBytes);
return ecrecover(prefixedValue , v, r, s);

The output I get for the verify function is


However my java program is outputting the address as


Anyone see where I'm going wrong?

The values I'm plugging into Ecrecover:

R: 0x524d3f28b7aad7418d9b9e30615e26104925b5fbaaf868a87bcf1b0b42172ec2
S: 0x07014e250540ce8e6dffb45e8c90e34ad5a31a3bbe5bb9a958ae7fdbee4f2048
V: 28
  • What you should compare is if the address given by solidity match the address used to sign the message.
    – Jaime
    Jun 21, 2018 at 14:08
  • @Jaime I updated the question, the output still doesn't match
    – Hughes_J
    Jun 21, 2018 at 14:13
  • In solidity you are passing the hash of the message, you should use the message instead.
    – Jaime
    Jun 21, 2018 at 15:45
  • That was something I didn't make clear in the code apologies, I'm actually only passing the original message. I updated the question again.
    – Hughes_J
    Jun 21, 2018 at 16:08
  • can you write down the message to try to replicate it?
    – Jaime
    Jun 21, 2018 at 17:49

1 Answer 1


So there were two problems with my code. Firstly, I read online about the need to append "\x19Ethereum Signed Message:\n32" to the hash in Solidity, however, this is only required when using Web3js not Web3j. Secondly, I was hashing in my smart contract when the data was not hashed originally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.