1

I am trying to make a Meta Transaction without using the conventional TypedData object. Instead I have used ethers.utils._TypedDataEncoder.hash to create the hash.

I signed the hash using the ethereumjs-util and eth-sig-util packages but want a different approach to create the signature.

Now I am not sure how to use the hash without using the ethereumjs-util and eth-sig-util packages but rather the signer created using the private key (const walletSigner = new ethers.Wallet(PRIVATE_KEY, provider)).

const signature = await walletSigner.sign(hash)

Something like the above which will create a valid signature.

Please let me know if the hash can be signed using the web3.js Javascript Ethereum API.

1 Answer 1

0

for meta transaction you have 3 steps :

1-Implement the signature function in your smart contract and pass your values to it. it use "_hashTypedDataV4" and "keccak256", like below:

function hashGenerator(YourStruct memory yourStruct)
    internal
    view
    returns (bytes32)
{
    return
        _hashTypedDataV4(
            keccak256(
                abi.encode(
                    keccak256(
                        "YourStruct(address x,uint256 y)"
                    ),
                    yourStruct.x//x value 
                    yourStruct.y//y value
                    
                )
            )
        );
}

2-in web3 you must do the same in sign values, like this

 const signature = await signer._signTypedData(
  // Domain
  {
    name: "nameOfYourContract",
    version: "1.0",//your version
    chainId: yourchainID,
    verifyingContract: this.yourContract,
  },
  // Types
  {
    YourStruct: [
      { name: "x", type: "address" },
      { name: "y", type: "uint256" },
    ],
  },
  // Values
  {
   x_value,
   y_value
  } //
);

3-in the last step you have to compare web3 signature with your smart contract signature to prove it.

  function signVerify(
    YourStruct memory yourStruct,
    bytes calldata signature
) internal view returns (address) {
    bytes32 digest = hashGenerator(yourStruct);
    return ECDSAUpgradeable.recover(digest, signature);
}

for better understanding, I recommend to read this link.

Your Answer

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

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