From 016fc15acf1910bf0cddfec8a6e7a90fb2452bad Mon Sep 17 00:00:00 2001 From: Peiling Ding Date: Sat, 20 Oct 2018 12:02:21 -0700 Subject: [PATCH 1/2] Add logic to decode signed integer. --- lib/abi/type_decoder.ex | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/abi/type_decoder.ex b/lib/abi/type_decoder.ex index 2d9abf1..87d7623 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 @@ -260,6 +265,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` From 6dc022d9affe5d02ecda31f9d9c8c17aa92e5b46 Mon Sep 17 00:00:00 2001 From: Peiling Ding Date: Sun, 28 Oct 2018 15:27:45 -0700 Subject: [PATCH 2/2] Add code to handle the case where the input data is not enough long to parse the array type arguments. --- lib/abi/type_decoder.ex | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/abi/type_decoder.ex b/lib/abi/type_decoder.ex index 87d7623..9bf8c14 100644 --- a/lib/abi/type_decoder.ex +++ b/lib/abi/type_decoder.ex @@ -209,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