EIPs Explained

ERC-2771 · Final · ERC · 2020-07-01

ERC-2771: Secure Protocol for Native Meta Transactions

A contract-level convention for gasless meta-transactions: a trusted forwarder appends the real signer's address to calldata, and the recipient reads it with _msgSender() instead of msg.sender.

What problem does it solve?

A user who holds tokens but no ETH cannot pay for gas, so they cannot interact with a contract. A meta-transaction solves this: the user signs a request off-chain, and a third party (a gas relay) submits it on-chain and pays the gas.

The catch is msg.sender. When the relay submits the call, the contract sees the relay (or a forwarder contract) as the sender, not the user. ERC-2771 standardises how the real signer's address is passed through securely, and how wallets can discover that a contract supports this, without any protocol change.

How it works

Four roles:

  • Transaction Signer: the user who signs the request.
  • Gas Relay: an untrusted party that pays gas and submits the transaction.
  • Trusted Forwarder: a contract that verifies the user's signature and nonce, then calls the recipient.
  • Recipient: your contract.

The forwarder must append the signer's 20-byte address to the end of the calldata. The recipient then:

  1. checks that msg.sender is a forwarder it trusts;
  2. if so, and calldata is at least 20 bytes, reads the last 20 bytes as the real sender;
  3. otherwise, uses msg.sender as usual.

Contracts expose this through _msgSender() (and _msgData(), which strips the suffix) and must implement:

function isTrustedForwarder(address forwarder) external view returns (bool);

It must not revert and should use less than 50,000 gas, so wallets and other contracts can query it.

Sample code

A recipient with one immutable trusted forwarder (OpenZeppelin ships the same logic as ERC2771Context):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Recipient {
    address private immutable _trustedForwarder;
    mapping(address => uint256) public points;

    constructor(address forwarder) { _trustedForwarder = forwarder; }

    function isTrustedForwarder(address forwarder) public view returns (bool) {
        return forwarder == _trustedForwarder;
    }

    function _msgSender() internal view returns (address signer) {
        signer = msg.sender;
        if (msg.data.length >= 20 && isTrustedForwarder(signer)) {
            assembly { signer := shr(96, calldataload(sub(calldatasize(), 20))) }
        }
    }

    function claim() external {
        points[_msgSender()] += 1; // credited to the user, not the relayer
    }
}

On the forwarder side, the call looks like:

(bool ok, ) = to.call{value: value, gas: gasLimit}(abi.encodePacked(data, from));

Things to know

  • Status: Final.
  • Trusting a forwarder means trusting it with every user's identity: a malicious or buggy forwarder can make _msgSender() return any address. Keep the trusted set immutable if possible, and treat an upgradeable forwarder as part of your trust base.
  • If the set of trusted forwarders can change, only the contract owner should be able to change it; otherwise an attacker can add their own forwarder and impersonate anyone.
  • Combining ERC-2771 with multicall/delegatecall batching has caused real address-spoofing bugs, because inner calls can carry an attacker-controlled 20-byte suffix. Use library versions that handle this.
  • For new designs, smart accounts (ERC-4337, EIP-7702) with paymasters achieve gasless UX without each contract needing to support a forwarder. For token approvals alone, ERC-2612 permit is simpler.

#meta-transactions #gasless #relayer #forwarder #ux

Details

Authors
Ronan Sandford (@wighawag), Liraz Siri (@lirazsiri), Dror Tirosh (@drortirosh), Yoav Weiss (@yoavw), Alex Forshtat (@forshtat), Hadrien Croubois (@Amxx), Sachin Tomar (@tomarsachin2271), Patrick McCorry (@stonecoldpat), Nicolas Venturo (@nventuro), Fabian Vogelsteller (@frozeman), Gavin John (@Pandapip1)
Status
Final
Type
Standards Track / ERC
Created
2020-07-01
Required by

Machine-readable: Markdown · JSON