-
Notifications
You must be signed in to change notification settings - Fork 266
/
registrar.js
406 lines (367 loc) · 11.7 KB
/
registrar.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
import getENS, { getNamehash, getResolverContract } from './ens'
import getWeb3, { getWeb3Read, getAccount, getBlock } from './web3'
import { abi as legacyAuctionRegistrarContract } from '@ensdomains/ens/build/contracts/HashRegistrar'
import { abi as deedContract } from '@ensdomains/ens/build/contracts/Deed'
import { abi as permanentRegistrarContract } from '@ensdomains/ethregistrar/build/contracts/BaseRegistrarImplementation'
import { abi as permanentRegistrarControllerContract } from '@ensdomains/ethregistrar/build/contracts/ETHRegistrarController'
import {
legacyRegistrar as legacyRegistrarInterfaceId,
permanentRegistrar as permanentRegistrarInterfaceId
} from '../constants/interfaces'
let ethRegistrar
let ethRegistrarRead
let permanentRegistrar
let permanentRegistrarRead
let permanentRegistrarController
let permanentRegistrarControllerRead
export const getLegacyAuctionRegistrar = async () => {
if (ethRegistrar) {
return {
ethRegistrar: ethRegistrar.methods,
ethRegistrarRead: ethRegistrarRead.methods
}
}
try {
const web3 = await getWeb3()
const web3Read = await getWeb3Read()
const { Resolver } = await getEthResolver()
let legacyAuctionRegistrarAddress = await Resolver.interfaceImplementer(
await getNamehash('eth'),
legacyRegistrarInterfaceId
).call()
ethRegistrar = new web3.eth.Contract(
legacyAuctionRegistrarContract,
legacyAuctionRegistrarAddress
)
ethRegistrarRead = new web3Read.eth.Contract(
legacyAuctionRegistrarContract,
legacyAuctionRegistrarAddress
)
return {
ethRegistrar,
ethRegistrarRead
}
} catch (e) {}
}
export const getPermanentRegistrar = async () => {
if (permanentRegistrar) {
return {
permanentRegistrar: permanentRegistrar.methods,
permanentRegistrarRead: permanentRegistrarRead.methods
}
}
try {
const { readENS: ENS } = await getENS()
const web3 = await getWeb3()
const web3Read = await getWeb3Read()
const ethAddr = await ENS.owner(await getNamehash('eth')).call()
permanentRegistrar = new web3.eth.Contract(
permanentRegistrarContract,
ethAddr
)
permanentRegistrarRead = new web3Read.eth.Contract(
permanentRegistrarContract,
ethAddr
)
return {
permanentRegistrar: permanentRegistrar.methods,
permanentRegistrarRead: permanentRegistrarRead.methods
}
} catch (e) {}
}
export const getPermanentRegistrarController = async () => {
if (permanentRegistrarController) {
return {
permanentRegistrarController: permanentRegistrarController.methods,
_permanentRegistrarController: permanentRegistrarController,
permanentRegistrarControllerRead:
permanentRegistrarControllerRead.methods,
_permanentRegistrarControllerRead: permanentRegistrarControllerRead
}
}
try {
const web3 = await getWeb3()
const web3Read = await getWeb3Read()
const { Resolver } = await getEthResolver()
let controllerAddress = await Resolver.interfaceImplementer(
await getNamehash('eth'),
permanentRegistrarInterfaceId
).call()
permanentRegistrarController = new web3.eth.Contract(
permanentRegistrarControllerContract,
controllerAddress
)
permanentRegistrarControllerRead = new web3Read.eth.Contract(
permanentRegistrarControllerContract,
controllerAddress
)
return {
permanentRegistrarController: permanentRegistrarController.methods,
_permanentRegistrarController: permanentRegistrarController,
permanentRegistrarControllerRead:
permanentRegistrarControllerRead.methods,
_permanentRegistrarControllerRead: permanentRegistrarControllerRead
}
} catch (e) {}
}
const getEthResolver = async () => {
const { readENS: ENS } = await getENS()
const ethnamehash = await getNamehash('eth')
const resolverAddr = await ENS.resolver(ethnamehash).call()
return await getResolverContract(resolverAddr)
}
export const getLegacyEntry = async name => {
let obj
try {
const { ethRegistrarRead: Registrar } = await getLegacyAuctionRegistrar()
const web3 = await getWeb3()
const namehash = web3.utils.sha3(name)
let deedOwner = '0x0'
const entry = await Registrar.methods.entries(namehash).call()
if (parseInt(entry[1], 16) !== 0) {
const deed = await getDeed(entry[1])
deedOwner = await deed.owner().call()
}
obj = {
deedOwner,
state: parseInt(entry[0]),
registrationDate: parseInt(entry[2]) * 1000,
revealDate: (parseInt(entry[2]) - 24 * 2 * 60 * 60) * 1000,
value: parseInt(entry[3]),
highestBid: parseInt(entry[4])
}
} catch (e) {
obj = {
deedOwner: '0x0',
state: 0,
registrationDate: 0,
revealDate: 0,
value: 0,
highestBid: 0,
expiryTime: 0,
error: e.message
}
}
return obj
}
export const getPermanentEntry = async name => {
let obj = {
available: null,
nameExpires: null
}
try {
const web3 = await getWeb3()
const namehash = web3.utils.sha3(name)
const { permanentRegistrarRead: Registrar } = await getPermanentRegistrar()
// Returns true if name is available
obj.available = await Registrar.available(namehash).call()
// This is used for old registrar to figure out when the name can be migrated.
obj.migrationLockPeriod = parseInt(
await Registrar.MIGRATION_LOCK_PERIOD().call()
)
obj.transferPeriodEnds = await Registrar.transferPeriodEnds().call()
// Returns registrar address if owned by new registrar
obj.ownerOf = await Registrar.ownerOf(namehash).call()
const nameExpires = await Registrar.nameExpires(namehash).call()
if (nameExpires > 0) {
obj.nameExpires = new Date(nameExpires * 1000)
}
} catch (e) {
obj.error = e.message
} finally {
return obj
}
}
export const getDeed = async address => {
const web3Read = await getWeb3Read()
const deed = new web3Read.eth.Contract(deedContract, address)
return deed.methods
}
export const getEntry = async name => {
let legacyEntry = await getLegacyEntry(name)
let block = await getBlock()
let ret = {
currentBlockDate: new Date(block.timestamp * 1000),
registrant: 0,
transferEndDate: null,
isNewRegistrar: false
}
try {
let permEntry = await getPermanentEntry(name)
if (ret.registrationDate && permEntry.migrationLockPeriod) {
ret.migrationStartDate = new Date(
ret.registrationDate + permEntry.migrationLockPeriod * 1000
)
} else {
ret.migrationStartDate = null
}
if (permEntry.transferPeriodEnds) {
ret.transferEndDate = new Date(permEntry.transferPeriodEnds * 1000)
}
ret.available = permEntry.available
if (!permEntry.available) {
// Owned
ret.state = 2
}
if (permEntry.ownerOf) {
ret.isNewRegistrar = true
ret.registrant = permEntry.ownerOf
}
if (permEntry.nameExpires) {
ret.expiryTime = permEntry.nameExpires
}
} catch (e) {
console.log('error getting permanent registry', e)
}
return {
...legacyEntry,
...ret
}
}
export const transferOwner = async ({ to, name }) => {
try {
const web3 = await getWeb3()
const nameArray = name.split('.')
const labelHash = web3.utils.sha3(nameArray[0])
const account = await getAccount()
const { permanentRegistrarRead: Registrar } = await getPermanentRegistrar()
return () =>
Registrar.safeTransferFrom(account, to, labelHash).send({
from: account
})
} catch (e) {
console.log('error getting permanentRegistrar contract', e)
}
}
export const reclaim = async ({ name, address }) => {
try {
const web3 = await getWeb3()
const nameArray = name.split('.')
const labelHash = web3.utils.sha3(nameArray[0])
const account = await getAccount()
const { permanentRegistrarRead: Registrar } = await getPermanentRegistrar()
return () =>
Registrar.reclaim(labelHash, address).send({
from: account
})
} catch (e) {
console.log('error getting permanentRegistrar contract', e)
}
}
export const getRentPrice = async (name, duration) => {
const {
permanentRegistrarControllerRead
} = await getPermanentRegistrarController()
const price = await permanentRegistrarControllerRead
.rentPrice(name, duration)
.call()
return price
}
export const getMinimumCommitmentAge = async () => {
const {
permanentRegistrarControllerRead
} = await getPermanentRegistrarController()
return await permanentRegistrarControllerRead.minCommitmentAge().call()
}
export const makeCommitment = async (name, owner, secret = '') => {
const {
permanentRegistrarControllerRead
} = await getPermanentRegistrarController()
const commitment = await permanentRegistrarControllerRead
.makeCommitment(name, owner, secret)
.call()
return commitment
}
export const commit = async (label, secret = '') => {
const {
permanentRegistrarController
} = await getPermanentRegistrarController()
const account = await getAccount()
const commitment = await makeCommitment(label, account, secret)
return () =>
permanentRegistrarController.commit(commitment).send({ from: account })
}
export const register = async (label, duration, secret) => {
const {
permanentRegistrarController
} = await getPermanentRegistrarController()
const account = await getAccount()
const price = await getRentPrice(label, duration)
return () =>
permanentRegistrarController
.register(label, account, duration, secret)
.send({ from: account, gas: 1000000, value: price })
}
export const renew = async (label, duration) => {
const {
permanentRegistrarController
} = await getPermanentRegistrarController()
const account = await getAccount()
const price = await getRentPrice(label, duration)
return () =>
permanentRegistrarController
.renew(label, duration)
.send({ from: account, gas: 1000000, value: price })
}
export const createSealedBid = async (name, bidAmount, secret) => {
const Registrar = await getLegacyAuctionRegistrar()
const web3 = await getWeb3()
const account = await getAccount()
const namehash = web3.sha3(name)
return Registrar.methods
.shaBid(
namehash,
account,
web3.utils.toWei(bidAmount, 'ether'),
web3.utils.sha3(secret)
)
.send({ from: account })
}
export const newBid = async (sealedBid, decoyBidAmount) => {
const Registrar = await getLegacyAuctionRegistrar()
const web3 = await getWeb3()
const account = await getAccount()
return Registrar.methodsnewBid(sealedBid).send({
from: account,
value: web3.utils.toWei(decoyBidAmount, 'ether')
})
}
export const startAuctionsAndBid = async (
hashes,
sealedBid,
decoyBidAmount
) => {
const Registrar = await getLegacyAuctionRegistrar()
const web3 = await getWeb3()
const account = await getAccount()
return Registrar.startAuctionsAndBid(hashes, sealedBid()).send({
from: account,
value: web3.utils.toWei(decoyBidAmount, 'ether')
})
}
export const transferRegistrars = async label => {
const { ethRegistrar } = await getLegacyAuctionRegistrar()
const account = await getAccount()
const web3 = await getWeb3()
const hash = web3.utils.sha3(label)
const tx = ethRegistrar.transferRegistrars(hash)
const gas = await tx.estimateGas({ from: account })
return () =>
tx.send({
from: account,
gas: gas
})
}
export const releaseDeed = async label => {
const { ethRegistrar } = await getLegacyAuctionRegistrar()
const account = await getAccount()
const web3 = await getWeb3()
const hash = web3.utils.sha3(label)
const tx = ethRegistrar.releaseDeed(hash)
const gas = await tx.estimateGas({ from: account })
return () =>
tx.send({
from: account,
gas: gas
})
}