EIPs Explained

ERC-2612 · Final · ERC · 2020-04-13

ERC-2612: Permit Extension for EIP-20 Signed Approvals

Adds a permit() function to ERC-20 so a holder can grant an allowance by signing an EIP-712 message off-chain, saving a separate approve transaction.

What problem does it solve?

With plain ERC-20, using a token in a contract takes two transactions: approve from the holder, then the contract call that runs transferFrom. approve is tied to msg.sender, so the holder must send it themselves and must hold ETH for gas, even if they only want to spend tokens.

ERC-2612 adds one function, permit, that sets an allowance from a signature instead of msg.sender. The holder signs off-chain for free; anyone (the dApp, a relayer, the spender) submits it. Approve and use can then happen in a single transaction.

How it works

A compliant token adds three functions:

function permit(address owner, address spender, uint256 value, uint256 deadline,
                uint8 v, bytes32 r, bytes32 s) external;
function nonces(address owner) external view returns (uint256);
function DOMAIN_SEPARATOR() external view returns (bytes32);

permit sets allowance[owner][spender] = value, increments nonces[owner], and emits Approval, but only if:

  • block.timestamp <= deadline,
  • owner != address(0),
  • the signature is a valid secp256k1 signature by owner over the EIP-712 struct Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline) using the current nonce.

Otherwise it must revert. The domain separator binds the signature to this token contract and chain (commonly name, version, chainId, verifyingContract). The nonce stops replay.

Sample code

Signing a permit off-chain and using it on-chain (ethers v6):

import { Contract, Wallet, Signature, MaxUint256 } from "ethers";

const token = new Contract(tokenAddress, [
  "function name() view returns (string)",
  "function nonces(address) view returns (uint256)",
], provider);

const owner = new Wallet(process.env.PRIVATE_KEY!, provider);
const { chainId } = await provider.getNetwork();
const deadline = BigInt(Math.floor(Date.now() / 1000) + 3600); // 1 hour

const domain = { name: await token.name(), version: "1", chainId, verifyingContract: tokenAddress };
const types = {
  Permit: [
    { name: "owner", type: "address" },
    { name: "spender", type: "address" },
    { name: "value", type: "uint256" },
    { name: "nonce", type: "uint256" },
    { name: "deadline", type: "uint256" },
  ],
};
const message = { owner: owner.address, spender, value: 1000n, nonce: await token.nonces(owner.address), deadline };

const { v, r, s } = Signature.from(await owner.signTypedData(domain, types, message));
// Send (owner, spender, value, deadline, v, r, s) to the spender contract.

Spender side, approving and pulling in one call:

function depositWithPermit(uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external {
    IERC20Permit(token).permit(msg.sender, address(this), value, deadline, v, r, s);
    IERC20(token).transferFrom(msg.sender, address(this), value);
}

Things to know

  • The version string in the domain varies by token. Read it from the contract (many expose eip712Domain() per ERC-5267) rather than assuming "1".
  • Front-running: anyone who sees the signature can call permit first. The approval still happens, but your depositWithPermit would then revert. Robust integrations wrap permit in try/catch and check the allowance.
  • DAI's earlier permit uses a different signature (allowed bool and expiry), so it is not ERC-2612 compatible.
  • Permit signatures are a common phishing vector: signing one grants a spending allowance without any transaction. Wallets should display them clearly and users should read before signing.
  • If the domain separator is cached at deploy time, signatures could be replayed after a chain split. Recompute it when block.chainid changes.
  • Standard permit only supports EOA signers. Smart-contract wallets need ERC-1271-aware variants.

#token #erc20 #signatures #eip712 #gasless

Details

Authors
Martin Lundfall (@Mrchico)
Status
Final
Type
Standards Track / ERC
Created
2020-04-13
Requires
Required by

Machine-readable: Markdown · JSON