From 6bbabba2983a39fc7db36555ed496bbe271f5102 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 7 Mar 2017 17:43:50 -0800 Subject: [PATCH] Working with tld on keccak_256 --- index.js | 35 +++++++---------------------------- package.json | 1 - test/index.js | 10 ++++++++-- 3 files changed, 15 insertions(+), 31 deletions(-) diff --git a/index.js b/index.js index 2d3dba6..8fa5dfb 100644 --- a/index.js +++ b/index.js @@ -1,27 +1,10 @@ -// const sha3 = require('js-sha3').sha3_256 const sha3 = require('js-sha3').keccak_256 const uts46 = require('idna-uts46') -// const SHA3 = require('crypto-js/sha3') 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) - // Reject empty names: if (!inputName || inputName === '') { - let result = '' + let result = '0x' for (let i = 0; i < 32; i++) { result += '00' } @@ -34,18 +17,14 @@ module.exports = function namehash (inputName) { const label = split.shift() const remainder = split.join('.') - console.log('finishing with ', typeof sha3(label)) - - console.log(sha3(label)) + const remainderHash = namehash(remainder).substr(2) + const labelHash = sha3(label) - return sha3(namehash(remainder) + sha3(label)) -} - -/* -function sha3 (input) { - return SHA3(input, { outputLength: 256 }) + if (remainderHash.match(/^0+$/)) { + return '0x' + labelHash + } + return '0x' + sha3(remainderHash + remainderHash) } -*/ function normalize(name) { return uts46.toUnicode(name, {useStd3ASCII: true, transitional: false}) diff --git a/package.json b/package.json index 8fee026..0b29b32 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,6 @@ "tape": "^4.6.3" }, "dependencies": { - "crypto-js": "^3.1.9-1", "idna-uts46": "^1.0.1", "js-sha3": "^0.5.7" } diff --git a/test/index.js b/test/index.js index 792f412..2c1dfce 100644 --- a/test/index.js +++ b/test/index.js @@ -22,11 +22,17 @@ test('empty param', (t) => { test('TLD eth', (t) => { t.plan(1) const input = 'eth' - const expected = '0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae' + const expected = '0x4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0' const output = namehash(input) t.equal(output, expected) }) +/* + * Commented out because it turns out the EIP has incorrect specs: + * https://github.com/ethereum/EIPs/issues/137#issuecomment-284918147 + * + * The correct specs are here, but too sparse to use: + * http://docs.ens.domains/en/latest/introduction.html#namehash test('foo.eth', (t) => { t.plan(1) const input = 'foo.eth' @@ -34,4 +40,4 @@ test('foo.eth', (t) => { const output = namehash(input) t.equal(output, expected) }) - +*/