ens.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package ens
  17. //go:generate abigen --sol contract/ENS.sol --exc contract/AbstractENS.sol:AbstractENS --pkg contract --out contract/ens.go
  18. //go:generate abigen --sol contract/FIFSRegistrar.sol --exc contract/AbstractENS.sol:AbstractENS --pkg contract --out contract/fifsregistrar.go
  19. //go:generate abigen --sol contract/PublicResolver.sol --exc contract/AbstractENS.sol:AbstractENS --pkg contract --out contract/publicresolver.go
  20. import (
  21. "strings"
  22. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/contracts/ens/contract"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. )
  28. var (
  29. MainNetAddress = common.HexToAddress("0x314159265dD8dbb310642f98f50C066173C1259b")
  30. TestNetAddress = common.HexToAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010")
  31. )
  32. // ENS is the swarm domain name registry and resolver
  33. type ENS struct {
  34. *contract.ENSSession
  35. contractBackend bind.ContractBackend
  36. }
  37. // NewENS creates a struct exposing convenient high-level operations for interacting with
  38. // the Ethereum Name Service.
  39. func NewENS(transactOpts *bind.TransactOpts, contractAddr common.Address, contractBackend bind.ContractBackend) (*ENS, error) {
  40. ens, err := contract.NewENS(contractAddr, contractBackend)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return &ENS{
  45. &contract.ENSSession{
  46. Contract: ens,
  47. TransactOpts: *transactOpts,
  48. },
  49. contractBackend,
  50. }, nil
  51. }
  52. // DeployENS deploys an instance of the ENS nameservice, with a 'first-in, first-served' root registrar.
  53. func DeployENS(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend) (common.Address, *ENS, error) {
  54. // Deploy the ENS registry
  55. ensAddr, _, _, err := contract.DeployENS(transactOpts, contractBackend)
  56. if err != nil {
  57. return ensAddr, nil, err
  58. }
  59. ens, err := NewENS(transactOpts, ensAddr, contractBackend)
  60. if err != nil {
  61. return ensAddr, nil, err
  62. }
  63. // Deploy the registrar
  64. regAddr, _, _, err := contract.DeployFIFSRegistrar(transactOpts, contractBackend, ensAddr, [32]byte{})
  65. if err != nil {
  66. return ensAddr, nil, err
  67. }
  68. // Set the registrar as owner of the ENS root
  69. if _, err = ens.SetOwner([32]byte{}, regAddr); err != nil {
  70. return ensAddr, nil, err
  71. }
  72. return ensAddr, ens, nil
  73. }
  74. func ensParentNode(name string) (common.Hash, common.Hash) {
  75. parts := strings.SplitN(name, ".", 2)
  76. label := crypto.Keccak256Hash([]byte(parts[0]))
  77. if len(parts) == 1 {
  78. return [32]byte{}, label
  79. }
  80. parentNode, parentLabel := ensParentNode(parts[1])
  81. return crypto.Keccak256Hash(parentNode[:], parentLabel[:]), label
  82. }
  83. func EnsNode(name string) common.Hash {
  84. parentNode, parentLabel := ensParentNode(name)
  85. return crypto.Keccak256Hash(parentNode[:], parentLabel[:])
  86. }
  87. func (ens *ENS) getResolver(node [32]byte) (*contract.PublicResolverSession, error) {
  88. resolverAddr, err := ens.Resolver(node)
  89. if err != nil {
  90. return nil, err
  91. }
  92. resolver, err := contract.NewPublicResolver(resolverAddr, ens.contractBackend)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return &contract.PublicResolverSession{
  97. Contract: resolver,
  98. TransactOpts: ens.TransactOpts,
  99. }, nil
  100. }
  101. func (ens *ENS) getRegistrar(node [32]byte) (*contract.FIFSRegistrarSession, error) {
  102. registrarAddr, err := ens.Owner(node)
  103. if err != nil {
  104. return nil, err
  105. }
  106. registrar, err := contract.NewFIFSRegistrar(registrarAddr, ens.contractBackend)
  107. if err != nil {
  108. return nil, err
  109. }
  110. return &contract.FIFSRegistrarSession{
  111. Contract: registrar,
  112. TransactOpts: ens.TransactOpts,
  113. }, nil
  114. }
  115. // Resolve is a non-transactional call that returns the content hash associated with a name.
  116. func (ens *ENS) Resolve(name string) (common.Hash, error) {
  117. node := EnsNode(name)
  118. resolver, err := ens.getResolver(node)
  119. if err != nil {
  120. return common.Hash{}, err
  121. }
  122. ret, err := resolver.Content(node)
  123. if err != nil {
  124. return common.Hash{}, err
  125. }
  126. return common.BytesToHash(ret[:]), nil
  127. }
  128. // Addr is a non-transactional call that returns the address associated with a name.
  129. func (ens *ENS) Addr(name string) (common.Address, error) {
  130. node := EnsNode(name)
  131. resolver, err := ens.getResolver(node)
  132. if err != nil {
  133. return common.Address{}, err
  134. }
  135. ret, err := resolver.Addr(node)
  136. if err != nil {
  137. return common.Address{}, err
  138. }
  139. return common.BytesToAddress(ret[:]), nil
  140. }
  141. // SetAddress sets the address associated with a name. Only works if the caller
  142. // owns the name, and the associated resolver implements a `setAddress` function.
  143. func (ens *ENS) SetAddr(name string, addr common.Address) (*types.Transaction, error) {
  144. node := EnsNode(name)
  145. resolver, err := ens.getResolver(node)
  146. if err != nil {
  147. return nil, err
  148. }
  149. opts := ens.TransactOpts
  150. opts.GasLimit = 200000
  151. return resolver.Contract.SetAddr(&opts, node, addr)
  152. }
  153. // Register registers a new domain name for the caller, making them the owner of the new name.
  154. // Only works if the registrar for the parent domain implements the FIFS registrar protocol.
  155. func (ens *ENS) Register(name string) (*types.Transaction, error) {
  156. parentNode, label := ensParentNode(name)
  157. registrar, err := ens.getRegistrar(parentNode)
  158. if err != nil {
  159. return nil, err
  160. }
  161. return registrar.Contract.Register(&ens.TransactOpts, label, ens.TransactOpts.From)
  162. }
  163. // SetContentHash sets the content hash associated with a name. Only works if the caller
  164. // owns the name, and the associated resolver implements a `setContent` function.
  165. func (ens *ENS) SetContentHash(name string, hash common.Hash) (*types.Transaction, error) {
  166. node := EnsNode(name)
  167. resolver, err := ens.getResolver(node)
  168. if err != nil {
  169. return nil, err
  170. }
  171. opts := ens.TransactOpts
  172. opts.GasLimit = 200000
  173. return resolver.Contract.SetContent(&opts, node, hash)
  174. }