Try using buffers

master
Dan Finlay 8 years ago
parent e6ad659a6c
commit 5427f53196
No known key found for this signature in database
GPG Key ID: 931102F24B36007A

@ -1,9 +1,22 @@
// const sha3 = require('js-sha3').sha3_256 // const sha3 = require('js-sha3').sha3_256
const sha3 = require('js-sha3').shake_256 const sha3 = require('js-sha3').keccak_256
const uts46 = require('idna-uts46') const uts46 = require('idna-uts46')
// const SHA3 = require('crypto-js/sha3') // const SHA3 = require('crypto-js/sha3')
module.exports = function namehash (inputName) { module.exports = function namehash (inputName) {
let node = new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
if(inputName && inputName !== '') {
let name = normalize(inputName)
var labels = name.split('.')
for(var i = labels.length - 1; i >= 0; i--) {
const combined = new Buffer([node, sha3(labels[i])])
node = sha3(combined)
}
}
return '0x' + node.toString('hex')
console.log('called with ' + inputName) console.log('called with ' + inputName)
// Reject empty names: // Reject empty names:
@ -15,7 +28,7 @@ module.exports = function namehash (inputName) {
return result return result
} }
const name = normalize(inputName) name = normalize(inputName)
const split = name.split('.') const split = name.split('.')
const label = split.shift() const label = split.shift()
@ -35,5 +48,5 @@ function sha3 (input) {
*/ */
function normalize(name) { function normalize(name) {
return uts46.toUnicode(name, {useStd3ASCII: true, transitional: false}); return uts46.toUnicode(name, {useStd3ASCII: true, transitional: false})
} }

@ -7,15 +7,22 @@ const namehash = require('../')
test('empty name', (t) => { test('empty name', (t) => {
t.plan(1) t.plan(1)
const input = '' const input = ''
const expected = '0000000000000000000000000000000000000000000000000000000000000000' const expected = '0x0000000000000000000000000000000000000000000000000000000000000000'
const output = namehash(input) const output = namehash(input)
t.equal(output, expected) t.equal(output, expected)
}) })
test('empty param', (t) => {
t.plan(1)
const expected = '0x0000000000000000000000000000000000000000000000000000000000000000'
const output = namehash()
t.equal(output, expected)
})
test('TLD eth', (t) => { test('TLD eth', (t) => {
t.plan(1) t.plan(1)
const input = 'eth' const input = 'eth'
const expected = '93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae' const expected = '0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae'
const output = namehash(input) const output = namehash(input)
t.equal(output, expected) t.equal(output, expected)
}) })
@ -23,7 +30,7 @@ test('TLD eth', (t) => {
test('foo.eth', (t) => { test('foo.eth', (t) => {
t.plan(1) t.plan(1)
const input = 'foo.eth' const input = 'foo.eth'
const expected = 'de9b09fd7c5f901e23a3f19fecc54828e9c848539801e86591bd9801b019f84f' const expected = '0xde9b09fd7c5f901e23a3f19fecc54828e9c848539801e86591bd9801b019f84f'
const output = namehash(input) const output = namehash(input)
t.equal(output, expected) t.equal(output, expected)
}) })

Loading…
Cancel
Save