This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcommon.go
322 lines (296 loc) · 6.58 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
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
package common
import (
"encoding/binary"
"encoding/hex"
"errors"
"log"
"math/big"
"reflect"
"strconv"
"github.com/opennetsys/golkadot/common/bnutil"
"github.com/opennetsys/golkadot/common/hexutil"
"github.com/opennetsys/golkadot/common/u8compact"
"github.com/opennetsys/golkadot/common/u8util"
)
// ErrTypeUnsupported ...
var ErrTypeUnsupported = errors.New("type not supported")
// TypeToU8a ...
func TypeToU8a(value interface{}, isBare bool) []uint8 {
switch v := value.(type) {
case *big.Int:
return bnutil.ToUint8Slice(v, 64, true, false)
case int8:
return bnutil.ToUint8Slice(big.NewInt(int64(v)), 8, true, false)
case int16:
return bnutil.ToUint8Slice(big.NewInt(int64(v)), 16, true, false)
case int32:
return bnutil.ToUint8Slice(big.NewInt(int64(v)), 32, true, false)
case int64:
return bnutil.ToUint8Slice(big.NewInt(v), 64, true, false)
case uint8:
return bnutil.ToUint8Slice(big.NewInt(int64(v)), 8, true, false)
case uint16:
return bnutil.ToUint8Slice(big.NewInt(int64(v)), 16, true, false)
case uint32:
return bnutil.ToUint8Slice(big.NewInt(int64(v)), 32, true, false)
case uint64:
return bnutil.ToUint8Slice(big.NewInt(int64(v)), 64, true, false)
case []byte:
return v[:]
case *string:
var encoded []byte
if v != nil {
encoded = []byte(*v)
}
if isBare {
return encoded
}
return u8compact.AddLength(encoded, -1)
case string:
if isBare {
return []byte(v)
}
return u8compact.AddLength([]byte(v), -1)
}
return nil
}
// TypeLen ...
func TypeLen(value interface{}) int {
switch v := value.(type) {
case *big.Int:
return v.BitLen()
case int8:
return 8
case int16:
return 16
case int32:
return 32
case int64:
return 64
case uint8:
return 8
case uint16:
return 16
case uint32:
return 32
case uint64:
return 64
case *string:
if v != nil {
return len(*v)
}
case string:
return len(v)
}
return 0
}
// TypeBitLen ...
func TypeBitLen(value interface{}) int {
switch v := value.(type) {
case *big.Int:
return v.BitLen()
case int8:
return 8
case int16:
return 16
case int32:
return 32
case int64:
return 64
case uint8:
return 8
case uint16:
return 16
case uint32:
return 32
case uint64:
return 64
}
return 0
}
// TypeEncodedLen ...
func TypeEncodedLen(value interface{}) int {
switch v := value.(type) {
case *big.Int:
if v == nil {
return 0
}
return v.BitLen()
case int8:
return 8
case int16:
return 16
case int32:
return 32
case int64:
return 64
case uint8:
return 8
case uint16:
return 16
case uint32:
return 32
case uint64:
return 64
case *string:
if v == nil {
return 0
}
return TypeLen(v) + len(u8compact.CompactToUint8Slice(big.NewInt(int64(TypeLen(v))), -1))
case string:
return TypeLen(v) + len(u8compact.CompactToUint8Slice(big.NewInt(int64(TypeLen(v))), -1))
}
return 0
}
// TypeToString ...
func TypeToString(value interface{}) string {
switch v := value.(type) {
case *big.Int:
return v.String()
case int8:
return strconv.Itoa(int(v))
case int16:
return strconv.Itoa(int(v))
case int32:
return strconv.Itoa(int(v))
case int64:
return strconv.Itoa(int(v))
case uint8:
return strconv.Itoa(int(v))
case uint16:
return strconv.Itoa(int(v))
case uint32:
return strconv.Itoa(int(v))
case uint64:
return strconv.Itoa(int(v))
case *string:
if v != nil {
return *v
}
return ""
case string:
return v
case []uint8:
offset, length := u8compact.FromUint8Slice(v, 8)
end := int(offset) + int(length.Uint64())
if end > len(v) {
end = len(v)
}
return string(v[offset:end])
}
return ""
}
// TypeToHex ...
func TypeToHex(value interface{}) string {
switch v := value.(type) {
case *big.Int:
return hexutil.HexFixLength(hex.EncodeToString(v.Bytes()), TypeBitLen(v), true)
case int8:
return hexutil.HexFixLength(hex.EncodeToString(TypeToBytes(v)), 8, true)
case int16:
return hexutil.HexFixLength(hex.EncodeToString(TypeToBytes(v)), 16, true)
case int32:
return hexutil.HexFixLength(hex.EncodeToString(TypeToBytes(v)), 32, true)
case int64:
return hexutil.HexFixLength(hex.EncodeToString(TypeToBytes(v)), 64, true)
case uint8:
return hexutil.HexFixLength(hex.EncodeToString(TypeToBytes(v)), 8, true)
case uint16:
return hexutil.HexFixLength(hex.EncodeToString(TypeToBytes(v)), 16, true)
case uint32:
return hexutil.HexFixLength(hex.EncodeToString(TypeToBytes(v)), 32, true)
case uint64:
return hexutil.HexFixLength(hex.EncodeToString(TypeToBytes(v)), 61, true)
case []uint8:
return hexutil.HexFixLength(hex.EncodeToString(v), len(v), true)
case *string:
if hexutil.ValidHex(*v) {
return u8util.ToHex(TypeToU8a(*v, false), -1, true)
}
return u8util.ToHex(TypeToU8a(*v, false), -1, true)
case string:
if hexutil.ValidHex(v) {
return u8util.ToHex(TypeToU8a(v, false), -1, true)
}
return u8util.ToHex(TypeToU8a(v, false), -1, true)
default:
log.Fatal(ErrTypeUnsupported)
}
return ""
}
// TypeEquals ...
func TypeEquals(value interface{}, other interface{}) bool {
return TypeToString(value) == TypeToString(other)
}
// TypeIsZeroValue ...
func TypeIsZeroValue(value interface{}) bool {
switch v := value.(type) {
case *big.Int:
return v.Cmp(big.NewInt(0)) == 0
case int8:
return v == 0
case int16:
return v == 0
case int32:
return v == 0
case int64:
return v == 0
case uint8:
return v == 0
case uint16:
return v == 0
case uint32:
return v == 0
case uint64:
return v == 0
default:
log.Fatal(ErrTypeUnsupported)
}
return false
}
// TypeToBytes ...
func TypeToBytes(value interface{}) []byte {
switch v := value.(type) {
case *big.Int:
return v.Bytes()
case int8:
bs := make([]byte, 64)
binary.LittleEndian.PutUint64(bs, uint64(v))
return bs[:1]
case int16:
bs := make([]byte, 64)
binary.LittleEndian.PutUint64(bs, uint64(v))
return bs[:2]
case int32:
bs := make([]byte, 64)
binary.LittleEndian.PutUint64(bs, uint64(v))
return bs[:4]
case int64:
bs := make([]byte, 64)
binary.LittleEndian.PutUint64(bs, uint64(v))
return bs[:8]
case uint8:
bs := make([]byte, 64)
binary.LittleEndian.PutUint64(bs, uint64(v))
return bs[:1]
case uint16:
bs := make([]byte, 64)
binary.LittleEndian.PutUint64(bs, uint64(v))
return bs[:2]
case uint32:
bs := make([]byte, 64)
binary.LittleEndian.PutUint64(bs, uint64(v))
return bs[:4]
case uint64:
bs := make([]byte, 64)
binary.LittleEndian.PutUint64(bs, uint64(v))
return bs[:8]
default:
log.Fatal(ErrTypeUnsupported)
}
return nil
}
// TypeIsNil ...
func TypeIsNil(value interface{}) bool {
return value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil())
}