feat: add reverse lookup for ens (#2912)

This commit is contained in:
Anthony Laibe 2022-10-24 12:07:44 +02:00 committed by GitHub
parent b2dce92f3f
commit a69a59c601
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 7 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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()