-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder.go
407 lines (336 loc) · 9.74 KB
/
decoder.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
// Copyright 2021 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eth
import (
"encoding/binary"
"encoding/hex"
"fmt"
"math/big"
"strings"
"go.uber.org/zap"
)
type Decoder struct {
buffer []byte
offset uint64
total uint64
}
func NewDecoderFromString(input string) (*Decoder, error) {
data, err := NewHex(input)
if err != nil {
return nil, fmt.Errorf("unable to decode hex input %q: %w", input, err)
}
return NewDecoder(data), nil
}
func NewDecoder(input []byte) *Decoder {
return &Decoder{
buffer: input,
offset: 0,
total: uint64(len(input)),
}
}
func (d *Decoder) String() string {
return fmt.Sprintf("offset %d, total: %d", d.offset, d.total)
}
func (d *Decoder) SetBytes(input []byte) *Decoder {
d.buffer = input
d.offset = 0
d.total = uint64(len(input))
return d
}
func (d *Decoder) ReadMethodCall() (*MethodCall, error) {
methodSignature, err := d.ReadMethod()
if err != nil {
return nil, err
}
methodDef, err := NewMethodDef(methodSignature)
if err != nil {
return nil, err
}
// Method offset of 4 since all offset jump must take into accounts the first 4 bytes of the input
parameters, err := d.readParameters(methodDef.Parameters, 4)
if err != nil {
return nil, fmt.Errorf("read parameters: %w", err)
}
return methodDef.NewCall(parameters...), nil
}
func (d *Decoder) ReadOutput(parameters []*MethodParameter) (out []interface{}, err error) {
return d.readParameters(parameters, 0)
}
func (d *Decoder) readParameters(parameters []*MethodParameter, methodOffset uint64) (out []interface{}, err error) {
out = make([]interface{}, len(parameters))
for i, param := range parameters {
var currentOffset uint64
isOffset := isOffsetType(param.TypeName)
if isOffset {
currentOffset = d.offset
offset, err := d.read("uint256")
if err != nil {
return nil, fmt.Errorf("read offset for type %q (element #%d) at offset %d: %w", param.TypeName, i, d.offset, err)
}
jumpToOffset := offset.(*big.Int).Uint64() + methodOffset
zlog.Debug("about to jump to offset for type", zap.Uint64("actual_offset", d.offset), zap.Uint64("jump_to_offset", jumpToOffset))
// The minus 32 is to ensure that offset hits a location where at least 32 bytes can be read
if jumpToOffset > d.total-32 {
return nil, NewErrDecoding("invalid offset value %d (max possible value %d) for type %q (element #%d) at offset %d", jumpToOffset, d.total-32, param.TypeName, i, d.offset)
}
d.offset = jumpToOffset
}
value, err := d.Read(param.TypeName)
if err != nil {
return nil, fmt.Errorf("read type %q (element #%d) at offset %d: %w", param.TypeName, i, d.offset, err)
}
if isOffset {
d.offset = (currentOffset + 32)
}
out[i] = value
}
return
}
func (d *Decoder) Read(typeName string) (interface{}, error) {
var isAnArray bool
isAnArray, resolvedTypeName := isArray(typeName)
if !isAnArray {
return d.read(resolvedTypeName)
}
length, err := d.read("uint256")
if err != nil {
return nil, fmt.Errorf("cannot read slice %s size: %w", typeName, err)
}
size := length.(*big.Int).Uint64()
arr, err := newArray(resolvedTypeName, size)
if err != nil {
return nil, fmt.Errorf("cannot setup new array: %w", err)
}
for i := uint64(0); i < size; i++ {
out, err := d.read(resolvedTypeName)
if err != nil {
return nil, fmt.Errorf("cannot read item from slice %s.%d: %w", typeName, i, err)
}
arr.At(i, out)
}
return arr, nil
}
func (d *Decoder) read(typeName string) (out interface{}, err error) {
switch typeName {
case "bool":
return d.ReadBool()
case "uint8":
v, err := d.ReadUint64()
if err != nil {
return nil, err
}
return uint8(v), nil
case "uint16":
v, err := d.ReadUint64()
if err != nil {
return nil, err
}
return uint16(v), nil
case "uint24":
v, err := d.ReadUint64()
if err != nil {
return nil, err
}
return uint32(v), nil
case "uint32":
v, err := d.ReadUint64()
if err != nil {
return nil, err
}
return uint32(v), nil
case "uint40":
v, err := d.ReadUint64()
if err != nil {
return nil, err
}
return v, nil
case "uint48":
v, err := d.ReadUint64()
if err != nil {
return nil, err
}
return v, nil
case "uint56":
v, err := d.ReadUint64()
if err != nil {
return nil, err
}
return v, nil
case "uint64":
v, err := d.ReadUint64()
if err != nil {
return nil, err
}
return v, nil
case "uint72", "uint80", "uint88", "uint96", "uint104", "uint112", "uint120", "uint128", "uint136", "uint144", "uint152", "uint160", "uint168", "uint176", "uint184", "uint192", "uint200", "uint208", "uint216", "uint224", "uint232", "uint240", "uint248", "uint256":
return d.ReadBigInt()
case "method":
return d.ReadMethod()
case "address":
return d.ReadAddress()
case "string":
return d.ReadString()
case "bytes32":
return d.ReadFixedBytes()
case "bytes":
return d.ReadBytes()
}
return nil, NewErrDecoding("type %q is not handled right now", typeName)
}
func (d *Decoder) ReadMethod() (out string, err error) {
data, err := d.ReadBuffer(4)
if err != nil {
return out, err
}
signatureID := hex.EncodeToString(data)
out, ok := KnownSignatures[signatureID]
if !ok {
return "", NewErrDecoding("method signature not found for %s", signatureID)
}
return out, nil
}
func (d *Decoder) ReadBool() (out bool, err error) {
data, err := d.ReadBuffer(32)
if err != nil {
return out, err
}
return (data[31] == byte(0x01)), nil
}
func (d *Decoder) ReadString() (out string, err error) {
size, err := d.ReadBigInt()
if err != nil {
return out, err
}
remaining := 32 - (size.Uint64() % 32)
data, err := d.ReadBuffer(size.Uint64())
if err != nil {
return out, err
}
out = strings.ToValidUTF8(string(data), "�")
d.offset += remaining
return
}
func (d *Decoder) ReadBytes() ([]byte, error) {
size, err := d.ReadBigInt()
if err != nil {
return nil, err
}
data, err := d.ReadBuffer(size.Uint64())
if err != nil {
return nil, err
}
return data, nil
}
func (d *Decoder) ReadFixedBytes() ([]byte, error) {
data, err := d.ReadBuffer(32)
if err != nil {
return nil, err
}
return data, nil
}
func (d *Decoder) ReadAddress() (out Address, err error) {
data, err := d.ReadBuffer(32)
if err != nil {
return out, err
}
address := Address(data[12:])
if tracer.Enabled() {
zlog.Debug("read address", zap.Stringer("value", address))
}
return address, nil
}
func (d *Decoder) ReadUint64() (out uint64, err error) {
data, err := d.ReadBuffer(32)
if err != nil {
return out, err
}
return binary.BigEndian.Uint64(data[24:]), nil
}
func (d *Decoder) ReadBigInt() (out *big.Int, err error) {
data, err := d.ReadBuffer(32)
if err != nil {
return out, err
}
return new(big.Int).SetBytes(data[:]), nil
}
func (d *Decoder) ReadBuffer(byteCount uint64) ([]byte, error) {
if tracer.Enabled() {
zlog.Debug("trying to read bytes", zap.Uint64("byte_count", byteCount), zap.Uint64("remaining", d.total-d.offset))
}
if d.total-d.offset < byteCount {
return nil, NewErrDecoding("not enough bytes to read %d bytes, only %d remaining", byteCount, d.total-d.offset)
}
out := d.buffer[d.offset : d.offset+byteCount]
if tracer.Enabled() {
zlog.Debug("read bytes", zap.Uint64("byte_count", byteCount), zap.String("data", hex.EncodeToString(out)))
}
d.offset += byteCount
return out, nil
}
type decodedArray interface {
At(index uint64, value interface{})
}
func newArray(typeName string, count uint64) (decodedArray, error) {
switch typeName {
case "bool":
return BoolArray(make([]bool, count)), nil
case "uint8":
return Uint8Array(make([]uint8, count)), nil
case "uint16":
return Uint16Array(make([]uint16, count)), nil
case "uint24", "uint32":
return Uint32Array(make([]uint32, count)), nil
case "uint40", "uint48", "uint56", "uint64":
return Uint64Array(make([]uint64, count)), nil
case "uint72", "uint80", "uint88", "uint96", "uint104", "uint112", "uint120", "uint128", "uint136", "uint144", "uint152", "uint160", "uint168", "uint176", "uint184", "uint192", "uint200", "uint208", "uint216", "uint224", "uint232", "uint240", "uint248", "uint256":
return BigIntArray(make([]*big.Int, count)), nil
case "address":
return AddressArray(make([]Address, count)), nil
case "string":
return StringArray(make([]string, count)), nil
}
return nil, NewErrDecoding("array of type %q is not handled right now", typeName)
}
type BoolArray []bool
func (a BoolArray) At(index uint64, value interface{}) {
([]bool)(a)[index] = value.(bool)
}
type StringArray []string
func (a StringArray) At(index uint64, value interface{}) {
([]string)(a)[index] = value.(string)
}
type AddressArray []Address
func (a AddressArray) At(index uint64, value interface{}) {
([]Address)(a)[index] = value.(Address)
}
type BigIntArray []*big.Int
func (a BigIntArray) At(index uint64, value interface{}) {
([]*big.Int)(a)[index] = value.(*big.Int)
}
type Uint8Array []uint8
func (a Uint8Array) At(index uint64, value interface{}) {
([]uint8)(a)[index] = value.(uint8)
}
type Uint16Array []uint16
func (a Uint16Array) At(index uint64, value interface{}) {
([]uint16)(a)[index] = value.(uint16)
}
type Uint32Array []uint32
func (a Uint32Array) At(index uint64, value interface{}) {
([]uint32)(a)[index] = value.(uint32)
}
type Uint64Array []uint64
func (a Uint64Array) At(index uint64, value interface{}) {
([]uint64)(a)[index] = value.(uint64)
}