Skip to content

Commit b528131

Browse files
committed
Added namehash to utils.
1 parent 169347d commit b528131

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

utils/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var bigNumber = require('./bignumber');
88
var contractAddress = require('./contract-address');
99
var convert = require('./convert');
1010
var keccak256 = require('./keccak256');
11+
var namehash = require('./namehash');
1112
var sha256 = require('./sha2').sha256;
1213
var randomBytes = require('./random-bytes');
1314
var properties = require('./properties');
@@ -40,6 +41,8 @@ module.exports = {
4041
toUtf8Bytes: utf8.toUtf8Bytes,
4142
toUtf8String: utf8.toUtf8String,
4243

44+
namehash: namehash,
45+
4346
getAddress: address.getAddress,
4447
getContractAddress: contractAddress.getContractAddress,
4548

utils/namehash.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
var convert = require('./convert');
4+
var utf8 = require('./utf8');
5+
var keccak256 = require('./keccak256');
6+
7+
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];
8+
var Partition = new RegExp("^((.*)\\.)?([^.]+)$");
9+
var UseSTD3ASCIIRules = new RegExp("^[a-z0-9.-]*$");
10+
11+
function namehash(name, depth) {
12+
name = name.toLowerCase();
13+
14+
// Supporting the full UTF-8 space requires additional (and large)
15+
// libraries, so for now we simply do not support them.
16+
// It should be fairly easy in the future to support systems with
17+
// String.normalize, but that is future work.
18+
if (!name.match(UseSTD3ASCIIRules)) {
19+
throw new Error('contains invalid UseSTD3ASCIIRules characters');
20+
}
21+
22+
var result = Zeros;
23+
var processed = 0;
24+
while (name.length && (!depth || processed < depth)) {
25+
var partition = name.match(Partition);
26+
var label = utf8.toUtf8Bytes(partition[3]);
27+
result = keccak256(convert.concat([result, keccak256(label)]));
28+
29+
name = partition[2] || '';
30+
31+
processed++;
32+
}
33+
34+
return convert.hexlify(result);
35+
}
36+
37+
module.exports = namehash;
38+

utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ethers-utils",
3-
"version": "2.0.3",
3+
"version": "2.0.4",
44
"description": "Utilities for the Ethers Ethereum library.",
55
"bugs": {
66
"url": "http://github.com/ethers-io/ethers.js/issues",

0 commit comments

Comments
 (0)