-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Remove abi.encodePacked #11593
Comments
Which types can be used with |
Arrays. https://docs.soliditylang.org/en/v0.8.6/abi-spec.html. What is the effect of this proposal on event index encoding? |
Event index encoding will not change, since we only plan to remove Is packed encoding of arrays used in the real world? |
One place where it can be used is for EIP-712 hashes - https://eips.ethereum.org/EIPS/eip-712#definition-of-encodedata. For example if there is a struct that contains an array: // SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract MultiTransferDemo {
struct MultiTransfer {
uint256 id;
Transfer[] transfers;
}
struct Transfer {
uint256 amount;
address recepient;
}
function hash(MultiTransfer memory mt) public pure returns (bytes32) {
return
keccak256(
abi.encode(
keccak256(
"MultiTransfer(uint256 id,Transfer[] transfers)Transfer(uint256 amount,address recepient)"
),
mt.id,
hashTransfers(mt.transfers)
)
);
}
function hashTransfers(Transfer[] memory transfers)
public
pure
returns (bytes32)
{
bytes32[] memory hashedTransfers = new bytes32[](transfers.length);
for (uint256 i = 0; i < transfers.length; i++) {
hashedTransfers[i] = hashTransfer(transfers[i]);
}
return keccak256(abi.encodePacked(hashedTransfers));
}
function hashTransfer(Transfer memory transfer)
public
pure
returns (bytes32)
{
return
keccak256(
abi.encode(
keccak256("Transfer(uint256 amount,address recepient)"),
transfer.amount,
transfer.recepient
)
);
}
}
It can also be implemented via bytes.concat, but I'm not sure which version has better performance: function hashTransfers2(Transfer[] memory transfers)
public
pure
returns (bytes32)
{
bytes memory encodedTransfers = new bytes(0);
for (uint256 i = 0; i < transfers.length; i++) {
encodedTransfers = bytes.concat(
encodedTransfers,
hashTransfer(transfers[i])
);
}
return keccak256(encodedTransfers);
} |
As a real world example, this use of Would need to use casting to do the same thing:
Would it be considered more 'idiomatic' to use bytes.concat and cast from |
I think that |
Curious on the current thinking around this. |
Current plan is to indeed remove |
#11593 is this issue :) |
I think it's a solution for the general case but I also wouldn't mind easing on the overly verbose conversions in select cases where they don't add anything. Even with some nicer conversion from #11284 this would be more verbose than it needs to be for clarity. |
What's your latest advice on how to convert an I'm still relying on |
I think abi.encodePacked is very convenient for concatenation. For instance, to concatenate like: abi.encodePacked(address, bytesN, uint256, …) |
The most common problem i have met is that how can i do this using other languages? such as js/golang? |
abi.encodePacked
was introduced in 0.4.22 as a backwards compatibility measure for the irregular "packed" encoding.Since then we have introduced
bytes.concat
(#10903) and learned that most projects (rightly) do not rely on the packed encoding, but only concatenation. I suggest we dropabi.encodePacked
in the next breaking release.(We could consider introducing a message with #11508 if we want to test the waters 😅 , but would rather refrain from this.)
The text was updated successfully, but these errors were encountered: