-
Notifications
You must be signed in to change notification settings - Fork 66
/
abi.go
415 lines (366 loc) · 9.21 KB
/
abi.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// SPDX-License-Identifier: Apache-2.0
package hiero
import (
"bytes"
"encoding/json"
"fmt"
"hash"
"io"
"regexp"
"strings"
"sync"
"golang.org/x/crypto/sha3"
)
// ABI represents the ethereum abi format
type ABI struct {
Constructor *Method
Methods map[string]*Method
MethodsBySignature map[string]*Method
Events map[string]*Event
Errors map[string]*Error
}
func (a *ABI) GetMethod(name string) *Method {
m := a.Methods[name]
return m
}
func (a *ABI) GetMethodBySignature(methodSignature string) *Method {
m := a.MethodsBySignature[methodSignature]
return m
}
func (a *ABI) addError(e *Error) {
if len(a.Errors) == 0 {
a.Errors = map[string]*Error{}
}
a.Errors[e.Name] = e
}
func (a *ABI) addEvent(e *Event) {
if len(a.Events) == 0 {
a.Events = map[string]*Event{}
}
name := overloadedName(e.Name, func(s string) bool {
_, ok := a.Events[s]
return ok
})
a.Events[name] = e
}
func (a *ABI) addMethod(m *Method) {
if len(a.Methods) == 0 {
a.Methods = map[string]*Method{}
}
if len(a.MethodsBySignature) == 0 {
a.MethodsBySignature = map[string]*Method{}
}
name := overloadedName(m.Name, func(s string) bool {
_, ok := a.Methods[s]
return ok
})
a.Methods[name] = m
a.MethodsBySignature[m.Sig()] = m
}
func overloadedName(rawName string, isAvail func(string) bool) string {
name := rawName
ok := isAvail(name)
for idx := 0; ok; idx++ {
name = fmt.Sprintf("%s%d", rawName, idx)
ok = isAvail(name)
}
return name
}
// NewABI returns a parsed ABI struct
func NewABI(s string) (*ABI, error) {
return NewABIFromReader(bytes.NewReader([]byte(s)))
}
// NewABIFromReader returns an ABI object from a reader
func NewABIFromReader(r io.Reader) (*ABI, error) {
var abi *ABI
dec := json.NewDecoder(r)
if err := dec.Decode(&abi); err != nil {
return nil, err
}
return abi, nil
}
// UnmarshalJSON implements json.Unmarshaler interface
// nolint
func (a *ABI) UnmarshalJSON(data []byte) error {
var fields []struct {
Type string
Name string
Anonymous bool
StateMutability string
Inputs []*ArgumentStr
Outputs []*ArgumentStr
}
if err := json.Unmarshal(data, &fields); err != nil {
return err
}
for _, field := range fields {
switch field.Type {
case "constructor":
if a.Constructor != nil {
return fmt.Errorf("multiple constructor declaration")
}
input, err := NewTupleTypeFromArgs(field.Inputs)
if err != nil {
return err
}
a.Constructor = &Method{
Inputs: input,
}
case "function", "":
c := field.StateMutability == "view" || field.StateMutability == "pure"
inputs, err := NewTupleTypeFromArgs(field.Inputs)
if err != nil {
return err
}
outputs, err := NewTupleTypeFromArgs(field.Outputs)
if err != nil {
return err
}
method := &Method{
Name: field.Name,
Const: c,
Inputs: inputs,
Outputs: outputs,
}
a.addMethod(method)
case "event":
input, err := NewTupleTypeFromArgs(field.Inputs)
if err != nil {
return err
}
event := &Event{
Name: field.Name,
Anonymous: field.Anonymous,
Inputs: input,
}
a.addEvent(event)
case "error":
input, err := NewTupleTypeFromArgs(field.Inputs)
if err != nil {
return err
}
errObj := &Error{
Name: field.Name,
Inputs: input,
}
a.addError(errObj)
case "fallback":
case "receive":
// do nothing
default:
return fmt.Errorf("unknown field type '%s'", field.Type)
}
}
return nil
}
// nolint
func NewABIFromList(humanReadableAbi []string) (*ABI, error) {
res := &ABI{}
for _, c := range humanReadableAbi {
if strings.HasPrefix(c, "constructor") {
typ, err := NewType("tuple" + strings.TrimPrefix(c, "constructor"))
if err != nil {
return nil, err
}
res.Constructor = &Method{
Inputs: typ,
}
} else if strings.HasPrefix(c, "function ") {
method, err := NewMethod(c)
if err != nil {
return nil, err
}
res.addMethod(method)
} else if strings.HasPrefix(c, "event ") {
evnt, err := NewEvent(c)
if err != nil {
return nil, err
}
res.addEvent(evnt)
} else if strings.HasPrefix(c, "error ") {
errTyp, err := NewError(c)
if err != nil {
return nil, err
}
res.addError(errTyp)
} else {
return nil, fmt.Errorf("either event or function expected")
}
}
return res, nil
}
// Method is a callable function in the contract
type Method struct {
Name string
Const bool
Inputs *Type
Outputs *Type
}
// Sig returns the signature of the method
func (m *Method) Sig() string {
return buildSignature(m.Name, m.Inputs)
}
// ID returns the id of the method
func (m *Method) ID() []byte {
k := acquireKeccak()
k.Write([]byte(m.Sig()))
dst := k.Sum(nil)[:4]
releaseKeccak(k)
return dst
}
// Encode encodes the inputs with this function
func (m *Method) Encode(args interface{}) ([]byte, error) {
data, err := Encode(args, m.Inputs)
if err != nil {
return nil, err
}
data = append(m.ID(), data...)
return data, nil
}
// Decode decodes the output with this function
func (m *Method) Decode(data []byte) (map[string]interface{}, error) {
if len(data) == 0 {
return nil, fmt.Errorf("empty response")
}
respInterface, err := Decode(m.Outputs, data)
if err != nil {
return nil, err
}
resp := respInterface.(map[string]interface{})
return resp, nil
}
func NewMethod(name string) (*Method, error) {
name, inputs, outputs, err := parseMethodSignature(name)
if err != nil {
return nil, err
}
m := &Method{Name: name, Inputs: inputs, Outputs: outputs}
return m, nil
}
var (
funcRegexpWithReturn = regexp.MustCompile(`(\w*)\s*\((.*)\)(.*)\s*returns\s*\((.*)\)`)
funcRegexpWithoutReturn = regexp.MustCompile(`(\w*)\s*\((.*)\)(.*)`)
)
// Event is a triggered log mechanism
type Event struct {
Name string
Anonymous bool
Inputs *Type
}
// NewEvent creates a new solidity event object using the signature
func NewEvent(name string) (*Event, error) {
name, typ, err := parseEventOrErrorSignature("event ", name)
if err != nil {
return nil, err
}
return NewEventFromType(name, typ), nil
}
// NewEventFromType creates a new solidity event object using the name and type
func NewEventFromType(name string, typ *Type) *Event {
return &Event{Name: name, Inputs: typ}
}
// Error is a solidity error object
type Error struct {
Name string
Inputs *Type
}
// NewError creates a new solidity error object
func NewError(name string) (*Error, error) {
name, typ, err := parseEventOrErrorSignature("error ", name)
if err != nil {
return nil, err
}
return &Error{Name: name, Inputs: typ}, nil
}
// ArgumentStr encodes a type object
type ArgumentStr struct {
Name string
Type string
Indexed bool
Components []*ArgumentStr
InternalType string
}
var keccakPool = sync.Pool{
New: func() interface{} {
return sha3.NewLegacyKeccak256()
},
}
func acquireKeccak() hash.Hash {
return keccakPool.Get().(hash.Hash)
}
func releaseKeccak(k hash.Hash) {
k.Reset()
keccakPool.Put(k)
}
type Log struct {
Removed bool
LogIndex uint64
TransactionIndex uint64
TransactionHash Hash
BlockHash Hash
BlockNumber uint64
Address Address
Topics []Hash
Data []byte
}
// nolint
func parseMethodSignature(name string) (string, *Type, *Type, error) {
name = strings.Replace(name, "\n", " ", -1)
name = strings.Replace(name, "\t", " ", -1)
name = strings.TrimPrefix(name, "function ")
name = strings.TrimSpace(name)
var funcName, inputArgs, outputArgs string
if strings.Contains(name, "returns") {
matches := funcRegexpWithReturn.FindAllStringSubmatch(name, -1)
if len(matches) == 0 {
return "", nil, nil, fmt.Errorf("no matches found")
}
funcName = strings.TrimSpace(matches[0][1])
inputArgs = strings.TrimSpace(matches[0][2])
outputArgs = strings.TrimSpace(matches[0][4])
} else {
matches := funcRegexpWithoutReturn.FindAllStringSubmatch(name, -1)
if len(matches) == 0 {
return "", nil, nil, fmt.Errorf("no matches found")
}
funcName = strings.TrimSpace(matches[0][1])
inputArgs = strings.TrimSpace(matches[0][2])
}
input, err := NewType("tuple(" + inputArgs + ")")
if err != nil {
return "", nil, nil, err
}
output, err := NewType("tuple(" + outputArgs + ")")
if err != nil {
return "", nil, nil, err
}
return funcName, input, output, nil
}
func buildSignature(name string, typ *Type) string {
types := make([]string, len(typ.tuple))
for i, input := range typ.tuple {
// nolint
types[i] = strings.Replace(input.Elem.String(), "tuple", "", -1)
}
return fmt.Sprintf("%v(%v)", name, strings.Join(types, ","))
}
func parseEventOrErrorSignature(prefix string, name string) (string, *Type, error) {
if !strings.HasPrefix(name, prefix) {
return "", nil, fmt.Errorf("prefix '%s' not found", prefix)
}
name = strings.TrimPrefix(name, prefix)
if !strings.HasSuffix(name, ")") {
return "", nil, fmt.Errorf("failed to parse input, expected 'name(types)'")
}
indx := strings.Index(name, "(")
if indx == -1 {
return "", nil, fmt.Errorf("failed to parse input, expected 'name(types)'")
}
funcName, signature := name[:indx], name[indx:]
signature = "tuple" + signature
typ, err := NewType(signature)
if err != nil {
return "", nil, err
}
return funcName, typ, nil
}