Repository navigation
Expand file tree
/
Copy pathtype_decoder.ex
More file actions
373 lines (305 loc) · 11.1 KB
/
Copy pathtype_decoder.ex
File metadata and controls
373 lines (305 loc) · 11.1 KB
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
defmodule ABI.TypeDecoder do
@moduledoc """
`ABI.TypeDecoder` is responsible for decoding types to the format
expected by Solidity. We generally take a function selector and binary
data and decode that into the original arguments according to the
specification.
"""
alias ABI.FunctionSelector
@doc """
Decodes the given data based on the function selector.
Note, we don't currently try to guess the function name?
## Examples
iex> "00000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001"
...> |> Base.decode16!(case: :lower)
...> |> ABI.TypeDecoder.decode(
...> %ABI.FunctionSelector{
...> function: "baz",
...> types: [
...> {:uint, 32},
...> :bool
...> ],
...> returns: [:bool]
...> }
...> )
[69, true]
iex> "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd6"
...> |> Base.decode16!(case: :lower)
...> |> ABI.TypeDecoder.decode(
...> %ABI.FunctionSelector{
...> function: "baz",
...> types: [
...> {:int, 8}
...> ],
...> returns: [:int]
...> }
...> )
[-42]
iex> ABI.TypeEncoder.encode(["hello world"],[:string])
...> |> ABI.TypeDecoder.decode(
...> %ABI.FunctionSelector{
...> function: nil,
...> types: [
...> :string
...> ]
...> }
...> )
["hello world"]
iex> "00000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000001"
...> |> Base.decode16!(case: :lower)
...> |> ABI.TypeDecoder.decode(
...> %ABI.FunctionSelector{
...> function: nil,
...> types: [
...> {:tuple, [{:uint, 32}, :bool]}
...> ]
...> }
...> )
[{17, true}]
iex> ABI.TypeEncoder.encode([[17,1]],[{:array,{:uint,32}}])
...> |> ABI.TypeDecoder.decode(
...> %ABI.FunctionSelector{
...> function: nil,
...> types: [
...> {:array, {:uint, 32}}
...> ]
...> }
...> )
[[17, 1]]
iex> ABI.TypeEncoder.encode([[17, 1], true, <<16, 32>>], [{:array, {:uint, 32}},:bool,{:bytes, 2}])
...> |> ABI.TypeDecoder.decode(
...> %ABI.FunctionSelector{
...> function: nil,
...> types: [
...> {:array, {:uint, 32}},
...> :bool,
...> {:bytes, 2}
...> ]
...> }
...> )
[[17, 1], true, <<16, 32>>]
iex> ABI.TypeEncoder.encode([{"awesome", true}], [{:tuple, [:string, :bool]}])
...> |> ABI.TypeDecoder.decode(
...> %ABI.FunctionSelector{
...> function: nil,
...> types: [
...> {:tuple, [:string, :bool]}
...> ]
...> }
...> )
[{"awesome", true}]
iex> ABI.TypeEncoder.encode([{[]}],[{:tuple, [{:array, :address}]}])
...> |> ABI.TypeDecoder.decode(
...> %ABI.FunctionSelector{
...> function: nil,
...> types: [
...> {:tuple, [{:array, :address}]}
...> ]
...> }
...> )
[{[]}]
iex> ABI.TypeEncoder.encode( [{
...> "Unauthorized",
...> [
...> 184341788326688649239867304918349890235378717380,
...> 765664983403968947098136133435535343021479462042,
...> ]
...> }], [{:tuple,[:string, {:array, {:uint, 256}}]}])
...> |> ABI.TypeDecoder.decode(
...> %ABI.FunctionSelector{
...> function: nil,
...> types: [{:tuple,[
...> :string,
...> {:array, {:uint, 256}}
...> ]}]
...> }
...> )
[{
"Unauthorized",
[
184341788326688649239867304918349890235378717380,
765664983403968947098136133435535343021479462042,
]
}]
"""
def decode(encoded_data, selector_or_types, data_type \\ :input)
def decode(encoded_data, %FunctionSelector{types: types, method_id: method_id}, :input)
when is_binary(method_id) do
{:ok, ^method_id, rest} = ABI.Util.split_method_id(encoded_data)
decode_raw(rest, types)
end
def decode(encoded_data, %FunctionSelector{types: types}, :input) do
decode(encoded_data, types)
end
def decode(encoded_data, %FunctionSelector{returns: types, method_id: method_id}, :output)
when is_binary(method_id) do
case ABI.Util.split_method_id(encoded_data) do
{:ok, ^method_id, rest} -> decode_raw(rest, types)
_ -> decode_raw(encoded_data, types)
end
end
def decode(encoded_data, %FunctionSelector{returns: types}, :output) do
decode(encoded_data, types)
end
def decode(encoded_data, types, _) when is_list(types) do
decode_raw(encoded_data, types)
end
@doc """
Similar to `ABI.TypeDecoder.decode/2` except accepts a list of types instead
of a function selector.
## Examples
iex> ABI.TypeEncoder.encode([{"awesome", true}], [{:tuple, [:string, :bool]}])
...> |> ABI.TypeDecoder.decode_raw([{:tuple, [:string, :bool]}])
[{"awesome", true}]
"""
def decode_raw(binary_data, types) do
{result, _} = do_decode_raw(binary_data, types)
result
end
def do_decode_raw(binary_data, types) do
{reversed_result, binary_rest} =
Enum.reduce(types, {[], binary_data}, fn type, {acc, binary} ->
{value, rest} =
if FunctionSelector.is_dynamic?(type) do
decode_type(type, binary, binary_data)
else
decode_type(type, binary)
end
{[value | acc], rest}
end)
{Enum.reverse(reversed_result), binary_rest}
end
def mod(x, n) do
remainder = rem(x, n)
if (remainder < 0 and n > 0) or (remainder > 0 and n < 0),
do: n + remainder,
else: remainder
end
@spec decode_bytes(binary(), non_neg_integer(), :left) ::
{binary(), binary()}
def decode_bytes(data, size_in_bytes, :left) do
total_size_in_bytes = size_in_bytes + mod(32 - size_in_bytes, 32)
padding_size_in_bytes = total_size_in_bytes - size_in_bytes
<<_padding::binary-size(padding_size_in_bytes), value::binary-size(size_in_bytes),
rest::binary>> = data
{value, rest}
end
@spec decode_bytes(binary(), non_neg_integer(), :right, binary(), binary()) ::
{binary(), binary()}
def decode_bytes(_, size_in_bytes, :right, full_data, _) do
total_size_in_bytes = size_in_bytes + mod(32 - size_in_bytes, 32)
padding_size_in_bytes = total_size_in_bytes - size_in_bytes
<<value::binary-size(size_in_bytes), _padding::binary-size(padding_size_in_bytes),
rest2::binary>> = full_data
{value, rest2}
end
@spec decode_bytes(binary(), non_neg_integer(), :right, binary()) ::
{binary(), binary()}
def decode_bytes(data, size_in_bytes, :right, rest) do
total_size_in_bytes = size_in_bytes + mod(32 - size_in_bytes, 32)
padding_size_in_bytes = total_size_in_bytes - size_in_bytes
<<value::binary-size(size_in_bytes), _padding::binary-size(padding_size_in_bytes),
_rest::binary>> = data
{value, rest}
end
defp decode_type({:uint, size_in_bits}, data) do
decode_uint(data, size_in_bits)
end
defp decode_type({:int, size_in_bits}, data) do
decode_int(data, size_in_bits)
end
defp decode_type({:bytes, size}, data) when size > 0 and size <= 32 do
decode_bytes(data, size, :right, data, data)
end
defp decode_type({:array, type, size}, data) do
types = List.duplicate(type, size)
{tuple, bytes} = decode_type({:tuple, types}, data)
{Tuple.to_list(tuple), bytes}
end
defp decode_type({:tuple, types}, data) do
{reversed_result, _, binary} =
Enum.reduce(types, {[], [], data}, fn type, {acc, dynamic, binary} ->
if FunctionSelector.is_dynamic?(type) do
{val, binary} = decode_type(type, binary, data)
{[val | acc], [type | dynamic], binary}
else
{val, binary} = decode_type(type, binary)
{[val | acc], dynamic, binary}
end
end)
result_dynamic = []
{result, _} =
Enum.reduce(reversed_result, {[], result_dynamic}, fn
value, {acc, dynamic} -> {[value | acc], dynamic}
end)
{List.to_tuple(result), binary}
end
defp decode_type(:address, data), do: decode_bytes(data, 20, :left)
defp decode_type(:bool, data) do
{encoded_value, rest} = decode_uint(data, 8)
value =
case encoded_value do
1 -> true
0 -> false
end
{value, rest}
end
defp decode_type({:array, type}, data, full_data) do
{offset, rest_bytes} = decode_uint(data, 256)
<<_padding::binary-size(offset), rest_data::binary>> = full_data
{count, bytes} = decode_uint(rest_data, 256)
types = List.duplicate(type, count)
{tuple, _bytes} = decode_type({:tuple, types}, bytes)
{Tuple.to_list(tuple), rest_bytes}
end
defp decode_type({:array, type, size}, data, full_data) do
{offset, rest_bytes} = decode_uint(data, 256)
<<_padding::binary-size(offset), rest_data::binary>> = full_data
types = List.duplicate(type, size)
{tuple, _} = decode_type({:tuple, types}, rest_data)
{Tuple.to_list(tuple), rest_bytes}
end
defp decode_type({:tuple, types}, data, full_data) do
{offset, rest_bytes} = decode_uint(data, 256)
<<_padding::binary-size(offset), tuple_data::binary>> = full_data
{reversed_result, _, _binary} =
Enum.reduce(types, {[], [], tuple_data}, fn type, {acc, dynamic, binary} ->
if FunctionSelector.is_dynamic?(type) do
{val, binary} = decode_type(type, binary, tuple_data)
{[val | acc], [type | dynamic], binary}
else
{val, binary} = decode_type(type, binary)
{[val | acc], dynamic, binary}
end
end)
result_dynamic = []
{result, _} =
Enum.reduce(reversed_result, {[], result_dynamic}, fn
value, {acc, dynamic} -> {[value | acc], dynamic}
end)
{List.to_tuple(result), rest_bytes}
end
defp decode_type(:string, data, full_data) do
decode_type(:bytes, data, full_data)
end
defp decode_type(:bytes, data, full_data) do
{offset, rest} = decode_uint(data, 256)
<<_padding::binary-size(offset), rest_data::binary>> = full_data
{byte_size, dynamic_length_data} = decode_uint(rest_data, 256)
decode_bytes(dynamic_length_data, byte_size, :right, rest)
end
defp decode_type({:bytes, 0}, data, _),
do: {<<>>, data}
defp decode_type(els, _, _) do
raise "Unsupported decoding type: #{inspect(els)}"
end
defp decode_uint(data, size_in_bits) do
total_bit_size = size_in_bits + mod(256 - size_in_bits, 256)
<<value::integer-size(total_bit_size), rest::binary>> = data
{value, rest}
end
defp decode_int(data, _size_in_bits) do
<<value::signed-256, rest::binary>> = data
{value, rest}
end
end