github.com/daeglee/go-ethereum@v0.0.0-20190504220456-cad3e8d18e9b/contracts/ens/ens_test.go (about) 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 17 package ens 18 19 import ( 20 "math/big" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/accounts/abi/bind" 24 "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" 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" 29 "github.com/ethereum/go-ethereum/crypto" 30 ) 31 32 var ( 33 key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 34 name = "my name on ENS" 35 hash = crypto.Keccak256Hash([]byte("my content")) 36 fallbackHash = crypto.Keccak256Hash([]byte("my content hash")) 37 addr = crypto.PubkeyToAddress(key.PublicKey) 38 testAddr = common.HexToAddress("0x1234123412341234123412341234123412341234") 39 ) 40 41 func TestENS(t *testing.T) { 42 contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000) 43 transactOpts := bind.NewKeyedTransactor(key) 44 45 ensAddr, ens, err := DeployENS(transactOpts, contractBackend) 46 if err != nil { 47 t.Fatalf("can't deploy root registry: %v", err) 48 } 49 contractBackend.Commit() 50 51 // Set ourself as the owner of the name. 52 if _, err := ens.Register(name); err != nil { 53 t.Fatalf("can't register: %v", err) 54 } 55 contractBackend.Commit() 56 57 // Deploy a resolver and make it responsible for the name. 58 resolverAddr, _, _, err := contract.DeployPublicResolver(transactOpts, contractBackend, ensAddr) 59 if err != nil { 60 t.Fatalf("can't deploy resolver: %v", err) 61 } 62 63 if _, err := ens.SetResolver(EnsNode(name), resolverAddr); err != nil { 64 t.Fatalf("can't set resolver: %v", err) 65 } 66 contractBackend.Commit() 67 68 // Set the content hash for the name. 69 cid, err := EncodeSwarmHash(hash) 70 if err != nil { 71 t.Fatal(err) 72 } 73 if _, err = ens.SetContentHash(name, cid); err != nil { 74 t.Fatalf("can't set content hash: %v", err) 75 } 76 contractBackend.Commit() 77 78 // Try to resolve the name. 79 resolvedHash, err := ens.Resolve(name) 80 if err != nil { 81 t.Fatalf("expected no error, got %v", err) 82 } 83 if resolvedHash.Hex() != hash.Hex() { 84 t.Fatalf("resolve error, expected %v, got %v", hash.Hex(), resolvedHash.Hex()) 85 } 86 87 // set the address for the name 88 if _, err = ens.SetAddr(name, testAddr); err != nil { 89 t.Fatalf("can't set address: %v", err) 90 } 91 contractBackend.Commit() 92 93 // Try to resolve the name to an address 94 recoveredAddr, err := ens.Addr(name) 95 if err != nil { 96 t.Fatalf("expected no error, got %v", err) 97 } 98 if testAddr.Hex() != recoveredAddr.Hex() { 99 t.Fatalf("resolve error, expected %v, got %v", testAddr.Hex(), recoveredAddr.Hex()) 100 } 101 102 // deploy the fallback contract and see that the fallback mechanism works 103 fallbackResolverAddr, _, _, err := fallback_contract.DeployPublicResolver(transactOpts, contractBackend, ensAddr) 104 if err != nil { 105 t.Fatalf("can't deploy resolver: %v", err) 106 } 107 if _, err := ens.SetResolver(EnsNode(name), fallbackResolverAddr); err != nil { 108 t.Fatalf("can't set resolver: %v", err) 109 } 110 contractBackend.Commit() 111 112 // Set the content hash for the name. 113 if _, err = ens.SetContentHash(name, fallbackHash.Bytes()); err != nil { 114 t.Fatalf("can't set content hash: %v", err) 115 } 116 contractBackend.Commit() 117 118 // Try to resolve the name. 119 fallbackResolvedHash, err := ens.Resolve(name) 120 if err != nil { 121 t.Fatalf("expected no error, got %v", err) 122 } 123 if fallbackResolvedHash.Hex() != fallbackHash.Hex() { 124 t.Fatalf("resolve error, expected %v, got %v", hash.Hex(), resolvedHash.Hex()) 125 } 126 }