ens.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. //go:generate abigen --sol contract/ENSRegistry.sol --exc contract/ENS.sol:ENS --pkg contract --out contract/ensregistry.go
  19. //go:generate abigen --sol contract/FIFSRegistrar.sol --exc contract/ENS.sol:ENS --pkg contract --out contract/fifsregistrar.go
  20. //go:generate abigen --sol contract/PublicResolver.sol --exc contract/ENS.sol:ENS --pkg contract --out contract/publicresolver.go
  21. import (
  22. "encoding/binary"
  23. "strings"
  24. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/contracts/ens/contract"
  27. "github.com/ethereum/go-ethereum/contracts/ens/fallback_contract"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/crypto"
  30. )
  31. var (
  32. MainNetAddress = common.HexToAddress("0x314159265dD8dbb310642f98f50C066173C1259b")
  33. TestNetAddress = common.HexToAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010")
  34. contentHash_Interface_Id [4]byte
  35. )
  36. const contentHash_Interface_Id_Spec = 0xbc1c58d1
  37. func init() {
  38. binary.BigEndian.PutUint32(contentHash_Interface_Id[:], contentHash_Interface_Id_Spec)
  39. }
  40. // ENS is the swarm domain name registry and resolver
  41. type ENS struct {
  42. *contract.ENSSession
  43. contractBackend bind.ContractBackend
  44. }
  45. // NewENS creates a struct exposing convenient high-level operations for interacting with
  46. // the Ethereum Name Service.
  47. func NewENS(transactOpts *bind.TransactOpts, contractAddr common.Address, contractBackend bind.ContractBackend) (*ENS, error) {
  48. ens, err := contract.NewENS(contractAddr, contractBackend)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return &ENS{
  53. &contract.ENSSession{
  54. Contract: ens,
  55. TransactOpts: *transactOpts,
  56. },
  57. contractBackend,
  58. }, nil
  59. }
  60. // DeployENS deploys an instance of the ENS nameservice, with a 'first-in, first-served' root registrar.
  61. func DeployENS(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend) (common.Address, *ENS, error) {
  62. // Deploy the ENS registry
  63. ensAddr, _, _, err := contract.DeployENSRegistry(transactOpts, contractBackend)
  64. if err != nil {
  65. return ensAddr, nil, err
  66. }
  67. ens, err := NewENS(transactOpts, ensAddr, contractBackend)
  68. if err != nil {
  69. return ensAddr, nil, err
  70. }
  71. // Deploy the registrar
  72. regAddr, _, _, err := contract.DeployFIFSRegistrar(transactOpts, contractBackend, ensAddr, [32]byte{})
  73. if err != nil {
  74. return ensAddr, nil, err
  75. }
  76. // Set the registrar as owner of the ENS root
  77. if _, err = ens.SetOwner([32]byte{}, regAddr); err != nil {
  78. return ensAddr, nil, err
  79. }
  80. return ensAddr, ens, nil
  81. }
  82. func ensParentNode(name string) (common.Hash, common.Hash) {
  83. parts := strings.SplitN(name, ".", 2)
  84. label := crypto.Keccak256Hash([]byte(parts[0]))
  85. if len(parts) == 1 {
  86. return [32]byte{}, label
  87. }
  88. parentNode, parentLabel := ensParentNode(parts[1])
  89. return crypto.Keccak256Hash(parentNode[:], parentLabel[:]), label
  90. }
  91. func EnsNode(name string) common.Hash {
  92. parentNode, parentLabel := ensParentNode(name)
  93. return crypto.Keccak256Hash(parentNode[:], parentLabel[:])
  94. }
  95. func (ens *ENS) getResolver(node [32]byte) (*contract.PublicResolverSession, error) {
  96. resolverAddr, err := ens.Resolver(node)
  97. if err != nil {
  98. return nil, err
  99. }
  100. resolver, err := contract.NewPublicResolver(resolverAddr, ens.contractBackend)
  101. if err != nil {
  102. return nil, err
  103. }
  104. return &contract.PublicResolverSession{
  105. Contract: resolver,
  106. TransactOpts: ens.TransactOpts,
  107. }, nil
  108. }
  109. func (ens *ENS) getFallbackResolver(node [32]byte) (*fallback_contract.PublicResolverSession, error) {
  110. resolverAddr, err := ens.Resolver(node)
  111. if err != nil {
  112. return nil, err
  113. }
  114. resolver, err := fallback_contract.NewPublicResolver(resolverAddr, ens.contractBackend)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return &fallback_contract.PublicResolverSession{
  119. Contract: resolver,
  120. TransactOpts: ens.TransactOpts,
  121. }, nil
  122. }
  123. func (ens *ENS) getRegistrar(node [32]byte) (*contract.FIFSRegistrarSession, error) {
  124. registrarAddr, err := ens.Owner(node)
  125. if err != nil {
  126. return nil, err
  127. }
  128. registrar, err := contract.NewFIFSRegistrar(registrarAddr, ens.contractBackend)
  129. if err != nil {
  130. return nil, err
  131. }
  132. return &contract.FIFSRegistrarSession{
  133. Contract: registrar,
  134. TransactOpts: ens.TransactOpts,
  135. }, nil
  136. }
  137. // Resolve is a non-transactional call that returns the content hash associated with a name.
  138. func (ens *ENS) Resolve(name string) (common.Hash, error) {
  139. node := EnsNode(name)
  140. resolver, err := ens.getResolver(node)
  141. if err != nil {
  142. return common.Hash{}, err
  143. }
  144. // IMPORTANT: The old contract is deprecated. This code should be removed latest on June 1st 2019
  145. supported, err := resolver.SupportsInterface(contentHash_Interface_Id)
  146. if err != nil {
  147. return common.Hash{}, err
  148. }
  149. if !supported {
  150. resolver, err := ens.getFallbackResolver(node)
  151. if err != nil {
  152. return common.Hash{}, err
  153. }
  154. ret, err := resolver.Content(node)
  155. if err != nil {
  156. return common.Hash{}, err
  157. }
  158. return common.BytesToHash(ret[:]), nil
  159. }
  160. // END DEPRECATED CODE
  161. contentHash, err := resolver.Contenthash(node)
  162. if err != nil {
  163. return common.Hash{}, err
  164. }
  165. return extractContentHash(contentHash)
  166. }
  167. // Addr is a non-transactional call that returns the address associated with a name.
  168. func (ens *ENS) Addr(name string) (common.Address, error) {
  169. node := EnsNode(name)
  170. resolver, err := ens.getResolver(node)
  171. if err != nil {
  172. return common.Address{}, err
  173. }
  174. ret, err := resolver.Addr(node)
  175. if err != nil {
  176. return common.Address{}, err
  177. }
  178. return common.BytesToAddress(ret[:]), nil
  179. }
  180. // SetAddress sets the address associated with a name. Only works if the caller
  181. // owns the name, and the associated resolver implements a `setAddress` function.
  182. func (ens *ENS) SetAddr(name string, addr common.Address) (*types.Transaction, error) {
  183. node := EnsNode(name)
  184. resolver, err := ens.getResolver(node)
  185. if err != nil {
  186. return nil, err
  187. }
  188. opts := ens.TransactOpts
  189. opts.GasLimit = 200000
  190. return resolver.Contract.SetAddr(&opts, node, addr)
  191. }
  192. // Register registers a new domain name for the caller, making them the owner of the new name.
  193. // Only works if the registrar for the parent domain implements the FIFS registrar protocol.
  194. func (ens *ENS) Register(name string) (*types.Transaction, error) {
  195. parentNode, label := ensParentNode(name)
  196. registrar, err := ens.getRegistrar(parentNode)
  197. if err != nil {
  198. return nil, err
  199. }
  200. return registrar.Contract.Register(&ens.TransactOpts, label, ens.TransactOpts.From)
  201. }
  202. // SetContentHash sets the content hash associated with a name. Only works if the caller
  203. // owns the name, and the associated resolver implements a `setContenthash` function.
  204. func (ens *ENS) SetContentHash(name string, hash []byte) (*types.Transaction, error) {
  205. node := EnsNode(name)
  206. resolver, err := ens.getResolver(node)
  207. if err != nil {
  208. return nil, err
  209. }
  210. opts := ens.TransactOpts
  211. opts.GasLimit = 200000
  212. // IMPORTANT: The old contract is deprecated. This code should be removed latest on June 1st 2019
  213. supported, err := resolver.SupportsInterface(contentHash_Interface_Id)
  214. if err != nil {
  215. return nil, err
  216. }
  217. if !supported {
  218. resolver, err := ens.getFallbackResolver(node)
  219. if err != nil {
  220. return nil, err
  221. }
  222. opts := ens.TransactOpts
  223. opts.GasLimit = 200000
  224. var b [32]byte
  225. copy(b[:], hash)
  226. return resolver.Contract.SetContent(&opts, node, b)
  227. }
  228. // END DEPRECATED CODE
  229. return resolver.Contract.SetContenthash(&opts, node, hash)
  230. }