Go to main content
Development/Blockchain

What is ERC-2612

by Nyangbari 2024. 5. 2.

ERC-2612 = ERC20 + EIP-712 signature

No need to send a transaction to approve a spender; just include a value in your signature to grant approval!

 

ERC-2612 introduces a function called "permit", which uses an EIP-712 signature as an input to update the allowance mapping.

The signature must include the following elements:

  1. owner: The address of the token owner who signed the message.
  2. spender: The address of the spender contract that will transfer tokens on behalf of the owner.
  3. value: The maximum amount of tokens that the spender can transfer.
  4. nonce: A unique number to prevent replay attacks.
  5. deadline: A timestamp after which the signature becomes invalid.
  6. v, r, s: Components of the EIP-712 signature that prove the owner’s consent.
// Permit signature format
struct Permit {
    address owner;
    address spender;
    uint256 value;
    uint256 nonce;
    uint256 deadline;
}

// This can also be found in our StETHPermit.sol
function permit(
    address owner,
    address spender,
    uint256 value,
    uint256 deadline,
    uint8 v,
    bytes32 r,
    bytes32 s
) external;

The contract checks the validity of the signature and matches these parameters.

 

Before updating the allowance mapping, the contract verifies two main things:

 

1. Signature Validity

  • It hashes the parameters using EIP-712.
  • It recovers the signer’s address from the signature using ecrecover.
  • It compares the recovered address with the owner’s address provided in the parameters.
  • If they match, it confirms that the owner has signed the message and authorized the spender. If they do not match, it rejects the transaction as an unauthorized attempt.

 

2. Parameter Validity

  • It checks whether the deadline has not passed, whether the value does not exceed the owner’s balance, and whether the nonce is correct.
  • If all conditions are met, the approval is considered valid and executed; otherwise, it is rejected.
  • Once these checks are completed, the spender is authorized to transfer the specified amount of tokens on behalf of the owner. This mechanism allows the owner to authorize a spender without sending a transaction and without incurring gas fees. Owners simply sign with their private key and send it to the spender contract.

 

 

 

Reference:

https://medium.com/frak-defi/erc-2612-the-ultimate-guide-to-gasless-erc-20-approvals-2cd32ddee534

 

ERC-2612: The Ultimate Guide to Gasless ERC-20 Approvals

How to use EIP-712 signatures to save gas, batch approvals and increase security for your ERC-20 tokens

medium.com