/
accountethaddr.go
79 lines (70 loc) · 2.47 KB
/
accountethaddr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2019 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package account
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/iotexproject/iotex-address/address"
"github.com/spf13/cobra"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/util"
"github.com/iotexproject/iotex-core/pkg/util/addrutil"
)
// Multi-language support
var (
_ethaddrCmdShorts = map[config.Language]string{
config.English: "Translate address between IOTX and ETH",
config.Chinese: "在IOTX和ETH间转换地址",
}
_ethaddrCmdUses = map[config.Language]string{
config.English: "ethaddr (ALIAS|IOTEX_ADDRESS|ETH_ADDRESS)",
config.Chinese: "ethaddr (别名|IOTEX_地址|ETH_地址)",
}
)
// _accountEthaddrCmd represents the account ethaddr command
var _accountEthaddrCmd = &cobra.Command{
Use: config.TranslateInLang(_ethaddrCmdUses, config.UILanguage),
Short: config.TranslateInLang(_ethaddrCmdShorts, config.UILanguage),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := accountEthaddr(args[0])
return output.PrintError(err)
},
}
type ethaddrMessage struct {
IOAddr string `json:"ioAddr"`
EthAddr string `json:"ethAddr"`
}
func accountEthaddr(arg string) error {
var ethAddress common.Address
ioAddr, err := util.Address(arg)
if err != nil {
if ok := common.IsHexAddress(arg); !ok {
return output.NewError(output.AddressError, "", err)
}
ethAddress = common.HexToAddress(arg)
ioAddress, err := address.FromBytes(ethAddress.Bytes())
if err != nil {
return output.NewError(output.AddressError, "failed to form IoTeX address from ETH address", nil)
}
ioAddr = ioAddress.String()
} else {
ethAddress, err = addrutil.IoAddrToEvmAddr(ioAddr)
if err != nil {
return output.NewError(output.AddressError, "", err)
}
}
message := ethaddrMessage{IOAddr: ioAddr, EthAddr: ethAddress.String()}
fmt.Println(message.String())
return nil
}
func (m *ethaddrMessage) String() string {
if output.Format == "" {
return fmt.Sprintf("%s - %s", m.IOAddr, m.EthAddr)
}
return output.FormatString(output.Result, m)
}