ens_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. import (
  18. "math/big"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  21. "github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
  22. "github.com/ethereum/go-ethereum/contracts/ens/contract"
  23. "github.com/ethereum/go-ethereum/core"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. )
  26. var (
  27. key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  28. name = "my name on ENS"
  29. hash = crypto.Keccak256Hash([]byte("my content"))
  30. addr = crypto.PubkeyToAddress(key.PublicKey)
  31. )
  32. func TestENS(t *testing.T) {
  33. contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}})
  34. transactOpts := bind.NewKeyedTransactor(key)
  35. ensAddr, ens, err := DeployENS(transactOpts, contractBackend)
  36. if err != nil {
  37. t.Fatalf("can't deploy root registry: %v", err)
  38. }
  39. contractBackend.Commit()
  40. // Set ourself as the owner of the name.
  41. if _, err := ens.Register(name); err != nil {
  42. t.Fatalf("can't register: %v", err)
  43. }
  44. contractBackend.Commit()
  45. // Deploy a resolver and make it responsible for the name.
  46. resolverAddr, _, _, err := contract.DeployPublicResolver(transactOpts, contractBackend, ensAddr)
  47. if err != nil {
  48. t.Fatalf("can't deploy resolver: %v", err)
  49. }
  50. if _, err := ens.SetResolver(ensNode(name), resolverAddr); err != nil {
  51. t.Fatalf("can't set resolver: %v", err)
  52. }
  53. contractBackend.Commit()
  54. // Set the content hash for the name.
  55. if _, err = ens.SetContentHash(name, hash); err != nil {
  56. t.Fatalf("can't set content hash: %v", err)
  57. }
  58. contractBackend.Commit()
  59. // Try to resolve the name.
  60. vhost, err := ens.Resolve(name)
  61. if err != nil {
  62. t.Fatalf("expected no error, got %v", err)
  63. }
  64. if vhost != hash {
  65. t.Fatalf("resolve error, expected %v, got %v", hash.Hex(), vhost.Hex())
  66. }
  67. }