forked from omnes-tech/abi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
156 lines (129 loc) · 4.83 KB
/
common.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
package abi
import (
"fmt"
"strconv"
"strings"
)
// IsDynamic checks whether given type string is a dynamic,
// i.e. if it is either a string, bytes, or an array.
func IsDynamic(typeStr string, isTuple bool) bool {
openBracketsIndex := strings.Index(typeStr, "[")
closeBracketsIndex := strings.Index(typeStr, "]")
if openBracketsIndex == -1 {
if strings.Contains(typeStr, "string") ||
typeStr == "bytes" ||
(isTuple && (strings.Contains(typeStr, "bytes,") || strings.Contains(typeStr, "bytes)"))) {
return true
}
} else if closeBracketsIndex == openBracketsIndex+1 {
return true
}
return false
}
// IsArray checks whether given type string is an array.
// Returns whether or not it is an array and the array length
// for bounded arrays (i.e. `uint256[3]`). Array length can
// be 0 wheter an error occured, it is not an array,
// or it is an unbounded array (i.e. `uint256[]`).
func IsArray(typeStr string) (bool, int, error) {
if strings.Count(typeStr, "[") != strings.Count(typeStr, "]") {
return false, 0, fmt.Errorf("invalid array definition")
}
if strings.Count(typeStr, "[") > 0 {
openBracketIndex := strings.LastIndex(typeStr, "[")
closeBracketIndex := strings.LastIndex(typeStr, "]")
closeParenthesisIndex := strings.LastIndex(typeStr, ")")
if openBracketIndex < closeParenthesisIndex || closeBracketIndex < closeParenthesisIndex {
return false, 0, nil
}
var arraySize int
var err error
if closeBracketIndex > openBracketIndex+1 {
arraySize, err = strconv.Atoi(typeStr[openBracketIndex+1 : closeBracketIndex])
if err != nil {
return false, 0, fmt.Errorf("invalid array definition")
}
}
return true, arraySize, nil
}
return false, 0, nil
}
// IsTuple checks whether given type string is a tuple (i.e. `(uint256,bytes,address)`).
// Also returns the array of type strings in the tuple (i.e. [uint256,bytes,address]).
func IsTuple(typeStr string) (bool, []string, error) {
if strings.Count(typeStr, "(") != strings.Count(typeStr, ")") {
return false, nil, fmt.Errorf("invalid tuple definition")
}
if strings.Count(typeStr, "(") > 0 {
openParenthesisIndex := strings.Index(typeStr, "(")
closeParenthesisIndex := strings.LastIndex(typeStr, ")")
innerCloseParenthesisIndex := strings.LastIndex(typeStr[:closeParenthesisIndex], ")")
innerCloseBracketsIndex := strings.LastIndex(typeStr[:closeParenthesisIndex], "]")
var splitTypes []string
if innerCloseParenthesisIndex != -1 &&
innerCloseBracketsIndex != -1 {
innerOpenParenthesisIndex := strings.Index(typeStr[openParenthesisIndex+1:closeParenthesisIndex], "(") + len(typeStr[:openParenthesisIndex]) + 1
if innerCloseParenthesisIndex > innerCloseBracketsIndex {
splitTypes = strings.Split(typeStr[openParenthesisIndex+1:innerOpenParenthesisIndex], ",")
splitTypes = append(splitTypes, typeStr[innerOpenParenthesisIndex:innerCloseParenthesisIndex+1])
splitTypes = append(splitTypes, strings.Split(typeStr[innerCloseParenthesisIndex+2:closeParenthesisIndex], ",")...)
} else {
splitTypes = strings.Split(typeStr[openParenthesisIndex+1:innerOpenParenthesisIndex], ",")
splitTypes = append(splitTypes, typeStr[innerOpenParenthesisIndex:innerCloseBracketsIndex+1])
splitTypes = append(splitTypes, strings.Split(typeStr[innerCloseBracketsIndex+2:closeParenthesisIndex], ",")...)
}
} else {
splitTypes = SplitParams(typeStr[openParenthesisIndex+1 : closeParenthesisIndex])
}
splitTypes = cleanSplitTypes(splitTypes)
return true, splitTypes, nil
}
return false, nil, nil
}
// GetSigTypes gets the input parameters type from given function
// signature.
// Calls splitParams function.
func GetSigTypes(funcSig string) ([]string, error) {
openParIndex := strings.Index(funcSig, "(")
if openParIndex == -1 {
return []string{}, fmt.Errorf("no opening parenthesis found in function signature")
}
closeParIndex := strings.LastIndex(funcSig, ")")
if closeParIndex == -1 {
return []string{}, fmt.Errorf("no closing parenthesis found in function signature")
}
return SplitParams(funcSig[openParIndex+1 : closeParIndex]), nil
}
// SplitParams splits parameters type from given type string
func SplitParams(typesStr string) []string {
if len(typesStr) == 0 {
return nil
}
var result []string
var buffer []string
insideParentheses := 0
for _, char := range typesStr {
if string(char) == "(" {
insideParentheses += 1
} else if string(char) == ")" {
insideParentheses -= 1
}
if string(char) == "," && insideParentheses <= 0 {
result = append(result, strings.Join(buffer, ""))
buffer = []string{}
} else {
buffer = append(buffer, string(char))
}
}
result = append(result, strings.Join(buffer, ""))
return result
}
func cleanSplitTypes(splitTypes []string) []string {
var result []string
for _, typeStr := range splitTypes {
if typeStr != "" {
result = append(result, typeStr)
}
}
return result
}