ERC-3009 Signature-approval-based token transfer (Transfer With Authorization)
Draft · Tokens · created 2020-09-28
If a token holder signs the transfer details off-chain, someone else can submit that signature to execute an ERC-20 transfer. The holder can send tokens without holding ether and can leave the gas fee to someone else.
Why it is needed
To send an ERC-20 token, the sender must submit the transaction themselves and pay the fee (gas) in ether (ETH). This creates a problem for users who hold tokens but no ether: they cannot transfer at all. ERC-2612, which serves the same purpose, uses a sequential number (nonce), so creating several transactions at once can cause number collisions, or a miner reordering transactions can cause failures. These failures become more frequent when gas fees are high and transactions sit in the queue for a long time. In addition, ERC-20's approve/transferFrom pattern is vulnerable to multiple-withdrawal attacks and encourages bad habits like unlimited approvals. ERC-777 and other proposals meant to replace it failed to gain wide adoption due to compatibility and security issues. A way was therefore needed to safely delegate transfers with a single signature, without relying on the approval pattern.
What to implement
event AuthorizationUsed(address indexed authorizer, bytes32 indexed nonce);: An event signaling that an authorization (nonce) has been used.function authorizationState(address authorizer, bytes32 nonce) external view returns (bool);: Returns whether a specific nonce for a specific authorizer has already been used.
In depth
Two transfer functions and state lookup
transferWithAuthorization takes from, to, value, validAfter, validBefore, nonce, and the signature v, r, s, and executes the transfer; anyone can submit it. receiveWithAuthorization takes the same arguments but additionally checks that the caller matches the recipient (to), preventing front-running by attackers watching the transaction pool. authorizationState(authorizer, nonce) can be used to look up whether a given nonce has been used, and an AuthorizationUsed(authorizer, nonce) event fires when an authorization is consumed. A contract function wrapping receiveWithAuthorization can take all the arguments ABI-encoded as a single bytes value to reduce the argument count.
Random 32-byte nonces instead of sequential nonces
ERC-2612 uses a sequential nonce, but this standard uses a random 32-byte nonce unique per authorizer address. Sequential nonces cause many problems in meta-transactions. A transaction with too high a number fails immediately instead of waiting, wasting gas. If a miner reorders transactions, even a correctly numbered one can fail. When using multiple apps at once, the next number cannot be known because of unconfirmed nonces, and when gas fees are high and transactions sit in the queue for a long time, the same number can easily be reused by mistake. With random nonces, a user can create as many transactions as they want concurrently. Used nonces are recorded in a mapping and rejected if reused.
The validAfter and validBefore validity window
Both values are Unix timestamps that define the interval during which the authorization is valid. Because submission is left to the relayer, the exact timing of when the transaction lands cannot be controlled precisely, so these parameters let the authorization become valid only from a specific future point, or set a deadline, protecting the user from side effects of submission that is too early or too late. The reference implementation checks now > validAfter and now < validBefore and rejects authorizations outside that window.
EIP-712 signatures and replay protection
The signature values v, r, s must be obtained using the EIP-712 typed message signing method. A Keccak-256 digest is formed by combining the domain separator, which contains the token's name, version, chainId, and verifyingContract, together with the type hashes (TRANSFER_WITH_AUTHORIZATION_TYPEHASH, RECEIVE_WITH_AUTHORIZATION_TYPEHASH) and the parameter values, and is signed with the holder's private key. Because the domain separator includes the contract address and chain ID, the signature is valid only for that token contract and cannot be replayed on another chain. The composition of the domain separator is up to the implementing contract, but including verifyingContract and chainId is strongly recommended. The signature can be obtained via a web3 provider's eth_signTypedData_v4 method.
The optional authorization cancellation feature
cancelAuthorization(authorizer, nonce, v, r, s) is optional. An authorization that has not yet been used can be canceled with an EIP-712 signature based on CANCEL_AUTHORIZATION_TYPEHASH, and a cancellation emits an AuthorizationCanceled(authorizer, nonce) event.
A forwarder pattern for existing contracts
To attach this standard to an existing contract that only supports the approve/transferFrom pattern, a forwarder contract is placed in front of it. The forwarder extracts the user and amount from the authorization, pulls the funds to itself via receiveWithAuthorization, then approves the parent contract to spend them and calls the relevant method, returning the resulting tokens and any remaining balance to the user. The example code checks that `to` is the forwarder itself and uses nonReentrant to prevent reentrancy.
Scope of application and proposal status
This standard does not apply to smart contract accounts, since contract accounts can use ERC-20's transfer and approve/transferFrom directly. For maximum compatibility, it is also recommended to implement ERC-2612 alongside this standard. The original status is Draft, meaning it is a proposal stage whose content may still change.
Things to watch out for
- Use receiveWithAuthorization in contracts: When calling from another contract, using transferWithAuthorization lets an attacker extract the authorization from the transaction pool and execute it first, which can leave an unprocessed deposit stuck. receiveWithAuthorization prevents this by checking that the caller is the recipient. If there are multiple functions that accept receiving authorizations, the leading bytes of the nonce can be designated as an identifier to prevent cross-use.
- Caution with ordering when submitting multiple transactions at once: Relayers and miners determine the processing order. For transfers that do not depend on one another this is usually not an issue, but for transfers with strong dependencies, it is recommended to submit the signed authorizations one at a time.
- Rejecting the zero address from ecrecover: The built-in ecrecover returns the zero address for an invalid signature. If the zero address is not rejected, funds belonging to the zero address could be transferred or approved without authorization, so it must always be rejected.