Added namehash to utils.

This commit is contained in:
ricmoo 2017-05-03 20:24:07 -04:00
parent 169347d0c4
commit b528131937
3 changed files with 42 additions and 1 deletions

View File

@ -8,6 +8,7 @@ var bigNumber = require('./bignumber');
var contractAddress = require('./contract-address');
var convert = require('./convert');
var keccak256 = require('./keccak256');
var namehash = require('./namehash');
var sha256 = require('./sha2').sha256;
var randomBytes = require('./random-bytes');
var properties = require('./properties');
@ -40,6 +41,8 @@ module.exports = {
toUtf8Bytes: utf8.toUtf8Bytes,
toUtf8String: utf8.toUtf8String,
namehash: namehash,
getAddress: address.getAddress,
getContractAddress: contractAddress.getContractAddress,

38
utils/namehash.js Normal file
View File

@ -0,0 +1,38 @@
'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;

View File

@ -1,6 +1,6 @@
{
"name": "ethers-utils",
"version": "2.0.3",
"version": "2.0.4",
"description": "Utilities for the Ethers Ethereum library.",
"bugs": {
"url": "http://github.com/ethers-io/ethers.js/issues",