-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.go
108 lines (88 loc) · 3.35 KB
/
address.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package convert
import (
"fmt"
"github.com/bcdevtools/devd/cmd/utils"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
"os"
"regexp"
"strings"
)
// GetConvertAddressCmd creates a helper command that convert account bech32 address into hex address or vice versa
func GetConvertAddressCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "address [address] [bech32_hrp]",
Aliases: []string{"a"},
Short: "Convert account bech32 address into hex address or vice versa",
Long: `Convert account bech32 address into hex address or vice versa.
- Case 1: if the input address is an EVM address, bech32 HRP as the second argument is required.
- Case 2: if the input address is a bech32 address, bech32 HRP as the second argument is present, convert.
- Case 3: if the input address is a bech32 address, will show the EVM address.
`,
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
defer func() {
utils.PrintlnStdErr("WARN: DO NOT use this command to convert address across chains with different HD-Path! (eg: Ethermint 60 and Cosmos 118)")
}()
normalizedInputAddress := strings.TrimSpace(strings.ToLower(args[0]))
var nextConvertToBech32Hrp string
if len(args) > 1 {
nextConvertToBech32Hrp = strings.TrimSpace(strings.ToLower(args[1]))
nextConvertToBech32Hrp = strings.TrimSuffix(nextConvertToBech32Hrp, "1")
}
// conditional processing
if regexp.MustCompile(`^(0x)?[a-f\d]{40}$`).MatchString(normalizedInputAddress) {
// case 1, is EVM address
if len(nextConvertToBech32Hrp) < 1 {
utils.PrintlnStdErr("ERR: missing Bech32 HRP as the second argument")
os.Exit(1)
}
evmAddress := common.HexToAddress(normalizedInputAddress)
bech32Addr, err := bech32.ConvertAndEncode(nextConvertToBech32Hrp, evmAddress.Bytes())
utils.ExitOnErr(err, "failed to convert EVM address to bech32 address")
fmt.Println(bech32Addr)
return
}
if regexp.MustCompile(`^(0x)?[a-f\d]{64}$`).MatchString(normalizedInputAddress) {
// case 1, but bytes addr (eg: interchain account)
if len(nextConvertToBech32Hrp) < 1 {
utils.PrintlnStdErr("ERR: missing Bech32 HRP as the second argument")
os.Exit(1)
}
bytesAddress := common.HexToHash(normalizedInputAddress)
bech32Addr, err := bech32.ConvertAndEncode(nextConvertToBech32Hrp, bytesAddress.Bytes())
utils.ExitOnErr(err, "failed to convert bytes address to bech32 address")
fmt.Println(bech32Addr)
return
}
// case 2 + 3
spl := strings.Split(normalizedInputAddress, "1")
if len(spl) != 2 || len(spl[0]) < 1 || len(spl[1]) < 1 {
utils.PrintlnStdErr("ERR: invalid bech32 address")
os.Exit(1)
}
bz, err := sdk.GetFromBech32(normalizedInputAddress, spl[0])
utils.ExitOnErr(err, "failed to decode bech32 address")
if len(nextConvertToBech32Hrp) > 0 {
// case 2
bech32Addr, err := bech32.ConvertAndEncode(nextConvertToBech32Hrp, bz)
utils.ExitOnErr(
err,
fmt.Sprintf(
"failed to convert bech32 address %s into bech32 address with HRP %s",
normalizedInputAddress,
nextConvertToBech32Hrp,
),
)
fmt.Println(bech32Addr)
return
}
// case 3
evmAddress := common.BytesToAddress(bz)
fmt.Println(evmAddress.Hex())
},
}
return cmd
}