EIPs Explained

ERC-3009 · Draft · ERC · 2020-09-28

ERC-3009: Transfer With Authorization

Lets an ERC-20 holder sign a one-off transfer authorization off-chain that anyone can submit, using random nonces and a validity window, enabling gasless and atomic token payments without allowances.

What problem does it solve?

To move ERC-20 tokens, a holder normally sends a transaction and therefore needs ETH for gas. Letting a contract move tokens on your behalf requires an approve first, which adds a transaction and often leads to risky "infinite" allowances.

ERC-2612 permit solved part of this with a signed approval, but it uses sequential nonces. If a user signs nonce 5 and nonce 6 in two different apps, or a relayer lands them in the wrong order, one of them fails. It also still relies on the allowance pattern.

ERC-3009 lets the holder sign the transfer itself. A relayer, the recipient or a contract submits it and pays the gas. Each authorization has a random 32-byte nonce, so users can create many in parallel with no ordering issues.

How it works

The holder signs an EIP-712 typed message:

TransferWithAuthorization(address from,address to,uint256 value,
                          uint256 validAfter,uint256 validBefore,bytes32 nonce)

The token exposes:

Function Purpose
transferWithAuthorization(from, to, value, validAfter, validBefore, nonce, v, r, s) Anyone may submit; moves value from from to to
receiveWithAuthorization(...) (same args) Same, but requires msg.sender == to; use this from contracts
authorizationState(authorizer, nonce) Whether a nonce has been used
cancelAuthorization(authorizer, nonce, v, r, s) Optional: burn a nonce before it is used

The contract checks that validAfter < now < validBefore, that the nonce is unused for from, and that the signature recovers to from. It then marks the nonce used, emits AuthorizationUsed(authorizer, nonce) and performs the transfer.

The validity window protects users when a relayer controls submission timing. The EIP-712 domain (including chainId and the token address) prevents replay on other chains or tokens.

Sample code

Signing an authorization and submitting it as a relayer (ethers v6):

import { Wallet, Contract, hexlify, randomBytes, Signature, parseUnits } from "ethers";

const domain = { name: "USD Coin", version: "2", chainId, verifyingContract: tokenAddress };
const types = {
  TransferWithAuthorization: [
    { name: "from", type: "address" },
    { name: "to", type: "address" },
    { name: "value", type: "uint256" },
    { name: "validAfter", type: "uint256" },
    { name: "validBefore", type: "uint256" },
    { name: "nonce", type: "bytes32" },
  ],
};
const message = {
  from: payer.address,
  to: merchant,
  value: parseUnits("10", 6),
  validAfter: 0,
  validBefore: Math.floor(Date.now() / 1000) + 3600, // 1 hour
  nonce: hexlify(randomBytes(32)),
};

const sig = Signature.from(await payer.signTypedData(domain, types, message)); // off-chain

// Relayer pays the gas:
const token = new Contract(tokenAddress, [
  "function transferWithAuthorization(address,address,uint256,uint256,uint256,bytes32,uint8,bytes32,bytes32)",
], relayer);
await token.transferWithAuthorization(
  message.from, message.to, message.value, message.validAfter,
  message.validBefore, message.nonce, sig.v, sig.r, sig.s,
);

Domain name and version must match what the token contract uses; read them from the contract rather than hard-coding.

Things to know

  • Status is Draft, although the interface has been implemented in production tokens, most prominently USDC.
  • Front-running: if a contract calls transferWithAuthorization, an observer can copy the signature from the mempool and submit it directly, so the transfer happens without your contract's logic running. Contracts should use receiveWithAuthorization, which only the payee can call.
  • It is designed for externally owned accounts. Contract accounts can just call transfer directly.
  • Implementations must reject a zero-address result from ecrecover; malformed signatures recover to address(0).
  • Tokens are encouraged to implement both ERC-3009 and ERC-2612 for maximum compatibility.

#token #erc20 #meta-transactions #gasless #eip712 #payments

Details

Authors
Peter Jihoon Kim (@petejkim), Kevin Britz (@kbrizzle), David Knott (@DavidLKnott)
Status
Draft
Type
Standards Track / ERC
Created
2020-09-28
Requires
Required by

Machine-readable: Markdown · JSON