You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
745 B
JavaScript

const sha3 = require('js-sha3').keccak_256
8 years ago
const uts46 = require('idna-uts46')
module.exports = function namehash (inputName) {
// Reject empty names:
if (!inputName || inputName === '') {
let result = '0x'
8 years ago
for (let i = 0; i < 32; i++) {
result += '00'
}
return result
}
name = normalize(inputName)
8 years ago
const split = name.split('.')
const label = split.shift()
const remainder = split.join('.')
8 years ago
const remainderHash = namehash(remainder).substr(2)
const labelHash = sha3(label)
8 years ago
if (remainderHash.match(/^0+$/)) {
return '0x' + labelHash
}
return '0x' + sha3(remainderHash + remainderHash)
8 years ago
}
function normalize(name) {
return uts46.toUnicode(name, {useStd3ASCII: true, transitional: false})
8 years ago
}