forked from ensdomains/ensjs-v2
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
578 lines (526 loc) · 21.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
This file is part of ethereum-ens.
ethereum-ens is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethereum-ens is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ethereum-ens. If not, see <http://www.gnu.org/licenses/>.
*/
var namehash = require('eth-ens-namehash')
var pako = require('pako');
var Promise = require('bluebird');
var textEncoding = require('text-encoding');
var TextDecoder = textEncoding.TextDecoder;
var _ = require('underscore');
var Web3 = require('web3');
var utils = require('./src/utils.js');
var abi = require('./src/abi.js');
var registryInterface = abi.registryInterface;
var resolverInterface = abi.resolverInterface;
var registryAddresses = {
// Mainnet
"1": "0x314159265dd8dbb310642f98f50c066173c1259b",
// Ropsten
"3": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
// Rinkeby
"4": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A",
};
var abiDecoders = {
1: function(data) {
data = new TextDecoder("utf-8").decode(data);
return JSON.parse(data);
},
2: function(data) {
data = pako.inflate(data, {to: 'string'});
return JSON.parse(data);
}
};
var supportedDecoders = _.reduce(_.keys(abiDecoders), function(memo, val) { return memo | val; });
/**
* Wrapper function that returns a version of ENS that is compatible
* with the provided version of Web3
*/
function ENS (provider, address, Web3js) {
if (Web3js !== undefined) {
Web3 = Web3js;
}
if (!!/^0\./.exec(Web3.version || (new Web3()).version.api)) {
return utils.construct(ENS_0, [provider, address]);
} else {
return utils.construct(ENS_1, [provider, address]);
}
}
ENS.NameNotFound = Error("ENS name not found");
/**
* @class
*/
function Resolver_1(ens, node, contract) {
this.ens = ens;
this.node = node;
this.instancePromise = ens.registryPromise.then(function(registry) {
return registry.methods.resolver(node).call().then(function(address) {
if(address == "0x0000000000000000000000000000000000000000") {
return Promise.reject(ENS.NameNotFound);
}
contract.options.address = address;
return contract;
});
});
_.each(contract.methods, function(method, signature) {
this[signature] = function() {
var args = Array.prototype.slice.call(arguments);
var params;
return this.instancePromise.then(function(instance) {
// Check abi interface for constant methods
var asyncObj = instance._jsonInterface.reduce(function(acc, curr) {
if (acc) {
return acc;
} else {
return curr.name === signature && !curr.constant ? curr : null;
}
}, null)
// Call if it's a constant method, send (with params) if it's not
if (asyncObj === null) {
return _.partial(instance.methods[signature], node).apply(instance.methods, args).call();
} else {
if (asyncObj.inputs.length < args.length + 1) {
params = args.splice(args.length - 1)[0];
}
return ens.web3.eth.getAccounts().then(function(accounts) {
return _.partial(instance.methods[signature], node).apply(instance.methods, args).send(params ? params : {from: accounts[0]});
})
}
}).bind(this);
}.bind(this);
}.bind(this));
}
/**
* resolverAddress returns the address of the resolver.
* @returns A promise for the address of the resolver.
*/
Resolver_1.prototype.resolverAddress = function() {
return this.instancePromise.then(function(instance) {
return instance._address;
});
}
/**
* reverseAddr looks up the reverse record for the address returned by the resolver's addr()
* @returns A promise for the Resolver for the reverse record.
*/
Resolver_1.prototype.reverseAddr = function() {
return this.addr().then(function(addr) {
return this.ens.reverse(addr);
}).bind(this);
}
/**
* abi returns the ABI associated with the name. Automatically looks for an ABI on the
* reverse record if none is found on the name itself.
* @param {bool} Optional. If false, do not look up the ABI on the reverse entry.
* @returns {object} A promise for the contract ABI.
*/
Resolver_1.prototype.abi = function(reverse) {
return this.instancePromise.then(function(instance) {
return instance.methods.ABI(this.node, supportedDecoders).call().then(function(result) {;
if(result[0] == 0) {
if(reverse == false) return null;
return this.reverseAddr().then(function(reverse) {
return reverse.abi(false);
});
} else {
return abiDecoders[result[0]](utils.fromHex(result[1]));
}
}.bind(this));
}.bind(this));
};
/**
* contract returns a web3 contract object. The address is that returned by this resolver's
* `addr()`, and the ABI is loaded from this resolver's `ABI()` method, or the ABI on the
* reverse record if that's not found. Returns null if no address is specified or no ABI
* was found. The returned contract object will not be promisifed or otherwise modified.
* @returns {object} A promise for the contract instance.
*/
Resolver_1.prototype.contract = function() {
return Promise.join(this.abi(), this.addr(), function(abi, addr) {
return new this.ens.web3.eth.Contract(abi, addr);
}.bind(this));
};
/**
* @class
*
* @description Provides an easy-to-use interface to the Ethereum Name Service.
*
* Example usage:
*
* var ENS = require('ethereum-ens');
* var Web3 = require('web3');
*
* var web3 = new Web3();
* var ens = new ENS(web3);
*
* var address = ens.resolver('foo.eth').addr().then(function(addr) { ... });
*
* 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;
* this has the same parameters as web3.
*
* @author Nick Johnson <[email protected]>
* @date 2016
* @license LGPL
*
* @param {object} provider A web3 provider to use to communicate with the blockchain.
* @param {address} address Optional. The address of the ENS registry. Defaults to the public ENS registry.
*/
function ENS_1(provider, address) {
// Ensures backwards compatibility
if (provider.currentProvider) {
provider = provider.currentProvider;
}
this.web3 = new Web3(provider);
var registryContract = new this.web3.eth.Contract(registryInterface);
if (address != undefined) {
registryContract.options.address = address;
this.registryPromise = Promise.resolve(registryContract);
} else {
this.registryPromise = new Promise((resolve) => {
this.web3.eth.net.getId().then(function(version) {
registryContract.options.address = registryAddresses[version];
resolve(registryContract);
});
})
}
}
ENS_1.NameNotFound = Error("ENS name not found");
/**
* resolver returns a resolver object for the specified name, throwing
* ENS.NameNotFound if the name does not exist in ENS.
* Resolver objects are wrappers around web3 contract objects, with the
* first argument - always the node ID in an ENS resolver - automatically
* supplied. So, to call the `addr(node)` function on a standard resolver,
* you only have to call `addr()`. Returned objects are also 'promisified' - they
* return a Bluebird Promise object instead of taking a callback.
* @param {string} name The name to look up.
* @param {list} abi Optional. The JSON ABI definition to use for the resolver.
* if none is supplied, a default definition implementing `has`, `addr`, `name`,
* `setName` and `setAddr` is supplied.
* @returns The resolver object.
*/
ENS_1.prototype.resolver = function(name, abi) {
abi = abi || resolverInterface;
var node = namehash.hash(name);
return new Resolver_1(this, node, new this.web3.eth.Contract(abi));
};
/**
* reverse returns a resolver object for the reverse resolution of the specified address,
* throwing ENS.NameNotFound if the reverse record does not exist in ENS.
* Resolver objects are wrappers around web3 contract objects, with the
* first argument - always the node ID in an ENS resolver - automatically
* supplied. So, to call the `addr(node)` function on a standard resolver,
* you only have to call `addr()`. Returned objects are also 'promisified' - they
* return a Bluebird Promise object instead of taking a callback.
* @param {string} address The address to look up.
* @param {list} abi Optional. The JSON ABI definition to use for the resolver.
* if none is supplied, a default definition implementing `has`, `addr`, `name`,
* `setName` and `setAddr` is supplied.
* @returns The resolver object.
*/
ENS_1.prototype.reverse = function(address, abi) {
if(address.startsWith("0x"))
address = address.slice(2);
return this.resolver(address.toLowerCase() + ".addr.reverse", abi);
};
/**
* setResolver sets the address of the resolver contract for the specified name.
* The calling account must be the owner of the name in order for this call to
* succeed.
* @param {string} name The name to update
* @param {address} address The address of the resolver
* @param {object} options An optional dict of parameters to pass to web3.
* @returns A promise that returns the transaction ID when the transaction is mined.
*/
ENS_1.prototype.setResolver = function(name, addr, params) {
var node = namehash.hash(name);
return this.registryPromise.then(function(registry) {
return this.web3.eth.getAccounts().then(function(accounts) {
return registry.methods.setResolver(node, addr).send(params ? params : {from: accounts[0]});
});
}.bind(this));
}
/**
* owner returns the address of the owner of the specified name.
* @param {string} name The name to look up.
* @returns A promise returning the owner address of the specified name.
*/
ENS_1.prototype.owner = function(name, callback) {
var node = namehash.hash(name);
return this.registryPromise.then(function(registry) {
return registry.methods.owner(node).call();
});
}
/**
* setOwner sets the owner of the specified name. Only the owner may call
* setResolver or setSubnodeOwner. The calling account must be the current
* owner of the name in order for this call to succeed.
* @param {string} name The name to update
* @param {address} address The address of the new owner
* @param {object} options An optional dict of parameters to pass to web3.
* @returns A promise returning the transaction ID of the transaction, once mined.
*/
ENS_1.prototype.setOwner = function(name, addr, params) {
var node = namehash.hash(name);
return this.registryPromise.then(function(registry) {
return this.web3.eth.getAccounts().then(function(accounts) {
return registry.methods.setOwner(node, addr).send(params ? params : {from: accounts[0]});
});
}.bind(this));
}
/**
* setSubnodeOwner sets the owner of the specified name. The calling account
* must be the owner of the parent name in order for this call to succeed -
* for example, to call setSubnodeOwner on 'foo.bar.eth', the caller must be
* the owner of 'bar.eth'.
* @param {string} name The name to update
* @param {address} address The address of the new owner
* @param {object} options An optional dict of parameters to pass to web3.
* @returns A promise returning the transaction ID of the transaction, once mined.
*/
ENS_1.prototype.setSubnodeOwner = function(name, addr, params) {
var node = utils.parentNamehash(name);
return this.registryPromise.then(function(registry) {
return this.web3.eth.getAccounts().then(function(accounts) {
return registry.methods.setSubnodeOwner(node[1], node[0], addr).send(params ? params : {from: accounts[0]});
});
}.bind(this));
}
// TODO: ADD A BOUNDARY
/**
* @class
*/
function Resolver_0(ens, node, contract) {
this.ens = ens;
this.node = node;
this.instancePromise = ens.registryPromise.then(function(registry) {
return registry.resolverAsync(node).then(function(address) {
if(address == "0x0000000000000000000000000000000000000000") {
return Promise.reject(ENS.NameNotFound);
}
return Promise.promisifyAll(contract.at(address));
});
});
_.each(contract.abi, function(signature) {
this[signature.name] = function() {
var args = arguments;
return this.instancePromise.then(function(instance) {
return _.partial(instance[signature.name + 'Async'], node).apply(instance, args);
}).bind(this);
}.bind(this);
}.bind(this));
}
/**
* resolverAddress returns the address of the resolver.
* @returns A promise for the address of the resolver.
*/
Resolver_0.prototype.resolverAddress = function() {
return this.instancePromise.then(function(instance) {
return instance.address;
});
}
/**
* reverseAddr looks up the reverse record for the address returned by the resolver's addr()
* @returns A promise for the Resolver for the reverse record.
*/
Resolver_0.prototype.reverseAddr = function() {
return this.addr().then(function(addr) {
return this.ens.reverse(addr);
}).bind(this);
}
/**
* abi returns the ABI associated with the name. Automatically looks for an ABI on the
* reverse record if none is found on the name itself.
* @param {bool} Optional. If false, do not look up the ABI on the reverse entry.
* @returns {object} A promise for the contract ABI.
*/
Resolver_0.prototype.abi = function(reverse) {
return this.instancePromise.then(function(instance) {
return instance.ABIAsync(this.node, supportedDecoders).then(function(result) {
if(result[0] == 0) {
if(reverse == false) return null;
return this.reverseAddr().then(function(reverse) {
return reverse.abi(false);
});
} else {
return abiDecoders[result[0]](utils.fromHex(result[1]));
}
}.bind(this));
}.bind(this));
};
/**
* contract returns a web3 contract object. The address is that returned by this resolver's
* `addr()`, and the ABI is loaded from this resolver's `ABI()` method, or the ABI on the
* reverse record if that's not found. Returns null if no address is specified or no ABI
* was found. The returned contract object will not be promisifed or otherwise modified.
* @returns {object} A promise for the contract instance.
*/
Resolver_0.prototype.contract = function() {
return Promise.join(this.abi(), this.addr(), function(abi, addr) {
return this.ens.web3.eth.contract(abi).at(addr);
}.bind(this));
};
/**
* @class
*
* @description Provides an easy-to-use interface to the Ethereum Name Service.
*
* Example usage:
*
* var ENS = require('ethereum-ens');
* var Web3 = require('web3');
*
* var web3 = new Web3();
* var ens = new ENS(web3);
*
* var address = ens.resolver('foo.eth').addr().then(function(addr) { ... });
*
* 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;
* this has the same parameters as web3.
*
* @author Nick Johnson <[email protected]>
* @date 2016
* @license LGPL
*
* @param {object} provider A web3 provider to use to communicate with the blockchain.
* @param {address} address Optional. The address of the ENS registry. Defaults to the public ENS registry.
*/
function ENS_0(provider, address) {
// Ensures backwards compatibility
if (provider.currentProvider) {
provider = provider.currentProvider;
}
this.web3 = new Web3(provider);
var registryContract = this.web3.eth.contract(registryInterface);
if(address != undefined) {
this.registryPromise = Promise.resolve(Promise.promisifyAll(registryContract.at(address)));
} else {
this.registryPromise = Promise.promisify(this.web3.version.getNetwork)().then(function(version) {
return Promise.promisifyAll(registryContract.at(registryAddresses[version]));
});
}
}
ENS_0.NameNotFound = Error("ENS name not found");
/**
* resolver returns a resolver object for the specified name, throwing
* ENS.NameNotFound if the name does not exist in ENS.
* Resolver objects are wrappers around web3 contract objects, with the
* first argument - always the node ID in an ENS resolver - automatically
* supplied. So, to call the `addr(node)` function on a standard resolver,
* you only have to call `addr()`. Returned objects are also 'promisified' - they
* return a Bluebird Promise object instead of taking a callback.
* @param {string} name The name to look up.
* @param {list} abi Optional. The JSON ABI definition to use for the resolver.
* if none is supplied, a default definition implementing `has`, `addr`, `name`,
* `setName` and `setAddr` is supplied.
* @returns The resolver object.
*/
ENS_0.prototype.resolver = function(name, abi) {
abi = abi || resolverInterface;
var node = namehash.hash(name);
return new Resolver_0(this, node, this.web3.eth.contract(abi));
};
/**
* reverse returns a resolver object for the reverse resolution of the specified address,
* throwing ENS.NameNotFound if the reverse record does not exist in ENS.
* Resolver objects are wrappers around web3 contract objects, with the
* first argument - always the node ID in an ENS resolver - automatically
* supplied. So, to call the `addr(node)` function on a standard resolver,
* you only have to call `addr()`. Returned objects are also 'promisified' - they
* return a Bluebird Promise object instead of taking a callback.
* @param {string} address The address to look up.
* @param {list} abi Optional. The JSON ABI definition to use for the resolver.
* if none is supplied, a default definition implementing `has`, `addr`, `name`,
* `setName` and `setAddr` is supplied.
* @returns The resolver object.
*/
ENS_0.prototype.reverse = function(address, abi) {
if(address.startsWith("0x"))
address = address.slice(2);
return this.resolver(address.toLowerCase() + ".addr.reverse", abi);
};
/**
* setResolver sets the address of the resolver contract for the specified name.
* The calling account must be the owner of the name in order for this call to
* succeed.
* @param {string} name The name to update
* @param {address} address The address of the resolver
* @param {object} options An optional dict of parameters to pass to web3.
* @returns A promise that returns the transaction ID when the transaction is mined.
*/
ENS_0.prototype.setResolver = function(name, addr, params) {
var node = namehash.hash(name);
return this.registryPromise.then(function(registry) {
return registry.setResolverAsync(node, addr, params);
});
}
/**
* owner returns the address of the owner of the specified name.
* @param {string} name The name to look up.
* @returns A promise returning the owner address of the specified name.
*/
ENS_0.prototype.owner = function(name, callback) {
var node = namehash.hash(name);
return this.registryPromise.then(function(registry) {
return registry.ownerAsync(node);
});
}
/**
* setOwner sets the owner of the specified name. Only the owner may call
* setResolver or setSubnodeOwner. The calling account must be the current
* owner of the name in order for this call to succeed.
* @param {string} name The name to update
* @param {address} address The address of the new owner
* @param {object} options An optional dict of parameters to pass to web3.
* @returns A promise returning the transaction ID of the transaction, once mined.
*/
ENS_0.prototype.setOwner = function(name, addr, params) {
var node = namehash.hash(name);
return this.registryPromise.then(function(registry) {
return registry.setOwnerAsync(node, addr, params);
});
}
/**
* setSubnodeOwner sets the owner of the specified name. The calling account
* must be the owner of the parent name in order for this call to succeed -
* for example, to call setSubnodeOwner on 'foo.bar.eth', the caller must be
* the owner of 'bar.eth'.
* @param {string} name The name to update
* @param {address} address The address of the new owner
* @param {object} options An optional dict of parameters to pass to web3.
* @returns A promise returning the transaction ID of the transaction, once mined.
*/
ENS_0.prototype.setSubnodeOwner = function(name, addr, params) {
var node = utils.parentNamehash(name);
return this.registryPromise.then(function(registry) {
return registry.setSubnodeOwnerAsync(node[1], node[0], addr, params);
});
}
module.exports = ENS;