-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.js
32 lines (25 loc) · 635 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var sha3 = require('js-sha3').keccak_256
var uts46 = require('idna-uts46-hx')
function namehash (inputName) {
// Reject empty names:
var node = ''
for (var i = 0; i < 32; i++) {
node += '00'
}
var name = normalize(inputName)
if (name) {
var labels = name.split('.')
for(var i = labels.length - 1; i >= 0; i--) {
var labelSha = sha3(labels[i])
node = sha3(new Buffer(node + labelSha, 'hex'))
}
}
return '0x' + node
}
function normalize(name) {
return name ? uts46.toUnicode(name, {useStd3ASCII: true, transitional: false}) : name
}
module.exports = {
hash: namehash,
normalize
}