-
Notifications
You must be signed in to change notification settings - Fork 37
/
name_test.go
596 lines (503 loc) · 21.7 KB
/
name_test.go
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
// Copyright 2019, 2022 Weald Technology Trading
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ens
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"math/rand"
"os"
"strings"
"testing"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
string2eth "github.com/wealdtech/go-string2eth"
)
func TestName(t *testing.T) {
dsRegistrant := common.HexToAddress("b6E040C9ECAaE172a89bD561c5F73e1C48d28cd9")
dsController := common.HexToAddress("0904Dac3347eA47d208F3Fd67402D039a3b99859")
dsResolver := common.HexToAddress("231b0Ee14048e9dCcD1d247744d114a4EB5E8E63")
dsExpiry := time.Unix(1904119920, 0)
dsRegistrationInterval := 60 * time.Second
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
name, err := NewName(client, "resolver.eth")
require.Nil(t, err, "Failed to create name")
registrant, err := name.Registrant()
require.Nil(t, err, "Failed to obtain registrant")
assert.Equal(t, dsRegistrant, registrant, "Failed to obtain correct registrant")
controller, err := name.Controller()
require.Nil(t, err, "Failed to obtain controller")
assert.Equal(t, dsController, controller, "Failed to obtain correct controller")
expiry, err := name.Expires()
require.Nil(t, err, "Failed to obtain expiry")
assert.Equal(t, dsExpiry, expiry, "Failed to obtain correct expiry")
registrationInterval, err := name.RegistrationInterval()
require.Nil(t, err, "Failed to obtain registration interval")
assert.Equal(t, dsRegistrationInterval, registrationInterval, "Failed to obtain correct registration interval")
resolverAddress, err := name.ResolverAddress()
require.Nil(t, err, "Failed to obtain resolver address")
assert.Equal(t, dsResolver, resolverAddress, "Failed to obtain correct resolver address")
}
func TestNameExpiry(t *testing.T) {
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
domain := unregisteredDomain(client)
name, err := NewName(client, domain)
require.Nil(t, err, "Failed to create name")
_, err = name.Expires()
assert.Equal(t, err.Error(), "not registered")
}
func TestNameReRegistration(t *testing.T) {
registrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(registrant) {
t.Skip()
}
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
name, err := NewName(client, "resolver.eth")
require.Nil(t, err, "Failed to create name")
// Register stage 1 - should fail as already registered
opts, err := generateTxOpts(client, registrant, "0")
require.Nil(t, err, "Failed to generate transaction options")
_, _, err = name.RegisterStageOne(registrant, opts)
require.EqualError(t, err, "name is already registered")
}
func TestInvalidName(t *testing.T) {
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
_, err := NewName(client, "ab.eth")
require.Equal(t, err.Error(), "name is not valid according to the rules of the registrar (too short, invalid characters, etc.)")
}
func TestNameRegistration(t *testing.T) {
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
registrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(registrant) {
t.Skip()
}
domain := unregisteredDomain(client)
name, err := NewName(client, domain)
require.Nil(t, err, "Failed to create name")
// Register stage 1
opts, err := generateTxOpts(client, registrant, "0")
require.Nil(t, err, "Failed to generate transaction options")
tx, secret, err := name.RegisterStageOne(registrant, opts)
require.Nil(t, err, "Failed to send stage one transaction")
// Wait until mined
waitForTransaction(client, tx.Hash())
// Wait until ready to submit stage 2
interval, err := name.RegistrationInterval()
require.Nil(t, err, "Failed to obtain registration interval")
time.Sleep(interval)
// Sleep for 1 more minute to ensure we are over the interval
time.Sleep(60 * time.Second)
// Register stage 2
opts, err = generateTxOpts(client, registrant, "0.1 Ether")
require.Nil(t, err, "Failed to generate transaction options")
tx, err = name.RegisterStageTwo(registrant, secret, opts)
require.Nil(t, err, "Failed to send stage two transaction")
// Wait until mined
waitForTransaction(client, tx.Hash())
// Confirm registered
registeredRegistrant, err := name.Registrant()
require.Nil(t, err, "Failed to obtain registrant")
assert.Equal(t, registrant, registeredRegistrant, "failed to register name")
}
func TestNameRegistrationStageTwoNoStageOne(t *testing.T) {
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
registrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(registrant) {
t.Skip()
}
domain := unregisteredDomain(client)
name, err := NewName(client, domain)
require.Nil(t, err, "Failed to create name")
// Register stage 2
opts, err := generateTxOpts(client, registrant, "0.1 Ether")
require.Nil(t, err, "Failed to generate transaction options")
var secret [32]byte
_, err = name.RegisterStageTwo(registrant, secret, opts)
require.Equal(t, err.Error(), "stage 2 attempted prior to successful stage 1 transaction")
}
func TestNameRegistrationNoValue(t *testing.T) {
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
registrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(registrant) {
t.Skip()
}
domain := unregisteredDomain(client)
name, err := NewName(client, domain)
require.Nil(t, err, "Failed to create name")
// Register stage 1
opts, err := generateTxOpts(client, registrant, "0")
require.Nil(t, err, "Failed to generate transaction options")
tx, secret, err := name.RegisterStageOne(registrant, opts)
require.Nil(t, err, "Failed to send stage one transaction")
// Wait until mined
waitForTransaction(client, tx.Hash())
// Wait until ready to submit stage 2
interval, err := name.RegistrationInterval()
require.Nil(t, err, "Failed to obtain registration interval")
time.Sleep(interval)
// Sleep for 1 more minute to ensure we are over the interval
time.Sleep(60 * time.Second)
// Register stage 2 - no value
opts, err = generateTxOpts(client, registrant, "0")
require.Nil(t, err, "Failed to generate transaction options")
_, err = name.RegisterStageTwo(registrant, secret, opts)
assert.Equal(t, err.Error(), "not enough funds to cover minimum duration of 672h0m0s")
}
func TestNameRegistrationNoInterval(t *testing.T) {
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
registrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(registrant) {
t.Skip()
}
domain := unregisteredDomain(client)
name, err := NewName(client, domain)
require.Nil(t, err, "Failed to create name")
// Register stage 1
opts, err := generateTxOpts(client, registrant, "0")
require.Nil(t, err, "Failed to generate transaction options")
tx, secret, err := name.RegisterStageOne(registrant, opts)
require.Nil(t, err, "Failed to send stage one transaction")
// Wait until mined
waitForTransaction(client, tx.Hash())
// Register stage 2 immediately - should fail
opts, err = generateTxOpts(client, registrant, "0.1 Ether")
require.Nil(t, err, "Failed to generate transaction options")
_, err = name.RegisterStageTwo(registrant, secret, opts)
require.NotNil(t, err, "No error when trying to register stage 2 immediately")
assert.Equal(t, err.Error(), "too early to send second transaction")
}
func TestNameExtension(t *testing.T) {
registrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(registrant) {
t.Skip()
}
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
name, err := NewName(client, "foobar5.eth")
require.Nil(t, err, "Failed to create name")
oldExpires, err := name.Expires()
require.Nil(t, err, "Failed to obtain old expires")
opts, err := generateTxOpts(client, registrant, "0.001Ether")
require.Nil(t, err, "Failed to generate transaction options")
tx, err := name.ExtendRegistration(opts)
require.Nil(t, err, "Failed to send transaction")
// Wait until mined
waitForTransaction(client, tx.Hash())
// Confirm expiry has increased
newExpires, err := name.Expires()
require.Nil(t, err, "Failed to obtain new expires")
assert.True(t, newExpires.After(oldExpires), "Failed to increase registration period")
}
func TestNameExtensionLowValue(t *testing.T) {
registrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(registrant) {
t.Skip()
}
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
name, err := NewName(client, "foobar5.eth")
require.Nil(t, err, "Failed to create name")
opts, err := generateTxOpts(client, registrant, "1 wei")
require.Nil(t, err, "Failed to generate transaction options")
_, err = name.ExtendRegistration(opts)
assert.Equal(t, err.Error(), "not enough funds to extend the registration")
}
func TestNameExtensionNotRegistered(t *testing.T) {
registrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(registrant) {
t.Skip()
}
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
domain := unregisteredDomain(client)
name, err := NewName(client, domain)
require.Nil(t, err, "Failed to create name")
opts, err := generateTxOpts(client, registrant, "0.001Ether")
require.Nil(t, err, "Failed to generate transaction options")
_, err = name.ExtendRegistration(opts)
assert.Equal(t, err.Error(), "name is not registered")
}
func TestNameSubdomainCreate(t *testing.T) {
dsRegistrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(dsRegistrant) {
t.Skip()
}
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
name, err := NewName(client, "foobar5.eth")
require.Nil(t, err, "Failed to create name")
sub := unregisteredDomain(client)
sub = strings.TrimSuffix(sub, ".eth")
opts, err := generateTxOpts(client, dsRegistrant, "0")
require.Nil(t, err, "Failed to generate transaction options")
tx, err := name.CreateSubdomain(sub, dsRegistrant, opts)
require.Nil(t, err, "Failed to send transaction")
// Wait until mined
waitForTransaction(client, tx.Hash())
// Confirm registrantship of the subdomain
subdomain := fmt.Sprintf("%s.foobar5.eth", sub)
registry, err := NewRegistry(client)
require.Nil(t, err, "Failed to create registry")
controller, err := registry.Owner(subdomain)
require.Nil(t, err, "Failed to obtain subname's controller")
assert.Equal(t, dsRegistrant, controller, "Unexpected controller for %s", subdomain)
}
func TestNameSubdomainCreateAlreadyExists(t *testing.T) {
dsRegistrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(dsRegistrant) {
t.Skip()
}
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
name, err := NewName(client, "foobar5.eth")
require.Nil(t, err, "Failed to create name")
sub := "go-ens-test-1331354196"
opts, err := generateTxOpts(client, dsRegistrant, "0")
require.Nil(t, err, "Failed to generate transaction options")
_, err = name.CreateSubdomain(sub, dsRegistrant, opts)
require.NotNil(t, err, "Failed to error when it should")
assert.Equal(t, "that subdomain already exists", err.Error())
}
func TestSetController(t *testing.T) {
dsRegistrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(dsRegistrant) {
t.Skip()
}
dsController := common.HexToAddress("E195c59BCF26fD36c82d1C720860127A5c1c4040")
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
name, err := NewName(client, "foobar5.eth")
require.Nil(t, err, "Failed to create name")
// Ensure that the registrant starts out as the controller
controller, err := name.Controller()
require.Nil(t, err, "Failed to obtain controller")
require.Equal(t, dsRegistrant, controller, "Initial controller incorrect")
// Set the controller
opts, err := generateTxOpts(client, dsRegistrant, "0")
require.Nil(t, err, "Failed to generate transaction options")
tx, err := name.SetController(dsController, opts)
require.Nil(t, err, "Failed to generate transaction")
waitForTransaction(client, tx.Hash())
// Confirm the controller is set
controller, err = name.Controller()
require.Nil(t, err, "Failed to obtain controller (2)")
require.Equal(t, dsController, controller, "Updated controller incorrect")
// Reset the controller role
opts, err = generateTxOpts(client, dsRegistrant, "0")
require.Nil(t, err, "Failed to generate transaction options")
tx, err = name.SetController(dsRegistrant, opts)
require.Nil(t, err, "Failed to generate transaction (2)")
waitForTransaction(client, tx.Hash())
// Confirm the controller is reset
controller, err = name.Controller()
require.Nil(t, err, "Failed to obtain controller (3)")
require.Equal(t, dsRegistrant, controller, "Reset controller incorrect")
}
func TestSetControllerUnauthorised(t *testing.T) {
dsRegistrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(dsRegistrant) {
t.Skip()
}
dsThief := common.HexToAddress("E195c59BCF26fD36c82d1C720860127A5c1c4040")
if !hasPrivateKey(dsThief) {
t.Skip()
}
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
name, err := NewName(client, "foobar5.eth")
require.Nil(t, err, "Failed to create name")
// Ensure that the registrant starts out as the controller
controller, err := name.Controller()
require.Nil(t, err, "Failed to obtain controller")
require.Equal(t, dsRegistrant, controller, "Initial controller incorrect")
// Try to set the controller
opts, err := generateTxOpts(client, dsThief, "0")
require.Nil(t, err, "Failed to generate transaction options")
_, err = name.SetController(dsRegistrant, opts)
require.NotNil(t, err, "Failed to error when it should")
assert.Equal(t, "not authorised to change the controller", err.Error())
}
func TestReclaim(t *testing.T) {
dsRegistrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(dsRegistrant) {
t.Skip()
}
dsController := common.HexToAddress("E195c59BCF26fD36c82d1C720860127A5c1c4040")
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
name, err := NewName(client, "foobar5.eth")
require.Nil(t, err, "Failed to create name")
// Ensure that the registrant starts out as the controller
controller, err := name.Controller()
require.Nil(t, err, "Failed to obtain controller")
require.Equal(t, dsRegistrant, controller, "Initial controller incorrect")
// Set the controller
opts, err := generateTxOpts(client, dsRegistrant, "0")
require.Nil(t, err, "Failed to generate transaction options")
tx, err := name.SetController(dsController, opts)
require.Nil(t, err, "Failed to generate transaction")
waitForTransaction(client, tx.Hash())
// Confirm the controller is set
controller, err = name.Controller()
require.Nil(t, err, "Failed to obtain controller (2)")
require.Equal(t, dsController, controller, "Updated controller incorrect")
// Set the controller role
opts, err = generateTxOpts(client, dsRegistrant, "0")
require.Nil(t, err, "Failed to generate transaction options")
tx, err = name.Reclaim(opts)
require.Nil(t, err, "Failed to generate transaction (2)")
waitForTransaction(client, tx.Hash())
// Confirm the controller is reset
controller, err = name.Controller()
require.Nil(t, err, "Failed to obtain controller (3)")
require.Equal(t, dsRegistrant, controller, "Reset controller incorrect")
}
func TestReclaimUnauthorised(t *testing.T) {
dsRegistrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
dsThief := common.HexToAddress("E195c59BCF26fD36c82d1C720860127A5c1c4040")
if !hasPrivateKey(dsThief) {
t.Skip()
}
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
name, err := NewName(client, "foobar5.eth")
require.Nil(t, err, "Failed to create name")
// Ensure that the registrant starts out as the controller
controller, err := name.Controller()
require.Nil(t, err, "Failed to obtain controller")
require.Equal(t, dsRegistrant, controller, "Initial controller incorrect")
// Try to reclaim
opts, err := generateTxOpts(client, dsThief, "0")
require.Nil(t, err, "Failed to generate transaction options")
_, err = name.Reclaim(opts)
require.NotNil(t, err, "Failed to error when it should")
assert.Equal(t, "not the registrant", err.Error())
}
func TestTransfer(t *testing.T) {
dsRegistrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
if !hasPrivateKey(dsRegistrant) {
t.Skip()
}
dsNewRegistrant := common.HexToAddress("E195c59BCF26fD36c82d1C720860127A5c1c4040")
if !hasPrivateKey(dsNewRegistrant) {
t.Skip()
}
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
name, err := NewName(client, "foobar5.eth")
require.Nil(t, err, "Failed to create name")
// Ensure the existing registrant is correct
registrant, err := name.Registrant()
require.Nil(t, err, "Failed to obtain registrant")
require.Equal(t, dsRegistrant, registrant, "Initial registrant incorrect")
// Set the registrant
opts, err := generateTxOpts(client, dsRegistrant, "0")
require.Nil(t, err, "Failed to generate transaction options")
tx, err := name.Transfer(dsNewRegistrant, opts)
require.Nil(t, err, "Failed to generate transaction")
waitForTransaction(client, tx.Hash())
// Confirm the new registrant is set
registrant, err = name.Registrant()
require.Nil(t, err, "Failed to obtain registrant (2)")
require.Equal(t, dsNewRegistrant, registrant, "Updated registrant incorrect")
// Reset the registrant
opts, err = generateTxOpts(client, dsNewRegistrant, "0")
require.Nil(t, err, "Failed to generate transaction options")
tx, err = name.Transfer(dsRegistrant, opts)
require.Nil(t, err, "Failed to generate transaction (2)")
waitForTransaction(client, tx.Hash())
// Confirm the registrant is reset
registrant, err = name.Registrant()
require.Nil(t, err, "Failed to obtain registrant (3)")
require.Equal(t, dsRegistrant, registrant, "Reset registrant incorrect")
}
func TestTransferUnauthorised(t *testing.T) {
dsRegistrant := common.HexToAddress("388Ea662EF2c223eC0B047D41Bf3c0f362142ad5")
dsThief := common.HexToAddress("E195c59BCF26fD36c82d1C720860127A5c1c4040")
if !hasPrivateKey(dsThief) {
t.Skip()
}
client, _ := ethclient.Dial("https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
name, err := NewName(client, "foobar5.eth")
require.Nil(t, err, "Failed to create name")
// Ensure that the registrant starts out as the controller
registrant, err := name.Registrant()
require.Nil(t, err, "Failed to obtain registrant")
require.Equal(t, dsRegistrant, registrant, "Initial registrant incorrect")
// Try to steal
opts, err := generateTxOpts(client, dsThief, "0")
require.Nil(t, err, "Failed to generate transaction options")
_, err = name.Transfer(dsThief, opts)
require.NotNil(t, err, "Failed to error when it should")
assert.Equal(t, "not the current registrant", err.Error())
}
func generateTxOpts(client *ethclient.Client, sender common.Address, valueStr string) (*bind.TransactOpts, error) {
key, err := crypto.HexToECDSA(os.Getenv(fmt.Sprintf("PRIVATE_KEY_%x", sender)))
if err != nil {
return nil, fmt.Errorf("Failed to obtain private key for %x", sender)
}
signer := keySigner(big.NewInt(3), key)
if signer == nil {
return nil, errors.New("no signer")
}
value, err := string2eth.StringToWei(valueStr)
if err != nil {
return nil, err
}
curNonce, err := client.PendingNonceAt(context.Background(), sender)
if err != nil {
return nil, err
}
nonce := int64(curNonce)
opts := &bind.TransactOpts{
From: sender,
Signer: signer,
GasPrice: big.NewInt(10000000000),
Value: value,
Nonce: big.NewInt(0).SetInt64(nonce),
}
return opts, nil
}
func keySigner(chainID *big.Int, key *ecdsa.PrivateKey) bind.SignerFn {
return func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
keyAddr := crypto.PubkeyToAddress(key.PublicKey)
if address != keyAddr {
return nil, errors.New("not authorized to sign this account")
}
return types.SignTx(tx, types.NewEIP155Signer(chainID), key)
}
}
func waitForTransaction(client *ethclient.Client, txHash common.Hash) {
for {
_, pending, err := client.TransactionByHash(context.Background(), txHash)
if err == nil && !pending {
return
}
time.Sleep(5 * time.Second)
}
}
func unregisteredDomain(client *ethclient.Client) string {
rand.Seed(time.Now().UTC().UnixNano())
registry, _ := NewRegistry(client)
for {
// #nosec G404
domain := fmt.Sprintf("go-ens-test-%d.eth", rand.Int31())
controller, _ := registry.Owner(domain)
if controller == UnknownAddress {
return domain
}
}
}
func hasPrivateKey(address common.Address) bool {
_, err := crypto.HexToECDSA(os.Getenv(fmt.Sprintf("PRIVATE_KEY_%x", address)))
return err == nil
}