I am using the following to sign a message

 var str="Trial with signature";
 var sig=web3.eth.sign(eth.accounts[0],text);

Now after obtaining the hash value from the text and the signature, to verify the same, I use the following contract

contract signature
    event ret_addr(address addr);
    function sig_verify(bytes32 hash, bytes sig) returns(address)                       //verifying the signature
        bytes32 r;
        bytes32 s;
        uint8 v;
                r := mload(add(sig, 32))
                s := mload(add(sig, 64))
                v := byte(0, mload(add(sig, 96)))
        ret_addr(ecrecover(hash, v, r, s));

The issue is I am not getting the correct address back. Why is this so? It used to work fine earlier but now it is giving some random addresses.


2 Answers 2


Something is wrong with assembly section. You can debug by extending your event like this

event ret_addr(address addr, uint8 v, bytes32 r, bytes32 s);

and call it with corrwsponding variables

ret_addr(ecrecover(hash, v, r, s), v, r, s);

My result:

        "topic": "1171b064e2eda38d82ce5fadc8a911d493f2d22ff88013d965c9885638dee60d",
        "event": "ret_addr",
        "args": [

As you can see, v is neither 27 nor 28. r and s are not hex encoded but with 0x prefix.


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.