I am using the following to sign a message
var str="Trial with signature";
text=web3.sha3(str);
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;
assembly
{
r := mload(add(sig, 32))
s := mload(add(sig, 64))
v := byte(0, mload(add(sig, 96)))
}
if(v<27)
v+=27;
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.
mload(add(sig, 65))
instead ofmload(add(sig, 96))
and see answer below aboutEthereum Signed Message
prefix.