Skip to content

Instantly share code, notes, and snippets.

@rafaelcaricio
Created June 18, 2017 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelcaricio/f5002c6d9779cd598ef3594af73ffe2f to your computer and use it in GitHub Desktop.
Save rafaelcaricio/f5002c6d9779cd598ef3594af73ffe2f to your computer and use it in GitHub Desktop.
Namehash Algorithm from EIP137 - Ethereum Name Service
import codecs
# pip install pysha3==1.0.2
from sha3 import keccak_256
def sha3(text):
return keccak_256(text).digest()
def namehash(name):
if name == '':
return b'\x00' * 32
label, _, remainder = name.partition('.')
return sha3(namehash(remainder) + sha3(label.encode()))
def hex(c):
return '0x' + codecs.encode(c, 'hex').decode()
if __name__ == '__main__':
print(hex(namehash(''))) # 0x0000000000000000000000000000000000000000000000000000000000000000
print(hex(namehash('eth'))) # 0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae
print(hex(namehash('foo.eth'))) # 0xde9b09fd7c5f901e23a3f19fecc54828e9c848539801e86591bd9801b019f84f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment