From fa72e227d157a7a4e4c4112cb8503ee555f6a9b1 Mon Sep 17 00:00:00 2001 From: zeroXbrock <2791467+zeroXbrock@users.noreply.github.com> Date: Wed, 17 Jul 2024 18:11:57 -0700 Subject: [PATCH] add Misc lib w/ stripSelector --- src/utils/Misc.sol | 15 +++++++++++++++ test/utils/Misc.t.sol | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/utils/Misc.sol create mode 100644 test/utils/Misc.t.sol 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); + } +}