I've been practicing Ethernaut CTF and I'm stuck on the Delegation problem.

First, I tried to approach this in the console. But I couldn't get ownership.

var pwn = web3.utils.toHex("pwn()")
await contract.sendTransaction({data: pwn})

Second, I tried different approach. And finally, I got ownership.

var pwn_function = web3.utils.keccak256("pwn()")
await contract.sendTransaction({data: pwn_function})

By the way, What's the difference between the first approach and the second one? And I could find web3.utils.toHex in the web3.js document but couldn't find web3.utils.keccak256. What did I miss?

1 Answer 1


The functions toHex and keccak256 are very different things.

The first simply converts something (usually a number) to hexadecimal format. So it's basically encoding the data differently, but the data is still exactly the same - you can convert it back if you want to.

The second is calculating a hash value for the given data. You can't reverse that operation: you can't get the input from the output.

I don't know why the keccak256 function can't be found by searching in the web3js documentation (https://web3js.readthedocs.io/en/v1.8.0/search.html?q=keccak256&check_keywords=yes&area=default).

  • en.wikipedia.org/wiki/SHA-3
    – Ram
    Commented Oct 3, 2022 at 18:21
  • I know that the data field must have hexadecimal numbers, but aren't both toHex and keccak256 making hexadecimal numbers? By the way, why can I get ownership only for the value that came out as keccak256?
    – Shift_that
    Commented Oct 3, 2022 at 18:29
  • Hexadecimal format is just a way to represent data. Yes, those both happen to output their data in hex format. No idea about why you need to use keccak256, you can post a new question about that if you wish. Commented Oct 4, 2022 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.