ens.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "strings"
  20. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/contracts/ens/contract"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. )
  26. var (
  27. MainNetAddress = common.HexToAddress("0x314159265dD8dbb310642f98f50C066173C1259b")
  28. TestNetAddress = common.HexToAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010")
  29. )
  30. // swarm domain name registry and resolver
  31. type ENS struct {
  32. *contract.ENSSession
  33. contractBackend bind.ContractBackend
  34. }
  35. // NewENS creates a struct exposing convenient high-level operations for interacting with
  36. // the Ethereum Name Service.
  37. func NewENS(transactOpts *bind.TransactOpts, contractAddr common.Address, contractBackend bind.ContractBackend) (*ENS, error) {
  38. ens, err := contract.NewENS(contractAddr, contractBackend)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return &ENS{
  43. &contract.ENSSession{
  44. Contract: ens,
  45. TransactOpts: *transactOpts,
  46. },
  47. contractBackend,
  48. }, nil
  49. }
  50. // DeployENS deploys an instance of the ENS nameservice, with a 'first-in, first-served' root registrar.
  51. func DeployENS(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend) (*ENS, error) {
  52. // Deploy the ENS registry
  53. ensAddr, _, _, err := contract.DeployENS(transactOpts, contractBackend, transactOpts.From)
  54. if err != nil {
  55. return nil, err
  56. }
  57. ens, err := NewENS(transactOpts, ensAddr, contractBackend)
  58. if err != nil {
  59. return nil, err
  60. }
  61. // Deploy the registrar
  62. regAddr, _, _, err := contract.DeployFIFSRegistrar(transactOpts, contractBackend, ensAddr, [32]byte{})
  63. if err != nil {
  64. return nil, err
  65. }
  66. // Set the registrar as owner of the ENS root
  67. _, err = ens.SetOwner([32]byte{}, regAddr)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return ens, nil
  72. }
  73. func ensParentNode(name string) (common.Hash, common.Hash) {
  74. parts := strings.SplitN(name, ".", 2)
  75. label := crypto.Keccak256Hash([]byte(parts[0]))
  76. if len(parts) == 1 {
  77. return [32]byte{}, label
  78. } else {
  79. parentNode, parentLabel := ensParentNode(parts[1])
  80. return crypto.Keccak256Hash(parentNode[:], parentLabel[:]), label
  81. }
  82. }
  83. func ensNode(name string) common.Hash {
  84. parentNode, parentLabel := ensParentNode(name)
  85. return crypto.Keccak256Hash(parentNode[:], parentLabel[:])
  86. }
  87. func (self *ENS) getResolver(node [32]byte) (*contract.PublicResolverSession, error) {
  88. resolverAddr, err := self.Resolver(node)
  89. if err != nil {
  90. return nil, err
  91. }
  92. resolver, err := contract.NewPublicResolver(resolverAddr, self.contractBackend)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return &contract.PublicResolverSession{
  97. Contract: resolver,
  98. TransactOpts: self.TransactOpts,
  99. }, nil
  100. }
  101. func (self *ENS) getRegistrar(node [32]byte) (*contract.FIFSRegistrarSession, error) {
  102. registrarAddr, err := self.Owner(node)
  103. if err != nil {
  104. return nil, err
  105. }
  106. registrar, err := contract.NewFIFSRegistrar(registrarAddr, self.contractBackend)
  107. if err != nil {
  108. return nil, err
  109. }
  110. return &contract.FIFSRegistrarSession{
  111. Contract: registrar,
  112. TransactOpts: self.TransactOpts,
  113. }, nil
  114. }
  115. // Resolve is a non-transactional call that returns the content hash associated with a name.
  116. func (self *ENS) Resolve(name string) (common.Hash, error) {
  117. node := ensNode(name)
  118. resolver, err := self.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. // Register registers a new domain name for the caller, making them the owner of the new name.
  129. // Only works if the registrar for the parent domain implements the FIFS registrar protocol.
  130. func (self *ENS) Register(name string) (*types.Transaction, error) {
  131. parentNode, label := ensParentNode(name)
  132. registrar, err := self.getRegistrar(parentNode)
  133. if err != nil {
  134. return nil, err
  135. }
  136. opts := self.TransactOpts
  137. opts.GasLimit = 200000
  138. return registrar.Contract.Register(&opts, label, self.TransactOpts.From)
  139. }
  140. // SetContentHash sets the content hash associated with a name. Only works if the caller
  141. // owns the name, and the associated resolver implements a `setContent` function.
  142. func (self *ENS) SetContentHash(name string, hash common.Hash) (*types.Transaction, error) {
  143. node := ensNode(name)
  144. resolver, err := self.getResolver(node)
  145. if err != nil {
  146. return nil, err
  147. }
  148. opts := self.TransactOpts
  149. opts.GasLimit = 200000
  150. return resolver.Contract.SetContent(&opts, node, hash)
  151. }