ens.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. // 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. } else {
  80. parentNode, parentLabel := ensParentNode(parts[1])
  81. return crypto.Keccak256Hash(parentNode[:], parentLabel[:]), label
  82. }
  83. }
  84. func ensNode(name string) common.Hash {
  85. parentNode, parentLabel := ensParentNode(name)
  86. return crypto.Keccak256Hash(parentNode[:], parentLabel[:])
  87. }
  88. func (self *ENS) getResolver(node [32]byte) (*contract.PublicResolverSession, error) {
  89. resolverAddr, err := self.Resolver(node)
  90. if err != nil {
  91. return nil, err
  92. }
  93. resolver, err := contract.NewPublicResolver(resolverAddr, self.contractBackend)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return &contract.PublicResolverSession{
  98. Contract: resolver,
  99. TransactOpts: self.TransactOpts,
  100. }, nil
  101. }
  102. func (self *ENS) getRegistrar(node [32]byte) (*contract.FIFSRegistrarSession, error) {
  103. registrarAddr, err := self.Owner(node)
  104. if err != nil {
  105. return nil, err
  106. }
  107. registrar, err := contract.NewFIFSRegistrar(registrarAddr, self.contractBackend)
  108. if err != nil {
  109. return nil, err
  110. }
  111. return &contract.FIFSRegistrarSession{
  112. Contract: registrar,
  113. TransactOpts: self.TransactOpts,
  114. }, nil
  115. }
  116. // Resolve is a non-transactional call that returns the content hash associated with a name.
  117. func (self *ENS) Resolve(name string) (common.Hash, error) {
  118. node := ensNode(name)
  119. resolver, err := self.getResolver(node)
  120. if err != nil {
  121. return common.Hash{}, err
  122. }
  123. ret, err := resolver.Content(node)
  124. if err != nil {
  125. return common.Hash{}, err
  126. }
  127. return common.BytesToHash(ret[:]), nil
  128. }
  129. // Register registers a new domain name for the caller, making them the owner of the new name.
  130. // Only works if the registrar for the parent domain implements the FIFS registrar protocol.
  131. func (self *ENS) Register(name string) (*types.Transaction, error) {
  132. parentNode, label := ensParentNode(name)
  133. registrar, err := self.getRegistrar(parentNode)
  134. if err != nil {
  135. return nil, err
  136. }
  137. return registrar.Contract.Register(&self.TransactOpts, label, self.TransactOpts.From)
  138. }
  139. // SetContentHash sets the content hash associated with a name. Only works if the caller
  140. // owns the name, and the associated resolver implements a `setContent` function.
  141. func (self *ENS) SetContentHash(name string, hash common.Hash) (*types.Transaction, error) {
  142. node := ensNode(name)
  143. resolver, err := self.getResolver(node)
  144. if err != nil {
  145. return nil, err
  146. }
  147. opts := self.TransactOpts
  148. opts.GasLimit = 200000
  149. return resolver.Contract.SetContent(&opts, node, hash)
  150. }