From f1b9a3e2f415f6fc43519fdfc5b28ac2afcdada0 Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Fri, 28 Sep 2018 22:46:41 +0200 Subject: [PATCH] contracts/ens: expose Add and SetAddr in ENS (#17661) I am planning to use this to resolve names to user addresses for Swarm/MRU feeds. --- contracts/ens/ens.go | 32 ++++++++++++++++++++++++++++++++ contracts/ens/ens_test.go | 25 +++++++++++++++++++++---- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/contracts/ens/ens.go b/contracts/ens/ens.go index 75d9d0e4b..b7448c471 100644 --- a/contracts/ens/ens.go +++ b/contracts/ens/ens.go @@ -151,6 +151,38 @@ func (self *ENS) Resolve(name string) (common.Hash, error) { return common.BytesToHash(ret[:]), nil } +// Addr is a non-transactional call that returns the address associated with a name. +func (self *ENS) Addr(name string) (common.Address, error) { + node := EnsNode(name) + + resolver, err := self.getResolver(node) + if err != nil { + return common.Address{}, err + } + + ret, err := resolver.Addr(node) + if err != nil { + return common.Address{}, err + } + + return common.BytesToAddress(ret[:]), nil +} + +// SetAddress sets the address associated with a name. Only works if the caller +// owns the name, and the associated resolver implements a `setAddress` function. +func (self *ENS) SetAddr(name string, addr common.Address) (*types.Transaction, error) { + node := EnsNode(name) + + resolver, err := self.getResolver(node) + if err != nil { + return nil, err + } + + opts := self.TransactOpts + opts.GasLimit = 200000 + return resolver.Contract.SetAddr(&opts, node, addr) +} + // Register registers a new domain name for the caller, making them the owner of the new name. // Only works if the registrar for the parent domain implements the FIFS registrar protocol. func (self *ENS) Register(name string) (*types.Transaction, error) { diff --git a/contracts/ens/ens_test.go b/contracts/ens/ens_test.go index 411b04197..cd64fbf15 100644 --- a/contracts/ens/ens_test.go +++ b/contracts/ens/ens_test.go @@ -22,16 +22,18 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/contracts/ens/contract" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/crypto" ) var ( - key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") - name = "my name on ENS" - hash = crypto.Keccak256Hash([]byte("my content")) - addr = crypto.PubkeyToAddress(key.PublicKey) + key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") + name = "my name on ENS" + hash = crypto.Keccak256Hash([]byte("my content")) + addr = crypto.PubkeyToAddress(key.PublicKey) + testAddr = common.HexToAddress("0x1234123412341234123412341234123412341234") ) func TestENS(t *testing.T) { @@ -74,4 +76,19 @@ func TestENS(t *testing.T) { if vhost != hash { t.Fatalf("resolve error, expected %v, got %v", hash.Hex(), vhost.Hex()) } + + // set the address for the name + if _, err = ens.SetAddr(name, testAddr); err != nil { + t.Fatalf("can't set address: %v", err) + } + contractBackend.Commit() + + // Try to resolve the name to an address + recoveredAddr, err := ens.Addr(name) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if vhost != hash { + t.Fatalf("resolve error, expected %v, got %v", testAddr.Hex(), recoveredAddr.Hex()) + } }