0

I am trying to make a small DApp using web3, ganache, Metamask, javascript and Remix.

My DApp has to sign a message and verify it, and I see two ways to sign a message.

One using web3.eth.sign(...), and the other using a contract function from this link:

  function recover(bytes32 hash, bytes signature)
    public
    pure
    returns (address)
  {
    bytes32 r;
    bytes32 s;
    uint8 v;

    // Check the signature length
    if (signature.length != 65) {
      return (address(0));
    }
    // Divide the signature in r, s and v variables with inline assembly.
    assembly {
      r := mload(add(signature, 0x20))
      s := mload(add(signature, 0x40))
      v := byte(0, mload(add(signature, 0x60)))
    }

    // Version of signature should be 27 or 28, but 0 and 1 are also possible versions
    if (v < 27) {
      v += 27;
    }
    // If the version is correct return the signer address
    if (v != 27 && v != 28) {
      return (address(0));
      //address(0)
    } else {
      // solium-disable-next-line arg-overflow
      return ecrecover(hash, v, r, s);
    }
  }

Is it better to sign local using javascript function or to sign in solidity?

6
  • Typically you shouldn't have any need to verify transaction details inside the blockchain. A contract simply accepts a transaction and processes it, the underlying blockchain framework takes care of all verifications. – Lauri Peltonen Apr 6 '20 at 4:30
  • thank you so much for comment, but in my DApp i need two operation, one to register some information, second allow to any user to check if information right and come from which source, so for that i used verify. Is there any mistake in my thought ? any suggestions to make this scenario in right way? @LauriPeltonen – norah Apr 6 '20 at 21:42
  • would it be enough to just check the sender's address? You get it from msg.sender – Lauri Peltonen Apr 7 '20 at 5:11
  • good, i will read about this function, I saw a lot of tutorials used these two function, Are you know when it be helpful to use it especially: 'recover' function? i mean in which scenario i may need a signature rather than account address? @LauriPeltonen ,, sorry to annoying you. – norah Apr 7 '20 at 21:32
  • sorry, no, not very familiar with recover – Lauri Peltonen Apr 8 '20 at 4:38

Your Answer

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

Browse other questions tagged or ask your own question.