ens.go 5.1 KB

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