rust-ethereum / ethabi

Encode and decode smart contract invocations
Apache License 2.0
517 stars 186 forks source link

`Function::decode_input` should assume that first 4 bytes are function signature hash #222

Open midnightexigent opened 3 years ago

midnightexigent commented 3 years ago

to solve the problem mentionned here #180

possible new implementation for Function::decode_input

    pub fn decode_input(&self, data: &[u8]) -> Result<Vec<Token>> {
        let signed = short_signature(&self.name, &self.input_params()).to_vec();
            if signed[..4] != data[..4] { return Err(".."); }
        decode(&self.input_param_types(), &data[4..])
    }
alex88 commented 2 years ago

Would also be nice to have a method that finds the function based on the input data by itself.

r2d2-rmbl commented 2 years ago

I had a struct layout like this: ((address,bytes),string,bytes32) for my encoded function call, so just trimming those 4 bytes didn't work for me. This is because we need to add a tuple offset for tuples with dynamic data.

I ended up having to

  1. trim the first 4 function bytes
  2. prepend '0000000000000000000000000000000000000000000000000000000000000020' to the encoded bytes' hexstring.