forked from wealdtech/go-ens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseregistrar.go
214 lines (194 loc) · 6.05 KB
/
baseregistrar.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
// Copyright 2019 - 2023 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 (
"bytes"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/wealdtech/go-ens/v3/contracts/baseregistrar"
"golang.org/x/crypto/sha3"
)
// BaseRegistrar is the structure for the registrar.
type BaseRegistrar struct {
backend bind.ContractBackend
domain string
Contract *baseregistrar.Contract
ContractAddr common.Address
}
// NewBaseRegistrar obtains the registrar contract for a given domain.
func NewBaseRegistrar(backend bind.ContractBackend, domain string, chainId ChainId) (*BaseRegistrar, error) {
address, err := RegistrarContractAddress(backend, domain, chainId)
if err != nil {
return nil, err
}
if address == UnknownAddress {
return nil, fmt.Errorf("no registrar for domain %s", domain)
}
contract, err := baseregistrar.NewContract(address, backend)
if err != nil {
return nil, err
}
supported, err := contract.SupportsInterface(nil, [4]byte{0x28, 0xed, 0x4f, 0x6c})
if err != nil {
return nil, err
}
if !supported {
return nil, fmt.Errorf("purported registrar for domain %s does not support reclaim functionality", domain)
}
return &BaseRegistrar{
backend: backend,
domain: domain,
Contract: contract,
ContractAddr: address,
}, nil
}
// PriorAuctionContract obtains the previous (auction) registrar contract.
func (r *BaseRegistrar) PriorAuctionContract() (*AuctionRegistrar, error) {
address, err := r.Contract.PreviousRegistrar(nil)
if err != nil {
// Means there is no prior registrar.
//nolint:nilerr
return nil, nil
}
auctionContract, err := NewAuctionRegistrarAt(r.backend, r.domain, address)
if err != nil {
return nil, errors.New("failed to instantiate prior auction contract")
}
// Confirm ths really is an auction contract by trying to create (but not submit) a bid.
var shaBid [32]byte
var emptyHash [32]byte
sha := sha3.NewLegacyKeccak256()
if _, err := sha.Write(emptyHash[:]); err != nil {
return nil, err
}
if _, err := sha.Write(UnknownAddress.Bytes()); err != nil {
return nil, err
}
var amountBytes [32]byte
if _, err := sha.Write(amountBytes[:]); err != nil {
return nil, err
}
if _, err := sha.Write(emptyHash[:]); err != nil {
return nil, err
}
sha.Sum(shaBid[:0])
contractShaBid, err := auctionContract.ShaBid(emptyHash, UnknownAddress, big.NewInt(0), emptyHash)
if err != nil {
return nil, errors.New("failed to confirm auction contract")
}
if !bytes.Equal(contractShaBid[:], shaBid[:]) {
return nil, errors.New("failed to confirm auction contract")
}
return auctionContract, nil
}
// RegisteredWith returns one of "temporary", "permanent" or "none" for the
// registrar on which this name is registered.
func (r *BaseRegistrar) RegisteredWith(domain string) (string, error) {
name, err := UnqualifiedName(domain, r.domain)
if err != nil {
return "", err
}
// See if we're registered at all - fetch the owner to find out.
registry, err := NewRegistry(r.backend, EthereumMainnet)
if err != nil {
return "", err
}
owner, err := registry.Owner(domain)
if err != nil {
return "", err
}
// Fetch the temporary registrar and see if we're registered there.
auctionRegistrar, err := r.PriorAuctionContract()
if err != nil {
return "", err
}
if auctionRegistrar != nil {
state, err := auctionRegistrar.State(name)
if err != nil {
return "", err
}
if state == "Won" || state == "Owned" {
return "temporary", nil
}
}
// No temporary registrar or no entry in same.
if owner == UnknownAddress {
return "none", nil
}
return "permanent", nil
}
// Owner obtains the owner of the underlying token that represents the name.
func (r *BaseRegistrar) Owner(domain string) (common.Address, error) {
name, err := UnqualifiedName(domain, r.domain)
if err != nil {
return UnknownAddress, err
}
labelHash, err := LabelHash(name)
if err != nil {
return UnknownAddress, err
}
owner, err := r.Contract.OwnerOf(nil, new(big.Int).SetBytes(labelHash[:]))
// Registrar reverts rather than provide a 0 owner, so...
if err != nil && err.Error() == "execution reverted" {
return UnknownAddress, nil
}
return owner, err
}
// SetOwner sets the owner of the token holding the name.
func (r *BaseRegistrar) SetOwner(opts *bind.TransactOpts, domain string, newOwner common.Address) (*types.Transaction, error) {
name, err := UnqualifiedName(domain, r.domain)
if err != nil {
return nil, err
}
owner, err := r.Owner(name)
if err != nil {
return nil, err
}
labelHash, err := LabelHash(name)
if err != nil {
return nil, err
}
id := new(big.Int).SetBytes(labelHash[:])
return r.Contract.TransferFrom(opts, owner, newOwner, id)
}
// Expiry obtains the unix timestamp at which the registration expires.
func (r *BaseRegistrar) Expiry(domain string) (*big.Int, error) {
name, err := UnqualifiedName(domain, r.domain)
if err != nil {
return nil, err
}
labelHash, err := LabelHash(name)
if err != nil {
return nil, err
}
id := new(big.Int).SetBytes(labelHash[:])
return r.Contract.NameExpires(nil, id)
}
// Reclaim reclaims a domain by the owner.
func (r *BaseRegistrar) Reclaim(opts *bind.TransactOpts, domain string, newOwner common.Address) (*types.Transaction, error) {
name, err := UnqualifiedName(domain, r.domain)
if err != nil {
return nil, err
}
labelHash, err := LabelHash(name)
if err != nil {
return nil, err
}
id := new(big.Int).SetBytes(labelHash[:])
return r.Contract.Reclaim(opts, id, newOwner)
}