github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/client/cli/utils.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/pkg/errors"
     8  
     9  	"github.com/ethereum/go-ethereum/common"
    10  
    11  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    12  )
    13  
    14  func accountToHex(addr string) (string, error) {
    15  	if strings.HasPrefix(addr, sdk.GetConfig().GetBech32AccountAddrPrefix()) {
    16  		// Check to see if address is Cosmos bech32 formatted
    17  		toAddr, err := sdk.AccAddressFromBech32(addr)
    18  		if err != nil {
    19  			return "", errors.Wrap(err, "must provide a valid Bech32 address")
    20  		}
    21  		ethAddr := common.BytesToAddress(toAddr.Bytes())
    22  		return ethAddr.Hex(), nil
    23  	}
    24  
    25  	if !strings.HasPrefix(addr, "0x") {
    26  		addr = "0x" + addr
    27  	}
    28  
    29  	valid := common.IsHexAddress(addr)
    30  	if !valid {
    31  		return "", fmt.Errorf("%s is not a valid Ethereum or Cosmos address", addr)
    32  	}
    33  
    34  	ethAddr := common.HexToAddress(addr)
    35  
    36  	return ethAddr.Hex(), nil
    37  }
    38  
    39  func formatKeyToHash(key string) string {
    40  	if !strings.HasPrefix(key, "0x") {
    41  		key = "0x" + key
    42  	}
    43  
    44  	ethkey := common.HexToHash(key)
    45  
    46  	return ethkey.Hex()
    47  }