0

i want to compare two signatures

one i created from web3

  const privateKey = //'address user'
  const hash = web3.utils.soliditySha3(address);
  const signature1 = web3.eth.accounts.sign(hash, privateKey);  

and from solidity (erc1155) i am generating like this

bytes32 pack = keccak256(abi.encodePacked(msg.sender));
 bytes32 signature2 = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32",pack));

i want to compare but those signatures never are the same, how can i get the same signature?

signature1==signature2

1 Answer 1

0

You can import the ECDSA.sol from OpenZeppelin to verify signature. In solidity:

function _recoverSigner(bytes32 hash, bytes memory _signature) internal returns(address) { 
        return ECDSA.recover(
            keccak256(
                abi.encodePacked("\x19Ethereum Signed Message:\n32", hash)
            ),
            _signature
        );
    }

UPDATE

I think I misunderstood your question. The solution I propose above is a way to "Verify" signature1.

Why do you want to generate signature in solidity? In my opinion, we should not sign anything on solidity! Here is reason why: link

The things that many projects on Ethereum do is to let the user sign a transaction (like the one that you created using web3 - "signature1"), then use that "signature1" and verify it on the smart contract:

In client's dapp

  const privateKey = //'address user'
  const hash = web3.utils.soliditySha3(address);
  const signature1 = web3.eth.accounts.sign(hash, privateKey);  

In solidity - verify it:

function verifySignature(address _signer, bytes32 hash, bytes calldata _signature) public returns (bool){
  address signer = _recoverSigner(hash, _signature);
  return _signer == signer;
}

Here is an article about signing & validating: Medium

3
  • with your code i would comparing like this signature1==signature2 ?
    – signup
    May 7, 2022 at 4:40
  • I have just updated the answer, please check it out
    – longhuynh
    May 7, 2022 at 8:40
  • for this method (address _signer, bytes32 hash, bytes calldata _signature), from web3 i should send the hash and signature right?
    – signup
    May 7, 2022 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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