[Ethereum] Web3j Signature and Solidity Ecrecover

ecrecovergo-ethereumjavasolidityweb3j

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(Numeric.toHexString(credentials.getAddress().getBytes()));
    System.out.println(Numeric.toHexString(testString.getBytes()));
    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

0x60009F80EA2D060fd804285d8a8a649351521582

However my java program is outputting the address as

0x307838336530386135653737393031313236613136373231353930613865313337396662376231623662

Anyone see where I'm going wrong?

The values I'm plugging into Ecrecover:

R: 0x524d3f28b7aad7418d9b9e30615e26104925b5fbaaf868a87bcf1b0b42172ec2
S: 0x07014e250540ce8e6dffb45e8c90e34ad5a31a3bbe5bb9a958ae7fdbee4f2048
V: 28

Best Answer

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.