2017-05-04 03:24:07 +03:00
|
|
|
'use strict';
|
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
import { concat, hexlify } from './convert';
|
|
|
|
import { toUtf8Bytes } from './utf8';
|
|
|
|
import { keccak256 } from './keccak256';
|
2017-05-04 03:24:07 +03:00
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
var Zeros = new Uint8Array([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]);
|
2017-05-04 03:24:07 +03:00
|
|
|
var Partition = new RegExp("^((.*)\\.)?([^.]+)$");
|
|
|
|
var UseSTD3ASCIIRules = new RegExp("^[a-z0-9.-]*$");
|
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
export function namehash(name: string): string {
|
2017-05-04 03:24:07 +03:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
var result: string | Uint8Array = Zeros;
|
|
|
|
while (name.length) {
|
2017-05-04 03:24:07 +03:00
|
|
|
var partition = name.match(Partition);
|
2018-06-13 22:39:39 +03:00
|
|
|
var label = toUtf8Bytes(partition[3]);
|
|
|
|
result = keccak256(concat([result, keccak256(label)]));
|
2017-05-04 03:24:07 +03:00
|
|
|
|
|
|
|
name = partition[2] || '';
|
|
|
|
}
|
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
return hexlify(result);
|
2017-05-04 03:24:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|