i am trying to encode a bool variable as callback data (bytes). i am doing this by using abi.encodePacked
for reduced space and cost and a custom decoding function.
The issue is that after decoding, the returned variable is always true no matter what the packed encoded value is.
here is the code :
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Ballot
* @dev Implements voting process along with vote delegation
*/
contract TEST {
function encodeBytes() public pure returns (bytes memory){
bool x = false;
return abi.encodePacked(x);
}
function decode(bytes memory data) public pure returns ( bool b) {
assembly {
b := mload(
add(
data,
8
)
)
}
}
function decodeAndReturn() public returns (bool){
bytes memory packed = encodeBytes();
return decode(packed);
}
}
calling decodeAndReturn
would return true even if the encoded variable is explicitly set to false.
is the issue with my decoding function ?