2

I have a code that is working:

solidity

function checkSignature(bytes32 h, uint8 v, bytes32 r, bytes32 s) public pure returns (address signer) {
    bytes memory prefix = "\x19Ethereum Signed Message:\n32";    
    bytes32 prefixedHash = keccak256(abi.encodePacked(prefix,h));  
    signer = ecrecover(prefixedHash, v, r, s); 
}

javascript

    const message = "Hello";
    const h = web3.utils.soliditySha3(message);
    console.log(h.length);
    let signature = await web3.eth.sign(h, second);

    var r = signature.slice(0, 66);
    var s = "0x" + signature.slice(66, 130);
    var v = "0x" + signature.slice(130, 132);
    v = web3.utils.toDecimal(v);
    v = v + 27;

    const result = await multisig.checkSignature(h, v, r, s);
    //result === second

The logged length of h is 66. 512bit hash should be 64bytes. The first 2 bytes(0x)denotes this is a hex value.

I am sending it as a solidity bytes32 variable which has a fixed length of 32. How is the code working?

I might be missing something very obvious. Please point it out. Thanks in advance.

3

You are using soliditySha3, which produces a 256-bit (32 bytes) hash, because of that your code works with bytes32.

As you can see in the docs, the output of soliditySha3 is something like the following, with a length of 66, 64 without 0x. Check that one byte corresponds to two chars, so the result is 32 bytes.

var x = web3.utils.soliditySha3('234'); // auto detects: uint256
> "0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2
  • 1
    Ok. I forgot a hex char was half a byte. There are 16 combinations which can be represented by 4 bits. – Sanjay S B Aug 8 at 12:26

Your Answer

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

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