Skip to content

Instantly share code, notes, and snippets.

@rjchow
Last active June 17, 2022 15:42
Show Gist options
  • Save rjchow/9c87d2dcc068451ff710a65fb8b32f76 to your computer and use it in GitHub Desktop.
Save rjchow/9c87d2dcc068451ff710a65fb8b32f76 to your computer and use it in GitHub Desktop.
Howto extcodehash

Extcodehash EIP-1052

EIP-1052 Introduces the EXTCODEHASH EVM opcode which takes an address as a parameter and returns the hash of the bytecode located at that address.

Full details on the opcode

Solidity (asm) contract for calling extcodehash

Solidity contract with asm call to extcodehash

contract ContractHash {
    function getContractHash(address a) public view returns (bytes32 hash) {
        assembly {
            hash := extcodehash(a)
        }
    }
}

Procedure for generating bytecode for manual deployment

docker run -v $(pwd)/contracts:/tmp/contracts ethereum/solc:stable --bin --abi --evm-version constantinople /tmp/contracts/ContractHash.sol

Compiler output

======= /tmp/contracts/ContractHash.sol:ContractHash =======
Binary: 
608060405234801561001057600080fd5b5060b98061001f6000396000f3fe608060405260043610601c5760003560e01c8063f5441a74146021575b600080fd5b348015602c57600080fd5b50606c60048036036020811015604157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506082565b6040518082815260200191505060405180910390f35b6000813f905091905056fea165627a7a72305820416e7621097e451b973693d88c35992144d2e2aaa974751c620fdfbe0c4e14990029
Contract JSON ABI 
[{"constant":true,"inputs":[{"name":"a","type":"address"}],"name":"getContractHash","outputs":[{"name":"hash","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"}]

If you're too lazy to deploy it yourself

There is an instance on Ropsten at 0x1B6841A7B559eB1c35F5Bb78e7da119fe8D11121

Generating hash of contract using web3.utils

let addressOfContract = _INSERT_CONTRACT_ADDRESS_HERE_

let Web3 = require('web3')
let web3 = new Web3('https://ropsten.infura.io')


let contractBytecode = web3.eth.getCode(addressOfContract)

web3.utils.keccak256(contractBytecode)
@gavortex
Copy link

{
"linkReferences": {},
"object": "THINGS YOU NEEDED HERE", copy only this part from remix to generate your hash
"opcodes": "-",
"sourceMap": "-"
}
keccak256 hash

Go to https://emn178.github.io/online-tools/keccak_256.html, paste in the content, and make sure you select Input Type as Hex, then you will get the init code hash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment