EIPs Explained

ERC-4494 · Stagnant · ERC · 2021-11-25

ERC-4494: Permit for ERC-721 NFTs

Brings ERC-2612-style gasless permit approvals to ERC-721 NFTs: the owner signs an EIP-712 message, and anyone can submit it to approve a spender for one token.

What problem does it solve?

To sell or deposit an NFT, the usual flow takes two transactions. First the owner calls approve. Then the marketplace or protocol calls transferFrom. That costs extra gas and confuses new users.

ERC-2612 fixed this for ERC-20 tokens with permit: the owner signs an off-chain message and the protocol submits the approval and the action together. ERC-4494 brings the same pattern to ERC-721, adapted to the fact that each NFT is a single item rather than an amount.

How it works

The NFT contract adds three functions:

Function Purpose
permit(spender, tokenId, deadline, sig) Approve spender on tokenId using the owner's signature
nonces(tokenId) Current nonce of this token, used for replay protection
DOMAIN_SEPARATOR() The EIP-712 domain the signature is bound to

The owner signs an EIP-712 typed message Permit(address spender,uint256 tokenId,uint256 nonce,uint256 deadline). permit succeeds only if all of these hold:

  • block.timestamp <= deadline
  • the token's owner is not the zero address
  • the signed nonce equals nonces(tokenId)
  • sig is a valid signature from the current owner. A normal 65-byte signature or a 64-byte EIP-2098 compact signature is accepted, which is why sig is bytes rather than v, r, s.

The key differences from ERC-2612:

  • The nonce belongs to the token, not to the owner. It must go up on every transfer of that tokenId. Once the NFT changes hands, any outstanding permits for it stop working.
  • An owner can sign several permits for the same NFT, because the approval is tied to the token's current ownership.

Contracts must report support through ERC-165 with interface ID 0x5604e225.

Sample code

Signing a permit off-chain (ethers v6):

import { Contract, Wallet, JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const owner = new Wallet(process.env.PRIVATE_KEY!, provider);
const nft = new Contract(nftAddress, [
  "function name() view returns (string)",
  "function nonces(uint256) view returns (uint256)",
  "function permit(address,uint256,uint256,bytes)",
], provider);

const { chainId } = await provider.getNetwork();
const domain = { name: await nft.name(), version: "1", chainId, verifyingContract: nftAddress };
const types = {
  Permit: [
    { name: "spender", type: "address" },
    { name: "tokenId", type: "uint256" },
    { name: "nonce", type: "uint256" },
    { name: "deadline", type: "uint256" },
  ],
};
const deadline = BigInt(Math.floor(Date.now() / 1000) + 3600);
const value = { spender, tokenId, nonce: await nft.nonces(tokenId), deadline };
const sig = await owner.signTypedData(domain, types, value);
// A relayer or the spender can now call nft.permit(spender, tokenId, deadline, sig)

The version string must match whatever the contract uses in its domain separator.

Things to know

  • Status: Stagnant. The interface is used in some projects, but the proposal was never finalized.
  • Uniswap v3's position NFTs have an earlier permit with a different design: the nonce is bumped when the permit is created, and the signature is split into v, r, s. It is not ERC-4494 compatible.
  • Like any permit, whoever holds the signature can choose to submit it or not. Use short deadlines. The same approval race as ERC-20 applies.
  • ecrecover returns the zero address on bad input. Implementations must reject a zero signer and a zero owner.
  • If you cache DOMAIN_SEPARATOR at deployment, recompute it when chainid changes, to avoid replay after a chain split.

#nft #erc-721 #permit #signatures #eip-712

Details

Authors
Simon Fremaux (@dievardump), William Schwab (@wschwab)
Status
Stagnant
Type
Standards Track / ERC
Created
2021-11-25
Requires

Machine-readable: Markdown · JSON