So I am trying to create the namehash() function in PHP for my own project and to also learn how to do it. But i am having issues.
This python function works as intended:
def namehash(name: str, encoding = 'utf-8'):
if name == '':
return b'\x00' * 32
else:
label, _, remainder = name.partition('.')
return sha3.keccak_256(namehash(remainder) + sha3.keccak_256(bytes(label, encoding = encoding)).digest()).digest()
namehash('') = 0x0000000000000000000000000000000000000000000000000000000000000000
namehash('eth') = 0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae
However this PHP function doesn not:
function namehash($_name){
$node = '';
for($i = 0; $i < 32; $i++){
$node .= '0';
}
if($_name){
$labels = explode('.', $_name);
for($i = count($labels) - 1; $i >= 0; $i--){
$labelSha = Keccak::hash($labels[$i], 256);
$node = Keccak::hash($node . $labelSha, 256);
}
}
return '0x' . $node;
}
0x00000000000000000000000000000000
0xc850e024ab508b89ab4833ddd18f253ddd83897cb813469cc62f68d359c5f314
I assume something is not encoded properly, but I am having a hard time finding what. Can anyone show me what the issue is?