github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/alias/alias.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package alias
     7  
     8  import (
     9  	"github.com/ethereum/go-ethereum/common"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/iotexproject/iotex-address/address"
    14  	"github.com/iotexproject/iotex-core/ioctl/config"
    15  	"github.com/iotexproject/iotex-core/ioctl/output"
    16  	"github.com/iotexproject/iotex-core/ioctl/util"
    17  	"github.com/iotexproject/iotex-core/ioctl/validator"
    18  	"github.com/iotexproject/iotex-core/pkg/util/addrutil"
    19  )
    20  
    21  // Multi-language support
    22  var (
    23  	_aliasCmdShorts = map[config.Language]string{
    24  		config.English: "Manage aliases of IoTeX addresses",
    25  		config.Chinese: "管理IoTeX的地址别名",
    26  	}
    27  )
    28  
    29  // Errors
    30  var (
    31  	ErrNoAliasFound = errors.New("no alias is found")
    32  )
    33  
    34  // Flags
    35  var (
    36  	_format      string
    37  	_forceImport bool
    38  )
    39  
    40  type alias struct {
    41  	Name    string `json:"name" yaml:"name"`
    42  	Address string `json:"address" yaml:"address"`
    43  }
    44  
    45  type aliases struct {
    46  	Aliases []alias `json:"aliases" yaml:"aliases"`
    47  }
    48  
    49  // AliasCmd represents the alias command
    50  var AliasCmd = &cobra.Command{
    51  	Use:   "alias",
    52  	Short: config.TranslateInLang(_aliasCmdShorts, config.UILanguage),
    53  }
    54  
    55  func init() {
    56  	AliasCmd.AddCommand(_aliasSetCmd)
    57  	AliasCmd.AddCommand(_aliasListCmd)
    58  	AliasCmd.AddCommand(_aliasRemoveCmd)
    59  	AliasCmd.AddCommand(_aliasImportCmd)
    60  	AliasCmd.AddCommand(_aliasExportCmd)
    61  }
    62  
    63  // IOAddress returns the address in IoTeX address _format
    64  func IOAddress(in string) (address.Address, error) {
    65  	addr, err := util.Address(in)
    66  	if err != nil {
    67  		return nil, output.NewError(output.AddressError, "", err)
    68  	}
    69  	return address.FromString(addr)
    70  }
    71  
    72  // EtherAddress returns the address in ether _format
    73  func EtherAddress(in string) (common.Address, error) {
    74  	addr, err := util.Address(in)
    75  	if err != nil {
    76  		return common.Address{}, output.NewError(output.AddressError, "", err)
    77  	}
    78  	return addrutil.IoAddrToEvmAddr(addr)
    79  }
    80  
    81  // Alias returns the alias corresponding to address
    82  func Alias(address string) (string, error) {
    83  	if err := validator.ValidateAddress(address); err != nil {
    84  		return "", output.NewError(output.ValidationError, "", err)
    85  	}
    86  	for alias, addr := range config.ReadConfig.Aliases {
    87  		if addr == address {
    88  			return alias, nil
    89  		}
    90  	}
    91  	return "", output.NewError(output.AddressError, ErrNoAliasFound.Error(), nil)
    92  }
    93  
    94  // GetAliasMap gets the map from address to alias
    95  func GetAliasMap() map[string]string {
    96  	aliases := make(map[string]string)
    97  	for name, addr := range config.ReadConfig.Aliases {
    98  		aliases[addr] = name
    99  	}
   100  	return aliases
   101  }