github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/contracts/ens/ens_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package ens 13 14 import ( 15 "math/big" 16 "testing" 17 18 "github.com/Sberex/go-sberex/accounts/abi/bind" 19 "github.com/Sberex/go-sberex/accounts/abi/bind/backends" 20 "github.com/Sberex/go-sberex/contracts/ens/contract" 21 "github.com/Sberex/go-sberex/core" 22 "github.com/Sberex/go-sberex/crypto" 23 ) 24 25 var ( 26 key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 27 name = "my name on ENS" 28 hash = crypto.Keccak256Hash([]byte("my content")) 29 addr = crypto.PubkeyToAddress(key.PublicKey) 30 ) 31 32 func TestENS(t *testing.T) { 33 contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}) 34 transactOpts := bind.NewKeyedTransactor(key) 35 36 ensAddr, ens, err := DeployENS(transactOpts, contractBackend) 37 if err != nil { 38 t.Fatalf("can't deploy root registry: %v", err) 39 } 40 contractBackend.Commit() 41 42 // Set ourself as the owner of the name. 43 if _, err := ens.Register(name); err != nil { 44 t.Fatalf("can't register: %v", err) 45 } 46 contractBackend.Commit() 47 48 // Deploy a resolver and make it responsible for the name. 49 resolverAddr, _, _, err := contract.DeployPublicResolver(transactOpts, contractBackend, ensAddr) 50 if err != nil { 51 t.Fatalf("can't deploy resolver: %v", err) 52 } 53 if _, err := ens.SetResolver(ensNode(name), resolverAddr); err != nil { 54 t.Fatalf("can't set resolver: %v", err) 55 } 56 contractBackend.Commit() 57 58 // Set the content hash for the name. 59 if _, err = ens.SetContentHash(name, hash); err != nil { 60 t.Fatalf("can't set content hash: %v", err) 61 } 62 contractBackend.Commit() 63 64 // Try to resolve the name. 65 vhost, err := ens.Resolve(name) 66 if err != nil { 67 t.Fatalf("expected no error, got %v", err) 68 } 69 if vhost != hash { 70 t.Fatalf("resolve error, expected %v, got %v", hash.Hex(), vhost.Hex()) 71 } 72 }