-
Notifications
You must be signed in to change notification settings - Fork 34
/
registry.go
178 lines (159 loc) · 5.59 KB
/
registry.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
// Copyright 2017 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 (
"errors"
"math/big"
"github.com/ethereum/go-ethereum/accounts"
"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/auctionregistrar"
"github.com/wealdtech/go-ens/v3/contracts/registry"
"github.com/wealdtech/go-ens/v3/util"
)
// Registry is the structure for the registry contract.
type Registry struct {
backend bind.ContractBackend
Contract *registry.Contract
ContractAddr common.Address
}
// NewRegistry obtains the ENS registry.
func NewRegistry(backend bind.ContractBackend) (*Registry, error) {
address, err := RegistryContractAddress(backend)
if err != nil {
return nil, err
}
return NewRegistryAt(backend, address)
}
// NewRegistryAt obtains the ENS registry at a given address.
func NewRegistryAt(backend bind.ContractBackend, address common.Address) (*Registry, error) {
contract, err := registry.NewContract(address, backend)
if err != nil {
return nil, err
}
return &Registry{
backend: backend,
Contract: contract,
ContractAddr: address,
}, nil
}
// Owner returns the address of the owner of a name.
func (r *Registry) Owner(name string) (common.Address, error) {
nameHash, err := NameHash(name)
if err != nil {
return UnknownAddress, err
}
return r.Contract.Owner(nil, nameHash)
}
// ResolverAddress returns the address of the resolver for a name.
func (r *Registry) ResolverAddress(name string) (common.Address, error) {
nameHash, err := NameHash(name)
if err != nil {
return UnknownAddress, err
}
return r.Contract.Resolver(nil, nameHash)
}
// SetResolver sets the resolver for a name.
func (r *Registry) SetResolver(opts *bind.TransactOpts, name string, address common.Address) (*types.Transaction, error) {
nameHash, err := NameHash(name)
if err != nil {
return nil, err
}
return r.Contract.SetResolver(opts, nameHash, address)
}
// Resolver returns the resolver for a name.
func (r *Registry) Resolver(name string) (*Resolver, error) {
address, err := r.ResolverAddress(name)
if err != nil {
return nil, err
}
return NewResolverAt(r.backend, name, address)
}
// SetOwner sets the ownership of a domain.
func (r *Registry) SetOwner(opts *bind.TransactOpts, name string, address common.Address) (*types.Transaction, error) {
nameHash, err := NameHash(name)
if err != nil {
return nil, err
}
return r.Contract.SetOwner(opts, nameHash, address)
}
// SetSubdomainOwner sets the ownership of a subdomain, potentially creating it in the process.
func (r *Registry) SetSubdomainOwner(opts *bind.TransactOpts, name string, subname string, address common.Address) (*types.Transaction, error) {
nameHash, err := NameHash(name)
if err != nil {
return nil, err
}
labelHash, err := LabelHash(subname)
if err != nil {
return nil, err
}
return r.Contract.SetSubnodeOwner(opts, nameHash, labelHash, address)
}
// RegistryContractAddress obtains the address of the registry contract for a chain.
// This is (currently) the same for all chains.
func RegistryContractAddress(_ bind.ContractBackend) (common.Address, error) {
// Instantiate the registry contract. The same for all chains.
return common.HexToAddress("00000000000C2E074eC69A0dFb2997BA6C7d2e1e"), nil
}
// RegistryContractFromRegistrar obtains the registry contract given an
// existing registrar contract.
func RegistryContractFromRegistrar(backend bind.ContractBackend, registrar *auctionregistrar.Contract) (*registry.Contract, error) {
if registrar == nil {
return nil, errors.New("no registrar contract")
}
registryAddress, err := registrar.Ens(nil)
if err != nil {
return nil, err
}
return registry.NewContract(registryAddress, backend)
}
// SetResolver sets the resolver for a name.
func SetResolver(session *registry.ContractSession, name string, resolverAddr *common.Address) (*types.Transaction, error) {
nameHash, err := NameHash(name)
if err != nil {
return nil, err
}
return session.SetResolver(nameHash, *resolverAddr)
}
// SetSubdomainOwner sets the owner for a subdomain of a name.
func SetSubdomainOwner(session *registry.ContractSession, name string, subdomain string, ownerAddr *common.Address) (*types.Transaction, error) {
nameHash, err := NameHash(name)
if err != nil {
return nil, err
}
labelHash, err := LabelHash(subdomain)
if err != nil {
return nil, err
}
return session.SetSubnodeOwner(nameHash, labelHash, *ownerAddr)
}
// CreateRegistrySession creates a session suitable for multiple calls.
func CreateRegistrySession(chainID *big.Int, wallet *accounts.Wallet, account *accounts.Account, passphrase string, contract *registry.Contract, gasPrice *big.Int) *registry.ContractSession {
// Create a signer.
signer := util.AccountSigner(chainID, wallet, account, passphrase)
// Return our session.
session := ®istry.ContractSession{
Contract: contract,
CallOpts: bind.CallOpts{
Pending: true,
},
TransactOpts: bind.TransactOpts{
From: account.Address,
Signer: signer,
GasPrice: gasPrice,
},
}
return session
}