-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (147 loc) · 3.96 KB
/
main.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"log"
"os"
cli "github.com/urfave/cli/v2"
"github.com/zeuslawyer/hextool/encdec"
"github.com/zeuslawyer/hextool/internal/flags"
"github.com/zeuslawyer/hextool/selector"
)
const (
testStringHex = "0x22596f75277665206265656e2048657865642122"
testBigIntHex = "0xd431"
)
func main() {
app := cli.NewApp()
app.Name = "hextool"
app.Description = "A cli devtool to help you encode and decode hex values for Ethereum and EVM based chains."
app.Commands = []*cli.Command{
{
Name: "tostring",
Aliases: []string{"getstring"},
Usage: "decode a hex string to string",
Action: func(cliCtx *cli.Context) error {
fmt.Printf("%v\n", encdec.DecodeHexToString(cliCtx.String("hex")))
return nil
},
Flags: []cli.Flag{
flags.CommandFlags["hex"],
},
},
{
Name: "toint",
Aliases: []string{"getint"},
Usage: "decode a hex string to int",
Action: func(cliCtx *cli.Context) error {
fmt.Printf("%v\n", encdec.DecodeHexToBigInt(cliCtx.String("hex")))
return nil
},
Flags: []cli.Flag{
flags.CommandFlags["hex"],
},
},
{
Name: "selector",
Args: false,
Aliases: []string{"selectorFromSig"},
Usage: "calculates the function selector from a given function signature.",
Action: func(cliCtx *cli.Context) error {
fmt.Printf("%v\n", selector.SelectorFromSig(cliCtx.String("sig")))
return nil
},
Flags: []cli.Flag{
flags.CommandFlags["sig"],
},
},
{
Name: "decodeMethodSelector",
Aliases: []string{"methodsig"},
Usage: "Look through the provided ABI to find a function signature that matches the given function selector",
Action: func(cliCtx *cli.Context) error {
fmt.Printf("%v\n", selector.SigFromSelector(
cliCtx.String("selector"),
cliCtx.String("path"),
cliCtx.String("url")),
)
return nil
},
Flags: []cli.Flag{
flags.CommandFlags["selector"],
flags.CommandFlags["path"],
flags.CommandFlags["url"],
},
},
{
Name: "decodeErrorSelector",
Aliases: []string{"errorSig"},
Usage: "Look through the provided ABI to find the error signature that matches the given error selector",
Action: func(cliCtx *cli.Context) error {
fmt.Printf("%v\n", selector.ErrorSigFromSelector(
cliCtx.String("selector"),
cliCtx.String("path"),
cliCtx.String("url")),
)
return nil
},
Flags: []cli.Flag{
flags.CommandFlags["selector"],
flags.CommandFlags["path"],
flags.CommandFlags["url"],
},
},
{
Name: "decodeEvent",
Aliases: []string{"eventsig"},
Usage: "Look through the provided ABI to find the event signature that matches the given 32 byte topic hash",
Action: func(cliCtx *cli.Context) error {
fmt.Printf("%v\n", selector.EventFromTopicHash(
cliCtx.String("topic"),
cliCtx.String("path"),
cliCtx.String("url")),
)
return nil
},
Flags: []cli.Flag{
flags.CommandFlags["topic"],
flags.CommandFlags["path"],
flags.CommandFlags["url"],
},
},
{
Name: "abi.decode",
Aliases: []string{"abidecode"},
Usage: "abi-decode the given input hex into its corresponding data as per the comma-separated types provided",
Action: func(cliCtx *cli.Context) error {
fmt.Printf("%v\n", encdec.AbiDecode(
cliCtx.String("hex"),
cliCtx.String("types"),
))
return nil
},
Flags: []cli.Flag{
flags.CommandFlags["hex"],
flags.CommandFlags["types"],
},
},
{
Name: "abi.encode",
Aliases: []string{"abiencode"},
Usage: "abi-encode the given input values into hex, as per the data types provided. Input values and data types be comma-separated",
Action: func(cliCtx *cli.Context) error {
fmt.Printf("%v\n", encdec.AbiEncode(
cliCtx.String("values"),
cliCtx.String("types"),
))
return nil
},
Flags: []cli.Flag{
flags.CommandFlags["values"],
flags.CommandFlags["types"],
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}