Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 85e682f

Browse files
committed
Simplify transfers of names
1 parent 6521ddb commit 85e682f

File tree

1 file changed

+33
-53
lines changed

1 file changed

+33
-53
lines changed

packages/deployer/ens.js

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,22 @@ class ENS {
5252
//is supposed to be the "default resolver"; I'm not sure what that means,
5353
//but I figure using the resolver for "addr.reverse" ought to suffice,
5454
//right? So let's set up a resolver for it.
55-
const reverseResolutionSpecialName = "addr.reverse";
56-
debug("made it here");
5755
//but in order to set its resolver, we first have to set its owner.
58-
await this.setNameOwner({ from, name: reverseResolutionSpecialName });
59-
debug("owner set");
56+
await this.setNameOwner({ from, name: "addr.reverse" });
6057
//now we can actually set the resolver
6158
const { resolverAddress } = await this.ensureResolverExists({
6259
from,
63-
name: reverseResolutionSpecialName
60+
name: "addr.reverse"
6461
});
6562
debug("resolver set: %s", resolverAddress);
66-
//...but wait! we need it to be owned by the registry, not by us.
67-
//(otherwise the deployment will revert.) so, let's hand over
68-
//ownership to the registry.
69-
await this.alienateNameOwner({
63+
//...but wait! we need it to be owned by the registry (or 0), not by us.
64+
//(otherwise the deployment will revert.) so, let's hand over ownership to
65+
//the registry.
66+
await this.updateNameOwner({
7067
from,
71-
owner: registryAddress,
72-
name: reverseResolutionSpecialName
68+
newOwner: registryAddress,
69+
name: "addr.reverse"
7370
});
74-
debug("owner alienated");
7571
//now we can do the deployment!
7672
const reverseRegistrar = await ReverseRegistrar.new(
7773
registryAddress,
@@ -80,33 +76,24 @@ class ENS {
8076
from
8177
}
8278
);
83-
debug("deployed");
8479
//except, we're not done... we need to transfer ownership from the registry
8580
//to the reverse registrar.
8681
//(if there were a previous reverse registrar, this would happen automatically,
8782
//but there wasn't, so it doesn't.)
88-
//so, first let's claim the name for ourself again
89-
await this.setNameOwner({ from, name: reverseResolutionSpecialName });
90-
debug("reclaimed");
91-
//and now let's give it away again
92-
await this.alienateNameOwner({
83+
await this.updateNameOwner({
9384
from,
94-
owner: reverseRegistrar.address,
95-
name: reverseResolutionSpecialName
85+
newOwner: reverseRegistrar.address,
86+
name: "addr.reverse"
9687
});
97-
debug("re-alienated");
88+
//and we're done!
9889
}
9990

10091
async ensureResolverExists({ from, name }) {
10192
// See if the resolver is set, if not then set it
102-
debug("getting resolver");
10393
const resolverAddress = await this.ensjs.name(name).getResolver();
104-
debug("got resolver");
10594
// names with no set resolver have 0x0 returned
10695
if (resolverAddress !== "0x0000000000000000000000000000000000000000") {
107-
debug("getting addr");
10896
const resolvedAddress = await this.ensjs.name(name).getAddress("ETH");
109-
debug("got addr");
11097
return { resolvedAddress, resolverAddress };
11198
}
11299
// deploy a resolver if one isn't set
@@ -116,11 +103,8 @@ class ENS {
116103

117104
let registryAddress = this.determineENSRegistryAddress();
118105

119-
debug("deploying resolver");
120106
const publicResolver = await PublicResolver.new(registryAddress, { from });
121-
debug("deployed; setting resolver");
122107
await this.ensjs.name(name).setResolver(publicResolver.address, { from });
123-
debug("set resolver");
124108
return { resolvedAddress: null, resolverAddress: publicResolver.address };
125109
}
126110

@@ -185,33 +169,29 @@ class ENS {
185169
}
186170
}
187171

188-
//this method assumes that the current owner is `from`,
189-
//and sets the new owner to be `owner`.
190-
async alienateNameOwner({ name, from, owner }) {
191-
//setNameOwner goes down the tree, from top to bottom.
192-
//here, however, we have to go *up* the tree.
193-
//as such, we are going to process the labels in order, rather
194-
//than in reverse order.
172+
//this method assumes that from owns the parent!
173+
//it won't work otherwise!
174+
async updateNameOwner({ name, from, newOwner }) {
175+
//this method does *not* walk the tree.
176+
//it only updates this individual entry.
195177

196-
let remaining = name;
197-
while (remaining !== "") {
198-
let label, suffix;
199-
const dotIndex = remaining.indexOf("."); //find the first dot
200-
if (dotIndex !== -1) {
201-
label = remaining.slice(0, dotIndex); //everything before the dot
202-
suffix = remaining.slice(dotIndex + 1); //everything after the dot
203-
} else {
204-
label = remaining;
205-
suffix = "";
206-
}
207-
await this.devRegistry.setSubnodeOwner(
208-
suffix !== "" ? hash(suffix) : "0x0",
209-
sha3(label),
210-
owner,
211-
{ from }
212-
);
213-
remaining = suffix;
178+
let label, suffix;
179+
180+
const dotIndex = name.indexOf("."); //find the first dot
181+
if (dotIndex !== -1) {
182+
label = name.slice(0, dotIndex); //everything before the dot
183+
suffix = name.slice(dotIndex + 1); //everything after the dot
184+
} else {
185+
label = name;
186+
suffix = "";
214187
}
188+
189+
await this.devRegistry.setSubnodeOwner(
190+
suffix !== "" ? hash(suffix) : "0x0",
191+
sha3(label),
192+
newOwner,
193+
{ from }
194+
);
215195
}
216196

217197
parseAddress(addressOrContract) {

0 commit comments

Comments
 (0)