EIPs Explained

ERC-7597 · Draft · ERC · 2024-01-15

ERC-7597: Signature Validation Extension for Permit

A draft extension of ERC-2612 permit that takes the signature as raw bytes and checks it with ERC-1271 when the owner is a contract, so smart contract wallets can also make gasless token approvals.

What problem does it solve?

ERC-2612 permit lets a token holder approve a spender by signing an off-chain message instead of sending an approve transaction. But its signature is passed as v, r, s and checked with ecrecover, which only works for externally owned accounts with a secp256k1 key.

Smart contract wallets (multisigs, passkey wallets, ERC-4337 accounts) do not have such a key. They prove signatures through ERC-1271 isValidSignature, which may use any scheme. So under plain ERC-2612 they cannot use permit at all. ERC-7597 adds a permit variant that works for both.

How it works

The token adds an overload that takes the signature as bytes:

function permit(address owner, address spender, uint256 value, uint256 deadline, bytes memory signature) external;

nonces(owner) and DOMAIN_SEPARATOR() stay exactly as in ERC-2612, and so does the signed EIP-712 Permit message. The call sets the allowance, increments the owner's nonce and emits Approval only if:

  • block.timestamp <= deadline,
  • owner is not the zero address,
  • the nonce in the signed message equals nonces[owner],
  • the signature is valid: for an EOA, a 65-byte abi.encodePacked(r, s, v) ECDSA signature; for a contract, owner.isValidSignature(digest, signature) returns the ERC-1271 magic value.

Otherwise it reverts. A token can keep the old v, r, s overload by packing those values into bytes and calling the same internal function, so existing integrations keep working.

Sample code

Token side, using OpenZeppelin's SignatureChecker (handles EOA and ERC-1271 in one call):

function permit(address owner, address spender, uint256 value, uint256 deadline, bytes memory signature) public {
    require(block.timestamp <= deadline, "expired");
    require(owner != address(0), "zero owner");
    bytes32 structHash = keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, _useNonce(owner), deadline));
    bytes32 digest = _hashTypedDataV4(structHash);
    require(SignatureChecker.isValidSignatureNow(owner, digest, signature), "bad signature");
    _approve(owner, spender, value);
}

Client side with ethers v6. For a contract wallet, the wallet's own signer produces the bytes:

import { Contract, Signature } from "ethers";

const token = new Contract(tokenAddr, [
  "function nonces(address) view returns (uint256)",
  "function name() view returns (string)",
  "function permit(address,address,uint256,uint256,bytes)",
], signer);

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

const sig = await signer.signTypedData(domain, types, msg);  // EOA: 65 bytes r||s||v
await token["permit(address,address,uint256,uint256,bytes)"](owner, spender, value, deadline, sig);

Things to know

  • Status: Draft.
  • The EIP-712 domain version differs between tokens; read it from the token (for example via ERC-5267 eip712Domain() where supported) instead of assuming "1".
  • For contract wallets, security depends entirely on the wallet's isValidSignature logic.
  • ERC-1271 only works for wallets already deployed. Counterfactual (not yet deployed) wallets need ERC-6492, which this proposal does not cover.
  • The spec's reference snippet is illustrative and omits nonce handling; base real code on an audited permit implementation.

#token #permit #signatures #smart-wallets #erc20

Details

Authors
Yvonne Zhang (@yvonnezhangc), Aloysius Chan (@circle-aloychan)
Status
Draft
Type
Standards Track / ERC
Created
2024-01-15
Requires

Machine-readable: Markdown · JSON