EIPs Explained

EIP-1052 · Final · Core · 2018-05-02

EIP-1052: EXTCODEHASH opcode

Added the EXTCODEHASH opcode (0x3f), which returns the keccak256 hash of another account's code cheaply, so contracts can check code identity without copying the bytecode.

What problem does it solve?

Contracts often want to know what code another address runs, not the code itself. A factory or registry might allow only known implementations. A security tool might approve a contract once its exact bytecode has been audited.

Before this EIP, the only way was EXTCODECOPY: copy the whole bytecode into memory and hash it. For large contracts that cost a lot of gas. EIP-1052 lets the client return the hash it already stores for every account.

How it works

  • New opcode EXTCODEHASH at 0x3f. It takes one stack item and uses only its lowest 160 bits as an address (the top 96 bits are ignored, like BALANCE and EXTCODESIZE).
  • It pushes:
    • keccak256(code) for an account that has code,
    • 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 (the hash of empty data) for an account that exists but has no code,
    • 0 if the account does not exist or is empty as defined by EIP-161 (no code, zero nonce, zero balance).
  • The original gas cost was 400, the same as BALANCE, because the work is the same account lookup.

Because "no account" and "account without code" give different results, EXTCODEHASH can also tell whether an account exists.

Sample code

Solidity exposes it as address.codehash:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract ImplementationAllowlist {
    bytes32 private constant EMPTY = keccak256("");
    mapping(bytes32 => bool) public approved;

    function approve(address impl) external /* onlyOwner */ {
        approved[impl.codehash] = true;
    }

    function isApproved(address target) public view returns (bool) {
        bytes32 h = target.codehash;               // EXTCODEHASH
        return h != bytes32(0) && h != EMPTY && approved[h];
    }
}

Or in assembly:

assembly { h := extcodehash(target) }

Things to know

  • This proposal is Final. It went live in the Constantinople upgrade.
  • The gas cost changed later. EIP-1884 raised it to 700, and EIP-2929 replaced it with cold and warm access pricing.
  • A contract's code hash is empty during its own constructor, because the code is only stored when the constructor ends. A "has code" check can therefore be bypassed by calling from a constructor. Do not use it to block contracts from calling you.
  • With EIP-7702, an EOA can carry a delegation designator. EXTCODEHASH returns the hash of that designator and does not follow it to the delegate's code.
  • Code hashes pair well with CREATE2 addresses, since both are derived deterministically from the code.

#evm #opcode #constantinople #code #security

Details

Authors
Nick Johnson <[email protected]>, Paweł Bylica <[email protected]>
Status
Final
Type
Standards Track / Core
Created
2018-05-02
Requires
Required by

Machine-readable: Markdown · JSON