forked from maurelian/eth-registrar-ens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
525 lines (465 loc) · 21.4 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
var interfaces = require('./interfaces.js');
var ENS = require('ethereum-ens');
var StringPrep = require('node-stringprep').StringPrep;
var NamePrep = new StringPrep('nameprep');
var _ = require('underscore');
// var registrarInterface = [{"constant":false,"inputs":[{"name":"_hash","type":"bytes32"}],"name":"releaseDeed","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"unhashedName","type":"string"}],"name":"invalidateName","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"hash","type":"bytes32"},{"name":"owner","type":"address"},{"name":"value","type":"uint256"},{"name":"salt","type":"bytes32"}],"name":"shaBid","outputs":[{"name":"sealedBid","type":"bytes32"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"entries","outputs":[{"name":"status","type":"uint8"},{"name":"deed","type":"address"},{"name":"registrationDate","type":"uint256"},{"name":"value","type":"uint256"},{"name":"highestBid","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"ens","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"sealedBids","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_hash","type":"bytes32"},{"name":"newOwner","type":"address"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_hash","type":"bytes32"}],"name":"finalizeAuction","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_hash","type":"bytes32"},{"name":"_owner","type":"address"},{"name":"_value","type":"uint256"},{"name":"_salt","type":"bytes32"}],"name":"unsealBid","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"registryCreated","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"sealedBid","type":"bytes32"}],"name":"newBid","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[{"name":"seal","type":"bytes32"}],"name":"cancelBid","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_hashes","type":"bytes32[]"}],"name":"startAuctions","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_hash","type":"bytes32"}],"name":"startAuction","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"rootNode","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"inputs":[{"name":"_ens","type":"address"},{"name":"_rootNode","type":"bytes32"}],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"hash","type":"bytes32"},{"indexed":false,"name":"auctionExpiryDate","type":"uint256"}],"name":"AuctionStarted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"hash","type":"bytes32"},{"indexed":false,"name":"deposit","type":"uint256"}],"name":"NewBid","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"hash","type":"bytes32"},{"indexed":true,"name":"owner","type":"address"},{"indexed":false,"name":"value","type":"uint256"},{"indexed":false,"name":"status","type":"uint8"}],"name":"BidRevealed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"hash","type":"bytes32"},{"indexed":true,"name":"owner","type":"address"},{"indexed":false,"name":"value","type":"uint256"},{"indexed":false,"name":"now","type":"uint256"}],"name":"HashRegistered","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"hash","type":"bytes32"},{"indexed":false,"name":"value","type":"uint256"}],"name":"HashReleased","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"hash","type":"bytes32"},{"indexed":true,"name":"name","type":"string"},{"indexed":false,"name":"value","type":"uint256"},{"indexed":false,"name":"now","type":"uint256"}],"name":"HashInvalidated","type":"event"}];
// var registryInterface = [{"constant":true,"inputs":[{"name":"node","type":"bytes32"}],"name":"resolver","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":true,"inputs":[{"name":"node","type":"bytes32"}],"name":"owner","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"resolver","type":"address"}],"name":"setResolver","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"label","type":"bytes32"},{"name":"owner","type":"address"}],"name":"setSubnodeOwner","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"owner","type":"address"}],"name":"setOwner","outputs":[],"type":"function"}];
var namehash = ENS.prototype.namehash;
/**
* Constructs a new Registrar instance, providing an easy-to-use interface to the
* [Initial Registrar][wiki], which governs the `.eth` namespace. Either Registrar.init(),
* or registrar.initDefault() must be called
* [wiki]: https://github.com/ethereum/ens/wiki
*
* Example usage:
*
* var Registrar = require('eth-registrar-ens');
* var Web3 = require('web3');
*
* var web3 = new Web3();
* var registrar = new Registrar(web3)
*
* // On Ropsten with the public ENS registry
* registrar.init();
* console.log(registrar.ens.registry.address); // '0x112234455c3a32fd11230c42e7bccd4a84e02010'
* console.log(registrar.rootNode); // '0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae'
*
* var name = 'foobarbaz';
* registrar.startAuction(name);
*
* var owner = web3.eth.accounts[0]
* var value = web3.toWei(1, 'ether');
*
* // generate a sealed bid
* var bid = registrar.shaBid(name, owner, value, 'secret');
*
* // submit a bid, and a deposit value. The parameters of your true bid are secret.
* var deposit = web3.toWei(2, 'ether');
* registrar.newBid(bid, {value: deposit});
*
* // reveal your bid during the reveal period
* registrar.unsealBid(name, owner, value, 'secret');
*
* // After the registration date has passed, assign ownership of the name
* // in the ENS. In this case, the highest bidder would now own 'foobarbaz.eth'
* registrar.finalizeAuction(name);
*
*
*
*
*
* 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 create transactions also take an optional 'options' argument;
* this has the same parameters as web3.
*
* @author J Maurelian
* @date 2016
* @license LGPL
*
* @class
* @param {object} web3 A web3 instance to use to communicate with the blockchain.
* @param {address} address The address of the registrar.
* @param {integer} min_length The minimum length of a name require by the registrar.
* @param {string} tld The top level domain
* @param {string} ens The address of the ENS instance
*/
function Registrar(web3){
this.web3 = web3;
}
var publicRegistryAddress = "0x112234455c3a32fd11230c42e7bccd4a84e02010";
Registrar.prototype.init = function(ens, tld, min_length){
// get registrar address from ens
this.ens = ens || new ENS(this.web3);
this.tld = tld || 'eth';
this.min_length = min_length || 7;
this.address = this.ens.owner(this.tld);
this.contract = this.web3.eth.contract(interfaces.registrarInterface).at(this.address);
// this isn't used yet, but I expect it will be handy
this.rootNode = namehash(this.tld);
}
Registrar.TooShort = Error("Name is too short");
Registrar.prototype.fixSha3 = function () {
var original = this.web3.sha3;
this.web3.sha3 = function() {
var result = original.apply(this, arguments);
if (result[1] !== 'x') {
return '0x' + result;
}
return result;
}
return this.web3;
};
function cleanName(input) {
return NamePrep.prepare(input)
.replace(/[áăǎâäȧạȁàảȃāąᶏẚåḁⱥã]/g,"a")
.replace(/[èéêëēěĕȅȩḙėẹẻęẽ]/g,"e")
.replace(/[íĭǐîïịȉìỉȋīįᶖɨĩḭ]/g,"i")
.replace(/[óŏǒôöȯọőȍòỏơȏꝋꝍⱺōǫøõ]/g,"o")
.replace(/[úŭǔûṷüṳụűȕùủưȗūųᶙůũṵ]/g,"u")
.replace(/[çćčĉċ]/g,"c")
.replace(/[śšşŝșṡṣʂᵴꞩᶊȿ]/g,"s")
.replace(/[^a-z0-9\-\_]*/g, "");
}
Registrar.SpecialCharacters = Error (
"Name cannot contain special characters other than \
a-z, 0-9, '-' and '_'."
);
Registrar.prototype.validateName = function (name) {
if (name.length <= this.min_length)
throw Registrar.TooShort;
if (name != cleanName(name)){
throw Registrar.SpecialCharacters;
}
}
/**
* Constructs a new Entry instance corresponding to a name.
*
* @param {string} name The unhashed name
* @param {string} hash
* @param {number} status
* @param {address} deed
* @param {number} registrationDate
* @param {number} value
* @param {number} highestBid
*/
function Entry(name, hash, status, deed, registrationDate, value, highestBid){
this.name = name;
this.hash = hash;
this.status = status;
this.deed = deed;
this.registrationDate = registrationDate;
this.value = value;
this.highestBid = highestBid;
// Check the auction mode
let mode ='';
if (name.length <= 7) {
// If name is short, check if it has been bought
if (this.status == 0) {
mode = 'invalid';
} else {
mode = 'can-invalidate';
}
} else {
// If name is of valid length
if (this.status == 0) {
// Not an auction yet
mode = 'open';
} else if (this.status == 1) {
let now = new Date();
let registration = new Date(this.registrationDate*1000);
let hours = 60*60*1000;
if ((registration - now) > 24 * hours ) {
// Bids are open
mode = 'auction';
} else if (now < registration && (registration - now) < 24 * hours ) {
// reveal time!
mode = 'reveal';
} else if (now > registration && (now - registration) < 24 * hours ) {
// finalize now
mode = 'finalize';
} else {
// finalize now but can open?
mode = 'finalize-open';
}
} else if (this.status == 2) {
mode = 'owned';
}
}
this.mode = mode;
}
/**
* Returns the properties of the entry for a given a name
*
* @method getEntry
* @alias Registrar.getEntry
* @memberOf Registrar.prototype
* @param {string} name The name to get the entry for
* @param {function} callback An optional callback; if specified, the
* function executes asynchronously.
*
* @returns An Entry object
*/
Registrar.prototype.getEntry = function(name, callback){
var name = cleanName(name);
var hash = this.web3.sha3(name);
var e = this.contract.entries(hash);
var entry = new Entry(name, hash, e[0].toNumber(), e[1], e[2].toNumber(), e[3].toNumber(), e[4].toNumber());
if (callback){
callback(null, entry);
} else {
return entry;
}
};
/**
* Converts a name to a hash string, and opens an auction on that hash.
*
* @param {string} name The name to start an auction on
* @param {object} params An optional transaction object to pass to web3.
* @param {function} callback An optional callback; if specified, the
* function executes asynchronously.
*
* @returns The txid if callback is not supplied.
*/
Registrar.prototype.startAuction = function(name){
var hash = this.web3.sha3(name);
var callback = undefined;
if(typeof arguments[arguments.length - 1] == 'function') {
callback = arguments[arguments.length - 1];
}
var params = {};
// test to see if we have parameters for the web3 request:
if(callback && arguments.length == 3) {
params = arguments[arguments.length - 2];
} else if(!callback && arguments.length == 2) {
params = arguments[arguments.length - 1];
}
if(!callback) {
this.validateName(name)
return this.contract.startAuction(hash, params);
} else {
try {
this.validateName(name);
// if name is not valid, this line won't be called.
this.contract.startAuction(hash, params, callback);
} catch(e) {
callback(e, null);
}
}
};
/**
* Opens auctions for multiple names at once. Since names are registered as hashes,
* this helps to prevent other bidders from guessing which names you are interested in.
*
* @param {array} names An array of names to start auctions on
* @param {object} params An optional transaction object to pass to web3.
* @param {function} callback An optional callback; if specified, the
* function executes asynchronously.
*
* @returns The txid if callback is not supplied.
*/
Registrar.prototype.startAuctions = function(names){
var hashes = _.map(names, this.web3.sha3);
var invalidNames = names.filter(
function(name){ return name.length < this.min_length;}, this
);
var callback = undefined;
if (typeof(arguments[arguments.length - 1]) == 'function'){
callback = arguments[arguments.length - 1];
}
var params = {};
if (callback && arguments.length == 3){
params = arguments[arguments.length - 2];
} else if (!callback && arguments.length == 2){
params = arguments[arguments.length - 1];
}
if (!callback) {
if (invalidNames.length > 0)
throw Registrar.TooShort;
return this.contract.startAuctions(hashes, params);
} else {
if (invalidNames.length > 0)
callback(Registrar.TooShort, null);
else
this.contract.startAuctions(hashes, params, callback);
}
};
/**
* Generates the "bid string" (hash) which representing a sealed bid. This does not
* submit the bid to the registrar, it only calls on the registrar's corresponding
* method.
*
* ToDo: Make `owner` default to sender if not specified.
*
* @param {string} name The name to be bid on
* @param {string} address An optional owner address
* @param {number} value The value of your bid in wei
* @param {secret} secret An optional random value
*
* @returns the sealed bid hash string
*/
// How should we better handle secret generation and storage?
// it could be abstracted away and handle generation, storage agnd retrieval.
// var bid = ethRegistrar.shaBid(web3.sha3('name'), eth.accounts[0], web3.toWei(1, 'ether'), web3.sha3('secret'));
Registrar.prototype.shaBid = function(name, owner, value, secret, callback){
var name = cleanName(name);
var hash = this.web3.sha3(name);
var hexSecret = this.web3.sha3(secret);
if (!callback){
return this.contract.shaBid(hash, owner, value, hexSecret);
} else {
this.contract.shaBid(hash, owner, value, hexSecret, callback);
}
};
Registrar.NoDeposit = Error("You must specify a deposit amount greater than the value of your bid");
/**
* Submits a bid string to the registrar, creating a new sealed bid entry.
* The value
*
* @param {string} bid
* @param {object} params A dict of parameters to pass to web3. An amount must be included.
* @param {function} callback An optional callback; if specified, the
* function executes asynchronously.
*
* @returns The transaction ID if callback is not supplied.
*/
/* At present this provides very little utility, aside from putting the method where you would expect it to be.
* More value would be in:
* creating the bid hash string
* accepting the value as a "deposit" variable
* accepting a bid object:
{
name: "name",
owner: "0xaddress",
value: 1,
deposit: 2,
secret: "secret"
}
*/
Registrar.prototype.newBid = function(bid, params, callback){
// Unlike the previous methods, params are necessary here in order to make the deposit
if (!callback){
if(!params.value){
throw Registrar.NoDeposit;
}
return this.contract.newBid(bid, params);
} else {
if(!params.value){
callback(Registrar.NoDeposit, null);
} else {
this.contract.newBid(bid, params, callback);
}
}
};
/**
* Submits the parameters of a bid. The registrar will then generate
* the bid string, and associate them with the previously submitted bid string and
* deposit. If you have not already submitted a bid string, the registrar will throw.
* If your bid is revealed as the current highest; the difference between your deposit
* and bid value will be returned to you, and the previous highest bidder will have
* their funds returned. If you are not the highest bidder, all your funds will be
* returned. Returns are sent to the owner address on the bid.
*
* @param {string} name
* @param {address} owner An optional owner address; defaults to sender
* @param {number} value The value of your bid
* @param {secret} secret The secret used to create the bid string
* @param {object} options An optional transaction object to pass to web3.
* @param {function} callback An optional callback; if specified, the
* function executes asynchronously.
*
* @returns The transaction ID if callback is not supplied.
*/
Registrar.prototype.unsealBid = function(name, owner, value, secret){
var name = cleanName(name);
var hash = this.web3.sha3(name);
var hexSecret = this.web3.sha3(secret);
var callback = undefined;
if (typeof(arguments[arguments.length - 1]) == 'function'){
callback = arguments[arguments.length - 1];
}
var params = {};
if (callback && arguments.length == 6){
params = arguments[arguments.length - 2];
} else if (!callback && arguments.length == 5){
params = arguments[arguments.length - 1];
}
if (!callback){
return this.contract.unsealBid(hash, owner, value, hexSecret, params);
} else {
this.contract.unsealBid(hash, owner, value, hexSecret, params, callback);
}
};
/**
* __Not yet implemented__
* After the registration date has passed, calling finalizeAuction
* will set the winner as the owner of the corresponding ENS subnode.
*
* @param {string} name
* @param {object} options An optional transaction object to pass to web3.
* @param {function} callback An optional callback; if specified, the
* function executes asynchronously.
*
* @returns The transaction ID if callback is not supplied.
*/
Registrar.prototype.finalizeAuction = function(name){
var name = cleanName(name);
var hash = this.web3.sha3(name);
var callback = undefined;
if(typeof arguments[arguments.length - 1] == 'function') {
callback = arguments[arguments.length - 1];
}
var params = {};
// test to see if we have parameters for the web3 request:
if(callback && arguments.length == 3) {
params = arguments[arguments.length - 2];
} else if(!callback && arguments.length == 2) {
params = arguments[arguments.length - 1];
}
if(!callback) {
return this.contract.startAuction(hash, params);
} else {
this.contract.startAuction(hash, params, callback);
}
};
/**
* __Not yet implemented__
* The owner of a domain may transfer it, and the associated deed,
* to someone else at any time.
*
* @param {string} name The node to transfer
* @param {string} newOwner The address to transfer ownership to
* @param {object} options An optional transaction object to pass to web3.
* @param {function} callback An optional callback; if specified, the
* function executes asynchronously.
*
* @returns The transaction ID if callback is not supplied.
*/
Registrar.prototype.transfer = function(name, newOwner){
}
/**
* __Not yet implemented__
* After one year, the owner can release the property and get their ether back
*
* @param {string} name The name to release
* @param {object} options An optional transaction object to pass to web3.
* @param {function} callback An optional callback; if specified, the
* function executes asynchronously.
*
* @returns The transaction ID if callback is not supplied.
*/
Registrar.prototype.releaseDeed = function(name){
}
/**
* __Not yet implemented__
* Submit a name 6 characters long or less. If it has been registered,
* the submitter will earn a portion of the deed value, and the name will be updated
*
* @param {string} name An invalid name to search for in the registry.
* @param {object} options An optional transaction object to pass to web3.
* @param {function} callback An optional callback; if specified, the
* function executes asynchronously.
*
* @returns The transaction ID if callback is not supplied.
*/
Registrar.prototype.invalidateName = function(name){
};
/**
* __Not yet implemented__
* Transfers the deed to the current registrar, if different from this one.
* Used during the upgrade process to a permanent registrar.
*
* @param name The name to transfer.
* @param {object} options An optional transaction object to pass to web3.
* @param {function} callback An optional callback; if specified, the
* function executes asynchronously.
*
* @returns The transaction ID if callback is not supplied.
*/
Registrar.prototype.transferRegistrars = function(name){
};
module.exports = Registrar;