39 lines
1.2 KiB
JavaScript
39 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
var convert = require('./convert');
|
|
var utf8 = require('./utf8');
|
|
var keccak256 = require('./keccak256');
|
|
|
|
var Zeros = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
var Partition = new RegExp("^((.*)\\.)?([^.]+)$");
|
|
var UseSTD3ASCIIRules = new RegExp("^[a-z0-9.-]*$");
|
|
|
|
function namehash(name, depth) {
|
|
name = name.toLowerCase();
|
|
|
|
// Supporting the full UTF-8 space requires additional (and large)
|
|
// libraries, so for now we simply do not support them.
|
|
// It should be fairly easy in the future to support systems with
|
|
// String.normalize, but that is future work.
|
|
if (!name.match(UseSTD3ASCIIRules)) {
|
|
throw new Error('contains invalid UseSTD3ASCIIRules characters');
|
|
}
|
|
|
|
var result = Zeros;
|
|
var processed = 0;
|
|
while (name.length && (!depth || processed < depth)) {
|
|
var partition = name.match(Partition);
|
|
var label = utf8.toUtf8Bytes(partition[3]);
|
|
result = keccak256(convert.concat([result, keccak256(label)]));
|
|
|
|
name = partition[2] || '';
|
|
|
|
processed++;
|
|
}
|
|
|
|
return convert.hexlify(result);
|
|
}
|
|
|
|
module.exports = namehash;
|
|
|