-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
ethers-ens.ts
891 lines (716 loc) · 27.3 KB
/
ethers-ens.ts
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
#!/usr/bin/env node
'use strict';
import { ethers } from "ethers";
import { Base58 } from "@ethersproject/basex";
import { ArgParser, CLI, Help, Plugin } from '../cli';
import { version } from "../_version";
const logger = new ethers.utils.Logger(version);
const ensAbi = [
"function setOwner(bytes32 node, address owner) external @500000",
"function setSubnodeOwner(bytes32 node, bytes32 label, address owner) external @500000",
"function setResolver(bytes32 node, address resolver) external @500000",
"function owner(bytes32 node) external view returns (address)",
"function resolver(bytes32 node) external view returns (address)"
];
const States = Object.freeze([ "Open", "Auction", "Owned", "Forbidden", "Reveal", "NotAvailable" ]);
const deedAbi = [
"function owner() view returns (address)"
];
const ethLegacyRegistrarAbi = [
"function entries(bytes32 _hash) view returns (uint8 state, address owner, uint registrationDate, uint value, uint highestBid)",
"function transferRegistrars(bytes32 _hash) @500000",
];
const ethControllerAbi = [
"function rentPrice(string memory name, uint duration) view public returns(uint)",
"function available(string memory label) public view returns(bool)",
"function makeCommitment(string memory name, address owner, bytes32 secret) pure public returns(bytes32)",
"function commit(bytes32 commitment) public @500000",
"function register(string calldata name, address owner, uint duration, bytes32 secret) payable @500000",
"function renew(string calldata name, uint duration) payable @500000",
];
const ethRegistrarAbi = [
"function ownerOf(uint256 tokenId) view returns (address)",
"function reclaim(uint256 id, address owner) @500000",
"function safeTransferFrom(address from, address to, uint256 tokenId) @500000"
];
const resolverAbi = [
"function interfaceImplementer(bytes32 nodehash, bytes4 interfaceId) view returns (address)",
"function addr(bytes32 nodehash) view returns (address)",
"function setAddr(bytes32 nodehash, address addr) @500000",
"function name(bytes32 nodehash) view returns (string)",
"function setName(bytes32 nodehash, string name) @500000",
"function text(bytes32 nodehash, string key) view returns (string)",
"function setText(bytes32 nodehash, string key, string value) @500000",
"function contenthash(bytes32 nodehash) view returns (bytes)",
"function setContenthash(bytes32 nodehash, bytes contenthash) @500000",
];
//const InterfaceID_ERC721 = "0x6ccb2df4";
const InterfaceID_Controller = "0x018fac06";
const InterfaceID_Legacy = "0x7ba18ba1";
/*
const reverseRegistrarAbi = [
];
*/
function listify(words: Array<string>): string {
if (words.length === 1) { return words[0]; }
return words.slice(0, words.length - 1).join(", ") + " and " + words[words.length - 1];
}
let cli = new CLI();
abstract class EnsPlugin extends Plugin {
_ethAddressCache: { [ addressOrInterfaceId: string ]: string };
constructor() {
super();
ethers.utils.defineReadOnly(this, "_ethAddressCache", { });
}
getEns(): ethers.Contract {
return new ethers.Contract(this.network.ensAddress, ensAbi, this.accounts[0] || this.provider);
}
async getResolver(nodehash: string): Promise<ethers.Contract> {
if (!this._ethAddressCache[nodehash]) {
this._ethAddressCache[nodehash] = await this.getEns().resolver(nodehash);
}
return new ethers.Contract(this._ethAddressCache[nodehash], resolverAbi, this.accounts[0] || this.provider);
}
async getEthInterfaceAddress(interfaceId: string): Promise<string> {
let ethNodehash = ethers.utils.namehash("eth");
if (!this._ethAddressCache[interfaceId]) {
let resolver = await this.getResolver(ethNodehash);
this._ethAddressCache[interfaceId] = await resolver.interfaceImplementer(ethNodehash, interfaceId);
}
return this._ethAddressCache[interfaceId];
}
async getEthController(): Promise<ethers.Contract> {
let address = await this.getEthInterfaceAddress(InterfaceID_Controller);
return new ethers.Contract(address, ethControllerAbi, this.accounts[0] || this.provider);
}
async getEthLegacyRegistrar(): Promise<ethers.Contract> {
let address = await this.getEthInterfaceAddress(InterfaceID_Legacy);
return new ethers.Contract(address, ethLegacyRegistrarAbi, this.accounts[0] || this.provider);
}
async getEthRegistrar(): Promise<ethers.Contract> {
//let address = await this.getEthInterfaceAddress(InterfaceID_ERC721);
let address = await this.getEns().owner(ethers.utils.namehash("eth"));
return new ethers.Contract(address, ethRegistrarAbi, this.accounts[0] || this.provider);
}
}
class LookupPlugin extends EnsPlugin {
names: Array<string>;
static getHelp(): Help {
return {
name: "lookup [ NAME | ADDRESS [ ... ] ]",
help: "Lookup a name or address"
}
}
async prepareArgs(args: Array<string>): Promise<void> {
await super.prepareArgs(args);
this.names = args;
}
async run(): Promise<void> {
await super.run();
let ens = this.getEns();
let controller = await this.getEthController();
let registrar = await this.getEthRegistrar();
let legacyRegistrar = await this.getEthLegacyRegistrar();
for (let i = 0; i < this.names.length; i++) {
let name = this.names[i];
let nodehash = ethers.utils.namehash(name);
let details: { [ key: string]: string } = {
Nodehash: nodehash
};
let owner = await ens.owner(nodehash);
let resolverAddress: string = null;
if (owner === ethers.constants.AddressZero) {
owner = null;
} else {
details.Controller = owner;
details.Resolver = await ens.resolver(nodehash).then((address: string) => {
if (address === ethers.constants.AddressZero) {
return "(not configured)";
}
resolverAddress = address;
return address;
});
}
let comps = name.split(".");
if (comps.length === 2 && comps[1] === "eth") {
details.Labelhash = ethers.utils.id(comps[0].toLowerCase()); // @TODO: nameprep
details.Available = await controller.available(comps[0]);
if (!details.Available) {
try {
let ownerOf = await registrar.ownerOf(details.Labelhash);
if (ownerOf !== ethers.constants.AddressZero) {
details.Registrant = ownerOf;
details.Registrar = "Permanent";
}
} catch (error) {
let entry = await legacyRegistrar.entries(details.Labelhash);
let deed = new ethers.Contract(entry.owner, deedAbi, this.provider);
details.Registrant = await deed.owner();
details.Registrar = "Legacy";
details["Deed Value"] = (ethers.utils.formatEther(entry.value) + " ether");
details["Highest Bid"] = (ethers.utils.formatEther(entry.highestBid) + " ether");
}
}
}
if (resolverAddress) {
let resolver = new ethers.Contract(resolverAddress, resolverAbi, this.provider);
details["Address"] = await resolver.addr(nodehash);
let email = await resolver.text(nodehash, "email").catch((error: any) => (""));
if (email) { details["E-mail"] = email; }
let website = await resolver.text(nodehash, "website").catch((error: any) => (""));
if (website) { details["Website"] = website; }
let content = await resolver.contenthash(nodehash).then((hash: string) => {
if (hash === "0x") { return ""; }
if (hash.substring(0, 10) === "0xe3010170" && ethers.utils.isHexString(hash, 38)) {
return Base58.encode(ethers.utils.hexDataSlice(hash, 4)) + " (IPFS)";
}
return hash + " (unknown format)";
}, (error: any) => (""));
if (content) { details["Content Hash"] = content; }
}
let ordered: { [ key: string]: string } = { };
"Nodehash,Labelhash,Available,Registrant,Controller,Resolver,Address,Registrar,Deed Value,Highest Bid,E-mail,Website,Content Hash".split(",").forEach((key) => {
if (!details[key]) { return; }
ordered[key] = details[key];
});
for (let key in details) {
if (ordered[key]) { continue; }
ordered[key] = details[key];
}
this.dump("Name: " + this.names[i], ordered);
}
}
}
cli.addPlugin("lookup", LookupPlugin);
abstract class AccountPlugin extends EnsPlugin {
name: string;
nodehash: string;
static getHelp(): Help {
return logger.throwError("subclasses must implemetn this", ethers.errors.UNSUPPORTED_OPERATION, {
operation: "getHelp"
});
}
async _setValue(key: string, value: string): Promise<void> {
ethers.utils.defineReadOnly<any, any>(this, key, value);
if (key === "name") {
await this._setValue("nodehash", ethers.utils.namehash(value));
}
}
async prepareArgs(args: Array<string>): Promise<void> {
await super.prepareArgs(args);
let helpLine = ethers.utils.getStatic<() => Help>(this.constructor, "getHelp")().name;
let params = helpLine.split(" ");
let command = params[0];
params = params.slice(1);
if (this.accounts.length !== 1) {
this.throwError(command + " requires an account");
}
if (args.length !== params.length) {
this.throwError(command + " requires exactly " + listify(params));
}
for (let i = 0; i < params.length; i++ ) {
await this._setValue(params[i].toLowerCase(), args[i]);
}
}
}
abstract class ControllerPlugin extends AccountPlugin {
salt: string;
owner: string
label: string;
duration: number;
static getOptionHelp(): Array<Help> {
return [
{
name: "[ --duration DAYS ]",
help: "Register duration (default: 365 days)"
},
{
name: "[ --salt SALT ]",
help: "SALT to blind the commit with"
},
{
name: "[ --secret SECRET ]",
help: "Use id(SECRET) as the salt"
},
{
name: "[ --owner OWNER ]",
help: "The target owner (default: current account)"
}
];
}
async _setValue(key: string, value: string): Promise<void> {
if (key === "name") {
let comps = value.split(".");
if (comps.length !== 2 || comps[1] !== "eth") {
this.throwError("Invalid NAME");
}
await super._setValue("label", comps[0]);
}
await super._setValue(key, value);
}
async prepareOptions(argParser: ArgParser): Promise<void> {
await super.prepareOptions(argParser);
this.salt = argParser.consumeOption("salt");
let secret = argParser.consumeOption("secret");
if (secret) {
if (this.salt) {
this.throwError("Cannot specify --salt with --secret");
}
this.salt = ethers.utils.id(secret);
}
this.owner = argParser.consumeOption("owner");
if (this.owner) {
this.owner = await this.getAddress(this.owner);
} else {
this.owner = await this.accounts[0].getAddress();
}
let duration = parseInt(argParser.consumeOption("duration") || "365");
if (duration < 28) {
this.throwError("registration must be for a minimum length of 28 days");
}
ethers.utils.defineReadOnly(this, "duration", duration * (60 * 60 * 24));
}
async prepareArgs(args: Array<string>): Promise<void> {
await super.prepareArgs(args);
if (!this.salt) {
let signature = await this.accounts[0].signMessage("commit-" + this.owner + "-" + this.name);
this.salt = ethers.utils.keccak256(signature);
}
}
}
class CommitPlugin extends ControllerPlugin {
static getHelp(): Help {
return {
name: "commit NAME",
help: "Submit a pre-commitment"
}
}
async run(): Promise<void> {
await super.run();
let ethController = await this.getEthController();
let commitment = await ethController.makeCommitment(this.label, this.owner, this.salt);
let fee = await ethController.rentPrice(this.label, this.duration);
this.dump("Commit: " + this.name, {
Nodehash: this.nodehash,
Owner: this.owner,
Salt: this.salt,
Duration: (this.duration + " seconds (informational)"),
Fee: ethers.utils.formatEther(fee) + " (informational)",
Commitment: commitment
});
await ethController.commit(commitment);
}
}
cli.addPlugin("commit", CommitPlugin);
class RevealPlugin extends ControllerPlugin {
static getHelp(): Help {
return {
name: "reveal NAME",
help: "Reveal a previous pre-commitment"
}
}
async run(): Promise<void> {
await super.run();
let ethController = await this.getEthController();
let fee = await ethController.rentPrice(this.label, this.duration);
this.dump("Reveal: " + this.name, {
Nodehash: this.nodehash,
Owner: this.owner,
Salt: this.salt,
Duration: (this.duration + " seconds"),
Fee: ethers.utils.formatEther(fee),
});
await ethController.register(this.label, this.owner, this.duration, this.salt, {
value: fee.mul(11).div(10)
});
}
}
cli.addPlugin("reveal", RevealPlugin);
/*
class CommitRevealPlugin extends RevealPlugin {
waitBlocks: number;
static getHelp(): Help {
return {
name: "commit-reveal LABEL",
help: "Commit, wait and reveal a name"
}
}
static getOptionHelp(): Array<Help> {
let help = CommitPlugin.getOptionHelp().slice();
help.push({
name: "[ --wait BLOCKS ]",
help: "Wait BLOCKS confirms (Default: 5)"
})
return help;
}
async prepareOptions(argParser: ArgParser): Promise<void> {
await super.prepareOptions(argParser);
let waitBlocks = argParser.consumeOption("wait");
try {
this.waitBlocks = parseInt(waitBlocks || "5");
} catch(error) {
this.throwError("Invalid --wait BLOCKS")
}
}
async run(): Promise<void> {
await super.run();
console.log(this);
}
}
cli.addPlugin("commit-reveal", CommitRevealPlugin);
*/
abstract class AddressAccountPlugin extends AccountPlugin {
address: string;
static getOptionHelp(): Array<Help> {
return [
{
name: "[ --address ADDRESS ]",
help: "Specify another address"
}
];
}
getDefaultAddress(): Promise<string> {
return this.accounts[0].getAddress();
}
async prepareOptions(argParser: ArgParser): Promise<void> {
await super.prepareOptions(argParser);
let address = argParser.consumeOption("address");
if (!address) {
address = await this.getDefaultAddress();
}
this.address = await this.getAddress(address);
}
}
class SetControllerPlugin extends AddressAccountPlugin {
static getHelp(): Help {
return {
name: "set-controller NAME",
help: "Set the controller (default: current account)"
}
}
async run(): Promise<void> {
await super.run();
this.dump("Set Subnode: " + this.name, {
"Nodehash": this.nodehash,
"Owner": this.address
});
this.getEns().setOwner(this.nodehash, this.address);
}
}
cli.addPlugin("set-controller", SetControllerPlugin);
class SetSubnodePlugin extends AddressAccountPlugin {
label: string;
node: string;
static getHelp(): Help {
return {
name: "set-subnode NAME",
help: "Set a subnode owner (default: current account)"
}
}
async _setValue(key: string, value: string): Promise<void> {
if (key === "name") {
let comps = value.toLowerCase().split(".");
await super._setValue("label", comps[0]);
await super._setValue("node", comps.slice(1).join("."));
}
await super._setValue(key, value);
}
async run(): Promise<void> {
await super.run();
this.dump("Set Subnode: " + this.name, {
"Label": this.label,
"Node": this.node,
"Owner": this.address
});
await this.getEns().setSubnodeOwner(ethers.utils.namehash(this.node), ethers.utils.id(this.label), this.address);
}
}
cli.addPlugin("set-subnode", SetSubnodePlugin);
class SetResolverPlugin extends AddressAccountPlugin {
static getHelp(): Help {
return {
name: "set-resolver NAME",
help: "Set the resolver (default: resolver.eth)"
}
}
getDefaultAddress(): Promise<string> {
return this.getAddress("resolver.eth");
}
async run(): Promise<void> {
await super.run();
this.dump("Set Resolver: " + this.name, {
"Nodehash": this.nodehash,
"Resolver": this.address
});
await this.getEns().setResolver(this.nodehash, this.address);
}
}
cli.addPlugin("set-resolver", SetResolverPlugin);
class SetAddrPlugin extends AddressAccountPlugin {
static getHelp(): Help {
return {
name: "set-addr NAME",
help: "Set the addr record (default: current account)"
}
}
async run(): Promise<void> {
await super.run();
this.dump("Set Addr: " + this.name, {
"Nodehash": this.nodehash,
"Address": this.address
});
let resolver = await this.getResolver(this.nodehash);
await resolver.setAddr(this.nodehash, this.address);
}
}
cli.addPlugin("set-addr", SetAddrPlugin);
class SetNamePlugin extends AddressAccountPlugin {
static getHelp(): Help {
return {
name: "set-name NAME",
help: "Set the reverse name record (default: current account)"
}
}
async run(): Promise<void> {
await super.run();
const nodehash = ethers.utils.namehash(this.address.substring(2) + ".addr.reverse");
this.dump("Set Name: " + this.name, {
"Nodehash": nodehash,
"Address": this.address
});
let resolver = await this.getResolver(nodehash);
await resolver.setName(nodehash, this.name);
}
}
cli.addPlugin("set-name", SetNamePlugin);
abstract class TextAccountPlugin extends AccountPlugin {
abstract getHeader(): string;
abstract getKey(): string;
abstract getValue(): string;
async run(): Promise<void> {
await super.run();
let key = this.getKey();
let value = this.getValue();
this.dump("Set " + this.getHeader() + ": " + this.name, {
Nodehash: this.nodehash,
Key: key,
Value: value
});
let resolver = await this.getResolver(this.nodehash);
await resolver.setText(this.nodehash, key, value);
}
}
class SetTextPlugin extends TextAccountPlugin {
key: string;
value: string;
static getHelp(): Help {
return {
name: "set-text NAME KEY VALUE",
help: "Set a text record"
}
}
getHeader(): string { return "Test" }
getKey(): string { return this.key; }
getValue(): string { return this.value; }
}
cli.addPlugin("set-text", SetTextPlugin);
class SetEmailPlugin extends TextAccountPlugin {
email: string;
static getHelp(): Help {
return {
name: "set-email NAME EMAIL",
help: "Set the email text record"
}
}
getHeader(): string { return "E-mail" }
getKey(): string { return "email"; }
getValue(): string { return this.email; }
}
cli.addPlugin("set-email", SetEmailPlugin);
class SetWebsitePlugin extends TextAccountPlugin {
url: string;
static getHelp(): Help {
return {
name: "set-website NAME URL",
help: "Set the website text record"
}
}
getHeader(): string { return "Website" }
getKey(): string { return "url"; }
getValue(): string { return this.url; }
}
cli.addPlugin("set-website", SetWebsitePlugin);
class SetContentPlugin extends AccountPlugin {
readonly hash: string;
readonly multihash: string;
static getHelp(): Help {
return {
name: "set-content NAME HASH",
help: "Set the IPFS Content Hash"
}
}
async _setValue(key: string, value: string): Promise<void> {
if (key === "hash") {
let bytes = Base58.decode(value);
if (bytes.length !== 34 || bytes[0] !== 18 || bytes[1] !== 32) {
this.throwError("Unsupported IPFS hash");
}
let multihash = ethers.utils.concat([ "0xe3010170", bytes ]);
await super._setValue("multihash", ethers.utils.hexlify(multihash));
}
await super._setValue(key, value);
}
async run(): Promise<void> {
await super.run();
this.dump("Set Content Hash: " + this.name, {
Nodehash: this.nodehash,
"Content Hash": this.hash
});
let resolver = await this.getResolver(this.nodehash);
await resolver.setContenthash(this.nodehash, this.multihash);
}
}
cli.addPlugin("set-content", SetContentPlugin);
class MigrateRegistrarPlugin extends AccountPlugin {
readonly label: string;
readonly deedValue: ethers.BigNumber;
readonly highestBid: ethers.BigNumber;
static getHelp(): Help {
return {
name: "migrate-registrar NAME",
help: "Migrate from the Legacy to the Permanent Registrar"
}
}
async prepareArgs(args: Array<string>): Promise<void> {
await super.prepareArgs(args);
// Only Top-Level names can be migrated
let comps = this.name.split(".");
if (comps.length !== 2 || comps[1] !== "eth") {
this.throwError("Not a top-level .eth name");
}
await super._setValue("label", comps[0]);
let ethLegacyRegistrar = await this.getEthLegacyRegistrar();
let entry: any = await ethLegacyRegistrar.entries(ethers.utils.id(comps[0]));
// Only owned names can be migrated
if (States[entry.state] !== "Owned") {
this.throwError("Name not present in the Legacy registrar");
}
let deed = new ethers.Contract(entry.owner, deedAbi, this.provider);
let owner = await deed.owner();
let address = await this.accounts[0].getAddress();
// Only the deed owner (registrant) may migrate a name
if (owner !== address) {
this.throwError("Only the registrant can migrate");
}
await super._setValue("deedValue", entry.value);
await super._setValue("highestBid", entry.highestBid);
}
async run(): Promise<void> {
await super.run();
this.dump("Migrate Registrar: " + this.name, {
"Nodehash": this.nodehash,
"Highest Bid": (ethers.utils.formatEther(this.highestBid) + " ether"),
"Deed Value": (ethers.utils.formatEther(this.deedValue) + " ether"),
});
let legacyRegistrar = await this.getEthLegacyRegistrar();
await legacyRegistrar.transferRegistrars(ethers.utils.id(this.label));
}
}
cli.addPlugin("migrate-registrar", MigrateRegistrarPlugin);
class TransferPlugin extends AccountPlugin {
readonly name: string;
readonly new_owner: string;
readonly label: string;
static getHelp(): Help {
return {
name: "transfer NAME NEW_OWNER",
help: "Transfer registrant ownership"
}
}
async _setValue(key: string, value: string): Promise<void> {
if (key === "new_owner") {
let address = await this.getAddress(value);
await super._setValue(key, address);
} else if (key === "name") {
let comps = value.split(".");
if (comps.length !== 2 || comps[1] !== "eth") {
this.throwError("Not a top-level .eth name");
}
await super._setValue("label", comps[0]);
await super._setValue(key, value);
} else {
await super._setValue(key, value);
}
}
async run(): Promise<void> {
await super.run();
this.dump("Transfer: " + this.name, {
Nodehash: this.nodehash,
"New Owner": this.new_owner,
});
let registrar = await this.getEthRegistrar();
await registrar.safeTransferFrom(this.accounts[0].getAddress(), this.new_owner, ethers.utils.id(this.label));
}
}
cli.addPlugin("transfer", TransferPlugin);
class ReclaimPlugin extends AddressAccountPlugin {
readonly label: string;
static getHelp(): Help {
return {
name: "reclaim NAME",
help: "Reset the controller by the registrant"
}
}
async _setValue(key: string, value: string): Promise<void> {
if (key === "name") {
let comps = value.split(".");
if (comps.length !== 2 || comps[1] !== "eth") {
this.throwError("Not a top-level .eth name");
}
let account = await this.accounts[0].getAddress();
let registrar = await this.getEthRegistrar();
let ownerOf: string = null;
try {
ownerOf = await registrar.ownerOf(ethers.utils.id(comps[0]));
} catch (error) {
this.throwError("Name not present in Permantent Registrar");
}
if (account !== ownerOf) {
this.throwError("Only the registrant can call reclaim");
}
await super._setValue("label", comps[0]);
}
await super._setValue(key, value);
}
async run(): Promise<void> {
await super.run();
this.dump("Reclaim: " + this.name, {
"Nodehash": this.nodehash,
"Address": this.address,
});
let registrar = await this.getEthRegistrar();
await registrar.reclaim(ethers.utils.id(this.label), this.address);
}
}
cli.addPlugin("reclaim", ReclaimPlugin);
/**
* To Do:
* register NAME --registrar
* set-reverse NAME
* renew NAME --duration DAYS
*
* Done:
* migrate-registrar NAME
* transfer NAME OWNER
* set-subnode LABEL.NAME
* set-owner NAME OWNER
* set-resolver NAME RESOLVER
* set-addr NAME ADDRESS
* set-reverse-name ADDRESS NAME
* set-email NAME EMAIL
* set-webstie NAME WEBSITE
* set-text NAME KEY VALUE
* set-content NAME HASH
* reclaim NAME --address OWNER
*/
cli.run(process.argv.slice(2))