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
signup
  • 15
  • 5

1 Answers1

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

longhuynh
  • 121
  • 5