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.

ERC-6066 proposes a way to validate signatures associated with NFTs, including tokens that follow ERC-721 and ERC-1155.

This article summarizes and explains the original proposal.

Other ERC proposals are covered here.

Overview

For externally owned accounts (EOAs), Ethereum provides the ecrecover() function, which recovers the signing address from a suitable message digest and signature. Smart contracts can validate signatures using ERC-1271. As of the original article's publication, however, there was no common interface for validating signatures associated with individual NFTs.

For background on ERC-721, see:

https://chaldene.net/erc721

ERC-6066 proposes a standard way for anyone to verify the validity of a signature associated with an NFT. It adapts the signature validation interface of ERC-1271 by adding a token identifier to isValidSignature(tokenId, hash, data).

The parameters identify the following information.

  • tokenId
    • The identifier of the NFT associated with the signature.
  • hash
    • The hash of the signed data.
  • data
    • Optional arbitrary data that may help with validation, such as signature bytes.

The function lets a verifier ask a token contract whether it considers a signature valid for a given NFT. This can support applications in which NFTs represent more than collectible assets. The token itself does not hold a private key or independently generate a cryptographic signature.

For background on ERC-1271, see:

[ERC-1271] Signature Validation for Smart Contracts

Motivation

NFTs have become widely used to represent distinct digital items. Digital art and profile pictures have been prominent applications of ERC-721 and ERC-1155, but NFTs can represent other things as well.

For background on ERC-1155, see:

https://chaldene.net/erc1155

For example, an organization could represent offices or positions using NFTs. A decentralized autonomous organization (DAO) might issue badges representing executive roles such as CEO or COO. Quarterly democratic elections could result in a different person occupying a particular position.

If the outgoing COO signs contracts or approvals using a personal EOA, those signatures remain associated with the outgoing COO's account even after a successor takes office.

One option is to use a multisignature wallet for the entire DAO, but that may not provide sufficiently granular separation of responsibilities. Another is to appoint a smart contract to represent the COO, although this adds complexity.

A DAO that represents its organizational hierarchy with the Ethereum Name Service (ENS) could also use the proposal to support signatures associated with wrapped ENS subdomains, which are themselves NFTs.

This would allow signature validation to refer to an office or role rather than exclusively to the personal account of the person occupying it. Whether approvals survive a role transfer depends on the implementation's authorization and revocation rules.

Specification

javascript
pragma solidity ^0.8.0;

interface IERC6066 {
    /**
     * @dev MUST return if the signature provided is valid for the provided tokenId and hash
     * @param tokenId   Token ID of the signing NFT
     * @param hash      Hash of the data to be signed
     * @param data      OPTIONAL arbitrary data that may aid verification
     *
     * MUST return the bytes4 magic value 0x12edb34f when function passes.
     * MUST NOT modify state (using STATICCALL for solc < 0.5, view modifier for solc > 0.5)
     * MUST allow external calls
     *
     */
    function isValidSignature(
        uint256 tokenId,
        bytes32 hash,
        bytes calldata data
    ) external view returns (bytes4 magicValue);
}

IERC6066 defines an interface for validating signed data associated with an NFT. Its isValidSignature function is intended for contracts such as ERC-721 and ERC-1155 token contracts, allowing an implementation to associate approvals with individual tokens.

The function takes the following parameters.

  • tokenId
    • The ID of the signing NFT.
  • hash
    • The hash of the data to be signed.
  • data
    • Arbitrary additional data that may aid signature verification. Its contents are optional, although the argument is part of the ABI.

If the signature is valid for the supplied tokenId and hash, the function must return the four-byte magic value 0x12edb34f.

The function must not modify state and must allow external calls. The interface uses the view modifier to express its read-only behavior.

A contract implementing IERC6066 provides its own mechanism for deciding whether a signature associated with an NFT is valid. Applications that support NFT-based signatures call this method on the relevant token contract to validate a given token ID, rather than treating ownership of any ERC-721 or ERC-1155 token as proof of authorization.

Rationale

The proposal does not prescribe a single method of generating signatures. This leaves implementations free to choose an appropriate mechanism, much as ERC-1271 leaves signature generation to the implementing contract.

It also draws on the relatively simple contract-based approval mechanism used by Gnosis Safe.

If validation requires additional information, the bytes calldata data argument can carry it. Implementations that do not need additional information can ignore it. The proposal follows the extensibility approach described in ERC-5750.

For background on ERC-5750, see:

[ERC-5750] Extending Method Behavior with Additional Data Parameters

ERC-6066 leaves signature generation flexible. An implementation can use a simple approval mechanism inspired by Gnosis Safe, or use additional data for more involved verification. The common interface does not require all implementations to use the same mechanism.

Backwards Compatibility

The proposal's reference approach differs from ordinary cryptographic signature verification. An implementation can use an onchain Boolean approval flag to indicate whether the NFT has authorized a given hash, similar to an approval mechanism in Gnosis Safe.

A Boolean flag is one possible implementation, not a requirement of ERC-6066. The external validation interface still returns a four-byte magic value for a valid signature rather than a Boolean. Implementations remain free to verify cryptographic signatures or use other authorization mechanisms.

Reference Implementation

The following example implements ERC-6066-style signature validation in an ERC-721 contract.

javascript
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "./interfaces/IERC6066.sol";

contract ERC6066Reference is ERC721, IERC6066 {
    // type(IERC6066).interfaceId
    bytes4 public constant MAGICVALUE = 0x12edb34f;
    bytes4 public constant BADVALUE = 0xffffffff;

    mapping(uint256 => mapping(bytes32 => bool)) internal _signatures;

    error ENotTokenOwner();

    /**
     * @dev Checks if the sender owns NFT with ID tokenId
     * @param tokenId   Token ID of the signing NFT
     */
    modifier onlyTokenOwner(uint256 tokenId) {
        if (ownerOf(tokenId) != _msgSender()) revert ENotTokenOwner();
        _;
    }

    constructor(string memory name_, string memory symbol_)
        ERC721(name_, symbol_)
    {}

    /**
     * @dev SHOULD sign the provided hash with NFT of tokenId given sender owns said NFT
     * @param tokenId   Token ID of the signing NFT
     * @param hash      Hash of the data to be signed
     */
    function sign(uint256 tokenId, bytes32 hash)
        external
        onlyTokenOwner(tokenId)
    {
        _signatures[tokenId][hash] = true;
    }

    /**
     * @dev MUST return if the signature provided is valid for the provided tokenId, hash, and optionally data
     */
    function isValidSignature(uint256 tokenId, bytes32 hash, bytes calldata data)
        external
        view
        override
        returns (bytes4 magicValue)
    {
        // The data parameter is unused in this example
        return _signatures[tokenId][hash] ? MAGICVALUE : BADVALUE;
    }

    /**
     * @dev ERC-165 support
     */
    function supportsInterface(
        bytes4 interfaceId
    ) public view virtual override returns (bool) {
        return
            interfaceId == type(IERC6066).interfaceId ||
            super.supportsInterface(interfaceId);
    }
}

MAGICVALUE

javascript
bytes4 public constant MAGICVALUE = 0x12edb34f;

Overview

A fixed four-byte value indicating a valid signature.

Details

The isValidSignature function returns this value when validation succeeds, allowing callers to recognize a valid signature.


BADVALUE

javascript
bytes4 public constant BADVALUE = 0xffffffff;

Overview

A fixed four-byte value indicating an invalid signature in this implementation.

Details

The example returns this value when isValidSignature does not find a valid approval. It is the reference implementation's failure value; the required success value is the magic value above.


_signatures

javascript
mapping(uint256 => mapping(bytes32 => bool)) internal _signatures;

Overview

An internal nested mapping that tracks approvals associated with individual NFTs.

Details

The mapping uses the token ID and data hash as keys and stores a Boolean indicating whether the signature is approved.


ENotTokenOwner

javascript
error ENotTokenOwner();

Overview

A custom error for a caller that does not own the relevant token.

Details

The error is used when a caller attempts an operation restricted to the token owner.


onlyTokenOwner

javascript
modifier onlyTokenOwner(uint256 tokenId) {
    if (ownerOf(tokenId) != _msgSender()) revert ENotTokenOwner();
    _;
}

Overview

A modifier restricting a function to the owner of a specified token.

Details

Before executing the function body, the modifier checks ownership. If the caller does not own the token, it reverts with ENotTokenOwner.

Parameter

  • tokenId — the ID of the token whose ownership is checked.

sign

javascript
function sign(uint256 tokenId, bytes32 hash)
    external
    onlyTokenOwner(tokenId)
{
    _signatures[tokenId][hash] = true;
}

Overview

Records an approval of a data hash for the specified NFT.

Details

The function uses onlyTokenOwner to restrict calls to the token owner. It records approval by setting the corresponding entry of the internal _signatures mapping to true, using the token ID and hash as keys. It does not generate an offchain cryptographic signature.

Parameters

  • tokenId
    • The ID of the NFT associated with the approval.
  • hash
    • The hash of the data to approve.

isValidSignature

javascript
function isValidSignature(uint256 tokenId, bytes32 hash, bytes calldata data)
    external
    view
    override
    returns (bytes4 magicValue)
{
    // The data parameter is unused in this example
    return _signatures[tokenId][hash] ? MAGICVALUE : BADVALUE;
}

Overview

Determines whether a signature is valid for the supplied token ID and hash.

Details

The function reads _signatures to determine whether the given combination has been approved. It returns MAGICVALUE for a valid signature and BADVALUE otherwise. The example does not use the data argument.

Parameters

  • tokenId
    • The ID of the NFT whose signature is being validated.
  • hash
    • The hash of the signed data.
  • data
    • Additional data, unused in this example.

Return value

  • magicValue
    • A four-byte value indicating the validation result.

supportsInterface

javascript
function supportsInterface(
    bytes4 interfaceId
) public view virtual override returns (bool) {
    return
        interfaceId == type(IERC6066).interfaceId ||
        super.supportsInterface(interfaceId);
}

Overview

Determines whether the contract supports a particular interface.

Details

The function checks whether the supplied interfaceId matches the interface ID of IERC6066 or is supported by the parent contract's supportsInterface function.

Parameter

  • interfaceId
    • The interface identifier to check.

Return value

  • A Boolean indicating whether the interface is supported.

Security Considerations

The potentially revocable nature of contract-based signatures also applies to NFT-based signatures. Developers and users should take this into account.

The example above records approvals by token ID and hash, but does not implement revocation or reset approvals on transfer. An earlier owner's approval can therefore remain valid after the token changes hands. Applications should explicitly define the intended approval lifecycle and revalidate signatures when appropriate.

Citation

Jack Boyuan Xu (@boyuanx), "ERC-6066: Signature Validation Method for NFTs," Ethereum Improvement Proposals, no. 6066, November 2022. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-6066.

Conclusion

ERC-6066 adapts contract-based signature validation to individual NFTs such as ERC-721 and ERC-1155 tokens. By including a token ID in the validation interface, it allows applications to associate approvals with the asset or role that a token represents.

Questions are welcome through Twitter direct messages.

Twitter @cardene777

I also publish on the following platforms.

https://chaldene.net/