poanetwork / ex_abi

The Ethereum ABI Interface
GNU General Public License v3.0
61 stars 43 forks source link

Incorect output decoding when returned value matches the function signature. #173

Closed steffenix closed 2 months ago

steffenix commented 3 months ago

I am currently working with the erc-1271 standard and encountered an issue when calling the isValidSignature function from the contract. The function returns the bytes4 constant internal MAGICVALUE which is 0x1626ba7e. However, it appears that the method signature is 0x1626ba7e. It is being removed from the returned value before decoding, leading to decoding failures.

https://github.com/poanetwork/ex_abi/blob/5c8c205fe43786663a682bcb2de34e458ba0b150/lib/abi/type_decoder.ex#L154-L155

contract ERC1271 {

  // bytes4(keccak256("isValidSignature(bytes32,bytes)")
  bytes4 constant internal MAGICVALUE = 0x1626ba7e;

  function isValidSignature(
    bytes32 _hash, 
    bytes memory _signature)
    public
    view 
    returns (bytes4 magicValue);
}

My current workaround is, could you tell me if that makes sense to you?

    if rem(byte_size(encoded_data), 32) == 0 do
      decode_raw(encoded_data, types)
    else
      case ABI.Util.split_method_id(encoded_data) do
        {:ok, ^method_id, rest} ->
          decode_raw(rest, types)
        _ -> decode_raw(encoded_data, types)
      end
    end

I have made a PR if that is good for you: https://github.com/poanetwork/ex_abi/pull/174

ayrat555 commented 2 months ago

fixed by https://github.com/poanetwork/ex_abi/pull/174