Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article examines ERC-2771, a standard for meta-transactions that allows another account to pay the gas costs of executing a user's requested contract operation.

The explanation below translates, summarizes, and expands on the original proposal.

Overview

ERC-2771 proposes a way for Ethereum contracts to accept calls made on behalf of users. These are known as meta-transactions. A forwarding contract can invoke another contract while conveying the identity of the user who authorized the operation.

Ordinarily, an externally owned account (EOA) submitting an Ethereum transaction must pay its gas costs.

An EOA is an Ethereum account controlled by a private key. Wallet software such as MetaMask can manage EOAs.

The proposed protocol makes it possible for users who do not hold enough ETH to pay gas to authorize contract operations. It introduces a contract known as a trusted Forwarder.

When a user wants to perform an operation, the user signs a request off-chain. A gas relay submits an Ethereum transaction to the Forwarder and pays its gas costs. The Forwarder verifies the request and calls the destination contract, known as the Recipient, with additional information identifying the original signer. The Recipient can then determine the effective sender and original calldata using the protocol's conventions, instead of treating the Forwarder as the user.

The key advantage is that applications can support this mechanism without fundamentally changing Ethereum's transaction protocol. Users without ETH can interact with participating contracts as long as a relay is willing to submit their authorized requests.

The following article also discusses meta-transactions.

https://chaldene.net/erc20permit#st-toc-h-7

Motivation

There has long been interest in allowing Ethereum contracts to accept operations authorized by EOAs that do not hold the ETH required to pay gas. Meta-transactions enable a third party to pay these costs. In the context of ERC-2771, a meta-transaction is an operation authorized by a transaction signer and relayed by a potentially untrusted third party, called a gas relay, which pays the transaction fees. This enables users without ETH to access participating contract functionality.

As illustrated below, a user can ask an intermediary to submit an authorized operation without directly paying the gas fee.

Third-party gas payment in a meta-transaction

Specification

Terminology

Transaction Signer

The Transaction Signer signs an off-chain request authorizing an operation and sends it to the gas relay.

Gas Relay

The Gas Relay receives the signed request, constructs an Ethereum transaction, pays its gas costs, and submits it to the trusted Forwarder for processing.

Trusted Forwarder

A Trusted Forwarder is a contract trusted by the Recipient to correctly verify the signature and nonce and validate the signer's request before forwarding it to the Recipient.

In the game example below, the game operator may run the relay infrastructure and deploy the trusted Forwarder. The protocol's trust relationship specifically means that the Recipient trusts the designated Forwarder contract to report the authenticated signer correctly.

The Forwarder is the immediate caller of the Recipient contract. It passes information that enables the Recipient to recognize the user as the effective sender of the authorized operation. The EVM's actual msg.sender is still the Forwarder, and the outer Ethereum transaction is submitted by the gas relay.

Recipient

The Recipient is a contract that accepts meta-transactions through a Trusted Forwarder. It processes the requested operation using the effective sender derived from the forwarded calldata.

The Transaction Signer authorizes the request off-chain, the Gas Relay pays for and submits the outer transaction, and the Trusted Forwarder verifies and forwards the request to the Recipient. This allows users without ETH to access the Recipient's functionality.

In a blockchain game, the player acts as the Transaction Signer. The game client obtains the player's signature and sends the signed request to the operator's Gas Relay. The relay submits a transaction to the Trusted Forwarder contract, which verifies the authorization. The Forwarder then calls the Recipient contract containing the game logic, which executes the requested operation on behalf of the player.

Meta-transaction roles in a blockchain game

Although the illustration may make the Trusted Forwarder and Recipient appear to be a single contract operated by the game provider, they represent distinct roles. The game provider can operate the relay and deploy or administer both contracts.

Example flow

ERC-2771 example flow Source: https://eips.ethereum.org/EIPS/eip-2771

Extracting the transaction signer's address

Appending the address in the Trusted Forwarder

When calling the Recipient, the Trusted Forwarder must append the transaction signer's address, represented as 20 bytes, to the original calldata.

javascript
(bool success, bytes memory returnData) = to.call.value(value)(abi.encodePacked(data, from));

Extracting the transaction signer's address

The Recipient can recover the effective sender using the following procedure.

  1. Check whether the immediate caller is a Trusted Forwarder.
    • The mechanism used to establish this trust is outside the scope of the proposal.
  2. If the caller is trusted and the calldata is at least 20 bytes long, extract its final 20 bytes as the original signer's address.
  3. If the caller is not a Trusted Forwarder, or if the calldata contains fewer than 20 bytes, return the original msg.sender.

Checking the Trusted Forwarder

The Recipient must check whether the immediate caller is trusted before interpreting appended bytes as the signer's address. Otherwise, an arbitrary caller could supply a forged sender address.

Discovering protocol support

Unless a frontend already knows that a Recipient supports native meta-transactions, it cannot reliably offer users that interaction method. A mechanism is therefore needed for Recipients to advertise their support.

This is particularly important for Web3 wallets, which may need to interact with previously unknown Recipient contracts.

Trusted Forwarders may have different interfaces and capabilities, including different batching mechanisms or signed message formats. Wallets need a way to determine whether a particular Forwarder is trusted by a given Recipient.

The Recipient must therefore implement the following function.

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

The isTrustedForwarder function must return true if the specified Forwarder is trusted by the Recipient and false otherwise. It must not revert.

The Recipient must also be capable of processing requests forwarded by its Trusted Forwarders.

Because isTrustedForwarder may be called on-chain, its execution cost should be bounded. The proposal recommends that it consume no more than 50,000 gas.

Rationale

The proposal standardizes a minimal contract interface to make native meta-transaction support easier to integrate. If a Recipient does not support ERC-2771, a user cannot assume that it will correctly interpret calls made through an ERC-2771 Forwarder.

Importance of a standardized contract interface

Without a common interface, clients lack a standard method for determining whether a Recipient supports a particular native meta-transaction protocol. They also lack a common convention for passing the original signer's identity to the Recipient.

Using Trusted Forwarders

Without a Trusted Forwarder, every Recipient would need to implement the logic required to safely authenticate and process meta-transactions itself.

Importance of a discovery mechanism

Without a discovery mechanism, clients cannot reliably determine whether a Recipient trusts a particular Forwarder.

Benefits of standardizing the contract interface

Separating the Recipient interface from the Forwarder's internal implementation allows Recipients to support different Forwarder implementations without changing their basic sender extraction logic, provided their trust configuration permits it.

The msg.sender problem and security

The EVM's msg.sender identifies the immediate caller, not necessarily the original user who authorized an operation. In a forwarded call, a contract that does not implement the relevant protocol sees the forwarding contract rather than the transaction signer. A secure meta-transaction protocol must prevent the gas relay from forging, modifying, or replaying requests authorized by the Transaction Signer. The Trusted Forwarder is responsible for the relevant request authentication and replay protection.

Reference implementation

javascript
contract RecipientExample {

    function purchaseItem(uint256 itemId) external {
        address sender = _msgSender();
        // ... perform the purchase for sender
    }

    address immutable _trustedForwarder;
    constructor(address trustedForwarder) internal {
        _trustedForwarder = trustedForwarder;
    }

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

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

}

This contract illustrates a Recipient that supports meta-transactions. It allows users to authorize a purchase without necessarily submitting the Ethereum transaction themselves.

purchaseItem

An externally callable function that performs a purchase using the supplied item ID and the effective sender.

_trustedForwarder

An immutable variable containing the Trusted Forwarder's address.

constructor

Receives the Trusted Forwarder's address during deployment and stores it in _trustedForwarder.

isTrustedForwarder

Checks whether the supplied address matches the configured Trusted Forwarder.

_msgSender

Returns the effective sender for ordinary and forwarded calls. When the immediate caller is trusted and at least 20 bytes of calldata are present, the assembly code extracts the appended signer address.

The original example uses historical Solidity syntax and declares isTrustedForwarder without the view modifier required by the interface. It is reproduced here for comparison with the original article and should be adapted and compiled before use.

Security considerations

A malicious Forwarder can misrepresent the value returned by the Recipient's _msgSender() function, allowing it to impersonate arbitrary accounts from the Recipient's perspective. Recipients must therefore be careful about which Forwarders they trust. If a Forwarder is upgradeable, the Recipient must also trust the parties controlling its upgrade mechanism.

Changes to the set of Trusted Forwarders must be restricted. Otherwise, an attacker could add a Forwarder under their control and cause the Recipient to accept forged sender identities. The proposal recommends making the Trusted Forwarder configuration immutable where practical. If updates are necessary, only appropriately authorized and trusted administrators should be able to perform them.

Selecting trustworthy Forwarder implementations and securing their configuration are therefore essential to preventing unauthorized operations.

Citation

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), "ERC-2771: Secure Protocol for Native Meta Transactions," Ethereum Improvement Proposals, no. 2771, July 2020. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-2771.

Conclusion

This article covered ERC-2771, a standard that allows applications to accept user-authorized operations through a Trusted Forwarder while a third party pays the transaction fees.

If you have questions, feel free to reach out via Twitter DM.

Twitter @cardene777

Original recruitment notice (2023)

The original article included the following recruitment notice for CryptoGames. It is retained for historical context and does not imply that the positions remain open today.

CryptoGames was recruiting new team members.

The author described the company as an environment offering substantial autonomy and opportunities to develop engineering skills, and invited people interested in blockchain, Web3, NFTs, or smart contract development to get in touch.