-
Notifications
You must be signed in to change notification settings - Fork 5
/
abi.go
216 lines (201 loc) · 6.41 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
package abi
import (
"fmt"
"reflect"
"unicode"
"github.com/defiweb/go-anymapper"
)
// Default is the default ABI instance that is used by the package-level
// functions.
//
// It is recommended to create a new ABI instance using NewABI rather than
// modifying the default instance, as this can potentially interfere with
// other packages that use the default ABI instance.
var Default = NewABI()
// ABI structure implements the Ethereum ABI (Application Binary Interface).
//
// It provides methods for working with ABI, such as parsing, encoding and
// decoding data.
//
// The package provides default ABI instance that is used by the package-level
// functions. It is possible to create custom ABI instances and use them
// instead of the default one.
type ABI struct {
// Types is a map of known ABI types.
// The key is the name of the type, and the value is the type.
Types map[string]Type
// Mapper is used to map values to and from ABI types.
Mapper Mapper
}
// Mapper used to map values to and from ABI types.
type Mapper interface {
Map(src any, dst any) error
}
// MapFrom maps the value from the ABI Value.
type MapFrom interface {
// MapFrom maps the value from the ABI Value.
//
// src is never a pointer.
MapFrom(m Mapper, src any) error
}
// MapTo maps the value to the ABI Value.
type MapTo interface {
// MapTo maps the value to the ABI Value.
//
// dst is always an initialized pointer.
MapTo(m Mapper, dst any) error
}
// NewABI creates a new ABI instance.
//
// For most use cases, the default ABI instance should be used instead of
// creating a new one.
func NewABI() *ABI {
mapper := anymapper.New()
mapper.Context.Tag = "abi"
mapper.Context.FieldMapper = fieldMapper
// Those hooks add support for MapTo and MapFrom interfaces.
//
// Interfaces are used only if a source or destination type implements
// the Value interface.
//
// If both types implement MapTo/MapFrom, then the method from the value
// that does NOT implement Value interface is used. This is done to ensure,
// that custom types that implement MapTo/MapFrom interface have priority
// over the default mapping functions.
mapper.Hooks = anymapper.Hooks{
MapFuncHook: func(m *anymapper.Mapper, src, dst reflect.Type) anymapper.MapFunc {
srcImplMapTo := src.Implements(mapToTy)
dstImplMapFrom := dst.Implements(mapFromTy)
switch {
case srcImplMapTo && dstImplMapFrom:
if src.Implements(valueTy) {
return func(m *anymapper.Mapper, _ *anymapper.Context, src, dst reflect.Value) error {
return dst.Interface().(MapFrom).MapFrom(m, src.Interface())
}
}
if dst.Implements(valueTy) {
return func(m *anymapper.Mapper, _ *anymapper.Context, src, dst reflect.Value) error {
return src.Interface().(MapTo).MapTo(m, addr(dst).Interface())
}
}
case srcImplMapTo:
return func(m *anymapper.Mapper, _ *anymapper.Context, src, dst reflect.Value) error {
return src.Interface().(MapTo).MapTo(m, addr(dst).Interface())
}
case dstImplMapFrom:
return func(m *anymapper.Mapper, _ *anymapper.Context, src, dst reflect.Value) error {
return dst.Interface().(MapFrom).MapFrom(m, src.Interface())
}
}
return nil
},
SourceValueHook: func(v reflect.Value) reflect.Value {
for {
// If the source implements MapTo, then return it but
// first dereference it.
if _, ok := v.Interface().(MapTo); ok {
for v.Kind() == reflect.Interface {
v = v.Elem()
}
return v
}
// If the source is not a pointer or interface, then
// break the loop and return an empty value. Returning an
// empty value will cause the default mapping function to
// be used. Otherwise, dereference the source and continue
// the loop.
if v.Kind() != reflect.Interface && v.Kind() != reflect.Ptr {
break
}
v = v.Elem()
}
return reflect.Value{}
},
DestinationValueHook: func(v reflect.Value) reflect.Value {
for {
// If the destination is a nil interface, then return it.
if v.Kind() == reflect.Interface && v.IsNil() {
return v
}
// If the destination is a nil pointer, then initialize it.
if v.Kind() == reflect.Ptr && v.IsNil() {
if !v.CanSet() {
return reflect.Value{}
}
v.Set(reflect.New(v.Type().Elem()))
}
// If the destination implements MapFrom, then return it but
// first dereference it.
if _, ok := v.Interface().(MapFrom); ok {
for v.Kind() == reflect.Interface {
v = v.Elem()
}
return v
}
// If the destination is not a pointer or interface, then
// break the loop and return an empty value. Returning an
// empty value will cause the anymapper package to ignore
// this hook. Otherwise, dereference the destination and
// continue the loop.
if v.Kind() != reflect.Interface && v.Kind() != reflect.Ptr {
break
}
v = v.Elem()
}
return reflect.Value{}
},
}
types := map[string]Type{}
types["bool"] = NewBoolType()
types["bytes"] = NewBytesType()
types["string"] = NewStringType()
types["address"] = NewAddressType()
types["int"] = NewAliasType("int", NewIntType(256))
types["uint"] = NewAliasType("uint", NewUintType(256))
for i := 1; i <= 32; i++ {
types[fmt.Sprintf("int%d", i*8)] = NewIntType(i * 8)
types[fmt.Sprintf("uint%d", i*8)] = NewUintType(i * 8)
types[fmt.Sprintf("bytes%d", i)] = NewFixedBytesType(i)
}
return &ABI{
Types: types,
Mapper: mapper,
}
}
// fieldMapper lowercase the first letter of the field name. If the field name
// starts with an acronym, it will lowercase the whole acronym. For example:
// - "User" will be mapped to "user"
// - "ID" will be mapped to "id"
// - "DAPPName" will be mapped to "dappName"
//
// Unfortunately, it does not work with field names that contain two acronyms
// next to each other. For example, "DAPPID" will be mapped to "dappid".
var fieldMapper = func(field string) string {
if len(field) == 0 {
return field
}
runes := []rune(field)
for i, c := range runes {
if unicode.IsUpper(c) && (i == 0 || i == len(runes)-1 || !(unicode.IsLower(runes[i+1]))) {
runes[i] = unicode.ToLower(c)
}
if unicode.IsLower(c) {
break
}
}
return string(runes)
}
func addr(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Ptr {
return v
}
if v.CanAddr() {
return v.Addr()
}
return v
}
var (
valueTy = reflect.TypeOf((*Value)(nil)).Elem()
mapFromTy = reflect.TypeOf((*MapFrom)(nil)).Elem()
mapToTy = reflect.TypeOf((*MapTo)(nil)).Elem()
)