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