ens.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 --pkg contract --out contract/ens.go
  18. import (
  19. "math/big"
  20. "strings"
  21. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/contracts/ens/contract"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/crypto"
  26. )
  27. var (
  28. MainNetAddress = common.HexToAddress("0x314159265dD8dbb310642f98f50C066173C1259b")
  29. TestNetAddress = common.HexToAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010")
  30. )
  31. // swarm domain name registry and resolver
  32. type ENS struct {
  33. *contract.ENSSession
  34. contractBackend bind.ContractBackend
  35. }
  36. // NewENS creates a struct exposing convenient high-level operations for interacting with
  37. // the Ethereum Name Service.
  38. func NewENS(transactOpts *bind.TransactOpts, contractAddr common.Address, contractBackend bind.ContractBackend) (*ENS, error) {
  39. ens, err := contract.NewENS(contractAddr, contractBackend)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return &ENS{
  44. &contract.ENSSession{
  45. Contract: ens,
  46. TransactOpts: *transactOpts,
  47. },
  48. contractBackend,
  49. }, nil
  50. }
  51. // DeployENS deploys an instance of the ENS nameservice, with a 'first-in, first-served' root registrar.
  52. func DeployENS(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend) (*ENS, error) {
  53. // Deploy the ENS registry
  54. ensAddr, _, _, err := contract.DeployENS(transactOpts, contractBackend, transactOpts.From)
  55. if err != nil {
  56. return nil, err
  57. }
  58. ens, err := NewENS(transactOpts, ensAddr, contractBackend)
  59. if err != nil {
  60. return nil, err
  61. }
  62. // Deploy the registrar
  63. regAddr, _, _, err := contract.DeployFIFSRegistrar(transactOpts, contractBackend, ensAddr, [32]byte{})
  64. if err != nil {
  65. return nil, err
  66. }
  67. // Set the registrar as owner of the ENS root
  68. _, err = ens.SetOwner([32]byte{}, regAddr)
  69. if err != nil {
  70. return nil, err
  71. }
  72. return 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. opts := self.TransactOpts
  138. opts.GasLimit = big.NewInt(200000)
  139. return registrar.Contract.Register(&opts, label, self.TransactOpts.From)
  140. }
  141. // SetContentHash sets the content hash associated with a name. Only works if the caller
  142. // owns the name, and the associated resolver implements a `setContent` function.
  143. func (self *ENS) SetContentHash(name string, hash common.Hash) (*types.Transaction, error) {
  144. node := ensNode(name)
  145. resolver, err := self.getResolver(node)
  146. if err != nil {
  147. return nil, err
  148. }
  149. opts := self.TransactOpts
  150. opts.GasLimit = big.NewInt(200000)
  151. return resolver.Contract.SetContent(&opts, node, hash)
  152. }