ethereum-ens
Advanced tools
Comparing version
50
index.js
@@ -15,8 +15,8 @@ /* | ||
var CryptoJS = require('crypto-js'); | ||
var namehash = require('eth-ens-namehash') | ||
var pako = require('pako'); | ||
var Promise = require('bluebird'); | ||
var sha3 = require('js-sha3').keccak_256 | ||
var textEncoding = require('text-encoding'); | ||
var TextDecoder = textEncoding.TextDecoder; | ||
var uts46 = require('idna-uts46'); | ||
var _ = require('underscore'); | ||
@@ -267,2 +267,4 @@ | ||
"3": "0x112234455c3a32fd11230c42e7bccd4a84e02010", | ||
// Rinkeby | ||
"4": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A", | ||
} | ||
@@ -422,40 +424,8 @@ | ||
function sha3(input) { | ||
return CryptoJS.SHA3(input, {outputLength: 256}) | ||
} | ||
/** | ||
* normalise namepreps a name, throwing an exception if it contains invalid characters. | ||
* @param {string} name The name to normalise | ||
* @returns The normalised name. Throws ENS.InvalidName if the name contains invalid characters. | ||
*/ | ||
function normalise(name) { | ||
return uts46.toUnicode(name, {useStd3ASCII: true, transitional: false}); | ||
} | ||
ENS.normalise = normalise; | ||
/** | ||
* namehash implements ENS' name hash algorithm. | ||
* @param {string} name The name to hash | ||
* @returns The computed namehash, as a hex string. | ||
*/ | ||
function namehash(name) { | ||
name = normalise(name); | ||
var node = CryptoJS.enc.Hex.parse('0000000000000000000000000000000000000000000000000000000000000000'); | ||
if(name && name != '') { | ||
var labels = name.split("."); | ||
for(var i = labels.length - 1; i >= 0; i--) { | ||
node = sha3(node.concat(sha3(labels[i]))); | ||
} | ||
} | ||
return '0x' + node.toString(); | ||
} | ||
ENS.namehash = namehash; | ||
function parentNamehash(name) { | ||
var dot = name.indexOf('.'); | ||
if(dot == -1) { | ||
return ['0x' + sha3(normalise(name)), namehash('')]; | ||
return ['0x' + sha3(namehash.normalize(name)), namehash.hash('')]; | ||
} else { | ||
return ['0x' + sha3(normalise(name.slice(0, dot))), namehash(name.slice(dot + 1))]; | ||
return ['0x' + sha3(namehash.normalize(name.slice(0, dot))), namehash.hash(name.slice(dot + 1))]; | ||
} | ||
@@ -480,3 +450,3 @@ } | ||
abi = abi || resolverInterface; | ||
var node = namehash(name); | ||
var node = namehash.hash(name); | ||
return new Resolver(this, node, this.web3.eth.contract(abi)); | ||
@@ -515,3 +485,3 @@ }; | ||
ENS.prototype.setResolver = function(name, addr, params) { | ||
var node = namehash(name); | ||
var node = namehash.hash(name); | ||
@@ -529,3 +499,3 @@ return this.registryPromise.then(function(registry) { | ||
ENS.prototype.owner = function(name, callback) { | ||
var node = namehash(name); | ||
var node = namehash.hash(name); | ||
@@ -547,3 +517,3 @@ return this.registryPromise.then(function(registry) { | ||
ENS.prototype.setOwner = function(name, addr, params) { | ||
var node = namehash(name); | ||
var node = namehash.hash(name); | ||
@@ -550,0 +520,0 @@ return this.registryPromise.then(function(registry) { |
{ | ||
"name": "ethereum-ens", | ||
"version": "0.6.1", | ||
"version": "0.7.1", | ||
"homepage": "https://github.com/Arachnid/ensjs#readme", | ||
@@ -29,4 +29,4 @@ "description": "Javascript bindings for the Ethereum Name Service", | ||
"bluebird": "^3.4.7", | ||
"crypto-js": "^3.1.6", | ||
"idna-uts46": "^1.0.1", | ||
"eth-ens-namehash": "^2.0.0", | ||
"js-sha3": "^0.5.7", | ||
"pako": "^1.0.4", | ||
@@ -33,0 +33,0 @@ "text-encoding": "^0.6.4", |
@@ -8,6 +8,11 @@ <!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
- `ens` | ||
- `address` | ||
- `node` | ||
- `abi` | ||
- `contract` | ||
## resolverAddress | ||
resolverAddress returns the address of the resolver. | ||
Returns **any** A promise for the address of the resolver. | ||
## reverseAddr | ||
@@ -17,8 +22,4 @@ | ||
**Parameters** | ||
Returns **any** A promise for the Resolver for the reverse record. | ||
- `callback` An optional callback, for asynchronous operation. | ||
Returns **any** In synchronous operation, the Resolver for the reverse record. | ||
## abi | ||
@@ -31,5 +32,6 @@ | ||
- `callback` An optional callback, for asynchronous operation. | ||
- `Optional.null` **bool** If false, do not look up the ABI on the reverse entry. | ||
- `reverse` | ||
Returns **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** In synchronous operation, the contract ABI. | ||
Returns **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** A promise for the contract ABI. | ||
@@ -41,10 +43,6 @@ ## contract | ||
reverse record if that's not found. Returns null if no address is specified or no ABI | ||
was found. | ||
was found. The returned contract object will not be promisifed or otherwise modified. | ||
**Parameters** | ||
Returns **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** A promise for the contract instance. | ||
- `callback` An optional callback, for asynchronous operation. | ||
Returns **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** In synchronous operation, the contract instance. | ||
# ENS | ||
@@ -62,9 +60,13 @@ | ||
var address = ens.resolver('foo.eth').addr(); | ||
var address = ens.resolver('foo.eth').addr().then(function(addr) { ... }); | ||
Throughout this module, the same optionally-asynchronous pattern as web3 is | ||
used: all functions that call web3 take a callback as an optional last | ||
argument; if supplied, the function returns nothing, but instead calls the | ||
callback with (err, result) when the operation completes. | ||
Functions that require communicating with the node return promises, rather than | ||
using callbacks. A promise has a `then` function, which takes a callback and will | ||
call it when the promise is fulfilled; `then` returns another promise, so you can | ||
chain callbacks. For more details, see <http://bluebirdjs.com/>. | ||
Notably, the `resolver` method returns a resolver instance immediately; lookup of | ||
the resolver address is done in the background or when you first call an asynchronous | ||
method on the resolver. | ||
Functions that create transactions also take an optional 'options' argument; | ||
@@ -76,3 +78,3 @@ this has the same parameters as web3. | ||
- `web3` **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** A web3 instance to use to communicate with the blockchain. | ||
- `address` **address** The address of the ENS registry. Defaults to the public ENS registry if not supplied. | ||
- `address` **address** Optional. The address of the ENS registry. Defaults to the public ENS registry. | ||
@@ -91,3 +93,4 @@ **Meta** | ||
supplied. So, to call the `addr(node)` function on a standard resolver, | ||
you only have to call `addr()`. | ||
you only have to call `addr()`. Returned objects are also 'promisified' - they | ||
return a Bluebird Promise object instead of taking a callback. | ||
@@ -100,6 +103,4 @@ **Parameters** | ||
`setName` and `setAddr` is supplied. | ||
- `callback` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Optional. If specified, the function executes | ||
asynchronously. | ||
Returns **any** The resolver object if callback is not supplied. | ||
Returns **any** The resolver object. | ||
@@ -113,3 +114,4 @@ ## reverse | ||
supplied. So, to call the `addr(node)` function on a standard resolver, | ||
you only have to call `addr()`. | ||
you only have to call `addr()`. Returned objects are also 'promisified' - they | ||
return a Bluebird Promise object instead of taking a callback. | ||
@@ -122,6 +124,4 @@ **Parameters** | ||
`setName` and `setAddr` is supplied. | ||
- `callback` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Optional. If specified, the function executes | ||
asynchronously. | ||
Returns **any** The resolver object if callback is not supplied. | ||
Returns **any** The resolver object. | ||
@@ -139,7 +139,6 @@ ## setResolver | ||
- `options` **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** An optional dict of parameters to pass to web3. | ||
- `callback` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** An optional callback; if specified, the | ||
function executes asynchronously. | ||
- `addr` | ||
- `params` | ||
Returns **any** The transaction ID if callback is not supplied. | ||
Returns **any** A promise that returns the transaction ID when the transaction is mined. | ||
@@ -153,6 +152,5 @@ ## owner | ||
- `name` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The name to look up. | ||
- `callback` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** An optional callback; if specified, the | ||
function executes asynchronously. | ||
- `callback` | ||
Returns **any** The resolved address if callback is not supplied. | ||
Returns **any** A promise returning the owner address of the specified name. | ||
@@ -170,7 +168,6 @@ ## setOwner | ||
- `options` **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** An optional dict of parameters to pass to web3. | ||
- `callback` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** An optional callback; if specified, the | ||
function executes asynchronously. | ||
- `addr` | ||
- `params` | ||
Returns **any** The transaction ID if callback is not supplied. | ||
Returns **any** A promise returning the transaction ID of the transaction, once mined. | ||
@@ -189,26 +186,5 @@ ## setSubnodeOwner | ||
- `options` **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** An optional dict of parameters to pass to web3. | ||
- `callback` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** An optional callback; if specified, the | ||
function executes asynchronously. | ||
- `addr` | ||
- `params` | ||
Returns **any** The transaction ID if callback is not supplied. | ||
# normalise | ||
normalise namepreps a name, throwing an exception if it contains invalid characters. | ||
**Parameters** | ||
- `name` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The name to normalise | ||
Returns **any** The normalised name. Throws ENS.InvalidName if the name contains invalid characters. | ||
# namehash | ||
namehash implements ENS' name hash algorithm. | ||
**Parameters** | ||
- `name` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The name to hash | ||
Returns **any** The computed namehash, as a hex string. | ||
Returns **any** A promise returning the transaction ID of the transaction, once mined. |
@@ -55,23 +55,2 @@ var ENS = require('../index.js'); | ||
describe('#namehash()', function() { | ||
it('should produce valid hashes', function() { | ||
assert.equal(ENS.namehash(''), '0x0000000000000000000000000000000000000000000000000000000000000000'); | ||
assert.equal(ENS.namehash('eth'), '0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae'); | ||
assert.equal(ENS.namehash('foo.eth'), '0xde9b09fd7c5f901e23a3f19fecc54828e9c848539801e86591bd9801b019f84f'); | ||
}); | ||
it('should canonicalize with nameprep', function() { | ||
assert.equal(ENS.namehash('name.eth'), ENS.namehash('NAME.eth')); | ||
}); | ||
it('should prohibit invalid names', function() { | ||
try { | ||
ENS.normalise('foo_!bar'); | ||
assert.fail("Expected exception"); | ||
} catch(e) { | ||
assert.equal('Error: Illegal char _', e); | ||
} | ||
}); | ||
}) | ||
describe('#resolve()', function() { | ||
@@ -78,0 +57,0 @@ it('should get resolver addresses', function(done) { |
38904
-6.86%669
-6.3%177
-11.94%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed