Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time
//SPDX-License-Identifier: MIT
pragma solidity 0.6.6;
import "./lib/EIP712Base.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
contract EIP712MetaTransaction is EIP712Base {
using SafeMath for uint256;
bytes32 private constant META_TRANSACTION_TYPEHASH = keccak256(bytes("MetaTransaction(uint256 nonce,address from,bytes functionSignature)"));
event MetaTransactionExecuted(address userAddress, address payable relayerAddress, bytes functionSignature);
mapping(address => uint256) private nonces;
* Meta transaction structure.
* No point of including value field here as if user is doing value transfer then he has the funds to pay for gas
* He should call the desired function directly in that case.
struct MetaTransaction {
uint256 nonce;
address from;
bytes functionSignature;
constructor(string memory name, string memory version) public EIP712Base(name, version) {}
function convertBytesToBytes4(bytes memory inBytes) internal returns (bytes4 outBytes4) {
if (inBytes.length == 0) {
return 0x0;
assembly {
outBytes4 := mload(add(inBytes, 32))
function executeMetaTransaction(address userAddress,
bytes memory functionSignature, bytes32 sigR, bytes32 sigS, uint8 sigV) public payable returns(bytes memory) {
bytes4 destinationFunctionSig = convertBytesToBytes4(functionSignature);
require(destinationFunctionSig != msg.sig, "functionSignature can not be of executeMetaTransaction method");
MetaTransaction memory metaTx = MetaTransaction({
nonce: nonces[userAddress],
from: userAddress,
functionSignature: functionSignature
require(verify(userAddress, metaTx, sigR, sigS, sigV), "Signer and signature do not match");
nonces[userAddress] = nonces[userAddress].add(1);
// Append userAddress at the end to extract it from calling context
(bool success, bytes memory returnData) = address(this).call(abi.encodePacked(functionSignature, userAddress));
require(success, "Function call not successful");
emit MetaTransactionExecuted(userAddress, msg.sender, functionSignature);
return returnData;
function hashMetaTransaction(MetaTransaction memory metaTx) internal pure returns (bytes32) {
return keccak256(abi.encode(
function getNonce(address user) external view returns(uint256 nonce) {
nonce = nonces[user];
function verify(address user, MetaTransaction memory metaTx, bytes32 sigR, bytes32 sigS, uint8 sigV) internal view returns (bool) {
address signer = ecrecover(toTypedMessageHash(hashMetaTransaction(metaTx)), sigV, sigR, sigS);
require(signer != address(0), "Invalid signature");
return signer == user;
function msgSender() internal view returns(address sender) {
if(msg.sender == address(this)) {
bytes memory array =;
uint256 index =;
assembly {
// Load the 32 bytes word from memory with the address on the lower 20 bytes, and mask those.
sender := and(mload(add(array, index)), 0xffffffffffffffffffffffffffffffffffffffff)
} else {
sender = msg.sender;
return sender;