I have signed a data string using the following code snippet in web3js

var data = "hello world";

web3.personal.sign(web3.fromUtf8(data), web3.eth.accounts[0], function(error, result) {
    if (!error) {
    } else {

Now I am trying to verify the address used to sign said string from web3j

public static boolean verifyAddressFromSignature(String address, String signature) 
    byte[] signatureBytes = Hash.sha3(signature).getBytes();
    byte v = signatureBytes[64];
    if(v < 27) 
        v += 27; 

    SignatureData sd = new SignatureData(v, (byte[]) Arrays.copyOfRange(signatureBytes, 0, 32), (byte[]) Arrays.copyOfRange(signatureBytes, 32, 64));

    String addressRecovered = null;
    boolean match = false;

    // Iterate for each possible key to recover
    for (int i = 0; i < 4; i++) 
        BigInteger publicKey = Sign.recoverFromSignature((byte) i, new ECDSASignature(new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())), signature.getBytes());

        if (publicKey != null) 
            addressRecovered = "0x" + Keys.getAddress(publicKey); 
            if (addressRecovered.equals(address)) 
                match = true;

    return match;

However, none of the generated addresses match the one used to create the signature.

Can anyone shed any light on what I am doing wrong?


You are trying to verify the signature without providing what the message was which was hashed.. this is one more param you need to provide

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy