2

I am trying to recreate the namehash algorithm from EIP-137.

After spending a lot of time trying to create my own algorithm, I tried to find a working python3 package online and couldn't find any.

I have been using the Python web3 api https://web3py.readthedocs.io/en/stable/web3.main.html

Right now, I can successfully get label hashes of names like 'vitalik' and 'eth' with the code below.

from web3 import Web3
w3 = Web3(Web3.HTTPProvider("my_provider"))

print(w3.keccak(text='eth').hex())
print(w3.keccak(text='vitalik').hex())

However, my implementation is failing when I get to the part where it says "Then, starting with the last component, the previous output is concatenated with the label hash and hashed again."

I have tried almost every way I can think of to do this concatenation. A few of the ways I have tried:

w3.keccak(hexstr=namehash(remainder) + w3.keccak(text=label))
w3.solidityKeccak(['address'], ["vitalik.eth"]).hex()
w3.solidityKeccak(['address', 'address'], ["vitalik", "eth"]).hex()

Nothing seems to give me the correct namehash.

Any help would be appreciated.

1 Answer 1

3

The following works for me. First I concatenate uint256(0) with keccak(eth) to make the .eth node. Then I concatenate this result with keccak(vitalik) to make vitalik.eth.

x0 = 0
x1 = w3.keccak(text='eth')
eth = w3.solidityKeccak(['uint256', 'bytes32'], [x0, x1])  # this is .eth

vitalik = w3.keccak(text='vitalik')

vitaliketh = w3.solidityKeccak(['bytes32', 'bytes32'], [eth, vitalik])  # this is vitalik.eth
1
  • 1
    Thank you for this! This code actually didn't work for me at first. I finally realized that my web3 environment was broken. If this happens to anyone else, I used a clean python virtual environment, following the steps here: web3py.readthedocs.io/en/stable/… After this, your code worked perfect. Commented Sep 14, 2022 at 17:19

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.