-
Notifications
You must be signed in to change notification settings - Fork 441
/
ABIEncoding.swift
executable file
·388 lines (378 loc) · 14.7 KB
/
ABIEncoding.swift
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
//
// Created by Alex Vlasov on 25/10/2018.
// Copyright © 2018 Alex Vlasov. All rights reserved.
//
import Foundation
import BigInt
public struct ABIEncoder {
}
extension ABIEncoder {
public static func convertToBigUInt(_ value: AnyObject) -> BigUInt? {
switch value {
case let v as BigUInt:
return v
case let v as BigInt:
switch v.sign {
case .minus:
return nil
case .plus:
return v.magnitude
}
case let v as String:
let base10 = BigUInt(v, radix: 10)
if base10 != nil {
return base10!
}
let base16 = BigUInt(v.stripHexPrefix(), radix: 16)
if base16 != nil {
return base16!
}
break
case let v as UInt:
return BigUInt(v)
case let v as UInt8:
return BigUInt(v)
case let v as UInt16:
return BigUInt(v)
case let v as UInt32:
return BigUInt(v)
case let v as UInt64:
return BigUInt(v)
case let v as Int:
return BigUInt(v)
case let v as Int8:
return BigUInt(v)
case let v as Int16:
return BigUInt(v)
case let v as Int32:
return BigUInt(v)
case let v as Int64:
return BigUInt(v)
default:
return nil
}
return nil
}
public static func convertToBigInt(_ value: AnyObject) -> BigInt? {
switch value {
case let v as BigUInt:
return BigInt(v)
case let v as BigInt:
return v
case let v as String:
let base10 = BigInt(v, radix: 10)
if base10 != nil {
return base10
}
let base16 = BigInt(v.stripHexPrefix(), radix: 16)
if base16 != nil {
return base16
}
break
case let v as UInt:
return BigInt(v)
case let v as UInt8:
return BigInt(v)
case let v as UInt16:
return BigInt(v)
case let v as UInt32:
return BigInt(v)
case let v as UInt64:
return BigInt(v)
case let v as Int:
return BigInt(v)
case let v as Int8:
return BigInt(v)
case let v as Int16:
return BigInt(v)
case let v as Int32:
return BigInt(v)
case let v as Int64:
return BigInt(v)
default:
return nil
}
return nil
}
public static func convertToData(_ value: AnyObject) -> Data? {
switch value {
case let d as Data:
return d
case let d as String:
if d.hasHexPrefix() {
let hex = Data.fromHex(d)
if hex != nil {
return hex
}
}
let str = d.data(using: .utf8)
if str != nil {
return str
}
case let d as [UInt8]:
return Data(d)
case let d as EthereumAddress:
return d.addressData
case let d as [IntegerLiteralType]:
var bytesArray = [UInt8]()
for el in d {
guard el >= 0, el <= 255 else {return nil}
bytesArray.append(UInt8(el))
}
return Data(bytesArray)
default:
return nil
}
return nil
}
public static func encode(types: [ABI.Element.InOut], values: [AnyObject]) -> Data? {
guard types.count == values.count else {return nil}
let params = types.compactMap { (el) -> ABI.Element.ParameterType in
return el.type
}
return encode(types: params, values: values)
}
public static func encode(types: [ABI.Element.ParameterType], values: [AnyObject]) -> Data? {
guard types.count == values.count else {return nil}
var tails = [Data]()
var heads = [Data]()
for i in 0 ..< types.count {
let enc = encodeSingleType(type: types[i], value: values[i])
guard let encoding = enc else {return nil}
if types[i].isStatic {
heads.append(encoding)
tails.append(Data())
} else {
heads.append(Data(repeating: 0x0, count: 32))
tails.append(encoding)
}
}
var headsConcatenated = Data()
for h in heads {
headsConcatenated.append(h)
}
var tailsPointer = BigUInt(headsConcatenated.count)
headsConcatenated = Data()
var tailsConcatenated = Data()
for i in 0 ..< types.count {
let head = heads[i]
let tail = tails[i]
if !types[i].isStatic {
guard let newHead = tailsPointer.abiEncode(bits: 256) else {return nil}
headsConcatenated.append(newHead)
tailsConcatenated.append(tail)
tailsPointer = tailsPointer + BigUInt(tail.count)
} else {
headsConcatenated.append(head)
tailsConcatenated.append(tail)
}
}
return headsConcatenated + tailsConcatenated
}
public static func encodeSingleType(type: ABI.Element.ParameterType, value: AnyObject) -> Data? {
switch type {
case .uint(_):
if let biguint = convertToBigUInt(value) {
return biguint.abiEncode(bits: 256)
}
if let bigint = convertToBigInt(value) {
return bigint.abiEncode(bits: 256)
}
case .int(_):
if let biguint = convertToBigUInt(value) {
return biguint.abiEncode(bits: 256)
}
if let bigint = convertToBigInt(value) {
return bigint.abiEncode(bits: 256)
}
case .address:
if let string = value as? String {
guard let address = EthereumAddress(string) else {return nil}
let data = address.addressData
return data.setLengthLeft(32)
} else if let address = value as? EthereumAddress {
guard address.isValid else {break}
let data = address.addressData
return data.setLengthLeft(32)
} else if let data = value as? Data {
return data.setLengthLeft(32)
}
case .bool:
if let bool = value as? Bool {
if (bool) {
return BigUInt(1).abiEncode(bits: 256)
} else {
return BigUInt(0).abiEncode(bits: 256)
}
}
case .bytes(let length):
guard let data = convertToData(value) else {break}
if data.count > length {break}
return data.setLengthRight(32)
case .string:
if let string = value as? String {
var dataGuess: Data?
if string.hasHexPrefix() {
dataGuess = Data.fromHex(string.lowercased().stripHexPrefix())
}
else {
dataGuess = string.data(using: .utf8)
}
guard let data = dataGuess else {break}
let minLength = ((data.count + 31) / 32)*32
guard let paddedData = data.setLengthRight(UInt64(minLength)) else {break}
let length = BigUInt(data.count)
guard let head = length.abiEncode(bits: 256) else {break}
let total = head+paddedData
return total
}
case .dynamicBytes:
guard let data = convertToData(value) else {break}
let minLength = ((data.count + 31) / 32)*32
guard let paddedData = data.setLengthRight(UInt64(minLength)) else {break}
let length = BigUInt(data.count)
guard let head = length.abiEncode(bits: 256) else {break}
let total = head+paddedData
return total
case .array(type: let subType, length: let length):
switch type.arraySize {
case .dynamicSize:
guard length == 0 else {break}
guard let val = value as? [AnyObject] else {break}
guard let lengthEncoding = BigUInt(val.count).abiEncode(bits: 256) else {break}
if subType.isStatic {
// work in a previous context
var toReturn = Data()
for i in 0 ..< val.count {
let enc = encodeSingleType(type: subType, value: val[i])
guard let encoding = enc else {break}
toReturn.append(encoding)
}
let total = lengthEncoding + toReturn
// print("Dynamic array of static types encoding :\n" + String(total.toHexString()))
return total
} else {
// create new context
var tails = [Data]()
var heads = [Data]()
for i in 0 ..< val.count {
let enc = encodeSingleType(type: subType, value: val[i])
guard let encoding = enc else {return nil}
heads.append(Data(repeating: 0x0, count: 32))
tails.append(encoding)
}
var headsConcatenated = Data()
for h in heads {
headsConcatenated.append(h)
}
var tailsPointer = BigUInt(headsConcatenated.count)
headsConcatenated = Data()
var tailsConcatenated = Data()
for i in 0 ..< val.count {
let head = heads[i]
let tail = tails[i]
if tail != Data() {
guard let newHead = tailsPointer.abiEncode(bits: 256) else {return nil}
headsConcatenated.append(newHead)
tailsConcatenated.append(tail)
tailsPointer = tailsPointer + BigUInt(tail.count)
} else {
headsConcatenated.append(head)
tailsConcatenated.append(tail)
}
}
let total = lengthEncoding + headsConcatenated + tailsConcatenated
// print("Dynamic array of dynamic types encoding :\n" + String(total.toHexString()))
return total
}
case .staticSize(let staticLength):
guard staticLength != 0 else {break}
guard let val = value as? [AnyObject] else {break}
guard staticLength == val.count else {break}
if subType.isStatic {
// work in a previous context
var toReturn = Data()
for i in 0 ..< val.count {
let enc = encodeSingleType(type: subType, value: val[i])
guard let encoding = enc else {break}
toReturn.append(encoding)
}
// print("Static array of static types encoding :\n" + String(toReturn.toHexString()))
let total = toReturn
return total
} else {
// create new context
var tails = [Data]()
var heads = [Data]()
for i in 0 ..< val.count {
let enc = encodeSingleType(type: subType, value: val[i])
guard let encoding = enc else {return nil}
heads.append(Data(repeating: 0x0, count: 32))
tails.append(encoding)
}
var headsConcatenated = Data()
for h in heads {
headsConcatenated.append(h)
}
var tailsPointer = BigUInt(headsConcatenated.count)
headsConcatenated = Data()
var tailsConcatenated = Data()
for i in 0 ..< val.count {
let tail = tails[i]
guard let newHead = tailsPointer.abiEncode(bits: 256) else {return nil}
headsConcatenated.append(newHead)
tailsConcatenated.append(tail)
tailsPointer = tailsPointer + BigUInt(tail.count)
}
let total = headsConcatenated + tailsConcatenated
// print("Static array of dynamic types encoding :\n" + String(total.toHexString()))
return total
}
case .notArray:
break
}
case .tuple(types: let subTypes):
var tails = [Data]()
var heads = [Data]()
guard let val = value as? [AnyObject] else {break}
for i in 0 ..< subTypes.count {
let enc = encodeSingleType(type: subTypes[i], value: val[i])
guard let encoding = enc else {return nil}
if subTypes[i].isStatic {
heads.append(encoding)
tails.append(Data())
} else {
heads.append(Data(repeating: 0x0, count: 32))
tails.append(encoding)
}
}
var headsConcatenated = Data()
for h in heads {
headsConcatenated.append(h)
}
var tailsPointer = BigUInt(headsConcatenated.count)
headsConcatenated = Data()
var tailsConcatenated = Data()
for i in 0 ..< subTypes.count {
let head = heads[i]
let tail = tails[i]
if !subTypes[i].isStatic {
guard let newHead = tailsPointer.abiEncode(bits: 256) else {return nil}
headsConcatenated.append(newHead)
tailsConcatenated.append(tail)
tailsPointer = tailsPointer + BigUInt(tail.count)
} else {
headsConcatenated.append(head)
tailsConcatenated.append(tail)
}
}
let total = headsConcatenated + tailsConcatenated
return total
case .function:
if let data = value as? Data {
return data.setLengthLeft(32)
}
}
return nil
}
}