Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article examines ERC-7597, which extends ERC-2612 to support ERC-1271 signature validation.

This article translates, summarizes, and supplements the material linked below.

I have also covered other EIPs.

Overview

The proposal extends ERC-2612 to allow smart contract wallets to authorize ERC-20 token allowances through signatures, enabling gas-sponsored approval and transfer flows.

See the following article for ERC-2612.

[ERC-2612] Combining ERC-20 Approvals and Transfers in One Transaction

See the following article for ERC-20.

https://chaldene.net/erc20

Motivation

ERC-2612 uses the signature parameters v, r, and s, with validation based on conventional EOA signatures.

As smart contract wallets become more widely used and adopt ERC-1271, applications need more flexible signature verification mechanisms that can invoke the wallet's own validation logic.

Accepting an opaque signature byte array allows wallets to use custom signature algorithms and schemes.

Unstructured signature bytes are treated as an arbitrary byte sequence rather than being restricted to a fixed signature layout. The verification implementation determines the supported formats and algorithms.

Specification

A conforming contract must implement the following permit function.

js
function permit(address owner, address spender, uint value, uint deadline, bytes memory signature) external
  • owner
    • Address of the account granting the allowance.
  • spender
    • Address receiving the allowance.
  • value
    • Token amount authorized.
  • deadline
    • Signature expiration timestamp.
  • signature
    • Signature data.

The contract must also implement the following functions defined by ERC-2612.

js
function nonces(address owner) external view returns (uint)
function DOMAIN_SEPARATOR() external view returns (bytes32)
  • nonces
    • Returns the current permit nonce for each owner.
  • DOMAIN_SEPARATOR
    • Returns the domain separator defined according to EIP-712.

See the following article for EIP-712.

[EIP-712] Hashing and Signing Typed Structured Data

A successful permit must satisfy the following conditions.

  • The current block timestamp is no greater than deadline.
    • An expired authorization is invalid.
  • The owner is not the zero address.
    • An authorization from 0x0 is invalid.
  • The signed nonce matches the current value of nonces[owner].
    • The nonce used in the signed message must match the contract's current permit nonce for the owner.
    • This is a contract-managed authorization nonce, distinct from an EOA's transaction nonce.
  • The signature is valid.
    • For an EOA owner, the signature must be a valid secp256k1 signature in abi.encodePacked(r, s, v) format.
    • For a smart contract owner, the signature is verified using the owner's ERC-1271 isValidSignature() function.

If the applicable conditions are not met, the permit call reverts.

Additional Notes

Replacing the fixed v, r, and s parameters with a signature byte array gives developers a common interface for validating signatures from both EOAs and smart contract wallets.

This accommodates signature schemes and algorithms appropriate to each wallet type, including conventional wallets such as MetaMask and contract-based wallets.

Compatibility

The proposal is compatible with existing ERC-2612 mechanisms. Contracts and applications using the conventional v, r, and s signature format can retain that interface.

Implementations wishing to support both formats can reduce duplication using the following approach.

js
function permit(
    address owner,
    address spender,
    uint256 value,
    uint256 deadline,
    uint8 v, 
    bytes32 r, 
    bytes32 s
) external {
    _permit(owner, spender, value, deadline, abi.encodePacked(r, s, v));
}

The example adapts the existing signature parameters to the new byte-array representation.

  1. Conventional signature parameters
    • The permit function accepts uint8 v, bytes32 r, bytes32 s.
    • It converts them to the byte-array representation.
    • Specifically, abi.encodePacked(r, s, v) concatenates the three values.
  2. Delegating to the internal function
    • The resulting byte array is passed to the internal _permit function.
    • This function implements the shared signature verification logic.
    • Both interfaces can therefore use a common implementation.

Internal Function _permit

The following example illustrates an internal function processing the signature byte array.

js
function _permit(
    address owner,
    address spender,
    uint256 value,
    uint256 deadline,
    bytes memory signature
) internal {
    require(block.timestamp <= deadline, "Permit: expired deadline");
    require(owner != address(0), "Permit: invalid owner");

    uint nonce = nonces[owner];
    bytes32 structHash = keccak256(
        abi.encode(
            keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
            owner,
            spender,
            value,
            nonce,
            deadline
        )
    );

    bytes32 hash = keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR, structHash));
    address signer = recoverSigner(hash, signature);

    if (isContract(owner)) {
        require(IERC1271(owner).isValidSignature(hash, signature) == 0x1626ba7e, "Permit: invalid signature from contract");
    } else {
        require(signer == owner, "Permit: invalid signature");
    }

    nonces[owner] = nonce + 1;
    allowance[owner][spender] = value;
    emit Approval(owner, spender, value);
}

function isContract(address account) internal view returns (bool) {
    uint32 size;
    assembly {
        size := extcodesize(account)
    }
    return size > 0;
}

function recoverSigner(bytes32 hash, bytes memory signature) internal pure returns (address) {
    bytes32 r;
    bytes32 s;
    uint8 v;

    if (signature.length != 65) {
        return address(0);
    }

    assembly {
        r := mload(add(signature, 0x20))
        s := mload(add(signature, 0x40))
        v := byte(0, mload(add(signature, 0x60)))
    }

    if (v < 27) {
        v += 27;
    }

    return ecrecover(hash, v, r, s);
}
  • _permit verifies the signature, sets the allowance when the requirements are met, and increments the permit nonce.
  • It applies the appropriate signature verification logic for an EOA or smart contract owner.

Implementation

js
/**
 * @notice Update allowance with a signed permit
 * @dev Signature bytes can be used for both EOA wallets and contract wallets.
 * @param owner       Token owner's address (Authorizer)
 * @param spender     Spender's address
 * @param value       Amount of allowance
 * @param deadline    The time at which the signature expires (unix time)
 * @param signature   Unstructured bytes signature signed by an EOA wallet or a contract wallet
 */
function permit(
    address owner,
    address spender,
    uint256 value,
    uint256 deadline,
    bytes memory signature
) external {
    require(deadline >= now, "Permit is expired");
    require(owner != address(0), "ERC20: approve from the zero address");
    require(spender != address(0), "ERC20: approve to the zero address");

    bytes32 digest = keccak256(abi.encodePacked(
        hex"1901",
        DOMAIN_SEPARATOR,
        keccak256(abi.encode(
            keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
            owner,
            spender,
            value,
            nonce,
            deadline
        ))
    ));
    
    require(
        // Check for both ECDSA signature and and ERC-1271 signature. A sample SignatureChecker is available at
        // https://github.com/OpenZeppelin/openzeppelin-contracts/blob/7bd2b2a/contracts/utils/cryptography/SignatureChecker.sol
        SignatureChecker.isValidSignatureNow(
            owner,
            typedDataHash,
            signature
        ),
        "Invalid signature"
    );
    
    allowed[owner][spender] = value;
    emit Approval(owner, spender, value);
}

Security

The security of contract-wallet permits depends on the implementation of the wallet's isValidSignature() function.

Correct Signature Verification

The wallet must correctly verify authorization according to its signature scheme. Signature formats, message hashes, algorithms, and the expected ERC-1271 success value must be handled correctly.

Replay Protection

Use a unique permit nonce for each authorization and enforce the expiration deadline. This prevents reuse of consumed signatures and limits the period during which unused authorizations remain valid.

Multisignature Support

If a wallet uses multiple signers, its validation logic must enforce the required signing threshold, signer eligibility, and any applicable signature ordering or uniqueness constraints.

Integrity of Signed Data

The signed message hash must commit to all required authorization fields. Implementations must ensure that no required field is omitted or altered.

Citation

Yvonne Zhang (@yvonnezhangc), Aloysius Chan (@circle-aloychan), "ERC-7597: Signature Validation Extension for Permit [DRAFT]," Ethereum Improvement Proposals, no. 7597, January 2024. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-7597.

Conclusion

ERC-7597 extends ERC-2612 to support ERC-1271-based signature verification for smart contract wallets.

If you have questions, feel free to contact me on Twitter.

Twitter @cardene777

I also publish articles on the following platforms.

https://chaldene.net/