ERC-2771 (recommended)
Native meta transactions with top notch security
Last updated
Native meta transactions with top notch security
Last updated
If you plan to use ERC-2771 with a multicall
method or any other method using delegateCall()
Please read carefully the section
If you are using @gelatonetwork/relay-sdk
v3 or contracts from the package @gelatonetwork/relay-context
v2 please follow this to migrate to the new versions.
After reading this page:
You'll understand the difference between and
You'll understand how to use and in combination with to achieve a gasless UX for your app, with secure user signature verification.
You'll understand ERC-2771's and how it allows for the off-chain sender address to be verified on-chain.
As detailed in the section, it's crucial to ensure that your relay implementation is impervious to vulnerabilities when using a relayer. The most secure approach is to utilize our ERC-2771 implementations:
When using sponsoredCallERC2771
, you sponsor your user's gas fees, leveraging for payment. In contrast, with callWithSyncFeeERC2771
, the fees are paid from the target contract.
In both instances, users are prompted to sign their transaction's relay request using their private keys (for instance, through MetaMask). This step is crucial for security purposes. Gelato verifies on-chain that the user's signature corresponds with the required address before forwarding the call.
When relaying a message to a target smart contract function, it's essential for the function to authenticate the message's origin and confirm it was forwarded through the correct relayer. Without these verifications, your target function becomes susceptible to exploitation. employs sophisticated data encoding to relay the original _msgSender
from off-chain, and it guarantees that only the trustedForwarder
is capable of encoding this value. These two parameters, in tandem, safeguard against any potential misconduct, ensuring a secure transmission of information from off-chain to on-chain!
In the context of relaying, msg.sender
loses its usual informational significance. Under normal circumstances, msg.sender
would denote the user initiating the transaction; however, with off-chain relaying, we lose this valuable piece of information.
Consider this scenario: how does a target smart contract determine who can call a particular function? In this case, msg.sender
will be the relayer, but merely whitelisting this address is insufficient and still permits others using the same relayer to call your function. This situation can raise significant concerns, particularly when low-level calls are involved.
The optimal solution would be to allow the initiator of the relay call to specify an address and relay this address on-chain. The target smart contract can then authenticate a function call using this address.
The challenge then becomes: how can we successfully transmit information (a specific address) via low-level calldata
from off-chain to on-chain without disrupting the calldata
's integrity?
Here's where the real magic unfolds. The trustedForwarder
encodes the from
address (i.e., the off-chain address) into the calldata
by appending it at the end:
Now, the target contract can validate the from
address by decoding the data in the same manner, ensuring that this message has been passed through the trustedForwarder
.
The necessary target contract function can then confidently confirm that the correct entity signed and requested this payload to be relayed, and only via a trusted forwarder - in our case, the Gelato Relay.
Let's take as an example relay method sponsoredCallERC2771
. Method callWithSyncFeeERC2771
works similarly.
Gelato Relay's sponsoredCallERC2771
function encodes the user's address, which can then be utilized by the ERC-2771 compatible target
smart contract. The most relevant part, where the user address is appended to the calldata
, is shown below:
where _encodeERC2771Context
refers to:
We are encoding the calldata
and the user address together by simply appending the user's address to the end as required by ERC-2771.
or
The _msgSender()
function encapsulates the main functionality of ERC-2771, by decoding the user address from the last 20 bytes of the calldata
.
In Solidity, the logic is equivalent to:
The function _msgData()
removes the msg.sender
from the entire calldata
if the contract was called by the trustedForwarder
, or otherwise falls back to return the original calldata
.
Within the function that you would like to be called with Gelato Relay, replace all instances of msg.sender
with a call to the _msgSender()
function inherited from ERC2771Context
. _msgSender()
is the off-chain signer of the relay request, allowing for secure whitelisting on your target function.
If your contract is not upgradeable, then you will have to redeploy your contract to set GelatoRelay1BalanceERC2771.sol
as your trustedForwarder
:
GelatoRelay1BalanceERC2771.sol
is immutable for security reasons. This means that once you set GelatoRelay1BalanceERC2771.sol
as your trusted forwarder, there is no way for Gelato to change the ERC2771 signature verification scheme and so you can be sure that the intended _msgSender
is correct and accessible from within your target contract.
Let's take a look at an example using relay method sponsoredCallERC2771
. For callWithSyncFeeERC2771
please refer to the steps described .
See also relay-context-contracts:
This contract's main functionality (originally implemented by ) is to decode the off-chain msg.sender
from the encoded calldata using _msgSender()
.
The trustedForwarder
variable is set in the constructor which allows for setting a trusted party that will relay your message to your target
smart contract. In our case, this is Gelato Relay1BalanceERC2771.sol
which you can find in the section.
Gelato's smart contracts handle the encoding of important information to the calldata
(see ). It is the job of your target
smart contract function to decode this information using this _msgSender()
function.
Please refer to the section to find out which Gelato relay address to use as a trustedForwarder
. Use GelatoRelay1BalanceERC2771.sol
address for sponsoredCallERC2771
.