0

using Solidity in a smart contract, how is the namehash computed from name ("foo.eth") ?

conS
  • 31
  • 4
  • This is the exact method ENS uses in Solidity: https://github.com/ensdomains/ens-contracts/blob/master/contracts/wrapper/BytesUtil.sol – enriquejr99 Oct 11 '21 at 17:54

1 Answers1

2

formal specifications on https://eips.ethereum.org/EIPS/eip-137

function computeNamehash(string _name) public pure returns (bytes32 namehash) {
  namehash = 0x0000000000000000000000000000000000000000000000000000000000000000;
  namehash = keccak256(
  abi.encodePacked(namehash, keccak256(abi.encodePacked('eth')))
  );
  namehash = keccak256(
  abi.encodePacked(namehash, keccak256(abi.encodePacked(_name)))
  );
}
conS
  • 31
  • 4
  • It's almost certainly best to calculate this off-chain and pass in a namehash instead. But if you must take this approach, do not forget the normalization step from the EIP: "Each label must be a valid normalised label as described in UTS46 with the options transitional=false and useSTD3AsciiRules=true. For Javascript implementations, a library is available that normalises and checks names." – carver Jul 17 '18 at 23:23
  • The question is how to calculate it on-state. Feel free to write an answer to the question. – conS Jul 18 '18 at 10:48