module Eth::Abi::Decoder
def type(type, arg)
-
(DecodingError)
- if decoding fails for type.
Returns:
-
(String)
- the decoded data for the type.
Parameters:
-
arg
(String
) -- encoded type data string. -
type
(Eth::Abi::Type
) -- type to be decoded.
def type(type, arg) if %w(string bytes).include?(type.base_type) and type.sub_type.empty? # Case: decoding a string/bytes if type.dimensions.empty? l = Util.deserialize_big_endian_to_int arg[0, 32] data = arg[32..-1] raise DecodingError, "Wrong data size for string/bytes object" unless data.size == Util.ceil32(l) # decoded strings and bytes data[0, l] # Case: decoding array of string/bytes else l = Util.deserialize_big_endian_to_int arg[0, 32] # Decode each element of the array (1..l).map do |i| pointer = Util.deserialize_big_endian_to_int arg[i * 32, 32] # Pointer to the size of the array's element data_l = Util.deserialize_big_endian_to_int arg[32 + pointer, 32] # length of the element type(Type.parse(type.base_type), arg[pointer + 32, Util.ceil32(data_l) + 32]) end end elsif type.dynamic? l = Util.deserialize_big_endian_to_int arg[0, 32] nested_sub = type.nested_sub # ref https://github.com/ethereum/tests/issues/691 raise NotImplementedError, "Decoding dynamic arrays with nested dynamic sub-types is not implemented for ABI." if nested_sub.dynamic? # decoded dynamic-sized arrays (0...l).map { |i| type(nested_sub, arg[32 + nested_sub.size * i, nested_sub.size]) } elsif !type.dimensions.empty? l = type.dimensions.first nested_sub = type.nested_sub # decoded static-size arrays (0...l).map { |i| type(nested_sub, arg[nested_sub.size * i, nested_sub.size]) } else # decoded primitive types primitive_type type, arg end end