From a69a59c6014566344c1c102099baa192dd6e0a7c Mon Sep 17 00:00:00 2001 From: Anthony Laibe <491074+alaibe@users.noreply.github.com> Date: Mon, 24 Oct 2022 12:07:44 +0200 Subject: [PATCH] feat: add reverse lookup for ens (#2912) --- contracts/contracts.go | 17 ++++++++++------- services/ens/api.go | 9 +++++++++ services/ens/api_test.go | 10 ++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/contracts/contracts.go b/contracts/contracts.go index fdc43d6b3..0cab96ab6 100644 --- a/contracts/contracts.go +++ b/contracts/contracts.go @@ -19,23 +19,26 @@ type ContractMaker struct { RPCClient *rpc.Client } -func (c *ContractMaker) NewRegistry(chainID uint64) (*resolver.ENSRegistryWithFallback, error) { - contractAddr, err := resolver.ContractAddress(chainID) - if err != nil { - return nil, err - } - +func (c *ContractMaker) NewRegistryWithAddress(chainID uint64, address common.Address) (*resolver.ENSRegistryWithFallback, error) { backend, err := c.RPCClient.EthClient(chainID) if err != nil { return nil, err } return resolver.NewENSRegistryWithFallback( - contractAddr, + address, backend, ) } +func (c *ContractMaker) NewRegistry(chainID uint64) (*resolver.ENSRegistryWithFallback, error) { + contractAddr, err := resolver.ContractAddress(chainID) + if err != nil { + return nil, err + } + return c.NewRegistryWithAddress(chainID, contractAddr) +} + func (c *ContractMaker) NewPublicResolver(chainID uint64, resolverAddress *common.Address) (*resolver.PublicResolver, error) { backend, err := c.RPCClient.EthClient(chainID) if err != nil { diff --git a/services/ens/api.go b/services/ens/api.go index e1ecd908f..614a75ed2 100644 --- a/services/ens/api.go +++ b/services/ens/api.go @@ -14,6 +14,7 @@ import ( "github.com/multiformats/go-multibase" "github.com/multiformats/go-multihash" "github.com/pkg/errors" + "github.com/wealdtech/go-ens/v3" "github.com/wealdtech/go-multicodec" "github.com/ethereum/go-ethereum" @@ -98,6 +99,14 @@ func (api *API) Resolver(ctx context.Context, chainID uint64, username string) ( return &resolver, nil } +func (api *API) GetName(ctx context.Context, chainID uint64, address common.Address) (string, error) { + backend, err := api.contractMaker.RPCClient.EthClient(chainID) + if err != nil { + return "", err + } + return ens.ReverseResolve(backend, address) +} + func (api *API) OwnerOf(ctx context.Context, chainID uint64, username string) (*common.Address, error) { err := validateENSUsername(username) if err != nil { diff --git a/services/ens/api_test.go b/services/ens/api_test.go index 0eb5b0e2a..a51d7cbb8 100644 --- a/services/ens/api_test.go +++ b/services/ens/api_test.go @@ -10,6 +10,7 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" + "github.com/ethereum/go-ethereum/common" gethrpc "github.com/ethereum/go-ethereum/rpc" "github.com/status-im/status-go/appdatabase" "github.com/status-im/status-go/params" @@ -67,6 +68,15 @@ func TestResolver(t *testing.T) { require.Equal(t, "0x4976fb03C32e5B8cfe2b6cCB31c09Ba78EBaBa41", r.String()) } +func TestGetName(t *testing.T) { + api, cancel := setupTestAPI(t) + defer cancel() + + r, err := api.GetName(context.Background(), 1, common.HexToAddress("0x7d28Ab6948F3Db2F95A43742265D382a4888c120")) + require.NoError(t, err) + require.Equal(t, "rramos.eth", r) +} + func TestOwnerOf(t *testing.T) { api, cancel := setupTestAPI(t) defer cancel()