diff --git a/lib/abi/type_decoder.ex b/lib/abi/type_decoder.ex index 2d9abf1..9bf8c14 100644 --- a/lib/abi/type_decoder.ex +++ b/lib/abi/type_decoder.ex @@ -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 @@ -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 @@ -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) + <> = 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`