---
eip: 1052
label: EIP-1052
title: EXTCODEHASH opcode
status: Final
type: Standards Track
category: Core
created: 2018-05-02
requires: 161
official: https://eips.ethereum.org/EIPS/eip-1052
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1052.md
canonical: https://eips.huatuo.xyz/eip-1052/
lang: en
tldr: "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."
tags: [evm, opcode, constantinople, code, security]
---

# EIP-1052: EXTCODEHASH opcode

> This EIP specifies a new opcode, which returns the keccak256 hash of a contract's code.

**TL;DR:** 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](/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`:

```solidity
// 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:

```solidity
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](/eip-1884/) raised it to 700, and [EIP-2929](/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](/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](/eip-1014/) addresses, since both are derived deterministically from the code.


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-1052
