Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/abi/type_decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ defmodule ABI.TypeDecoder do
decode_uint(data, size_in_bits)
end

@spec decode_type(ABI.FunctionSelector.type(), binary()) :: {any(), binary()}
defp decode_type({:int, size_in_bits}, data) do
decode_int(data, size_in_bits)
end

defp decode_type(:address, data), do: decode_bytes(data, 20, :left)

defp decode_type(:bool, data) do
Expand Down Expand Up @@ -204,6 +209,11 @@ defmodule ABI.TypeDecoder do
decode_type({:array, type, element_count}, rest)
end

defp decode_type({:array, _type, element_count}, data)
when element_count * 32 > byte_size(data) do
raise("The input binary data is not long enough to use to decode the array.")
end

defp decode_type({:array, _type, 0}, data), do: {[], data}

defp decode_type({:array, type, element_count}, data) do
Expand Down Expand Up @@ -260,6 +270,14 @@ defmodule ABI.TypeDecoder do
{value, rest}
end

@spec decode_int(binary(), integer()) :: {integer(), binary()}
defp decode_int(data, size_in_bits) do
total_bit_size = size_in_bits + ExthCrypto.Math.mod(256 - size_in_bits, 256)
<<value::integer-signed-big-size(total_bit_size), rest::binary>> = data

{value, rest}
end

@spec decode_bytes(binary(), integer(), atom()) :: {binary(), binary()}
def decode_bytes(data, size_in_bytes, padding_direction) do
# TODO: Create `unright_pad` repo, err, add to `ExthCrypto.Math`
Expand Down