EIPs Explained

EIP-7761 · Stagnant · Core · 2024-09-01

EIP-7761: EXTCODETYPE instruction

Adds an EXTCODETYPE opcode to EOF that reports whether an address has no code, legacy code or EOF code, restoring the EOA check that ERC-721/1155 safe transfers rely on.

What problem does it solve?

EOF (EIP-3540, bundled in EIP-7692) deliberately removes code introspection: EXTCODESIZE, EXTCODECOPY and friends are not available to EOF contracts. That breaks two real needs:

  • NFT safe transfers. ERC-721 and ERC-1155 safeTransfer succeeds directly for EOAs but must call onERC721Received / onERC1155Received on contracts. Implementations decide which path to take with EXTCODESIZE.
  • Proxy safety. An EOF proxy cannot EXTDELEGATECALL into legacy code. If it were upgraded to point at a legacy implementation, it could brick itself. It needs to check the target's code type first.

EIP-7761 adds one narrow introspection opcode that covers both cases.

How it works

EXTCODETYPE (opcode 0xe9) pops an address and pushes:

Value Meaning
0 (TYPE_NONE) No code: an EOA, empty account, or a contract still being constructed
1 (TYPE_LEGACY_CONTRACT) Legacy EVM bytecode
2 (TYPE_EOF_CONTRACT) Code starting with 0xef0001 (EOFv1)

Gas follows the usual account-access pricing: 100 warm, 2,600 cold (EIP-2929 rules). An address with any of the high 12 bytes set causes an exceptional halt.

If the account carries an EIP-7702 delegation designator (0xef0100…), the instruction follows the delegation and reports the type of the delegated code, charging the delegation access cost. This differs from EXTCODEHASH/EXTCODECOPY, which describe the designator itself; here the goal is to describe what will actually execute.

Sample code

Pseudocode for the instruction:

def extcodetype(evm):
    charge(GAS_WARM_ACCESS)                       # 100
    addr = evm.stack.pop()
    if addr >> 160:
        raise ExceptionalHalt
    if addr not in evm.accessed_addresses:
        charge(GAS_COLD_ACCOUNT_ACCESS - GAS_WARM_ACCESS)
        evm.accessed_addresses.add(addr)
    code = get_code(addr)
    if not code:
        return evm.stack.push(0)                  # TYPE_NONE
    if code.startswith(b"\xef\x01\x00"):          # EIP-7702 delegation
        code = load_delegated_code(code)          # charges delegation access
    if code.startswith(b"\xef\x00\x01"):
        return evm.stack.push(2)                  # TYPE_EOF_CONTRACT
    evm.stack.push(1)                             # TYPE_LEGACY_CONTRACT

How an EOF NFT contract would use it (conceptual):

if EXTCODETYPE(to) != 0: require onERC721Received(...) == magic

Things to know

  • Status: Stagnant. It depends on EOF, which has not shipped on Ethereum mainnet.
  • It is a deliberate exception to EOF's "no code introspection" principle. Alternatives considered included new status codes or flags on EXT*CALL, or simply re-enabling EXTCODESIZE.
  • Checking "is this a contract?" is a weak signal: during construction a contract has no code yet, and with EIP-7702 an EOA can have code. Don't use it as a security boundary.
  • Introduced without bumping the EOF version, because 0xe9 was previously rejected by EOF validation.

#evm #eof #opcode #core

Details

Authors
Andrei Maiboroda (@gumb0), Piotr Dobaczewski (@pdobacz), Danno Ferrin (@shemnon)
Status
Stagnant
Type
Standards Track / Core
Created
2024-09-01
Requires
Required by

Machine-readable: Markdown · JSON