From 679a7ef7ce19f65cfc52dc3ef9fe54cc64fa696a Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 7 Mar 2017 17:09:17 -0800 Subject: [PATCH] One bad state --- index.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 5 +++++ test/index.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 index.js create mode 100644 test/index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..c0eb375 --- /dev/null +++ b/index.js @@ -0,0 +1,46 @@ +// const sha3 = require('js-sha3').sha3_256 +const sha3 = require('js-sha3').shake_256 +const uts46 = require('idna-uts46') +// const SHA3 = require('crypto-js/sha3') + +module.exports = function namehash (inputName) { + console.log('called with ' + inputName) + + // Reject empty names: + if (!inputName || inputName === '') { + let result = '' + for (let i = 0; i < 32; i++) { + result += '00' + } + return result + } + + const name = normalize(inputName) + const split = name.split('.') + + const label = toBuffer(split.shift()) + const remainder = toBuffer(split.join('.')) + + console.log('finishing with ', typeof sha3(label)) + + console.log(sha3(label)) + + const result = sha3(namehash(remainder) + sha3(label)) + console.dir(result) + throw new Error('stop') + return result +} + +/* +function sha3 (input) { + return SHA3(input, { outputLength: 256 }) +} +*/ + +function normalize(name) { + return uts46.toUnicode(name, {useStd3ASCII: true, transitional: false}); +} + +function toBuffer(str) { + return new Buffer(str, 'utf8') +} diff --git a/package.json b/package.json index c29c029..8fee026 100644 --- a/package.json +++ b/package.json @@ -22,5 +22,10 @@ "homepage": "https://github.com/flyswatter/eth-ens-namehash#readme", "devDependencies": { "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 new file mode 100644 index 0000000..2767aad --- /dev/null +++ b/test/index.js @@ -0,0 +1,30 @@ +const test = require('tape') +const namehash = require('../') + +// Test results specified in original ENS Proposal: +// https://github.com/ethereum/EIPs/issues/137 + +test('empty name', (t) => { + t.plan(1) + const input = '' + const expected = '0000000000000000000000000000000000000000000000000000000000000000' + const output = namehash(input) + t.equal(output, expected) +}) + +test('TLD eth', (t) => { + t.plan(1) + const input = 'eth' + const expected = '93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae' + const output = namehash(input) + t.equal(output, expected) +}) + +test('foo.eth', (t) => { + t.plan(1) + const input = 'foo.eth' + const expected = 'de9b09fd7c5f901e23a3f19fecc54828e9c848539801e86591bd9801b019f84f' + const output = namehash(input) + t.equal(output, expected) +}) +