diff --git a/src/utils/Misc.sol b/src/utils/Misc.sol new file mode 100644 index 0000000..d2f15c6 --- /dev/null +++ b/src/utils/Misc.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +library Misc { + /** + * @notice Returns the given bytes with the first 4 bytes removed. + */ + function stripSelector(bytes memory data) internal pure returns (bytes memory trimmedData) { + trimmedData = new bytes(data.length - 4); + assembly { + mstore(add(trimmedData, 0x20), sub(mload(add(data, 0x20)), 0x04)) + mstore(add(trimmedData, 0x20), mload(add(data, 0x24))) + } + } +} diff --git a/test/utils/Misc.t.sol b/test/utils/Misc.t.sol new file mode 100644 index 0000000..182642e --- /dev/null +++ b/test/utils/Misc.t.sol @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: Unlicense +pragma solidity ^0.8.13; + +import "forge-std/Test.sol"; +import "src/Test.sol"; +import "src/utils/Misc.sol"; + +contract MiscTest is Test { + function x(uint256 n) public pure returns (uint256) { + return n; + } + + function testStripSelector() public pure { + bytes memory suaveCalldata = abi.encodeWithSelector(this.x.selector, 123); + bytes memory strippedCalldata = Misc.stripSelector(suaveCalldata); + assertEq(strippedCalldata.length, 32); + uint256 num = abi.decode(strippedCalldata, (uint256)); + assertEq(num, 123); + } +}