Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ABI encodePacked with < 256 bit integers #2494

Open
rellfy opened this issue Jul 2, 2023 · 2 comments
Open

ABI encodePacked with < 256 bit integers #2494

rellfy opened this issue Jul 2, 2023 · 2 comments

Comments

@rellfy
Copy link

rellfy commented Jul 2, 2023

It doesn't seem like it is currently possible to encodePacked an integer with less than 256 bits?

Additional abi::Token types could be added to handle these values, which would be redundant for abi.encode but not the packed variant.

From packed.rs:

Uint and Int tokens will be encoded using the least number of bits, so no padding will be
added by default.

If I am reading this right, it means that the caller has no control over the number of bits the integer will be encoded as, which doesn't seem ideal as the EVM could be expecting a certain number of bits regardless of the value passed, such as for signature validation.

@ratankaliani
Copy link

Agreed, this implementation is incorrect. When encoding Uint's that are not U256 (ex. U64, U32), into_token within encode_packed does not encode the parameters correctly.

Ex.
For abi.encodePacked(uint64, uint32), this is the expected output:

uint64 authoritySetId = 4;
uint32 trustedBlock = 10;
bytes memory encodedBytes = abi.encodePacked(authoritySetId,trustedBlock);
console.logBytes(encodedBytes);

Output:
0x00000000000000040000000a

However, when invoking the same with ethers-rs, this is the output:

let authority_set_id: u64 = 4;
let trusted_block_number: u32 = 10;

let input = encode_packed(&[
    authority_set_id.into_token(),
    trusted_block_number.into_token(),
]).expect("Failed to encode packed data.");
println!("input {:?}", hex::encode(input));

Output:
0x040a

The reason this is the case is that ethabi is the library that ethers-rs uses for encodePacked, and its implementation is deprecated.

Can we move ethers-rs ABI encoding to alloy-rs, which does abi.encodePacked here?

@ratankaliani
Copy link

ratankaliani commented Oct 31, 2023

Honestly, would be good to use alloy-rs either way, as ethabi is not actively maintained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants