-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
278 lines (232 loc) · 6.76 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os/exec"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)
type Contract struct {
Addr common.Address
GivenName string
OwnName string
ParentContract *Contract
AdminContract *Contract
ImplContract *Contract
BeaconContract *Contract
LinkedContracts []*Contract
}
func (c *Contract) AddLinkedAddress(name string, addr common.Address) {
child := &Contract{
ParentContract: c,
Addr: addr,
GivenName: name,
}
c.LinkedContracts = append(c.LinkedContracts, child)
}
func (c *Contract) AddEIP1967Children(children EIP1967Slots) {
if c.AdminContract != nil ||
c.BeaconContract != nil ||
c.ImplContract != nil {
panic("trying to add eip1967 children again!")
}
c.AdminContract = &Contract{
Addr: children.AdminAddr,
ParentContract: c,
GivenName: "EIP1967::Admin",
}
c.ImplContract = &Contract{
Addr: children.ImplementationAddr,
ParentContract: c,
GivenName: "EIP1967::Impl",
}
c.BeaconContract = &Contract{
Addr: children.BeaconAddr,
ParentContract: c,
GivenName: "EIP1967::Beacon",
}
}
func (c *Contract) AddDependencies(eth *ETHClient, ethscan *ETHScan, depth, maxDepth int) {
if depth == maxDepth {
return
}
augmentedSourceCode, err := ethscan.GetSourceCode(c.Addr)
if err != nil && err.Error() != "contract source code not verified" {
panic(err)
}
// XXX: probably the worst...
setOfLinked := make(map[string]struct{})
// XXX: rate limit gotten or the augmented source code was not verified
if augmentedSourceCode != nil {
c.OwnName = augmentedSourceCode.ContractName
parsedABI, err := ParseABI(augmentedSourceCode.ABI)
if err != nil {
panic(err)
}
selectors := AddressGettersToSelectors(parsedABI.Methods)
for name, sel := range selectors {
res := eth.CallContractGetAddr(c.Addr, sel)
c.AddLinkedAddress(name, res)
setOfLinked[res.Hex()] = struct{}{}
}
} else {
// default to only abi...
abi, err := ethscan.GetABI(c.Addr)
if err != nil {
panic(err)
}
if abi != nil {
parsedABI, err := ParseABI(abi)
if err != nil {
panic(err)
}
selectors := AddressGettersToSelectors(parsedABI.Methods)
for name, sel := range selectors {
res := eth.CallContractGetAddr(c.Addr, sel)
c.AddLinkedAddress(name, res)
setOfLinked[res.Hex()] = struct{}{}
}
}
}
slots, err := eth.GetEIP1967Slots(c.Addr)
if err != nil {
panic(err)
}
if !slots.Empty() {
c.AddEIP1967Children(slots)
c.AdminContract.AddDependencies(eth, ethscan, depth+1, maxDepth)
c.BeaconContract.AddDependencies(eth, ethscan, depth+1, maxDepth)
c.ImplContract.AddDependencies(eth, ethscan, depth+1, maxDepth)
}
if len(c.LinkedContracts) > 0 {
for _, linkedContract := range c.LinkedContracts {
linkedContract.AddDependencies(eth, ethscan, depth+1, maxDepth)
}
}
if c.OwnName != "" && c.OwnName != c.GivenName {
c.OwnName = fmt.Sprintf(" - %s", c.OwnName)
} else {
c.OwnName = ""
}
}
func (c *Contract) buildString(depth int) string {
prefix := strings.Repeat(" ", depth)
s := fmt.Sprintf("%s\n", c.Addr)
if c.AdminContract != nil {
s += fmt.Sprintf("%s|-> (%s%s) - %s", prefix, c.AdminContract.GivenName, c.AdminContract.OwnName,
c.AdminContract.buildString(depth+1))
}
if c.BeaconContract != nil {
s += fmt.Sprintf("%s|-> (%s%s) - %s", prefix, c.BeaconContract.GivenName,
c.BeaconContract.OwnName, c.BeaconContract.buildString(depth+1))
}
if c.ImplContract != nil {
s += fmt.Sprintf("%s|-> (%s%s) - %s", prefix, c.ImplContract.GivenName,
c.ImplContract.OwnName, c.ImplContract.buildString(depth+1))
}
for _, linkedAddr := range c.LinkedContracts {
s += fmt.Sprintf("%s|-> (%s%s) - %s", prefix, linkedAddr.GivenName,
linkedAddr.OwnName, linkedAddr.buildString(depth+1))
}
return s
}
func (c *Contract) String() string {
return c.buildString(0)
}
type Flags struct {
TargetAddr string
ETHScanAPIKey string
ETHClientEndpoint string
MaxDepth int
}
func parseFlags() Flags {
addr := flag.String("addr", "", "the address of the target contract, 0x prefixed")
ethscanApiKey := flag.String("ethscankey", "", "an Etherscan API Key")
ethclientEndpoint := flag.String("jsonrpc", "", "an Ethereum JSON RPC endpoint")
maxDepth := flag.Int("maxDepth", 5, "the max recursive depth")
flag.Parse()
if *addr == "" {
log.Fatal("[ERR] target address not set")
}
if *ethscanApiKey == "" {
log.Fatal("[ERR] no Etherscan API key set")
}
if *ethclientEndpoint == "" {
log.Fatal("[ERR] no JSON-RPC endpoint set")
}
return Flags{*addr, *ethscanApiKey, *ethclientEndpoint, *maxDepth}
}
func main() {
flags := parseFlags()
eth, err := NewETHClient(flags.ETHClientEndpoint)
if err != nil {
log.Fatalf("[ERR] there was a problem initializing the JSON-RPC client: %s", err)
}
ethscan := NewETHScan(flags.ETHScanAPIKey)
targetContract := common.HexToAddress(flags.TargetAddr)
fmt.Printf("- Target contract: %s\n", flags.TargetAddr)
fmt.Printf("- Max depth: %d\n", flags.MaxDepth)
fmt.Printf("- Starting. This may take a while...\n")
contract := Contract{Addr: targetContract}
contract.AddDependencies(eth, ðscan, 0, flags.MaxDepth)
fmt.Printf("\n%s", contract.String())
}
type EIP1967Slots struct {
ImplementationAddr common.Address
BeaconAddr common.Address
AdminAddr common.Address
}
func (e EIP1967Slots) Empty() bool {
return e.ImplementationAddr == common.Address{} &&
e.BeaconAddr == common.Address{} &&
e.AdminAddr == common.Address{}
}
// XXX: TODO
// CONTINUE PARSING THE SOURCE CODE
type SourceCode struct {
StateVariable bool `json:"stateVariable"`
}
func ParseSourceCode(sourceCode string) error {
tmp, err := ioutil.TempFile("/tmp", "ethdep")
if err != nil {
return err
}
_, err = tmp.WriteString(sourceCode)
if err != nil {
return err
}
cmd := exec.Command("solc", "--ast-compact-json", tmp.Name())
outputRaw, err := cmd.Output()
if err != nil {
return err
}
var output string
for i, c := range outputRaw {
if c == '{' {
output = string(outputRaw[i:])
}
}
output += "foo"
return nil
}
func ParseABI(rawABI []byte) (abi.ABI, error) {
return abi.JSON(strings.NewReader(string(rawABI)))
}
func AddressGettersToSelectors(methods map[string]abi.Method) map[string][]byte {
selectors := make(map[string][]byte)
isRelevantMethod := func(m abi.Method) bool {
return len(m.Outputs) == 1 && // getters return only one thing
m.Type == abi.Function && // getters are not fallback, constructors, etc
len(m.Inputs) == 0 && // getters have no inputs
m.Outputs[0].Type.String() == "address" // and we only care about addr
}
for _, m := range methods {
if isRelevantMethod(m) {
selectors[m.Name] = m.ID
}
}
return selectors
}