github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/rpc/ens.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package rpc
     6  
     7  import (
     8  	"strings"
     9  
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils"
    12  	"github.com/ethereum/go-ethereum/common"
    13  	ensGo "github.com/wealdtech/go-ens/v3"
    14  )
    15  
    16  // GetEnsAddresses converts an array of strings, if they contains .eth, into addresses. Note, we take
    17  // chain parameter, but ignore it choosing to look at mainnet ENS only
    18  func (conn *Connection) GetEnsAddresses(addrs []string) (out []string, found bool) {
    19  	has := false
    20  	for _, addr := range addrs {
    21  		if strings.Contains(addr, ".eth") {
    22  			has = true
    23  			break
    24  		}
    25  	}
    26  	if !has {
    27  		for _, term := range addrs {
    28  			out = append(out, utils.LowerIfHex(term))
    29  		}
    30  		return out, false
    31  	}
    32  
    33  	// Note: we use ENS on mainnet always
    34  	tc := TempConnection("mainnet")
    35  	if ec, err := tc.getClient(); err != nil {
    36  		return
    37  	} else {
    38  		defer ec.Close()
    39  		for _, term := range addrs {
    40  			if strings.Contains(term, ".eth") {
    41  				if val, err := ensGo.Resolve(ec, term); err == nil && len(val) > 0 {
    42  					term = val.Hex()
    43  					found = true
    44  				}
    45  			}
    46  			out = append(out, utils.LowerIfHex(term))
    47  		}
    48  		return
    49  	}
    50  }
    51  
    52  // GetEnsAddress converts a single string, if it contains .eth, into an address. Note, we take
    53  // chain parameter, but ignore it choosing to look at mainnet ENS only
    54  func (conn *Connection) GetEnsAddress(addrOrEns string) (string, bool) {
    55  	if !strings.Contains(addrOrEns, ".eth") {
    56  		return utils.LowerIfHex(addrOrEns), false
    57  	}
    58  
    59  	// Note: we use ENS on mainnet always
    60  	tc := TempConnection("mainnet")
    61  	if ec, err := tc.getClient(); err != nil {
    62  		return "", false
    63  	} else {
    64  		defer ec.Close()
    65  		if val, err := ensGo.Resolve(ec, addrOrEns); err == nil && len(val) > 0 {
    66  			return utils.LowerIfHex(val.Hex()), true
    67  		}
    68  		return utils.LowerIfHex(addrOrEns), false
    69  	}
    70  }
    71  
    72  // GetEnsName converts an address into an ens name if registered, returns the address otherwise.
    73  func (conn *Connection) GetEnsName(addr string) (string, bool) {
    74  	if !base.IsValidAddress(addr) {
    75  		return utils.LowerIfHex(addr), false
    76  	}
    77  
    78  	// Note: we use ENS on mainnet always
    79  	tc := TempConnection("mainnet")
    80  	if ec, err := tc.getClient(); err != nil {
    81  		return "", false
    82  	} else {
    83  		defer ec.Close()
    84  		if val, err := ensGo.ReverseResolve(ec, common.HexToAddress(addr)); err == nil && len(val) > 0 {
    85  			return utils.LowerIfHex(val), true
    86  		}
    87  		return utils.LowerIfHex(addr), false
    88  	}
    89  }
    90  
    91  // IsSame returns true if the two strings are the same, ignoring case.
    92  // If not equal, it also tried to interpret the strings as addresses using ENS.
    93  func IsSame(a string, b string) bool {
    94  	if strings.EqualFold(a, b) {
    95  		return true
    96  	}
    97  	if !strings.HasSuffix(b, ".eth") {
    98  		return false
    99  	}
   100  	unused := TempConnection("mainnet")
   101  	ensAddr, _ := unused.GetEnsAddress(b)
   102  	return ensAddr == a
   103  }