ERC-7604 · Draft · ERC · 2024-01-27
ERC-7604: ERC-1155 Permit Approvals
Brings ERC-2612-style gasless permit signatures to ERC-1155: an owner signs an EIP-712 message granting a spender an allowance for a specific token ID and amount, and anyone can submit it on-chain.
What problem does it solve?
To let a marketplace or other contract move your tokens, you normally first send an approval transaction, then the actual action. That costs gas twice and needs ETH in the wallet. ERC-2612 fixed this for ERC-20 with permit: you sign an off-chain message and the dapp submits the approval together with its own call. ERC-4494 did the same for ERC-721.
ERC-1155 had no equivalent. It is also harder, because an ERC-1155 balance is both a token ID and an amount, and core ERC-1155 only supports all-or-nothing operator approvals. ERC-7604 builds on ERC-5216, which adds per-ID, per-amount allowances to ERC-1155, and defines a permit for them.
How it works
Three functions are added:
function permit(address owner, address operator, uint256 tokenId, uint256 value, uint256 deadline, bytes memory sig) external;
function nonces(address owner, uint256 tokenId) external view returns (uint256);
function DOMAIN_SEPARATOR() external view returns (bytes32);
The owner signs an EIP-712 Permit(address owner,address spender,uint256 tokenId,uint256 value,uint256 nonce,uint256 deadline) message. A call to permit succeeds only if:
block.timestamp <= deadline,owneris not the zero address,- the signed
nonceequals the currentnonces(owner, tokenId), sigis a valid signature fromowner: a normal 65-byte ECDSA signature, a 64-byte ERC-2098 compact signature, or an ERC-1271 contract-wallet signature.
On success it sets the ERC-5216 allowance(owner, spender, tokenId) to value, bumps the nonce and emits ERC-5216's Approval event. Note that nonces are tracked per owner and per token ID. Anyone may call permit; msg.sender is not checked.
The signature is passed as bytes rather than v, r, s so that compact and contract signatures fit. Contracts advertise support via ERC-165 interface ID 0x7409106d.
Sample code
Signing a permit with ethers v6:
import { Contract, Wallet } from "ethers";
const owner = new Wallet(process.env.PRIVATE_KEY!, provider);
const token = new Contract(tokenAddress, [
"function nonces(address owner, uint256 tokenId) view returns (uint256)",
"function permit(address owner, address operator, uint256 tokenId, uint256 value, uint256 deadline, bytes sig)",
], owner);
const tokenId = 7n;
const value = 3n;
const nonce = await token.nonces(owner.address, tokenId);
const deadline = BigInt(Math.floor(Date.now() / 1000) + 3600);
const domain = { name: "MyItems", version: "1", chainId: 1n, verifyingContract: tokenAddress };
const types = {
Permit: [
{ name: "owner", type: "address" },
{ name: "spender", type: "address" },
{ name: "tokenId", type: "uint256" },
{ name: "value", type: "uint256" },
{ name: "nonce", type: "uint256" },
{ name: "deadline", type: "uint256" },
],
};
const sig = await owner.signTypedData(domain, types, {
owner: owner.address, spender: marketplace, tokenId, value, nonce, deadline,
});
// The marketplace (or a relayer) can now submit it:
// await token.permit(owner.address, marketplace, tokenId, value, deadline, sig);
The domain name and version must match whatever the token contract uses to build its DOMAIN_SEPARATOR.
Things to know
- Status is Draft, and it depends on ERC-5216, which is itself not yet Final.
- Signed permits can be front-run (someone else submits first; the outcome for the owner is the same) or withheld by the relayer. Use short deadlines.
- Setting
valueordeadlinetotype(uint256).maxcreates unlimited or non-expiring approvals; treat such signatures like infinite ERC-20 approvals. - If the domain separator is cached at deployment, a future chain split could allow cross-chain replay. Recompute it when
block.chainidchanges. - The usual approval race condition (changing a non-zero allowance) applies here too.