ERC-7758 Signature-approval-based token transfer (Transfer With Authorization)

Review · Tokens · created 2020-09-28

A standard that lets a token holder approve a transfer with an EIP-712 signature, so someone else can submit that signature and pay the gas to move ERC-20 tokens on the holder's behalf.

Why it is needed

To send ERC-20 tokens, the holder has to submit the transaction themselves and hold ETH separately for gas. Users who only hold tokens cannot make a transfer at all. The approve and transferFrom allowance pattern used to work around this is vulnerable to multiple withdrawal attacks and encourages the bad practice of setting unlimited allowances. The spread of upgradeable contracts has made conditions ripe for such attacks to actually occur. The existing signature-based approval standard, EIP-2612, uses sequential nonces instead, so creating several transactions at once risks an app reusing a nonce that has not yet been processed, or a miner reordering submissions so some transactions fail. This problem gets worse when high gas fees leave transactions pending for a long time.

What to implement

In depth

The two transfer functions and their parameters

There are two core functions. transferWithAuthorization can be submitted by anyone who holds a valid signature. receiveWithAuthorization adds a check that the caller matches the recipient (to), which prevents front running where an attacker watching the transaction pool intercepts the signature and executes it first. Both functions take from (the payer and authorizer), to (the recipient), value (the amount), validAfter and validBefore (the start and expiry times as Unix timestamps), nonce (a unique nonce), and the EIP-712 signature's v, r, s. The reference implementation checks that the current time is greater than validAfter and less than validBefore, that the nonce is unused, and that the address recovered from the signature matches from, then marks the nonce used and executes the transfer.

Random 32-byte nonces instead of sequential nonces

The nonce is a random 32-byte value unique per authorizer address, and its usage status is checked via authorizationState(authorizer, nonce). The source explains that meta-transactions using sequential nonces have many problems. If a nonce submitted is too high, it is reverted immediately instead of waiting like a normal transaction, wasting gas, and if transactions submitted by different relayers get mined in a different order, a transaction can fail even with a correct nonce. Using multiple apps at once makes it impossible to know the next nonce without off-chain nonce tracking, and when high gas fees leave transactions pending for a long time, it becomes easy to accidentally reuse the same nonce. Random nonces remove these constraints and let you create as many transactions at once as you want.

Controlling timing with validAfter and validBefore

Leaving submission to a relayer means you cannot precisely control when a transaction enters the network. The validAfter and validBefore parameters let you schedule and expire an authorization so it is only valid after a specific time and only until a specific deadline, protecting users from unwanted outcomes that could arise from submission that is too early or too late. In the web3 example code, validAfter is set to 0 and validBefore is set to one hour from the current time, creating an authorization valid for only one hour.

EIP-712 signatures and replay prevention

The signature values v, r, s must be obtained using the EIP-712 typed message signing method. Because the domain separator is hashed with the token name, version, chainId, and verifyingContract (the token contract address), a signature meant for one token contract cannot be reused on a different chain or contract. Which fields go into the domain separator is up to the implementation, but it is strongly recommended to always include verifyingContract and chainId. Message types are distinguished by typehash constants, with TRANSFER_WITH_AUTHORIZATION_TYPEHASH, RECEIVE_WITH_AUTHORIZATION_TYPEHASH, and CANCEL_AUTHORIZATION_TYPEHASH defined. Ultimately, the private key signs a digest formed by concatenating 0x1901, the domain separator, and the hash of the parameters.

The optional authorization cancellation feature

A cancelAuthorization function that invalidates an unused authorization, along with an AuthorizationCanceled event, are defined as optional. Cancellation is also requested via an EIP-712 signature (v, r, s) of type CancelAuthorization containing the authorizer address and nonce. This is a mechanism for changing your mind before an authorization you have already signed and distributed gets executed.

Relationship to EIP-2612 and ERC-777

EIP-2612 already exists as a signature-based meta-transaction standard, and the source recommends implementing both for compatibility. There are two differences. EIP-2612 uses sequential nonces, while this standard uses random 32-byte nonces, and EIP-2612 relies on ERC-20's approve and transferFrom approval pattern, while this standard authorizes the transfer itself via signature. The source notes that alternative token standards such as ERC-777, which tried to fix the flaws of the approval pattern, were not widely adopted due to compatibility and potential security concerns.

Connecting existing contracts through a forwarder

To attach this standard to an existing contract (a parent contract) that uses the approve and transferFrom pattern, you can build a forwarding contract (forwarder). The forwarder extracts the user and deposit amount from the authorization, moves the user's funds to the forwarder via receiveWithAuthorization, grants a spending approval to the parent contract, calls the relevant function on the parent contract, and then returns the resulting tokens to the user. A function wrapping receiveWithAuthorization can also take all arguments as a single ABI-encoded bytes value to reduce the argument count, and the example attaches this to selector 0xef55bec6 for the call.

Proposal status

This proposal is currently in Review status. Keep in mind that it is not yet a finalized standard and its content may change.

Things to watch out for

Original ERC · ← ERCs